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

fix: Cast CAIP-2 and CAIP-10 structs to their proper types #210

Merged
merged 1 commit into from
Aug 30, 2024
Merged
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
12 changes: 9 additions & 3 deletions src/caip-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Infer } from '@metamask/superstruct';
import { is, pattern, string } from '@metamask/superstruct';
import type { Infer, Struct } from '@metamask/superstruct';

export const CAIP_CHAIN_ID_REGEX =
/^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})$/u;
Expand All @@ -16,7 +16,10 @@ export const CAIP_ACCOUNT_ADDRESS_REGEX = /^[-.%a-zA-Z0-9]{1,128}$/u;
/**
* A CAIP-2 chain ID, i.e., a human-readable namespace and reference.
*/
export const CaipChainIdStruct = pattern(string(), CAIP_CHAIN_ID_REGEX);
export const CaipChainIdStruct = pattern(
string(),
CAIP_CHAIN_ID_REGEX,
) as Struct<CaipChainId, null>;
export type CaipChainId = `${string}:${string}`;

/**
Expand All @@ -34,7 +37,10 @@ export type CaipReference = Infer<typeof CaipReferenceStruct>;
/**
* A CAIP-10 account ID, i.e., a human-readable namespace, reference, and account address.
*/
export const CaipAccountIdStruct = pattern(string(), CAIP_ACCOUNT_ID_REGEX);
export const CaipAccountIdStruct = pattern(
string(),
CAIP_ACCOUNT_ID_REGEX,
) as Struct<CaipAccountId, null>;
export type CaipAccountId = `${string}:${string}:${string}`;

/**
Expand Down
Loading