Skip to content

Commit

Permalink
Reconnect WC v2
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Aug 1, 2023
1 parent 063194c commit 0f17711
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/utils/__tests__/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,23 @@ describe('utils/wallet', () => {
expect(result).toBe(true)
})

it('should check if WalletConnect is unlocked', async () => {
it('should check if WalletConnect v1 is unlocked', async () => {
window.localStorage.setItem('walletconnect', 'true')

expect(await isWalletUnlocked(WalletNames.WALLET_CONNECT)).toBe(true)
expect(await isWalletUnlocked(WalletNames.WALLET_CONNECT_V2)).toBe(true)

window.localStorage.removeItem('walletconnect')

expect(await isWalletUnlocked(WalletNames.WALLET_CONNECT)).toBe(false)
})

it('should check if WalletConnect v2 is unlocked', async () => {
window.localStorage.setItem('wc@2:client:0.3//session', '[{"topic":"123"}]')

expect(await isWalletUnlocked(WalletNames.WALLET_CONNECT_V2)).toBe(true)

window.localStorage.removeItem('wc@2:client:0.3//session')

expect(await isWalletUnlocked(WalletNames.WALLET_CONNECT_V2)).toBe(false)
})

Expand Down
10 changes: 8 additions & 2 deletions src/utils/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ export const isWalletUnlocked = async (walletName: string): Promise<boolean> =>
return (await window.ethereum?._metamask?.isUnlocked?.()) || false
}

// Wallet connect creates a localStorage entry when connected and removes it when disconnected
if (walletName === WalletNames.WALLET_CONNECT || walletName === WalletNames.WALLET_CONNECT_V2) {
// WalletConnect v2
if (walletName === WalletNames.WALLET_CONNECT_V2) {
const wcSession = window.localStorage.getItem('wc@2:client:0.3//session')
return !!wcSession && wcSession !== '[]'
}

// WalletConnect v1 creates a localStorage entry when connected and removes it when disconnected
if (walletName === WalletNames.WALLET_CONNECT) {
return window.localStorage.getItem('walletconnect') !== null
}

Expand Down

0 comments on commit 0f17711

Please sign in to comment.