Skip to content

Commit

Permalink
fix: Cast CAIP-2 and CAIP-10 structs to their proper types (#210)
Browse files Browse the repository at this point in the history
Fixes a problem with `CaipChainIdStruct` and `CaipAccountIdStruct` which
were not cast to their respective types and as such were typed as
`Struct<string, null>`. This makes them hard to use when using
`Describe`.

This PR mirrors the implementation from `Hex` by casting the structs.
  • Loading branch information
FrederikBolding authored Aug 30, 2024
1 parent 74c03ab commit c2048b0
Showing 1 changed file with 9 additions and 3 deletions.
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

0 comments on commit c2048b0

Please sign in to comment.