Skip to content

Commit 1625b15

Browse files
[SDK] Fix wallet reconnection for previously connected wallets
1 parent 552f702 commit 1625b15

File tree

6 files changed

+17
-29
lines changed

6 files changed

+17
-29
lines changed

.changeset/clean-wings-repeat.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+
Always reconnect any previously connected wallet properly

packages/thirdweb/src/react/core/hooks/wallets/useAutoConnect.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export function useAutoConnectCore(
1414
storage: AsyncStorage,
1515
props: AutoConnectProps & { wallets: Wallet[] },
1616
createWalletFn: (id: WalletId) => Wallet,
17-
getInstalledWallets?: () => Wallet[],
1817
) {
1918
const manager = useConnectionManagerCtx("useAutoConnect");
2019
const { connect } = useConnect({
@@ -28,7 +27,6 @@ export function useAutoConnectCore(
2827
autoConnectCore({
2928
connectOverride: connect,
3029
createWalletFn,
31-
getInstalledWallets,
3230
manager,
3331
props,
3432
setLastAuthProvider,

packages/thirdweb/src/react/web/hooks/wallets/useAutoConnect.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { webLocalStorage } from "../../../../utils/storage/webStorage.js";
22
import type { AutoConnectProps } from "../../../../wallets/connection/types.js";
33
import { createWallet } from "../../../../wallets/create-wallet.js";
44
import { getDefaultWallets } from "../../../../wallets/defaultWallets.js";
5-
import { getInstalledWalletProviders } from "../../../../wallets/injected/mipdStore.js";
65
import { useAutoConnectCore } from "../../../core/hooks/wallets/useAutoConnect.js";
76

87
/**
@@ -34,15 +33,5 @@ export function useAutoConnect(props: AutoConnectProps) {
3433
wallets,
3534
},
3635
createWallet,
37-
() => {
38-
const specifiedWalletIds = new Set(wallets.map((x) => x.id));
39-
40-
// pass the wallets that are not already specified but are installed by the user
41-
const installedWallets = getInstalledWalletProviders()
42-
.filter((x) => !specifiedWalletIds.has(x.info.rdns))
43-
.map((x) => createWallet(x.info.rdns));
44-
45-
return installedWallets;
46-
},
4736
);
4837
}

packages/thirdweb/src/wallets/connection/autoConnect.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { webLocalStorage } from "../../utils/storage/webStorage.js";
22
import { createWallet } from "../create-wallet.js";
33
import { getDefaultWallets } from "../defaultWallets.js";
4-
import { getInstalledWalletProviders } from "../injected/mipdStore.js";
54
import type { Wallet } from "../interfaces/wallet.js";
65
import { createConnectionManager } from "../manager/index.js";
76
import { autoConnectCore } from "./autoConnectCore.js";
@@ -44,16 +43,6 @@ export async function autoConnect(
4443
const manager = createConnectionManager(webLocalStorage);
4544
const result = await autoConnectCore({
4645
createWalletFn: createWallet,
47-
getInstalledWallets: () => {
48-
const specifiedWalletIds = new Set(wallets.map((x) => x.id));
49-
50-
// pass the wallets that are not already specified but are installed by the user
51-
const installedWallets = getInstalledWalletProviders()
52-
.filter((x) => !specifiedWalletIds.has(x.info.rdns))
53-
.map((x) => createWallet(x.info.rdns));
54-
55-
return installedWallets;
56-
},
5746
manager,
5847
props: {
5948
...props,

packages/thirdweb/src/wallets/connection/autoConnectCore.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ type AutoConnectCoreProps = {
2828
connectOverride?: (
2929
walletOrFn: Wallet | (() => Promise<Wallet>),
3030
) => Promise<Wallet | null>;
31-
getInstalledWallets?: () => Wallet[];
3231
setLastAuthProvider?: (
3332
authProvider: AuthArgsType["strategy"],
3433
storage: AsyncStorage,
@@ -69,7 +68,6 @@ const _autoConnectCore = async ({
6968
createWalletFn,
7069
manager,
7170
connectOverride,
72-
getInstalledWallets,
7371
setLastAuthProvider,
7472
}: AutoConnectCoreProps): Promise<boolean> => {
7573
const { wallets, onConnect } = props;
@@ -120,7 +118,17 @@ const _autoConnectCore = async ({
120118
// in that case, we default to the passed chain to connect to
121119
const lastConnectedChain =
122120
(await getLastConnectedChain(storage)) || props.chain;
123-
const availableWallets = [...wallets, ...(getInstalledWallets?.() ?? [])];
121+
const availableWallets = lastConnectedWalletIds.map((id) => {
122+
const specifiedWallet = wallets.find((w) => w.id === id);
123+
if (specifiedWallet) {
124+
return specifiedWallet;
125+
}
126+
return createWalletFn(id as WalletId);
127+
});
128+
console.log(
129+
"availableWallets",
130+
availableWallets.map((w) => w.id),
131+
);
124132
const activeWallet =
125133
lastActiveWalletId &&
126134
(availableWallets.find((w) => w.id === lastActiveWalletId) ||

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,12 @@ export function createConnectionManager(storage: AsyncStorage) {
238238
// save last connected wallet ids to storage
239239
effect(
240240
async () => {
241-
const prevAccounts = (await getStoredConnectedWalletIds(storage)) || [];
242241
const accounts = connectedWallets.getValue();
243242
const ids = accounts.map((acc) => acc?.id).filter((c) => !!c) as string[];
244243

245244
storage.setItem(
246245
CONNECTED_WALLET_IDS,
247-
stringify(Array.from(new Set([...prevAccounts, ...ids]))),
246+
stringify(Array.from(new Set([...ids]))),
248247
);
249248
},
250249
[connectedWallets],

0 commit comments

Comments
 (0)