thirdweb 5.105.45 → 5.105.46

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 (44) hide show
  1. package/dist/cjs/react/core/hooks/transaction/useSendTransaction.js +4 -0
  2. package/dist/cjs/react/core/hooks/transaction/useSendTransaction.js.map +1 -1
  3. package/dist/cjs/version.js +1 -1
  4. package/dist/cjs/wallets/create-wallet.js +2 -0
  5. package/dist/cjs/wallets/create-wallet.js.map +1 -1
  6. package/dist/cjs/wallets/in-app/core/authentication/siwe.js +5 -3
  7. package/dist/cjs/wallets/in-app/core/authentication/siwe.js.map +1 -1
  8. package/dist/cjs/wallets/in-app/native/native-connector.js +0 -1
  9. package/dist/cjs/wallets/in-app/native/native-connector.js.map +1 -1
  10. package/dist/cjs/wallets/in-app/web/lib/web-connector.js +0 -1
  11. package/dist/cjs/wallets/in-app/web/lib/web-connector.js.map +1 -1
  12. package/dist/cjs/wallets/wallet-connect/controller.js +103 -11
  13. package/dist/cjs/wallets/wallet-connect/controller.js.map +1 -1
  14. package/dist/esm/react/core/hooks/transaction/useSendTransaction.js +4 -0
  15. package/dist/esm/react/core/hooks/transaction/useSendTransaction.js.map +1 -1
  16. package/dist/esm/version.js +1 -1
  17. package/dist/esm/wallets/create-wallet.js +2 -0
  18. package/dist/esm/wallets/create-wallet.js.map +1 -1
  19. package/dist/esm/wallets/in-app/core/authentication/siwe.js +5 -3
  20. package/dist/esm/wallets/in-app/core/authentication/siwe.js.map +1 -1
  21. package/dist/esm/wallets/in-app/native/native-connector.js +0 -1
  22. package/dist/esm/wallets/in-app/native/native-connector.js.map +1 -1
  23. package/dist/esm/wallets/in-app/web/lib/web-connector.js +0 -1
  24. package/dist/esm/wallets/in-app/web/lib/web-connector.js.map +1 -1
  25. package/dist/esm/wallets/wallet-connect/controller.js +103 -11
  26. package/dist/esm/wallets/wallet-connect/controller.js.map +1 -1
  27. package/dist/types/react/core/hooks/transaction/useSendTransaction.d.ts.map +1 -1
  28. package/dist/types/version.d.ts +1 -1
  29. package/dist/types/wallets/create-wallet.d.ts.map +1 -1
  30. package/dist/types/wallets/in-app/core/authentication/siwe.d.ts +0 -2
  31. package/dist/types/wallets/in-app/core/authentication/siwe.d.ts.map +1 -1
  32. package/dist/types/wallets/in-app/native/native-connector.d.ts.map +1 -1
  33. package/dist/types/wallets/in-app/web/lib/web-connector.d.ts.map +1 -1
  34. package/dist/types/wallets/wallet-connect/controller.d.ts.map +1 -1
  35. package/package.json +3 -3
  36. package/src/extensions/erc721/read/getNFT.test.ts +1 -1
  37. package/src/react/core/hooks/transaction/useSendTransaction.ts +5 -0
  38. package/src/version.ts +1 -1
  39. package/src/wallets/create-wallet.ts +2 -0
  40. package/src/wallets/in-app/core/authentication/siwe.ts +5 -5
  41. package/src/wallets/in-app/native/native-connector.ts +0 -1
  42. package/src/wallets/in-app/web/lib/web-connector.test.ts +0 -1
  43. package/src/wallets/in-app/web/lib/web-connector.ts +0 -1
  44. package/src/wallets/wallet-connect/controller.ts +117 -14
@@ -132,7 +132,7 @@ export async function connectWC(
132
132
  ...(wcOptions?.pairingTopic
133
133
  ? { pairingTopic: wcOptions?.pairingTopic }
134
134
  : {}),
135
- namespaces: {
135
+ optionalNamespaces: {
136
136
  [NAMESPACE]: {
137
137
  chains: chainsToRequest,
138
138
  events: ["chainChanged", "accountsChanged"],
@@ -157,14 +157,8 @@ export async function connectWC(
157
157
  );
158
158
  const currentChainId = chainsToRequest[0]?.split(":")[1] || 1;
159
159
  const providerChainId = normalizeChainId(currentChainId);
160
- const accounts: string[] = await provider.request(
161
- {
162
- method: "eth_requestAccounts",
163
- params: [],
164
- },
165
- `eip155:${providerChainId}`,
166
- );
167
- const address = accounts[0];
160
+ const account = firstAccountOn(provider.session, `eip155:1`); // grab the address from mainnet
161
+ const address = account;
168
162
  if (!address) {
169
163
  throw new Error("No accounts found on provider.");
170
164
  }
@@ -202,6 +196,109 @@ export async function connectWC(
202
196
  );
203
197
  }
204
198
 
199
+ async function ensureTargetChain(
200
+ provider: Awaited<ReturnType<typeof initProvider>>,
201
+ chain: Chain,
202
+ walletInfo: WalletInfo,
203
+ ) {
204
+ if (!provider.session) {
205
+ throw new Error("No session found on provider.");
206
+ }
207
+ const TARGET_CAIP = `eip155:${chain.id}`;
208
+ const TARGET_HEX = numberToHex(chain.id);
209
+
210
+ // Fast path: already enabled
211
+ if (hasChainEnabled(provider.session, TARGET_CAIP)) {
212
+ provider.setDefaultChain(TARGET_CAIP);
213
+ return;
214
+ }
215
+
216
+ // 1) Try switch
217
+ try {
218
+ await requestAndOpenWallet({
219
+ provider,
220
+ payload: {
221
+ method: "wallet_switchEthereumChain",
222
+ params: [{ chainId: TARGET_HEX }],
223
+ },
224
+ chain: TARGET_CAIP, // route to target
225
+ walletInfo,
226
+ });
227
+ provider.setDefaultChain(TARGET_CAIP);
228
+ return;
229
+ } catch (err: any) {
230
+ const code = err?.code ?? err?.data?.originalError?.code;
231
+ // 4001 user rejected; stop
232
+ if (code === 4001) throw new Error("User rejected chain switch");
233
+ // fall through on 4902 or unknown -> try add
234
+ }
235
+
236
+ // 2) Add the chain via any chain we already have
237
+ const routeChain = anyRoutableChain(provider.session);
238
+ if (!routeChain)
239
+ throw new Error("No routable chain to send wallet_addEthereumChain");
240
+
241
+ try {
242
+ await requestAndOpenWallet({
243
+ provider,
244
+ payload: {
245
+ method: "wallet_addEthereumChain",
246
+ params: [
247
+ {
248
+ chainId: TARGET_HEX,
249
+ chainName: chain.name,
250
+ nativeCurrency: chain.nativeCurrency,
251
+ rpcUrls: [chain.rpc],
252
+ blockExplorerUrls: [chain.blockExplorers?.[0]?.url ?? ""],
253
+ },
254
+ ],
255
+ },
256
+ chain: routeChain, // route via known-good chain, not the target
257
+ walletInfo,
258
+ });
259
+ } catch (err: any) {
260
+ const code = err?.code ?? err?.data?.originalError?.code;
261
+ if (code === 4001) throw new Error("User rejected add chain");
262
+ throw new Error(`Add chain failed: ${err?.message || String(err)}`);
263
+ }
264
+
265
+ // 3) Re-try switch after add
266
+ await requestAndOpenWallet({
267
+ provider,
268
+ payload: {
269
+ method: "wallet_switchEthereumChain",
270
+ params: [{ chainId: TARGET_HEX }],
271
+ },
272
+ chain: TARGET_CAIP,
273
+ walletInfo,
274
+ });
275
+ provider.setDefaultChain(TARGET_CAIP);
276
+
277
+ // 4) Verify enablement
278
+ if (!hasChainEnabled(provider.session, TARGET_CAIP)) {
279
+ throw new Error("Target chain still not enabled by wallet");
280
+ }
281
+ }
282
+
283
+ type WCSession = Awaited<ReturnType<typeof UniversalProvider.init>>["session"];
284
+
285
+ function getNS(session: WCSession) {
286
+ return session?.namespaces?.eip155;
287
+ }
288
+ function hasChainEnabled(session: WCSession, caip: string) {
289
+ const ns = getNS(session);
290
+ return !!ns?.accounts?.some((a) => a.startsWith(`${caip}:`));
291
+ }
292
+ function firstAccountOn(session: WCSession, caip: string): string | null {
293
+ const ns = getNS(session);
294
+ const hit = ns?.accounts?.find((a) => a.startsWith(`${caip}:`));
295
+ return hit ? (hit.split(":")[2] ?? null) : null;
296
+ }
297
+ function anyRoutableChain(session: WCSession): string | null {
298
+ const ns = getNS(session);
299
+ return ns?.accounts?.[0]?.split(":")?.slice(0, 2)?.join(":") ?? null; // e.g. "eip155:1"
300
+ }
301
+
205
302
  /**
206
303
  * Auto connect to already connected wallet connect session.
207
304
  * @internal
@@ -545,14 +642,17 @@ function onConnect(
545
642
  account,
546
643
  chain,
547
644
  disconnect,
548
- (newChain) => switchChainWC(provider, newChain),
645
+ (newChain) => switchChainWC(provider, newChain, walletInfo),
549
646
  ];
550
647
  }
551
648
 
552
- async function switchChainWC(provider: WCProvider, chain: Chain) {
553
- const chainId = chain.id;
649
+ async function switchChainWC(
650
+ provider: WCProvider,
651
+ chain: Chain,
652
+ walletInfo: WalletInfo,
653
+ ) {
554
654
  try {
555
- provider.setDefaultChain(`eip155:${chainId}`);
655
+ await ensureTargetChain(provider, chain, walletInfo);
556
656
  } catch (error) {
557
657
  const message =
558
658
  typeof error === "string" ? error : (error as ProviderRpcError)?.message;
@@ -605,7 +705,10 @@ function getChainsToRequest(options: {
605
705
  chainIds.push(chain.id);
606
706
  }
607
707
 
608
- if (!options.chain && optionalChains.length === 0) {
708
+ // always include mainnet
709
+ // many wallets only support a handful of chains, but mainnet is always supported
710
+ // we will add additional chains in switchChain if needed
711
+ if (!chainIds.includes(1)) {
609
712
  rpcMap[1] = getCachedChain(1).rpc;
610
713
  chainIds.push(1);
611
714
  }