generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcaip-types.ts
546 lines (486 loc) · 16.8 KB
/
caip-types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
import type { Infer } from '@metamask/superstruct';
import { is } from '@metamask/superstruct';
import { definePattern } from './superstruct';
export const CAIP_CHAIN_ID_REGEX =
/^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})$/u;
export const CAIP_NAMESPACE_REGEX = /^[-a-z0-9]{3,8}$/u;
export const CAIP_REFERENCE_REGEX = /^[-_a-zA-Z0-9]{1,32}$/u;
export const CAIP_ACCOUNT_ID_REGEX =
/^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})):(?<accountAddress>[-.%a-zA-Z0-9]{1,128})$/u;
export const CAIP_ACCOUNT_ADDRESS_REGEX = /^[-.%a-zA-Z0-9]{1,128}$/u;
export const CAIP_ASSET_NAMESPACE_REGEX = /^[-a-z0-9]{3,8}$/u;
export const CAIP_ASSET_REFERENCE_REGEX = /^[-.%a-zA-Z0-9]{1,128}$/u;
export const CAIP_TOKEN_ID_REGEX = /^[-.%a-zA-Z0-9]{1,78}$/u;
export 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;
export 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;
const CAIP_ASSET_TYPE_OR_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-2 chain ID, i.e., a human-readable namespace and reference.
*/
export const CaipChainIdStruct = definePattern<`${string}:${string}`>(
'CaipChainId',
CAIP_CHAIN_ID_REGEX,
);
export type CaipChainId = Infer<typeof CaipChainIdStruct>;
/**
* A CAIP-2 namespace, i.e., the first part of a CAIP chain ID.
*/
export const CaipNamespaceStruct = definePattern(
'CaipNamespace',
CAIP_NAMESPACE_REGEX,
);
export type CaipNamespace = Infer<typeof CaipNamespaceStruct>;
/**
* A CAIP-2 reference, i.e., the second part of a CAIP chain ID.
*/
export const CaipReferenceStruct = definePattern(
'CaipReference',
CAIP_REFERENCE_REGEX,
);
export type CaipReference = Infer<typeof CaipReferenceStruct>;
/**
* A CAIP-10 account ID, i.e., a human-readable namespace, reference, and account address.
*/
export const CaipAccountIdStruct =
definePattern<`${string}:${string}:${string}`>(
'CaipAccountId',
CAIP_ACCOUNT_ID_REGEX,
);
export type CaipAccountId = Infer<typeof CaipAccountIdStruct>;
/**
* A CAIP-10 account address, i.e., the third part of the CAIP account ID.
*/
export const CaipAccountAddressStruct = definePattern(
'CaipAccountAddress',
CAIP_ACCOUNT_ADDRESS_REGEX,
);
export type CaipAccountAddress = Infer<typeof CaipAccountAddressStruct>;
/**
* A CAIP-19 asset namespace, i.e., a namespace domain of an asset.
*/
export const CaipAssetNamespaceStruct = definePattern(
'CaipAssetNamespace',
CAIP_ASSET_NAMESPACE_REGEX,
);
export type CaipAssetNamespace = Infer<typeof CaipAssetNamespaceStruct>;
/**
* A CAIP-19 asset reference, i.e., an identifier for an asset within a given namespace.
*/
export const CaipAssetReferenceStruct = definePattern(
'CaipAssetReference',
CAIP_ASSET_REFERENCE_REGEX,
);
export type CaipAssetReference = Infer<typeof CaipAssetReferenceStruct>;
/**
* A CAIP-19 asset token ID, i.e., a unique identifier for an addressable asset of a given type
*/
export const CaipTokenIdStruct = definePattern(
'CaipTokenId',
CAIP_TOKEN_ID_REGEX,
);
export type CaipTokenId = Infer<typeof CaipTokenIdStruct>;
/**
* A CAIP-19 asset type identifier, i.e., a human-readable type of asset identifier.
*/
export const CaipAssetTypeStruct =
definePattern<`${string}:${string}/${string}:${string}`>(
'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<`${string}:${string}/${string}:${string}/${string}`>(
'CaipAssetId',
CAIP_ASSET_ID_REGEX,
);
export type CaipAssetId = Infer<typeof CaipAssetIdStruct>;
/**
* A CAIP-19 asset type or asset ID identifier, i.e., a human-readable type of asset identifier.
*/
export const CaipAssetTypeOrIdStruct = definePattern<
CaipAssetType | CaipAssetId
>('CaipAssetTypeOrId', CAIP_ASSET_TYPE_OR_ID_REGEX);
export type CaipAssetTypeOrId = Infer<typeof CaipAssetTypeOrIdStruct>;
/** Known CAIP namespaces. */
export enum KnownCaipNamespace {
/** BIP-122 (Bitcoin) compatible chains. */
Bip122 = 'bip122',
/** Solana compatible chains */
Solana = 'solana',
/** EIP-155 compatible chains. */
Eip155 = 'eip155',
Wallet = 'wallet',
}
/**
* Check if the given value is a {@link CaipChainId}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipChainId}.
*/
export function isCaipChainId(value: unknown): value is CaipChainId {
return is(value, CaipChainIdStruct);
}
/**
* Check if the given value is a {@link CaipNamespace}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipNamespace}.
*/
export function isCaipNamespace(value: unknown): value is CaipNamespace {
return is(value, CaipNamespaceStruct);
}
/**
* Check if the given value is a {@link CaipReference}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipReference}.
*/
export function isCaipReference(value: unknown): value is CaipReference {
return is(value, CaipReferenceStruct);
}
/**
* Check if the given value is a {@link CaipAccountId}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipAccountId}.
*/
export function isCaipAccountId(value: unknown): value is CaipAccountId {
return is(value, CaipAccountIdStruct);
}
/**
* Check if a value is a {@link CaipAccountAddress}.
*
* @param value - The value to validate.
* @returns True if the value is a valid {@link CaipAccountAddress}.
*/
export function isCaipAccountAddress(
value: unknown,
): value is CaipAccountAddress {
return is(value, CaipAccountAddressStruct);
}
/**
* Check if the given value is a {@link CaipAssetNamespace}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipAssetNamespace}.
*/
export function isCaipAssetNamespace(
value: unknown,
): value is CaipAssetNamespace {
return is(value, CaipAssetNamespaceStruct);
}
/**
* Check if the given value is a {@link CaipAssetReference}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipAssetReference}.
*/
export function isCaipAssetReference(
value: unknown,
): value is CaipAssetReference {
return is(value, CaipAssetReferenceStruct);
}
/**
* Check if the given value is a {@link CaipTokenId}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipTokenId}.
*/
export function isCaipTokenId(value: unknown): value is CaipTokenId {
return is(value, CaipTokenIdStruct);
}
/**
* Check if the given value is a {@link CaipAssetType}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipAssetType}.
*/
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}.
*/
export function isCaipAssetId(value: unknown): value is CaipAssetId {
return is(value, CaipAssetIdStruct);
}
/**
* Parse a CAIP-2 chain ID to an object containing the namespace and reference.
* This validates the CAIP-2 chain ID before parsing it.
*
* @param caipChainId - The CAIP-2 chain ID to validate and parse.
* @returns The parsed CAIP-2 chain ID.
*/
export function parseCaipChainId(caipChainId: CaipChainId): {
namespace: CaipNamespace;
reference: CaipReference;
} {
const match = CAIP_CHAIN_ID_REGEX.exec(caipChainId);
if (!match?.groups) {
throw new Error('Invalid CAIP chain ID.');
}
return {
namespace: match.groups.namespace as CaipNamespace,
reference: match.groups.reference as CaipReference,
};
}
/**
* Parse an CAIP-10 account ID to an object containing the chain ID, parsed chain ID, and account address.
* This validates the CAIP-10 account ID before parsing it.
*
* @param caipAccountId - The CAIP-10 account ID to validate and parse.
* @returns The parsed CAIP-10 account ID.
*/
export function parseCaipAccountId(caipAccountId: CaipAccountId): {
address: CaipAccountAddress;
chainId: CaipChainId;
chain: { namespace: CaipNamespace; reference: CaipReference };
} {
const match = CAIP_ACCOUNT_ID_REGEX.exec(caipAccountId);
if (!match?.groups) {
throw new Error('Invalid CAIP account ID.');
}
return {
address: match.groups.accountAddress as CaipAccountAddress,
chainId: match.groups.chainId as CaipChainId,
chain: {
namespace: match.groups.namespace as CaipNamespace,
reference: match.groups.reference as CaipReference,
},
};
}
/**
* Parse a CAIP-19 asset type to an object containing the chain ID, parsed chain ID,
* asset namespace, and asset reference
*
* This validates the CAIP-19 asset type before parsing it.
*
* @param caipAssetType - The CAIP-19 asset type to validate and parse.
* @returns The parsed CAIP-19 asset type.
*/
export function parseCaipAssetType(caipAssetType: CaipAssetType): {
assetNamespace: CaipAssetNamespace;
assetReference: CaipAssetReference;
chainId: CaipChainId;
chain: { namespace: CaipNamespace; reference: CaipReference };
} {
const match = CAIP_ASSET_TYPE_REGEX.exec(caipAssetType);
if (!match?.groups) {
throw new Error('Invalid CAIP asset type.');
}
return {
assetNamespace: match.groups.assetNamespace as CaipAssetNamespace,
assetReference: match.groups.assetReference as CaipAssetReference,
chainId: match.groups.chainId as CaipChainId,
chain: {
namespace: match.groups.namespace as CaipNamespace,
reference: match.groups.reference as CaipReference,
},
};
}
/**
* Parse a CAIP-19 asset ID to an object containing the chain ID, parsed chain ID,
* asset namespace, asset reference, and token ID.
*
* This validates the CAIP-19 asset ID before parsing it.
*
* @param caipAssetId - The CAIP-19 asset ID to validate and parse.
* @returns The parsed CAIP-19 asset ID.
*/
export function parseCaipAssetId(caipAssetId: CaipAssetId): {
assetNamespace: CaipAssetNamespace;
assetReference: CaipAssetReference;
tokenId: CaipTokenId;
chainId: CaipChainId;
chain: { namespace: CaipNamespace; reference: CaipReference };
} {
const match = CAIP_ASSET_ID_REGEX.exec(caipAssetId);
if (!match?.groups) {
throw new Error('Invalid CAIP asset ID.');
}
return {
assetNamespace: match.groups.assetNamespace as CaipAssetNamespace,
assetReference: match.groups.assetReference as CaipAssetReference,
tokenId: match.groups.tokenId as CaipTokenId,
chainId: match.groups.chainId as CaipChainId,
chain: {
namespace: match.groups.namespace as CaipNamespace,
reference: match.groups.reference as CaipReference,
},
};
}
/**
* Chain ID as defined per the CAIP-2
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md}.
*
* It defines a way to uniquely identify any blockchain in a human-readable
* way.
*
* @param namespace - The standard (ecosystem) of similar blockchains.
* @param reference - Identify of a blockchain within a given namespace.
* @throws {@link Error}
* This exception is thrown if the inputs does not comply with the CAIP-2
* syntax specification
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md#syntax}.
* @returns A CAIP chain ID.
*/
export function toCaipChainId(
namespace: CaipNamespace,
reference: CaipReference,
): CaipChainId {
if (!isCaipNamespace(namespace)) {
throw new Error(
`Invalid "namespace", must match: ${CAIP_NAMESPACE_REGEX.toString()}`,
);
}
if (!isCaipReference(reference)) {
throw new Error(
`Invalid "reference", must match: ${CAIP_REFERENCE_REGEX.toString()}`,
);
}
return `${namespace}:${reference}`;
}
/**
* Account ID as defined per the CAIP-10
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-10.md}.
*
* It defines a way to uniquely identify any blockchain account in a human-readable
* way.
*
* @param namespace - The standard (ecosystem) of similar blockchains.
* @param reference - Identity of a blockchain within a given namespace.
* @param accountAddress - The address of the blockchain account.
* @throws {@link Error}
* This exception is thrown if the inputs do not comply with the CAIP-10
* syntax specification
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-10.md#syntax}.
* @returns A CAIP account ID.
*/
export function toCaipAccountId(
namespace: CaipNamespace,
reference: CaipReference,
accountAddress: CaipAccountAddress,
): CaipAccountId {
if (!isCaipNamespace(namespace)) {
throw new Error(
`Invalid "namespace", must match: ${CAIP_NAMESPACE_REGEX.toString()}`,
);
}
if (!isCaipReference(reference)) {
throw new Error(
`Invalid "reference", must match: ${CAIP_REFERENCE_REGEX.toString()}`,
);
}
if (!isCaipAccountAddress(accountAddress)) {
throw new Error(
`Invalid "accountAddress", must match: ${CAIP_ACCOUNT_ADDRESS_REGEX.toString()}`,
);
}
return `${namespace}:${reference}:${accountAddress}`;
}
/**
* Asset Type as defined per the CAIP-19
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-19.md}.
*
* It defines a way to uniquely identify any blockchain asset in a human-readable
* way.
*
* @param namespace - The standard (ecosystem) of similar blockchains.
* @param reference - Identity of a blockchain within a given namespace.
* @param assetNamespace - The namespace domain of an asset.
* @param assetReference - The identity of an asset within a given namespace.
* @throws {@link Error}
* This exception is thrown if the inputs do not comply with the CAIP-19
* syntax specification
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-19.md#syntax}.
* @returns A CAIP asset type.
*/
export function toCaipAssetType(
namespace: CaipNamespace,
reference: CaipReference,
assetNamespace: CaipAssetNamespace,
assetReference: CaipAssetReference,
): CaipAssetType {
if (!isCaipNamespace(namespace)) {
throw new Error(
`Invalid "namespace", must match: ${CAIP_NAMESPACE_REGEX.toString()}`,
);
}
if (!isCaipReference(reference)) {
throw new Error(
`Invalid "reference", must match: ${CAIP_REFERENCE_REGEX.toString()}`,
);
}
if (!isCaipAssetNamespace(assetNamespace)) {
throw new Error(
`Invalid "assetNamespace", must match: ${CAIP_ASSET_NAMESPACE_REGEX.toString()}`,
);
}
if (!isCaipAssetReference(assetReference)) {
throw new Error(
`Invalid "assetReference", must match: ${CAIP_ASSET_REFERENCE_REGEX.toString()}`,
);
}
return `${namespace}:${reference}/${assetNamespace}:${assetReference}`;
}
/**
* Asset ID as defined per the CAIP-19
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-19.md}.
*
* It defines a way to uniquely identify any blockchain asset in a human-readable
* way.
*
* @param namespace - The standard (ecosystem) of similar blockchains.
* @param reference - Identity of a blockchain within a given namespace.
* @param assetNamespace - The namespace domain of an asset.
* @param assetReference - The identity of an asset within a given namespace.
* @param tokenId - The unique identifier for an addressable asset of a given type.
* @throws {@link Error}
* This exception is thrown if the inputs do not comply with the CAIP-19
* syntax specification
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-19.md#syntax}.
* @returns A CAIP asset ID.
*/
export function toCaipAssetId(
namespace: CaipNamespace,
reference: CaipReference,
assetNamespace: CaipAssetNamespace,
assetReference: CaipAssetReference,
tokenId: CaipTokenId,
): CaipAssetId {
if (!isCaipNamespace(namespace)) {
throw new Error(
`Invalid "namespace", must match: ${CAIP_NAMESPACE_REGEX.toString()}`,
);
}
if (!isCaipReference(reference)) {
throw new Error(
`Invalid "reference", must match: ${CAIP_REFERENCE_REGEX.toString()}`,
);
}
if (!isCaipAssetNamespace(assetNamespace)) {
throw new Error(
`Invalid "assetNamespace", must match: ${CAIP_ASSET_NAMESPACE_REGEX.toString()}`,
);
}
if (!isCaipAssetReference(assetReference)) {
throw new Error(
`Invalid "assetReference", must match: ${CAIP_ASSET_REFERENCE_REGEX.toString()}`,
);
}
if (!isCaipTokenId(tokenId)) {
throw new Error(
`Invalid "tokenId", must match: ${CAIP_TOKEN_ID_REGEX.toString()}`,
);
}
return `${namespace}:${reference}/${assetNamespace}:${assetReference}/${tokenId}`;
}