Skip to content

Commit

Permalink
fix(wallets): prevent WalletConnect v1 session data collision
Browse files Browse the repository at this point in the history
Skip `reconnectSession` for inactive Pera/Defly wallets since wallet state
is already managed by use-wallet. This prevents a bug where both wallets
share the same WalletConnect v1 storage key, causing the inactive wallet
to overwrite the active wallet's connected accounts on session resume.
  • Loading branch information
drichar committed Dec 18, 2024
1 parent 9869279 commit ffd6141
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/use-wallet/src/__tests__/wallets/defly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,29 @@ describe('DeflyWallet', () => {
expect(store.state.wallets[WalletId.DEFLY]).toBeUndefined()
expect(wallet.isConnected).toBe(false)
})

it('should skip reconnectSession if Pera is active', async () => {
const walletState: WalletState = {
accounts: [account1],
activeAccount: account1
}

store = new Store<State>({
...defaultState,
activeWallet: WalletId.PERA,
wallets: {
[WalletId.DEFLY]: walletState
}
})

wallet = createWalletWithStore(store)

await wallet.resumeSession()

expect(mockLogger.info).toHaveBeenCalledWith('Skipping reconnectSession for Defly (inactive)')
expect(mockDeflyWallet.reconnectSession).not.toHaveBeenCalled()
expect(store.state.wallets[WalletId.DEFLY]).toEqual(walletState)
})
})

describe('setActive', () => {
Expand Down
23 changes: 23 additions & 0 deletions packages/use-wallet/src/__tests__/wallets/pera.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,29 @@ describe('PeraWallet', () => {
expect(store.state.wallets[WalletId.PERA]).toBeUndefined()
expect(wallet.isConnected).toBe(false)
})

it('should skip reconnectSession if Defly is active', async () => {
const walletState: WalletState = {
accounts: [account1],
activeAccount: account1
}

store = new Store<State>({
...defaultState,
activeWallet: WalletId.DEFLY,
wallets: {
[WalletId.PERA]: walletState
}
})

wallet = createWalletWithStore(store)

await wallet.resumeSession()

expect(mockLogger.info).toHaveBeenCalledWith('Skipping reconnectSession for Pera (inactive)')
expect(mockPeraWallet.reconnectSession).not.toHaveBeenCalled()
expect(store.state.wallets[WalletId.PERA]).toEqual(walletState)
})
})

describe('setActive', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/use-wallet/src/wallets/defly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ export class DeflyWallet extends BaseWallet {
return
}

// If Pera is active, skip reconnectSession for Defly
if (state.activeWallet === WalletId.PERA) {
this.logger.info('Skipping reconnectSession for Defly (inactive)')
return
}

this.logger.info('Resuming session...')

const client = this.client || (await this.initializeClient())
Expand Down
6 changes: 6 additions & 0 deletions packages/use-wallet/src/wallets/pera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ export class PeraWallet extends BaseWallet {
return
}

// If Defly is active, skip reconnectSession for Pera
if (state.activeWallet === WalletId.DEFLY) {
this.logger.info('Skipping reconnectSession for Pera (inactive)')
return
}

this.logger.info('Resuming session...')

const client = this.client || (await this.initializeClient())
Expand Down

0 comments on commit ffd6141

Please sign in to comment.