-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SDK] Fix: Account switching with connected smart wallets (#5592)
Fixes #5537 <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on improving wallet connection handling in the `thirdweb` project, specifically when switching signer accounts and simplifying connection logic for smart wallets. ### Detailed summary - Updated `biome.json` to add `"noUselessElse": "off"`. - Modified wallet connection logic in `packages/thirdweb/src/wallets/manager/index.ts` for better handling of active wallets. - Added tests for connection management in `packages/thirdweb/src/wallets/manager/connection-manager.test.ts`. - Removed environment variable logging across multiple API routes. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
- Loading branch information
1 parent
ea855c6
commit c3d7b66
Showing
8 changed files
with
114 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"thirdweb": patch | ||
--- | ||
|
||
Properly updates active smart wallet when switching signer account on EOA wallet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/thirdweb/src/wallets/manager/connection-manager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
import { TEST_CLIENT } from "../../../test/src/test-clients.js"; | ||
import { TEST_ACCOUNT_A } from "../../../test/src/test-wallets.js"; | ||
import { sepolia } from "../../chains/chain-definitions/sepolia.js"; | ||
import type { ThirdwebClient } from "../../client/client.js"; | ||
import type { AsyncStorage } from "../../utils/storage/AsyncStorage.js"; | ||
import type { Account, Wallet } from "../interfaces/wallet.js"; | ||
import type { SmartWalletOptions } from "../smart/types.js"; | ||
import { createConnectionManager } from "./index.js"; | ||
|
||
describe.runIf(process.env.TW_SECRET_KEY)("Connection Manager", () => { | ||
let storage: AsyncStorage; | ||
let client: ThirdwebClient; | ||
let wallet: Wallet; | ||
let account: Account; | ||
let smartWalletOptions: SmartWalletOptions; | ||
|
||
beforeEach(() => { | ||
storage = { | ||
getItem: vi.fn(), | ||
setItem: vi.fn(), | ||
removeItem: vi.fn(), | ||
}; | ||
client = TEST_CLIENT; | ||
account = TEST_ACCOUNT_A; | ||
wallet = { | ||
id: "wallet-id", | ||
getAccount: vi.fn().mockReturnValue(account), | ||
subscribe: vi.fn(), | ||
disconnect: vi.fn(), | ||
switchChain: vi.fn(), | ||
getChain: vi.fn().mockReturnValue(sepolia), | ||
getConfig: vi.fn(), | ||
} as unknown as Wallet; | ||
smartWalletOptions = { | ||
chain: sepolia, | ||
} as SmartWalletOptions; | ||
}); | ||
|
||
it("connect should handle connection and call onConnect", async () => { | ||
const manager = createConnectionManager(storage); | ||
const onConnect = vi.fn(); | ||
|
||
await manager.connect(wallet, { client, onConnect }); | ||
|
||
expect(onConnect).toHaveBeenCalledWith(wallet); | ||
expect(storage.setItem).toHaveBeenCalledWith(expect.any(String), wallet.id); | ||
}); | ||
|
||
it("handleConnection should connect smart wallet", async () => { | ||
const manager = createConnectionManager(storage); | ||
|
||
const smartWallet = await manager.handleConnection(wallet, { | ||
client, | ||
accountAbstraction: smartWalletOptions, | ||
}); | ||
|
||
expect(manager.activeWalletStore.getValue()).toBe(smartWallet); | ||
}); | ||
|
||
it("handleConnection should add wallet to connected wallets", async () => { | ||
const manager = createConnectionManager(storage); | ||
|
||
await manager.handleConnection(wallet, { client }); | ||
|
||
expect(manager.connectedWallets.getValue()).toContain(wallet); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters