@metamask/utils 11.4.0 → 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,18 @@ 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
+
16
+ ## [11.4.1]
17
+
18
+ ### Fixed
19
+
20
+ - Improve performance of `getChecksumAddress` function ([#246](https://github.com/MetaMask/utils/pull/246))
21
+
10
22
  ## [11.4.0]
11
23
 
12
24
  ### Changed
@@ -415,7 +427,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
415
427
 
416
428
  - Initial release
417
429
 
418
- [Unreleased]: https://github.com/MetaMask/utils/compare/v11.4.0...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
432
+ [11.4.1]: https://github.com/MetaMask/utils/compare/v11.4.0...v11.4.1
419
433
  [11.4.0]: https://github.com/MetaMask/utils/compare/v11.3.0...v11.4.0
420
434
  [11.3.0]: https://github.com/MetaMask/utils/compare/v11.2.0...v11.3.0
421
435
  [11.2.0]: https://github.com/MetaMask/utils/compare/v11.1.0...v11.2.0
package/dist/hex.cjs CHANGED
@@ -1,14 +1,24 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- 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;
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;
4
7
  const superstruct_1 = require("@metamask/superstruct");
5
8
  const sha3_1 = require("@noble/hashes/sha3");
9
+ const lodash_memoize_1 = __importDefault(require("lodash.memoize"));
6
10
  const assert_1 = require("./assert.cjs");
7
- const bytes_1 = require("./bytes.cjs");
8
- exports.HexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^(?:0x)?[0-9a-f]+$/iu);
9
- exports.StrictHexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-f]+$/iu);
10
- exports.HexAddressStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-f]{40}$/u);
11
- 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';
12
22
  /**
13
23
  * Check if a string is a valid hex string.
14
24
  *
@@ -16,7 +26,7 @@ exports.HexChecksumAddressStruct = (0, superstruct_1.pattern)((0, superstruct_1.
16
26
  * @returns Whether the value is a valid hex string.
17
27
  */
18
28
  function isHexString(value) {
19
- return (0, superstruct_1.is)(value, exports.HexStruct);
29
+ return isString(value) && HEX_REGEX.test(value);
20
30
  }
21
31
  exports.isHexString = isHexString;
22
32
  /**
@@ -27,9 +37,29 @@ exports.isHexString = isHexString;
27
37
  * @returns Whether the value is a valid hex string.
28
38
  */
29
39
  function isStrictHexString(value) {
30
- return (0, superstruct_1.is)(value, exports.StrictHexStruct);
40
+ return isString(value) && STRICT_HEX_REGEX.test(value);
31
41
  }
32
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;
33
63
  /**
34
64
  * Assert that a value is a valid hex string.
35
65
  *
@@ -52,40 +82,40 @@ function assertIsStrictHexString(value) {
52
82
  }
53
83
  exports.assertIsStrictHexString = assertIsStrictHexString;
54
84
  /**
55
- * Validate that the passed prefixed hex string is an all-lowercase
56
- * hex address, or a valid mixed-case checksum address.
85
+ * Encode a passed hex string as an ERC-55 mixed-case checksum address.
86
+ * This is the unmemoized version, primarily used for testing.
57
87
  *
58
- * @param possibleAddress - Input parameter to check against.
59
- * @returns Whether or not the input is a valid hex address.
88
+ * @param hexAddress - The hex address to encode.
89
+ * @returns The address encoded according to ERC-55.
90
+ * @see https://eips.ethereum.org/EIPS/eip-55
60
91
  */
61
- function isValidHexAddress(possibleAddress) {
62
- return ((0, superstruct_1.is)(possibleAddress, exports.HexAddressStruct) ||
63
- isValidChecksumAddress(possibleAddress));
92
+ function getChecksumAddressUnmemoized(hexAddress) {
93
+ (0, assert_1.assert)(isHexChecksumAddress(hexAddress), 'Invalid hex address.');
94
+ const address = remove0x(hexAddress).toLowerCase();
95
+ const hashBytes = (0, sha3_1.keccak_256)(address);
96
+ const { length } = address;
97
+ const result = new Array(length); // Pre-allocate array
98
+ for (let i = 0; i < length; i++) {
99
+ /* eslint-disable no-bitwise */
100
+ const byteIndex = i >> 1; // Faster than Math.floor(i / 2)
101
+ const nibbleIndex = i & 1; // Faster than i % 2
102
+ const byte = hashBytes[byteIndex];
103
+ const nibble = nibbleIndex === 0 ? byte >> 4 : byte & 0x0f;
104
+ /* eslint-enable no-bitwise */
105
+ result[i] = nibble >= 8 ? address[i].toUpperCase() : address[i];
106
+ }
107
+ return `0x${result.join('')}`;
64
108
  }
65
- exports.isValidHexAddress = isValidHexAddress;
109
+ exports.getChecksumAddressUnmemoized = getChecksumAddressUnmemoized;
66
110
  /**
67
111
  * Encode a passed hex string as an ERC-55 mixed-case checksum address.
112
+ * This function is memoized for performance.
68
113
  *
69
- * @param address - The hex address to encode.
114
+ * @param hexAddress - The hex address to encode.
70
115
  * @returns The address encoded according to ERC-55.
71
116
  * @see https://eips.ethereum.org/EIPS/eip-55
72
117
  */
73
- function getChecksumAddress(address) {
74
- (0, assert_1.assert)((0, superstruct_1.is)(address, exports.HexChecksumAddressStruct), 'Invalid hex address.');
75
- const unPrefixed = remove0x(address.toLowerCase());
76
- const unPrefixedHash = remove0x((0, bytes_1.bytesToHex)((0, sha3_1.keccak_256)(unPrefixed)));
77
- return `0x${unPrefixed
78
- .split('')
79
- .map((character, nibbleIndex) => {
80
- const hashCharacter = unPrefixedHash[nibbleIndex];
81
- (0, assert_1.assert)((0, superstruct_1.is)(hashCharacter, (0, superstruct_1.string)()), 'Hash shorter than address.');
82
- return parseInt(hashCharacter, 16) > 7
83
- ? character.toUpperCase()
84
- : character;
85
- })
86
- .join('')}`;
87
- }
88
- exports.getChecksumAddress = getChecksumAddress;
118
+ exports.getChecksumAddress = (0, lodash_memoize_1.default)(getChecksumAddressUnmemoized);
89
119
  /**
90
120
  * Validate that the passed hex string is a valid ERC-55 mixed-case
91
121
  * checksum address.
@@ -93,13 +123,40 @@ exports.getChecksumAddress = getChecksumAddress;
93
123
  * @param possibleChecksum - The hex address to check.
94
124
  * @returns True if the address is a checksum address.
95
125
  */
96
- function isValidChecksumAddress(possibleChecksum) {
97
- if (!(0, superstruct_1.is)(possibleChecksum, exports.HexChecksumAddressStruct)) {
126
+ function isValidChecksumAddressUnmemoized(possibleChecksum) {
127
+ if (!isHexChecksumAddress(possibleChecksum)) {
98
128
  return false;
99
129
  }
100
- return getChecksumAddress(possibleChecksum) === possibleChecksum;
130
+ return (0, exports.getChecksumAddress)(possibleChecksum) === possibleChecksum;
101
131
  }
102
- 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);
103
160
  /**
104
161
  * Add the `0x`-prefix to a hexadecimal string. If the string already has the
105
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;AAE7D,yCAAkC;AAClC,uCAAqC;AAIxB,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,KAAK,EAAE,uBAAe,CAAC,CAAC;AACpC,CAAC;AAF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,CAAC;SACD,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;AAChB,CAAC;AAdD,gDAcC;AAED;;;;;;GAMG;AACH,SAAgB,sBAAsB,CAAC,gBAAqB;IAC1D,IAAI,CAAC,IAAA,gBAAE,EAAC,gBAAgB,EAAE,gCAAwB,CAAC,EAAE;QACnD,OAAO,KAAK,CAAC;KACd;IAED,OAAO,kBAAkB,CAAC,gBAAgB,CAAC,KAAK,gBAAgB,CAAC;AACnE,CAAC;AAND,wDAMC;AAED;;;;;;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 type { Struct } from '@metamask/superstruct';\nimport { is, pattern, string } from '@metamask/superstruct';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\n\nimport { assert } from './assert';\nimport { bytesToHex } from './bytes';\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 *\n * @param address - 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 getChecksumAddress(address: Hex): Hex {\n assert(is(address, HexChecksumAddressStruct), 'Invalid hex address.');\n const unPrefixed = remove0x(address.toLowerCase());\n const unPrefixedHash = remove0x(bytesToHex(keccak256(unPrefixed)));\n return `0x${unPrefixed\n .split('')\n .map((character, nibbleIndex) => {\n const hashCharacter = unPrefixedHash[nibbleIndex];\n assert(is(hashCharacter, string()), 'Hash shorter than address.');\n return parseInt(hashCharacter, 16) > 7\n ? character.toUpperCase()\n : character;\n })\n .join('')}`;\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 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,qB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{ 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,4 +1,5 @@
1
- import type { Struct } from "@metamask/superstruct";
1
+ /// <reference types="lodash" />
2
+ import { type Struct } from "@metamask/superstruct";
2
3
  export type Hex = `0x${string}`;
3
4
  export declare const HexStruct: Struct<string, null>;
4
5
  export declare const StrictHexStruct: Struct<`0x${string}`, null>;
@@ -19,6 +20,20 @@ export declare function isHexString(value: unknown): value is string;
19
20
  * @returns Whether the value is a valid hex string.
20
21
  */
21
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;
22
37
  /**
23
38
  * Assert that a value is a valid hex string.
24
39
  *
@@ -35,21 +50,31 @@ export declare function assertIsHexString(value: unknown): asserts value is stri
35
50
  */
36
51
  export declare function assertIsStrictHexString(value: unknown): asserts value is Hex;
37
52
  /**
38
- * Validate that the passed prefixed hex string is an all-lowercase
39
- * hex address, or a valid mixed-case checksum address.
53
+ * Encode a passed hex string as an ERC-55 mixed-case checksum address.
54
+ * This is the unmemoized version, primarily used for testing.
40
55
  *
41
- * @param possibleAddress - Input parameter to check against.
42
- * @returns Whether or not the input is a valid hex address.
56
+ * @param hexAddress - The hex address to encode.
57
+ * @returns The address encoded according to ERC-55.
58
+ * @see https://eips.ethereum.org/EIPS/eip-55
43
59
  */
44
- export declare function isValidHexAddress(possibleAddress: Hex): boolean;
60
+ export declare function getChecksumAddressUnmemoized(hexAddress: Hex): Hex;
45
61
  /**
46
62
  * Encode a passed hex string as an ERC-55 mixed-case checksum address.
63
+ * This function is memoized for performance.
47
64
  *
48
- * @param address - The hex address to encode.
65
+ * @param hexAddress - The hex address to encode.
49
66
  * @returns The address encoded according to ERC-55.
50
67
  * @see https://eips.ethereum.org/EIPS/eip-55
51
68
  */
52
- export declare function getChecksumAddress(address: Hex): Hex;
69
+ export declare const getChecksumAddress: typeof getChecksumAddressUnmemoized & import("lodash").MemoizedFunction;
70
+ /**
71
+ * Validate that the passed hex string is a valid ERC-55 mixed-case
72
+ * checksum address.
73
+ *
74
+ * @param possibleChecksum - The hex address to check.
75
+ * @returns True if the address is a checksum address.
76
+ */
77
+ export declare function isValidChecksumAddressUnmemoized(possibleChecksum: Hex): boolean;
53
78
  /**
54
79
  * Validate that the passed hex string is a valid ERC-55 mixed-case
55
80
  * checksum address.
@@ -57,7 +82,23 @@ export declare function getChecksumAddress(address: Hex): Hex;
57
82
  * @param possibleChecksum - The hex address to check.
58
83
  * @returns True if the address is a checksum address.
59
84
  */
60
- export declare function isValidChecksumAddress(possibleChecksum: Hex): boolean;
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;
61
102
  /**
62
103
  * Add the `0x`-prefix to a hexadecimal string. If the string already has the
63
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,s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odash" />
2
+ import { type Struct } from "@metamask/superstruct";
2
3
  export type Hex = `0x${string}`;
3
4
  export declare const HexStruct: Struct<string, null>;
4
5
  export declare const StrictHexStruct: Struct<`0x${string}`, null>;
@@ -19,6 +20,20 @@ export declare function isHexString(value: unknown): value is string;
19
20
  * @returns Whether the value is a valid hex string.
20
21
  */
21
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;
22
37
  /**
23
38
  * Assert that a value is a valid hex string.
24
39
  *
@@ -35,21 +50,31 @@ export declare function assertIsHexString(value: unknown): asserts value is stri
35
50
  */
36
51
  export declare function assertIsStrictHexString(value: unknown): asserts value is Hex;
37
52
  /**
38
- * Validate that the passed prefixed hex string is an all-lowercase
39
- * hex address, or a valid mixed-case checksum address.
53
+ * Encode a passed hex string as an ERC-55 mixed-case checksum address.
54
+ * This is the unmemoized version, primarily used for testing.
40
55
  *
41
- * @param possibleAddress - Input parameter to check against.
42
- * @returns Whether or not the input is a valid hex address.
56
+ * @param hexAddress - The hex address to encode.
57
+ * @returns The address encoded according to ERC-55.
58
+ * @see https://eips.ethereum.org/EIPS/eip-55
43
59
  */
44
- export declare function isValidHexAddress(possibleAddress: Hex): boolean;
60
+ export declare function getChecksumAddressUnmemoized(hexAddress: Hex): Hex;
45
61
  /**
46
62
  * Encode a passed hex string as an ERC-55 mixed-case checksum address.
63
+ * This function is memoized for performance.
47
64
  *
48
- * @param address - The hex address to encode.
65
+ * @param hexAddress - The hex address to encode.
49
66
  * @returns The address encoded according to ERC-55.
50
67
  * @see https://eips.ethereum.org/EIPS/eip-55
51
68
  */
52
- export declare function getChecksumAddress(address: Hex): Hex;
69
+ export declare const getChecksumAddress: typeof getChecksumAddressUnmemoized & import("lodash").MemoizedFunction;
70
+ /**
71
+ * Validate that the passed hex string is a valid ERC-55 mixed-case
72
+ * checksum address.
73
+ *
74
+ * @param possibleChecksum - The hex address to check.
75
+ * @returns True if the address is a checksum address.
76
+ */
77
+ export declare function isValidChecksumAddressUnmemoized(possibleChecksum: Hex): boolean;
53
78
  /**
54
79
  * Validate that the passed hex string is a valid ERC-55 mixed-case
55
80
  * checksum address.
@@ -57,7 +82,23 @@ export declare function getChecksumAddress(address: Hex): Hex;
57
82
  * @param possibleChecksum - The hex address to check.
58
83
  * @returns True if the address is a checksum address.
59
84
  */
60
- export declare function isValidChecksumAddress(possibleChecksum: Hex): boolean;
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;
61
102
  /**
62
103
  * Add the `0x`-prefix to a hexadecimal string. If the string already has the
63
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,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amask/superstruct";
2
2
  import { keccak_256 as keccak256 } from "@noble/hashes/sha3";
3
+ import memoize from "lodash.memoize";
3
4
  import { assert } from "./assert.mjs";
4
- import { bytesToHex } from "./bytes.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.
@@ -45,38 +70,39 @@ export function assertIsStrictHexString(value) {
45
70
  assert(isStrictHexString(value), 'Value must be a hexadecimal string, starting with "0x".');
46
71
  }
47
72
  /**
48
- * Validate that the passed prefixed hex string is an all-lowercase
49
- * hex address, or a valid mixed-case checksum address.
73
+ * Encode a passed hex string as an ERC-55 mixed-case checksum address.
74
+ * This is the unmemoized version, primarily used for testing.
50
75
  *
51
- * @param possibleAddress - Input parameter to check against.
52
- * @returns Whether or not the input is a valid hex address.
76
+ * @param hexAddress - The hex address to encode.
77
+ * @returns The address encoded according to ERC-55.
78
+ * @see https://eips.ethereum.org/EIPS/eip-55
53
79
  */
54
- export function isValidHexAddress(possibleAddress) {
55
- return (is(possibleAddress, HexAddressStruct) ||
56
- isValidChecksumAddress(possibleAddress));
80
+ export function getChecksumAddressUnmemoized(hexAddress) {
81
+ assert(isHexChecksumAddress(hexAddress), 'Invalid hex address.');
82
+ const address = remove0x(hexAddress).toLowerCase();
83
+ const hashBytes = keccak256(address);
84
+ const { length } = address;
85
+ const result = new Array(length); // Pre-allocate array
86
+ for (let i = 0; i < length; i++) {
87
+ /* eslint-disable no-bitwise */
88
+ const byteIndex = i >> 1; // Faster than Math.floor(i / 2)
89
+ const nibbleIndex = i & 1; // Faster than i % 2
90
+ const byte = hashBytes[byteIndex];
91
+ const nibble = nibbleIndex === 0 ? byte >> 4 : byte & 0x0f;
92
+ /* eslint-enable no-bitwise */
93
+ result[i] = nibble >= 8 ? address[i].toUpperCase() : address[i];
94
+ }
95
+ return `0x${result.join('')}`;
57
96
  }
58
97
  /**
59
98
  * Encode a passed hex string as an ERC-55 mixed-case checksum address.
99
+ * This function is memoized for performance.
60
100
  *
61
- * @param address - The hex address to encode.
101
+ * @param hexAddress - The hex address to encode.
62
102
  * @returns The address encoded according to ERC-55.
63
103
  * @see https://eips.ethereum.org/EIPS/eip-55
64
104
  */
65
- export function getChecksumAddress(address) {
66
- assert(is(address, HexChecksumAddressStruct), 'Invalid hex address.');
67
- const unPrefixed = remove0x(address.toLowerCase());
68
- const unPrefixedHash = remove0x(bytesToHex(keccak256(unPrefixed)));
69
- return `0x${unPrefixed
70
- .split('')
71
- .map((character, nibbleIndex) => {
72
- const hashCharacter = unPrefixedHash[nibbleIndex];
73
- assert(is(hashCharacter, string()), 'Hash shorter than address.');
74
- return parseInt(hashCharacter, 16) > 7
75
- ? character.toUpperCase()
76
- : character;
77
- })
78
- .join('')}`;
79
- }
105
+ export const getChecksumAddress = memoize(getChecksumAddressUnmemoized);
80
106
  /**
81
107
  * Validate that the passed hex string is a valid ERC-55 mixed-case
82
108
  * checksum address.
@@ -84,12 +110,38 @@ export function getChecksumAddress(address) {
84
110
  * @param possibleChecksum - The hex address to check.
85
111
  * @returns True if the address is a checksum address.
86
112
  */
87
- export function isValidChecksumAddress(possibleChecksum) {
88
- if (!is(possibleChecksum, HexChecksumAddressStruct)) {
113
+ export function isValidChecksumAddressUnmemoized(possibleChecksum) {
114
+ if (!isHexChecksumAddress(possibleChecksum)) {
89
115
  return false;
90
116
  }
91
117
  return getChecksumAddress(possibleChecksum) === possibleChecksum;
92
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);
93
145
  /**
94
146
  * Add the `0x`-prefix to a hexadecimal string. If the string already has the
95
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;AAE7D,OAAO,EAAE,MAAM,EAAE,qBAAiB;AAClC,OAAO,EAAE,UAAU,EAAE,oBAAgB;AAIrC,MAAM,CAAC,MAAM,SAAS,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,sBAAsB,CAAC,CAAC;AACnE,MAAM,CAAC,MAAM,eAAe,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,iBAAiB,CAGjE,CAAC;AACF,MAAM,CAAC,MAAM,gBAAgB,GAAG,OAAO,CACrC,MAAM,EAAE,EACR,mBAAmB,CACC,CAAC;AACvB,MAAM,CAAC,MAAM,wBAAwB,GAAG,OAAO,CAC7C,MAAM,EAAE,EACR,sBAAsB,CACF,CAAC;AAEvB;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,OAAO,EAAE,CAAC,KAAK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SAAS,CAAC,WAAW,EAAE;YACzB,CAAC,CAAC,SAAS,CAAC;IAChB,CAAC,CAAC;SACD,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,sBAAsB,CAAC,gBAAqB;IAC1D,IAAI,CAAC,EAAE,CAAC,gBAAgB,EAAE,wBAAwB,CAAC,EAAE;QACnD,OAAO,KAAK,CAAC;KACd;IAED,OAAO,kBAAkB,CAAC,gBAAgB,CAAC,KAAK,gBAAgB,CAAC;AACnE,CAAC;AAED;;;;;;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 type { Struct } from '@metamask/superstruct';\nimport { is, pattern, string } from '@metamask/superstruct';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\n\nimport { assert } from './assert';\nimport { bytesToHex } from './bytes';\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 *\n * @param address - 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 getChecksumAddress(address: Hex): Hex {\n assert(is(address, HexChecksumAddressStruct), 'Invalid hex address.');\n const unPrefixed = remove0x(address.toLowerCase());\n const unPrefixedHash = remove0x(bytesToHex(keccak256(unPrefixed)));\n return `0x${unPrefixed\n .split('')\n .map((character, nibbleIndex) => {\n const hashCharacter = unPrefixedHash[nibbleIndex];\n assert(is(hashCharacter, string()), 'Hash shorter than address.');\n return parseInt(hashCharacter, 16) > 7\n ? character.toUpperCase()\n : character;\n })\n .join('')}`;\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 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,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port { 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,6 +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.isHexChecksumAddress = exports.isHexAddress = exports.isStrictHexString = exports.isHexString = exports.HexChecksumAddressStruct = exports.HexAddressStruct = exports.StrictHexStruct = exports.HexStruct = void 0;
17
18
  __exportStar(require("./assert.cjs"), exports);
18
19
  __exportStar(require("./base64.cjs"), exports);
19
20
  __exportStar(require("./bytes.cjs"), exports);
@@ -23,7 +24,22 @@ __exportStar(require("./coercers.cjs"), exports);
23
24
  __exportStar(require("./collections.cjs"), exports);
24
25
  __exportStar(require("./encryption-types.cjs"), exports);
25
26
  __exportStar(require("./errors.cjs"), exports);
26
- __exportStar(require("./hex.cjs"), exports);
27
+ var hex_1 = require("./hex.cjs");
28
+ Object.defineProperty(exports, "HexStruct", { enumerable: true, get: function () { return hex_1.HexStruct; } });
29
+ Object.defineProperty(exports, "StrictHexStruct", { enumerable: true, get: function () { return hex_1.StrictHexStruct; } });
30
+ Object.defineProperty(exports, "HexAddressStruct", { enumerable: true, get: function () { return hex_1.HexAddressStruct; } });
31
+ Object.defineProperty(exports, "HexChecksumAddressStruct", { enumerable: true, get: function () { return hex_1.HexChecksumAddressStruct; } });
32
+ Object.defineProperty(exports, "isHexString", { enumerable: true, get: function () { return hex_1.isHexString; } });
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; } });
36
+ Object.defineProperty(exports, "assertIsHexString", { enumerable: true, get: function () { return hex_1.assertIsHexString; } });
37
+ Object.defineProperty(exports, "assertIsStrictHexString", { enumerable: true, get: function () { return hex_1.assertIsStrictHexString; } });
38
+ Object.defineProperty(exports, "isValidHexAddress", { enumerable: true, get: function () { return hex_1.isValidHexAddress; } });
39
+ Object.defineProperty(exports, "getChecksumAddress", { enumerable: true, get: function () { return hex_1.getChecksumAddress; } });
40
+ Object.defineProperty(exports, "isValidChecksumAddress", { enumerable: true, get: function () { return hex_1.isValidChecksumAddress; } });
41
+ Object.defineProperty(exports, "add0x", { enumerable: true, get: function () { return hex_1.add0x; } });
42
+ Object.defineProperty(exports, "remove0x", { enumerable: true, get: function () { return hex_1.remove0x; } });
27
43
  __exportStar(require("./json.cjs"), exports);
28
44
  __exportStar(require("./keyring.cjs"), exports);
29
45
  __exportStar(require("./logging.cjs"), exports);
@@ -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;AACzB,4CAAsB;AACtB,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 * 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
@@ -7,7 +7,8 @@ export * from "./coercers.cjs";
7
7
  export * from "./collections.cjs";
8
8
  export * from "./encryption-types.cjs";
9
9
  export * from "./errors.cjs";
10
- export * from "./hex.cjs";
10
+ export type { Hex } from "./hex.cjs";
11
+ export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, isHexAddress, isHexChecksumAddress, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x, } from "./hex.cjs";
11
12
  export * from "./json.cjs";
12
13
  export * from "./keyring.cjs";
13
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,0BAAsB;AACtB,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
@@ -7,7 +7,8 @@ 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 * from "./hex.mjs";
10
+ export type { Hex } from "./hex.mjs";
11
+ export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, isHexAddress, isHexChecksumAddress, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x, } from "./hex.mjs";
11
12
  export * from "./json.mjs";
12
13
  export * from "./keyring.mjs";
13
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,0BAAsB;AACtB,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 * 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;AACzB,0BAAsB;AACtB,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 * 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.0",
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": {
@@ -67,6 +67,7 @@
67
67
  "@scure/base": "^1.1.3",
68
68
  "@types/debug": "^4.1.7",
69
69
  "debug": "^4.3.4",
70
+ "lodash.memoize": "^4.1.2",
70
71
  "pony-cause": "^2.1.10",
71
72
  "semver": "^7.5.4",
72
73
  "uuid": "^9.0.1"
@@ -83,6 +84,7 @@
83
84
  "@ts-bridge/shims": "^0.1.1",
84
85
  "@types/jest": "^28.1.7",
85
86
  "@types/jest-when": "^3.5.3",
87
+ "@types/lodash.memoize": "^4.1.9",
86
88
  "@types/node": "~18.18.14",
87
89
  "@types/uuid": "^9.0.8",
88
90
  "@typescript-eslint/eslint-plugin": "^5.43.0",