Skip to content

Commit c2e4958

Browse files
committed
only connect to the specific accounts loaded from storage on restore
1 parent 0dcba25 commit c2e4958

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

packages/core/src/manager.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ export class WalletManager extends StateBase {
348348

349349
private _reconnect = async (
350350
walletName: WalletName,
351-
checkConnection = false
351+
checkConnection = false,
352+
onlyChainNames?: ChainName[]
352353
) => {
353354
if (
354355
checkConnection &&
@@ -359,9 +360,21 @@ export class WalletManager extends StateBase {
359360
this.logger?.debug('[Event Emit] `refresh_connection` (manager)');
360361
this.coreEmitter.emit('refresh_connection');
361362
await this.getMainWallet(walletName).connect();
362-
await this.getMainWallet(walletName)
363-
.getChainWalletList(true)[0]
364-
?.connect(true);
363+
await Promise.all(
364+
this.getMainWallet(walletName)
365+
.getChainWalletList(true)
366+
.map((w, index) =>
367+
!onlyChainNames
368+
? // If no chains specified, just connect the first chain wallet and sync to all active chain wallets.
369+
index === 0
370+
? w.connect(true)
371+
: undefined
372+
: // If chains specified, connect only the specified chains, without sync.
373+
onlyChainNames.includes(w.chainName)
374+
? w.connect(false)
375+
: undefined
376+
)
377+
);
365378
};
366379

367380
private _restoreAccounts = async () => {
@@ -375,6 +388,7 @@ export class WalletManager extends StateBase {
375388
const mainWallet = this.getMainWallet(walletName);
376389
mainWallet.activate();
377390

391+
let onlyChainNames: ChainName[] | undefined;
378392
if (mainWallet.clientMutable.state === State.Done) {
379393
const accountsStr = window.localStorage.getItem(
380394
'cosmos-kit@2:core//accounts'
@@ -462,11 +476,15 @@ export class WalletManager extends StateBase {
462476
return;
463477
}
464478
}
479+
480+
onlyChainNames = connectedChainRecords.map(
481+
(chainRecord) => chainRecord.name
482+
);
465483
}
466484
}
467485

468486
if (mainWallet.walletInfo.mode !== 'wallet-connect') {
469-
await this._reconnect(walletName);
487+
await this._reconnect(walletName, undefined, onlyChainNames);
470488
}
471489
} catch (error) {
472490
if (error instanceof WalletNotProvidedError) {

0 commit comments

Comments
 (0)