generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add Caip Types #116
Merged
Merged
Add Caip Types #116
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7e5ef64
add caip chain id
jiexi 05127fc
add parsing
jiexi 0478ad2
Update parseCaipChainIdString behavior
jiexi 0304a09
lint
jiexi 782e4fb
drop string suffix
jiexi 6d09fb6
Merge branch 'main' into jl/mmp-901/caip-2
jiexi c2a686c
Switch to @metamask/snap-utils caip types
jiexi 488863c
lint
jiexi a171671
Merge branch 'main' into jl/mmp-901/caip-2
jiexi 621e8ae
Separate regex const. Add CaipAccountAddressStruct
jiexi 192ebbf
Add CaipAccountAddress specs
jiexi 253c92b
tighten types
jiexi 2668bb1
remove unused types. cleanup specs. add fixtures
jiexi a70473a
last bit
jiexi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export const CAIP_CHAIN_ID_FIXTURES = [ | ||
'eip155:1', | ||
'bip122:000000000019d6689c085ae165831e93', | ||
'bip122:12a765e31ffd4059bada1e25190f6e98', | ||
'bip122:fdbe99b90c90bae7505796461471d89a', | ||
'cosmos:cosmoshub-2', | ||
'cosmos:cosmoshub-3', | ||
'cosmos:Binance-Chain-Tigris', | ||
'cosmos:iov-mainnet', | ||
'starknet:SN_GOERLI', | ||
'lip9:9ee11e9df416b18b', | ||
'chainstd:8c3444cf8970a9e41a706fab93e7a6c4', | ||
] as const; | ||
|
||
export const CAIP_NAMESPACE_FIXTURES = Array.from( | ||
new Set(CAIP_CHAIN_ID_FIXTURES.map((value) => value.split(':')[0])), | ||
); | ||
|
||
export const CAIP_REFERENCE_FIXTURES = Array.from( | ||
new Set(CAIP_CHAIN_ID_FIXTURES.map((value) => value.split(':')[1])), | ||
); | ||
|
||
export const CAIP_ACCOUNT_ID_FIXTURES = [ | ||
'eip155:1:0xab16a96D359eC26a11e2C2b3d8f8B8942d5Bfcdb', | ||
'bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6', | ||
'cosmos:cosmoshub-3:cosmos1t2uflqwqe0fsj0shcfkrvpukewcw40yjj6hdc0', | ||
'polkadot:b0a8d493285c2df73290dfb7e61f870f:5hmuyxw9xdgbpptgypokw4thfyoe3ryenebr381z9iaegmfy', | ||
'starknet:SN_GOERLI:0x02dd1b492765c064eac4039e3841aa5f382773b598097a40073bd8b48170ab57', | ||
'chainstd:8c3444cf8970a9e41a706fab93e7a6c4:6d9b0b4b9994e8a6afbd3dc3ed983cd51c755afb27cd1dc7825ef59c134a39f7', | ||
'hedera:mainnet:0.0.1234567890-zbhlt', | ||
] as const; | ||
|
||
export const CAIP_ACCOUNT_ADDRESS_FIXTURES = Array.from( | ||
new Set(CAIP_ACCOUNT_ID_FIXTURES.map((value) => value.split(':')[2])), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './bytes'; | ||
export * from './caip-types'; | ||
export * from './coercions'; | ||
export * from './json'; | ||
export * from './numbers'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { expectAssignable, expectNotAssignable } from 'tsd'; | ||
|
||
import type { | ||
CaipAccountAddress, | ||
CaipAccountId, | ||
CaipChainId, | ||
CaipNamespace, | ||
CaipReference, | ||
} from '.'; | ||
|
||
const embeddedString = 'test'; | ||
|
||
// Valid caip strings: | ||
|
||
expectAssignable<CaipChainId>('namespace:reference'); | ||
expectAssignable<CaipChainId>('namespace:'); | ||
expectAssignable<CaipChainId>(':reference'); | ||
expectAssignable<CaipChainId>(`${embeddedString}:${embeddedString}`); | ||
|
||
expectAssignable<CaipNamespace>('string'); | ||
expectAssignable<CaipNamespace>(`${embeddedString}`); | ||
|
||
expectAssignable<CaipReference>('string'); | ||
expectAssignable<CaipReference>(`${embeddedString}`); | ||
|
||
expectAssignable<CaipAccountId>('namespace:reference:accountAddress'); | ||
expectAssignable<CaipAccountId>('namespace:reference:'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar as above — I'm assuming this is non-enforceable via TypeScript? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above |
||
expectAssignable<CaipAccountId>(':reference:accountAddress'); | ||
expectAssignable<CaipAccountId>( | ||
`${embeddedString}:${embeddedString}:${embeddedString}`, | ||
); | ||
|
||
expectAssignable<CaipAccountAddress>('string'); | ||
expectAssignable<CaipAccountAddress>(`${embeddedString}`); | ||
|
||
// Not valid caip strings: | ||
|
||
expectAssignable<CaipChainId>('namespace:😀'); | ||
expectAssignable<CaipChainId>('😀:reference'); | ||
expectNotAssignable<CaipChainId>(0); | ||
expectNotAssignable<CaipChainId>('🙃'); | ||
|
||
expectNotAssignable<CaipNamespace>(0); | ||
|
||
expectNotAssignable<CaipReference>(0); | ||
|
||
expectAssignable<CaipAccountId>('namespace:reference:😀'); | ||
expectAssignable<CaipAccountId>('😀:reference:accountAddress'); | ||
expectNotAssignable<CaipAccountId>(0); | ||
expectNotAssignable<CaipAccountId>('🙃'); | ||
|
||
expectNotAssignable<CaipAccountAddress>(0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,276 @@ | ||
import { | ||
CAIP_ACCOUNT_ADDRESS_FIXTURES, | ||
CAIP_ACCOUNT_ID_FIXTURES, | ||
CAIP_CHAIN_ID_FIXTURES, | ||
CAIP_NAMESPACE_FIXTURES, | ||
CAIP_REFERENCE_FIXTURES, | ||
} from './__fixtures__'; | ||
import { | ||
isCaipAccountAddress, | ||
isCaipAccountId, | ||
isCaipChainId, | ||
isCaipNamespace, | ||
isCaipReference, | ||
parseCaipAccountId, | ||
parseCaipChainId, | ||
} from './caip-types'; | ||
|
||
describe('isCaipChainId', () => { | ||
it.each(CAIP_CHAIN_ID_FIXTURES)( | ||
'returns true for a valid chain id %s', | ||
(id) => { | ||
expect(isCaipChainId(id)).toBe(true); | ||
}, | ||
); | ||
|
||
it.each([ | ||
true, | ||
false, | ||
null, | ||
undefined, | ||
1, | ||
{}, | ||
[], | ||
'!@#$%^&*()', | ||
'a', | ||
':1', | ||
'123:', | ||
'abC:1', | ||
'eip155', | ||
'eip155:', | ||
'eip155:1:2', | ||
'bip122', | ||
'bip122:', | ||
'bip122:000000000019d6689c085ae165831e93:2', | ||
])('returns false for an invalid chain id %s', (id) => { | ||
expect(isCaipChainId(id)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isCaipNamespace', () => { | ||
it.each([...CAIP_NAMESPACE_FIXTURES])( | ||
'returns true for a valid namespace %s', | ||
(id) => { | ||
expect(isCaipNamespace(id)).toBe(true); | ||
}, | ||
); | ||
|
||
it.each([true, false, null, undefined, 1, {}, [], 'abC', '12', '123456789'])( | ||
'returns false for an invalid namespace %s', | ||
(id) => { | ||
expect(isCaipNamespace(id)).toBe(false); | ||
}, | ||
); | ||
}); | ||
|
||
describe('isCaipReference', () => { | ||
it.each([...CAIP_REFERENCE_FIXTURES])( | ||
'returns true for a valid reference %s', | ||
(id) => { | ||
expect(isCaipReference(id)).toBe(true); | ||
}, | ||
); | ||
|
||
it.each([ | ||
true, | ||
false, | ||
null, | ||
undefined, | ||
1, | ||
{}, | ||
[], | ||
'', | ||
'!@#$%^&*()', | ||
Array(33).fill('0').join(''), | ||
])('returns false for an invalid reference %s', (id) => { | ||
expect(isCaipReference(id)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isCaipAccountId', () => { | ||
it.each([...CAIP_ACCOUNT_ID_FIXTURES])( | ||
'returns true for a valid account id %s', | ||
(id) => { | ||
expect(isCaipAccountId(id)).toBe(true); | ||
}, | ||
); | ||
|
||
it.each([ | ||
true, | ||
false, | ||
null, | ||
undefined, | ||
1, | ||
{}, | ||
[], | ||
'', | ||
'!@#$%^&*()', | ||
'foo', | ||
'eip155', | ||
'eip155:', | ||
'eip155:1', | ||
'eip155:1:', | ||
'eip155:1:0x0000000000000000000000000000000000000000:2', | ||
'bip122', | ||
'bip122:', | ||
'bip122:000000000019d6689c085ae165831e93', | ||
'bip122:000000000019d6689c085ae165831e93:', | ||
'bip122:000000000019d6689c085ae165831e93:0x0000000000000000000000000000000000000000:2', | ||
])('returns false for an invalid account id %s', (id) => { | ||
expect(isCaipAccountId(id)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isCaipAccountAddress', () => { | ||
it.each([...CAIP_ACCOUNT_ADDRESS_FIXTURES])( | ||
'returns true for a valid account address %s', | ||
(id) => { | ||
expect(isCaipAccountAddress(id)).toBe(true); | ||
}, | ||
); | ||
|
||
it.each([ | ||
true, | ||
false, | ||
null, | ||
undefined, | ||
1, | ||
{}, | ||
[], | ||
'', | ||
'!@#$%^&*()', | ||
Array(129).fill('0').join(''), | ||
])('returns false for an invalid account address %s', (id) => { | ||
expect(isCaipAccountAddress(id)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('parseCaipChainId', () => { | ||
it('parses valid chain ids', () => { | ||
expect(parseCaipChainId('eip155:1')).toMatchInlineSnapshot(` | ||
{ | ||
"namespace": "eip155", | ||
"reference": "1", | ||
} | ||
`); | ||
|
||
expect(parseCaipChainId('bip122:000000000019d6689c085ae165831e93')) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"namespace": "bip122", | ||
"reference": "000000000019d6689c085ae165831e93", | ||
} | ||
`); | ||
|
||
expect(parseCaipChainId('cosmos:cosmoshub-3')).toMatchInlineSnapshot(` | ||
{ | ||
"namespace": "cosmos", | ||
"reference": "cosmoshub-3", | ||
} | ||
`); | ||
|
||
expect(parseCaipChainId('polkadot:b0a8d493285c2df73290dfb7e61f870f')) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"namespace": "polkadot", | ||
"reference": "b0a8d493285c2df73290dfb7e61f870f", | ||
} | ||
`); | ||
}); | ||
|
||
it.each([ | ||
true, | ||
false, | ||
null, | ||
undefined, | ||
1, | ||
'foo', | ||
'foobarbazquz:1', | ||
'foo:', | ||
'foo:foobarbazquzfoobarbazquzfoobarbazquzfoobarbazquzfoobarbazquzfoobarbazquz', | ||
])('throws for invalid input %s', (input) => { | ||
expect(() => parseCaipChainId(input as any)).toThrow( | ||
'Invalid CAIP chain ID.', | ||
); | ||
}); | ||
}); | ||
|
||
describe('parseCaipAccountId', () => { | ||
it('parses valid account ids', () => { | ||
expect( | ||
parseCaipAccountId('eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb'), | ||
).toMatchInlineSnapshot(` | ||
{ | ||
"address": "0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb", | ||
"chain": { | ||
"namespace": "eip155", | ||
"reference": "1", | ||
}, | ||
"chainId": "eip155:1", | ||
} | ||
`); | ||
|
||
expect( | ||
parseCaipAccountId( | ||
'bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6', | ||
), | ||
).toMatchInlineSnapshot(` | ||
{ | ||
"address": "128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6", | ||
"chain": { | ||
"namespace": "bip122", | ||
"reference": "000000000019d6689c085ae165831e93", | ||
}, | ||
"chainId": "bip122:000000000019d6689c085ae165831e93", | ||
} | ||
`); | ||
|
||
expect( | ||
parseCaipAccountId( | ||
'cosmos:cosmoshub-3:cosmos1t2uflqwqe0fsj0shcfkrvpukewcw40yjj6hdc0', | ||
), | ||
).toMatchInlineSnapshot(` | ||
{ | ||
"address": "cosmos1t2uflqwqe0fsj0shcfkrvpukewcw40yjj6hdc0", | ||
"chain": { | ||
"namespace": "cosmos", | ||
"reference": "cosmoshub-3", | ||
}, | ||
"chainId": "cosmos:cosmoshub-3", | ||
} | ||
`); | ||
|
||
expect( | ||
parseCaipAccountId( | ||
'polkadot:b0a8d493285c2df73290dfb7e61f870f:5hmuyxw9xdgbpptgypokw4thfyoe3ryenebr381z9iaegmfy', | ||
), | ||
).toMatchInlineSnapshot(` | ||
{ | ||
"address": "5hmuyxw9xdgbpptgypokw4thfyoe3ryenebr381z9iaegmfy", | ||
"chain": { | ||
"namespace": "polkadot", | ||
"reference": "b0a8d493285c2df73290dfb7e61f870f", | ||
}, | ||
"chainId": "polkadot:b0a8d493285c2df73290dfb7e61f870f", | ||
} | ||
`); | ||
}); | ||
|
||
it.each([ | ||
true, | ||
false, | ||
null, | ||
undefined, | ||
1, | ||
'foo', | ||
'foobarbazquz:1', | ||
'foo:', | ||
'foo:foobarbazquzfoobarbazquzfoobarbazquzfoobarbazquzfoobarbazquzfoobarbazquz', | ||
'eip155:1', | ||
'eip155:1:', | ||
])('throws for invalid input %s', (input) => { | ||
expect(() => parseCaipAccountId(input as any)).toThrow( | ||
'Invalid CAIP account ID.', | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming that this is non-enforceable via TypeScript, is that correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately not. We could try to hack it by doing something like this
But I believe this makes a type with n^2 definitions since they get crossmulitplied