-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A
useAccount
hook that throws if a wallet does not have an account …
…matching a given address
- Loading branch information
1 parent
64498f6
commit 82daf8f
Showing
5 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { address } from '@solana/addresses'; | ||
import { SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET, SolanaError } from '@solana/errors'; | ||
import { SOLANA_CHAINS } from '@solana/wallet-standard-chains'; | ||
import { Wallet } from '@wallet-standard/base'; | ||
|
||
import { renderHook } from '../test-renderer'; | ||
import { useWalletAccount } from '../useWalletAccount'; | ||
|
||
describe('useWalletAccount', () => { | ||
let mockWallet: Wallet; | ||
beforeEach(() => { | ||
mockWallet = { | ||
accounts: [ | ||
{ | ||
address: address('Httx5rAMNW3zA6NtXbgpnq22RdS9qK6rRBiNi8Msoc8a'), | ||
chains: ['solana:devnet'], | ||
features: ['solana:signMessage', 'solana:signAndSendTransaction'], | ||
icon: 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAIBAAA=', | ||
label: 'My Test Account', | ||
publicKey: new Uint8Array([ | ||
251, 6, 90, 16, 167, 85, 10, 206, 169, 88, 60, 180, 238, 49, 109, 108, 152, 101, 243, 178, 93, | ||
190, 195, 73, 206, 97, 76, 131, 200, 38, 175, 179, | ||
]), | ||
}, | ||
], | ||
chains: SOLANA_CHAINS, | ||
features: {}, | ||
icon: 'data:image/svg+xml;base64,ABC', | ||
name: 'Mock Wallet', | ||
version: '1.0.0', | ||
}; | ||
// Suppresses console output when an `ErrorBoundary` is hit. | ||
// See https://stackoverflow.com/a/72632884/802047 | ||
jest.spyOn(console, 'error').mockImplementation(); | ||
jest.spyOn(console, 'warn').mockImplementation(); | ||
}); | ||
it('returns the account matching the given address', () => { | ||
const { result } = renderHook(() => | ||
useWalletAccount(mockWallet, address('Httx5rAMNW3zA6NtXbgpnq22RdS9qK6rRBiNi8Msoc8a')), | ||
); | ||
expect(result.current).toBe(mockWallet.accounts[0]); | ||
}); | ||
it('fatals when a wallet has no account matching the given address', () => { | ||
const { result } = renderHook(() => | ||
useWalletAccount(mockWallet, address('6bDQKLGyVpAUzhZa8jDvKbAPPs33ESMdTAjN4HX5PEVu')), | ||
); | ||
expect(result.__type).toBe('error'); | ||
expect(result.current).toEqual( | ||
new SolanaError(SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET, { | ||
accountAddress: '6bDQKLGyVpAUzhZa8jDvKbAPPs33ESMdTAjN4HX5PEVu', | ||
walletName: 'Mock Wallet', | ||
}), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Address } from '@solana/addresses'; | ||
import { SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET, SolanaError } from '@solana/errors'; | ||
import { Wallet, WalletAccount } from '@wallet-standard/base'; | ||
|
||
export function useWalletAccount(wallet: Wallet, address: Address): WalletAccount { | ||
const account = wallet.accounts.find(account => account.address === address); | ||
if (!account) { | ||
throw new SolanaError(SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET, { | ||
accountAddress: address, | ||
walletName: wallet.name, | ||
}); | ||
} | ||
return account; | ||
} |