@metamask/utils 8.3.0 → 8.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -1
- package/dist/assert.cjs +127 -0
- package/dist/assert.cjs.map +1 -0
- package/dist/{types/assert.d.ts → assert.d.cts} +3 -3
- package/dist/assert.d.cts.map +1 -0
- package/dist/assert.d.mts +61 -0
- package/dist/assert.d.mts.map +1 -0
- package/dist/assert.mjs +119 -15
- package/dist/assert.mjs.map +1 -1
- package/dist/base64.cjs +34 -0
- package/dist/base64.cjs.map +1 -0
- package/dist/{types/base64.d.ts → base64.d.cts} +3 -3
- package/dist/base64.d.cts.map +1 -0
- package/dist/base64.d.mts +25 -0
- package/dist/base64.d.mts.map +1 -0
- package/dist/base64.mjs +28 -9
- package/dist/base64.mjs.map +1 -1
- package/dist/bytes.cjs +400 -0
- package/dist/{chunk-QEPVHEP7.js.map → bytes.cjs.map} +1 -1
- package/dist/{types/bytes.d.ts → bytes.d.cts} +3 -3
- package/dist/bytes.d.cts.map +1 -0
- package/dist/bytes.d.mts +183 -0
- package/dist/bytes.d.mts.map +1 -0
- package/dist/bytes.mjs +379 -42
- package/dist/bytes.mjs.map +1 -1
- package/dist/caip-types.cjs +151 -0
- package/dist/caip-types.cjs.map +1 -0
- package/dist/{types/caip-types.d.ts → caip-types.d.cts} +33 -12
- package/dist/caip-types.d.cts.map +1 -0
- package/dist/caip-types.d.mts +114 -0
- package/dist/caip-types.d.mts.map +1 -0
- package/dist/caip-types.mjs +139 -39
- package/dist/caip-types.mjs.map +1 -1
- package/dist/checksum.cjs +7 -0
- package/dist/checksum.cjs.map +1 -0
- package/dist/checksum.d.cts +2 -0
- package/dist/checksum.d.cts.map +1 -0
- package/dist/checksum.d.mts +2 -0
- package/dist/checksum.d.mts.map +1 -0
- package/dist/checksum.mjs +3 -11
- package/dist/checksum.mjs.map +1 -1
- package/dist/coercers.cjs +162 -0
- package/dist/coercers.cjs.map +1 -0
- package/dist/{types/coercers.d.ts → coercers.d.cts} +7 -7
- package/dist/coercers.d.cts.map +1 -0
- package/dist/coercers.d.mts +97 -0
- package/dist/coercers.d.mts.map +1 -0
- package/dist/coercers.mjs +154 -17
- package/dist/coercers.mjs.map +1 -1
- package/dist/collections.cjs +109 -0
- package/dist/collections.cjs.map +1 -0
- package/dist/{types/collections.d.ts → collections.d.cts} +1 -1
- package/dist/collections.d.cts.map +1 -0
- package/dist/collections.d.mts +39 -0
- package/dist/collections.d.mts.map +1 -0
- package/dist/collections.mjs +103 -8
- package/dist/collections.mjs.map +1 -1
- package/dist/encryption-types.cjs +3 -0
- package/dist/encryption-types.cjs.map +1 -0
- package/dist/{types/encryption-types.d.ts → encryption-types.d.cts} +2 -2
- package/dist/encryption-types.d.cts.map +1 -0
- package/dist/encryption-types.d.mts +7 -0
- package/dist/encryption-types.d.mts.map +1 -0
- package/dist/encryption-types.mjs +1 -1
- package/dist/encryption-types.mjs.map +1 -1
- package/dist/errors.cjs +111 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/{types/errors.d.ts → errors.d.cts} +1 -1
- package/dist/errors.d.cts.map +1 -0
- package/dist/errors.d.mts +58 -0
- package/dist/errors.d.mts.map +1 -0
- package/dist/errors.mjs +102 -16
- package/dist/errors.mjs.map +1 -1
- package/dist/fs.cjs +248 -0
- package/dist/fs.cjs.map +1 -0
- package/dist/{types/fs.d.ts → fs.d.cts} +3 -3
- package/dist/fs.d.cts.map +1 -0
- package/dist/fs.d.mts +133 -0
- package/dist/fs.d.mts.map +1 -0
- package/dist/fs.mjs +209 -25
- package/dist/fs.mjs.map +1 -1
- package/dist/hex.cjs +134 -0
- package/dist/hex.cjs.map +1 -0
- package/dist/{types/hex.d.ts → hex.d.cts} +3 -3
- package/dist/hex.d.cts.map +1 -0
- package/dist/hex.d.mts +77 -0
- package/dist/hex.d.mts.map +1 -0
- package/dist/hex.mjs +121 -34
- package/dist/hex.mjs.map +1 -1
- package/dist/index.cjs +37 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +21 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +20 -292
- package/dist/index.mjs.map +1 -1
- package/dist/json.cjs +413 -0
- package/dist/json.cjs.map +1 -0
- package/dist/{types/json.d.ts → json.d.cts} +21 -22
- package/dist/json.d.cts.map +1 -0
- package/dist/json.d.mts +383 -0
- package/dist/json.d.mts.map +1 -0
- package/dist/json.mjs +389 -74
- package/dist/json.mjs.map +1 -1
- package/dist/keyring.cjs +3 -0
- package/dist/keyring.cjs.map +1 -0
- package/dist/{types/keyring.d.ts → keyring.d.cts} +7 -7
- package/dist/keyring.d.cts.map +1 -0
- package/dist/keyring.d.mts +224 -0
- package/dist/keyring.d.mts.map +1 -0
- package/dist/keyring.mjs +1 -1
- package/dist/keyring.mjs.map +1 -1
- package/dist/logging.cjs +43 -0
- package/dist/logging.cjs.map +1 -0
- package/dist/{types/logging.d.ts → logging.d.cts} +2 -2
- package/dist/logging.d.cts.map +1 -0
- package/dist/logging.d.mts +30 -0
- package/dist/logging.d.mts.map +1 -0
- package/dist/logging.mjs +34 -9
- package/dist/logging.mjs.map +1 -1
- package/dist/misc.cjs +149 -0
- package/dist/misc.cjs.map +1 -0
- package/dist/{types/misc.d.ts → misc.d.cts} +6 -6
- package/dist/misc.d.cts.map +1 -0
- package/dist/misc.d.mts +119 -0
- package/dist/misc.d.mts.map +1 -0
- package/dist/misc.mjs +136 -27
- package/dist/misc.mjs.map +1 -1
- package/dist/node.cjs +19 -0
- package/dist/node.cjs.map +1 -0
- package/dist/node.d.cts +3 -0
- package/dist/node.d.cts.map +1 -0
- package/dist/node.d.mts +3 -0
- package/dist/node.d.mts.map +1 -0
- package/dist/node.mjs +2 -312
- package/dist/node.mjs.map +1 -1
- package/dist/number.cjs +102 -0
- package/dist/number.cjs.map +1 -0
- package/dist/{types/number.d.ts → number.d.cts} +1 -1
- package/dist/number.d.cts.map +1 -0
- package/dist/number.d.mts +73 -0
- package/dist/number.d.mts.map +1 -0
- package/dist/number.mjs +93 -16
- package/dist/number.mjs.map +1 -1
- package/dist/opaque.cjs +3 -0
- package/dist/opaque.cjs.map +1 -0
- package/dist/opaque.d.cts +6 -0
- package/dist/opaque.d.cts.map +1 -0
- package/dist/opaque.d.mts +6 -0
- package/dist/opaque.d.mts.map +1 -0
- package/dist/opaque.mjs +1 -1
- package/dist/opaque.mjs.map +1 -1
- package/dist/promise.cjs +40 -0
- package/dist/promise.cjs.map +1 -0
- package/dist/{types/promise.d.ts → promise.d.cts} +12 -2
- package/dist/promise.d.cts.map +1 -0
- package/dist/promise.d.mts +45 -0
- package/dist/promise.d.mts.map +1 -0
- package/dist/promise.mjs +35 -7
- package/dist/promise.mjs.map +1 -1
- package/dist/time.cjs +67 -0
- package/dist/time.cjs.map +1 -0
- package/dist/{types/time.d.ts → time.d.cts} +1 -1
- package/dist/time.d.cts.map +1 -0
- package/dist/time.d.mts +49 -0
- package/dist/time.d.mts.map +1 -0
- package/dist/time.mjs +60 -10
- package/dist/time.mjs.map +1 -1
- package/dist/transaction-types.cjs +3 -0
- package/dist/transaction-types.cjs.map +1 -0
- package/dist/{types/transaction-types.d.ts → transaction-types.d.cts} +11 -11
- package/dist/transaction-types.d.cts.map +1 -0
- package/dist/transaction-types.d.mts +117 -0
- package/dist/transaction-types.d.mts.map +1 -0
- package/dist/transaction-types.mjs +1 -1
- package/dist/transaction-types.mjs.map +1 -1
- package/dist/versions.cjs +95 -0
- package/dist/versions.cjs.map +1 -0
- package/dist/{types/versions.d.ts → versions.d.cts} +5 -5
- package/dist/versions.d.cts.map +1 -0
- package/dist/versions.d.mts +101 -0
- package/dist/versions.d.mts.map +1 -0
- package/dist/versions.mjs +85 -26
- package/dist/versions.mjs.map +1 -1
- package/package.json +28 -17
- package/dist/assert.js +0 -16
- package/dist/assert.js.map +0 -1
- package/dist/base64.js +0 -11
- package/dist/base64.js.map +0 -1
- package/dist/bytes.js +0 -43
- package/dist/bytes.js.map +0 -1
- package/dist/caip-types.js +0 -40
- package/dist/caip-types.js.map +0 -1
- package/dist/checksum.js +0 -12
- package/dist/checksum.js.map +0 -1
- package/dist/chunk-2LBGT4GH.js +0 -15
- package/dist/chunk-2LBGT4GH.js.map +0 -1
- package/dist/chunk-3W5G4CYI.js +0 -25
- package/dist/chunk-3W5G4CYI.js.map +0 -1
- package/dist/chunk-4D6XQBHA.js +0 -69
- package/dist/chunk-4D6XQBHA.js.map +0 -1
- package/dist/chunk-4NIRTM4M.js +0 -23
- package/dist/chunk-4NIRTM4M.js.map +0 -1
- package/dist/chunk-4RMX5YWE.js +0 -34
- package/dist/chunk-4RMX5YWE.js.map +0 -1
- package/dist/chunk-52OU772R.mjs +0 -122
- package/dist/chunk-52OU772R.mjs.map +0 -1
- package/dist/chunk-5AVWINSB.js +0 -1
- package/dist/chunk-5AVWINSB.js.map +0 -1
- package/dist/chunk-622IOGVI.mjs +0 -1
- package/dist/chunk-622IOGVI.mjs.map +0 -1
- package/dist/chunk-6C35XQOF.mjs +0 -257
- package/dist/chunk-6C35XQOF.mjs.map +0 -1
- package/dist/chunk-6NZW4WK4.js +0 -35
- package/dist/chunk-6NZW4WK4.js.map +0 -1
- package/dist/chunk-6ZDHSOUV.js +0 -59
- package/dist/chunk-6ZDHSOUV.js.map +0 -1
- package/dist/chunk-74DGVJVE.mjs +0 -59
- package/dist/chunk-74DGVJVE.mjs.map +0 -1
- package/dist/chunk-AY6FDCBT.mjs +0 -1
- package/dist/chunk-AY6FDCBT.mjs.map +0 -1
- package/dist/chunk-B7LIM2PK.mjs +0 -23
- package/dist/chunk-B7LIM2PK.mjs.map +0 -1
- package/dist/chunk-BFQDMI3M.js +0 -122
- package/dist/chunk-BFQDMI3M.js.map +0 -1
- package/dist/chunk-DHVKFDHQ.js +0 -95
- package/dist/chunk-DHVKFDHQ.js.map +0 -1
- package/dist/chunk-E4C7EW4R.js +0 -16
- package/dist/chunk-E4C7EW4R.js.map +0 -1
- package/dist/chunk-EQMZL4XU.js +0 -1
- package/dist/chunk-EQMZL4XU.js.map +0 -1
- package/dist/chunk-GZS3IQBZ.mjs +0 -16
- package/dist/chunk-GZS3IQBZ.mjs.map +0 -1
- package/dist/chunk-H4YFDLB7.mjs +0 -70
- package/dist/chunk-H4YFDLB7.mjs.map +0 -1
- package/dist/chunk-I575FZFH.mjs +0 -1
- package/dist/chunk-I575FZFH.mjs.map +0 -1
- package/dist/chunk-IZC266HS.js +0 -55
- package/dist/chunk-IZC266HS.js.map +0 -1
- package/dist/chunk-JPAL7Q5S.mjs +0 -90
- package/dist/chunk-JPAL7Q5S.mjs.map +0 -1
- package/dist/chunk-LC2CRSWD.js +0 -1
- package/dist/chunk-LC2CRSWD.js.map +0 -1
- package/dist/chunk-NQMRFZHB.mjs +0 -35
- package/dist/chunk-NQMRFZHB.mjs.map +0 -1
- package/dist/chunk-O3EIM33O.mjs +0 -95
- package/dist/chunk-O3EIM33O.mjs.map +0 -1
- package/dist/chunk-OLLG4H35.js +0 -257
- package/dist/chunk-OLLG4H35.js.map +0 -1
- package/dist/chunk-QEPVHEP7.js +0 -273
- package/dist/chunk-QVEKZRZ2.js +0 -70
- package/dist/chunk-QVEKZRZ2.js.map +0 -1
- package/dist/chunk-RIRDOQPX.mjs +0 -15
- package/dist/chunk-RIRDOQPX.mjs.map +0 -1
- package/dist/chunk-RKRGAFXY.js +0 -1
- package/dist/chunk-RKRGAFXY.js.map +0 -1
- package/dist/chunk-ROQSKDP5.mjs +0 -69
- package/dist/chunk-ROQSKDP5.mjs.map +0 -1
- package/dist/chunk-RRYOWRCV.mjs +0 -1
- package/dist/chunk-RRYOWRCV.mjs.map +0 -1
- package/dist/chunk-S3UHBN2Z.mjs +0 -44
- package/dist/chunk-S3UHBN2Z.mjs.map +0 -1
- package/dist/chunk-TGOMTREC.mjs +0 -75
- package/dist/chunk-TGOMTREC.mjs.map +0 -1
- package/dist/chunk-THNLGEXV.mjs +0 -34
- package/dist/chunk-THNLGEXV.mjs.map +0 -1
- package/dist/chunk-U7ZUGCE7.js +0 -75
- package/dist/chunk-U7ZUGCE7.js.map +0 -1
- package/dist/chunk-UOTVU7OQ.js +0 -1
- package/dist/chunk-UOTVU7OQ.js.map +0 -1
- package/dist/chunk-VA2DRBDE.mjs +0 -273
- package/dist/chunk-VA2DRBDE.mjs.map +0 -1
- package/dist/chunk-VFXTVNXN.js +0 -44
- package/dist/chunk-VFXTVNXN.js.map +0 -1
- package/dist/chunk-WA4DHIND.mjs +0 -1
- package/dist/chunk-WA4DHIND.mjs.map +0 -1
- package/dist/chunk-X66SUIEF.mjs +0 -25
- package/dist/chunk-X66SUIEF.mjs.map +0 -1
- package/dist/chunk-XYGUOY6N.mjs +0 -55
- package/dist/chunk-XYGUOY6N.mjs.map +0 -1
- package/dist/chunk-Z2RGWDD7.js +0 -90
- package/dist/chunk-Z2RGWDD7.js.map +0 -1
- package/dist/coercers.js +0 -18
- package/dist/coercers.js.map +0 -1
- package/dist/collections.js +0 -10
- package/dist/collections.js.map +0 -1
- package/dist/encryption-types.js +0 -2
- package/dist/encryption-types.js.map +0 -1
- package/dist/errors.js +0 -17
- package/dist/errors.js.map +0 -1
- package/dist/fs.js +0 -26
- package/dist/fs.js.map +0 -1
- package/dist/hex.js +0 -35
- package/dist/hex.js.map +0 -1
- package/dist/index.js +0 -293
- package/dist/index.js.map +0 -1
- package/dist/json.js +0 -75
- package/dist/json.js.map +0 -1
- package/dist/keyring.js +0 -2
- package/dist/keyring.js.map +0 -1
- package/dist/logging.js +0 -10
- package/dist/logging.js.map +0 -1
- package/dist/misc.js +0 -28
- package/dist/misc.js.map +0 -1
- package/dist/node.js +0 -313
- package/dist/node.js.map +0 -1
- package/dist/number.js +0 -18
- package/dist/number.js.map +0 -1
- package/dist/opaque.js +0 -2
- package/dist/opaque.js.map +0 -1
- package/dist/promise.js +0 -8
- package/dist/promise.js.map +0 -1
- package/dist/time.js +0 -12
- package/dist/time.js.map +0 -1
- package/dist/transaction-types.js +0 -2
- package/dist/transaction-types.js.map +0 -1
- package/dist/types/assert.d.ts.map +0 -1
- package/dist/types/base64.d.ts.map +0 -1
- package/dist/types/bytes.d.ts.map +0 -1
- package/dist/types/caip-types.d.ts.map +0 -1
- package/dist/types/checksum.d.ts +0 -2
- package/dist/types/checksum.d.ts.map +0 -1
- package/dist/types/coercers.d.ts.map +0 -1
- package/dist/types/collections.d.ts.map +0 -1
- package/dist/types/encryption-types.d.ts.map +0 -1
- package/dist/types/errors.d.ts.map +0 -1
- package/dist/types/fs.d.ts.map +0 -1
- package/dist/types/hex.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -21
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/json.d.ts.map +0 -1
- package/dist/types/keyring.d.ts.map +0 -1
- package/dist/types/logging.d.ts.map +0 -1
- package/dist/types/misc.d.ts.map +0 -1
- package/dist/types/node.d.ts +0 -3
- package/dist/types/node.d.ts.map +0 -1
- package/dist/types/number.d.ts.map +0 -1
- package/dist/types/opaque.d.ts +0 -6
- package/dist/types/opaque.d.ts.map +0 -1
- package/dist/types/promise.d.ts.map +0 -1
- package/dist/types/time.d.ts.map +0 -1
- package/dist/types/transaction-types.d.ts.map +0 -1
- package/dist/types/versions.d.ts.map +0 -1
- package/dist/versions.js +0 -27
- package/dist/versions.js.map +0 -1
package/dist/bytes.d.mts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import type { Hex } from "./hex.mjs";
|
|
2
|
+
export type Bytes = bigint | number | string | Uint8Array;
|
|
3
|
+
/**
|
|
4
|
+
* Check if a value is a `Uint8Array`.
|
|
5
|
+
*
|
|
6
|
+
* @param value - The value to check.
|
|
7
|
+
* @returns Whether the value is a `Uint8Array`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function isBytes(value: unknown): value is Uint8Array;
|
|
10
|
+
/**
|
|
11
|
+
* Assert that a value is a `Uint8Array`.
|
|
12
|
+
*
|
|
13
|
+
* @param value - The value to check.
|
|
14
|
+
* @throws If the value is not a `Uint8Array`.
|
|
15
|
+
*/
|
|
16
|
+
export declare function assertIsBytes(value: unknown): asserts value is Uint8Array;
|
|
17
|
+
/**
|
|
18
|
+
* Convert a `Uint8Array` to a hexadecimal string.
|
|
19
|
+
*
|
|
20
|
+
* @param bytes - The bytes to convert to a hexadecimal string.
|
|
21
|
+
* @returns The hexadecimal string.
|
|
22
|
+
*/
|
|
23
|
+
export declare function bytesToHex(bytes: Uint8Array): Hex;
|
|
24
|
+
/**
|
|
25
|
+
* Convert a `Uint8Array` to a `bigint`.
|
|
26
|
+
*
|
|
27
|
+
* To convert a `Uint8Array` to a `number` instead, use {@link bytesToNumber}.
|
|
28
|
+
* To convert a two's complement encoded `Uint8Array` to a `bigint`, use
|
|
29
|
+
* {@link bytesToSignedBigInt}.
|
|
30
|
+
*
|
|
31
|
+
* @param bytes - The bytes to convert to a `bigint`.
|
|
32
|
+
* @returns The `bigint`.
|
|
33
|
+
*/
|
|
34
|
+
export declare function bytesToBigInt(bytes: Uint8Array): bigint;
|
|
35
|
+
/**
|
|
36
|
+
* Convert a `Uint8Array` to a signed `bigint`. This assumes that the bytes are
|
|
37
|
+
* encoded in two's complement.
|
|
38
|
+
*
|
|
39
|
+
* To convert a `Uint8Array` to an unsigned `bigint` instead, use
|
|
40
|
+
* {@link bytesToBigInt}.
|
|
41
|
+
*
|
|
42
|
+
* @see https://en.wikipedia.org/wiki/Two%27s_complement
|
|
43
|
+
* @param bytes - The bytes to convert to a signed `bigint`.
|
|
44
|
+
* @returns The signed `bigint`.
|
|
45
|
+
*/
|
|
46
|
+
export declare function bytesToSignedBigInt(bytes: Uint8Array): bigint;
|
|
47
|
+
/**
|
|
48
|
+
* Convert a `Uint8Array` to a `number`.
|
|
49
|
+
*
|
|
50
|
+
* To convert a `Uint8Array` to a `bigint` instead, use {@link bytesToBigInt}.
|
|
51
|
+
*
|
|
52
|
+
* @param bytes - The bytes to convert to a number.
|
|
53
|
+
* @returns The number.
|
|
54
|
+
* @throws If the resulting number is not a safe integer.
|
|
55
|
+
*/
|
|
56
|
+
export declare function bytesToNumber(bytes: Uint8Array): number;
|
|
57
|
+
/**
|
|
58
|
+
* Convert a UTF-8 encoded `Uint8Array` to a `string`.
|
|
59
|
+
*
|
|
60
|
+
* @param bytes - The bytes to convert to a string.
|
|
61
|
+
* @returns The string.
|
|
62
|
+
*/
|
|
63
|
+
export declare function bytesToString(bytes: Uint8Array): string;
|
|
64
|
+
/**
|
|
65
|
+
* Convert a `Uint8Array` to a base64 encoded string.
|
|
66
|
+
*
|
|
67
|
+
* @param bytes - The bytes to convert to a base64 encoded string.
|
|
68
|
+
* @returns The base64 encoded string.
|
|
69
|
+
*/
|
|
70
|
+
export declare function bytesToBase64(bytes: Uint8Array): string;
|
|
71
|
+
/**
|
|
72
|
+
* Convert a hexadecimal string to a `Uint8Array`. The string can optionally be
|
|
73
|
+
* prefixed with `0x`. It accepts even and odd length strings.
|
|
74
|
+
*
|
|
75
|
+
* If the value is "0x", an empty `Uint8Array` is returned.
|
|
76
|
+
*
|
|
77
|
+
* @param value - The hexadecimal string to convert to bytes.
|
|
78
|
+
* @returns The bytes as `Uint8Array`.
|
|
79
|
+
*/
|
|
80
|
+
export declare function hexToBytes(value: string): Uint8Array;
|
|
81
|
+
/**
|
|
82
|
+
* Convert a `bigint` to a `Uint8Array`.
|
|
83
|
+
*
|
|
84
|
+
* This assumes that the `bigint` is an unsigned integer. To convert a signed
|
|
85
|
+
* `bigint` instead, use {@link signedBigIntToBytes}.
|
|
86
|
+
*
|
|
87
|
+
* @param value - The bigint to convert to bytes.
|
|
88
|
+
* @returns The bytes as `Uint8Array`.
|
|
89
|
+
*/
|
|
90
|
+
export declare function bigIntToBytes(value: bigint): Uint8Array;
|
|
91
|
+
/**
|
|
92
|
+
* Convert a signed `bigint` to a `Uint8Array`. This uses two's complement
|
|
93
|
+
* encoding to represent negative numbers.
|
|
94
|
+
*
|
|
95
|
+
* To convert an unsigned `bigint` to a `Uint8Array` instead, use
|
|
96
|
+
* {@link bigIntToBytes}.
|
|
97
|
+
*
|
|
98
|
+
* @see https://en.wikipedia.org/wiki/Two%27s_complement
|
|
99
|
+
* @param value - The number to convert to bytes.
|
|
100
|
+
* @param byteLength - The length of the resulting `Uint8Array`. If the number
|
|
101
|
+
* is larger than the maximum value that can be represented by the given length,
|
|
102
|
+
* an error is thrown.
|
|
103
|
+
* @returns The bytes as `Uint8Array`.
|
|
104
|
+
*/
|
|
105
|
+
export declare function signedBigIntToBytes(value: bigint, byteLength: number): Uint8Array;
|
|
106
|
+
/**
|
|
107
|
+
* Convert a `number` to a `Uint8Array`.
|
|
108
|
+
*
|
|
109
|
+
* @param value - The number to convert to bytes.
|
|
110
|
+
* @returns The bytes as `Uint8Array`.
|
|
111
|
+
* @throws If the number is not a safe integer.
|
|
112
|
+
*/
|
|
113
|
+
export declare function numberToBytes(value: number): Uint8Array;
|
|
114
|
+
/**
|
|
115
|
+
* Convert a `string` to a UTF-8 encoded `Uint8Array`.
|
|
116
|
+
*
|
|
117
|
+
* @param value - The string to convert to bytes.
|
|
118
|
+
* @returns The bytes as `Uint8Array`.
|
|
119
|
+
*/
|
|
120
|
+
export declare function stringToBytes(value: string): Uint8Array;
|
|
121
|
+
/**
|
|
122
|
+
* Convert a base64 encoded string to a `Uint8Array`.
|
|
123
|
+
*
|
|
124
|
+
* @param value - The base64 encoded string to convert to bytes.
|
|
125
|
+
* @returns The bytes as `Uint8Array`.
|
|
126
|
+
*/
|
|
127
|
+
export declare function base64ToBytes(value: string): Uint8Array;
|
|
128
|
+
/**
|
|
129
|
+
* Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,
|
|
130
|
+
* a `bigint`, a `number`, or a `string`.
|
|
131
|
+
*
|
|
132
|
+
* This will attempt to guess the type of the value based on its type and
|
|
133
|
+
* contents. For more control over the conversion, use the more specific
|
|
134
|
+
* conversion functions, such as {@link hexToBytes} or {@link stringToBytes}.
|
|
135
|
+
*
|
|
136
|
+
* If the value is a `string`, and it is prefixed with `0x`, it will be
|
|
137
|
+
* interpreted as a hexadecimal string. Otherwise, it will be interpreted as a
|
|
138
|
+
* UTF-8 string. To convert a hexadecimal string to bytes without interpreting
|
|
139
|
+
* it as a UTF-8 string, use {@link hexToBytes} instead.
|
|
140
|
+
*
|
|
141
|
+
* If the value is a `bigint`, it is assumed to be unsigned. To convert a signed
|
|
142
|
+
* `bigint` to bytes, use {@link signedBigIntToBytes} instead.
|
|
143
|
+
*
|
|
144
|
+
* If the value is a `Uint8Array`, it will be returned as-is.
|
|
145
|
+
*
|
|
146
|
+
* @param value - The value to convert to bytes.
|
|
147
|
+
* @returns The bytes as `Uint8Array`.
|
|
148
|
+
*/
|
|
149
|
+
export declare function valueToBytes(value: Bytes): Uint8Array;
|
|
150
|
+
/**
|
|
151
|
+
* Concatenate multiple byte-like values into a single `Uint8Array`. The values
|
|
152
|
+
* can be `Uint8Array`, `bigint`, `number`, or `string`. This uses
|
|
153
|
+
* {@link valueToBytes} under the hood to convert each value to bytes. Refer to
|
|
154
|
+
* the documentation of that function for more information.
|
|
155
|
+
*
|
|
156
|
+
* @param values - The values to concatenate.
|
|
157
|
+
* @returns The concatenated bytes as `Uint8Array`.
|
|
158
|
+
*/
|
|
159
|
+
export declare function concatBytes(values: Bytes[]): Uint8Array;
|
|
160
|
+
/**
|
|
161
|
+
* Create a {@link DataView} from a {@link Uint8Array}. This is a convenience
|
|
162
|
+
* function that avoids having to create a {@link DataView} manually, which
|
|
163
|
+
* requires passing the `byteOffset` and `byteLength` parameters every time.
|
|
164
|
+
*
|
|
165
|
+
* Not passing the `byteOffset` and `byteLength` parameters can result in
|
|
166
|
+
* unexpected behavior when the {@link Uint8Array} is a view of a larger
|
|
167
|
+
* {@link ArrayBuffer}, e.g., when using {@link Uint8Array.subarray}.
|
|
168
|
+
*
|
|
169
|
+
* This function also supports Node.js {@link Buffer}s.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* const bytes = new Uint8Array([1, 2, 3]);
|
|
174
|
+
*
|
|
175
|
+
* // This is equivalent to:
|
|
176
|
+
* // const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
177
|
+
* const dataView = createDataView(bytes);
|
|
178
|
+
* ```
|
|
179
|
+
* @param bytes - The bytes to create the {@link DataView} from.
|
|
180
|
+
* @returns The {@link DataView}.
|
|
181
|
+
*/
|
|
182
|
+
export declare function createDataView(bytes: Uint8Array): DataView;
|
|
183
|
+
//# sourceMappingURL=bytes.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.d.mts","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAE,kBAAc;AAUjC,MAAM,MAAM,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU,CAAC;AAwC1D;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,UAAU,CAE3D;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,UAAU,CAEzE;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,GAAG,CAgBjD;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAKvD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAU7D;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAWvD;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAIvD;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,UA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|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
1
|
+
import { base64 } from "@scure/base";
|
|
2
|
+
import { assert } from "./assert.mjs";
|
|
3
|
+
import { add0x, assertIsHexString, remove0x } from "./hex.mjs";
|
|
4
|
+
// '0'.charCodeAt(0) === 48
|
|
5
|
+
const HEX_MINIMUM_NUMBER_CHARACTER = 48;
|
|
6
|
+
// '9'.charCodeAt(0) === 57
|
|
7
|
+
const HEX_MAXIMUM_NUMBER_CHARACTER = 58;
|
|
8
|
+
const HEX_CHARACTER_OFFSET = 87;
|
|
9
|
+
/**
|
|
10
|
+
* Memoized function that returns an array to be used as a lookup table for
|
|
11
|
+
* converting bytes to hexadecimal values.
|
|
12
|
+
*
|
|
13
|
+
* The array is created lazily and then cached for future use. The benefit of
|
|
14
|
+
* this approach is that the performance of converting bytes to hex is much
|
|
15
|
+
* better than if we were to call `toString(16)` on each byte.
|
|
16
|
+
*
|
|
17
|
+
* The downside is that the array is created once and then never garbage
|
|
18
|
+
* collected. This is not a problem in practice because the array is only 256
|
|
19
|
+
* elements long.
|
|
20
|
+
*
|
|
21
|
+
* @returns A function that returns the lookup table.
|
|
22
|
+
*/
|
|
23
|
+
function getPrecomputedHexValuesBuilder() {
|
|
24
|
+
// To avoid issues with tree shaking, we need to use a function to return the
|
|
25
|
+
// array. This is because the array is only used in the `bytesToHex` function
|
|
26
|
+
// and if we were to use a global variable, the array might be removed by the
|
|
27
|
+
// tree shaker.
|
|
28
|
+
const lookupTable = [];
|
|
29
|
+
return () => {
|
|
30
|
+
if (lookupTable.length === 0) {
|
|
31
|
+
for (let i = 0; i < 256; i++) {
|
|
32
|
+
lookupTable.push(i.toString(16).padStart(2, '0'));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return lookupTable;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Function implementation of the {@link getPrecomputedHexValuesBuilder}
|
|
40
|
+
* function.
|
|
41
|
+
*/
|
|
42
|
+
const getPrecomputedHexValues = getPrecomputedHexValuesBuilder();
|
|
43
|
+
/**
|
|
44
|
+
* Check if a value is a `Uint8Array`.
|
|
45
|
+
*
|
|
46
|
+
* @param value - The value to check.
|
|
47
|
+
* @returns Whether the value is a `Uint8Array`.
|
|
48
|
+
*/
|
|
49
|
+
export function isBytes(value) {
|
|
50
|
+
return value instanceof Uint8Array;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Assert that a value is a `Uint8Array`.
|
|
54
|
+
*
|
|
55
|
+
* @param value - The value to check.
|
|
56
|
+
* @throws If the value is not a `Uint8Array`.
|
|
57
|
+
*/
|
|
58
|
+
export function assertIsBytes(value) {
|
|
59
|
+
assert(isBytes(value), 'Value must be a Uint8Array.');
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Convert a `Uint8Array` to a hexadecimal string.
|
|
63
|
+
*
|
|
64
|
+
* @param bytes - The bytes to convert to a hexadecimal string.
|
|
65
|
+
* @returns The hexadecimal string.
|
|
66
|
+
*/
|
|
67
|
+
export function bytesToHex(bytes) {
|
|
68
|
+
assertIsBytes(bytes);
|
|
69
|
+
if (bytes.length === 0) {
|
|
70
|
+
return '0x';
|
|
71
|
+
}
|
|
72
|
+
const lookupTable = getPrecomputedHexValues();
|
|
73
|
+
const hexadecimal = new Array(bytes.length);
|
|
74
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
75
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
76
|
+
hexadecimal[i] = lookupTable[bytes[i]];
|
|
77
|
+
}
|
|
78
|
+
return add0x(hexadecimal.join(''));
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Convert a `Uint8Array` to a `bigint`.
|
|
82
|
+
*
|
|
83
|
+
* To convert a `Uint8Array` to a `number` instead, use {@link bytesToNumber}.
|
|
84
|
+
* To convert a two's complement encoded `Uint8Array` to a `bigint`, use
|
|
85
|
+
* {@link bytesToSignedBigInt}.
|
|
86
|
+
*
|
|
87
|
+
* @param bytes - The bytes to convert to a `bigint`.
|
|
88
|
+
* @returns The `bigint`.
|
|
89
|
+
*/
|
|
90
|
+
export function bytesToBigInt(bytes) {
|
|
91
|
+
assertIsBytes(bytes);
|
|
92
|
+
const hexadecimal = bytesToHex(bytes);
|
|
93
|
+
return BigInt(hexadecimal);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Convert a `Uint8Array` to a signed `bigint`. This assumes that the bytes are
|
|
97
|
+
* encoded in two's complement.
|
|
98
|
+
*
|
|
99
|
+
* To convert a `Uint8Array` to an unsigned `bigint` instead, use
|
|
100
|
+
* {@link bytesToBigInt}.
|
|
101
|
+
*
|
|
102
|
+
* @see https://en.wikipedia.org/wiki/Two%27s_complement
|
|
103
|
+
* @param bytes - The bytes to convert to a signed `bigint`.
|
|
104
|
+
* @returns The signed `bigint`.
|
|
105
|
+
*/
|
|
106
|
+
export function bytesToSignedBigInt(bytes) {
|
|
107
|
+
assertIsBytes(bytes);
|
|
108
|
+
let value = BigInt(0);
|
|
109
|
+
for (const byte of bytes) {
|
|
110
|
+
// eslint-disable-next-line no-bitwise
|
|
111
|
+
value = (value << BigInt(8)) + BigInt(byte);
|
|
112
|
+
}
|
|
113
|
+
return BigInt.asIntN(bytes.length * 8, value);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Convert a `Uint8Array` to a `number`.
|
|
117
|
+
*
|
|
118
|
+
* To convert a `Uint8Array` to a `bigint` instead, use {@link bytesToBigInt}.
|
|
119
|
+
*
|
|
120
|
+
* @param bytes - The bytes to convert to a number.
|
|
121
|
+
* @returns The number.
|
|
122
|
+
* @throws If the resulting number is not a safe integer.
|
|
123
|
+
*/
|
|
124
|
+
export function bytesToNumber(bytes) {
|
|
125
|
+
assertIsBytes(bytes);
|
|
126
|
+
const bigint = bytesToBigInt(bytes);
|
|
127
|
+
assert(bigint <= BigInt(Number.MAX_SAFE_INTEGER), 'Number is not a safe integer. Use `bytesToBigInt` instead.');
|
|
128
|
+
return Number(bigint);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Convert a UTF-8 encoded `Uint8Array` to a `string`.
|
|
132
|
+
*
|
|
133
|
+
* @param bytes - The bytes to convert to a string.
|
|
134
|
+
* @returns The string.
|
|
135
|
+
*/
|
|
136
|
+
export function bytesToString(bytes) {
|
|
137
|
+
assertIsBytes(bytes);
|
|
138
|
+
return new TextDecoder().decode(bytes);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Convert a `Uint8Array` to a base64 encoded string.
|
|
142
|
+
*
|
|
143
|
+
* @param bytes - The bytes to convert to a base64 encoded string.
|
|
144
|
+
* @returns The base64 encoded string.
|
|
145
|
+
*/
|
|
146
|
+
export function bytesToBase64(bytes) {
|
|
147
|
+
assertIsBytes(bytes);
|
|
148
|
+
return base64.encode(bytes);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Convert a hexadecimal string to a `Uint8Array`. The string can optionally be
|
|
152
|
+
* prefixed with `0x`. It accepts even and odd length strings.
|
|
153
|
+
*
|
|
154
|
+
* If the value is "0x", an empty `Uint8Array` is returned.
|
|
155
|
+
*
|
|
156
|
+
* @param value - The hexadecimal string to convert to bytes.
|
|
157
|
+
* @returns The bytes as `Uint8Array`.
|
|
158
|
+
*/
|
|
159
|
+
export function hexToBytes(value) {
|
|
160
|
+
// "0x" is often used as empty byte array.
|
|
161
|
+
if (value?.toLowerCase?.() === '0x') {
|
|
162
|
+
return new Uint8Array();
|
|
163
|
+
}
|
|
164
|
+
assertIsHexString(value);
|
|
165
|
+
// Remove the `0x` prefix if it exists, and pad the string to have an even
|
|
166
|
+
// number of characters.
|
|
167
|
+
const strippedValue = remove0x(value).toLowerCase();
|
|
168
|
+
const normalizedValue = strippedValue.length % 2 === 0 ? strippedValue : `0${strippedValue}`;
|
|
169
|
+
const bytes = new Uint8Array(normalizedValue.length / 2);
|
|
170
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
171
|
+
// While this is not the prettiest way to convert a hexadecimal string to a
|
|
172
|
+
// `Uint8Array`, it is a lot faster than using `parseInt` to convert each
|
|
173
|
+
// character.
|
|
174
|
+
const c1 = normalizedValue.charCodeAt(i * 2);
|
|
175
|
+
const c2 = normalizedValue.charCodeAt(i * 2 + 1);
|
|
176
|
+
const n1 = c1 -
|
|
177
|
+
(c1 < HEX_MAXIMUM_NUMBER_CHARACTER
|
|
178
|
+
? HEX_MINIMUM_NUMBER_CHARACTER
|
|
179
|
+
: HEX_CHARACTER_OFFSET);
|
|
180
|
+
const n2 = c2 -
|
|
181
|
+
(c2 < HEX_MAXIMUM_NUMBER_CHARACTER
|
|
182
|
+
? HEX_MINIMUM_NUMBER_CHARACTER
|
|
183
|
+
: HEX_CHARACTER_OFFSET);
|
|
184
|
+
bytes[i] = n1 * 16 + n2;
|
|
185
|
+
}
|
|
186
|
+
return bytes;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Convert a `bigint` to a `Uint8Array`.
|
|
190
|
+
*
|
|
191
|
+
* This assumes that the `bigint` is an unsigned integer. To convert a signed
|
|
192
|
+
* `bigint` instead, use {@link signedBigIntToBytes}.
|
|
193
|
+
*
|
|
194
|
+
* @param value - The bigint to convert to bytes.
|
|
195
|
+
* @returns The bytes as `Uint8Array`.
|
|
196
|
+
*/
|
|
197
|
+
export function bigIntToBytes(value) {
|
|
198
|
+
assert(typeof value === 'bigint', 'Value must be a bigint.');
|
|
199
|
+
assert(value >= BigInt(0), 'Value must be a non-negative bigint.');
|
|
200
|
+
const hexadecimal = value.toString(16);
|
|
201
|
+
return hexToBytes(hexadecimal);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Check if a `bigint` fits in a certain number of bytes.
|
|
205
|
+
*
|
|
206
|
+
* @param value - The `bigint` to check.
|
|
207
|
+
* @param bytes - The number of bytes.
|
|
208
|
+
* @returns Whether the `bigint` fits in the number of bytes.
|
|
209
|
+
*/
|
|
210
|
+
function bigIntFits(value, bytes) {
|
|
211
|
+
assert(bytes > 0);
|
|
212
|
+
/* eslint-disable no-bitwise */
|
|
213
|
+
const mask = value >> BigInt(31);
|
|
214
|
+
return !(((~value & mask) + (value & ~mask)) >> BigInt(bytes * 8 + ~0));
|
|
215
|
+
/* eslint-enable no-bitwise */
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Convert a signed `bigint` to a `Uint8Array`. This uses two's complement
|
|
219
|
+
* encoding to represent negative numbers.
|
|
220
|
+
*
|
|
221
|
+
* To convert an unsigned `bigint` to a `Uint8Array` instead, use
|
|
222
|
+
* {@link bigIntToBytes}.
|
|
223
|
+
*
|
|
224
|
+
* @see https://en.wikipedia.org/wiki/Two%27s_complement
|
|
225
|
+
* @param value - The number to convert to bytes.
|
|
226
|
+
* @param byteLength - The length of the resulting `Uint8Array`. If the number
|
|
227
|
+
* is larger than the maximum value that can be represented by the given length,
|
|
228
|
+
* an error is thrown.
|
|
229
|
+
* @returns The bytes as `Uint8Array`.
|
|
230
|
+
*/
|
|
231
|
+
export function signedBigIntToBytes(value, byteLength) {
|
|
232
|
+
assert(typeof value === 'bigint', 'Value must be a bigint.');
|
|
233
|
+
assert(typeof byteLength === 'number', 'Byte length must be a number.');
|
|
234
|
+
assert(byteLength > 0, 'Byte length must be greater than 0.');
|
|
235
|
+
assert(bigIntFits(value, byteLength), 'Byte length is too small to represent the given value.');
|
|
236
|
+
// ESLint doesn't like mutating function parameters, so to avoid having to
|
|
237
|
+
// disable the rule, we create a new variable.
|
|
238
|
+
let numberValue = value;
|
|
239
|
+
const bytes = new Uint8Array(byteLength);
|
|
240
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
241
|
+
bytes[i] = Number(BigInt.asUintN(8, numberValue));
|
|
242
|
+
// eslint-disable-next-line no-bitwise
|
|
243
|
+
numberValue >>= BigInt(8);
|
|
244
|
+
}
|
|
245
|
+
return bytes.reverse();
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Convert a `number` to a `Uint8Array`.
|
|
249
|
+
*
|
|
250
|
+
* @param value - The number to convert to bytes.
|
|
251
|
+
* @returns The bytes as `Uint8Array`.
|
|
252
|
+
* @throws If the number is not a safe integer.
|
|
253
|
+
*/
|
|
254
|
+
export function numberToBytes(value) {
|
|
255
|
+
assert(typeof value === 'number', 'Value must be a number.');
|
|
256
|
+
assert(value >= 0, 'Value must be a non-negative number.');
|
|
257
|
+
assert(Number.isSafeInteger(value), 'Value is not a safe integer. Use `bigIntToBytes` instead.');
|
|
258
|
+
const hexadecimal = value.toString(16);
|
|
259
|
+
return hexToBytes(hexadecimal);
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Convert a `string` to a UTF-8 encoded `Uint8Array`.
|
|
263
|
+
*
|
|
264
|
+
* @param value - The string to convert to bytes.
|
|
265
|
+
* @returns The bytes as `Uint8Array`.
|
|
266
|
+
*/
|
|
267
|
+
export function stringToBytes(value) {
|
|
268
|
+
assert(typeof value === 'string', 'Value must be a string.');
|
|
269
|
+
return new TextEncoder().encode(value);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Convert a base64 encoded string to a `Uint8Array`.
|
|
273
|
+
*
|
|
274
|
+
* @param value - The base64 encoded string to convert to bytes.
|
|
275
|
+
* @returns The bytes as `Uint8Array`.
|
|
276
|
+
*/
|
|
277
|
+
export function base64ToBytes(value) {
|
|
278
|
+
assert(typeof value === 'string', 'Value must be a string.');
|
|
279
|
+
return base64.decode(value);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,
|
|
283
|
+
* a `bigint`, a `number`, or a `string`.
|
|
284
|
+
*
|
|
285
|
+
* This will attempt to guess the type of the value based on its type and
|
|
286
|
+
* contents. For more control over the conversion, use the more specific
|
|
287
|
+
* conversion functions, such as {@link hexToBytes} or {@link stringToBytes}.
|
|
288
|
+
*
|
|
289
|
+
* If the value is a `string`, and it is prefixed with `0x`, it will be
|
|
290
|
+
* interpreted as a hexadecimal string. Otherwise, it will be interpreted as a
|
|
291
|
+
* UTF-8 string. To convert a hexadecimal string to bytes without interpreting
|
|
292
|
+
* it as a UTF-8 string, use {@link hexToBytes} instead.
|
|
293
|
+
*
|
|
294
|
+
* If the value is a `bigint`, it is assumed to be unsigned. To convert a signed
|
|
295
|
+
* `bigint` to bytes, use {@link signedBigIntToBytes} instead.
|
|
296
|
+
*
|
|
297
|
+
* If the value is a `Uint8Array`, it will be returned as-is.
|
|
298
|
+
*
|
|
299
|
+
* @param value - The value to convert to bytes.
|
|
300
|
+
* @returns The bytes as `Uint8Array`.
|
|
301
|
+
*/
|
|
302
|
+
export function valueToBytes(value) {
|
|
303
|
+
if (typeof value === 'bigint') {
|
|
304
|
+
return bigIntToBytes(value);
|
|
305
|
+
}
|
|
306
|
+
if (typeof value === 'number') {
|
|
307
|
+
return numberToBytes(value);
|
|
308
|
+
}
|
|
309
|
+
if (typeof value === 'string') {
|
|
310
|
+
if (value.startsWith('0x')) {
|
|
311
|
+
return hexToBytes(value);
|
|
312
|
+
}
|
|
313
|
+
return stringToBytes(value);
|
|
314
|
+
}
|
|
315
|
+
if (isBytes(value)) {
|
|
316
|
+
return value;
|
|
317
|
+
}
|
|
318
|
+
throw new TypeError(`Unsupported value type: "${typeof value}".`);
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Concatenate multiple byte-like values into a single `Uint8Array`. The values
|
|
322
|
+
* can be `Uint8Array`, `bigint`, `number`, or `string`. This uses
|
|
323
|
+
* {@link valueToBytes} under the hood to convert each value to bytes. Refer to
|
|
324
|
+
* the documentation of that function for more information.
|
|
325
|
+
*
|
|
326
|
+
* @param values - The values to concatenate.
|
|
327
|
+
* @returns The concatenated bytes as `Uint8Array`.
|
|
328
|
+
*/
|
|
329
|
+
export function concatBytes(values) {
|
|
330
|
+
const normalizedValues = new Array(values.length);
|
|
331
|
+
let byteLength = 0;
|
|
332
|
+
for (let i = 0; i < values.length; i++) {
|
|
333
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
334
|
+
const value = valueToBytes(values[i]);
|
|
335
|
+
normalizedValues[i] = value;
|
|
336
|
+
byteLength += value.length;
|
|
337
|
+
}
|
|
338
|
+
const bytes = new Uint8Array(byteLength);
|
|
339
|
+
for (let i = 0, offset = 0; i < normalizedValues.length; i++) {
|
|
340
|
+
// While we could simply spread the values into an array and use
|
|
341
|
+
// `Uint8Array.from`, that is a lot slower than using `Uint8Array.set`.
|
|
342
|
+
bytes.set(normalizedValues[i], offset);
|
|
343
|
+
offset += normalizedValues[i].length;
|
|
344
|
+
}
|
|
345
|
+
return bytes;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Create a {@link DataView} from a {@link Uint8Array}. This is a convenience
|
|
349
|
+
* function that avoids having to create a {@link DataView} manually, which
|
|
350
|
+
* requires passing the `byteOffset` and `byteLength` parameters every time.
|
|
351
|
+
*
|
|
352
|
+
* Not passing the `byteOffset` and `byteLength` parameters can result in
|
|
353
|
+
* unexpected behavior when the {@link Uint8Array} is a view of a larger
|
|
354
|
+
* {@link ArrayBuffer}, e.g., when using {@link Uint8Array.subarray}.
|
|
355
|
+
*
|
|
356
|
+
* This function also supports Node.js {@link Buffer}s.
|
|
357
|
+
*
|
|
358
|
+
* @example
|
|
359
|
+
* ```typescript
|
|
360
|
+
* const bytes = new Uint8Array([1, 2, 3]);
|
|
361
|
+
*
|
|
362
|
+
* // This is equivalent to:
|
|
363
|
+
* // const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
364
|
+
* const dataView = createDataView(bytes);
|
|
365
|
+
* ```
|
|
366
|
+
* @param bytes - The bytes to create the {@link DataView} from.
|
|
367
|
+
* @returns The {@link DataView}.
|
|
368
|
+
*/
|
|
369
|
+
export function createDataView(bytes) {
|
|
370
|
+
// To maintain compatibility with Node.js, we need to check if the bytes are
|
|
371
|
+
// a Buffer. If so, we need to slice the buffer to get the underlying
|
|
372
|
+
// ArrayBuffer.
|
|
373
|
+
// eslint-disable-next-line no-restricted-globals
|
|
374
|
+
if (typeof Buffer !== 'undefined' && bytes instanceof Buffer) {
|
|
375
|
+
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
376
|
+
return new DataView(buffer);
|
|
377
|
+
}
|
|
378
|
+
return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
379
|
+
}
|
|
43
380
|
//# sourceMappingURL=bytes.mjs.map
|
package/dist/bytes.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
1
|
+
{"version":3,"file":"bytes.mjs","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,oBAAoB;AAErC,OAAO,EAAE,MAAM,EAAE,qBAAiB;AAElC,OAAO,EAAE,KAAK,EAAE,iBAAiB,EAAE,QAAQ,EAAE,kBAAc;AAE3D,2BAA2B;AAC3B,MAAM,4BAA4B,GAAG,EAAE,CAAC;AAExC,2BAA2B;AAC3B,MAAM,4BAA4B,GAAG,EAAE,CAAC;AACxC,MAAM,oBAAoB,GAAG,EAAE,CAAC;AAIhC;;;;;;;;;;;;;GAaG;AACH,SAAS,8BAA8B;IACrC,6EAA6E;IAC7E,6EAA6E;IAC7E,6EAA6E;IAC7E,eAAe;IACf,MAAM,WAAW,GAAa,EAAE,CAAC;IAEjC,OAAO,GAAG,EAAE;QACV,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,EAAE;YAC5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE;gBAC5B,WAAW,CAAC,IAA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OAAO,MAAM,KAAK,WAAW,IAAI,KAAK,YAAY,MAAM,EAAE;QAC5D,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAC/B,KAAK,CAAC,UAAU,EAChB,KAAK,CAAC,UAAU,GAAG,KAAK,CAAC,UAAU,CACpC,CAAC;QAEF,OAAO,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC;KAC7B;IAED,OAAO,IAAI,QAAQ,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC;AACxE,CAAC","sourcesContent":["import { base64 } from '@scure/base';\n\nimport { assert } from './assert';\nimport type { Hex } from './hex';\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): Hex {\n assertIsBytes(bytes);\n\n if (bytes.length === 0) {\n return '0x';\n }\n\n const lookupTable = getPrecomputedHexValues();\n const hexadecimal = 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 hexadecimal[i] = lookupTable[bytes[i]!];\n }\n\n return add0x(hexadecimal.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 hexadecimal = bytesToHex(bytes);\n return BigInt(hexadecimal);\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().decode(bytes);\n}\n\n/**\n * Convert a `Uint8Array` to a base64 encoded string.\n *\n * @param bytes - The bytes to convert to a base64 encoded string.\n * @returns The base64 encoded string.\n */\nexport function bytesToBase64(bytes: Uint8Array): string {\n assertIsBytes(bytes);\n\n return base64.encode(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 * If the value is \"0x\", an empty `Uint8Array` is returned.\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 // \"0x\" is often used as empty byte array.\n if (value?.toLowerCase?.() === '0x') {\n return new Uint8Array();\n }\n\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 hexadecimal = value.toString(16);\n return hexToBytes(hexadecimal);\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 hexadecimal = value.toString(16);\n return hexToBytes(hexadecimal);\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 base64 encoded string to a `Uint8Array`.\n *\n * @param value - The base64 encoded string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function base64ToBytes(value: string): Uint8Array {\n assert(typeof value === 'string', 'Value must be a string.');\n\n return base64.decode(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 * This will attempt to guess the type of the value based on its type and\n * contents. For more control over the conversion, use the more specific\n * conversion functions, such as {@link hexToBytes} or {@link stringToBytes}.\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\n/**\n * Create a {@link DataView} from a {@link Uint8Array}. This is a convenience\n * function that avoids having to create a {@link DataView} manually, which\n * requires passing the `byteOffset` and `byteLength` parameters every time.\n *\n * Not passing the `byteOffset` and `byteLength` parameters can result in\n * unexpected behavior when the {@link Uint8Array} is a view of a larger\n * {@link ArrayBuffer}, e.g., when using {@link Uint8Array.subarray}.\n *\n * This function also supports Node.js {@link Buffer}s.\n *\n * @example\n * ```typescript\n * const bytes = new Uint8Array([1, 2, 3]);\n *\n * // This is equivalent to:\n * // const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n * const dataView = createDataView(bytes);\n * ```\n * @param bytes - The bytes to create the {@link DataView} from.\n * @returns The {@link DataView}.\n */\nexport function createDataView(bytes: Uint8Array): DataView {\n // To maintain compatibility with Node.js, we need to check if the bytes are\n // a Buffer. If so, we need to slice the buffer to get the underlying\n // ArrayBuffer.\n // eslint-disable-next-line no-restricted-globals\n if (typeof Buffer !== 'undefined' && bytes instanceof Buffer) {\n const buffer = bytes.buffer.slice(\n bytes.byteOffset,\n bytes.byteOffset + bytes.byteLength,\n );\n\n return new DataView(buffer);\n }\n\n return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n}\n"]}
|