@metamask/utils 7.0.0 → 7.1.0

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 CHANGED
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [7.1.0]
10
+ ### Added
11
+ - CAIP-2 and CAIP-10 Types ([#116](https://github.com/MetaMask/utils/pull/116))
12
+
9
13
  ## [7.0.0]
10
14
  ### Added
11
15
  - Add `getKnownPropertyNames` function ([#111](https://github.com/MetaMask/utils/pull/111))
@@ -174,7 +178,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
174
178
  ### Added
175
179
  - Initial release
176
180
 
177
- [Unreleased]: https://github.com/MetaMask/utils/compare/v7.0.0...HEAD
181
+ [Unreleased]: https://github.com/MetaMask/utils/compare/v7.1.0...HEAD
182
+ [7.1.0]: https://github.com/MetaMask/utils/compare/v7.0.0...v7.1.0
178
183
  [7.0.0]: https://github.com/MetaMask/utils/compare/v6.2.0...v7.0.0
179
184
  [6.2.0]: https://github.com/MetaMask/utils/compare/v6.1.0...v6.2.0
180
185
  [6.1.0]: https://github.com/MetaMask/utils/compare/v6.0.1...v6.1.0
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ CAIP_CHAIN_ID_REGEX: function() {
13
+ return CAIP_CHAIN_ID_REGEX;
14
+ },
15
+ CAIP_NAMESPACE_REGEX: function() {
16
+ return CAIP_NAMESPACE_REGEX;
17
+ },
18
+ CAIP_REFERENCE_REGEX: function() {
19
+ return CAIP_REFERENCE_REGEX;
20
+ },
21
+ CAIP_ACCOUNT_ID_REGEX: function() {
22
+ return CAIP_ACCOUNT_ID_REGEX;
23
+ },
24
+ CAIP_ACCOUNT_ADDRESS_REGEX: function() {
25
+ return CAIP_ACCOUNT_ADDRESS_REGEX;
26
+ },
27
+ CaipChainIdStruct: function() {
28
+ return CaipChainIdStruct;
29
+ },
30
+ CaipNamespaceStruct: function() {
31
+ return CaipNamespaceStruct;
32
+ },
33
+ CaipReferenceStruct: function() {
34
+ return CaipReferenceStruct;
35
+ },
36
+ CaipAccountIdStruct: function() {
37
+ return CaipAccountIdStruct;
38
+ },
39
+ CaipAccountAddressStruct: function() {
40
+ return CaipAccountAddressStruct;
41
+ },
42
+ isCaipChainId: function() {
43
+ return isCaipChainId;
44
+ },
45
+ isCaipNamespace: function() {
46
+ return isCaipNamespace;
47
+ },
48
+ isCaipReference: function() {
49
+ return isCaipReference;
50
+ },
51
+ isCaipAccountId: function() {
52
+ return isCaipAccountId;
53
+ },
54
+ isCaipAccountAddress: function() {
55
+ return isCaipAccountAddress;
56
+ },
57
+ parseCaipChainId: function() {
58
+ return parseCaipChainId;
59
+ },
60
+ parseCaipAccountId: function() {
61
+ return parseCaipAccountId;
62
+ }
63
+ });
64
+ const _superstruct = require("superstruct");
65
+ const CAIP_CHAIN_ID_REGEX = RegExp("^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})$", "u");
66
+ const CAIP_NAMESPACE_REGEX = /^[-a-z0-9]{3,8}$/u;
67
+ const CAIP_REFERENCE_REGEX = /^[-_a-zA-Z0-9]{1,32}$/u;
68
+ const CAIP_ACCOUNT_ID_REGEX = RegExp("^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})):(?<accountAddress>[-.%a-zA-Z0-9]{1,128})$", "u");
69
+ const CAIP_ACCOUNT_ADDRESS_REGEX = /^[-.%a-zA-Z0-9]{1,128}$/u;
70
+ const CaipChainIdStruct = (0, _superstruct.pattern)((0, _superstruct.string)(), CAIP_CHAIN_ID_REGEX);
71
+ const CaipNamespaceStruct = (0, _superstruct.pattern)((0, _superstruct.string)(), CAIP_NAMESPACE_REGEX);
72
+ const CaipReferenceStruct = (0, _superstruct.pattern)((0, _superstruct.string)(), CAIP_REFERENCE_REGEX);
73
+ const CaipAccountIdStruct = (0, _superstruct.pattern)((0, _superstruct.string)(), CAIP_ACCOUNT_ID_REGEX);
74
+ const CaipAccountAddressStruct = (0, _superstruct.pattern)((0, _superstruct.string)(), CAIP_ACCOUNT_ADDRESS_REGEX);
75
+ function isCaipChainId(value) {
76
+ return (0, _superstruct.is)(value, CaipChainIdStruct);
77
+ }
78
+ function isCaipNamespace(value) {
79
+ return (0, _superstruct.is)(value, CaipNamespaceStruct);
80
+ }
81
+ function isCaipReference(value) {
82
+ return (0, _superstruct.is)(value, CaipReferenceStruct);
83
+ }
84
+ function isCaipAccountId(value) {
85
+ return (0, _superstruct.is)(value, CaipAccountIdStruct);
86
+ }
87
+ function isCaipAccountAddress(value) {
88
+ return (0, _superstruct.is)(value, CaipAccountAddressStruct);
89
+ }
90
+ function parseCaipChainId(caipChainId) {
91
+ const match = CAIP_CHAIN_ID_REGEX.exec(caipChainId);
92
+ if (!match?.groups) {
93
+ throw new Error('Invalid CAIP chain ID.');
94
+ }
95
+ return {
96
+ namespace: match.groups.namespace,
97
+ reference: match.groups.reference
98
+ };
99
+ }
100
+ function parseCaipAccountId(caipAccountId) {
101
+ const match = CAIP_ACCOUNT_ID_REGEX.exec(caipAccountId);
102
+ if (!match?.groups) {
103
+ throw new Error('Invalid CAIP account ID.');
104
+ }
105
+ return {
106
+ address: match.groups.accountAddress,
107
+ chainId: match.groups.chainId,
108
+ chain: {
109
+ namespace: match.groups.namespace,
110
+ reference: match.groups.reference
111
+ }
112
+ };
113
+ }
114
+
115
+ //# sourceMappingURL=caip-types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/caip-types.ts"],"sourcesContent":["import type { Infer } from 'superstruct';\nimport { is, pattern, string } from '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(string(), CAIP_CHAIN_ID_REGEX);\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(string(), CAIP_ACCOUNT_ID_REGEX);\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/**\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"],"names":["CAIP_CHAIN_ID_REGEX","CAIP_NAMESPACE_REGEX","CAIP_REFERENCE_REGEX","CAIP_ACCOUNT_ID_REGEX","CAIP_ACCOUNT_ADDRESS_REGEX","CaipChainIdStruct","CaipNamespaceStruct","CaipReferenceStruct","CaipAccountIdStruct","CaipAccountAddressStruct","isCaipChainId","isCaipNamespace","isCaipReference","isCaipAccountId","isCaipAccountAddress","parseCaipChainId","parseCaipAccountId","pattern","string","value","is","caipChainId","match","exec","groups","Error","namespace","reference","caipAccountId","address","accountAddress","chainId","chain"],"mappings":";;;;;;;;;;;IAGaA,mBAAmB;eAAnBA;;IAGAC,oBAAoB;eAApBA;;IAEAC,oBAAoB;eAApBA;;IAEAC,qBAAqB;eAArBA;;IAGAC,0BAA0B;eAA1BA;;IAKAC,iBAAiB;eAAjBA;;IAMAC,mBAAmB;eAAnBA;;IAMAC,mBAAmB;eAAnBA;;IAMAC,mBAAmB;eAAnBA;;IAMAC,wBAAwB;eAAxBA;;IAYGC,aAAa;eAAbA;;IAUAC,eAAe;eAAfA;;IAUAC,eAAe;eAAfA;;IAUAC,eAAe;eAAfA;;IAUAC,oBAAoB;eAApBA;;IAaAC,gBAAgB;eAAhBA;;IAsBAC,kBAAkB;eAAlBA;;;6BAhIoB;AAE7B,MAAMhB,sBACX;AAEK,MAAMC,uBAAuB;AAE7B,MAAMC,uBAAuB;AAE7B,MAAMC,wBACX;AAEK,MAAMC,6BAA6B;AAKnC,MAAMC,oBAAoBY,IAAAA,oBAAO,EAACC,IAAAA,mBAAM,KAAIlB;AAM5C,MAAMM,sBAAsBW,IAAAA,o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__esModule", {
5
5
  _export_star(require("./assert"), exports);
6
6
  _export_star(require("./base64"), exports);
7
7
  _export_star(require("./bytes"), exports);
8
+ _export_star(require("./caip-types"), exports);
8
9
  _export_star(require("./checksum"), exports);
9
10
  _export_star(require("./coercers"), exports);
10
11
  _export_star(require("./collections"), exports);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './assert';\nexport * from './base64';\nexport * from './bytes';\nexport * from './checksum';\nexport * from './coercers';\nexport * from './collections';\nexport * from './encryption-types';\nexport * from './hex';\nexport * from './json';\nexport * from './keyring';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './opaque';\nexport * from './time';\nexport * from './transaction-types';\nexport * from './versions';\n"],"names":[],"mappings":";;;;qBAAc;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA"}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './assert';\nexport * from './base64';\nexport * from './bytes';\nexport * from './caip-types';\nexport * from './checksum';\nexport * from './coercers';\nexport * from './collections';\nexport * from './encryption-types';\nexport * from './hex';\nexport * from './json';\nexport * from './keyring';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './opaque';\nexport * from './time';\nexport * from './transaction-types';\nexport * from './versions';\n"],"names":[],"mappings":";;;;qBAAc;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA"}
@@ -0,0 +1,99 @@
1
+ import { is, pattern, string } from 'superstruct';
2
+ export const CAIP_CHAIN_ID_REGEX = RegExp("^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})$", "u");
3
+ export const CAIP_NAMESPACE_REGEX = /^[-a-z0-9]{3,8}$/u;
4
+ export const CAIP_REFERENCE_REGEX = /^[-_a-zA-Z0-9]{1,32}$/u;
5
+ export const CAIP_ACCOUNT_ID_REGEX = RegExp("^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})):(?<accountAddress>[-.%a-zA-Z0-9]{1,128})$", "u");
6
+ export const CAIP_ACCOUNT_ADDRESS_REGEX = /^[-.%a-zA-Z0-9]{1,128}$/u;
7
+ /**
8
+ * A CAIP-2 chain ID, i.e., a human-readable namespace and reference.
9
+ */ export const CaipChainIdStruct = pattern(string(), CAIP_CHAIN_ID_REGEX);
10
+ /**
11
+ * A CAIP-2 namespace, i.e., the first part of a CAIP chain ID.
12
+ */ export const CaipNamespaceStruct = pattern(string(), CAIP_NAMESPACE_REGEX);
13
+ /**
14
+ * A CAIP-2 reference, i.e., the second part of a CAIP chain ID.
15
+ */ export const CaipReferenceStruct = pattern(string(), CAIP_REFERENCE_REGEX);
16
+ /**
17
+ * A CAIP-10 account ID, i.e., a human-readable namespace, reference, and account address.
18
+ */ export const CaipAccountIdStruct = pattern(string(), CAIP_ACCOUNT_ID_REGEX);
19
+ /**
20
+ * A CAIP-10 account address, i.e., the third part of the CAIP account ID.
21
+ */ export const CaipAccountAddressStruct = pattern(string(), CAIP_ACCOUNT_ADDRESS_REGEX);
22
+ /**
23
+ * Check if the given value is a {@link CaipChainId}.
24
+ *
25
+ * @param value - The value to check.
26
+ * @returns Whether the value is a {@link CaipChainId}.
27
+ */ export function isCaipChainId(value) {
28
+ return is(value, CaipChainIdStruct);
29
+ }
30
+ /**
31
+ * Check if the given value is a {@link CaipNamespace}.
32
+ *
33
+ * @param value - The value to check.
34
+ * @returns Whether the value is a {@link CaipNamespace}.
35
+ */ export function isCaipNamespace(value) {
36
+ return is(value, CaipNamespaceStruct);
37
+ }
38
+ /**
39
+ * Check if the given value is a {@link CaipReference}.
40
+ *
41
+ * @param value - The value to check.
42
+ * @returns Whether the value is a {@link CaipReference}.
43
+ */ export function isCaipReference(value) {
44
+ return is(value, CaipReferenceStruct);
45
+ }
46
+ /**
47
+ * Check if the given value is a {@link CaipAccountId}.
48
+ *
49
+ * @param value - The value to check.
50
+ * @returns Whether the value is a {@link CaipAccountId}.
51
+ */ export function isCaipAccountId(value) {
52
+ return is(value, CaipAccountIdStruct);
53
+ }
54
+ /**
55
+ * Check if a value is a {@link CaipAccountAddress}.
56
+ *
57
+ * @param value - The value to validate.
58
+ * @returns True if the value is a valid {@link CaipAccountAddress}.
59
+ */ export function isCaipAccountAddress(value) {
60
+ return is(value, CaipAccountAddressStruct);
61
+ }
62
+ /**
63
+ * Parse a CAIP-2 chain ID to an object containing the namespace and reference.
64
+ * This validates the CAIP-2 chain ID before parsing it.
65
+ *
66
+ * @param caipChainId - The CAIP-2 chain ID to validate and parse.
67
+ * @returns The parsed CAIP-2 chain ID.
68
+ */ export function parseCaipChainId(caipChainId) {
69
+ const match = CAIP_CHAIN_ID_REGEX.exec(caipChainId);
70
+ if (!match?.groups) {
71
+ throw new Error('Invalid CAIP chain ID.');
72
+ }
73
+ return {
74
+ namespace: match.groups.namespace,
75
+ reference: match.groups.reference
76
+ };
77
+ }
78
+ /**
79
+ * Parse an CAIP-10 account ID to an object containing the chain ID, parsed chain ID, and account address.
80
+ * This validates the CAIP-10 account ID before parsing it.
81
+ *
82
+ * @param caipAccountId - The CAIP-10 account ID to validate and parse.
83
+ * @returns The parsed CAIP-10 account ID.
84
+ */ export function parseCaipAccountId(caipAccountId) {
85
+ const match = CAIP_ACCOUNT_ID_REGEX.exec(caipAccountId);
86
+ if (!match?.groups) {
87
+ throw new Error('Invalid CAIP account ID.');
88
+ }
89
+ return {
90
+ address: match.groups.accountAddress,
91
+ chainId: match.groups.chainId,
92
+ chain: {
93
+ namespace: match.groups.namespace,
94
+ reference: match.groups.reference
95
+ }
96
+ };
97
+ }
98
+
99
+ //# sourceMappingURL=caip-types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/caip-types.ts"],"sourcesContent":["import type { Infer } from 'superstruct';\nimport { is, pattern, string } from '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(string(), CAIP_CHAIN_ID_REGEX);\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(string(), CAIP_ACCOUNT_ID_REGEX);\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/**\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"],"names":["is","pattern","string","CAIP_CHAIN_ID_REGEX","CAIP_NAMESPACE_REGEX","CAIP_REFERENCE_REGEX","CAIP_ACCOUNT_ID_REGEX","CAIP_ACCOUNT_ADDRESS_REGEX","CaipChainIdStruct","CaipNamespaceStruct","CaipReferenceStruct","CaipAccountIdStruct","CaipAccountAddressStruct","isCaipChainId","value","isCaipNamespace","isCaipReference","isCaipAccountId","isCaipAccountAddress","parseCaipChainId","caipChainId","match","exec","groups","Error","namespace","reference","parseCaipAccountId","caipAccountId","address","accountAddress","chainId","chain"],"mappings":"AACA,SAASA,EAAE,EAAEC,OAAO,EAAEC,MAAM,QAAQ,cAAc;AAElD,OAAO,MAAMC,sBACX,gFAAoE;AAEtE,OAAO,MAAMC,uBAAuB,oBAAoB;AAExD,OAAO,MAAMC,uBAAuB,yBAAyB;AAE7D,OAAO,MAAMC,wBACX,qIAAyH;AAE3H,OAAO,MAAMC,6BAA6B,2BAA2B;AAErE;;CAEC,GACD,OAAO,MAAMC,oBAAoBP,QAAQC,UAAUC,qBAAqB;AAGxE;;CAEC,GACD,OAAO,MAAMM,sBAAsBR,QAAQC,UAAUE,sBAAsB;AAG3E;;CAEC,GACD,OAAO,MAAMM,sBAAsBT,QAAQC,UAAUG,sBAAsB;AAG3E;;CAEC,GACD,OAAO,MAAMM,sBAAsBV,QAAQC,UAAUI,uBAAuB;AAG5E;;CAEC,GACD,OAAO,MAAMM,2BAA2BX,QACtCC,UACAK,4BACA;AAGF;;;;;CAKC,GACD,OAAO,SAASM,cAAcC,KAAc;IAC1C,OAAOd,GAAGc,OAAON;AACnB;AAEA;;;;;CAKC,GACD,OAAO,SAASO,gBAAgBD,KAAc;IAC5C,OAAOd,GAAGc,OAAOL;AACnB;AAEA;;;;;CAKC,GACD,OAAO,SAASO,gBAAgBF,KAAc;IAC5C,OAAOd,GAAGc,OAAOJ;AACnB;AAEA;;;;;CAKC,GACD,OAAO,SAASO,gBAAgBH,KAAc;IAC5C,OAAOd,GAAGc,OAAOH;AACnB;AAEA;;;;;CAKC,GACD,OAAO,SAASO,qBACdJ,KAAc;IAEd,OAAOd,GAAGc,OAAOF;AACnB;AAEA;;;;;;CAMC,GACD,OAAO,SAASO,iBAAiBC,WAAwB;IAIvD,MAAMC,QAAQlB,oBAAoBmB,IAAI,CAACF;IACvC,IAAI,CAACC,OAAOE,QAAQ;QAClB,MAAM,IAAIC,MAAM;IAClB;IAEA,OAAO;QACLC,WAAWJ,MAAME,MAAM,CAACE,SAAS;QACjCC,WAAWL,MAAME,MAAM,CAACG,SAAS;IACnC;AACF;AAEA;;;;;;CAMC,GACD,OAAO,SAASC,mBAAmBC,aAA4B;IAK7D,MAAMP,QAAQf,sBAAsBgB,IAAI,CAACM;IACzC,IAAI,CAACP,OAAOE,QAAQ;QAClB,MAAM,IAAIC,MAAM;IAClB;IAEA,OAAO;QACLK,SAASR,MAAME,MAAM,CAACO,cAAc;QACpCC,SAASV,MAAME,MAAM,CAACQ,OAAO;QAC7BC,OAAO;YACLP,WAAWJ,MAAME,MAAM,CAACE,SAAS;YACjCC,WAAWL,MAAME,MAAM,CAACG,SAAS;QACnC;IACF;AACF"}
package/dist/esm/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './assert';
2
2
  export * from './base64';
3
3
  export * from './bytes';
4
+ export * from './caip-types';
4
5
  export * from './checksum';
5
6
  export * from './coercers';
6
7
  export * from './collections';
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './assert';\nexport * from './base64';\nexport * from './bytes';\nexport * from './checksum';\nexport * from './coercers';\nexport * from './collections';\nexport * from './encryption-types';\nexport * from './hex';\nexport * from './json';\nexport * from './keyring';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './opaque';\nexport * from './time';\nexport * from './transaction-types';\nexport * from './versions';\n"],"names":[],"mappings":"AAAA,cAAc,WAAW;AACzB,cAAc,WAAW;AACzB,cAAc,UAAU;AACxB,cAAc,aAAa;AAC3B,cAAc,aAAa;AAC3B,cAAc,gBAAgB;AAC9B,cAAc,qBAAqB;AACnC,cAAc,QAAQ;AACtB,cAAc,SAAS;AACvB,cAAc,YAAY;AAC1B,cAAc,YAAY;AAC1B,cAAc,SAAS;AACvB,cAAc,WAAW;AACzB,cAAc,WAAW;AACzB,cAAc,SAAS;AACvB,cAAc,sBAAsB;AACpC,cAAc,aAAa"}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './assert';\nexport * from './base64';\nexport * from './bytes';\nexport * from './caip-types';\nexport * from './checksum';\nexport * from './coercers';\nexport * from './collections';\nexport * from './encryption-types';\nexport * from './hex';\nexport * from './json';\nexport * from './keyring';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './opaque';\nexport * from './time';\nexport * from './transaction-types';\nexport * from './versions';\n"],"names":[],"mappings":"AAAA,cAAc,WAAW;AACzB,cAAc,WAAW;AACzB,cAAc,UAAU;AACxB,cAAc,eAAe;AAC7B,cAAc,aAAa;AAC3B,cAAc,aAAa;AAC3B,cAAc,gBAAgB;AAC9B,cAAc,qBAAqB;AACnC,cAAc,QAAQ;AACtB,cAAc,SAAS;AACvB,cAAc,YAAY;AAC1B,cAAc,YAAY;AAC1B,cAAc,SAAS;AACvB,cAAc,WAAW;AACzB,cAAc,WAAW;AACzB,cAAc,SAAS;AACvB,cAAc,sBAAsB;AACpC,cAAc,aAAa"}
@@ -0,0 +1,93 @@
1
+ import type { Infer } from 'superstruct';
2
+ export declare const CAIP_CHAIN_ID_REGEX: RegExp;
3
+ export declare const CAIP_NAMESPACE_REGEX: RegExp;
4
+ export declare const CAIP_REFERENCE_REGEX: RegExp;
5
+ export declare const CAIP_ACCOUNT_ID_REGEX: RegExp;
6
+ export declare const CAIP_ACCOUNT_ADDRESS_REGEX: RegExp;
7
+ /**
8
+ * A CAIP-2 chain ID, i.e., a human-readable namespace and reference.
9
+ */
10
+ export declare const CaipChainIdStruct: import("superstruct").Struct<string, null>;
11
+ export declare type CaipChainId = `${string}:${string}`;
12
+ /**
13
+ * A CAIP-2 namespace, i.e., the first part of a CAIP chain ID.
14
+ */
15
+ export declare const CaipNamespaceStruct: import("superstruct").Struct<string, null>;
16
+ export declare type CaipNamespace = Infer<typeof CaipNamespaceStruct>;
17
+ /**
18
+ * A CAIP-2 reference, i.e., the second part of a CAIP chain ID.
19
+ */
20
+ export declare const CaipReferenceStruct: import("superstruct").Struct<string, null>;
21
+ export declare type CaipReference = Infer<typeof CaipReferenceStruct>;
22
+ /**
23
+ * A CAIP-10 account ID, i.e., a human-readable namespace, reference, and account address.
24
+ */
25
+ export declare const CaipAccountIdStruct: import("superstruct").Struct<string, null>;
26
+ export declare type CaipAccountId = `${string}:${string}:${string}`;
27
+ /**
28
+ * A CAIP-10 account address, i.e., the third part of the CAIP account ID.
29
+ */
30
+ export declare const CaipAccountAddressStruct: import("superstruct").Struct<string, null>;
31
+ export declare type CaipAccountAddress = Infer<typeof CaipAccountAddressStruct>;
32
+ /**
33
+ * Check if the given value is a {@link CaipChainId}.
34
+ *
35
+ * @param value - The value to check.
36
+ * @returns Whether the value is a {@link CaipChainId}.
37
+ */
38
+ export declare function isCaipChainId(value: unknown): value is CaipChainId;
39
+ /**
40
+ * Check if the given value is a {@link CaipNamespace}.
41
+ *
42
+ * @param value - The value to check.
43
+ * @returns Whether the value is a {@link CaipNamespace}.
44
+ */
45
+ export declare function isCaipNamespace(value: unknown): value is CaipNamespace;
46
+ /**
47
+ * Check if the given value is a {@link CaipReference}.
48
+ *
49
+ * @param value - The value to check.
50
+ * @returns Whether the value is a {@link CaipReference}.
51
+ */
52
+ export declare function isCaipReference(value: unknown): value is CaipReference;
53
+ /**
54
+ * Check if the given value is a {@link CaipAccountId}.
55
+ *
56
+ * @param value - The value to check.
57
+ * @returns Whether the value is a {@link CaipAccountId}.
58
+ */
59
+ export declare function isCaipAccountId(value: unknown): value is CaipAccountId;
60
+ /**
61
+ * Check if a value is a {@link CaipAccountAddress}.
62
+ *
63
+ * @param value - The value to validate.
64
+ * @returns True if the value is a valid {@link CaipAccountAddress}.
65
+ */
66
+ export declare function isCaipAccountAddress(value: unknown): value is CaipAccountAddress;
67
+ /**
68
+ * Parse a CAIP-2 chain ID to an object containing the namespace and reference.
69
+ * This validates the CAIP-2 chain ID before parsing it.
70
+ *
71
+ * @param caipChainId - The CAIP-2 chain ID to validate and parse.
72
+ * @returns The parsed CAIP-2 chain ID.
73
+ */
74
+ export declare function parseCaipChainId(caipChainId: CaipChainId): {
75
+ namespace: CaipNamespace;
76
+ reference: CaipReference;
77
+ };
78
+ /**
79
+ * Parse an CAIP-10 account ID to an object containing the chain ID, parsed chain ID, and account address.
80
+ * This validates the CAIP-10 account ID before parsing it.
81
+ *
82
+ * @param caipAccountId - The CAIP-10 account ID to validate and parse.
83
+ * @returns The parsed CAIP-10 account ID.
84
+ */
85
+ export declare function parseCaipAccountId(caipAccountId: CaipAccountId): {
86
+ address: CaipAccountAddress;
87
+ chainId: CaipChainId;
88
+ chain: {
89
+ namespace: CaipNamespace;
90
+ reference: CaipReference;
91
+ };
92
+ };
93
+ //# sourceMappingURL=caip-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-types.d.ts","sourceRoot":"","sources":["../../src/caip-types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAGzC,eAAO,MAAM,mBAAmB,QACqC,CAAC;AAEtE,eAAO,MAAM,oBAAoB,QAAsB,CAAC;AAExD,eAAO,MAAM,oBAAoB,QAA2B,CAAC;AAE7D,eAAO,MAAM,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E,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"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=caip-types.test-d.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-types.test-d.d.ts","sourceRoot":"","sources":["../../src/caip-types.test-d.ts"],"names":[],"mappings":""}
@@ -1,6 +1,7 @@
1
1
  export * from './assert';
2
2
  export * from './base64';
3
3
  export * from './bytes';
4
+ export * from './caip-types';
4
5
  export * from './checksum';
5
6
  export * from './coercers';
6
7
  export * from './collections';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,OAAO,CAAC;AACtB,cAAc,QAAQ,CAAC;AACvB,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,OAAO,CAAC;AACtB,cAAc,QAAQ,CAAC;AACvB,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/utils",
3
- "version": "7.0.0",
3
+ "version": "7.1.0",
4
4
  "description": "Various JavaScript/TypeScript utilities of wide relevance to the MetaMask codebase",
5
5
  "homepage": "https://github.com/MetaMask/utils#readme",
6
6
  "bugs": {