@metamask/utils 11.4.1 → 11.4.2

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
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [11.4.2]
11
+
12
+ ### Fixed
13
+
14
+ - Improve performance of `isValidChecksumAddress` and `isValidHexAddress` functions ([#248](https://github.com/MetaMask/utils/pull/248))
15
+
10
16
  ## [11.4.1]
11
17
 
12
18
  ### Fixed
@@ -421,7 +427,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
421
427
 
422
428
  - Initial release
423
429
 
424
- [Unreleased]: https://github.com/MetaMask/utils/compare/v11.4.1...HEAD
430
+ [Unreleased]: https://github.com/MetaMask/utils/compare/v11.4.2...HEAD
431
+ [11.4.2]: https://github.com/MetaMask/utils/compare/v11.4.1...v11.4.2
425
432
  [11.4.1]: https://github.com/MetaMask/utils/compare/v11.4.0...v11.4.1
426
433
  [11.4.0]: https://github.com/MetaMask/utils/compare/v11.3.0...v11.4.0
427
434
  [11.3.0]: https://github.com/MetaMask/utils/compare/v11.2.0...v11.3.0
package/dist/hex.cjs CHANGED
@@ -3,15 +3,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.remove0x = exports.add0x = exports.isValidChecksumAddress = exports.getChecksumAddress = exports.getChecksumAddressUnmemoized = exports.isValidHexAddress = exports.assertIsStrictHexString = exports.assertIsHexString = exports.isStrictHexString = exports.isHexString = exports.HexChecksumAddressStruct = exports.HexAddressStruct = exports.StrictHexStruct = exports.HexStruct = void 0;
6
+ exports.remove0x = exports.add0x = exports.isValidHexAddress = exports.isValidHexAddressUnmemoized = exports.isValidChecksumAddress = exports.isValidChecksumAddressUnmemoized = exports.getChecksumAddress = exports.getChecksumAddressUnmemoized = exports.assertIsStrictHexString = exports.assertIsHexString = exports.isHexChecksumAddress = exports.isHexAddress = exports.isStrictHexString = exports.isHexString = exports.HexChecksumAddressStruct = exports.HexAddressStruct = exports.StrictHexStruct = exports.HexStruct = void 0;
7
7
  const superstruct_1 = require("@metamask/superstruct");
8
8
  const sha3_1 = require("@noble/hashes/sha3");
9
9
  const lodash_memoize_1 = __importDefault(require("lodash.memoize"));
10
10
  const assert_1 = require("./assert.cjs");
11
- exports.HexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^(?:0x)?[0-9a-f]+$/iu);
12
- exports.StrictHexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-f]+$/iu);
13
- exports.HexAddressStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-f]{40}$/u);
14
- exports.HexChecksumAddressStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-fA-F]{40}$/u);
11
+ // Use native regexes instead of superstruct for maximum performance.
12
+ // Pre-compiled regex for maximum performance - avoids recompilation on each call
13
+ const HEX_REGEX = /^(?:0x)?[0-9a-f]+$/iu;
14
+ const STRICT_HEX_REGEX = /^0x[0-9a-f]+$/iu;
15
+ const HEX_ADDRESS_REGEX = /^0x[0-9a-f]{40}$/u;
16
+ const HEX_CHECKSUM_ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/u;
17
+ exports.HexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), HEX_REGEX);
18
+ exports.StrictHexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), STRICT_HEX_REGEX);
19
+ exports.HexAddressStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), HEX_ADDRESS_REGEX);
20
+ exports.HexChecksumAddressStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), HEX_CHECKSUM_ADDRESS_REGEX);
21
+ const isString = (value) => typeof value === 'string';
15
22
  /**
16
23
  * Check if a string is a valid hex string.
17
24
  *
@@ -19,7 +26,7 @@ exports.HexChecksumAddressStruct = (0, superstruct_1.pattern)((0, superstruct_1.
19
26
  * @returns Whether the value is a valid hex string.
20
27
  */
21
28
  function isHexString(value) {
22
- return (0, superstruct_1.is)(value, exports.HexStruct);
29
+ return isString(value) && HEX_REGEX.test(value);
23
30
  }
24
31
  exports.isHexString = isHexString;
25
32
  /**
@@ -30,9 +37,29 @@ exports.isHexString = isHexString;
30
37
  * @returns Whether the value is a valid hex string.
31
38
  */
32
39
  function isStrictHexString(value) {
33
- return (0, superstruct_1.is)(value, exports.StrictHexStruct);
40
+ return isString(value) && STRICT_HEX_REGEX.test(value);
34
41
  }
35
42
  exports.isStrictHexString = isStrictHexString;
43
+ /**
44
+ * Check if a string is a valid hex address.
45
+ *
46
+ * @param value - The value to check.
47
+ * @returns Whether the value is a valid hex address.
48
+ */
49
+ function isHexAddress(value) {
50
+ return isString(value) && HEX_ADDRESS_REGEX.test(value);
51
+ }
52
+ exports.isHexAddress = isHexAddress;
53
+ /**
54
+ * Check if a string is a valid hex checksum address.
55
+ *
56
+ * @param value - The value to check.
57
+ * @returns Whether the value is a valid hex checksum address.
58
+ */
59
+ function isHexChecksumAddress(value) {
60
+ return isString(value) && HEX_CHECKSUM_ADDRESS_REGEX.test(value);
61
+ }
62
+ exports.isHexChecksumAddress = isHexChecksumAddress;
36
63
  /**
37
64
  * Assert that a value is a valid hex string.
38
65
  *
@@ -54,18 +81,6 @@ function assertIsStrictHexString(value) {
54
81
  (0, assert_1.assert)(isStrictHexString(value), 'Value must be a hexadecimal string, starting with "0x".');
55
82
  }
56
83
  exports.assertIsStrictHexString = assertIsStrictHexString;
57
- /**
58
- * Validate that the passed prefixed hex string is an all-lowercase
59
- * hex address, or a valid mixed-case checksum address.
60
- *
61
- * @param possibleAddress - Input parameter to check against.
62
- * @returns Whether or not the input is a valid hex address.
63
- */
64
- function isValidHexAddress(possibleAddress) {
65
- return ((0, superstruct_1.is)(possibleAddress, exports.HexAddressStruct) ||
66
- isValidChecksumAddress(possibleAddress));
67
- }
68
- exports.isValidHexAddress = isValidHexAddress;
69
84
  /**
70
85
  * Encode a passed hex string as an ERC-55 mixed-case checksum address.
71
86
  * This is the unmemoized version, primarily used for testing.
@@ -75,7 +90,7 @@ exports.isValidHexAddress = isValidHexAddress;
75
90
  * @see https://eips.ethereum.org/EIPS/eip-55
76
91
  */
77
92
  function getChecksumAddressUnmemoized(hexAddress) {
78
- (0, assert_1.assert)((0, superstruct_1.is)(hexAddress, exports.HexChecksumAddressStruct), 'Invalid hex address.');
93
+ (0, assert_1.assert)(isHexChecksumAddress(hexAddress), 'Invalid hex address.');
79
94
  const address = remove0x(hexAddress).toLowerCase();
80
95
  const hashBytes = (0, sha3_1.keccak_256)(address);
81
96
  const { length } = address;
@@ -108,13 +123,40 @@ exports.getChecksumAddress = (0, lodash_memoize_1.default)(getChecksumAddressUnm
108
123
  * @param possibleChecksum - The hex address to check.
109
124
  * @returns True if the address is a checksum address.
110
125
  */
111
- function isValidChecksumAddress(possibleChecksum) {
112
- if (!(0, superstruct_1.is)(possibleChecksum, exports.HexChecksumAddressStruct)) {
126
+ function isValidChecksumAddressUnmemoized(possibleChecksum) {
127
+ if (!isHexChecksumAddress(possibleChecksum)) {
113
128
  return false;
114
129
  }
115
130
  return (0, exports.getChecksumAddress)(possibleChecksum) === possibleChecksum;
116
131
  }
117
- exports.isValidChecksumAddress = isValidChecksumAddress;
132
+ exports.isValidChecksumAddressUnmemoized = isValidChecksumAddressUnmemoized;
133
+ /**
134
+ * Validate that the passed hex string is a valid ERC-55 mixed-case
135
+ * checksum address.
136
+ *
137
+ * @param possibleChecksum - The hex address to check.
138
+ * @returns True if the address is a checksum address.
139
+ */
140
+ exports.isValidChecksumAddress = (0, lodash_memoize_1.default)(isValidChecksumAddressUnmemoized);
141
+ /**
142
+ * Validate that the passed prefixed hex string is an all-lowercase
143
+ * hex address, or a valid mixed-case checksum address.
144
+ *
145
+ * @param possibleAddress - Input parameter to check against.
146
+ * @returns Whether or not the input is a valid hex address.
147
+ */
148
+ function isValidHexAddressUnmemoized(possibleAddress) {
149
+ return (isHexAddress(possibleAddress) || (0, exports.isValidChecksumAddress)(possibleAddress));
150
+ }
151
+ exports.isValidHexAddressUnmemoized = isValidHexAddressUnmemoized;
152
+ /**
153
+ * Validate that the passed prefixed hex string is an all-lowercase
154
+ * hex address, or a valid mixed-case checksum address.
155
+ *
156
+ * @param possibleAddress - Input parameter to check against.
157
+ * @returns Whether or not the input is a valid hex address.
158
+ */
159
+ exports.isValidHexAddress = (0, lodash_memoize_1.default)(isValidHexAddressUnmemoized);
118
160
  /**
119
161
  * Add the `0x`-prefix to a hexadecimal string. If the string already has the
120
162
  * prefix, it is returned as-is.
package/dist/hex.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"hex.cjs","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":";;;;;;AACA,uDAA4D;AAC5D,6CAA6D;AAC7D,oEAAqC;AAErC,yCAAkC;AAIrB,QAAA,SAAS,GAAG,IAAA,qBAAO,EAAC,IAAA,oBAAM,GAAE,EAAE,sBAAsB,CAAC,CAAC;AACtD,QAAA,eAAe,GAAG,IAAA,qBAAO,EAAC,IAAA,oBAAM,GAAE,EAAE,iBAAiB,CAGjE,CAAC;AACW,QAAA,gBAAgB,GAAG,IAAA,qBAAO,EACrC,IAAA,oBAAM,GAAE,EACR,mBAAmB,CACC,CAAC;AACV,QAAA,wBAAwB,GAAG,IAAA,qBAAO,EAC7C,IAAA,oBAAM,GAAE,EACR,sBAAsB,CACF,CAAC;AAEvB;;;;;GAKG;AACH,SAAgB,WAAW,CAAC,KAAc;IACxC,OAAO,IAAA,gBAAE,EAAC,KAAK,EAAE,iBAAS,CAAC,CAAC;AAC9B,CAAC;AAFD,kCAEC;AAED;;;;;;GAMG;AACH,SAAgB,iBAAiB,CAAC,KAAc;IAC9C,OAAO,IAAA,gBAAE,EAAC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CAAC,CAAC,CAAC;KACjC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC;AAND,4BAMC","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { is, pattern, string } from '@metamask/superstruct';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\nimport memoize from 'lodash.memoize';\n\nimport { assert } from './assert';\n\nexport type Hex = `0x${string}`;\n\nexport const HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu);\nexport const StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu) as Struct<\n Hex,\n null\n>;\nexport const HexAddressStruct = pattern(\n string(),\n /^0x[0-9a-f]{40}$/u,\n) as Struct<Hex, null>;\nexport const HexChecksumAddressStruct = pattern(\n string(),\n /^0x[0-9a-fA-F]{40}$/u,\n) as Struct<Hex, null>;\n\n/**\n * Check if a string is a valid hex string.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isHexString(value: unknown): value is string {\n return is(value, HexStruct);\n}\n\n/**\n * Strictly check if a string is a valid hex string. A valid hex string must\n * start with the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isStrictHexString(value: unknown): value is Hex {\n return is(value, StrictHexStruct);\n}\n\n/**\n * Assert that a value is a valid hex string.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsHexString(value: unknown): asserts value is string {\n assert(isHexString(value), 'Value must be a hexadecimal string.');\n}\n\n/**\n * Assert that a value is a valid hex string. A valid hex string must start with\n * the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsStrictHexString(value: unknown): asserts value is Hex {\n assert(\n isStrictHexString(value),\n 'Value must be a hexadecimal string, starting with \"0x\".',\n );\n}\n\n/**\n * Validate that the passed prefixed hex string is an all-lowercase\n * hex address, or a valid mixed-case checksum address.\n *\n * @param possibleAddress - Input parameter to check against.\n * @returns Whether or not the input is a valid hex address.\n */\nexport function isValidHexAddress(possibleAddress: Hex) {\n return (\n is(possibleAddress, HexAddressStruct) ||\n isValidChecksumAddress(possibleAddress)\n );\n}\n\n/**\n * Encode a passed hex string as an ERC-55 mixed-case checksum address.\n * This is the unmemoized version, primarily used for testing.\n *\n * @param hexAddress - The hex address to encode.\n * @returns The address encoded according to ERC-55.\n * @see https://eips.ethereum.org/EIPS/eip-55\n */\nexport function getChecksumAddressUnmemoized(hexAddress: Hex): Hex {\n assert(is(hexAddress, HexChecksumAddressStruct), 'Invalid hex address.');\n const address = remove0x(hexAddress).toLowerCase();\n\n const hashBytes = keccak256(address);\n const { length } = address;\n const result = new Array(length); // Pre-allocate array\n\n for (let i = 0; i < length; i++) {\n /* eslint-disable no-bitwise */\n const byteIndex = i >> 1; // Faster than Math.floor(i / 2)\n const nibbleIndex = i & 1; // Faster than i % 2\n const byte = hashBytes[byteIndex] as number;\n const nibble = nibbleIndex === 0 ? byte >> 4 : byte & 0x0f;\n /* eslint-enable no-bitwise */\n\n result[i] = nibble >= 8 ? (address[i] as string).toUpperCase() : address[i];\n }\n\n return `0x${result.join('')}`;\n}\n\n/**\n * Encode a passed hex string as an ERC-55 mixed-case checksum address.\n * This function is memoized for performance.\n *\n * @param hexAddress - The hex address to encode.\n * @returns The address encoded according to ERC-55.\n * @see https://eips.ethereum.org/EIPS/eip-55\n */\nexport const getChecksumAddress = memoize(getChecksumAddressUnmemoized);\n\n/**\n * Validate that the passed hex string is a valid ERC-55 mixed-case\n * checksum address.\n *\n * @param possibleChecksum - The hex address to check.\n * @returns True if the address is a checksum address.\n */\nexport function isValidChecksumAddress(possibleChecksum: Hex) {\n if (!is(possibleChecksum, HexChecksumAddressStruct)) {\n return false;\n }\n\n return getChecksumAddress(possibleChecksum) === possibleChecksum;\n}\n\n/**\n * Add the `0x`-prefix to a hexadecimal string. If the string already has the\n * prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to add the prefix to.\n * @returns The prefixed hexadecimal string.\n */\nexport function add0x(hexadecimal: string): Hex {\n if (hexadecimal.startsWith('0x')) {\n return hexadecimal as Hex;\n }\n\n if (hexadecimal.startsWith('0X')) {\n return `0x${hexadecimal.substring(2)}`;\n }\n\n return `0x${hexadecimal}`;\n}\n\n/**\n * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have\n * the prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to remove the prefix from.\n * @returns The un-prefixed hexadecimal string.\n */\nexport function remove0x(hexadecimal: string): string {\n if (hexadecimal.startsWith('0x') || hexadecimal.startsWith('0X')) {\n return hexadecimal.substring(2);\n }\n\n return hexadecimal;\n}\n"]}
1
+ {"version":3,"file":"hex.cjs","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":";;;;;;AAAA,uDAAqE;AACrE,6CAA6D;AAC7D,oEAAqC;AAErC,yCAAkC;AAIlC,qEAAqE;AACrE,iFAAiF;AACjF,MAAM,SAAS,GAAG,sBAAsB,CAAC;AACzC,MAAM,gBAAgB,GAAG,iBAAiB,CAAC;AAC3C,MAAM,iBAAiB,GAAG,mBAAmB,CAAC;AAC9C,MAAM,0BAA0B,GAAG,sBAAsB,CAAC;AAE7C,QAAA,SAAS,GAAG,IAAA,qBAAO,EAAC,IAAA,oBAAM,GAAE,EAAE,SAAS,CAAC,CAAC;AACzC,QAAA,eAAe,GAAG,IAAA,qBAAO,EAAC,IAAA,oBAAM,GAAE,EAAE,gBAAgB,CAGhE,CAAC;AACW,QAAA,gBAAgB,GAAG,IAAA,qBAAO,EAAC,IAAA,oBAAM,GAAE,EAAE,iBAAiB,CAGlE,CAAC;AACW,QAAA,wBAAwB,GAAG,IAAA,qBAAO,EAC7C,IAAA,oBAAM,GAAE,EACR,0BAA0B,CACN,CAAC;AAEvB,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAmB,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC;AAEhF;;;;;GAKG;AACH,SAAgB,WAAW,CAAC,KAAc;IACxC,OAAO,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AAC,eAAe,CAAC,CACzE,CAAC;AACJ,CAAC;AAJD,kEAIC;AAED;;;;;;GAMG;AACU,QAAA,iBAAiB,GAAG,IAAA,wBAAO,EAAC,2BAA2B,CAAC,CAAC;AAEtE;;;;;;GAMG;AACH,SAAgB,KAAK,CAAC,WAAmB;IACvC,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAChC,OAAO,WAAkB,CAAC;KAC3B;IAED,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAChC,OAAO,KAAK,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;KACxC;IAED,OAAO,KAAK,WAAW,EAAE,CAAC;AAC5B,CAAC;AAVD,sBAUC;AAED;;;;;;GAMG;AACH,SAAgB,QAAQ,CAAC,WAAmB;IAC1C,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAChE,OAAO,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;KACjC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC;AAND,4BAMC","sourcesContent":["import { pattern, type Struct, string } from '@metamask/superstruct';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\nimport memoize from 'lodash.memoize';\n\nimport { assert } from './assert';\n\nexport type Hex = `0x${string}`;\n\n// Use native regexes instead of superstruct for maximum performance.\n// Pre-compiled regex for maximum performance - avoids recompilation on each call\nconst HEX_REGEX = /^(?:0x)?[0-9a-f]+$/iu;\nconst STRICT_HEX_REGEX = /^0x[0-9a-f]+$/iu;\nconst HEX_ADDRESS_REGEX = /^0x[0-9a-f]{40}$/u;\nconst HEX_CHECKSUM_ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/u;\n\nexport const HexStruct = pattern(string(), HEX_REGEX);\nexport const StrictHexStruct = pattern(string(), STRICT_HEX_REGEX) as Struct<\n Hex,\n null\n>;\nexport const HexAddressStruct = pattern(string(), HEX_ADDRESS_REGEX) as Struct<\n Hex,\n null\n>;\nexport const HexChecksumAddressStruct = pattern(\n string(),\n HEX_CHECKSUM_ADDRESS_REGEX,\n) as Struct<Hex, null>;\n\nconst isString = (value: unknown): value is string => typeof value === 'string';\n\n/**\n * Check if a string is a valid hex string.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isHexString(value: unknown): value is string {\n return isString(value) && HEX_REGEX.test(value);\n}\n\n/**\n * Strictly check if a string is a valid hex string. A valid hex string must\n * start with the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isStrictHexString(value: unknown): value is Hex {\n return isString(value) && STRICT_HEX_REGEX.test(value);\n}\n\n/**\n * Check if a string is a valid hex address.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex address.\n */\nexport function isHexAddress(value: unknown): value is Hex {\n return isString(value) && HEX_ADDRESS_REGEX.test(value);\n}\n\n/**\n * Check if a string is a valid hex checksum address.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex checksum address.\n */\nexport function isHexChecksumAddress(value: unknown): value is Hex {\n return isString(value) && HEX_CHECKSUM_ADDRESS_REGEX.test(value);\n}\n\n/**\n * Assert that a value is a valid hex string.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsHexString(value: unknown): asserts value is string {\n assert(isHexString(value), 'Value must be a hexadecimal string.');\n}\n\n/**\n * Assert that a value is a valid hex string. A valid hex string must start with\n * the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsStrictHexString(value: unknown): asserts value is Hex {\n assert(\n isStrictHexString(value),\n 'Value must be a hexadecimal string, starting with \"0x\".',\n );\n}\n\n/**\n * Encode a passed hex string as an ERC-55 mixed-case checksum address.\n * This is the unmemoized version, primarily used for testing.\n *\n * @param hexAddress - The hex address to encode.\n * @returns The address encoded according to ERC-55.\n * @see https://eips.ethereum.org/EIPS/eip-55\n */\nexport function getChecksumAddressUnmemoized(hexAddress: Hex): Hex {\n assert(isHexChecksumAddress(hexAddress), 'Invalid hex address.');\n const address = remove0x(hexAddress).toLowerCase();\n\n const hashBytes = keccak256(address);\n const { length } = address;\n const result = new Array(length); // Pre-allocate array\n\n for (let i = 0; i < length; i++) {\n /* eslint-disable no-bitwise */\n const byteIndex = i >> 1; // Faster than Math.floor(i / 2)\n const nibbleIndex = i & 1; // Faster than i % 2\n const byte = hashBytes[byteIndex] as number;\n const nibble = nibbleIndex === 0 ? byte >> 4 : byte & 0x0f;\n /* eslint-enable no-bitwise */\n\n result[i] = nibble >= 8 ? (address[i] as string).toUpperCase() : address[i];\n }\n\n return `0x${result.join('')}`;\n}\n\n/**\n * Encode a passed hex string as an ERC-55 mixed-case checksum address.\n * This function is memoized for performance.\n *\n * @param hexAddress - The hex address to encode.\n * @returns The address encoded according to ERC-55.\n * @see https://eips.ethereum.org/EIPS/eip-55\n */\nexport const getChecksumAddress = memoize(getChecksumAddressUnmemoized);\n\n/**\n * Validate that the passed hex string is a valid ERC-55 mixed-case\n * checksum address.\n *\n * @param possibleChecksum - The hex address to check.\n * @returns True if the address is a checksum address.\n */\nexport function isValidChecksumAddressUnmemoized(possibleChecksum: Hex) {\n if (!isHexChecksumAddress(possibleChecksum)) {\n return false;\n }\n\n return getChecksumAddress(possibleChecksum) === possibleChecksum;\n}\n\n/**\n * Validate that the passed hex string is a valid ERC-55 mixed-case\n * checksum address.\n *\n * @param possibleChecksum - The hex address to check.\n * @returns True if the address is a checksum address.\n */\nexport const isValidChecksumAddress = memoize(isValidChecksumAddressUnmemoized);\n\n/**\n * Validate that the passed prefixed hex string is an all-lowercase\n * hex address, or a valid mixed-case checksum address.\n *\n * @param possibleAddress - Input parameter to check against.\n * @returns Whether or not the input is a valid hex address.\n */\nexport function isValidHexAddressUnmemoized(possibleAddress: Hex) {\n return (\n isHexAddress(possibleAddress) || isValidChecksumAddress(possibleAddress)\n );\n}\n\n/**\n * Validate that the passed prefixed hex string is an all-lowercase\n * hex address, or a valid mixed-case checksum address.\n *\n * @param possibleAddress - Input parameter to check against.\n * @returns Whether or not the input is a valid hex address.\n */\nexport const isValidHexAddress = memoize(isValidHexAddressUnmemoized);\n\n/**\n * Add the `0x`-prefix to a hexadecimal string. If the string already has the\n * prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to add the prefix to.\n * @returns The prefixed hexadecimal string.\n */\nexport function add0x(hexadecimal: string): Hex {\n if (hexadecimal.startsWith('0x')) {\n return hexadecimal as Hex;\n }\n\n if (hexadecimal.startsWith('0X')) {\n return `0x${hexadecimal.substring(2)}`;\n }\n\n return `0x${hexadecimal}`;\n}\n\n/**\n * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have\n * the prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to remove the prefix from.\n * @returns The un-prefixed hexadecimal string.\n */\nexport function remove0x(hexadecimal: string): string {\n if (hexadecimal.startsWith('0x') || hexadecimal.startsWith('0X')) {\n return hexadecimal.substring(2);\n }\n\n return hexadecimal;\n}\n"]}
package/dist/hex.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  /// <reference types="lodash" />
2
- import type { Struct } from "@metamask/superstruct";
2
+ import { type Struct } from "@metamask/superstruct";
3
3
  export type Hex = `0x${string}`;
4
4
  export declare const HexStruct: Struct<string, null>;
5
5
  export declare const StrictHexStruct: Struct<`0x${string}`, null>;
@@ -20,6 +20,20 @@ export declare function isHexString(value: unknown): value is string;
20
20
  * @returns Whether the value is a valid hex string.
21
21
  */
22
22
  export declare function isStrictHexString(value: unknown): value is Hex;
23
+ /**
24
+ * Check if a string is a valid hex address.
25
+ *
26
+ * @param value - The value to check.
27
+ * @returns Whether the value is a valid hex address.
28
+ */
29
+ export declare function isHexAddress(value: unknown): value is Hex;
30
+ /**
31
+ * Check if a string is a valid hex checksum address.
32
+ *
33
+ * @param value - The value to check.
34
+ * @returns Whether the value is a valid hex checksum address.
35
+ */
36
+ export declare function isHexChecksumAddress(value: unknown): value is Hex;
23
37
  /**
24
38
  * Assert that a value is a valid hex string.
25
39
  *
@@ -35,14 +49,6 @@ export declare function assertIsHexString(value: unknown): asserts value is stri
35
49
  * @throws If the value is not a valid hex string.
36
50
  */
37
51
  export declare function assertIsStrictHexString(value: unknown): asserts value is Hex;
38
- /**
39
- * Validate that the passed prefixed hex string is an all-lowercase
40
- * hex address, or a valid mixed-case checksum address.
41
- *
42
- * @param possibleAddress - Input parameter to check against.
43
- * @returns Whether or not the input is a valid hex address.
44
- */
45
- export declare function isValidHexAddress(possibleAddress: Hex): boolean;
46
52
  /**
47
53
  * Encode a passed hex string as an ERC-55 mixed-case checksum address.
48
54
  * This is the unmemoized version, primarily used for testing.
@@ -68,7 +74,31 @@ export declare const getChecksumAddress: typeof getChecksumAddressUnmemoized & i
68
74
  * @param possibleChecksum - The hex address to check.
69
75
  * @returns True if the address is a checksum address.
70
76
  */
71
- export declare function isValidChecksumAddress(possibleChecksum: Hex): boolean;
77
+ export declare function isValidChecksumAddressUnmemoized(possibleChecksum: Hex): boolean;
78
+ /**
79
+ * Validate that the passed hex string is a valid ERC-55 mixed-case
80
+ * checksum address.
81
+ *
82
+ * @param possibleChecksum - The hex address to check.
83
+ * @returns True if the address is a checksum address.
84
+ */
85
+ export declare const isValidChecksumAddress: typeof isValidChecksumAddressUnmemoized & import("lodash").MemoizedFunction;
86
+ /**
87
+ * Validate that the passed prefixed hex string is an all-lowercase
88
+ * hex address, or a valid mixed-case checksum address.
89
+ *
90
+ * @param possibleAddress - Input parameter to check against.
91
+ * @returns Whether or not the input is a valid hex address.
92
+ */
93
+ export declare function isValidHexAddressUnmemoized(possibleAddress: Hex): boolean;
94
+ /**
95
+ * Validate that the passed prefixed hex string is an all-lowercase
96
+ * hex address, or a valid mixed-case checksum address.
97
+ *
98
+ * @param possibleAddress - Input parameter to check against.
99
+ * @returns Whether or not the input is a valid hex address.
100
+ */
101
+ export declare const isValidHexAddress: typeof isValidHexAddressUnmemoized & import("lodash").MemoizedFunction;
72
102
  /**
73
103
  * Add the `0x`-prefix to a hexadecimal string. If the string already has the
74
104
  * prefix, it is returned as-is.
@@ -1 +1 @@
1
- {"version":3,"file":"hex.d.cts","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAOpD,MAAM,MAAM,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AAEhC,eAAO,MAAM,SAAS,sBAA4C,CAAC;AACnE,eAAO,MAAM,eAAe,6BAG3B,CAAC;AACF,eAAO,MAAM,gBAAgB,6BAGP,CAAC;AACvB,eAAO,MAAM,wBAAwB,6BAGf,CAAC;AAEvB;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE3D;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,GAAG,CAE9D;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAEzE;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,GAAG,CAK5E;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,eAAe,EAAE,GAAG,WAKrD;AAED;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAAC,UAAU,EAAE,GAAG,GAAG,GAAG,CAoBjE;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,yEAAwC,CAAC;AAExE;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,gBAAgB,EAAE,GAAG,WAM3D;AAED;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,WAAW,EAAE,MAAM,GAAG,GAAG,CAU9C;AAED;;;;;;GAMG;AACH,wBAA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AEhF;;;;;;GAMG;AACH,wBAAgB,2BAA2B,CAAC,eAAe,EAAE,GAAG,WAI/D;AAED;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,wEAAuC,CAAC;AAEtE;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,WAAW,EAAE,MAAM,GAAG,GAAG,CAU9C;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAMpD"}
package/dist/hex.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  /// <reference types="lodash" />
2
- import type { Struct } from "@metamask/superstruct";
2
+ import { type Struct } from "@metamask/superstruct";
3
3
  export type Hex = `0x${string}`;
4
4
  export declare const HexStruct: Struct<string, null>;
5
5
  export declare const StrictHexStruct: Struct<`0x${string}`, null>;
@@ -20,6 +20,20 @@ export declare function isHexString(value: unknown): value is string;
20
20
  * @returns Whether the value is a valid hex string.
21
21
  */
22
22
  export declare function isStrictHexString(value: unknown): value is Hex;
23
+ /**
24
+ * Check if a string is a valid hex address.
25
+ *
26
+ * @param value - The value to check.
27
+ * @returns Whether the value is a valid hex address.
28
+ */
29
+ export declare function isHexAddress(value: unknown): value is Hex;
30
+ /**
31
+ * Check if a string is a valid hex checksum address.
32
+ *
33
+ * @param value - The value to check.
34
+ * @returns Whether the value is a valid hex checksum address.
35
+ */
36
+ export declare function isHexChecksumAddress(value: unknown): value is Hex;
23
37
  /**
24
38
  * Assert that a value is a valid hex string.
25
39
  *
@@ -35,14 +49,6 @@ export declare function assertIsHexString(value: unknown): asserts value is stri
35
49
  * @throws If the value is not a valid hex string.
36
50
  */
37
51
  export declare function assertIsStrictHexString(value: unknown): asserts value is Hex;
38
- /**
39
- * Validate that the passed prefixed hex string is an all-lowercase
40
- * hex address, or a valid mixed-case checksum address.
41
- *
42
- * @param possibleAddress - Input parameter to check against.
43
- * @returns Whether or not the input is a valid hex address.
44
- */
45
- export declare function isValidHexAddress(possibleAddress: Hex): boolean;
46
52
  /**
47
53
  * Encode a passed hex string as an ERC-55 mixed-case checksum address.
48
54
  * This is the unmemoized version, primarily used for testing.
@@ -68,7 +74,31 @@ export declare const getChecksumAddress: typeof getChecksumAddressUnmemoized & i
68
74
  * @param possibleChecksum - The hex address to check.
69
75
  * @returns True if the address is a checksum address.
70
76
  */
71
- export declare function isValidChecksumAddress(possibleChecksum: Hex): boolean;
77
+ export declare function isValidChecksumAddressUnmemoized(possibleChecksum: Hex): boolean;
78
+ /**
79
+ * Validate that the passed hex string is a valid ERC-55 mixed-case
80
+ * checksum address.
81
+ *
82
+ * @param possibleChecksum - The hex address to check.
83
+ * @returns True if the address is a checksum address.
84
+ */
85
+ export declare const isValidChecksumAddress: typeof isValidChecksumAddressUnmemoized & import("lodash").MemoizedFunction;
86
+ /**
87
+ * Validate that the passed prefixed hex string is an all-lowercase
88
+ * hex address, or a valid mixed-case checksum address.
89
+ *
90
+ * @param possibleAddress - Input parameter to check against.
91
+ * @returns Whether or not the input is a valid hex address.
92
+ */
93
+ export declare function isValidHexAddressUnmemoized(possibleAddress: Hex): boolean;
94
+ /**
95
+ * Validate that the passed prefixed hex string is an all-lowercase
96
+ * hex address, or a valid mixed-case checksum address.
97
+ *
98
+ * @param possibleAddress - Input parameter to check against.
99
+ * @returns Whether or not the input is a valid hex address.
100
+ */
101
+ export declare const isValidHexAddress: typeof isValidHexAddressUnmemoized & import("lodash").MemoizedFunction;
72
102
  /**
73
103
  * Add the `0x`-prefix to a hexadecimal string. If the string already has the
74
104
  * prefix, it is returned as-is.
@@ -1 +1 @@
1
- {"version":3,"file":"hex.d.mts","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAOpD,MAAM,MAAM,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AAEhC,eAAO,MAAM,SAAS,sBAA4C,CAAC;AACnE,eAAO,MAAM,eAAe,6BAG3B,CAAC;AACF,eAAO,MAAM,gBAAgB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;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE3D;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,GAAG,CAE9D;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,GAAG,CAEzD;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,GAAG,CAEjE;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAEzE;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,GAAG,CAK5E;AAED;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAAC,UAAU,EAAE,GAAG,GAAG,GAAG,CAoBjE;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,yEAAwC,CAAC;AAExE;;;;;;GAMG;AACH,wBAAgB,gCAAgC,CAAC,gBAAgB,EAAE,GAAG,WAMrE;AAED;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,6EAA4C,CAAC;AAEhF;;;;;;GAMG;AACH,wBAAgB,2BAA2B,CAAC,eAAe,EAAE,GAAG,WAI/D;AAED;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,wEAAuC,CAAC;AAEtE;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,WAAW,EAAE,MAAM,GAAG,GAAG,CAU9C;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAMpD"}
package/dist/hex.mjs CHANGED
@@ -1,11 +1,18 @@
1
- import { is, pattern, string } from "@metamask/superstruct";
1
+ import { pattern, string } from "@metamask/superstruct";
2
2
  import { keccak_256 as keccak256 } from "@noble/hashes/sha3";
3
3
  import memoize from "lodash.memoize";
4
4
  import { assert } from "./assert.mjs";
5
- export const HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu);
6
- export const StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu);
7
- export const HexAddressStruct = pattern(string(), /^0x[0-9a-f]{40}$/u);
8
- export const HexChecksumAddressStruct = pattern(string(), /^0x[0-9a-fA-F]{40}$/u);
5
+ // Use native regexes instead of superstruct for maximum performance.
6
+ // Pre-compiled regex for maximum performance - avoids recompilation on each call
7
+ const HEX_REGEX = /^(?:0x)?[0-9a-f]+$/iu;
8
+ const STRICT_HEX_REGEX = /^0x[0-9a-f]+$/iu;
9
+ const HEX_ADDRESS_REGEX = /^0x[0-9a-f]{40}$/u;
10
+ const HEX_CHECKSUM_ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/u;
11
+ export const HexStruct = pattern(string(), HEX_REGEX);
12
+ export const StrictHexStruct = pattern(string(), STRICT_HEX_REGEX);
13
+ export const HexAddressStruct = pattern(string(), HEX_ADDRESS_REGEX);
14
+ export const HexChecksumAddressStruct = pattern(string(), HEX_CHECKSUM_ADDRESS_REGEX);
15
+ const isString = (value) => typeof value === 'string';
9
16
  /**
10
17
  * Check if a string is a valid hex string.
11
18
  *
@@ -13,7 +20,7 @@ export const HexChecksumAddressStruct = pattern(string(), /^0x[0-9a-fA-F]{40}$/u
13
20
  * @returns Whether the value is a valid hex string.
14
21
  */
15
22
  export function isHexString(value) {
16
- return is(value, HexStruct);
23
+ return isString(value) && HEX_REGEX.test(value);
17
24
  }
18
25
  /**
19
26
  * Strictly check if a string is a valid hex string. A valid hex string must
@@ -23,7 +30,25 @@ export function isHexString(value) {
23
30
  * @returns Whether the value is a valid hex string.
24
31
  */
25
32
  export function isStrictHexString(value) {
26
- return is(value, StrictHexStruct);
33
+ return isString(value) && STRICT_HEX_REGEX.test(value);
34
+ }
35
+ /**
36
+ * Check if a string is a valid hex address.
37
+ *
38
+ * @param value - The value to check.
39
+ * @returns Whether the value is a valid hex address.
40
+ */
41
+ export function isHexAddress(value) {
42
+ return isString(value) && HEX_ADDRESS_REGEX.test(value);
43
+ }
44
+ /**
45
+ * Check if a string is a valid hex checksum address.
46
+ *
47
+ * @param value - The value to check.
48
+ * @returns Whether the value is a valid hex checksum address.
49
+ */
50
+ export function isHexChecksumAddress(value) {
51
+ return isString(value) && HEX_CHECKSUM_ADDRESS_REGEX.test(value);
27
52
  }
28
53
  /**
29
54
  * Assert that a value is a valid hex string.
@@ -44,17 +69,6 @@ export function assertIsHexString(value) {
44
69
  export function assertIsStrictHexString(value) {
45
70
  assert(isStrictHexString(value), 'Value must be a hexadecimal string, starting with "0x".');
46
71
  }
47
- /**
48
- * Validate that the passed prefixed hex string is an all-lowercase
49
- * hex address, or a valid mixed-case checksum address.
50
- *
51
- * @param possibleAddress - Input parameter to check against.
52
- * @returns Whether or not the input is a valid hex address.
53
- */
54
- export function isValidHexAddress(possibleAddress) {
55
- return (is(possibleAddress, HexAddressStruct) ||
56
- isValidChecksumAddress(possibleAddress));
57
- }
58
72
  /**
59
73
  * Encode a passed hex string as an ERC-55 mixed-case checksum address.
60
74
  * This is the unmemoized version, primarily used for testing.
@@ -64,7 +78,7 @@ export function isValidHexAddress(possibleAddress) {
64
78
  * @see https://eips.ethereum.org/EIPS/eip-55
65
79
  */
66
80
  export function getChecksumAddressUnmemoized(hexAddress) {
67
- assert(is(hexAddress, HexChecksumAddressStruct), 'Invalid hex address.');
81
+ assert(isHexChecksumAddress(hexAddress), 'Invalid hex address.');
68
82
  const address = remove0x(hexAddress).toLowerCase();
69
83
  const hashBytes = keccak256(address);
70
84
  const { length } = address;
@@ -96,12 +110,38 @@ export const getChecksumAddress = memoize(getChecksumAddressUnmemoized);
96
110
  * @param possibleChecksum - The hex address to check.
97
111
  * @returns True if the address is a checksum address.
98
112
  */
99
- export function isValidChecksumAddress(possibleChecksum) {
100
- if (!is(possibleChecksum, HexChecksumAddressStruct)) {
113
+ export function isValidChecksumAddressUnmemoized(possibleChecksum) {
114
+ if (!isHexChecksumAddress(possibleChecksum)) {
101
115
  return false;
102
116
  }
103
117
  return getChecksumAddress(possibleChecksum) === possibleChecksum;
104
118
  }
119
+ /**
120
+ * Validate that the passed hex string is a valid ERC-55 mixed-case
121
+ * checksum address.
122
+ *
123
+ * @param possibleChecksum - The hex address to check.
124
+ * @returns True if the address is a checksum address.
125
+ */
126
+ export const isValidChecksumAddress = memoize(isValidChecksumAddressUnmemoized);
127
+ /**
128
+ * Validate that the passed prefixed hex string is an all-lowercase
129
+ * hex address, or a valid mixed-case checksum address.
130
+ *
131
+ * @param possibleAddress - Input parameter to check against.
132
+ * @returns Whether or not the input is a valid hex address.
133
+ */
134
+ export function isValidHexAddressUnmemoized(possibleAddress) {
135
+ return (isHexAddress(possibleAddress) || isValidChecksumAddress(possibleAddress));
136
+ }
137
+ /**
138
+ * Validate that the passed prefixed hex string is an all-lowercase
139
+ * hex address, or a valid mixed-case checksum address.
140
+ *
141
+ * @param possibleAddress - Input parameter to check against.
142
+ * @returns Whether or not the input is a valid hex address.
143
+ */
144
+ export const isValidHexAddress = memoize(isValidHexAddressUnmemoized);
105
145
  /**
106
146
  * Add the `0x`-prefix to a hexadecimal string. If the string already has the
107
147
  * prefix, it is returned as-is.
package/dist/hex.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"hex.mjs","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,8BAA8B;AAC5D,OAAO,EAAE,UAAU,IAAI,SAAS,EAAE,2BAA2B;AAC7D,OAAO,OAAO,uBAAuB;AAErC,OAAO,EAAE,MAAM,EAAE,qBAAiB;AAIlC,MAAM,CAAC,MAAM,SAAS,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,sBAAsB,CAAC,CAAC;AACnE,MAAM,CAAC,MAAM,eAAe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AAI,CAAC,EAAE;QAChC,OAAO,KAAK,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;KACxC;IAED,OAAO,KAAK,WAAW,EAAE,CAAC;AAC5B,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CAAC,WAAmB;IAC1C,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAChE,OAAO,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;KACjC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { is, pattern, string } from '@metamask/superstruct';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\nimport memoize from 'lodash.memoize';\n\nimport { assert } from './assert';\n\nexport type Hex = `0x${string}`;\n\nexport const HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu);\nexport const StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu) as Struct<\n Hex,\n null\n>;\nexport const HexAddressStruct = pattern(\n string(),\n /^0x[0-9a-f]{40}$/u,\n) as Struct<Hex, null>;\nexport const HexChecksumAddressStruct = pattern(\n string(),\n /^0x[0-9a-fA-F]{40}$/u,\n) as Struct<Hex, null>;\n\n/**\n * Check if a string is a valid hex string.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isHexString(value: unknown): value is string {\n return is(value, HexStruct);\n}\n\n/**\n * Strictly check if a string is a valid hex string. A valid hex string must\n * start with the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isStrictHexString(value: unknown): value is Hex {\n return is(value, StrictHexStruct);\n}\n\n/**\n * Assert that a value is a valid hex string.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsHexString(value: unknown): asserts value is string {\n assert(isHexString(value), 'Value must be a hexadecimal string.');\n}\n\n/**\n * Assert that a value is a valid hex string. A valid hex string must start with\n * the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsStrictHexString(value: unknown): asserts value is Hex {\n assert(\n isStrictHexString(value),\n 'Value must be a hexadecimal string, starting with \"0x\".',\n );\n}\n\n/**\n * Validate that the passed prefixed hex string is an all-lowercase\n * hex address, or a valid mixed-case checksum address.\n *\n * @param possibleAddress - Input parameter to check against.\n * @returns Whether or not the input is a valid hex address.\n */\nexport function isValidHexAddress(possibleAddress: Hex) {\n return (\n is(possibleAddress, HexAddressStruct) ||\n isValidChecksumAddress(possibleAddress)\n );\n}\n\n/**\n * Encode a passed hex string as an ERC-55 mixed-case checksum address.\n * This is the unmemoized version, primarily used for testing.\n *\n * @param hexAddress - The hex address to encode.\n * @returns The address encoded according to ERC-55.\n * @see https://eips.ethereum.org/EIPS/eip-55\n */\nexport function getChecksumAddressUnmemoized(hexAddress: Hex): Hex {\n assert(is(hexAddress, HexChecksumAddressStruct), 'Invalid hex address.');\n const address = remove0x(hexAddress).toLowerCase();\n\n const hashBytes = keccak256(address);\n const { length } = address;\n const result = new Array(length); // Pre-allocate array\n\n for (let i = 0; i < length; i++) {\n /* eslint-disable no-bitwise */\n const byteIndex = i >> 1; // Faster than Math.floor(i / 2)\n const nibbleIndex = i & 1; // Faster than i % 2\n const byte = hashBytes[byteIndex] as number;\n const nibble = nibbleIndex === 0 ? byte >> 4 : byte & 0x0f;\n /* eslint-enable no-bitwise */\n\n result[i] = nibble >= 8 ? (address[i] as string).toUpperCase() : address[i];\n }\n\n return `0x${result.join('')}`;\n}\n\n/**\n * Encode a passed hex string as an ERC-55 mixed-case checksum address.\n * This function is memoized for performance.\n *\n * @param hexAddress - The hex address to encode.\n * @returns The address encoded according to ERC-55.\n * @see https://eips.ethereum.org/EIPS/eip-55\n */\nexport const getChecksumAddress = memoize(getChecksumAddressUnmemoized);\n\n/**\n * Validate that the passed hex string is a valid ERC-55 mixed-case\n * checksum address.\n *\n * @param possibleChecksum - The hex address to check.\n * @returns True if the address is a checksum address.\n */\nexport function isValidChecksumAddress(possibleChecksum: Hex) {\n if (!is(possibleChecksum, HexChecksumAddressStruct)) {\n return false;\n }\n\n return getChecksumAddress(possibleChecksum) === possibleChecksum;\n}\n\n/**\n * Add the `0x`-prefix to a hexadecimal string. If the string already has the\n * prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to add the prefix to.\n * @returns The prefixed hexadecimal string.\n */\nexport function add0x(hexadecimal: string): Hex {\n if (hexadecimal.startsWith('0x')) {\n return hexadecimal as Hex;\n }\n\n if (hexadecimal.startsWith('0X')) {\n return `0x${hexadecimal.substring(2)}`;\n }\n\n return `0x${hexadecimal}`;\n}\n\n/**\n * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have\n * the prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to remove the prefix from.\n * @returns The un-prefixed hexadecimal string.\n */\nexport function remove0x(hexadecimal: string): string {\n if (hexadecimal.startsWith('0x') || hexadecimal.startsWith('0X')) {\n return hexadecimal.substring(2);\n }\n\n return hexadecimal;\n}\n"]}
1
+ {"version":3,"file":"hex.mjs","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,EAAE,8BAA8B;AACrE,OAAO,EAAE,UAAU,IAAI,SAAS,EAAE,2BAA2B;AAC7D,OAAO,OAAO,uBAAuB;AAErC,OAAO,EAAE,MAAM,EAAE,qBAAiB;AAIlC,qEAAqE;AACrE,iFAAiF;AACjF,MAAM,SAAS,GAAG,sBAAsB,CAAC;AACzC,MAAM,gBAAgB,GAAG,iBAAiB,CAAC;AAC3C,MAAM,iBAAiB,GAAG,mBAAmB,CAAC;AAC9C,MAAM,0BAA0B,GAAG,sBAAsB,CAAC;AAE1D,MAAM,CAAC,MAAM,SAAS,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,SAAS,CAAC,CAAC;AACtD,M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CAAC,EAAE;QAC3C,OAAO,KAAK,CAAC;KACd;IAED,OAAO,kBAAkB,CAAC,gBAAgB,CAAC,KAAK,gBAAgB,CAAC;AACnE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,OAAO,CAAC,gCAAgC,CAAC,CAAC;AAEhF;;;;;;GAMG;AACH,MAAM,UAAU,2BAA2B,CAAC,eAAoB;IAC9D,OAAO,CACL,YAAY,CAAC,eAAe,CAAC,IAAI,sBAAsB,CAAC,eAAe,CAAC,CACzE,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,OAAO,CAAC,2BAA2B,CAAC,CAAC;AAEtE;;;;;;GAMG;AACH,MAAM,UAAU,KAAK,CAAC,WAAmB;IACvC,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAChC,OAAO,WAAkB,CAAC;KAC3B;IAED,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAChC,OAAO,KAAK,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;KACxC;IAED,OAAO,KAAK,WAAW,EAAE,CAAC;AAC5B,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CAAC,WAAmB;IAC1C,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAChE,OAAO,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;KACjC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import { pattern, type Struct, string } from '@metamask/superstruct';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\nimport memoize from 'lodash.memoize';\n\nimport { assert } from './assert';\n\nexport type Hex = `0x${string}`;\n\n// Use native regexes instead of superstruct for maximum performance.\n// Pre-compiled regex for maximum performance - avoids recompilation on each call\nconst HEX_REGEX = /^(?:0x)?[0-9a-f]+$/iu;\nconst STRICT_HEX_REGEX = /^0x[0-9a-f]+$/iu;\nconst HEX_ADDRESS_REGEX = /^0x[0-9a-f]{40}$/u;\nconst HEX_CHECKSUM_ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/u;\n\nexport const HexStruct = pattern(string(), HEX_REGEX);\nexport const StrictHexStruct = pattern(string(), STRICT_HEX_REGEX) as Struct<\n Hex,\n null\n>;\nexport const HexAddressStruct = pattern(string(), HEX_ADDRESS_REGEX) as Struct<\n Hex,\n null\n>;\nexport const HexChecksumAddressStruct = pattern(\n string(),\n HEX_CHECKSUM_ADDRESS_REGEX,\n) as Struct<Hex, null>;\n\nconst isString = (value: unknown): value is string => typeof value === 'string';\n\n/**\n * Check if a string is a valid hex string.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isHexString(value: unknown): value is string {\n return isString(value) && HEX_REGEX.test(value);\n}\n\n/**\n * Strictly check if a string is a valid hex string. A valid hex string must\n * start with the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isStrictHexString(value: unknown): value is Hex {\n return isString(value) && STRICT_HEX_REGEX.test(value);\n}\n\n/**\n * Check if a string is a valid hex address.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex address.\n */\nexport function isHexAddress(value: unknown): value is Hex {\n return isString(value) && HEX_ADDRESS_REGEX.test(value);\n}\n\n/**\n * Check if a string is a valid hex checksum address.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex checksum address.\n */\nexport function isHexChecksumAddress(value: unknown): value is Hex {\n return isString(value) && HEX_CHECKSUM_ADDRESS_REGEX.test(value);\n}\n\n/**\n * Assert that a value is a valid hex string.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsHexString(value: unknown): asserts value is string {\n assert(isHexString(value), 'Value must be a hexadecimal string.');\n}\n\n/**\n * Assert that a value is a valid hex string. A valid hex string must start with\n * the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsStrictHexString(value: unknown): asserts value is Hex {\n assert(\n isStrictHexString(value),\n 'Value must be a hexadecimal string, starting with \"0x\".',\n );\n}\n\n/**\n * Encode a passed hex string as an ERC-55 mixed-case checksum address.\n * This is the unmemoized version, primarily used for testing.\n *\n * @param hexAddress - The hex address to encode.\n * @returns The address encoded according to ERC-55.\n * @see https://eips.ethereum.org/EIPS/eip-55\n */\nexport function getChecksumAddressUnmemoized(hexAddress: Hex): Hex {\n assert(isHexChecksumAddress(hexAddress), 'Invalid hex address.');\n const address = remove0x(hexAddress).toLowerCase();\n\n const hashBytes = keccak256(address);\n const { length } = address;\n const result = new Array(length); // Pre-allocate array\n\n for (let i = 0; i < length; i++) {\n /* eslint-disable no-bitwise */\n const byteIndex = i >> 1; // Faster than Math.floor(i / 2)\n const nibbleIndex = i & 1; // Faster than i % 2\n const byte = hashBytes[byteIndex] as number;\n const nibble = nibbleIndex === 0 ? byte >> 4 : byte & 0x0f;\n /* eslint-enable no-bitwise */\n\n result[i] = nibble >= 8 ? (address[i] as string).toUpperCase() : address[i];\n }\n\n return `0x${result.join('')}`;\n}\n\n/**\n * Encode a passed hex string as an ERC-55 mixed-case checksum address.\n * This function is memoized for performance.\n *\n * @param hexAddress - The hex address to encode.\n * @returns The address encoded according to ERC-55.\n * @see https://eips.ethereum.org/EIPS/eip-55\n */\nexport const getChecksumAddress = memoize(getChecksumAddressUnmemoized);\n\n/**\n * Validate that the passed hex string is a valid ERC-55 mixed-case\n * checksum address.\n *\n * @param possibleChecksum - The hex address to check.\n * @returns True if the address is a checksum address.\n */\nexport function isValidChecksumAddressUnmemoized(possibleChecksum: Hex) {\n if (!isHexChecksumAddress(possibleChecksum)) {\n return false;\n }\n\n return getChecksumAddress(possibleChecksum) === possibleChecksum;\n}\n\n/**\n * Validate that the passed hex string is a valid ERC-55 mixed-case\n * checksum address.\n *\n * @param possibleChecksum - The hex address to check.\n * @returns True if the address is a checksum address.\n */\nexport const isValidChecksumAddress = memoize(isValidChecksumAddressUnmemoized);\n\n/**\n * Validate that the passed prefixed hex string is an all-lowercase\n * hex address, or a valid mixed-case checksum address.\n *\n * @param possibleAddress - Input parameter to check against.\n * @returns Whether or not the input is a valid hex address.\n */\nexport function isValidHexAddressUnmemoized(possibleAddress: Hex) {\n return (\n isHexAddress(possibleAddress) || isValidChecksumAddress(possibleAddress)\n );\n}\n\n/**\n * Validate that the passed prefixed hex string is an all-lowercase\n * hex address, or a valid mixed-case checksum address.\n *\n * @param possibleAddress - Input parameter to check against.\n * @returns Whether or not the input is a valid hex address.\n */\nexport const isValidHexAddress = memoize(isValidHexAddressUnmemoized);\n\n/**\n * Add the `0x`-prefix to a hexadecimal string. If the string already has the\n * prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to add the prefix to.\n * @returns The prefixed hexadecimal string.\n */\nexport function add0x(hexadecimal: string): Hex {\n if (hexadecimal.startsWith('0x')) {\n return hexadecimal as Hex;\n }\n\n if (hexadecimal.startsWith('0X')) {\n return `0x${hexadecimal.substring(2)}`;\n }\n\n return `0x${hexadecimal}`;\n}\n\n/**\n * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have\n * the prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to remove the prefix from.\n * @returns The un-prefixed hexadecimal string.\n */\nexport function remove0x(hexadecimal: string): string {\n if (hexadecimal.startsWith('0x') || hexadecimal.startsWith('0X')) {\n return hexadecimal.substring(2);\n }\n\n return hexadecimal;\n}\n"]}
package/dist/index.cjs CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.remove0x = exports.add0x = exports.isValidChecksumAddress = exports.getChecksumAddress = exports.isValidHexAddress = exports.assertIsStrictHexString = exports.assertIsHexString = exports.isStrictHexString = exports.isHexString = exports.HexChecksumAddressStruct = exports.HexAddressStruct = exports.StrictHexStruct = exports.HexStruct = void 0;
17
+ exports.remove0x = exports.add0x = exports.isValidChecksumAddress = exports.getChecksumAddress = exports.isValidHexAddress = exports.assertIsStrictHexString = exports.assertIsHexString = exports.isHexChecksumAddress = exports.isHexAddress = exports.isStrictHexString = exports.isHexString = exports.HexChecksumAddressStruct = exports.HexAddressStruct = exports.StrictHexStruct = exports.HexStruct = void 0;
18
18
  __exportStar(require("./assert.cjs"), exports);
19
19
  __exportStar(require("./base64.cjs"), exports);
20
20
  __exportStar(require("./bytes.cjs"), exports);
@@ -31,6 +31,8 @@ Object.defineProperty(exports, "HexAddressStruct", { enumerable: true, get: func
31
31
  Object.defineProperty(exports, "HexChecksumAddressStruct", { enumerable: true, get: function () { return hex_1.HexChecksumAddressStruct; } });
32
32
  Object.defineProperty(exports, "isHexString", { enumerable: true, get: function () { return hex_1.isHexString; } });
33
33
  Object.defineProperty(exports, "isStrictHexString", { enumerable: true, get: function () { return hex_1.isStrictHexString; } });
34
+ Object.defineProperty(exports, "isHexAddress", { enumerable: true, get: function () { return hex_1.isHexAddress; } });
35
+ Object.defineProperty(exports, "isHexChecksumAddress", { enumerable: true, get: function () { return hex_1.isHexChecksumAddress; } });
34
36
  Object.defineProperty(exports, "assertIsHexString", { enumerable: true, get: function () { return hex_1.assertIsHexString; } });
35
37
  Object.defineProperty(exports, "assertIsStrictHexString", { enumerable: true, get: function () { return hex_1.assertIsStrictHexString; } });
36
38
  Object.defineProperty(exports, "isValidHexAddress", { enumerable: true, get: function () { return hex_1.isValidHexAddress; } });
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,+CAAyB;AACzB,+CAAyB;AACzB,8CAAwB;AACxB,mDAA6B;AAC7B,iDAA2B;AAC3B,iDAA2B;AAC3B,oDAA8B;AAC9B,yDAAmC;AACnC,+CAAyB;AAEzB,iCAce;AAbb,gGAAA,SAAS,OAAA;AACT,sGAAA,eAAe,OAAA;AACf,uGAAA,gBAAgB,OAAA;AAChB,+GAAA,wBAAwB,OAAA;AACxB,kGAAA,WAAW,OAAA;AACX,wGAAA,iBAAiB,OAAA;AACjB,wGAAA,iBAAiB,OAAA;AACjB,8GAAA,uBAAuB,OAAA;AACvB,wGAAA,iBAAiB,OAAA;AACjB,yGAAA,kBAAkB,OAAA;AAClB,6GAAA,sBAAsB,OAAA;AACtB,4FAAA,KAAK,OAAA;AACL,+FAAA,QAAQ,OAAA;AAEV,6CAAuB;AACvB,gDAA0B;AAC1B,gDAA0B;AAC1B,6CAAuB;AACvB,+CAAyB;AACzB,+CAAyB;AACzB,gDAA0B;AAC1B,oDAA8B;AAC9B,6CAAuB;AACvB,0DAAoC;AACpC,iDAA2B","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 './errors';\nexport type { Hex } from './hex';\nexport {\n HexStruct,\n StrictHexStruct,\n HexAddressStruct,\n HexChecksumAddressStruct,\n isHexString,\n isStrictHexString,\n assertIsHexString,\n assertIsStrictHexString,\n isValidHexAddress,\n getChecksumAddress,\n isValidChecksumAddress,\n add0x,\n remove0x,\n} from './hex';\nexport * from './json';\nexport * from './keyring';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './opaque';\nexport * from './promise';\nexport * from './superstruct';\nexport * from './time';\nexport * from './transaction-types';\nexport * from './versions';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,+CAAyB;AACzB,+CAAyB;AACzB,8CAAwB;AACxB,mDAA6B;AAC7B,iDAA2B;AAC3B,iDAA2B;AAC3B,oDAA8B;AAC9B,yDAAmC;AACnC,+CAAyB;AAEzB,iCAgBe;AAfb,gGAAA,SAAS,OAAA;AACT,sGAAA,eAAe,OAAA;AACf,uGAAA,gBAAgB,OAAA;AAChB,+GAAA,wBAAwB,OAAA;AACxB,kGAAA,WAAW,OAAA;AACX,wGAAA,iBAAiB,OAAA;AACjB,mGAAA,YAAY,OAAA;AACZ,2GAAA,oBAAoB,OAAA;AACpB,wGAAA,iBAAiB,OAAA;AACjB,8GAAA,uBAAuB,OAAA;AACvB,wGAAA,iBAAiB,OAAA;AACjB,yGAAA,kBAAkB,OAAA;AAClB,6GAAA,sBAAsB,OAAA;AACtB,4FAAA,KAAK,OAAA;AACL,+FAAA,QAAQ,OAAA;AAEV,6CAAuB;AACvB,gDAA0B;AAC1B,gDAA0B;AAC1B,6CAAuB;AACvB,+CAAyB;AACzB,+CAAyB;AACzB,gDAA0B;AAC1B,oDAA8B;AAC9B,6CAAuB;AACvB,0DAAoC;AACpC,iDAA2B","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 './errors';\nexport type { Hex } from './hex';\nexport {\n HexStruct,\n StrictHexStruct,\n HexAddressStruct,\n HexChecksumAddressStruct,\n isHexString,\n isStrictHexString,\n isHexAddress,\n isHexChecksumAddress,\n assertIsHexString,\n assertIsStrictHexString,\n isValidHexAddress,\n getChecksumAddress,\n isValidChecksumAddress,\n add0x,\n remove0x,\n} from './hex';\nexport * from './json';\nexport * from './keyring';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './opaque';\nexport * from './promise';\nexport * from './superstruct';\nexport * from './time';\nexport * from './transaction-types';\nexport * from './versions';\n"]}
package/dist/index.d.cts CHANGED
@@ -8,7 +8,7 @@ export * from "./collections.cjs";
8
8
  export * from "./encryption-types.cjs";
9
9
  export * from "./errors.cjs";
10
10
  export type { Hex } from "./hex.cjs";
11
- export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x, } from "./hex.cjs";
11
+ export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, isHexAddress, isHexChecksumAddress, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x, } from "./hex.cjs";
12
12
  export * from "./json.cjs";
13
13
  export * from "./keyring.cjs";
14
14
  export * from "./logging.cjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AACzB,YAAY,EAAE,GAAG,EAAE,kBAAc;AACjC,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,wBAAwB,EACxB,WAAW,EACX,iBAAiB,EACjB,iBAAiB,EACjB,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,EACL,QAAQ,GACT,kBAAc;AACf,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,kCAA8B;AAC9B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AACzB,YAAY,EAAE,GAAG,EAAE,kBAAc;AACjC,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,wBAAwB,EACxB,WAAW,EACX,iBAAiB,EACjB,YAAY,EACZ,oBAAoB,EACpB,iBAAiB,EACjB,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,EACL,QAAQ,GACT,kBAAc;AACf,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,kCAA8B;AAC9B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B"}
package/dist/index.d.mts CHANGED
@@ -8,7 +8,7 @@ export * from "./collections.mjs";
8
8
  export * from "./encryption-types.mjs";
9
9
  export * from "./errors.mjs";
10
10
  export type { Hex } from "./hex.mjs";
11
- export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x, } from "./hex.mjs";
11
+ export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, isHexAddress, isHexChecksumAddress, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x, } from "./hex.mjs";
12
12
  export * from "./json.mjs";
13
13
  export * from "./keyring.mjs";
14
14
  export * from "./logging.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AACzB,YAAY,EAAE,GAAG,EAAE,kBAAc;AACjC,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,wBAAwB,EACxB,WAAW,EACX,iBAAiB,EACjB,iBAAiB,EACjB,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,EACL,QAAQ,GACT,kBAAc;AACf,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,kCAA8B;AAC9B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AACzB,YAAY,EAAE,GAAG,EAAE,kBAAc;AACjC,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,wBAAwB,EACxB,WAAW,EACX,iBAAiB,EACjB,YAAY,EACZ,oBAAoB,EACpB,iBAAiB,EACjB,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,EACL,QAAQ,GACT,kBAAc;AACf,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,kCAA8B;AAC9B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B"}
package/dist/index.mjs CHANGED
@@ -7,7 +7,7 @@ export * from "./coercers.mjs";
7
7
  export * from "./collections.mjs";
8
8
  export * from "./encryption-types.mjs";
9
9
  export * from "./errors.mjs";
10
- export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x } from "./hex.mjs";
10
+ export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, isHexAddress, isHexChecksumAddress, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x } from "./hex.mjs";
11
11
  export * from "./json.mjs";
12
12
  export * from "./keyring.mjs";
13
13
  export * from "./logging.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AAEzB,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,wBAAwB,EACxB,WAAW,EACX,iBAAiB,EACjB,iBAAiB,EACjB,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,EACL,QAAQ,EACT,kBAAc;AACf,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,kCAA8B;AAC9B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B","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 './errors';\nexport type { Hex } from './hex';\nexport {\n HexStruct,\n StrictHexStruct,\n HexAddressStruct,\n HexChecksumAddressStruct,\n isHexString,\n isStrictHexString,\n assertIsHexString,\n assertIsStrictHexString,\n isValidHexAddress,\n getChecksumAddress,\n isValidChecksumAddress,\n add0x,\n remove0x,\n} from './hex';\nexport * from './json';\nexport * from './keyring';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './opaque';\nexport * from './promise';\nexport * from './superstruct';\nexport * from './time';\nexport * from './transaction-types';\nexport * from './versions';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AAEzB,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,wBAAwB,EACxB,WAAW,EACX,iBAAiB,EACjB,YAAY,EACZ,oBAAoB,EACpB,iBAAiB,EACjB,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,EACL,QAAQ,EACT,kBAAc;AACf,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,kCAA8B;AAC9B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B","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 './errors';\nexport type { Hex } from './hex';\nexport {\n HexStruct,\n StrictHexStruct,\n HexAddressStruct,\n HexChecksumAddressStruct,\n isHexString,\n isStrictHexString,\n isHexAddress,\n isHexChecksumAddress,\n assertIsHexString,\n assertIsStrictHexString,\n isValidHexAddress,\n getChecksumAddress,\n isValidChecksumAddress,\n add0x,\n remove0x,\n} from './hex';\nexport * from './json';\nexport * from './keyring';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './opaque';\nexport * from './promise';\nexport * from './superstruct';\nexport * from './time';\nexport * from './transaction-types';\nexport * from './versions';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/utils",
3
- "version": "11.4.1",
3
+ "version": "11.4.2",
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": {