Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add CaipAssetTypeOrId struct #229

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/caip-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export const CAIP_ASSET_TYPE_REGEX =
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.
*/
Expand Down Expand Up @@ -120,6 +123,14 @@ export const CaipAssetIdStruct =
);
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. */
Expand Down
1 change: 1 addition & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('index', () => {
"CaipAssetIdStruct",
"CaipAssetNamespaceStruct",
"CaipAssetReferenceStruct",
"CaipAssetTypeOrIdStruct",
"CaipAssetTypeStruct",
"CaipChainIdStruct",
"CaipNamespaceStruct",
Expand Down
1 change: 1 addition & 0 deletions src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('node', () => {
"CaipAssetIdStruct",
"CaipAssetNamespaceStruct",
"CaipAssetReferenceStruct",
"CaipAssetTypeOrIdStruct",
"CaipAssetTypeStruct",
"CaipChainIdStruct",
"CaipNamespaceStruct",
Expand Down
Loading