@metamask/utils 9.1.0 → 9.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -1
- package/dist/caip-types.cjs +1 -0
- package/dist/caip-types.cjs.map +1 -1
- package/dist/caip-types.d.cts +8 -7
- package/dist/caip-types.d.cts.map +1 -1
- package/dist/caip-types.d.mts +8 -7
- package/dist/caip-types.d.mts.map +1 -1
- package/dist/caip-types.mjs +1 -0
- package/dist/caip-types.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [9.2.1]
|
|
10
|
+
### Fixed
|
|
11
|
+
- Fix wrong types for CAIP-2 and CAIP-10 structs ([#210](https://github.com/MetaMask/utils/pull/210))
|
|
12
|
+
|
|
13
|
+
## [9.2.0]
|
|
14
|
+
### Added
|
|
15
|
+
- Add `Wallet` member to `KnownCaipNamespace` enum ([#207](https://github.com/MetaMask/utils/pull/207))
|
|
16
|
+
|
|
9
17
|
## [9.1.0]
|
|
10
18
|
### Added
|
|
11
19
|
- Add `PublicInterface` type ([#197](https://github.com/MetaMask/utils/pull/197))
|
|
@@ -251,7 +259,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
251
259
|
### Added
|
|
252
260
|
- Initial release
|
|
253
261
|
|
|
254
|
-
[Unreleased]: https://github.com/MetaMask/utils/compare/v9.1
|
|
262
|
+
[Unreleased]: https://github.com/MetaMask/utils/compare/v9.2.1...HEAD
|
|
263
|
+
[9.2.1]: https://github.com/MetaMask/utils/compare/v9.2.0...v9.2.1
|
|
264
|
+
[9.2.0]: https://github.com/MetaMask/utils/compare/v9.1.0...v9.2.0
|
|
255
265
|
[9.1.0]: https://github.com/MetaMask/utils/compare/v9.0.0...v9.1.0
|
|
256
266
|
[9.0.0]: https://github.com/MetaMask/utils/compare/v8.5.0...v9.0.0
|
|
257
267
|
[8.5.0]: https://github.com/MetaMask/utils/compare/v8.4.0...v8.5.0
|
package/dist/caip-types.cjs
CHANGED
|
@@ -32,6 +32,7 @@ var KnownCaipNamespace;
|
|
|
32
32
|
(function (KnownCaipNamespace) {
|
|
33
33
|
/** EIP-155 compatible chains. */
|
|
34
34
|
KnownCaipNamespace["Eip155"] = "eip155";
|
|
35
|
+
KnownCaipNamespace["Wallet"] = "wallet";
|
|
35
36
|
})(KnownCaipNamespace = exports.KnownCaipNamespace || (exports.KnownCaipNamespace = {}));
|
|
36
37
|
/**
|
|
37
38
|
* Check if the given value is a {@link CaipChainId}.
|
package/dist/caip-types.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"caip-types.cjs","sourceRoot":"","sources":["../src/caip-types.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"caip-types.cjs","sourceRoot":"","sources":["../src/caip-types.ts"],"names":[],"mappings":";;;AAAA,uDAA4D;AAG/C,QAAA,mBAAmB,GAC9B,mEAAmE,CAAC;AAEzD,QAAA,oBAAoB,GAAG,mBAAmB,CAAC;AAE3C,QAAA,oBAAoB,GAAG,wBAAwB,CAAC;AAEhD,QAAA,qBAAqB,GAChC,wHAAwH,CAAC;AAE9G,QAAA,0BAA0B,GAAG,0BAA0B,CAAC;AAErE;;GAEG;AACU,QAAA,iBAAiB,GAAG,IAAA,qBAAO,EACtC,IAAA,oBAAM,GAAE,EACR,2BAAmB,CACS,CAAC;AAG/B;;GAEG;AACU,QAAA,mBAAmB,GAAG,IAAA,qBAAO,EAAC,IAAA,oBAAM,GAAE,EAAE,4BAAoB,CAAC,CAAC;AAG3E;;GAEG;AACU,QAAA,mBAAmB,GAAG,IAAA,qBAAO,EAAC,IAAA,oBAAM,GAAE,EAAE,4BAAoB,CAAC,CAAC;AAG3E;;GAEG;AACU,QAAA,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C,wBAAwB,CAAC,CAAC;KAC3C;IAED,OAAO;QACL,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,SAA0B;QAClD,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,SAA0B;KACnD,CAAC;AACJ,CAAC;AAbD,4CAaC;AAED;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAAC,aAA4B;IAK7D,MAAM,KAAK,GAAG,6BAAqB,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IACxD,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE;QAClB,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;KAC7C;IAED,OAAO;QACL,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,cAAoC;QAC1D,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,OAAsB;QAC5C,KAAK,EAAE;YACL,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,SAA0B;YAClD,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,SAA0B;SACnD;KACF,CAAC;AACJ,CAAC;AAlBD,gDAkBC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAgB,aAAa,CAC3B,SAAwB,EACxB,SAAwB;IAExB,IAAI,CAAC,eAAe,CAAC,SAAS,CAAC,EAAE;QAC/B,MAAM,IAAI,KAAK,CACb,oCAAoC,4BAAoB,CAAC,QAAQ,EAAE,EAAE,CACtE,CAAC;KACH;IAED,IAAI,CAAC,eAAe,CAAC,SAAS,CAAC,EAAE;QAC/B,MAAM,IAAI,KAAK,CACb,oCAAoC,4BAAoB,CAAC,QAAQ,EAAE,EAAE,CACtE,CAAC;KACH;IAED,OAAO,GAAG,SAAS,IAAI,SAAS,EAAE,CAAC;AACrC,CAAC;AAjBD,sCAiBC","sourcesContent":["import { is, pattern, string } from '@metamask/superstruct';\nimport type { Infer, Struct } from '@metamask/superstruct';\n\nexport const CAIP_CHAIN_ID_REGEX =\n /^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})$/u;\n\nexport const CAIP_NAMESPACE_REGEX = /^[-a-z0-9]{3,8}$/u;\n\nexport const CAIP_REFERENCE_REGEX = /^[-_a-zA-Z0-9]{1,32}$/u;\n\nexport const CAIP_ACCOUNT_ID_REGEX =\n /^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})):(?<accountAddress>[-.%a-zA-Z0-9]{1,128})$/u;\n\nexport const CAIP_ACCOUNT_ADDRESS_REGEX = /^[-.%a-zA-Z0-9]{1,128}$/u;\n\n/**\n * A CAIP-2 chain ID, i.e., a human-readable namespace and reference.\n */\nexport const CaipChainIdStruct = pattern(\n string(),\n CAIP_CHAIN_ID_REGEX,\n) as Struct<CaipChainId, null>;\nexport type CaipChainId = `${string}:${string}`;\n\n/**\n * A CAIP-2 namespace, i.e., the first part of a CAIP chain ID.\n */\nexport const CaipNamespaceStruct = pattern(string(), CAIP_NAMESPACE_REGEX);\nexport type CaipNamespace = Infer<typeof CaipNamespaceStruct>;\n\n/**\n * A CAIP-2 reference, i.e., the second part of a CAIP chain ID.\n */\nexport const CaipReferenceStruct = pattern(string(), CAIP_REFERENCE_REGEX);\nexport type CaipReference = Infer<typeof CaipReferenceStruct>;\n\n/**\n * A CAIP-10 account ID, i.e., a human-readable namespace, reference, and account address.\n */\nexport const CaipAccountIdStruct = pattern(\n string(),\n CAIP_ACCOUNT_ID_REGEX,\n) as Struct<CaipAccountId, null>;\nexport type CaipAccountId = `${string}:${string}:${string}`;\n\n/**\n * A CAIP-10 account address, i.e., the third part of the CAIP account ID.\n */\nexport const CaipAccountAddressStruct = pattern(\n string(),\n CAIP_ACCOUNT_ADDRESS_REGEX,\n);\nexport type CaipAccountAddress = Infer<typeof CaipAccountAddressStruct>;\n\n/** Known CAIP namespaces. */\nexport enum KnownCaipNamespace {\n /** EIP-155 compatible chains. */\n Eip155 = 'eip155',\n Wallet = 'wallet',\n}\n\n/**\n * Check if the given value is a {@link CaipChainId}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipChainId}.\n */\nexport function isCaipChainId(value: unknown): value is CaipChainId {\n return is(value, CaipChainIdStruct);\n}\n\n/**\n * Check if the given value is a {@link CaipNamespace}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipNamespace}.\n */\nexport function isCaipNamespace(value: unknown): value is CaipNamespace {\n return is(value, CaipNamespaceStruct);\n}\n\n/**\n * Check if the given value is a {@link CaipReference}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipReference}.\n */\nexport function isCaipReference(value: unknown): value is CaipReference {\n return is(value, CaipReferenceStruct);\n}\n\n/**\n * Check if the given value is a {@link CaipAccountId}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipAccountId}.\n */\nexport function isCaipAccountId(value: unknown): value is CaipAccountId {\n return is(value, CaipAccountIdStruct);\n}\n\n/**\n * Check if a value is a {@link CaipAccountAddress}.\n *\n * @param value - The value to validate.\n * @returns True if the value is a valid {@link CaipAccountAddress}.\n */\nexport function isCaipAccountAddress(\n value: unknown,\n): value is CaipAccountAddress {\n return is(value, CaipAccountAddressStruct);\n}\n\n/**\n * Parse a CAIP-2 chain ID to an object containing the namespace and reference.\n * This validates the CAIP-2 chain ID before parsing it.\n *\n * @param caipChainId - The CAIP-2 chain ID to validate and parse.\n * @returns The parsed CAIP-2 chain ID.\n */\nexport function parseCaipChainId(caipChainId: CaipChainId): {\n namespace: CaipNamespace;\n reference: CaipReference;\n} {\n const match = CAIP_CHAIN_ID_REGEX.exec(caipChainId);\n if (!match?.groups) {\n throw new Error('Invalid CAIP chain ID.');\n }\n\n return {\n namespace: match.groups.namespace as CaipNamespace,\n reference: match.groups.reference as CaipReference,\n };\n}\n\n/**\n * Parse an CAIP-10 account ID to an object containing the chain ID, parsed chain ID, and account address.\n * This validates the CAIP-10 account ID before parsing it.\n *\n * @param caipAccountId - The CAIP-10 account ID to validate and parse.\n * @returns The parsed CAIP-10 account ID.\n */\nexport function parseCaipAccountId(caipAccountId: CaipAccountId): {\n address: CaipAccountAddress;\n chainId: CaipChainId;\n chain: { namespace: CaipNamespace; reference: CaipReference };\n} {\n const match = CAIP_ACCOUNT_ID_REGEX.exec(caipAccountId);\n if (!match?.groups) {\n throw new Error('Invalid CAIP account ID.');\n }\n\n return {\n address: match.groups.accountAddress as CaipAccountAddress,\n chainId: match.groups.chainId as CaipChainId,\n chain: {\n namespace: match.groups.namespace as CaipNamespace,\n reference: match.groups.reference as CaipReference,\n },\n };\n}\n\n/**\n * Chain ID as defined per the CAIP-2\n * {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md}.\n *\n * It defines a way to uniquely identify any blockchain in a human-readable\n * way.\n *\n * @param namespace - The standard (ecosystem) of similar blockchains.\n * @param reference - Identify of a blockchain within a given namespace.\n * @throws {@link Error}\n * This exception is thrown if the inputs does not comply with the CAIP-2\n * syntax specification\n * {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md#syntax}.\n * @returns A CAIP chain ID.\n */\nexport function toCaipChainId(\n namespace: CaipNamespace,\n reference: CaipReference,\n): CaipChainId {\n if (!isCaipNamespace(namespace)) {\n throw new Error(\n `Invalid \"namespace\", must match: ${CAIP_NAMESPACE_REGEX.toString()}`,\n );\n }\n\n if (!isCaipReference(reference)) {\n throw new Error(\n `Invalid \"reference\", must match: ${CAIP_REFERENCE_REGEX.toString()}`,\n );\n }\n\n return `${namespace}:${reference}`;\n}\n"]}
|
package/dist/caip-types.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Infer } from "@metamask/superstruct";
|
|
1
|
+
import type { Infer, Struct } from "@metamask/superstruct";
|
|
2
2
|
export declare const CAIP_CHAIN_ID_REGEX: RegExp;
|
|
3
3
|
export declare const CAIP_NAMESPACE_REGEX: RegExp;
|
|
4
4
|
export declare const CAIP_REFERENCE_REGEX: RegExp;
|
|
@@ -7,32 +7,33 @@ export declare const CAIP_ACCOUNT_ADDRESS_REGEX: RegExp;
|
|
|
7
7
|
/**
|
|
8
8
|
* A CAIP-2 chain ID, i.e., a human-readable namespace and reference.
|
|
9
9
|
*/
|
|
10
|
-
export declare const CaipChainIdStruct:
|
|
10
|
+
export declare const CaipChainIdStruct: Struct<`${string}:${string}`, null>;
|
|
11
11
|
export type CaipChainId = `${string}:${string}`;
|
|
12
12
|
/**
|
|
13
13
|
* A CAIP-2 namespace, i.e., the first part of a CAIP chain ID.
|
|
14
14
|
*/
|
|
15
|
-
export declare const CaipNamespaceStruct:
|
|
15
|
+
export declare const CaipNamespaceStruct: Struct<string, null>;
|
|
16
16
|
export type CaipNamespace = Infer<typeof CaipNamespaceStruct>;
|
|
17
17
|
/**
|
|
18
18
|
* A CAIP-2 reference, i.e., the second part of a CAIP chain ID.
|
|
19
19
|
*/
|
|
20
|
-
export declare const CaipReferenceStruct:
|
|
20
|
+
export declare const CaipReferenceStruct: Struct<string, null>;
|
|
21
21
|
export type CaipReference = Infer<typeof CaipReferenceStruct>;
|
|
22
22
|
/**
|
|
23
23
|
* A CAIP-10 account ID, i.e., a human-readable namespace, reference, and account address.
|
|
24
24
|
*/
|
|
25
|
-
export declare const CaipAccountIdStruct:
|
|
25
|
+
export declare const CaipAccountIdStruct: Struct<`${string}:${string}:${string}`, null>;
|
|
26
26
|
export type CaipAccountId = `${string}:${string}:${string}`;
|
|
27
27
|
/**
|
|
28
28
|
* A CAIP-10 account address, i.e., the third part of the CAIP account ID.
|
|
29
29
|
*/
|
|
30
|
-
export declare const CaipAccountAddressStruct:
|
|
30
|
+
export declare const CaipAccountAddressStruct: Struct<string, null>;
|
|
31
31
|
export type CaipAccountAddress = Infer<typeof CaipAccountAddressStruct>;
|
|
32
32
|
/** Known CAIP namespaces. */
|
|
33
33
|
export declare enum KnownCaipNamespace {
|
|
34
34
|
/** EIP-155 compatible chains. */
|
|
35
|
-
Eip155 = "eip155"
|
|
35
|
+
Eip155 = "eip155",
|
|
36
|
+
Wallet = "wallet"
|
|
36
37
|
}
|
|
37
38
|
/**
|
|
38
39
|
* Check if the given value is a {@link CaipChainId}.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"caip-types.d.cts","sourceRoot":"","sources":["../src/caip-types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"caip-types.d.cts","sourceRoot":"","sources":["../src/caip-types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAE3D,eAAO,MAAM,mBAAmB,QACqC,CAAC;AAEtE,eAAO,MAAM,oBAAoB,QAAsB,CAAC;AAExD,eAAO,MAAM,oBAAoB,QAA2B,CAAC;AAE7D,eAAO,MAAM,qBAAqB,QACwF,CAAC;AAE3H,eAAO,MAAM,0BAA0B,QAA6B,CAAC;AAErE;;GAEG;AACH,eAAO,MAAM,iBAAiB,qCAGA,CAAC;AAC/B,MAAM,MAAM,WAAW,GAAG,GAAG,MAAM,IAAI,MAAM,EAAE,CAAC;AAEhD;;GAEG;AACH,eAAO,MAAM,mBAAmB,sBAA0C,CAAC;AAC3E,MAAM,MAAM,aAAa,GAAG,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAE9D;;GAEG;AACH,eAAO,MAAM,mBAAmB,sBAA0C,CAAC;AAC3E,MAAM,MAAM,aAAa,GAAG,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAE9D;;GAEG;AACH,eAAO,MAAM,mBAAmB,+CAGA,CAAC;AACjC,MAAM,MAAM,aAAa,GAAG,GAAG,MAAM,IAAI,MAAM,IAAI,MAAM,EAAE,CAAC;AAE5D;;GAEG;AACH,eAAO,MAAM,wBAAwB,sBAGpC,CAAC;AACF,MAAM,MAAM,kBAAkB,GAAG,KAAK,CAAC,OAAO,wBAAwB,CAAC,CAAC;AAExE,6BAA6B;AAC7B,oBAAY,kBAAkB;IAC5B,iCAAiC;IACjC,MAAM,WAAW;IACjB,MAAM,WAAW;CAClB;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAElE;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAEtE;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAEtE;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAEtE;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,kBAAkB,CAE7B;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,EAAE,WAAW,GAAG;IAC1D,SAAS,EAAE,aAAa,CAAC;IACzB,SAAS,EAAE,aAAa,CAAC;CAC1B,CAUA;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,aAAa,EAAE,aAAa,GAAG;IAChE,OAAO,EAAE,kBAAkB,CAAC;IAC5B,OAAO,EAAE,WAAW,CAAC;IACrB,KAAK,EAAE;QAAE,SAAS,EAAE,aAAa,CAAC;QAAC,SAAS,EAAE,aAAa,CAAA;KAAE,CAAC;CAC/D,CAcA;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,aAAa,EACxB,SAAS,EAAE,aAAa,GACvB,WAAW,CAcb"}
|
package/dist/caip-types.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Infer } from "@metamask/superstruct";
|
|
1
|
+
import type { Infer, Struct } from "@metamask/superstruct";
|
|
2
2
|
export declare const CAIP_CHAIN_ID_REGEX: RegExp;
|
|
3
3
|
export declare const CAIP_NAMESPACE_REGEX: RegExp;
|
|
4
4
|
export declare const CAIP_REFERENCE_REGEX: RegExp;
|
|
@@ -7,32 +7,33 @@ export declare const CAIP_ACCOUNT_ADDRESS_REGEX: RegExp;
|
|
|
7
7
|
/**
|
|
8
8
|
* A CAIP-2 chain ID, i.e., a human-readable namespace and reference.
|
|
9
9
|
*/
|
|
10
|
-
export declare const CaipChainIdStruct:
|
|
10
|
+
export declare const CaipChainIdStruct: Struct<`${string}:${string}`, null>;
|
|
11
11
|
export type CaipChainId = `${string}:${string}`;
|
|
12
12
|
/**
|
|
13
13
|
* A CAIP-2 namespace, i.e., the first part of a CAIP chain ID.
|
|
14
14
|
*/
|
|
15
|
-
export declare const CaipNamespaceStruct:
|
|
15
|
+
export declare const CaipNamespaceStruct: Struct<string, null>;
|
|
16
16
|
export type CaipNamespace = Infer<typeof CaipNamespaceStruct>;
|
|
17
17
|
/**
|
|
18
18
|
* A CAIP-2 reference, i.e., the second part of a CAIP chain ID.
|
|
19
19
|
*/
|
|
20
|
-
export declare const CaipReferenceStruct:
|
|
20
|
+
export declare const CaipReferenceStruct: Struct<string, null>;
|
|
21
21
|
export type CaipReference = Infer<typeof CaipReferenceStruct>;
|
|
22
22
|
/**
|
|
23
23
|
* A CAIP-10 account ID, i.e., a human-readable namespace, reference, and account address.
|
|
24
24
|
*/
|
|
25
|
-
export declare const CaipAccountIdStruct:
|
|
25
|
+
export declare const CaipAccountIdStruct: Struct<`${string}:${string}:${string}`, null>;
|
|
26
26
|
export type CaipAccountId = `${string}:${string}:${string}`;
|
|
27
27
|
/**
|
|
28
28
|
* A CAIP-10 account address, i.e., the third part of the CAIP account ID.
|
|
29
29
|
*/
|
|
30
|
-
export declare const CaipAccountAddressStruct:
|
|
30
|
+
export declare const CaipAccountAddressStruct: Struct<string, null>;
|
|
31
31
|
export type CaipAccountAddress = Infer<typeof CaipAccountAddressStruct>;
|
|
32
32
|
/** Known CAIP namespaces. */
|
|
33
33
|
export declare enum KnownCaipNamespace {
|
|
34
34
|
/** EIP-155 compatible chains. */
|
|
35
|
-
Eip155 = "eip155"
|
|
35
|
+
Eip155 = "eip155",
|
|
36
|
+
Wallet = "wallet"
|
|
36
37
|
}
|
|
37
38
|
/**
|
|
38
39
|
* Check if the given value is a {@link CaipChainId}.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"caip-types.d.mts","sourceRoot":"","sources":["../src/caip-types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"caip-types.d.mts","sourceRoot":"","sources":["../src/caip-types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAE3D,eAAO,MAAM,mBAAmB,QACqC,CAAC;AAEtE,eAAO,MAAM,oBAAoB,QAAsB,CAAC;AAExD,eAAO,MAAM,oBAAoB,QAA2B,CAAC;AAE7D,eAAO,MAAM,qBAAqB,QACwF,CAAC;AAE3H,eAAO,MAAM,0BAA0B,QAA6B,CAAC;AAErE;;GAEG;AACH,eAAO,MAAM,iBAAiB,qCAGA,CAAC;AAC/B,MAAM,MAAM,WAAW,GAAG,GAAG,MAAM,IAAI,MAAM,EAAE,CAAC;AAEhD;;GAEG;AACH,eAAO,MAAM,mBAAmB,sBAA0C,CAAC;AAC3E,MAAM,MAAM,aAAa,GAAG,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAE9D;;GAEG;AACH,eAAO,MAAM,mBAAmB,sBAA0C,CAAC;AAC3E,MAAM,MAAM,aAAa,GAAG,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAE9D;;GAEG;AACH,eAAO,MAAM,mBAAmB,+CAGA,CAAC;AACjC,MAAM,MAAM,aAAa,GAAG,GAAG,MAAM,IAAI,MAAM,IAAI,MAAM,EAAE,CAAC;AAE5D;;GAEG;AACH,eAAO,MAAM,wBAAwB,sBAGpC,CAAC;AACF,MAAM,MAAM,kBAAkB,GAAG,KAAK,CAAC,OAAO,wBAAwB,CAAC,CAAC;AAExE,6BAA6B;AAC7B,oBAAY,kBAAkB;IAC5B,iCAAiC;IACjC,MAAM,WAAW;IACjB,MAAM,WAAW;CAClB;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAElE;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAEtE;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAEtE;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAEtE;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,kBAAkB,CAE7B;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,EAAE,WAAW,GAAG;IAC1D,SAAS,EAAE,aAAa,CAAC;IACzB,SAAS,EAAE,aAAa,CAAC;CAC1B,CAUA;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,aAAa,EAAE,aAAa,GAAG;IAChE,OAAO,EAAE,kBAAkB,CAAC;IAC5B,OAAO,EAAE,WAAW,CAAC;IACrB,KAAK,EAAE;QAAE,SAAS,EAAE,aAAa,CAAC;QAAC,SAAS,EAAE,aAAa,CAAA;KAAE,CAAC;CAC/D,CAcA;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,aAAa,EACxB,SAAS,EAAE,aAAa,GACvB,WAAW,CAcb"}
|
package/dist/caip-types.mjs
CHANGED
|
@@ -29,6 +29,7 @@ export var KnownCaipNamespace;
|
|
|
29
29
|
(function (KnownCaipNamespace) {
|
|
30
30
|
/** EIP-155 compatible chains. */
|
|
31
31
|
KnownCaipNamespace["Eip155"] = "eip155";
|
|
32
|
+
KnownCaipNamespace["Wallet"] = "wallet";
|
|
32
33
|
})(KnownCaipNamespace = KnownCaipNamespace || (KnownCaipNamespace = {}));
|
|
33
34
|
/**
|
|
34
35
|
* Check if the given value is a {@link CaipChainId}.
|
package/dist/caip-types.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"caip-types.mjs","sourceRoot":"","sources":["../src/caip-types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"caip-types.mjs","sourceRoot":"","sources":["../src/caip-types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,8BAA8B;AAG5D,MAAM,CAAC,MAAM,mBAAmB,GAC9B,mEAAmE,CAAC;AAEtE,MAAM,CAAC,MAAM,oBAAoB,GAAG,mBAAmB,CAAC;AAExD,MAAM,CAAC,MAAM,oBAAoB,GAAG,wBAAwB,CAAC;AAE7D,MAAM,CAAC,MAAM,qBAAqB,GAChC,wHAAwH,CAAC;AAE3H,MAAM,CAAC,MAAM,0BAA0B,GAAG,0BAA0B,CAAC;AAErE;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,OAAO,CACtC,MAAM,EAAE,EACR,mBAAmB,CACS,CAAC;AAG/B;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,oBAAoB,CAAC,CAAC;AAG3E;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,oBAAoB,CAAC,CAAC;AAG3E;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,OAAO,CACxC,MAAM,EAAE,EACR,qBAAqB,CACS,CAAC;AAGjC;;GAEG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,OAAO,CAC7C,MAAM,EAAE,EACR,0BAA0B,CAC3B,CAAC;AAGF,6BAA6B;AAC7B,MAAM,CAAN,IAAY,kBAIX;AAJD,WAAY,kBAAkB;IAC5B,iCAAiC;IACjC,uCAAiB,CAAA;IACjB,uCAAiB,CAAA;AACnB,CAAC,EAJW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C;IAED,OAAO;QACL,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,cAAoC;QAC1D,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,OAAsB;QAC5C,KAAK,EAAE;YACL,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,SAA0B;YAClD,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,SAA0B;SACnD;KACF,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,aAAa,CAC3B,SAAwB,EACxB,SAAwB;IAExB,IAAI,CAAC,eAAe,CAAC,SAAS,CAAC,EAAE;QAC/B,MAAM,IAAI,KAAK,CACb,oCAAoC,oBAAoB,CAAC,QAAQ,EAAE,EAAE,CACtE,CAAC;KACH;IAED,IAAI,CAAC,eAAe,CAAC,SAAS,CAAC,EAAE;QAC/B,MAAM,IAAI,KAAK,CACb,oCAAoC,oBAAoB,CAAC,QAAQ,EAAE,EAAE,CACtE,CAAC;KACH;IAED,OAAO,GAAG,SAAS,IAAI,SAAS,EAAE,CAAC;AACrC,CAAC","sourcesContent":["import { is, pattern, string } from '@metamask/superstruct';\nimport type { Infer, Struct } from '@metamask/superstruct';\n\nexport const CAIP_CHAIN_ID_REGEX =\n /^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})$/u;\n\nexport const CAIP_NAMESPACE_REGEX = /^[-a-z0-9]{3,8}$/u;\n\nexport const CAIP_REFERENCE_REGEX = /^[-_a-zA-Z0-9]{1,32}$/u;\n\nexport const CAIP_ACCOUNT_ID_REGEX =\n /^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})):(?<accountAddress>[-.%a-zA-Z0-9]{1,128})$/u;\n\nexport const CAIP_ACCOUNT_ADDRESS_REGEX = /^[-.%a-zA-Z0-9]{1,128}$/u;\n\n/**\n * A CAIP-2 chain ID, i.e., a human-readable namespace and reference.\n */\nexport const CaipChainIdStruct = pattern(\n string(),\n CAIP_CHAIN_ID_REGEX,\n) as Struct<CaipChainId, null>;\nexport type CaipChainId = `${string}:${string}`;\n\n/**\n * A CAIP-2 namespace, i.e., the first part of a CAIP chain ID.\n */\nexport const CaipNamespaceStruct = pattern(string(), CAIP_NAMESPACE_REGEX);\nexport type CaipNamespace = Infer<typeof CaipNamespaceStruct>;\n\n/**\n * A CAIP-2 reference, i.e., the second part of a CAIP chain ID.\n */\nexport const CaipReferenceStruct = pattern(string(), CAIP_REFERENCE_REGEX);\nexport type CaipReference = Infer<typeof CaipReferenceStruct>;\n\n/**\n * A CAIP-10 account ID, i.e., a human-readable namespace, reference, and account address.\n */\nexport const CaipAccountIdStruct = pattern(\n string(),\n CAIP_ACCOUNT_ID_REGEX,\n) as Struct<CaipAccountId, null>;\nexport type CaipAccountId = `${string}:${string}:${string}`;\n\n/**\n * A CAIP-10 account address, i.e., the third part of the CAIP account ID.\n */\nexport const CaipAccountAddressStruct = pattern(\n string(),\n CAIP_ACCOUNT_ADDRESS_REGEX,\n);\nexport type CaipAccountAddress = Infer<typeof CaipAccountAddressStruct>;\n\n/** Known CAIP namespaces. */\nexport enum KnownCaipNamespace {\n /** EIP-155 compatible chains. */\n Eip155 = 'eip155',\n Wallet = 'wallet',\n}\n\n/**\n * Check if the given value is a {@link CaipChainId}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipChainId}.\n */\nexport function isCaipChainId(value: unknown): value is CaipChainId {\n return is(value, CaipChainIdStruct);\n}\n\n/**\n * Check if the given value is a {@link CaipNamespace}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipNamespace}.\n */\nexport function isCaipNamespace(value: unknown): value is CaipNamespace {\n return is(value, CaipNamespaceStruct);\n}\n\n/**\n * Check if the given value is a {@link CaipReference}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipReference}.\n */\nexport function isCaipReference(value: unknown): value is CaipReference {\n return is(value, CaipReferenceStruct);\n}\n\n/**\n * Check if the given value is a {@link CaipAccountId}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipAccountId}.\n */\nexport function isCaipAccountId(value: unknown): value is CaipAccountId {\n return is(value, CaipAccountIdStruct);\n}\n\n/**\n * Check if a value is a {@link CaipAccountAddress}.\n *\n * @param value - The value to validate.\n * @returns True if the value is a valid {@link CaipAccountAddress}.\n */\nexport function isCaipAccountAddress(\n value: unknown,\n): value is CaipAccountAddress {\n return is(value, CaipAccountAddressStruct);\n}\n\n/**\n * Parse a CAIP-2 chain ID to an object containing the namespace and reference.\n * This validates the CAIP-2 chain ID before parsing it.\n *\n * @param caipChainId - The CAIP-2 chain ID to validate and parse.\n * @returns The parsed CAIP-2 chain ID.\n */\nexport function parseCaipChainId(caipChainId: CaipChainId): {\n namespace: CaipNamespace;\n reference: CaipReference;\n} {\n const match = CAIP_CHAIN_ID_REGEX.exec(caipChainId);\n if (!match?.groups) {\n throw new Error('Invalid CAIP chain ID.');\n }\n\n return {\n namespace: match.groups.namespace as CaipNamespace,\n reference: match.groups.reference as CaipReference,\n };\n}\n\n/**\n * Parse an CAIP-10 account ID to an object containing the chain ID, parsed chain ID, and account address.\n * This validates the CAIP-10 account ID before parsing it.\n *\n * @param caipAccountId - The CAIP-10 account ID to validate and parse.\n * @returns The parsed CAIP-10 account ID.\n */\nexport function parseCaipAccountId(caipAccountId: CaipAccountId): {\n address: CaipAccountAddress;\n chainId: CaipChainId;\n chain: { namespace: CaipNamespace; reference: CaipReference };\n} {\n const match = CAIP_ACCOUNT_ID_REGEX.exec(caipAccountId);\n if (!match?.groups) {\n throw new Error('Invalid CAIP account ID.');\n }\n\n return {\n address: match.groups.accountAddress as CaipAccountAddress,\n chainId: match.groups.chainId as CaipChainId,\n chain: {\n namespace: match.groups.namespace as CaipNamespace,\n reference: match.groups.reference as CaipReference,\n },\n };\n}\n\n/**\n * Chain ID as defined per the CAIP-2\n * {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md}.\n *\n * It defines a way to uniquely identify any blockchain in a human-readable\n * way.\n *\n * @param namespace - The standard (ecosystem) of similar blockchains.\n * @param reference - Identify of a blockchain within a given namespace.\n * @throws {@link Error}\n * This exception is thrown if the inputs does not comply with the CAIP-2\n * syntax specification\n * {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md#syntax}.\n * @returns A CAIP chain ID.\n */\nexport function toCaipChainId(\n namespace: CaipNamespace,\n reference: CaipReference,\n): CaipChainId {\n if (!isCaipNamespace(namespace)) {\n throw new Error(\n `Invalid \"namespace\", must match: ${CAIP_NAMESPACE_REGEX.toString()}`,\n );\n }\n\n if (!isCaipReference(reference)) {\n throw new Error(\n `Invalid \"reference\", must match: ${CAIP_REFERENCE_REGEX.toString()}`,\n );\n }\n\n return `${namespace}:${reference}`;\n}\n"]}
|
package/package.json
CHANGED