diff --git a/packages/store/__tests__/chain-wallet-store.test.ts b/packages/store/__tests__/chain-wallet-store.test.ts index 7f005171b..087e46599 100644 --- a/packages/store/__tests__/chain-wallet-store.test.ts +++ b/packages/store/__tests__/chain-wallet-store.test.ts @@ -128,7 +128,7 @@ describe('ChainWalletStore', () => { const walletState = chainWalletStore.walletState; expect(walletState).toBe(WalletState.Connected); - expect(mockInterchainStore.getChainWalletState).toHaveBeenCalledWith('cosmoshub', 'keplr'); + expect(mockInterchainStore.getChainWalletState).toHaveBeenCalledWith('keplr', 'cosmoshub'); }); it('should return NotExist when no state exists', () => { @@ -161,6 +161,7 @@ describe('ChainWalletStore', () => { const errorMessage = chainWalletStore.errorMessage; expect(errorMessage).toBe('Connection failed'); + expect(mockInterchainStore.getChainWalletState).toHaveBeenCalledWith('keplr', 'cosmoshub'); }); it('should return empty string when no state exists', () => { diff --git a/packages/store/__tests__/wallet-manager-store.test.ts b/packages/store/__tests__/wallet-manager-store.test.ts index 1fb41f5d0..d83717c5d 100644 --- a/packages/store/__tests__/wallet-manager-store.test.ts +++ b/packages/store/__tests__/wallet-manager-store.test.ts @@ -217,6 +217,39 @@ describe('WalletManagerStore', () => { currentChainName: savedState.currentChainName, }); }); + it('should restore a saved NotExist state as Disconnected', async () => { + const savedState: { chainWalletStates: ChainWalletState[]; currentWalletName: string; currentChainName: string } = { + chainWalletStates: [ + { + chainName: 'cosmoshub', + walletName: 'keplr', + walletState: WalletState.NotExist, + rpcEndpoint: '', + errorMessage: 'Client not exist', + account: undefined, + }, + ], + currentWalletName: '', + currentChainName: '', + }; + + mockLocalStorage.load.mockReturnValue(savedState); + + await walletManagerStore.init(); + + // restore() writes the saved state first; later calls only add or drop entries. + expect(mockInterchainStore.setState).toHaveBeenNthCalledWith(1, { + chainWalletStates: [ + { + ...savedState.chainWalletStates[0], + walletState: WalletState.Disconnected, + errorMessage: '', + }, + ], + currentWalletName: '', + currentChainName: '', + }); + }); }); describe('State Management', () => { diff --git a/packages/store/src/wallet-manager/chain-wallet-store.ts b/packages/store/src/wallet-manager/chain-wallet-store.ts index 9dada020f..f51244da3 100644 --- a/packages/store/src/wallet-manager/chain-wallet-store.ts +++ b/packages/store/src/wallet-manager/chain-wallet-store.ts @@ -25,11 +25,11 @@ export class ChainWalletStore extends BaseWallet { } get walletState(): WalletState { - return this.store.getChainWalletState(this.chain.chainName, this.wallet.info.name)?.walletState || WalletState.NotExist; + return this.store.getChainWalletState(this.wallet.info.name, this.chain.chainName)?.walletState || WalletState.NotExist; } get errorMessage(): string { - return this.store.getChainWalletState(this.chain.chainName, this.wallet.info.name)?.errorMessage || ''; + return this.store.getChainWalletState(this.wallet.info.name, this.chain.chainName)?.errorMessage || ''; } async init(): Promise { diff --git a/packages/store/src/wallet-manager/wallet-manager-store.ts b/packages/store/src/wallet-manager/wallet-manager-store.ts index 3a26ac587..1c1c05931 100644 --- a/packages/store/src/wallet-manager/wallet-manager-store.ts +++ b/packages/store/src/wallet-manager/wallet-manager-store.ts @@ -72,7 +72,12 @@ export class WalletManagerStore implements WalletManager { const oldState = this.localStorage.load(); this.store.setState({ - chainWalletStates: oldState.chainWalletStates || [], + // NotExist only means the extension was not injected yet on the load that saved it. + // Restoring it would block connect() for good, so start that wallet over as Disconnected; + // init() sets NotExist again if the extension is really missing. + chainWalletStates: (oldState.chainWalletStates || []).map(cws => cws.walletState === WalletState.NotExist + ? { ...cws, walletState: WalletState.Disconnected, errorMessage: '' } + : cws), currentWalletName: oldState.currentWalletName || '', currentChainName: oldState.currentChainName || '' });