-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A generic hook for fetching concrete features for wallet accounts (#55)
Given a `UiWalletAccount` and the name of a feature, this hook returns the feature object from the underlying Wallet Standard `Wallet`. This is a specialization of `useWalletFeature()` that takes into consideration that the features supported by a wallet might not be supported by every account in that wallet. In the event that the wallet or account does not support the feature, a `WalletStandardError` will be thrown.
- Loading branch information
Showing
8 changed files
with
109 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@wallet-standard/ui-features': patch | ||
--- | ||
|
||
A specialized function that fetches the underlying feature object from a `UiWalletAccount`, ensuring that both the wallet _and_ the account indicate support for that feature. |
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
44 changes: 44 additions & 0 deletions
44
packages/ui/features/src/__tests__/getWalletAccountFeature-test.ts
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,44 @@ | ||
import { | ||
WALLET_STANDARD_ERROR__FEATURES__WALLET_ACCOUNT_FEATURE_UNIMPLEMENTED, | ||
WalletStandardError, | ||
} from '@wallet-standard/errors'; | ||
import type { UiWalletAccount } from '@wallet-standard/ui-core'; | ||
|
||
import { getWalletAccountFeature } from '../getWalletAccountFeature.js'; | ||
import { getWalletFeature } from '../getWalletFeature.js'; | ||
|
||
jest.mock('../getWalletFeature.js'); | ||
|
||
describe('getWalletAccountFeature', () => { | ||
let mockWalletAccount: UiWalletAccount; | ||
beforeEach(() => { | ||
mockWalletAccount = { | ||
'~uiWalletHandle': Symbol() as UiWalletAccount['~uiWalletHandle'], | ||
address: 'abc', | ||
chains: ['solana:mainnet'], | ||
features: ['feature:a'], | ||
publicKey: new Uint8Array([1, 2, 3]), | ||
}; | ||
// 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('throws if the account does not support the feature requested', () => { | ||
expect(() => { | ||
getWalletAccountFeature(mockWalletAccount, 'feature:b'); | ||
}).toThrow( | ||
new WalletStandardError(WALLET_STANDARD_ERROR__FEATURES__WALLET_ACCOUNT_FEATURE_UNIMPLEMENTED, { | ||
address: 'abc', | ||
supportedChains: ['solana:mainnet'], | ||
supportedFeatures: ['feature:a'], | ||
featureName: 'feature:b', | ||
}) | ||
); | ||
}); | ||
it('returns the feature of the underlying wallet', () => { | ||
const mockFeature = {}; | ||
jest.mocked(getWalletFeature).mockReturnValue(mockFeature); | ||
expect(getWalletAccountFeature(mockWalletAccount, 'feature:a')).toBe(mockFeature); | ||
}); | ||
}); |
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,30 @@ | ||
import { | ||
WALLET_STANDARD_ERROR__FEATURES__WALLET_ACCOUNT_FEATURE_UNIMPLEMENTED, | ||
WalletStandardError, | ||
safeCaptureStackTrace, | ||
} from '@wallet-standard/errors'; | ||
import type { UiWalletAccount } from '@wallet-standard/ui-core'; | ||
|
||
import { getWalletFeature } from './getWalletFeature.js'; | ||
|
||
/** | ||
* Returns the feature object from the Wallet Standard `Wallet` that underlies a | ||
* `UiWalletAccount`. In the event that either the wallet or the account do not support the | ||
* feature, a `WalletStandardError` will be thrown. | ||
*/ | ||
export function getWalletAccountFeature<TWalletAccount extends UiWalletAccount>( | ||
walletAccount: TWalletAccount, | ||
featureName: TWalletAccount['features'][number] | ||
): unknown { | ||
if (!walletAccount.features.includes(featureName)) { | ||
const err = new WalletStandardError(WALLET_STANDARD_ERROR__FEATURES__WALLET_ACCOUNT_FEATURE_UNIMPLEMENTED, { | ||
address: walletAccount.address, | ||
featureName, | ||
supportedChains: [...walletAccount.chains], | ||
supportedFeatures: [...walletAccount.features], | ||
}); | ||
safeCaptureStackTrace(err, getWalletAccountFeature); | ||
throw err; | ||
} | ||
return getWalletFeature(walletAccount, featureName); | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './getWalletAccountFeature.js'; | ||
export * from './getWalletFeature.js'; |