Skip to content

Commit f0fe1fd

Browse files
authored
Unity 4.11.0, EmbeddedWallet -> InAppWallet + fix 4.10 regression (#2765)
1 parent b22a572 commit f0fe1fd

File tree

2 files changed

+48
-47
lines changed

2 files changed

+48
-47
lines changed

.changeset/ten-balloons-dream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@thirdweb-dev/unity-js-bridge": patch
3+
---
4+
5+
Unity 4.11.0, EmbeddedWallet -> InAppWallet + fix 4.10 regression

packages/unity-js-bridge/src/thirdweb-bridge.ts

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ import {
1717
import { ThirdwebStorage } from "@thirdweb-dev/storage";
1818
import {
1919
type DAppMetaData,
20-
type EmbeddedWalletOauthStrategy,
20+
InAppWallet,
21+
type InAppWalletOauthStrategy,
2122
type SmartWalletConfig,
22-
walletIds,
2323
} from "@thirdweb-dev/wallets";
2424
import type { AbstractClientWallet } from "@thirdweb-dev/wallets/evm/wallets/base";
2525
import { CoinbaseWallet } from "@thirdweb-dev/wallets/evm/wallets/coinbase-wallet";
26-
import { EmbeddedWallet } from "@thirdweb-dev/wallets/evm/wallets/embedded-wallet";
2726
import { EthersWallet } from "@thirdweb-dev/wallets/evm/wallets/ethers";
2827
import { InjectedWallet } from "@thirdweb-dev/wallets/evm/wallets/injected";
2928
import { LocalWallet } from "@thirdweb-dev/wallets/evm/wallets/local-wallet";
@@ -66,30 +65,28 @@ const bigNumberReplacer = (_key: string, value: any) => {
6665
return value;
6766
};
6867

69-
const WALLETS = [
70-
MetaMaskWallet,
71-
InjectedWallet,
72-
WalletConnect,
73-
CoinbaseWallet,
74-
LocalWallet,
75-
SmartWallet,
76-
EmbeddedWallet,
68+
const SUPPORTED_WALLET_IDS = [
69+
"injected",
70+
"metamask",
71+
"walletConnect",
72+
"coinbase",
73+
"localWallet",
74+
"smartWallet",
75+
"inAppWallet",
7776
] as const;
7877

79-
type PossibleWallet = (typeof WALLETS)[number]["id"];
80-
8178
type FundWalletInput = FundWalletOptions & {
8279
appId: string;
8380
};
8481

8582
interface TWBridge {
8683
initialize: (chain: ChainIdOrName, options: string) => void;
8784
connect: (
88-
wallet: PossibleWallet,
85+
wallet: string,
8986
chainId: string,
9087
password?: string,
9188
email?: string,
92-
personalWallet?: PossibleWallet,
89+
personalWallet?: string,
9390
authOptions?: string,
9491
smartWalletAccountOverride?: string,
9592
) => Promise<string>;
@@ -174,10 +171,11 @@ class ThirdwebBridge implements TWBridge {
174171
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix use of any
175172
(globalThis as any).X_SDK_PLATFORM = "unity";
176173
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix use of any
177-
(globalThis as any).X_SDK_VERSION = "unity-4.10.1";
174+
(globalThis as any).X_SDK_VERSION = "unity-4.11.0";
178175
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix use of any
179176
(globalThis as any).X_SDK_OS = browser?.os ?? "unknown";
180177
}
178+
181179
this.initializedChain = chain;
182180
const sdkOptions = JSON.parse(options);
183181
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix use of any
@@ -215,7 +213,7 @@ class ThirdwebBridge implements TWBridge {
215213
clientId: sdkOptions.clientId,
216214
});
217215
this.activeSDK = new ThirdwebSDK(chain, sdkOptions, storage);
218-
for (const possibleWallet of WALLETS) {
216+
for (const walletId of SUPPORTED_WALLET_IDS) {
219217
let walletInstance: AbstractClientWallet;
220218
const dappMetadata: DAppMetaData = {
221219
name: sdkOptions.wallet?.appName || "thirdweb powered game",
@@ -224,22 +222,22 @@ class ThirdwebBridge implements TWBridge {
224222
logoUrl: sdkOptions.wallet?.appIcons?.[0] || "",
225223
...sdkOptions.wallet?.extras,
226224
};
227-
switch (possibleWallet.id) {
225+
switch (walletId) {
228226
case "injected":
229227
walletInstance = new InjectedWallet({
230228
dappMetadata,
231229
chains: supportedChains,
232230
clientId: sdkOptions.clientId,
233231
});
234232
break;
235-
case walletIds.metamask:
233+
case "metamask":
236234
walletInstance = new MetaMaskWallet({
237235
dappMetadata,
238236
chains: supportedChains,
239237
clientId: sdkOptions.clientId,
240238
});
241239
break;
242-
case walletIds.walletConnect:
240+
case "walletConnect":
243241
walletInstance = new WalletConnect({
244242
projectId: sdkOptions.wallet?.walletConnectProjectId,
245243
dappMetadata,
@@ -261,21 +259,21 @@ class ThirdwebBridge implements TWBridge {
261259
},
262260
});
263261
break;
264-
case walletIds.coinbase:
262+
case "coinbase":
265263
walletInstance = new CoinbaseWallet({
266264
dappMetadata,
267265
chains: supportedChains,
268266
clientId: sdkOptions.clientId,
269267
});
270268
break;
271-
case walletIds.localWallet:
269+
case "localWallet":
272270
walletInstance = new LocalWallet({
273271
dappMetadata,
274272
chains: supportedChains,
275273
clientId: sdkOptions.clientId,
276274
});
277275
break;
278-
case walletIds.smartWallet: {
276+
case "smartWallet": {
279277
const config: SmartWalletConfig = {
280278
chain: chain,
281279
factoryAddress: sdkOptions.smartWalletConfig?.factoryAddress,
@@ -292,16 +290,16 @@ class ThirdwebBridge implements TWBridge {
292290
walletInstance = new SmartWallet(config);
293291
break;
294292
}
295-
case walletIds.embeddedWallet:
296-
walletInstance = new EmbeddedWallet({
293+
case "inAppWallet":
294+
walletInstance = new InAppWallet({
297295
clientId: sdkOptions.clientId,
298296
chain: Ethereum,
299297
dappMetadata,
300298
chains: supportedChains,
301299
});
302300
break;
303301
default:
304-
throw new Error(`Unknown wallet type: ${possibleWallet.id}`);
302+
throw new Error(`Unknown wallet type: ${walletId}`);
305303
}
306304
if (walletInstance) {
307305
walletInstance.on("connect", async () =>
@@ -312,19 +310,19 @@ class ThirdwebBridge implements TWBridge {
312310
);
313311
walletInstance.on("disconnect", () => this.updateSDKSigner());
314312

315-
this.walletMap.set(possibleWallet.id, walletInstance);
313+
this.walletMap.set(walletId, walletInstance);
316314
}
317315
}
318316
}
319317

320318
public async connect(
321319
// biome-ignore lint/style/useDefaultParameterLast: would change the order of parameters to fix this
322-
wallet: PossibleWallet = "injected",
320+
wallet: string,
323321
chainId: string,
324322
password?: string,
325323
email?: string,
326324
phoneNumber?: string,
327-
personalWallet: PossibleWallet = "localWallet",
325+
personalWallet?: string,
328326
authOptions?: string,
329327
smartWalletAccountOverride?: string,
330328
) {
@@ -335,13 +333,13 @@ class ThirdwebBridge implements TWBridge {
335333
const chainIdNumber = Number(chainId);
336334
if (walletInstance) {
337335
// local wallet needs to be generated or loaded before connecting
338-
if (walletInstance.walletId === walletIds.localWallet) {
336+
if (wallet === "localWallet") {
339337
await this.initializeLocalWallet(password as string);
340338
walletInstance.connect({ chainId: chainIdNumber });
341339
}
342340

343-
if (walletInstance.walletId === walletIds.embeddedWallet) {
344-
const embeddedWallet = walletInstance as EmbeddedWallet;
341+
if (wallet === "inAppWallet") {
342+
const embeddedWallet = walletInstance as InAppWallet;
345343
const authOptionsParsed = JSON.parse(authOptions || "{}");
346344
if (authOptionsParsed.authProvider === 0) {
347345
// EmailOTP
@@ -358,7 +356,7 @@ class ThirdwebBridge implements TWBridge {
358356
});
359357
} else if (authOptionsParsed.authProvider < 4) {
360358
// OAuth
361-
let authProvider: EmbeddedWalletOauthStrategy;
359+
let authProvider: InAppWalletOauthStrategy;
362360
switch (authOptionsParsed.authProvider) {
363361
// Google
364362
case 1:
@@ -437,17 +435,21 @@ class ThirdwebBridge implements TWBridge {
437435
`Invalid auth provider: ${authOptionsParsed.authProvider}`,
438436
);
439437
}
440-
} else if (walletInstance.walletId === walletIds.smartWallet) {
438+
} else if (wallet === "smartWallet") {
439+
if (!personalWallet) {
440+
throw new Error("Personal wallet is required for smart wallet");
441+
}
441442
const smartWallet = walletInstance as SmartWallet;
442-
const eoaWallet = this.walletMap.get(personalWallet);
443443
// Connect flow for EOA first
444444
await this.connect(
445-
eoaWallet?.walletId,
445+
personalWallet,
446446
chainId,
447447
password,
448448
email,
449-
personalWallet,
449+
phoneNumber,
450+
undefined,
450451
authOptions,
452+
undefined,
451453
);
452454
await this.switchNetwork(chainId); // workaround for polygon/mumbai
453455
if (this.activeWallet) {
@@ -471,7 +473,7 @@ class ThirdwebBridge implements TWBridge {
471473
this.updateSDKSigner(await walletInstance.getSigner());
472474
return await this.activeSDK.wallet.getAddress();
473475
}
474-
throw new Error("Invalid Wallet");
476+
throw new Error(`This wallet is not supported in WebGL: ${wallet}`);
475477
}
476478

477479
public async disconnect() {
@@ -728,9 +730,7 @@ class ThirdwebBridge implements TWBridge {
728730
}
729731

730732
public async exportWallet(password: string): Promise<string> {
731-
const localWallet = this.walletMap.get(
732-
walletIds.localWallet,
733-
) as LocalWallet;
733+
const localWallet = this.walletMap.get("localWallet") as LocalWallet;
734734
return await localWallet.export({
735735
strategy: "encryptedJson",
736736
password: password,
@@ -754,9 +754,7 @@ class ThirdwebBridge implements TWBridge {
754754
}
755755

756756
public async initializeLocalWallet(password: string): Promise<LocalWallet> {
757-
const localWallet = this.walletMap.get(
758-
walletIds.localWallet,
759-
) as LocalWallet;
757+
const localWallet = this.walletMap.get("localWallet") as LocalWallet;
760758
try {
761759
await localWallet.loadOrCreate({
762760
strategy: "encryptedJson",
@@ -872,9 +870,7 @@ class ThirdwebBridge implements TWBridge {
872870
if (!this.activeWallet) {
873871
throw new Error("No wallet connected");
874872
}
875-
const embeddedWallet = this.walletMap.get(
876-
walletIds.embeddedWallet,
877-
) as EmbeddedWallet;
873+
const embeddedWallet = this.walletMap.get("inAppWallet") as InAppWallet;
878874
const email = await embeddedWallet.getEmail();
879875
return JSON.stringify({ result: email });
880876
}

0 commit comments

Comments
 (0)