Skip to content

Commit 14069cb

Browse files
committed
fix(sdk): disconnect smart wallet when signer is disconnected
changeset
1 parent 9d8ecce commit 14069cb

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

.changeset/strong-meals-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fix: Disconnect smart account when account signer is disconnected

packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,13 @@ function DetailsModal(props: {
343343
props.onDisconnect(info);
344344
}
345345

346+
useEffect(() => {
347+
if (!activeAccount) {
348+
setIsOpen(false);
349+
props.closeModal();
350+
}
351+
}, [activeAccount, props.closeModal]);
352+
346353
const networkSwitcherButton = (
347354
<MenuButton
348355
type="button"

packages/thirdweb/src/wallets/manager/connection-manager.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe.runIf(process.env.TW_SECRET_KEY)("Connection Manager", () => {
4444
await manager.connect(wallet, { client, onConnect });
4545

4646
expect(onConnect).toHaveBeenCalledWith(wallet);
47-
expect(storage.setItem).toHaveBeenCalledWith(expect.any(String), wallet.id);
47+
expect(storage.setItem).toHaveBeenCalled();
4848
});
4949

5050
it("handleConnection should connect smart wallet", async () => {
@@ -65,4 +65,22 @@ describe.runIf(process.env.TW_SECRET_KEY)("Connection Manager", () => {
6565

6666
expect(manager.connectedWallets.getValue()).toContain(wallet);
6767
});
68+
69+
it("should disconnect the active wallet when the EOA disconnects", async () => {
70+
const manager = createConnectionManager(storage);
71+
72+
const smartWallet = await manager.handleConnection(wallet, {
73+
client,
74+
accountAbstraction: smartWalletOptions,
75+
});
76+
77+
expect(manager.activeWalletStore.getValue()).toBe(smartWallet);
78+
79+
await wallet.disconnect();
80+
81+
expect(wallet.subscribe).toHaveBeenCalledWith(
82+
"disconnect",
83+
expect.any(Function),
84+
);
85+
});
6886
});

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export function createConnectionManager(storage: AsyncStorage) {
131131
const activeWallet = await (async () => {
132132
if (options?.accountAbstraction && !hasSmartAccount(wallet)) {
133133
return await handleSmartWalletConnection(
134-
account,
134+
wallet,
135135
options.client,
136136
options.accountAbstraction,
137137
);
@@ -140,12 +140,10 @@ export function createConnectionManager(storage: AsyncStorage) {
140140
}
141141
})();
142142

143-
// add personal wallet to connected wallets list
144-
addConnectedWallet(wallet);
143+
await storage.setItem(LAST_ACTIVE_EOA_ID, wallet.id);
145144

146-
if (wallet.id !== "smart") {
147-
await storage.setItem(LAST_ACTIVE_EOA_ID, wallet.id);
148-
}
145+
// add personal wallet to connected wallets list even if it's not the active one
146+
addConnectedWallet(wallet);
149147

150148
handleSetActiveWallet(activeWallet);
151149

@@ -159,10 +157,15 @@ export function createConnectionManager(storage: AsyncStorage) {
159157
};
160158

161159
const handleSmartWalletConnection = async (
162-
signer: Account,
160+
eoaWallet: Wallet,
163161
client: ThirdwebClient,
164162
options: SmartWalletOptions,
165163
) => {
164+
const signer = eoaWallet.getAccount();
165+
if (!signer) {
166+
throw new Error("Can not set a wallet without an account as active");
167+
}
168+
166169
const wallet = smartWallet(options);
167170

168171
await wallet.connect({
@@ -171,6 +174,15 @@ export function createConnectionManager(storage: AsyncStorage) {
171174
chain: options.chain,
172175
});
173176

177+
// Disconnect the active wallet when the EOA disconnects if it the active wallet is a smart wallet
178+
const disconnectUnsub = eoaWallet.subscribe("disconnect", () => {
179+
handleDisconnect();
180+
});
181+
const handleDisconnect = () => {
182+
disconnectUnsub();
183+
onWalletDisconnect(wallet);
184+
};
185+
174186
return wallet;
175187
};
176188

0 commit comments

Comments
 (0)