@metamask-previews/utils 12.0.0-preview-601bac5ab → 12.0.0-preview-cbae429b5
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 +1 -20
- package/dist/assert.d.ts +47 -1
- package/dist/assert.d.ts.map +1 -1
- package/dist/assert.js +81 -3
- package/dist/assert.js.map +1 -1
- package/dist/base64.d.ts.map +1 -1
- package/dist/base64.js +1 -1
- package/dist/base64.js.map +1 -1
- package/dist/bytes.d.ts +198 -0
- package/dist/bytes.d.ts.map +1 -0
- package/dist/bytes.js +419 -0
- package/dist/bytes.js.map +1 -0
- package/dist/coercers.d.ts +1 -1
- package/dist/coercers.d.ts.map +1 -1
- package/dist/coercers.js +2 -2
- package/dist/coercers.js.map +1 -1
- package/dist/errors.d.ts +68 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +114 -0
- package/dist/errors.js.map +1 -0
- package/dist/fs.d.ts +133 -0
- package/dist/fs.d.ts.map +1 -0
- package/dist/fs.js +210 -0
- package/dist/fs.js.map +1 -0
- package/dist/hex.d.ts +47 -1
- package/dist/hex.d.ts.map +1 -1
- package/dist/hex.js +72 -2
- package/dist/hex.js.map +1 -1
- package/dist/index.d.ts +5 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/json.d.ts +7 -2
- package/dist/json.d.ts.map +1 -1
- package/dist/json.js +1 -1
- package/dist/json.js.map +1 -1
- package/dist/keyring.d.ts +2 -2
- package/dist/keyring.d.ts.map +1 -1
- package/dist/keyring.js.map +1 -1
- package/dist/misc.d.ts +88 -0
- package/dist/misc.d.ts.map +1 -1
- package/dist/misc.js +83 -0
- package/dist/misc.js.map +1 -1
- package/dist/node.d.ts +1 -2
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +1 -1
- package/dist/node.js.map +1 -1
- package/dist/number.d.ts +74 -0
- package/dist/number.d.ts.map +1 -0
- package/dist/number.js +95 -0
- package/dist/number.js.map +1 -0
- package/dist/transaction-types.d.ts +2 -2
- package/dist/transaction-types.d.ts.map +1 -1
- package/dist/transaction-types.js.map +1 -1
- package/package.json +32 -17
package/CHANGELOG.md
CHANGED
|
@@ -7,33 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
-
### Added
|
|
11
|
-
|
|
12
|
-
- Add support for Node.js 26 ([#10981](https://github.com/MetaMask/core/pull/10981))
|
|
13
|
-
|
|
14
10
|
### Changed
|
|
15
11
|
|
|
16
12
|
- This package was migrated from `MetaMask/utils` to the `MetaMask/core` monorepo ([#10175](https://github.com/MetaMask/core/pull/10175))
|
|
17
13
|
- See [MetaMask/utils](https://github.com/MetaMask/utils/blob/main/CHANGELOG.md) for the original changelog, which covers every release up to and including `12.0.0`.
|
|
18
|
-
- Move `Json`, `Mutable`, `NonEmptyArray`, `PartialOrAbsent`, `RuntimeObject`, `getKnownPropertyNames`, `hasProperty`, `isNullOrUndefined`, `isObject`, and `isPlainObject` to `@metamask/type-utils` ([#10908](https://github.com/MetaMask/core/pull/10908))
|
|
19
|
-
- These are still re-exported from `@metamask/utils`, so the public API is unchanged.
|
|
20
|
-
- It's recommended to import these types and functions from `@metamask/type-utils` instead of `@metamask/utils` going forward.
|
|
21
|
-
- Move `AssertionError`, `AssertionErrorConstructor`, `assert`, and `assertExhaustive` to `@metamask/type-utils` ([#10983](https://github.com/MetaMask/core/pull/10983))
|
|
22
|
-
- These are still re-exported from `@metamask/utils`, so the public API is unchanged.
|
|
23
|
-
- It's recommended to import these types and functions from `@metamask/type-utils` instead of `@metamask/utils` going forward.
|
|
24
|
-
- Move `ensureError`, `getErrorMessage`, `isErrorWithCode`, `isErrorWithMessage`, `isErrorWithStack`, and `wrapError` to `@metamask/error-utils` ([#10917](https://github.com/MetaMask/core/pull/10917))
|
|
25
|
-
- These are still re-exported from `@metamask/utils`, so the public API is unchanged.
|
|
26
|
-
- It's recommended to import these functions from `@metamask/error-utils` instead of `@metamask/utils` going forward.
|
|
27
|
-
- Move `FileSandbox`, `createSandbox`, `directoryExists`, `ensureDirectoryStructureExists`, `fileExists`, `forceRemove`, `readFile`, `readJsonFile`, `writeFile`, and `writeJsonFile` to `@metamask/node-utils` ([#10926](https://github.com/MetaMask/core/pull/10926))
|
|
28
|
-
- These are still re-exported from `@metamask/utils/node`, so the public API is unchanged.
|
|
29
|
-
- It's recommended to import these types and functions from `@metamask/node-utils` instead of `@metamask/utils/node` going forward.
|
|
30
|
-
- Move `Bytes`, `Hex`, `HEX_REGEX`, `STRICT_HEX_REGEX`, `add0x`, `areUint8ArraysEqual`, `assertIsBytes`, `assertIsHexString`, `assertIsStrictHexString`, `base64ToBytes`, `bigIntToBytes`, `bigIntToHex`, `bytesToBase64`, `bytesToBigInt`, `bytesToHex`, `bytesToNumber`, `bytesToSignedBigInt`, `bytesToString`, `concatBytes`, `createDataView`, `hexToBigInt`, `hexToBytes`, `hexToNumber`, `isBytes`, `isHexString`, `isStrictHexString`, `numberToBytes`, `numberToHex`, `remove0x`, `signedBigIntToBytes`, `stringToBytes`, and `valueToBytes` to `@metamask/byte-utils` ([#11037](https://github.com/MetaMask/core/pull/11037))
|
|
31
|
-
- These are still re-exported from `@metamask/utils`, so the public API is unchanged.
|
|
32
|
-
- It's recommended to import these types and functions from `@metamask/byte-utils` instead of `@metamask/utils` going forward.
|
|
33
14
|
- Bump `uuid` from `^9.0.1` to `^11.1.1` ([#10243](https://github.com/MetaMask/core/pull/10243))
|
|
34
15
|
- Bump `semver` from `^7.6.3` to `^7.8.5` ([#10443](https://github.com/MetaMask/core/pull/10443))
|
|
35
16
|
- Bump `pony-cause` from `^2.1.10` to `^2.1.11` ([#10440](https://github.com/MetaMask/core/pull/10440))
|
|
36
|
-
- Bump `@types/lodash` from `^4.17.20` to `^4.17.
|
|
17
|
+
- Bump `@types/lodash` from `^4.17.20` to `^4.17.25` ([#10435](https://github.com/MetaMask/core/pull/10435))
|
|
37
18
|
- Bump `lodash` from `^4.17.21` to `^4.18.1` ([#10447](https://github.com/MetaMask/core/pull/10447))
|
|
38
19
|
- Bump `debug` from `^4.3.4` to `^4.4.3` ([#10523](https://github.com/MetaMask/core/pull/10523))
|
|
39
20
|
|
package/dist/assert.d.ts
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
import type { Struct } from '@metamask/superstruct';
|
|
2
|
-
|
|
2
|
+
export type AssertionErrorConstructor = (new (args: {
|
|
3
|
+
message: string;
|
|
4
|
+
}) => Error) | ((args: {
|
|
5
|
+
message: string;
|
|
6
|
+
}) => Error);
|
|
7
|
+
/**
|
|
8
|
+
* The default error class that is thrown if an assertion fails.
|
|
9
|
+
*/
|
|
10
|
+
export declare class AssertionError extends Error {
|
|
11
|
+
readonly code = "ERR_ASSERTION";
|
|
12
|
+
constructor(options: {
|
|
13
|
+
message: string;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Same as Node.js assert.
|
|
18
|
+
* If the value is falsy, throws an error, does nothing otherwise.
|
|
19
|
+
*
|
|
20
|
+
* @throws {@link AssertionError} If value is falsy.
|
|
21
|
+
* @param value - The test that should be truthy to pass.
|
|
22
|
+
* @param message - Message to be passed to {@link AssertionError} or an
|
|
23
|
+
* {@link Error} instance to throw.
|
|
24
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
25
|
+
* Defaults to {@link AssertionError}. If a custom error class is provided for
|
|
26
|
+
* the `message` argument, this argument is ignored.
|
|
27
|
+
*/
|
|
28
|
+
export declare function assert(value: any, message?: string | Error, ErrorWrapper?: AssertionErrorConstructor): asserts value;
|
|
3
29
|
/**
|
|
4
30
|
* Assert a value against a Superstruct struct.
|
|
5
31
|
*
|
|
@@ -12,4 +38,24 @@ import type { AssertionErrorConstructor } from '@metamask/type-utils';
|
|
|
12
38
|
* @throws If the value is not valid.
|
|
13
39
|
*/
|
|
14
40
|
export declare function assertStruct<Type, Schema>(value: unknown, struct: Struct<Type, Schema>, errorPrefix?: string, ErrorWrapper?: AssertionErrorConstructor): asserts value is Type;
|
|
41
|
+
/**
|
|
42
|
+
* Use in the default case of a switch that you want to be fully exhaustive.
|
|
43
|
+
* Using this function forces the compiler to enforce exhaustivity during
|
|
44
|
+
* compile-time.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```
|
|
48
|
+
* const number = 1;
|
|
49
|
+
* switch (number) {
|
|
50
|
+
* case 0:
|
|
51
|
+
* ...
|
|
52
|
+
* case 1:
|
|
53
|
+
* ...
|
|
54
|
+
* default:
|
|
55
|
+
* assertExhaustive(snapPrefix);
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
* @param _object - The object on which the switch is being operated.
|
|
59
|
+
*/
|
|
60
|
+
export declare function assertExhaustive(_object: never): never;
|
|
15
61
|
//# sourceMappingURL=assert.d.ts.map
|
package/dist/assert.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAKpD,MAAM,MAAM,yBAAyB,GACjC,CAAC,KAAK,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAK,KAAK,CAAC,GAC1C,CAAC,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAK,KAAK,CAAC,CAAC;AAoD3C;;GAEG;AACH,qBAAa,cAAe,SAAQ,KAAK;IACvC,QAAQ,CAAC,IAAI,mBAAmB;IAEhC,YAAY,OAAO,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,EAEvC;CACF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CACpB,KAAK,EAAE,GAAG,EACV,OAAO,GAAE,MAAM,GAAG,KAA2B,EAC7C,YAAY,GAAE,yBAA0C,GACvD,OAAO,CAAC,KAAK,CAQf;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EACvC,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,EAC5B,WAAW,SAAqB,EAChC,YAAY,GAAE,yBAA0C,GACvD,OAAO,CAAC,KAAK,IAAI,IAAI,CASvB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,KAAK,GAAG,KAAK,CAItD"}
|
package/dist/assert.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
import { getErrorMessage } from '@metamask/error-utils';
|
|
2
1
|
import { assert as assertSuperstruct } from '@metamask/superstruct';
|
|
3
|
-
import {
|
|
2
|
+
import { getErrorMessage } from './errors.js';
|
|
3
|
+
/**
|
|
4
|
+
* Check if a value is a constructor, i.e., a function that can be called with
|
|
5
|
+
* the `new` keyword.
|
|
6
|
+
*
|
|
7
|
+
* @param fn - The value to check.
|
|
8
|
+
* @returns `true` if the value is a constructor, or `false` otherwise.
|
|
9
|
+
*/
|
|
10
|
+
function isConstructable(fn) {
|
|
11
|
+
/* istanbul ignore next */
|
|
12
|
+
return Boolean(typeof fn?.prototype?.constructor?.name === 'string');
|
|
13
|
+
}
|
|
4
14
|
/**
|
|
5
15
|
* Attempts to obtain the message from a possible error object. If it is
|
|
6
16
|
* possible to do so, any trailing period will be removed from the message;
|
|
@@ -15,6 +25,52 @@ function getErrorMessageWithoutTrailingPeriod(error) {
|
|
|
15
25
|
// We'll add our own period.
|
|
16
26
|
return getErrorMessage(error).replace(/\.$/u, '');
|
|
17
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Initialise an {@link AssertionErrorConstructor} error.
|
|
30
|
+
*
|
|
31
|
+
* @param ErrorWrapper - The error class to use.
|
|
32
|
+
* @param message - The error message.
|
|
33
|
+
* @returns The error object.
|
|
34
|
+
*/
|
|
35
|
+
function getError(ErrorWrapper, message) {
|
|
36
|
+
if (isConstructable(ErrorWrapper)) {
|
|
37
|
+
return new ErrorWrapper({
|
|
38
|
+
message,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return ErrorWrapper({
|
|
42
|
+
message,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The default error class that is thrown if an assertion fails.
|
|
47
|
+
*/
|
|
48
|
+
export class AssertionError extends Error {
|
|
49
|
+
code = 'ERR_ASSERTION';
|
|
50
|
+
constructor(options) {
|
|
51
|
+
super(options.message);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Same as Node.js assert.
|
|
56
|
+
* If the value is falsy, throws an error, does nothing otherwise.
|
|
57
|
+
*
|
|
58
|
+
* @throws {@link AssertionError} If value is falsy.
|
|
59
|
+
* @param value - The test that should be truthy to pass.
|
|
60
|
+
* @param message - Message to be passed to {@link AssertionError} or an
|
|
61
|
+
* {@link Error} instance to throw.
|
|
62
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
63
|
+
* Defaults to {@link AssertionError}. If a custom error class is provided for
|
|
64
|
+
* the `message` argument, this argument is ignored.
|
|
65
|
+
*/
|
|
66
|
+
export function assert(value, message = 'Assertion failed.', ErrorWrapper = AssertionError) {
|
|
67
|
+
if (!value) {
|
|
68
|
+
if (message instanceof Error) {
|
|
69
|
+
throw message;
|
|
70
|
+
}
|
|
71
|
+
throw getError(ErrorWrapper, message);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
18
74
|
/**
|
|
19
75
|
* Assert a value against a Superstruct struct.
|
|
20
76
|
*
|
|
@@ -31,7 +87,29 @@ export function assertStruct(value, struct, errorPrefix = 'Assertion failed', Er
|
|
|
31
87
|
assertSuperstruct(value, struct);
|
|
32
88
|
}
|
|
33
89
|
catch (error) {
|
|
34
|
-
throw
|
|
90
|
+
throw getError(ErrorWrapper, `${errorPrefix}: ${getErrorMessageWithoutTrailingPeriod(error)}.`);
|
|
35
91
|
}
|
|
36
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Use in the default case of a switch that you want to be fully exhaustive.
|
|
95
|
+
* Using this function forces the compiler to enforce exhaustivity during
|
|
96
|
+
* compile-time.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```
|
|
100
|
+
* const number = 1;
|
|
101
|
+
* switch (number) {
|
|
102
|
+
* case 0:
|
|
103
|
+
* ...
|
|
104
|
+
* case 1:
|
|
105
|
+
* ...
|
|
106
|
+
* default:
|
|
107
|
+
* assertExhaustive(snapPrefix);
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
* @param _object - The object on which the switch is being operated.
|
|
111
|
+
*/
|
|
112
|
+
export function assertExhaustive(_object) {
|
|
113
|
+
throw new Error('Invalid branch reached. Should be detected during compilation.');
|
|
114
|
+
}
|
|
37
115
|
//# sourceMappingURL=assert.js.map
|
package/dist/assert.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assert.js","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"assert.js","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,IAAI,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAEpE,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAM9C;;;;;;GAMG;AACH,SAAS,eAAe,CACtB,EAA6B;IAE7B,0BAA0B;IAC1B,OAAO,OAAO,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,WAAW,EAAE,IAAI,KAAK,QAAQ,CAAC,CAAC;AACvE,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,oCAAoC,CAAC,KAAc;IAC1D,4BAA4B;IAC5B,OAAO,eAAe,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;GAMG;AACH,SAAS,QAAQ,CACf,YAAuC,EACvC,OAAe;IAEf,IAAI,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;QAClC,OAAO,IAAI,YAAY,CAAC;YACtB,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IACD,OAAO,YAAY,CAAC;QAClB,OAAO;KACR,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,cAAe,SAAQ,KAAK;IAC9B,IAAI,GAAG,eAAe,CAAC;IAEhC,YAAY,OAA4B;QACtC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACzB,CAAC;CACF;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,MAAM,CACpB,KAAU,EACV,OAAO,GAAmB,mBAAmB,EAC7C,YAAY,GAA8B,cAAc;IAExD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,IAAI,OAAO,YAAY,KAAK,EAAE,CAAC;YAC7B,MAAM,OAAO,CAAC;QAChB,CAAC;QAED,MAAM,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IACxC,CAAC;AACH,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,YAAY,CAC1B,KAAc,EACd,MAA4B,EAC5B,WAAW,GAAG,kBAAkB,EAChC,YAAY,GAA8B,cAAc;IAExD,IAAI,CAAC;QACH,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IACnC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,QAAQ,CACZ,YAAY,EACZ,GAAG,WAAW,KAAK,oCAAoC,CAAC,KAAK,CAAC,GAAG,CAClE,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAc;IAC7C,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;AACJ,CAAC","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { assert as assertSuperstruct } from '@metamask/superstruct';\n\nimport { getErrorMessage } from './errors.js';\n\nexport type AssertionErrorConstructor =\n | (new (args: { message: string }) => Error)\n | ((args: { message: string }) => Error);\n\n/**\n * Check if a value is a constructor, i.e., a function that can be called with\n * the `new` keyword.\n *\n * @param fn - The value to check.\n * @returns `true` if the value is a constructor, or `false` otherwise.\n */\nfunction isConstructable(\n fn: AssertionErrorConstructor,\n): fn is new (args: { message: string }) => Error {\n /* istanbul ignore next */\n return Boolean(typeof fn?.prototype?.constructor?.name === 'string');\n}\n\n/**\n * Attempts to obtain the message from a possible error object. If it is\n * possible to do so, any trailing period will be removed from the message;\n * otherwise an empty string is returned.\n *\n * @param error - The error object to get the message from.\n * @returns The message without any trailing period if `error` is an object\n * with a `message` property; the string version of `error` without any trailing\n * period if it is not `undefined` or `null`; otherwise an empty string.\n */\nfunction getErrorMessageWithoutTrailingPeriod(error: unknown): string {\n // We'll add our own period.\n return getErrorMessage(error).replace(/\\.$/u, '');\n}\n\n/**\n * Initialise an {@link AssertionErrorConstructor} error.\n *\n * @param ErrorWrapper - The error class to use.\n * @param message - The error message.\n * @returns The error object.\n */\nfunction getError(\n ErrorWrapper: AssertionErrorConstructor,\n message: string,\n): Error {\n if (isConstructable(ErrorWrapper)) {\n return new ErrorWrapper({\n message,\n });\n }\n return ErrorWrapper({\n message,\n });\n}\n\n/**\n * The default error class that is thrown if an assertion fails.\n */\nexport 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 * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}. If a custom error class is provided for\n * the `message` argument, this argument is ignored.\n */\nexport function assert(\n value: any,\n message: string | Error = 'Assertion failed.',\n ErrorWrapper: AssertionErrorConstructor = AssertionError,\n): asserts value {\n if (!value) {\n if (message instanceof Error) {\n throw message;\n }\n\n throw getError(ErrorWrapper, message);\n }\n}\n\n/**\n * Assert a value against a Superstruct struct.\n *\n * @param value - The value to validate.\n * @param struct - The struct to validate against.\n * @param errorPrefix - A prefix to add to the error message. Defaults to\n * \"Assertion failed\".\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the value is not valid.\n */\nexport function assertStruct<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n errorPrefix = 'Assertion failed',\n ErrorWrapper: AssertionErrorConstructor = AssertionError,\n): asserts value is Type {\n try {\n assertSuperstruct(value, struct);\n } catch (error) {\n throw getError(\n ErrorWrapper,\n `${errorPrefix}: ${getErrorMessageWithoutTrailingPeriod(error)}.`,\n );\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"]}
|
package/dist/base64.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAKpD,MAAM,MAAM,aAAa,GAAG;IAC1B;;;;OAIG;IAEH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;;OAKG;IACH,YAAY,CAAC,EAAE,QAAQ,GAAG,WAAW,CAAC;CACvC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,MAAM,GAAI,IAAI,SAAS,MAAM,EAAE,MAAM,UACxC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,YACnB,aAAa,KACrB,MAAM,CAAC,IAAI,EAAE,MAAM,CA0BrB,CAAC"}
|
package/dist/base64.js
CHANGED
package/dist/base64.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base64.js","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"base64.js","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAEhD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAmBrC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CACpB,MAA4B,EAC5B,OAAO,GAAkB,EAAE,EACL,EAAE;IACxB,MAAM,eAAe,GAAG,OAAO,CAAC,eAAe,IAAI,KAAK,CAAC;IACzD,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,QAAQ,CAAC;IAEtD,IAAI,OAAe,CAAC;IACpB,IAAI,YAAY,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,GAAG,MAAM,CAAC,GAAG,CAAA,gBAAgB,CAAC;IACvC,CAAC;SAAM,CAAC;QACN,MAAM,CAAC,YAAY,KAAK,WAAW,CAAC,CAAC;QACrC,OAAO,GAAG,MAAM,CAAC,GAAG,CAAA,eAAe,CAAC;IACtC,CAAC;IAED,IAAI,EAAU,CAAC;IACf,IAAI,eAAe,EAAE,CAAC;QACpB,EAAE,GAAG,IAAI,MAAM,CACb,OAAO,OAAO,WAAW,OAAO,QAAQ,OAAO,UAAU,EACzD,GAAG,CACJ,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,EAAE,GAAG,IAAI,MAAM,CACb,OAAO,OAAO,WAAW,OAAO,SAAS,OAAO,QAAQ,OAAO,UAAU,EACzE,GAAG,CACJ,CAAC;IACJ,CAAC;IAED,OAAO,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AAC7B,CAAC,CAAC","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { pattern } from '@metamask/superstruct';\n\nimport { assert } from './assert.js';\n\nexport type Base64Options = {\n /**\n * Is the `=` padding at the end required or not.\n *\n * @default false\n */\n // Padding is optional in RFC 4648, that's why the default value is false\n paddingRequired?: boolean;\n /**\n * Which character set should be used.\n * The sets are based on {@link https://datatracker.ietf.org/doc/html/rfc4648 RFC 4648}.\n *\n * @default 'base64'\n */\n characterSet?: 'base64' | 'base64url';\n};\n\n/**\n * Ensure that a provided string-based struct is valid base64.\n *\n * @param struct - The string based struct.\n * @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.\n * @returns A superstruct validating base64.\n */\nexport const base64 = <Type extends string, Schema>(\n struct: Struct<Type, Schema>,\n options: Base64Options = {},\n): Struct<Type, Schema> => {\n const paddingRequired = options.paddingRequired ?? false;\n const characterSet = options.characterSet ?? 'base64';\n\n let letters: string;\n if (characterSet === 'base64') {\n letters = String.raw`[A-Za-z0-9+\\/]`;\n } else {\n assert(characterSet === 'base64url');\n letters = String.raw`[-_A-Za-z0-9]`;\n }\n\n let re: RegExp;\n if (paddingRequired) {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n } else {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n }\n\n return pattern(struct, re);\n};\n"]}
|
package/dist/bytes.d.ts
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import type { Hex } from './hex.js';
|
|
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<ArrayBuffer>;
|
|
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<ArrayBuffer>;
|
|
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<ArrayBuffer>;
|
|
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<ArrayBuffer>;
|
|
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<ArrayBuffer>;
|
|
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<ArrayBuffer>;
|
|
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<ArrayBuffer>;
|
|
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<ArrayBuffer>;
|
|
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<ArrayBufferImplementation extends ArrayBufferLike>(bytes: Uint8Array<ArrayBufferImplementation>): DataView<ArrayBufferImplementation>;
|
|
183
|
+
/**
|
|
184
|
+
* Compare two Uint8Arrays using a constant-time style loop to reduce timing
|
|
185
|
+
* side-channels when comparing sensitive data (e.g., mnemonic bytes, keys,
|
|
186
|
+
* authentication tags). Does not early-return on the first difference:
|
|
187
|
+
* work done depends only on the input lengths, so byte content does not affect timing.
|
|
188
|
+
*
|
|
189
|
+
* When to use:
|
|
190
|
+
* - Use for secret or security-sensitive byte comparisons to avoid content-based timing leaks.
|
|
191
|
+
* - Prefer when inputs are fixed-length (or validated to equal length) at the API boundary.
|
|
192
|
+
*
|
|
193
|
+
* @param a - The first Uint8Array to compare.
|
|
194
|
+
* @param b - The second Uint8Array to compare.
|
|
195
|
+
* @returns Whether the Uint8Arrays are equal.
|
|
196
|
+
*/
|
|
197
|
+
export declare function areUint8ArraysEqual(a: Uint8Array, b: Uint8Array): boolean;
|
|
198
|
+
//# sourceMappingURL=bytes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAUpC,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;AAYD;;;;;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,UAAU,GAAG,MAAM,CAIvD;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAAC,WAAW,CAAC,CAoCjE;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAAC,WAAW,CAAC,CAMpE;AAkBD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,MAAM,GACjB,UAAU,CAAC,WAAW,CAAC,CAqBzB;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAAC,WAAW,CAAC,CAUpE;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAAC,WAAW,CAAC,CAIpE;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAAC,WAAW,CAAC,CAIpE;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,UAAU,CAAC,WAAW,CAAC,CA0BlE;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,UAAU,CAAC,WAAW,CAAC,CAsBpE;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,cAAc,CAC5B,yBAAyB,SAAS,eAAe,EAEjD,KAAK,EAAE,UAAU,CAAC,yBAAyB,CAAC,GAC3C,QAAQ,CAAC,yBAAyB,CAAC,CAmBrC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CAazE"}
|