Skip to content

Commit

Permalink
fix: Unhandled exception in useOnboard (safe-global#1868)
Browse files Browse the repository at this point in the history
* fix: Unhandled exception in useOnboard

* fix: added logger message and added test case for error in getAddress

* Update src/hooks/wallets/useOnboard.test.ts

Co-authored-by: Aaron Cook <[email protected]>

---------

Co-authored-by: Aaron Cook <[email protected]>
  • Loading branch information
deepaksing and iamacook authored Apr 15, 2023
1 parent 5a8140d commit 061350c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/hooks/wallets/useOnboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,24 @@ describe('getConnectedWallet', () => {
chainId: '4',
})
})

it('should return null if the address is invalid', () => {
const wallets = [
{
label: 'Wallet 1',
icon: '',
provider: null as unknown as EIP1193Provider,
chains: [{ id: '0x4' }],
accounts: [
{
address: '0xinvalid',
ens: null,
balance: null,
},
],
},
] as WalletState[]

expect(getConnectedWallet(wallets)).toBeNull()
})
})
18 changes: 12 additions & 6 deletions src/hooks/wallets/useOnboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ export const getConnectedWallet = (wallets: WalletState[]): ConnectedWallet | nu
const account = primaryWallet?.accounts[0]
if (!account) return null

return {
label: primaryWallet.label,
address: getAddress(account.address),
ens: account.ens?.name,
chainId: Number(primaryWallet.chains[0].id).toString(10),
provider: primaryWallet.provider,
try {
const address = getAddress(account.address)
return {
label: primaryWallet.label,
address,
ens: account.ens?.name,
chainId: Number(primaryWallet.chains[0].id).toString(10),
provider: primaryWallet.provider,
}
} catch (e) {
logError(Errors._106, (e as Error).message)
return null
}
}

Expand Down
1 change: 1 addition & 0 deletions src/services/exceptions/ErrorCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum ErrorCodes {
_103 = '103: Error creating a SafeTransaction',
_104 = '104: Invalid chain short name in the URL',
_105 = '105: Error initializing the Safe Core SDK',
_106 = '106: Failed to get connected wallet',

_302 = '302: Error connecting to the wallet',
_303 = '303: Error creating pairing session',
Expand Down

0 comments on commit 061350c

Please sign in to comment.