|
1 |
| -import type { Infer, Struct } from '@metamask/superstruct'; |
2 |
| -import { is, pattern, string } from '@metamask/superstruct'; |
| 1 | +import type { Infer } from '@metamask/superstruct'; |
| 2 | +import { is } from '@metamask/superstruct'; |
| 3 | + |
| 4 | +import { definePattern } from './superstruct'; |
3 | 5 |
|
4 | 6 | export const CAIP_CHAIN_ID_REGEX =
|
5 | 7 | /^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})$/u;
|
@@ -28,83 +30,95 @@ export const CAIP_ASSET_ID_REGEX =
|
28 | 30 | /**
|
29 | 31 | * A CAIP-2 chain ID, i.e., a human-readable namespace and reference.
|
30 | 32 | */
|
31 |
| -export const CaipChainIdStruct = pattern( |
32 |
| - string(), |
| 33 | +export const CaipChainIdStruct = definePattern<`${string}:${string}`>( |
| 34 | + 'CaipChainId', |
33 | 35 | CAIP_CHAIN_ID_REGEX,
|
34 |
| -) as Struct<CaipChainId, null>; |
35 |
| -export type CaipChainId = `${string}:${string}`; |
| 36 | +); |
| 37 | +export type CaipChainId = Infer<typeof CaipChainIdStruct>; |
36 | 38 |
|
37 | 39 | /**
|
38 | 40 | * A CAIP-2 namespace, i.e., the first part of a CAIP chain ID.
|
39 | 41 | */
|
40 |
| -export const CaipNamespaceStruct = pattern(string(), CAIP_NAMESPACE_REGEX); |
| 42 | +export const CaipNamespaceStruct = definePattern( |
| 43 | + 'CaipNamespace', |
| 44 | + CAIP_NAMESPACE_REGEX, |
| 45 | +); |
41 | 46 | export type CaipNamespace = Infer<typeof CaipNamespaceStruct>;
|
42 | 47 |
|
43 | 48 | /**
|
44 | 49 | * A CAIP-2 reference, i.e., the second part of a CAIP chain ID.
|
45 | 50 | */
|
46 |
| -export const CaipReferenceStruct = pattern(string(), CAIP_REFERENCE_REGEX); |
| 51 | +export const CaipReferenceStruct = definePattern( |
| 52 | + 'CaipReference', |
| 53 | + CAIP_REFERENCE_REGEX, |
| 54 | +); |
47 | 55 | export type CaipReference = Infer<typeof CaipReferenceStruct>;
|
48 | 56 |
|
49 | 57 | /**
|
50 | 58 | * A CAIP-10 account ID, i.e., a human-readable namespace, reference, and account address.
|
51 | 59 | */
|
52 |
| -export const CaipAccountIdStruct = pattern( |
53 |
| - string(), |
54 |
| - CAIP_ACCOUNT_ID_REGEX, |
55 |
| -) as Struct<CaipAccountId, null>; |
56 |
| -export type CaipAccountId = `${string}:${string}:${string}`; |
| 60 | +export const CaipAccountIdStruct = |
| 61 | + definePattern<`${string}:${string}:${string}`>( |
| 62 | + 'CaipAccountId', |
| 63 | + CAIP_ACCOUNT_ID_REGEX, |
| 64 | + ); |
| 65 | +export type CaipAccountId = Infer<typeof CaipAccountIdStruct>; |
57 | 66 |
|
58 | 67 | /**
|
59 | 68 | * A CAIP-10 account address, i.e., the third part of the CAIP account ID.
|
60 | 69 | */
|
61 |
| -export const CaipAccountAddressStruct = pattern( |
62 |
| - string(), |
| 70 | +export const CaipAccountAddressStruct = definePattern( |
| 71 | + 'CaipAccountAddress', |
63 | 72 | CAIP_ACCOUNT_ADDRESS_REGEX,
|
64 | 73 | );
|
65 | 74 | export type CaipAccountAddress = Infer<typeof CaipAccountAddressStruct>;
|
66 | 75 |
|
67 | 76 | /**
|
68 | 77 | * A CAIP-19 asset namespace, i.e., a namespace domain of an asset.
|
69 | 78 | */
|
70 |
| -export const CaipAssetNamespaceStruct = pattern( |
71 |
| - string(), |
| 79 | +export const CaipAssetNamespaceStruct = definePattern( |
| 80 | + 'CaipAssetNamespace', |
72 | 81 | CAIP_ASSET_NAMESPACE_REGEX,
|
73 | 82 | );
|
74 | 83 | export type CaipAssetNamespace = Infer<typeof CaipAssetNamespaceStruct>;
|
75 | 84 |
|
76 | 85 | /**
|
77 | 86 | * A CAIP-19 asset reference, i.e., an identifier for an asset within a given namespace.
|
78 | 87 | */
|
79 |
| -export const CaipAssetReferenceStruct = pattern( |
80 |
| - string(), |
| 88 | +export const CaipAssetReferenceStruct = definePattern( |
| 89 | + 'CaipAssetReference', |
81 | 90 | CAIP_ASSET_REFERENCE_REGEX,
|
82 | 91 | );
|
83 | 92 | export type CaipAssetReference = Infer<typeof CaipAssetReferenceStruct>;
|
84 | 93 |
|
85 | 94 | /**
|
86 | 95 | * A CAIP-19 asset token ID, i.e., a unique identifier for an addressable asset of a given type
|
87 | 96 | */
|
88 |
| -export const CaipTokenIdStruct = pattern(string(), CAIP_TOKEN_ID_REGEX); |
| 97 | +export const CaipTokenIdStruct = definePattern( |
| 98 | + 'CaipTokenId', |
| 99 | + CAIP_TOKEN_ID_REGEX, |
| 100 | +); |
89 | 101 | export type CaipTokenId = Infer<typeof CaipTokenIdStruct>;
|
90 | 102 |
|
91 | 103 | /**
|
92 | 104 | * A CAIP-19 asset type identifier, i.e., a human-readable type of asset identifier.
|
93 | 105 | */
|
94 |
| -export const CaipAssetTypeStruct = pattern( |
95 |
| - string(), |
96 |
| - CAIP_ASSET_TYPE_REGEX, |
97 |
| -) as Struct<CaipAssetType, null>; |
98 |
| -export type CaipAssetType = `${string}:${string}/${string}:${string}`; |
| 106 | +export const CaipAssetTypeStruct = |
| 107 | + definePattern<`${string}:${string}/${string}:${string}`>( |
| 108 | + 'CaipAssetType', |
| 109 | + CAIP_ASSET_TYPE_REGEX, |
| 110 | + ); |
| 111 | +export type CaipAssetType = Infer<typeof CaipAssetTypeStruct>; |
99 | 112 |
|
100 | 113 | /**
|
101 | 114 | * A CAIP-19 asset ID identifier, i.e., a human-readable type of asset ID.
|
102 | 115 | */
|
103 |
| -export const CaipAssetIdStruct = pattern( |
104 |
| - string(), |
105 |
| - CAIP_ASSET_ID_REGEX, |
106 |
| -) as Struct<CaipAssetId, null>; |
107 |
| -export type CaipAssetId = `${string}:${string}/${string}:${string}/${string}`; |
| 116 | +export const CaipAssetIdStruct = |
| 117 | + definePattern<`${string}:${string}/${string}:${string}/${string}`>( |
| 118 | + 'CaipAssetId', |
| 119 | + CAIP_ASSET_ID_REGEX, |
| 120 | + ); |
| 121 | +export type CaipAssetId = Infer<typeof CaipAssetIdStruct>; |
108 | 122 |
|
109 | 123 | /** Known CAIP namespaces. */
|
110 | 124 | export enum KnownCaipNamespace {
|
|
0 commit comments