Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: re-connecting a locked wallet #2341

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/hooks/wallets/useOnboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,15 @@ export const useInitOnboard = () => {
}

// Connect to the last connected wallet
enableWallets().then(() => {
enableWallets().then(async () => {
if (onboard.state.get().wallets.length > 0) return

const label = lastWalletStorage.get()
if (!label) return
const isUnlocked = label && (await isWalletUnlocked(label))
if (!isUnlocked) return

isWalletUnlocked(label).then((isUnlocked) => {
isUnlocked &&
connectWallet(onboard, {
autoSelect: { label, disableModals: false },
})
connectWallet(onboard, {
autoSelect: { label, disableModals: true },
})
})
}, [chain, onboard])
Expand Down
61 changes: 61 additions & 0 deletions src/utils/__tests__/wallet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { isWalletUnlocked, WalletNames } from '../wallets'

describe('utils/wallet', () => {
it('should check if MetaMask is unlocked and return false', async () => {
// mock window.ethereum
Object.defineProperty(window, 'ethereum', {
value: {
isMetaMask: true,
selectedAddress: '0x123',
isConnected: () => true,
_metamask: {
isUnlocked: () => Promise.resolve(false),
},
},
writable: true,
})
const result = await isWalletUnlocked(WalletNames.METAMASK)
expect(result).toBe(false)
})

it('should check if MetaMask is unlocked and return true', async () => {
// mock window.ethereum
Object.defineProperty(window, 'ethereum', {
value: {
isMetaMask: true,
selectedAddress: '0x123',
isConnected: () => true,
_metamask: {
isUnlocked: () => Promise.resolve(true),
},
},
writable: true,
})
const result = await isWalletUnlocked(WalletNames.METAMASK)
expect(result).toBe(true)
})

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

expect(await isWalletUnlocked(WalletNames.WALLET_CONNECT)).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)
})

it('should return false for unhandled wallets', async () => {
expect(await isWalletUnlocked('PINEAPPLE')).toBe(false)
})
})
14 changes: 8 additions & 6 deletions src/utils/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ export const WalletNames = {
export const isWalletUnlocked = async (walletName: string): Promise<boolean> => {
if (typeof window === 'undefined') return false

if (window.ethereum?.isConnected?.()) {
return true
}

// Only MetaMask exposes a method to check if the wallet is unlocked
if (walletName === WalletNames.METAMASK) {
return window.ethereum?._metamask?.isUnlocked?.() || false
return (await window.ethereum?._metamask?.isUnlocked?.()) || false
}

// WalletConnect v2
if (walletName === WalletNames.WALLET_CONNECT_V2) {
const wcSession = window.localStorage.getItem('wc@2:client:0.3//session')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the 0.3 is versioning.

You can call storageKey (and maybe even get) from WC here to avoid outdated session data.

Copy link
Member Author

@katspaugh katspaugh Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware of the trade-offs here and would sooner check local storage directly rather than call some internal WC method. This can be made more robust by searching for a key named wc@2:client:*//session perhaps.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware of the trade-offs here and would sooner check local storage directly rather than call some internal WC method.

storageKey would return the key with which you could check the localStorage directly.

This can be made more robust by searching for a key named wc@2:client:*//session perhaps.

I'd personally prefer this.

return !!wcSession && wcSession !== '[]'
}

// Wallet connect creates a localStorage entry when connected and removes it when disconnected
// 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
Loading