Skip to content

Commit 481da35

Browse files
authored
fix(wallets): prevent WalletConnect v1 session data collision (#329)
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.
1 parent 9869279 commit 481da35

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

packages/use-wallet/src/__tests__/wallets/defly.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,29 @@ describe('DeflyWallet', () => {
449449
expect(store.state.wallets[WalletId.DEFLY]).toBeUndefined()
450450
expect(wallet.isConnected).toBe(false)
451451
})
452+
453+
it('should skip reconnectSession if Pera is active', async () => {
454+
const walletState: WalletState = {
455+
accounts: [account1],
456+
activeAccount: account1
457+
}
458+
459+
store = new Store<State>({
460+
...defaultState,
461+
activeWallet: WalletId.PERA,
462+
wallets: {
463+
[WalletId.DEFLY]: walletState
464+
}
465+
})
466+
467+
wallet = createWalletWithStore(store)
468+
469+
await wallet.resumeSession()
470+
471+
expect(mockLogger.info).toHaveBeenCalledWith('Skipping reconnectSession for Defly (inactive)')
472+
expect(mockDeflyWallet.reconnectSession).not.toHaveBeenCalled()
473+
expect(store.state.wallets[WalletId.DEFLY]).toEqual(walletState)
474+
})
452475
})
453476

454477
describe('setActive', () => {

packages/use-wallet/src/__tests__/wallets/pera.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,29 @@ describe('PeraWallet', () => {
449449
expect(store.state.wallets[WalletId.PERA]).toBeUndefined()
450450
expect(wallet.isConnected).toBe(false)
451451
})
452+
453+
it('should skip reconnectSession if Defly is active', async () => {
454+
const walletState: WalletState = {
455+
accounts: [account1],
456+
activeAccount: account1
457+
}
458+
459+
store = new Store<State>({
460+
...defaultState,
461+
activeWallet: WalletId.DEFLY,
462+
wallets: {
463+
[WalletId.PERA]: walletState
464+
}
465+
})
466+
467+
wallet = createWalletWithStore(store)
468+
469+
await wallet.resumeSession()
470+
471+
expect(mockLogger.info).toHaveBeenCalledWith('Skipping reconnectSession for Pera (inactive)')
472+
expect(mockPeraWallet.reconnectSession).not.toHaveBeenCalled()
473+
expect(store.state.wallets[WalletId.PERA]).toEqual(walletState)
474+
})
452475
})
453476

454477
describe('setActive', () => {

packages/use-wallet/src/wallets/defly.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ export class DeflyWallet extends BaseWallet {
137137
return
138138
}
139139

140+
// If Pera is active, skip reconnectSession for Defly
141+
if (state.activeWallet === WalletId.PERA) {
142+
this.logger.info('Skipping reconnectSession for Defly (inactive)')
143+
return
144+
}
145+
140146
this.logger.info('Resuming session...')
141147

142148
const client = this.client || (await this.initializeClient())

packages/use-wallet/src/wallets/pera.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ export class PeraWallet extends BaseWallet {
142142
return
143143
}
144144

145+
// If Defly is active, skip reconnectSession for Pera
146+
if (state.activeWallet === WalletId.DEFLY) {
147+
this.logger.info('Skipping reconnectSession for Pera (inactive)')
148+
return
149+
}
150+
145151
this.logger.info('Resuming session...')
146152

147153
const client = this.client || (await this.initializeClient())

0 commit comments

Comments
 (0)