From 0f17711e9e6550e60d9aaa8490bf508c9cec2fcf Mon Sep 17 00:00:00 2001 From: katspaugh Date: Tue, 1 Aug 2023 13:08:26 +0200 Subject: [PATCH] Reconnect WC v2 --- src/utils/__tests__/wallet.test.ts | 12 ++++++++++-- src/utils/wallets.ts | 10 ++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/utils/__tests__/wallet.test.ts b/src/utils/__tests__/wallet.test.ts index 91370a8135..10b8451497 100644 --- a/src/utils/__tests__/wallet.test.ts +++ b/src/utils/__tests__/wallet.test.ts @@ -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) }) diff --git a/src/utils/wallets.ts b/src/utils/wallets.ts index 5fb3485355..c73abac5ac 100644 --- a/src/utils/wallets.ts +++ b/src/utils/wallets.ts @@ -37,8 +37,14 @@ export const isWalletUnlocked = async (walletName: string): Promise => 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 }