Skip to content

Commit 0126f11

Browse files
authored
[WebGL] Support Smart Wallet Account Overrides (#129)
1 parent 3c0cab9 commit 0126f11

File tree

3 files changed

+55
-47
lines changed

3 files changed

+55
-47
lines changed

Assets/Thirdweb/Core/Plugin/thirdweb.jslib

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ var plugin = {
5959
email,
6060
personalWallet,
6161
authOptions,
62+
smartWalletAccountOverride,
6263
cb
6364
) {
6465
// convert taskId from pointer to str and allocate it to keep in memory
@@ -74,7 +75,8 @@ var plugin = {
7475
UTF8ToString(password),
7576
UTF8ToString(email),
7677
UTF8ToString(personalWallet),
77-
UTF8ToString(authOptions)
78+
UTF8ToString(authOptions),
79+
UTF8ToString(smartWalletAccountOverride)
7880
)
7981
.then((address) => {
8082
if (address) {

Assets/Thirdweb/Core/Scripts/Bridge.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ public static async Task<string> Connect(WalletConnection walletConnection)
9797
walletConnection.chainId.ToString(),
9898
string.IsNullOrEmpty(walletConnection.password) ? Utils.GetDeviceIdentifier() : walletConnection.password,
9999
walletConnection.email,
100-
walletConnection.personalWallet.ToString()[..1].ToLower() + walletConnection.personalWallet.ToString()[1..],
100+
walletConnection.personalWallet.ToString()[..1].ToLower() + walletConnection.personalWallet.ToString()[1..],
101101
JsonConvert.SerializeObject(walletConnection.authOptions),
102+
walletConnection.smartWalletAccountOverride,
102103
jsCallback
103104
);
104105
#endif
@@ -267,7 +268,7 @@ public static async Task<T> SmartWalletCreateSessionKey<T>(string options)
267268
[DllImport("__Internal")]
268269
private static extern string ThirdwebInitialize(string chainOrRPC, string options);
269270
[DllImport("__Internal")]
270-
private static extern string ThirdwebConnect(string taskId, string wallet, string chainId, string password, string email, string personalWallet, string authOptions, Action<string, string, string> cb);
271+
private static extern string ThirdwebConnect(string taskId, string wallet, string chainId, string password, string email, string personalWallet, string authOptions, string smartWalletAccountOverride, Action<string, string, string> cb);
271272
[DllImport("__Internal")]
272273
private static extern string ThirdwebDisconnect(string taskId, Action<string, string, string> cb);
273274
[DllImport("__Internal")]

Assets/WebGLTemplates/Thirdweb/lib/thirdweb-unity-bridge.js

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -347076,72 +347076,72 @@ Code: ${s}`;
347076347076
this.walletMap.set(s.id, o));
347077347077
}
347078347078
}
347079-
async connect(e = "injected", t, n, a, i = "localWallet", s) {
347079+
async connect(e = "injected", t, n, a, i = "localWallet", s, o) {
347080347080
if (!this.activeSDK) throw new Error("SDK not initialized");
347081-
let o = this.walletMap.get(e),
347082-
c = Number(t);
347083-
if (o) {
347081+
let c = this.walletMap.get(e),
347082+
u = Number(t);
347083+
if (c) {
347084347084
if (
347085-
(o.walletId === sb.walletIds.localWallet &&
347086-
(await this.initializeLocalWallet(n), o.connect({ chainId: c })),
347087-
o.walletId === sb.walletIds.embeddedWallet)
347085+
(c.walletId === sb.walletIds.localWallet &&
347086+
(await this.initializeLocalWallet(n), c.connect({ chainId: u })),
347087+
c.walletId === sb.walletIds.embeddedWallet)
347088347088
) {
347089-
let u = o,
347090-
l = JSON.parse(s || "{}");
347091-
if (l.authProvider === 0) {
347089+
let l = c,
347090+
f = JSON.parse(s || "{}");
347091+
if (f.authProvider === 0) {
347092347092
if (!a) throw new Error("Email is required for EmbeddedWallet");
347093-
let f = await u.authenticate({
347093+
let h = await l.authenticate({
347094347094
strategy: "iframe_email_verification",
347095347095
email: a,
347096347096
});
347097-
await u.connect({ chainId: c, authResult: f });
347098-
} else if (l.authProvider < 4) {
347099-
let f;
347100-
switch (l.authProvider) {
347097+
await l.connect({ chainId: u, authResult: h });
347098+
} else if (f.authProvider < 4) {
347099+
let h;
347100+
switch (f.authProvider) {
347101347101
case 1:
347102-
f = "google";
347102+
h = "google";
347103347103
break;
347104347104
case 2:
347105-
f = "apple";
347105+
h = "apple";
347106347106
break;
347107347107
case 3:
347108-
f = "facebook";
347108+
h = "facebook";
347109347109
break;
347110347110
default:
347111-
throw new Error("Invalid auth provider: " + l.authProvider);
347111+
throw new Error("Invalid auth provider: " + f.authProvider);
347112347112
}
347113-
let h = this.openPopupWindow();
347114-
if (!h) throw new Error("Failed to open login window");
347115-
let y = await u.authenticate({
347116-
strategy: f,
347117-
openedWindow: h,
347118-
closeOpenedWindow: (w) => {
347119-
w.close();
347113+
let y = this.openPopupWindow();
347114+
if (!y) throw new Error("Failed to open login window");
347115+
let w = await l.authenticate({
347116+
strategy: h,
347117+
openedWindow: y,
347118+
closeOpenedWindow: (S) => {
347119+
S.close();
347120347120
},
347121347121
});
347122-
await u.connect({ chainId: c, authResult: y });
347123-
} else if (l.authProvider === 4) {
347124-
let f = await u.authenticate({
347122+
await l.connect({ chainId: u, authResult: w });
347123+
} else if (f.authProvider === 4) {
347124+
let h = await l.authenticate({
347125347125
strategy: "jwt",
347126-
jwt: l.authToken,
347126+
jwt: f.authToken,
347127347127
});
347128-
await u.connect({ chainId: c, authResult: f });
347129-
} else throw new Error("Invalid auth provider: " + l.authProvider);
347130-
} else if (o.walletId === sb.walletIds.smartWallet) {
347131-
let u = o,
347132-
l = this.walletMap.get(i);
347128+
await l.connect({ chainId: u, authResult: h });
347129+
} else throw new Error("Invalid auth provider: " + f.authProvider);
347130+
} else if (c.walletId === sb.walletIds.smartWallet) {
347131+
let l = c,
347132+
f = this.walletMap.get(i);
347133347133
if (
347134-
(await this.connect(l?.walletId, t, n, a, i), this.activeWallet)
347134+
(await this.connect(f?.walletId, t, n, a, i), this.activeWallet)
347135347135
)
347136-
await this.initializeSmartWallet(u, this.activeWallet);
347136+
await this.initializeSmartWallet(l, this.activeWallet, o);
347137347137
else
347138347138
throw new Error(
347139347139
"Unable to connect EOA wallet to initialize smart wallet!"
347140347140
);
347141-
} else await o.connect({ chainId: c });
347141+
} else await c.connect({ chainId: u });
347142347142
return (
347143-
(this.activeWallet = o),
347144-
this.updateSDKSigner(await o.getSigner()),
347143+
(this.activeWallet = c),
347144+
this.updateSDKSigner(await c.getSigner()),
347145347145
await this.activeSDK.wallet.getAddress()
347146347146
);
347147347147
} else throw new Error("Invalid Wallet");
@@ -347310,10 +347310,15 @@ Code: ${s}`;
347310347310
.get(sb.walletIds.localWallet)
347311347311
.export({ strategy: "encryptedJson", password: e });
347312347312
}
347313-
async initializeSmartWallet(e, t) {
347314-
let n = await t.getAddress();
347315-
console.debug("Personal wallet address:", n),
347316-
await e.connect({ personalWallet: t }),
347313+
async initializeSmartWallet(e, t, n) {
347314+
n &&
347315+
console.debug(
347316+
"Initializing smart wallet with account address override:",
347317+
n
347318+
);
347319+
let a = await t.getAddress();
347320+
console.debug("Personal wallet address:", a),
347321+
await e.connect({ personalWallet: t, accountAddress: n }),
347317347322
e.listenerCount("disconnect") === 1 &&
347318347323
e.on("disconnect", () => {
347319347324
t.disconnect();

0 commit comments

Comments
 (0)