This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CAIP-19 types and split
utils
(#321)
- Loading branch information
Showing
12 changed files
with
201 additions
and
54 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
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,86 @@ | ||
import { isCaipAssetId, isCaipAssetType } from './caip'; | ||
|
||
describe('isCaipAssetType', () => { | ||
// Imported from: https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-19.md#test-cases | ||
it.each([ | ||
'eip155:1/slip44:60', | ||
'bip122:000000000019d6689c085ae165831e93/slip44:0', | ||
'cosmos:cosmoshub-3/slip44:118', | ||
'bip122:12a765e31ffd4059bada1e25190f6e98/slip44:2', | ||
'cosmos:Binance-Chain-Tigris/slip44:714', | ||
'cosmos:iov-mainnet/slip44:234', | ||
'lip9:9ee11e9df416b18b/slip44:134', | ||
'eip155:1/erc20:0x6b175474e89094c44da98b954eedeac495271d0f', | ||
'eip155:1/erc721:0x06012c8cf97BEaD5deAe237070F9587f8E7A266d', | ||
])('returns true for a valid asset type %s', (id) => { | ||
expect(isCaipAssetType(id)).toBe(true); | ||
}); | ||
|
||
it.each([ | ||
true, | ||
false, | ||
null, | ||
undefined, | ||
1, | ||
{}, | ||
[], | ||
'', | ||
'!@#$%^&*()', | ||
'foo', | ||
'eip155', | ||
'eip155:', | ||
'eip155:1', | ||
'eip155:1:', | ||
'eip155:1:0x0000000000000000000000000000000000000000:2', | ||
'bip122', | ||
'bip122:', | ||
'bip122:000000000019d6689c085ae165831e93', | ||
'bip122:000000000019d6689c085ae165831e93/', | ||
'bip122:000000000019d6689c085ae165831e93/tooooooolong', | ||
'bip122:000000000019d6689c085ae165831e93/tooooooolong:asset', | ||
'eip155:1/erc721', | ||
'eip155:1/erc721:', | ||
'eip155:1/erc721:0x06012c8cf97BEaD5deAe237070F9587f8E7A266d/', | ||
])('returns false for an invalid asset type %s', (id) => { | ||
expect(isCaipAssetType(id)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isCaipAssetId', () => { | ||
// Imported from: https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-19.md#test-cases | ||
it.each([ | ||
'eip155:1/erc721:0x06012c8cf97BEaD5deAe237070F9587f8E7A266d/771769', | ||
'hedera:mainnet/nft:0.0.55492/12', | ||
])('returns true for a valid asset id %s', (id) => { | ||
expect(isCaipAssetId(id)).toBe(true); | ||
}); | ||
|
||
it.each([ | ||
true, | ||
false, | ||
null, | ||
undefined, | ||
1, | ||
{}, | ||
[], | ||
'', | ||
'!@#$%^&*()', | ||
'foo', | ||
'eip155', | ||
'eip155:', | ||
'eip155:1', | ||
'eip155:1:', | ||
'eip155:1:0x0000000000000000000000000000000000000000:2', | ||
'bip122', | ||
'bip122:', | ||
'bip122:000000000019d6689c085ae165831e93', | ||
'bip122:000000000019d6689c085ae165831e93/', | ||
'bip122:000000000019d6689c085ae165831e93/tooooooolong', | ||
'bip122:000000000019d6689c085ae165831e93/tooooooolong:asset', | ||
'eip155:1/erc721', | ||
'eip155:1/erc721:', | ||
'eip155:1/erc721:0x06012c8cf97BEaD5deAe237070F9587f8E7A266d/', | ||
])('returns false for an invalid asset id %s', (id) => { | ||
expect(isCaipAssetType(id)).toBe(false); | ||
}); | ||
}); |
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,59 @@ | ||
import { is, type Infer } from 'superstruct'; | ||
|
||
import { definePattern } from '../superstruct'; | ||
|
||
const CAIP_ASSET_TYPE_REGEX = | ||
/^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32}))\/(?<assetNamespace>[-a-z0-9]{3,8}):(?<assetReference>[-.%a-zA-Z0-9]{1,128})$/u; | ||
|
||
const CAIP_ASSET_ID_REGEX = | ||
/^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32}))\/(?<assetNamespace>[-a-z0-9]{3,8}):(?<assetReference>[-.%a-zA-Z0-9]{1,128})\/(?<tokenId>[-.%a-zA-Z0-9]{1,78})$/u; | ||
|
||
/** | ||
* A CAIP-19 asset type identifier, i.e., a human-readable type of asset identifier. | ||
*/ | ||
export const CaipAssetTypeStruct = definePattern( | ||
'CaipAssetType', | ||
CAIP_ASSET_TYPE_REGEX, | ||
); | ||
export type CaipAssetType = Infer<typeof CaipAssetTypeStruct>; | ||
|
||
/** | ||
* A CAIP-19 asset ID identifier, i.e., a human-readable type of asset ID. | ||
*/ | ||
export const CaipAssetIdStruct = definePattern( | ||
'CaipAssetId', | ||
CAIP_ASSET_ID_REGEX, | ||
); | ||
export type CaipAssetId = Infer<typeof CaipAssetIdStruct>; | ||
|
||
/** | ||
* Check if the given value is a {@link CaipAssetType}. | ||
* | ||
* @param value - The value to check. | ||
* @returns Whether the value is a {@link CaipAssetType}. | ||
* @example | ||
* ```ts | ||
* isCaipAssetType('eip155:1/slip44:60'); // true | ||
* isCaipAssetType('cosmos:cosmoshub-3/slip44:118'); // true | ||
* isCaipAssetType('hedera:mainnet/nft:0.0.55492/12'); // false | ||
* ``` | ||
*/ | ||
export function isCaipAssetType(value: unknown): value is CaipAssetType { | ||
return is(value, CaipAssetTypeStruct); | ||
} | ||
|
||
/** | ||
* Check if the given value is a {@link CaipAssetId}. | ||
* | ||
* @param value - The value to check. | ||
* @returns Whether the value is a {@link CaipAssetId}. | ||
* @example | ||
* ```ts | ||
* isCaipAssetType('eip155:1/slip44:60'); // false | ||
* isCaipAssetType('cosmos:cosmoshub-3/slip44:118'); // false | ||
* isCaipAssetType('hedera:mainnet/nft:0.0.55492/12'); // true | ||
* ``` | ||
*/ | ||
export function isCaipAssetId(value: unknown): value is CaipAssetId { | ||
return is(value, CaipAssetIdStruct); | ||
} |
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,4 @@ | ||
export * from './caip'; | ||
export * from './typing'; | ||
export * from './url'; | ||
export * from './uuid'; |
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,19 @@ | ||
import { define } from 'superstruct'; | ||
|
||
/** | ||
* Validates if a given value is a valid URL. | ||
* | ||
* @param value - The value to be validated. | ||
* @returns A boolean indicating if the value is a valid URL. | ||
*/ | ||
export const UrlStruct = define<string>('Url', (value: unknown) => { | ||
let url; | ||
|
||
try { | ||
url = new URL(value as string); | ||
} catch (_) { | ||
return false; | ||
} | ||
|
||
return url.protocol === 'http:' || url.protocol === 'https:'; | ||
}); |
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,9 @@ | ||
import { definePattern } from '../superstruct'; | ||
|
||
/** | ||
* UUIDv4 struct. | ||
*/ | ||
export const UuidStruct = definePattern( | ||
'UuidV4', | ||
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu, | ||
); |