thirdweb 5.29.3-nightly-b051d844d0bb2d08b0d885d8c23e75ee97209976-20240614190954 → 5.29.3-nightly-8b74602e3b7fbe011bf886b9d5ee0e2ea62f7447-20240614202910

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/dist/cjs/react/web/hooks/wallets/useAutoConnect.js +2 -2
  2. package/dist/cjs/react/web/hooks/wallets/useAutoConnect.js.map +1 -1
  3. package/dist/cjs/version.js +1 -1
  4. package/dist/cjs/wallets/create-wallet.js +9 -2
  5. package/dist/cjs/wallets/create-wallet.js.map +1 -1
  6. package/dist/cjs/wallets/native/create-wallet.js +6 -5
  7. package/dist/cjs/wallets/native/create-wallet.js.map +1 -1
  8. package/dist/cjs/wallets/wallet-connect/controller.js +33 -63
  9. package/dist/cjs/wallets/wallet-connect/controller.js.map +1 -1
  10. package/dist/esm/react/web/hooks/wallets/useAutoConnect.js +2 -2
  11. package/dist/esm/react/web/hooks/wallets/useAutoConnect.js.map +1 -1
  12. package/dist/esm/version.js +1 -1
  13. package/dist/esm/wallets/create-wallet.js +9 -2
  14. package/dist/esm/wallets/create-wallet.js.map +1 -1
  15. package/dist/esm/wallets/native/create-wallet.js +6 -5
  16. package/dist/esm/wallets/native/create-wallet.js.map +1 -1
  17. package/dist/esm/wallets/wallet-connect/controller.js +33 -63
  18. package/dist/esm/wallets/wallet-connect/controller.js.map +1 -1
  19. package/dist/types/version.d.ts +1 -1
  20. package/dist/types/wallets/create-wallet.d.ts.map +1 -1
  21. package/dist/types/wallets/native/create-wallet.d.ts.map +1 -1
  22. package/dist/types/wallets/wallet-connect/controller.d.ts +4 -3
  23. package/dist/types/wallets/wallet-connect/controller.d.ts.map +1 -1
  24. package/package.json +1 -1
  25. package/src/react/web/hooks/wallets/useAutoConnect.ts +2 -2
  26. package/src/version.ts +1 -1
  27. package/src/wallets/create-wallet.ts +12 -0
  28. package/src/wallets/native/create-wallet.ts +9 -5
  29. package/src/wallets/wallet-connect/controller.ts +40 -66
@@ -21,8 +21,6 @@ import {
21
21
  stringToHex,
22
22
  uint8ArrayToHex,
23
23
  } from "../../utils/encoding/hex.js";
24
- import { isAndroid, isIOS, isMobile } from "../../utils/web/isMobile.js";
25
- import { openWindow } from "../../utils/web/openWindow.js";
26
24
  import { getWalletInfo } from "../__generated__/getWalletInfo.js";
27
25
  import type { WCSupportedWalletIds } from "../__generated__/wallet-ids.js";
28
26
  import type {
@@ -39,20 +37,16 @@ import type { WCAutoConnectOptions, WCConnectOptions } from "./types.js";
39
37
 
40
38
  import type { ThirdwebClient } from "../../client/client.js";
41
39
  import { getAddress } from "../../utils/address.js";
42
- import { isReactNative } from "../../utils/platform.js";
43
40
  import { parseTypedData } from "../../utils/signatures/helpers/parseTypedData.js";
41
+ import type { AsyncStorage } from "../../utils/storage/AsyncStorage.js";
44
42
  import {
45
43
  getSavedConnectParamsFromStorage,
46
44
  saveConnectParamsToStorage,
47
45
  } from "../../utils/storage/walletStorage.js";
48
- import { webLocalStorage } from "../../utils/storage/webStorage.js";
49
46
  import { formatWalletConnectUrl } from "../../utils/url.js";
50
47
  import type { WalletId } from "../wallet-types.js";
51
48
  import { DEFAULT_PROJECT_ID, NAMESPACE } from "./constants.js";
52
49
 
53
- // TODO (rn) inject this
54
- const asyncLocalStorage = webLocalStorage;
55
-
56
50
  type WCProvider = InstanceType<typeof EthereumProvider>;
57
51
 
58
52
  type SavedConnectParams = {
@@ -89,24 +83,26 @@ export async function connectWC(
89
83
  options: WCConnectOptions,
90
84
  emitter: WalletEmitter<WCSupportedWalletIds>,
91
85
  walletId: WCSupportedWalletIds | "walletConnect",
86
+ storage: AsyncStorage,
87
+ sessionHandler?: (uri: string) => void,
92
88
  ): Promise<ReturnType<typeof onConnect>> {
93
- const provider = await initProvider(options, walletId);
89
+ const provider = await initProvider(options, walletId, sessionHandler);
94
90
  const wcOptions = options.walletConnect;
95
91
 
96
92
  let { onDisplayUri } = wcOptions || {};
97
93
 
98
- if (isReactNative() && !onDisplayUri) {
94
+ // use default sessionHandler unless onDisplayUri is explicitly provided
95
+ if (!onDisplayUri && sessionHandler) {
99
96
  const walletInfo = await getWalletInfo(walletId);
100
- const nativeCallaback = (uri: string) => {
101
- const { Linking } = require("react-native");
97
+ const deeplinkHandler = (uri: string) => {
102
98
  const appUrl = walletInfo.mobile.native || walletInfo.mobile.universal;
103
99
  if (!appUrl) {
104
100
  throw new Error("No app url found for wallet connect to redirect to.");
105
101
  }
106
102
  const fullUrl = formatWalletConnectUrl(appUrl, uri).redirect;
107
- Linking.openURL(fullUrl);
103
+ sessionHandler(fullUrl);
108
104
  };
109
- onDisplayUri = nativeCallaback;
105
+ onDisplayUri = deeplinkHandler;
110
106
  }
111
107
 
112
108
  if (onDisplayUri) {
@@ -134,7 +130,7 @@ export async function connectWC(
134
130
  });
135
131
  }
136
132
 
137
- setRequestedChainsIds(chainsToRequest);
133
+ setRequestedChainsIds(chainsToRequest, storage);
138
134
  // If session exists and chains are authorized, enable provider for required chain
139
135
  const addresses = await provider.enable();
140
136
  const address = addresses[0];
@@ -156,8 +152,8 @@ export async function connectWC(
156
152
  pairingTopic: options.walletConnect?.pairingTopic,
157
153
  };
158
154
 
159
- if (asyncLocalStorage) {
160
- saveConnectParamsToStorage(asyncLocalStorage, walletId, savedParams);
155
+ if (storage) {
156
+ saveConnectParamsToStorage(storage, walletId, savedParams);
161
157
  }
162
158
  }
163
159
 
@@ -165,7 +161,7 @@ export async function connectWC(
165
161
  provider.events.removeListener("display_uri", wcOptions.onDisplayUri);
166
162
  }
167
163
 
168
- return onConnect(address, chain, provider, emitter);
164
+ return onConnect(address, chain, provider, emitter, storage);
169
165
  }
170
166
 
171
167
  /**
@@ -176,9 +172,11 @@ export async function autoConnectWC(
176
172
  options: WCAutoConnectOptions,
177
173
  emitter: WalletEmitter<WCSupportedWalletIds>,
178
174
  walletId: WCSupportedWalletIds | "walletConnect",
175
+ storage: AsyncStorage,
176
+ sessionHandler?: (uri: string) => void,
179
177
  ): Promise<ReturnType<typeof onConnect>> {
180
- const savedConnectParams: SavedConnectParams | null = asyncLocalStorage
181
- ? await getSavedConnectParamsFromStorage(asyncLocalStorage, walletId)
178
+ const savedConnectParams: SavedConnectParams | null = storage
179
+ ? await getSavedConnectParamsFromStorage(storage, walletId)
182
180
  : null;
183
181
 
184
182
  const provider = await initProvider(
@@ -196,6 +194,7 @@ export async function autoConnectWC(
196
194
  walletConnect: {},
197
195
  },
198
196
  walletId,
197
+ sessionHandler,
199
198
  true, // is auto connect
200
199
  );
201
200
 
@@ -212,7 +211,7 @@ export async function autoConnectWC(
212
211
  ? options.chain
213
212
  : getCachedChain(providerChainId);
214
213
 
215
- return onConnect(address, chain, provider, emitter);
214
+ return onConnect(address, chain, provider, emitter, storage);
216
215
  }
217
216
 
218
217
  // Connection utils -----------------------------------------------------------------------------------------------
@@ -220,6 +219,7 @@ export async function autoConnectWC(
220
219
  async function initProvider(
221
220
  options: WCConnectOptions,
222
221
  walletId: WCSupportedWalletIds | "walletConnect",
222
+ sessionRequestHandler?: (uri: string) => void,
223
223
  isAutoConnect = false,
224
224
  ) {
225
225
  const walletInfo = await getWalletInfo(walletId);
@@ -235,7 +235,7 @@ async function initProvider(
235
235
  });
236
236
 
237
237
  const provider = await EthereumProvider.init({
238
- showQrModal: isReactNative()
238
+ showQrModal: sessionRequestHandler
239
239
  ? false
240
240
  : wcOptions?.showQrModal === undefined
241
241
  ? defaultShowQrModal
@@ -277,41 +277,13 @@ async function initProvider(
277
277
 
278
278
  if (walletId !== "walletConnect") {
279
279
  function handleSessionRequest() {
280
- const preferNative =
280
+ const walletLinkToOpen =
281
281
  provider.session?.peer?.metadata?.redirect?.native ||
282
282
  walletInfo.mobile.native ||
283
283
  walletInfo.mobile.universal;
284
284
 
285
- if (isReactNative()) {
286
- const { Linking } = require("react-native");
287
- if (!preferNative) {
288
- throw new Error(
289
- "No app url found for wallet connect to redirect to.",
290
- );
291
- }
292
- Linking.openURL(preferNative);
293
- return;
294
- }
295
-
296
- if (!isMobile()) {
297
- return;
298
- }
299
-
300
- if (isAndroid()) {
301
- if (preferNative) {
302
- openWindow(preferNative);
303
- }
304
- } else if (isIOS()) {
305
- if (preferNative) {
306
- openWindow(preferNative);
307
- }
308
- } else {
309
- const preferUniversal =
310
- walletInfo.mobile.universal || walletInfo.mobile.native;
311
-
312
- if (preferUniversal) {
313
- openWindow(preferUniversal);
314
- }
285
+ if (sessionRequestHandler && walletLinkToOpen) {
286
+ sessionRequestHandler(walletLinkToOpen);
315
287
  }
316
288
  }
317
289
 
@@ -396,6 +368,7 @@ function onConnect(
396
368
  chain: Chain,
397
369
  provider: WCProvider,
398
370
  emitter: WalletEmitter<WCSupportedWalletIds>,
371
+ storage: AsyncStorage,
399
372
  ): [Account, Chain, DisconnectFn, SwitchChainFn] {
400
373
  const account = createAccount(provider, address);
401
374
 
@@ -407,8 +380,8 @@ function onConnect(
407
380
  }
408
381
 
409
382
  function onDisconnect() {
410
- setRequestedChainsIds([]);
411
- asyncLocalStorage?.removeItem(storageKeys.lastUsedChainId);
383
+ setRequestedChainsIds([], storage);
384
+ storage?.removeItem(storageKeys.lastUsedChainId);
412
385
  disconnect();
413
386
  emitter.emit("disconnect", undefined);
414
387
  }
@@ -426,7 +399,7 @@ function onConnect(
426
399
  function onChainChanged(newChainId: string) {
427
400
  const newChain = getCachedChain(normalizeChainId(newChainId));
428
401
  emitter.emit("chainChanged", newChain);
429
- asyncLocalStorage?.setItem(storageKeys.lastUsedChainId, String(newChainId));
402
+ storage?.setItem(storageKeys.lastUsedChainId, String(newChainId));
430
403
  }
431
404
 
432
405
  provider.on("accountsChanged", onAccountsChanged);
@@ -438,7 +411,7 @@ function onConnect(
438
411
  account,
439
412
  chain,
440
413
  disconnect,
441
- (newChain) => switchChainWC(provider, newChain),
414
+ (newChain) => switchChainWC(provider, newChain, storage),
442
415
  ];
443
416
  }
444
417
 
@@ -456,7 +429,11 @@ function getNamespaceChainsIds(provider: WCProvider): number[] {
456
429
  return chainIds ?? [];
457
430
  }
458
431
 
459
- async function switchChainWC(provider: WCProvider, chain: Chain) {
432
+ async function switchChainWC(
433
+ provider: WCProvider,
434
+ chain: Chain,
435
+ storage: AsyncStorage,
436
+ ) {
460
437
  const chainId = chain.id;
461
438
  try {
462
439
  const namespaceChains = getNamespaceChainsIds(provider);
@@ -481,9 +458,9 @@ async function switchChainWC(provider: WCProvider, chain: Chain) {
481
458
  },
482
459
  ],
483
460
  });
484
- const requestedChains = await getRequestedChainsIds();
461
+ const requestedChains = await getRequestedChainsIds(storage);
485
462
  requestedChains.push(chainId);
486
- setRequestedChainsIds(requestedChains);
463
+ setRequestedChainsIds(requestedChains, storage);
487
464
  }
488
465
  await provider.request({
489
466
  method: "wallet_switchEthereumChain",
@@ -504,19 +481,16 @@ async function switchChainWC(provider: WCProvider, chain: Chain) {
504
481
  * Set the requested chains to the storage.
505
482
  * @internal
506
483
  */
507
- function setRequestedChainsIds(chains: number[]) {
508
- asyncLocalStorage?.setItem(
509
- storageKeys.requestedChains,
510
- JSON.stringify(chains),
511
- );
484
+ function setRequestedChainsIds(chains: number[], storage: AsyncStorage) {
485
+ storage?.setItem(storageKeys.requestedChains, JSON.stringify(chains));
512
486
  }
513
487
 
514
488
  /**
515
489
  * Get the last requested chains from the storage.
516
490
  * @internal
517
491
  */
518
- async function getRequestedChainsIds(): Promise<number[]> {
519
- const data = await asyncLocalStorage?.getItem(storageKeys.requestedChains);
492
+ async function getRequestedChainsIds(storage: AsyncStorage): Promise<number[]> {
493
+ const data = await storage.getItem(storageKeys.requestedChains);
520
494
  return data ? JSON.parse(data) : [];
521
495
  }
522
496