Skip to content

Commit 367576c

Browse files
MananTankjnsdls
andauthored
feat: always use new session when calling connect method for WC (#2637)
Co-authored-by: Jonas Daniels <jonas.daniels@outlook.com>
1 parent 6fc9afa commit 367576c

File tree

8 files changed

+49
-110
lines changed

8 files changed

+49
-110
lines changed

.changeset/wise-tigers-drive.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
- Always start a new session when calling `wallet.connect` for WalletConnect
6+
- Fix JSDoc for `useConnectedWallets`
7+
- Add `accountsChanged` event back to `Wallet`
8+
- `wallet.disconnect` method cleanups

packages/thirdweb/src/react/core/hooks/wallets/wallet-hooks.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ export function useActiveWalletChain() {
7070
}
7171

7272
/**
73-
* A hook that returns all connected accounts
74-
* @returns An array of all connected accounts
73+
* A hook that returns all connected wallets
74+
* @returns An array of all connected wallets
7575
* @example
7676
* ```jsx
77-
* import { useConnectedAccounts } from "thirdweb/react";
77+
* import { useConnectedWallets } from "thirdweb/react";
7878
*
79-
* const accounts = useConnectedAccounts();
79+
* const wallets = useConnectedWallets();
8080
* ```
8181
* @walletConnection
8282
*/

packages/thirdweb/src/wallets/coinbase/coinbaseSDKWallet.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,12 @@ function onConnect(
212212
},
213213
};
214214

215-
function disconnect() {
216-
if (!provider) {
217-
return;
218-
}
219-
provider.disconnect();
220-
provider.close();
215+
async function disconnect() {
221216
provider.removeListener("accountsChanged", onAccountsChanged);
222217
provider.removeListener("chainChanged", onChainChanged);
223218
provider.removeListener("disconnect", onDisconnect);
219+
provider.disconnect();
220+
await provider.close();
224221
}
225222

226223
function onDisconnect() {
@@ -235,6 +232,7 @@ function onConnect(
235232
address: getAddress(accounts[0]),
236233
};
237234
emitter.emit("accountChanged", newAccount);
235+
emitter.emit("accountsChanged", accounts);
238236
} else {
239237
onDisconnect();
240238
}

packages/thirdweb/src/wallets/create-wallet.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,15 @@ export function createWallet<const ID extends WalletId>(
7676
chain = newChain;
7777
});
7878

79-
const unsubscribeDisconnect = emitter.subscribe("disconnect", () => {
79+
function reset() {
8080
account = undefined;
8181
chain = undefined;
82-
// unsubscribe
82+
}
83+
84+
let handleDisconnect = async () => {};
85+
86+
const unsubscribeDisconnect = emitter.subscribe("disconnect", () => {
87+
reset();
8388
unsubscribeChain();
8489
unsubscribeDisconnect();
8590
});
@@ -91,10 +96,6 @@ export function createWallet<const ID extends WalletId>(
9196
let handleSwitchChain: (chain: Chain) => Promise<void> = async () => {
9297
throw new Error("Not implemented yet");
9398
};
94-
let handleDisconnect: () => void = () => {
95-
account = undefined;
96-
chain = undefined;
97-
};
9899

99100
const wallet: Wallet<ID> = {
100101
id,
@@ -177,11 +178,7 @@ export function createWallet<const ID extends WalletId>(
177178
// set the states
178179
account = connectedAccount;
179180
chain = connectedChain;
180-
handleDisconnect = () => {
181-
doDisconnect();
182-
account = undefined;
183-
chain = undefined;
184-
};
181+
handleDisconnect = doDisconnect;
185182
handleSwitchChain = doSwitchChain;
186183
trackConnect({
187184
client: wcOptions.client,
@@ -226,11 +223,7 @@ export function createWallet<const ID extends WalletId>(
226223
// set the states
227224
account = connectedAccount;
228225
chain = connectedChain;
229-
handleDisconnect = () => {
230-
doDisconnect();
231-
account = undefined;
232-
chain = undefined;
233-
};
226+
handleDisconnect = doDisconnect;
234227
handleSwitchChain = doSwitchChain;
235228
trackConnect({
236229
client: options.client,
@@ -243,7 +236,10 @@ export function createWallet<const ID extends WalletId>(
243236
throw new Error("Failed to connect");
244237
},
245238
// these get overridden in connect and autoConnect
246-
disconnect: async () => handleDisconnect(),
239+
disconnect: async () => {
240+
reset();
241+
await handleDisconnect();
242+
},
247243
switchChain: (c) => handleSwitchChain(c),
248244
};
249245
return wallet;
@@ -341,11 +337,11 @@ export function smartWallet(
341337
return account;
342338
},
343339
disconnect: async () => {
344-
const { disconnectSmartWallet } = await import("./smart/index.js");
345-
await disconnectSmartWallet(_smartWallet);
346340
account = undefined;
347341
chain = undefined;
348342
emitter.emit("disconnect", undefined);
343+
const { disconnectSmartWallet } = await import("./smart/index.js");
344+
await disconnectSmartWallet(_smartWallet);
349345
},
350346
switchChain: async () => {
351347
throw new Error("Not implemented yet");
@@ -444,10 +440,12 @@ function coinbaseWalletSDK(): Wallet<"com.coinbase.wallet"> {
444440
let account: Account | undefined = undefined;
445441
let chain: Chain | undefined = undefined;
446442

447-
let handleDisconnect: () => void = () => {
443+
function reset() {
448444
account = undefined;
449445
chain = undefined;
450-
};
446+
}
447+
448+
let handleDisconnect = async () => {};
451449

452450
let handleSwitchChain = async (newChain: Chain) => {
453451
chain = newChain;
@@ -461,8 +459,7 @@ function coinbaseWalletSDK(): Wallet<"com.coinbase.wallet"> {
461459
);
462460

463461
const unsubscribeDisconnect = emitter.subscribe("disconnect", () => {
464-
handleDisconnect();
465-
// unsubscribe
462+
reset();
466463
unsubscribeChainChanged();
467464
unsubscribeDisconnect();
468465
});
@@ -517,7 +514,8 @@ function coinbaseWalletSDK(): Wallet<"com.coinbase.wallet"> {
517514
return account;
518515
},
519516
disconnect: async () => {
520-
handleDisconnect();
517+
reset();
518+
await handleDisconnect();
521519
},
522520
switchChain: async (newChain) => {
523521
await handleSwitchChain(newChain);

packages/thirdweb/src/wallets/injected/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,7 @@ async function onConnect(
185185
},
186186
};
187187

188-
function disconnect() {
189-
if (!provider || !provider.removeListener) {
190-
return;
191-
}
188+
async function disconnect() {
192189
provider.removeListener("accountsChanged", onAccountsChanged);
193190
provider.removeListener("chainChanged", onChainChanged);
194191
provider.removeListener("disconnect", onDisconnect);
@@ -207,6 +204,7 @@ async function onConnect(
207204
};
208205

209206
emitter.emit("accountChanged", newAccount);
207+
emitter.emit("accountsChanged", accounts);
210208
} else {
211209
onDisconnect();
212210
}

packages/thirdweb/src/wallets/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ export type AppMetadata = {
1919
logoUrl?: string;
2020
};
2121

22-
export type DisconnectFn = () => void;
22+
export type DisconnectFn = () => Promise<void>;
2323
export type SwitchChainFn = (chain: Chain) => Promise<void>;

packages/thirdweb/src/wallets/wallet-connect/index.ts

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ const defaultWCProjectId = "08c4b07e3ad25f1a27c14a4e8cecb6f0";
5252
const NAMESPACE = "eip155";
5353
const ADD_ETH_CHAIN_METHOD = "wallet_addEthereumChain";
5454

55-
const isNewChainsStale = true;
5655
const defaultShowQrModal = true;
5756

5857
const storageKeys = {
@@ -69,12 +68,6 @@ export async function connectWC(
6968
walletId: WCSupportedWalletIds | "walletConnect",
7069
): Promise<ReturnType<typeof onConnect>> {
7170
const provider = await initProvider(options, walletId);
72-
73-
const _isChainsState = await isChainsStale(provider, [
74-
provider.chainId,
75-
...(options?.walletConnect?.optionalChains || []).map((c) => c.id),
76-
]);
77-
7871
const wcOptions = options.walletConnect;
7972

8073
const { onDisplayUri } = wcOptions || {};
@@ -91,19 +84,17 @@ export async function connectWC(
9184
optionalChains: options.walletConnect?.optionalChains,
9285
});
9386

94-
// If there no active session, or the chain is stale, force connect.
95-
if (!provider.session || _isChainsState) {
87+
if (provider.session) {
9688
await provider.connect({
9789
...(wcOptions?.pairingTopic
9890
? { pairingTopic: wcOptions?.pairingTopic }
9991
: {}),
10092
optionalChains: chainsToRequest,
10193
rpcMap: rpcMap,
10294
});
103-
104-
setRequestedChainsIds(chainsToRequest);
10595
}
10696

97+
setRequestedChainsIds(chainsToRequest);
10798
// If session exists and chains are authorized, enable provider for required chain
10899
const addresses = await provider.enable();
109100
const address = addresses[0];
@@ -174,23 +165,6 @@ export async function autoConnectWC(
174165
return onConnect(address, chain, provider, emitter);
175166
}
176167

177-
// /**
178-
// * @internal
179-
// */
180-
// export async function disconnectWC(wallet: Wallet<WCSupportedWalletIds>) {
181-
// const provider = walletToProviderMap.get(wallet);
182-
// // const storage = getWalletData(wallet)?.storage;
183-
184-
// onDisconnect(wallet);
185-
// // if (storage) {
186-
// // deleteConnectParamsFromStorage(storage, wallet.id);
187-
// // }
188-
189-
// if (provider) {
190-
// provider.disconnect();
191-
// }
192-
// }
193-
194168
// Connection utils -----------------------------------------------------------------------------------------------
195169

196170
async function initProvider(
@@ -238,9 +212,9 @@ async function initProvider(
238212

239213
// disconnect the provider if chains are stale when (if not auto connecting)
240214
if (!isAutoConnect) {
241-
const isStale = await isChainsStale(provider, chainsToRequest);
215+
// const isStale = await isChainsStale(provider, chainsToRequest);
242216

243-
if (isStale && provider.session) {
217+
if (provider.session) {
244218
await provider.disconnect();
245219
}
246220
}
@@ -341,14 +315,11 @@ function onConnect(
341315
},
342316
};
343317

344-
function disconnect() {
345-
if (!provider) {
346-
return;
347-
}
348-
provider.disconnect();
318+
async function disconnect() {
349319
provider.removeListener("accountsChanged", onAccountsChanged);
350320
provider.removeListener("chainChanged", onChainChanged);
351321
provider.removeListener("disconnect", onDisconnect);
322+
await provider.disconnect();
352323
}
353324

354325
function onDisconnect() {
@@ -365,6 +336,7 @@ function onConnect(
365336
address: getAddress(accounts[0]),
366337
};
367338
emitter.emit("accountChanged", newAccount);
339+
emitter.emit("accountsChanged", accounts);
368340
} else {
369341
onDisconnect();
370342
}
@@ -396,9 +368,6 @@ function getNamespaceMethods(provider: WCProvider) {
396368
}
397369

398370
function getNamespaceChainsIds(provider: WCProvider): number[] {
399-
if (!provider) {
400-
return [];
401-
}
402371
const chainIds = provider.session?.namespaces[NAMESPACE]?.chains?.map(
403372
(chain) => parseInt(chain.split(":")[1] || ""),
404373
);
@@ -450,39 +419,6 @@ async function switchChainWC(provider: WCProvider, chain: Chain) {
450419
}
451420
}
452421

453-
/**
454-
* if every chain requested were already requested earlier - then they are not stale
455-
* @param connectToChainId
456-
* @internal
457-
*/
458-
async function isChainsStale(provider: WCProvider, chains: number[]) {
459-
const namespaceMethods = getNamespaceMethods(provider);
460-
461-
// if chain adding method is available, then chains are not stale
462-
if (namespaceMethods.includes(ADD_ETH_CHAIN_METHOD)) {
463-
return false;
464-
}
465-
466-
// if new chains are considered stale, then return true
467-
if (!isNewChainsStale) {
468-
return false;
469-
}
470-
471-
const requestedChains = await getRequestedChainsIds();
472-
const namespaceChains = getNamespaceChainsIds(provider);
473-
474-
// if any of the requested chains are not in the namespace chains, then they are stale
475-
if (
476-
namespaceChains.length &&
477-
!namespaceChains.some((id) => chains.includes(id))
478-
) {
479-
return false;
480-
}
481-
482-
// if chain was requested earlier, then they are not stale
483-
return !chains.every((id) => requestedChains.includes(id));
484-
}
485-
486422
/**
487423
* Set the requested chains to the storage.
488424
* @internal

packages/thirdweb/src/wallets/wallet-emitter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { WalletAutoConnectionOption, WalletId } from "./wallet-types.js";
55

66
export type WalletEmitterEvents<TWalletId extends WalletId> = {
77
accountChanged: Account;
8+
accountsChanged: string[];
89
disconnect?: never;
910
chainChanged: Chain;
1011
onConnect: WalletAutoConnectionOption<TWalletId>;

0 commit comments

Comments
 (0)