@metamask/utils 3.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -21,7 +21,7 @@ The full API documentation for the latest published version of this library is [
21
21
  - Install [Node.js](https://nodejs.org) version 12
22
22
  - If you are using [nvm](https://github.com/creationix/nvm#installation) (recommended) running `nvm use` will automatically choose the right node version for you.
23
23
  - Install [Yarn v1](https://yarnpkg.com/en/docs/install)
24
- - Run `yarn setup` to install dependencies and run any requried post-install scripts
24
+ - Run `yarn setup` to install dependencies and run any required post-install scripts
25
25
  - **Warning:** Do not use the `yarn` / `yarn install` command directly. Use `yarn setup` instead. The normal install command will skip required post-install scripts, leaving your development environment in an invalid state.
26
26
 
27
27
  ### Testing and Linting
@@ -0,0 +1,36 @@
1
+ export declare class AssertionError extends Error {
2
+ readonly code = "ERR_ASSERTION";
3
+ constructor(options: {
4
+ message: string;
5
+ });
6
+ }
7
+ /**
8
+ * Same as Node.js assert.
9
+ * If the value is falsy, throws an error, does nothing otherwise.
10
+ *
11
+ * @throws {@link AssertionError}. If value is falsy.
12
+ * @param value - The test that should be truthy to pass.
13
+ * @param message - Message to be passed to {@link AssertionError} or an
14
+ * {@link Error} instance to throw.
15
+ */
16
+ export declare function assert(value: any, message?: string | Error): asserts value;
17
+ /**
18
+ * Use in the default case of a switch that you want to be fully exhaustive.
19
+ * Using this function forces the compiler to enforce exhaustivity during
20
+ * compile-time.
21
+ *
22
+ * @example
23
+ * ```
24
+ * const number = 1;
25
+ * switch (number) {
26
+ * case 0:
27
+ * ...
28
+ * case 1:
29
+ * ...
30
+ * default:
31
+ * assertExhaustive(snapPrefix);
32
+ * }
33
+ * ```
34
+ * @param _object - The object on which the switch is being operated.
35
+ */
36
+ export declare function assertExhaustive(_object: never): never;
package/dist/assert.js ADDED
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertExhaustive = exports.assert = exports.AssertionError = void 0;
4
+ class AssertionError extends Error {
5
+ constructor(options) {
6
+ super(options.message);
7
+ this.code = 'ERR_ASSERTION';
8
+ }
9
+ }
10
+ exports.AssertionError = AssertionError;
11
+ /**
12
+ * Same as Node.js assert.
13
+ * If the value is falsy, throws an error, does nothing otherwise.
14
+ *
15
+ * @throws {@link AssertionError}. If value is falsy.
16
+ * @param value - The test that should be truthy to pass.
17
+ * @param message - Message to be passed to {@link AssertionError} or an
18
+ * {@link Error} instance to throw.
19
+ */
20
+ function assert(value, message) {
21
+ if (!value) {
22
+ if (message instanceof Error) {
23
+ throw message;
24
+ }
25
+ throw new AssertionError({ message: message !== null && message !== void 0 ? message : 'Assertion failed.' });
26
+ }
27
+ }
28
+ exports.assert = assert;
29
+ /**
30
+ * Use in the default case of a switch that you want to be fully exhaustive.
31
+ * Using this function forces the compiler to enforce exhaustivity during
32
+ * compile-time.
33
+ *
34
+ * @example
35
+ * ```
36
+ * const number = 1;
37
+ * switch (number) {
38
+ * case 0:
39
+ * ...
40
+ * case 1:
41
+ * ...
42
+ * default:
43
+ * assertExhaustive(snapPrefix);
44
+ * }
45
+ * ```
46
+ * @param _object - The object on which the switch is being operated.
47
+ */
48
+ function assertExhaustive(_object) {
49
+ throw new Error('Invalid branch reached. Should be detected during compilation.');
50
+ }
51
+ exports.assertExhaustive = assertExhaustive;
52
+ //# sourceMappingURL=assert.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert.js","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":";;;AAAA,MAAa,cAAe,SAAQ,KAAK;IAGvC,YAAY,OAA4B;QACtC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAHhB,SAAI,GAAG,eAAe,CAAC;IAIhC,CAAC;CACF;AAND,wCAMC;AAED;;;;;;;;GAQG;AACH,SAAgB,MAAM,CAAC,KAAU,EAAE,OAAwB;IACzD,IAAI,CAAC,KAAK,EAAE;QACV,IAAI,OAAO,YAAY,KAAK,EAAE;YAC5B,MAAM,OAAO,CAAC;SACf;QACD,MAAM,IAAI,cAAc,CAAC,EAAE,OAAO,EAAE,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,mBAAmB,EAAE,CAAC,CAAC;KACvE;AACH,CAAC;AAPD,wBAOC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,gBAAgB,CAAC,OAAc;IAC7C,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;AACJ,CAAC;AAJD,4CAIC","sourcesContent":["export class AssertionError extends Error {\n readonly code = 'ERR_ASSERTION';\n\n constructor(options: { message: string }) {\n super(options.message);\n }\n}\n\n/**\n * Same as Node.js assert.\n * If the value is falsy, throws an error, does nothing otherwise.\n *\n * @throws {@link AssertionError}. If value is falsy.\n * @param value - The test that should be truthy to pass.\n * @param message - Message to be passed to {@link AssertionError} or an\n * {@link Error} instance to throw.\n */\nexport function assert(value: any, message?: string | Error): asserts value {\n if (!value) {\n if (message instanceof Error) {\n throw message;\n }\n throw new AssertionError({ message: message ?? 'Assertion failed.' });\n }\n}\n\n/**\n * Use in the default case of a switch that you want to be fully exhaustive.\n * Using this function forces the compiler to enforce exhaustivity during\n * compile-time.\n *\n * @example\n * ```\n * const number = 1;\n * switch (number) {\n * case 0:\n * ...\n * case 1:\n * ...\n * default:\n * assertExhaustive(snapPrefix);\n * }\n * ```\n * @param _object - The object on which the switch is being operated.\n */\nexport function assertExhaustive(_object: never): never {\n throw new Error(\n 'Invalid branch reached. Should be detected during compilation.',\n );\n}\n"]}
@@ -0,0 +1,138 @@
1
+ export declare type Bytes = bigint | number | string | Uint8Array;
2
+ /**
3
+ * Check if a value is a `Uint8Array`.
4
+ *
5
+ * @param value - The value to check.
6
+ * @returns Whether the value is a `Uint8Array`.
7
+ */
8
+ export declare function isBytes(value: unknown): value is Uint8Array;
9
+ /**
10
+ * Assert that a value is a `Uint8Array`.
11
+ *
12
+ * @param value - The value to check.
13
+ * @throws If the value is not a `Uint8Array`.
14
+ */
15
+ export declare function assertIsBytes(value: unknown): asserts value is Uint8Array;
16
+ /**
17
+ * Convert a `Uint8Array` to a hexadecimal string.
18
+ *
19
+ * @param bytes - The bytes to convert to a hexadecimal string.
20
+ * @returns The hexadecimal string.
21
+ */
22
+ export declare function bytesToHex(bytes: Uint8Array): string;
23
+ /**
24
+ * Convert a `Uint8Array` to a `bigint`.
25
+ *
26
+ * To convert a `Uint8Array` to a `number` instead, use {@link bytesToNumber}.
27
+ * To convert a two's complement encoded `Uint8Array` to a `bigint`, use
28
+ * {@link bytesToSignedBigInt}.
29
+ *
30
+ * @param bytes - The bytes to convert to a `bigint`.
31
+ * @returns The `bigint`.
32
+ */
33
+ export declare function bytesToBigInt(bytes: Uint8Array): bigint;
34
+ /**
35
+ * Convert a `Uint8Array` to a signed `bigint`. This assumes that the bytes are
36
+ * encoded in two's complement.
37
+ *
38
+ * To convert a `Uint8Array` to an unsigned `bigint` instead, use
39
+ * {@link bytesToBigInt}.
40
+ *
41
+ * @see https://en.wikipedia.org/wiki/Two%27s_complement
42
+ * @param bytes - The bytes to convert to a signed `bigint`.
43
+ * @returns The signed `bigint`.
44
+ */
45
+ export declare function bytesToSignedBigInt(bytes: Uint8Array): bigint;
46
+ /**
47
+ * Convert a `Uint8Array` to a `number`.
48
+ *
49
+ * To convert a `Uint8Array` to a `bigint` instead, use {@link bytesToBigInt}.
50
+ *
51
+ * @param bytes - The bytes to convert to a number.
52
+ * @returns The number.
53
+ * @throws If the resulting number is not a safe integer.
54
+ */
55
+ export declare function bytesToNumber(bytes: Uint8Array): number;
56
+ /**
57
+ * Convert a UTF-8 encoded `Uint8Array` to a `string`.
58
+ *
59
+ * @param bytes - The bytes to convert to a string.
60
+ * @returns The string.
61
+ */
62
+ export declare function bytesToString(bytes: Uint8Array): string;
63
+ /**
64
+ * Convert a hexadecimal string to a `Uint8Array`. The string can optionally be
65
+ * prefixed with `0x`. It accepts even and odd length strings.
66
+ *
67
+ * @param value - The hexadecimal string to convert to bytes.
68
+ * @returns The bytes as `Uint8Array`.
69
+ */
70
+ export declare function hexToBytes(value: string): Uint8Array;
71
+ /**
72
+ * Convert a `bigint` to a `Uint8Array`.
73
+ *
74
+ * This assumes that the `bigint` is an unsigned integer. To convert a signed
75
+ * `bigint` instead, use {@link signedBigIntToBytes}.
76
+ *
77
+ * @param value - The bigint to convert to bytes.
78
+ * @returns The bytes as `Uint8Array`.
79
+ */
80
+ export declare function bigIntToBytes(value: bigint): Uint8Array;
81
+ /**
82
+ * Convert a signed `bigint` to a `Uint8Array`. This uses two's complement
83
+ * encoding to represent negative numbers.
84
+ *
85
+ * To convert an unsigned `bigint` to a `Uint8Array` instead, use
86
+ * {@link bigIntToBytes}.
87
+ *
88
+ * @see https://en.wikipedia.org/wiki/Two%27s_complement
89
+ * @param value - The number to convert to bytes.
90
+ * @param byteLength - The length of the resulting `Uint8Array`. If the number
91
+ * is larger than the maximum value that can be represented by the given length,
92
+ * an error is thrown.
93
+ * @returns The bytes as `Uint8Array`.
94
+ */
95
+ export declare function signedBigIntToBytes(value: bigint, byteLength: number): Uint8Array;
96
+ /**
97
+ * Convert a `number` to a `Uint8Array`.
98
+ *
99
+ * @param value - The number to convert to bytes.
100
+ * @returns The bytes as `Uint8Array`.
101
+ * @throws If the number is not a safe integer.
102
+ */
103
+ export declare function numberToBytes(value: number): Uint8Array;
104
+ /**
105
+ * Convert a `string` to a UTF-8 encoded `Uint8Array`.
106
+ *
107
+ * @param value - The string to convert to bytes.
108
+ * @returns The bytes as `Uint8Array`.
109
+ */
110
+ export declare function stringToBytes(value: string): Uint8Array;
111
+ /**
112
+ * Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,
113
+ * a `bigint`, a `number`, or a `string`.
114
+ *
115
+ * If the value is a `string`, and it is prefixed with `0x`, it will be
116
+ * interpreted as a hexadecimal string. Otherwise, it will be interpreted as a
117
+ * UTF-8 string. To convert a hexadecimal string to bytes without interpreting
118
+ * it as a UTF-8 string, use {@link hexToBytes} instead.
119
+ *
120
+ * If the value is a `bigint`, it is assumed to be unsigned. To convert a signed
121
+ * `bigint` to bytes, use {@link signedBigIntToBytes} instead.
122
+ *
123
+ * If the value is a `Uint8Array`, it will be returned as-is.
124
+ *
125
+ * @param value - The value to convert to bytes.
126
+ * @returns The bytes as `Uint8Array`.
127
+ */
128
+ export declare function valueToBytes(value: Bytes): Uint8Array;
129
+ /**
130
+ * Concatenate multiple byte-like values into a single `Uint8Array`. The values
131
+ * can be `Uint8Array`, `bigint`, `number`, or `string`. This uses
132
+ * {@link valueToBytes} under the hood to convert each value to bytes. Refer to
133
+ * the documentation of that function for more information.
134
+ *
135
+ * @param values - The values to concatenate.
136
+ * @returns The concatenated bytes as `Uint8Array`.
137
+ */
138
+ export declare function concatBytes(values: Bytes[]): Uint8Array;
package/dist/bytes.js ADDED
@@ -0,0 +1,330 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.concatBytes = exports.valueToBytes = exports.stringToBytes = exports.numberToBytes = exports.signedBigIntToBytes = exports.bigIntToBytes = exports.hexToBytes = exports.bytesToString = exports.bytesToNumber = exports.bytesToSignedBigInt = exports.bytesToBigInt = exports.bytesToHex = exports.assertIsBytes = exports.isBytes = void 0;
4
+ const assert_1 = require("./assert");
5
+ const hex_1 = require("./hex");
6
+ // '0'.charCodeAt(0) === 48
7
+ const HEX_MINIMUM_NUMBER_CHARACTER = 48;
8
+ // '9'.charCodeAt(0) === 57
9
+ const HEX_MAXIMUM_NUMBER_CHARACTER = 58;
10
+ const HEX_CHARACTER_OFFSET = 87;
11
+ /**
12
+ * Memoized function that returns an array to be used as a lookup table for
13
+ * converting bytes to hexadecimal values.
14
+ *
15
+ * The array is created lazily and then cached for future use. The benefit of
16
+ * this approach is that the performance of converting bytes to hex is much
17
+ * better than if we were to call `toString(16)` on each byte.
18
+ *
19
+ * The downside is that the array is created once and then never garbage
20
+ * collected. This is not a problem in practice because the array is only 256
21
+ * elements long.
22
+ *
23
+ * @returns A function that returns the lookup table.
24
+ */
25
+ function getPrecomputedHexValuesBuilder() {
26
+ // To avoid issues with tree shaking, we need to use a function to return the
27
+ // array. This is because the array is only used in the `bytesToHex` function
28
+ // and if we were to use a global variable, the array might be removed by the
29
+ // tree shaker.
30
+ const lookupTable = [];
31
+ return () => {
32
+ if (lookupTable.length === 0) {
33
+ for (let i = 0; i < 256; i++) {
34
+ lookupTable.push(i.toString(16).padStart(2, '0'));
35
+ }
36
+ }
37
+ return lookupTable;
38
+ };
39
+ }
40
+ /**
41
+ * Function implementation of the {@link getPrecomputedHexValuesBuilder}
42
+ * function.
43
+ */
44
+ const getPrecomputedHexValues = getPrecomputedHexValuesBuilder();
45
+ /**
46
+ * Check if a value is a `Uint8Array`.
47
+ *
48
+ * @param value - The value to check.
49
+ * @returns Whether the value is a `Uint8Array`.
50
+ */
51
+ function isBytes(value) {
52
+ return value instanceof Uint8Array;
53
+ }
54
+ exports.isBytes = isBytes;
55
+ /**
56
+ * Assert that a value is a `Uint8Array`.
57
+ *
58
+ * @param value - The value to check.
59
+ * @throws If the value is not a `Uint8Array`.
60
+ */
61
+ function assertIsBytes(value) {
62
+ (0, assert_1.assert)(isBytes(value), 'Value must be a Uint8Array.');
63
+ }
64
+ exports.assertIsBytes = assertIsBytes;
65
+ /**
66
+ * Convert a `Uint8Array` to a hexadecimal string.
67
+ *
68
+ * @param bytes - The bytes to convert to a hexadecimal string.
69
+ * @returns The hexadecimal string.
70
+ */
71
+ function bytesToHex(bytes) {
72
+ assertIsBytes(bytes);
73
+ const lookupTable = getPrecomputedHexValues();
74
+ const hex = new Array(bytes.length);
75
+ for (let i = 0; i < bytes.length; i++) {
76
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
77
+ hex[i] = lookupTable[bytes[i]];
78
+ }
79
+ return (0, hex_1.add0x)(hex.join(''));
80
+ }
81
+ exports.bytesToHex = bytesToHex;
82
+ /**
83
+ * Convert a `Uint8Array` to a `bigint`.
84
+ *
85
+ * To convert a `Uint8Array` to a `number` instead, use {@link bytesToNumber}.
86
+ * To convert a two's complement encoded `Uint8Array` to a `bigint`, use
87
+ * {@link bytesToSignedBigInt}.
88
+ *
89
+ * @param bytes - The bytes to convert to a `bigint`.
90
+ * @returns The `bigint`.
91
+ */
92
+ function bytesToBigInt(bytes) {
93
+ assertIsBytes(bytes);
94
+ const hex = bytesToHex(bytes);
95
+ return BigInt(hex);
96
+ }
97
+ exports.bytesToBigInt = bytesToBigInt;
98
+ /**
99
+ * Convert a `Uint8Array` to a signed `bigint`. This assumes that the bytes are
100
+ * encoded in two's complement.
101
+ *
102
+ * To convert a `Uint8Array` to an unsigned `bigint` instead, use
103
+ * {@link bytesToBigInt}.
104
+ *
105
+ * @see https://en.wikipedia.org/wiki/Two%27s_complement
106
+ * @param bytes - The bytes to convert to a signed `bigint`.
107
+ * @returns The signed `bigint`.
108
+ */
109
+ function bytesToSignedBigInt(bytes) {
110
+ assertIsBytes(bytes);
111
+ let value = BigInt(0);
112
+ for (const byte of bytes) {
113
+ // eslint-disable-next-line no-bitwise
114
+ value = (value << BigInt(8)) + BigInt(byte);
115
+ }
116
+ return BigInt.asIntN(bytes.length * 8, value);
117
+ }
118
+ exports.bytesToSignedBigInt = bytesToSignedBigInt;
119
+ /**
120
+ * Convert a `Uint8Array` to a `number`.
121
+ *
122
+ * To convert a `Uint8Array` to a `bigint` instead, use {@link bytesToBigInt}.
123
+ *
124
+ * @param bytes - The bytes to convert to a number.
125
+ * @returns The number.
126
+ * @throws If the resulting number is not a safe integer.
127
+ */
128
+ function bytesToNumber(bytes) {
129
+ assertIsBytes(bytes);
130
+ const bigint = bytesToBigInt(bytes);
131
+ (0, assert_1.assert)(bigint <= BigInt(Number.MAX_SAFE_INTEGER), 'Number is not a safe integer. Use `bytesToBigInt` instead.');
132
+ return Number(bigint);
133
+ }
134
+ exports.bytesToNumber = bytesToNumber;
135
+ /**
136
+ * Convert a UTF-8 encoded `Uint8Array` to a `string`.
137
+ *
138
+ * @param bytes - The bytes to convert to a string.
139
+ * @returns The string.
140
+ */
141
+ function bytesToString(bytes) {
142
+ assertIsBytes(bytes);
143
+ return new TextDecoder(undefined).decode(bytes);
144
+ }
145
+ exports.bytesToString = bytesToString;
146
+ /**
147
+ * Convert a hexadecimal string to a `Uint8Array`. The string can optionally be
148
+ * prefixed with `0x`. It accepts even and odd length strings.
149
+ *
150
+ * @param value - The hexadecimal string to convert to bytes.
151
+ * @returns The bytes as `Uint8Array`.
152
+ */
153
+ function hexToBytes(value) {
154
+ (0, hex_1.assertIsHexString)(value);
155
+ // Remove the `0x` prefix if it exists, and pad the string to have an even
156
+ // number of characters.
157
+ const strippedValue = (0, hex_1.remove0x)(value).toLowerCase();
158
+ const normalizedValue = strippedValue.length % 2 === 0 ? strippedValue : `0${strippedValue}`;
159
+ const bytes = new Uint8Array(normalizedValue.length / 2);
160
+ for (let i = 0; i < bytes.length; i++) {
161
+ // While this is not the prettiest way to convert a hexadecimal string to a
162
+ // `Uint8Array`, it is a lot faster than using `parseInt` to convert each
163
+ // character.
164
+ const c1 = normalizedValue.charCodeAt(i * 2);
165
+ const c2 = normalizedValue.charCodeAt(i * 2 + 1);
166
+ const n1 = c1 -
167
+ (c1 < HEX_MAXIMUM_NUMBER_CHARACTER
168
+ ? HEX_MINIMUM_NUMBER_CHARACTER
169
+ : HEX_CHARACTER_OFFSET);
170
+ const n2 = c2 -
171
+ (c2 < HEX_MAXIMUM_NUMBER_CHARACTER
172
+ ? HEX_MINIMUM_NUMBER_CHARACTER
173
+ : HEX_CHARACTER_OFFSET);
174
+ bytes[i] = n1 * 16 + n2;
175
+ }
176
+ return bytes;
177
+ }
178
+ exports.hexToBytes = hexToBytes;
179
+ /**
180
+ * Convert a `bigint` to a `Uint8Array`.
181
+ *
182
+ * This assumes that the `bigint` is an unsigned integer. To convert a signed
183
+ * `bigint` instead, use {@link signedBigIntToBytes}.
184
+ *
185
+ * @param value - The bigint to convert to bytes.
186
+ * @returns The bytes as `Uint8Array`.
187
+ */
188
+ function bigIntToBytes(value) {
189
+ (0, assert_1.assert)(typeof value === 'bigint', 'Value must be a bigint.');
190
+ (0, assert_1.assert)(value >= BigInt(0), 'Value must be a non-negative bigint.');
191
+ const hex = value.toString(16);
192
+ return hexToBytes(hex);
193
+ }
194
+ exports.bigIntToBytes = bigIntToBytes;
195
+ /**
196
+ * Check if a `bigint` fits in a certain number of bytes.
197
+ *
198
+ * @param value - The `bigint` to check.
199
+ * @param bytes - The number of bytes.
200
+ * @returns Whether the `bigint` fits in the number of bytes.
201
+ */
202
+ function bigIntFits(value, bytes) {
203
+ (0, assert_1.assert)(bytes > 0);
204
+ /* eslint-disable no-bitwise */
205
+ const mask = value >> BigInt(31);
206
+ return !(((~value & mask) + (value & ~mask)) >> BigInt(bytes * 8 + ~0));
207
+ /* eslint-enable no-bitwise */
208
+ }
209
+ /**
210
+ * Convert a signed `bigint` to a `Uint8Array`. This uses two's complement
211
+ * encoding to represent negative numbers.
212
+ *
213
+ * To convert an unsigned `bigint` to a `Uint8Array` instead, use
214
+ * {@link bigIntToBytes}.
215
+ *
216
+ * @see https://en.wikipedia.org/wiki/Two%27s_complement
217
+ * @param value - The number to convert to bytes.
218
+ * @param byteLength - The length of the resulting `Uint8Array`. If the number
219
+ * is larger than the maximum value that can be represented by the given length,
220
+ * an error is thrown.
221
+ * @returns The bytes as `Uint8Array`.
222
+ */
223
+ function signedBigIntToBytes(value, byteLength) {
224
+ (0, assert_1.assert)(typeof value === 'bigint', 'Value must be a bigint.');
225
+ (0, assert_1.assert)(typeof byteLength === 'number', 'Byte length must be a number.');
226
+ (0, assert_1.assert)(byteLength > 0, 'Byte length must be greater than 0.');
227
+ (0, assert_1.assert)(bigIntFits(value, byteLength), 'Byte length is too small to represent the given value.');
228
+ // ESLint doesn't like mutating function parameters, so to avoid having to
229
+ // disable the rule, we create a new variable.
230
+ let numberValue = value;
231
+ const bytes = new Uint8Array(byteLength);
232
+ for (let i = 0; i < bytes.length; i++) {
233
+ bytes[i] = Number(BigInt.asUintN(8, numberValue));
234
+ // eslint-disable-next-line no-bitwise
235
+ numberValue >>= BigInt(8);
236
+ }
237
+ return bytes.reverse();
238
+ }
239
+ exports.signedBigIntToBytes = signedBigIntToBytes;
240
+ /**
241
+ * Convert a `number` to a `Uint8Array`.
242
+ *
243
+ * @param value - The number to convert to bytes.
244
+ * @returns The bytes as `Uint8Array`.
245
+ * @throws If the number is not a safe integer.
246
+ */
247
+ function numberToBytes(value) {
248
+ (0, assert_1.assert)(typeof value === 'number', 'Value must be a number.');
249
+ (0, assert_1.assert)(value >= 0, 'Value must be a non-negative number.');
250
+ (0, assert_1.assert)(Number.isSafeInteger(value), 'Value is not a safe integer. Use `bigIntToBytes` instead.');
251
+ const hex = value.toString(16);
252
+ return hexToBytes(hex);
253
+ }
254
+ exports.numberToBytes = numberToBytes;
255
+ /**
256
+ * Convert a `string` to a UTF-8 encoded `Uint8Array`.
257
+ *
258
+ * @param value - The string to convert to bytes.
259
+ * @returns The bytes as `Uint8Array`.
260
+ */
261
+ function stringToBytes(value) {
262
+ (0, assert_1.assert)(typeof value === 'string', 'Value must be a string.');
263
+ return new TextEncoder().encode(value);
264
+ }
265
+ exports.stringToBytes = stringToBytes;
266
+ /**
267
+ * Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,
268
+ * a `bigint`, a `number`, or a `string`.
269
+ *
270
+ * If the value is a `string`, and it is prefixed with `0x`, it will be
271
+ * interpreted as a hexadecimal string. Otherwise, it will be interpreted as a
272
+ * UTF-8 string. To convert a hexadecimal string to bytes without interpreting
273
+ * it as a UTF-8 string, use {@link hexToBytes} instead.
274
+ *
275
+ * If the value is a `bigint`, it is assumed to be unsigned. To convert a signed
276
+ * `bigint` to bytes, use {@link signedBigIntToBytes} instead.
277
+ *
278
+ * If the value is a `Uint8Array`, it will be returned as-is.
279
+ *
280
+ * @param value - The value to convert to bytes.
281
+ * @returns The bytes as `Uint8Array`.
282
+ */
283
+ function valueToBytes(value) {
284
+ if (typeof value === 'bigint') {
285
+ return bigIntToBytes(value);
286
+ }
287
+ if (typeof value === 'number') {
288
+ return numberToBytes(value);
289
+ }
290
+ if (typeof value === 'string') {
291
+ if (value.startsWith('0x')) {
292
+ return hexToBytes(value);
293
+ }
294
+ return stringToBytes(value);
295
+ }
296
+ if (isBytes(value)) {
297
+ return value;
298
+ }
299
+ throw new TypeError(`Unsupported value type: "${typeof value}".`);
300
+ }
301
+ exports.valueToBytes = valueToBytes;
302
+ /**
303
+ * Concatenate multiple byte-like values into a single `Uint8Array`. The values
304
+ * can be `Uint8Array`, `bigint`, `number`, or `string`. This uses
305
+ * {@link valueToBytes} under the hood to convert each value to bytes. Refer to
306
+ * the documentation of that function for more information.
307
+ *
308
+ * @param values - The values to concatenate.
309
+ * @returns The concatenated bytes as `Uint8Array`.
310
+ */
311
+ function concatBytes(values) {
312
+ const normalizedValues = new Array(values.length);
313
+ let byteLength = 0;
314
+ for (let i = 0; i < values.length; i++) {
315
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
316
+ const value = valueToBytes(values[i]);
317
+ normalizedValues[i] = value;
318
+ byteLength += value.length;
319
+ }
320
+ const bytes = new Uint8Array(byteLength);
321
+ for (let i = 0, offset = 0; i < normalizedValues.length; i++) {
322
+ // While we could simply spread the values into an array and use
323
+ // `Uint8Array.from`, that is a lot slower than using `Uint8Array.set`.
324
+ bytes.set(normalizedValues[i], offset);
325
+ offset += normalizedValues[i].length;
326
+ }
327
+ return bytes;
328
+ }
329
+ exports.concatBytes = concatBytes;
330
+ //# sourceMappingURL=bytes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bytes.js","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":";;;AAAA,qCAAkC;AAClC,+BAA2D;AAE3D,2BAA2B;AAC3B,MAAM,4BAA4B,GAAG,EAAE,CAAC;AAExC,2BAA2B;AAC3B,MAAM,4BAA4B,GAAG,EAAE,CAAC;AACxC,MAAM,oBAAoB,GAAG,EAAE,C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AO,UAAU,CAAC,KAAK,CAAC,CAAC;SAC1B;QAED,OAAO,aAAa,CAAC,KAAK,CAAC,CAAC;KAC7B;IAED,IAAI,OAAO,CAAC,KAAK,CAAC,EAAE;QAClB,OAAO,KAAK,CAAC;KACd;IAED,MAAM,IAAI,SAAS,CAAC,4BAA4B,OAAO,KAAK,IAAI,CAAC,CAAC;AACpE,CAAC;AAtBD,oCAsBC;AAED;;;;;;;;GAQG;AACH,SAAgB,WAAW,CAAC,MAAe;IACzC,MAAM,gBAAgB,GAAG,IAAI,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAClD,IAAI,UAAU,GAAG,CAAC,CAAC;IAEnB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACtC,oEAAoE;QACpE,MAAM,KAAK,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC,CAAE,CAAC,CAAC;QAEvC,gBAAgB,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;QAC5B,UAAU,IAAI,KAAK,CAAC,MAAM,CAAC;KAC5B;IAED,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,UAAU,CAAC,CAAC;IACzC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,CAAC,EAAE,CAAC,GAAG,gBAAgB,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAC5D,gEAAgE;QAChE,uEAAuE;QACvE,KAAK,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;QACvC,MAAM,IAAI,gBAAgB,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;KACtC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AArBD,kCAqBC","sourcesContent":["import { assert } from './assert';\nimport { add0x, assertIsHexString, remove0x } from './hex';\n\n// '0'.charCodeAt(0) === 48\nconst HEX_MINIMUM_NUMBER_CHARACTER = 48;\n\n// '9'.charCodeAt(0) === 57\nconst HEX_MAXIMUM_NUMBER_CHARACTER = 58;\nconst HEX_CHARACTER_OFFSET = 87;\n\nexport type Bytes = bigint | number | string | Uint8Array;\n\n/**\n * Memoized function that returns an array to be used as a lookup table for\n * converting bytes to hexadecimal values.\n *\n * The array is created lazily and then cached for future use. The benefit of\n * this approach is that the performance of converting bytes to hex is much\n * better than if we were to call `toString(16)` on each byte.\n *\n * The downside is that the array is created once and then never garbage\n * collected. This is not a problem in practice because the array is only 256\n * elements long.\n *\n * @returns A function that returns the lookup table.\n */\nfunction getPrecomputedHexValuesBuilder(): () => string[] {\n // To avoid issues with tree shaking, we need to use a function to return the\n // array. This is because the array is only used in the `bytesToHex` function\n // and if we were to use a global variable, the array might be removed by the\n // tree shaker.\n const lookupTable: string[] = [];\n\n return () => {\n if (lookupTable.length === 0) {\n for (let i = 0; i < 256; i++) {\n lookupTable.push(i.toString(16).padStart(2, '0'));\n }\n }\n\n return lookupTable;\n };\n}\n\n/**\n * Function implementation of the {@link getPrecomputedHexValuesBuilder}\n * function.\n */\nconst getPrecomputedHexValues = getPrecomputedHexValuesBuilder();\n\n/**\n * Check if a value is a `Uint8Array`.\n *\n * @param value - The value to check.\n * @returns Whether the value is a `Uint8Array`.\n */\nexport function isBytes(value: unknown): value is Uint8Array {\n return value instanceof Uint8Array;\n}\n\n/**\n * Assert that a value is a `Uint8Array`.\n *\n * @param value - The value to check.\n * @throws If the value is not a `Uint8Array`.\n */\nexport function assertIsBytes(value: unknown): asserts value is Uint8Array {\n assert(isBytes(value), 'Value must be a Uint8Array.');\n}\n\n/**\n * Convert a `Uint8Array` to a hexadecimal string.\n *\n * @param bytes - The bytes to convert to a hexadecimal string.\n * @returns The hexadecimal string.\n */\nexport function bytesToHex(bytes: Uint8Array): string {\n assertIsBytes(bytes);\n\n const lookupTable = getPrecomputedHexValues();\n const hex = new Array(bytes.length);\n\n for (let i = 0; i < bytes.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n hex[i] = lookupTable[bytes[i]!];\n }\n\n return add0x(hex.join(''));\n}\n\n/**\n * Convert a `Uint8Array` to a `bigint`.\n *\n * To convert a `Uint8Array` to a `number` instead, use {@link bytesToNumber}.\n * To convert a two's complement encoded `Uint8Array` to a `bigint`, use\n * {@link bytesToSignedBigInt}.\n *\n * @param bytes - The bytes to convert to a `bigint`.\n * @returns The `bigint`.\n */\nexport function bytesToBigInt(bytes: Uint8Array): bigint {\n assertIsBytes(bytes);\n\n const hex = bytesToHex(bytes);\n return BigInt(hex);\n}\n\n/**\n * Convert a `Uint8Array` to a signed `bigint`. This assumes that the bytes are\n * encoded in two's complement.\n *\n * To convert a `Uint8Array` to an unsigned `bigint` instead, use\n * {@link bytesToBigInt}.\n *\n * @see https://en.wikipedia.org/wiki/Two%27s_complement\n * @param bytes - The bytes to convert to a signed `bigint`.\n * @returns The signed `bigint`.\n */\nexport function bytesToSignedBigInt(bytes: Uint8Array): bigint {\n assertIsBytes(bytes);\n\n let value = BigInt(0);\n for (const byte of bytes) {\n // eslint-disable-next-line no-bitwise\n value = (value << BigInt(8)) + BigInt(byte);\n }\n\n return BigInt.asIntN(bytes.length * 8, value);\n}\n\n/**\n * Convert a `Uint8Array` to a `number`.\n *\n * To convert a `Uint8Array` to a `bigint` instead, use {@link bytesToBigInt}.\n *\n * @param bytes - The bytes to convert to a number.\n * @returns The number.\n * @throws If the resulting number is not a safe integer.\n */\nexport function bytesToNumber(bytes: Uint8Array): number {\n assertIsBytes(bytes);\n\n const bigint = bytesToBigInt(bytes);\n\n assert(\n bigint <= BigInt(Number.MAX_SAFE_INTEGER),\n 'Number is not a safe integer. Use `bytesToBigInt` instead.',\n );\n\n return Number(bigint);\n}\n\n/**\n * Convert a UTF-8 encoded `Uint8Array` to a `string`.\n *\n * @param bytes - The bytes to convert to a string.\n * @returns The string.\n */\nexport function bytesToString(bytes: Uint8Array): string {\n assertIsBytes(bytes);\n\n return new TextDecoder(undefined).decode(bytes);\n}\n\n/**\n * Convert a hexadecimal string to a `Uint8Array`. The string can optionally be\n * prefixed with `0x`. It accepts even and odd length strings.\n *\n * @param value - The hexadecimal string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function hexToBytes(value: string): Uint8Array {\n assertIsHexString(value);\n\n // Remove the `0x` prefix if it exists, and pad the string to have an even\n // number of characters.\n const strippedValue = remove0x(value).toLowerCase();\n const normalizedValue =\n strippedValue.length % 2 === 0 ? strippedValue : `0${strippedValue}`;\n const bytes = new Uint8Array(normalizedValue.length / 2);\n\n for (let i = 0; i < bytes.length; i++) {\n // While this is not the prettiest way to convert a hexadecimal string to a\n // `Uint8Array`, it is a lot faster than using `parseInt` to convert each\n // character.\n const c1 = normalizedValue.charCodeAt(i * 2);\n const c2 = normalizedValue.charCodeAt(i * 2 + 1);\n const n1 =\n c1 -\n (c1 < HEX_MAXIMUM_NUMBER_CHARACTER\n ? HEX_MINIMUM_NUMBER_CHARACTER\n : HEX_CHARACTER_OFFSET);\n const n2 =\n c2 -\n (c2 < HEX_MAXIMUM_NUMBER_CHARACTER\n ? HEX_MINIMUM_NUMBER_CHARACTER\n : HEX_CHARACTER_OFFSET);\n\n bytes[i] = n1 * 16 + n2;\n }\n\n return bytes;\n}\n\n/**\n * Convert a `bigint` to a `Uint8Array`.\n *\n * This assumes that the `bigint` is an unsigned integer. To convert a signed\n * `bigint` instead, use {@link signedBigIntToBytes}.\n *\n * @param value - The bigint to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function bigIntToBytes(value: bigint): Uint8Array {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(value >= BigInt(0), 'Value must be a non-negative bigint.');\n\n const hex = value.toString(16);\n return hexToBytes(hex);\n}\n\n/**\n * Check if a `bigint` fits in a certain number of bytes.\n *\n * @param value - The `bigint` to check.\n * @param bytes - The number of bytes.\n * @returns Whether the `bigint` fits in the number of bytes.\n */\nfunction bigIntFits(value: bigint, bytes: number): boolean {\n assert(bytes > 0);\n\n /* eslint-disable no-bitwise */\n const mask = value >> BigInt(31);\n return !(((~value & mask) + (value & ~mask)) >> BigInt(bytes * 8 + ~0));\n /* eslint-enable no-bitwise */\n}\n\n/**\n * Convert a signed `bigint` to a `Uint8Array`. This uses two's complement\n * encoding to represent negative numbers.\n *\n * To convert an unsigned `bigint` to a `Uint8Array` instead, use\n * {@link bigIntToBytes}.\n *\n * @see https://en.wikipedia.org/wiki/Two%27s_complement\n * @param value - The number to convert to bytes.\n * @param byteLength - The length of the resulting `Uint8Array`. If the number\n * is larger than the maximum value that can be represented by the given length,\n * an error is thrown.\n * @returns The bytes as `Uint8Array`.\n */\nexport function signedBigIntToBytes(\n value: bigint,\n byteLength: number,\n): Uint8Array {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(typeof byteLength === 'number', 'Byte length must be a number.');\n assert(byteLength > 0, 'Byte length must be greater than 0.');\n assert(\n bigIntFits(value, byteLength),\n 'Byte length is too small to represent the given value.',\n );\n\n // ESLint doesn't like mutating function parameters, so to avoid having to\n // disable the rule, we create a new variable.\n let numberValue = value;\n const bytes = new Uint8Array(byteLength);\n\n for (let i = 0; i < bytes.length; i++) {\n bytes[i] = Number(BigInt.asUintN(8, numberValue));\n // eslint-disable-next-line no-bitwise\n numberValue >>= BigInt(8);\n }\n\n return bytes.reverse();\n}\n\n/**\n * Convert a `number` to a `Uint8Array`.\n *\n * @param value - The number to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n * @throws If the number is not a safe integer.\n */\nexport function numberToBytes(value: number): Uint8Array {\n assert(typeof value === 'number', 'Value must be a number.');\n assert(value >= 0, 'Value must be a non-negative number.');\n assert(\n Number.isSafeInteger(value),\n 'Value is not a safe integer. Use `bigIntToBytes` instead.',\n );\n\n const hex = value.toString(16);\n return hexToBytes(hex);\n}\n\n/**\n * Convert a `string` to a UTF-8 encoded `Uint8Array`.\n *\n * @param value - The string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function stringToBytes(value: string): Uint8Array {\n assert(typeof value === 'string', 'Value must be a string.');\n\n return new TextEncoder().encode(value);\n}\n\n/**\n * Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,\n * a `bigint`, a `number`, or a `string`.\n *\n * If the value is a `string`, and it is prefixed with `0x`, it will be\n * interpreted as a hexadecimal string. Otherwise, it will be interpreted as a\n * UTF-8 string. To convert a hexadecimal string to bytes without interpreting\n * it as a UTF-8 string, use {@link hexToBytes} instead.\n *\n * If the value is a `bigint`, it is assumed to be unsigned. To convert a signed\n * `bigint` to bytes, use {@link signedBigIntToBytes} instead.\n *\n * If the value is a `Uint8Array`, it will be returned as-is.\n *\n * @param value - The value to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function valueToBytes(value: Bytes): Uint8Array {\n if (typeof value === 'bigint') {\n return bigIntToBytes(value);\n }\n\n if (typeof value === 'number') {\n return numberToBytes(value);\n }\n\n if (typeof value === 'string') {\n if (value.startsWith('0x')) {\n return hexToBytes(value);\n }\n\n return stringToBytes(value);\n }\n\n if (isBytes(value)) {\n return value;\n }\n\n throw new TypeError(`Unsupported value type: \"${typeof value}\".`);\n}\n\n/**\n * Concatenate multiple byte-like values into a single `Uint8Array`. The values\n * can be `Uint8Array`, `bigint`, `number`, or `string`. This uses\n * {@link valueToBytes} under the hood to convert each value to bytes. Refer to\n * the documentation of that function for more information.\n *\n * @param values - The values to concatenate.\n * @returns The concatenated bytes as `Uint8Array`.\n */\nexport function concatBytes(values: Bytes[]): Uint8Array {\n const normalizedValues = new Array(values.length);\n let byteLength = 0;\n\n for (let i = 0; i < values.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const value = valueToBytes(values[i]!);\n\n normalizedValues[i] = value;\n byteLength += value.length;\n }\n\n const bytes = new Uint8Array(byteLength);\n for (let i = 0, offset = 0; i < normalizedValues.length; i++) {\n // While we could simply spread the values into an array and use\n // `Uint8Array.from`, that is a lot slower than using `Uint8Array.set`.\n bytes.set(normalizedValues[i], offset);\n offset += normalizedValues[i].length;\n }\n\n return bytes;\n}\n"]}
package/dist/hex.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Check if a string is a valid hex string.
3
+ *
4
+ * @param value - The value to check.
5
+ * @returns Whether the value is a valid hex string.
6
+ */
7
+ export declare function isHexString(value: unknown): value is string;
8
+ /**
9
+ * Assert that a value is a valid hex string.
10
+ *
11
+ * @param value - The value to check.
12
+ * @throws If the value is not a valid hex string.
13
+ */
14
+ export declare function assertIsHexString(value: unknown): asserts value is string;
15
+ /**
16
+ * Add the `0x`-prefix to a hexadecimal string. If the string already has the
17
+ * prefix, it is returned as-is.
18
+ *
19
+ * @param hex - The hexadecimal string to add the prefix to.
20
+ * @returns The prefixed hexadecimal string.
21
+ */
22
+ export declare function add0x(hex: string): string;
23
+ /**
24
+ * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have
25
+ * the prefix, it is returned as-is.
26
+ *
27
+ * @param hex - The hexadecimal string to remove the prefix from.
28
+ * @returns The un-prefixed hexadecimal string.
29
+ */
30
+ export declare function remove0x(hex: string): string;