generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(keyring-api)!: use CaipAccountId for ResolveAccountAddress.address (
#186) We forgot to mention this in SIP-26, but the `address` being returned by `resolveAccountAddress` must use CAIP-10 format. This is already being checked by the consumers of this method, but it's better that we catch any errors related to this at API level.
- Loading branch information
Showing
7 changed files
with
78 additions
and
12 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
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,45 @@ | ||
import { assert } from '@metamask/superstruct'; | ||
|
||
import { ResolvedAccountAddressStruct } from './address'; | ||
import type { ResolvedAccountAddress } from './address'; | ||
import type { CaipAccountId } from './caip'; | ||
import { BtcScope } from '../btc'; | ||
import { EthScope } from '../eth'; | ||
import { SolScope } from '../sol'; | ||
|
||
const MOCK_ETH_ADDRESS = '0x6431726EEE67570BF6f0Cf892aE0a3988F03903F'; | ||
const MOCK_BTC_ADDRESS = 'bc1qwl8399fz829uqvqly9tcatgrgtwp3udnhxfq4k'; | ||
const MOCK_SOL_ADDRESS = '7EcDhSYGxXyscszYEp35KHN8vvw3svAuLKTzXwCFLtV'; | ||
|
||
describe('ResolveAccountAddress', () => { | ||
it.each([ | ||
`${EthScope.Eoa}:${MOCK_ETH_ADDRESS}`, | ||
`${BtcScope.Mainnet}:${MOCK_BTC_ADDRESS}`, | ||
`${SolScope.Mainnet}:${MOCK_SOL_ADDRESS}`, | ||
] as CaipAccountId[])( | ||
'allows CAIP-10 account ID: %s', | ||
(address: CaipAccountId) => { | ||
const resolvedAddress: ResolvedAccountAddress = { | ||
address, | ||
}; | ||
expect(() => | ||
assert(resolvedAddress, ResolvedAccountAddressStruct), | ||
).not.toThrow(); | ||
}, | ||
); | ||
|
||
it.each([MOCK_ETH_ADDRESS, MOCK_BTC_ADDRESS, MOCK_SOL_ADDRESS])( | ||
'throws an error if address is not a CAIP-10 account ID: %s', | ||
(address: string) => { | ||
const resolvedAddress: ResolvedAccountAddress = { | ||
// We type-cast to force the error. | ||
address: address as CaipAccountId, | ||
}; | ||
expect(() => | ||
assert(resolvedAddress, ResolvedAccountAddressStruct), | ||
).toThrow( | ||
`At path: address -- Expected a value of type \`CaipAccountId\`, but received: \`"${address}"\``, | ||
); | ||
}, | ||
); | ||
}); |
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 |
---|---|---|
@@ -1,22 +1,31 @@ | ||
// istanbul ignore file | ||
|
||
import { | ||
CaipAccountIdStruct, | ||
CaipAssetIdStruct, | ||
CaipAssetTypeStruct, | ||
CaipAssetTypeOrIdStruct, | ||
CaipChainIdStruct, | ||
} from '@metamask/utils'; | ||
import type { | ||
CaipAccountId, | ||
CaipAssetId, | ||
CaipAssetType, | ||
CaipAssetTypeOrId, | ||
CaipChainId, | ||
} from '@metamask/utils'; | ||
|
||
export { | ||
CaipAccountIdStruct, | ||
CaipAssetIdStruct, | ||
CaipAssetTypeStruct, | ||
CaipAssetTypeOrIdStruct, | ||
CaipChainIdStruct, | ||
}; | ||
export type { CaipAssetId, CaipAssetType, CaipAssetTypeOrId, CaipChainId }; | ||
export type { | ||
CaipAccountId, | ||
CaipAssetId, | ||
CaipAssetType, | ||
CaipAssetTypeOrId, | ||
CaipChainId, | ||
}; |
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