Skip to content

Commit

Permalink
feat: add additional CAIP-19 types and parsing functions to align wit…
Browse files Browse the repository at this point in the history
…h proposal (#227)

Added full support for [CAIP-19 Asset
IDs](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-19.md)
including the necessary regex, structs, and parsing functions.

A few notes:
- While the regex of a chain namespace is technically the same as an
asset namespace, the official proposal makes a clear distinction between
the two, and so it's within the spirit of the spec to break these out
into separate properties. The same cannot be said for a chain reference
vs asset reference: these require different regex for validation.
- Previously, `CaipAssetId` required a tokenId (`/${string}`) to be
present. This has been changed to allow for tokenId to be optional (as
dictated by the official proposal) in both the type and its
corresponding regex string
- For completeness, `toCaipAccountId` and `parseCaipAssetId` functions
were added
  • Loading branch information
erwolff authored Dec 13, 2024
1 parent a5886d3 commit 48267f8
Show file tree
Hide file tree
Showing 6 changed files with 864 additions and 26 deletions.
29 changes: 29 additions & 0 deletions src/__fixtures__/caip-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,32 @@ export const CAIP_ACCOUNT_ID_FIXTURES = [
export const CAIP_ACCOUNT_ADDRESS_FIXTURES = Array.from(
new Set(CAIP_ACCOUNT_ID_FIXTURES.map((value) => value.split(':')[2])),
);

export const CAIP_ASSET_TYPE_FIXTURES = [
'eip155:1/slip44:60',
'eip155:1/erc20:0x6b175474e89094c44da98b954eedeac495271d0f',
'bip122:000000000019d6689c085ae165831e93/slip44:0',
'bip122:12a765e31ffd4059bada1e25190f6e98/slip44:2',
'cosmos:cosmoshub-3/slip44:118',
'cosmos:Binance-Chain-Tigris/slip44:714',
'lip9:9ee11e9df416b18b/slip44:134',
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/nft:Fz6LxeUg5qjesYX3BdmtTwyyzBtMxk644XiTqU5W3w9w',
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token:EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
] as const;

export const CAIP_ASSET_ID_FIXTURES = [
'eip155:1/erc721:0x06012c8cf97BEaD5deAe237070F9587f8E7A266d/771769',
'hedera:mainnet/nft:0.0.55492/12',
] as const;

export const CAIP_ASSET_NAMESPACE_FIXTURES = Array.from(
new Set(
CAIP_ASSET_TYPE_FIXTURES.map((value) => value.split('/')[1]?.split(':')[0]),
),
);

export const CAIP_ASSET_REFERENCE_FIXTURES = Array.from(
new Set(
CAIP_ASSET_TYPE_FIXTURES.map((value) => value.split('/')[1]?.split(':')[1]),
),
);
38 changes: 38 additions & 0 deletions src/caip-types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { expectAssignable, expectNotAssignable } from 'tsd';
import type {
CaipAccountAddress,
CaipAccountId,
CaipAssetId,
CaipAssetNamespace,
CaipAssetReference,
CaipAssetType,
CaipChainId,
CaipNamespace,
CaipReference,
Expand Down Expand Up @@ -33,6 +37,30 @@ expectAssignable<CaipAccountId>(
expectAssignable<CaipAccountAddress>('string');
expectAssignable<CaipAccountAddress>(`${embeddedString}`);

expectAssignable<CaipAssetNamespace>('string');
expectAssignable<CaipAssetNamespace>(`${embeddedString}`);

expectAssignable<CaipAssetReference>('string');
expectAssignable<CaipAssetReference>(`${embeddedString}`);

expectAssignable<CaipAssetType>(
'namespace:reference/assetNamespace:assetReference',
);
expectAssignable<CaipAssetType>('namespace:reference/:');
expectAssignable<CaipAssetType>(':reference/assetNamespace:');
expectAssignable<CaipAssetType>(
`${embeddedString}:${embeddedString}/${embeddedString}:${embeddedString}`,
);

expectAssignable<CaipAssetId>(
'namespace:reference/assetNamespace:assetReference/tokenId',
);
expectAssignable<CaipAssetId>('namespace:reference/:assetReference/');
expectAssignable<CaipAssetId>(':reference/assetNamespace:/');
expectAssignable<CaipAssetId>(
`${embeddedString}:${embeddedString}/${embeddedString}:${embeddedString}/${embeddedString}`,
);

// Not valid caip strings:

expectAssignable<CaipChainId>('namespace:😀');
Expand All @@ -50,3 +78,13 @@ expectNotAssignable<CaipAccountId>(0);
expectNotAssignable<CaipAccountId>('🙃');

expectNotAssignable<CaipAccountAddress>(0);

expectNotAssignable<CaipAssetNamespace>(0);

expectNotAssignable<CaipAssetReference>(0);

expectNotAssignable<CaipAssetType>(0);
expectNotAssignable<CaipAssetType>('🙃');

expectNotAssignable<CaipAssetId>(0);
expectNotAssignable<CaipAssetId>('🙃');
Loading

0 comments on commit 48267f8

Please sign in to comment.