@metamask/utils 11.8.1 → 11.10.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 CHANGED
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [11.10.0]
11
+
12
+ ### Added
13
+
14
+ - Add ensureError utility for converting unknown values to error ([#275](https://github.com/MetaMask/utils/pull/275))
15
+
16
+ ## [11.9.0]
17
+
18
+ ### Added
19
+
20
+ - Add `sha256` utility function ([#273](https://github.com/MetaMask/utils/pull/273))
21
+
10
22
  ## [11.8.1]
11
23
 
12
24
  ### Changed
@@ -474,7 +486,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
474
486
 
475
487
  - Initial release
476
488
 
477
- [Unreleased]: https://github.com/MetaMask/utils/compare/v11.8.1...HEAD
489
+ [Unreleased]: https://github.com/MetaMask/utils/compare/v11.10.0...HEAD
490
+ [11.10.0]: https://github.com/MetaMask/utils/compare/v11.9.0...v11.10.0
491
+ [11.9.0]: https://github.com/MetaMask/utils/compare/v11.8.1...v11.9.0
478
492
  [11.8.1]: https://github.com/MetaMask/utils/compare/v11.8.0...v11.8.1
479
493
  [11.8.0]: https://github.com/MetaMask/utils/compare/v11.7.0...v11.8.0
480
494
  [11.7.0]: https://github.com/MetaMask/utils/compare/v11.6.0...v11.7.0
package/dist/errors.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.wrapError = exports.getErrorMessage = exports.isErrorWithStack = exports.isErrorWithMessage = exports.isErrorWithCode = void 0;
3
+ exports.ensureError = exports.wrapError = exports.getErrorMessage = exports.isErrorWithStack = exports.isErrorWithMessage = exports.isErrorWithCode = void 0;
4
4
  const pony_cause_1 = require("pony-cause");
5
5
  const misc_1 = require("./misc.cjs");
6
6
  /**
@@ -108,4 +108,22 @@ function wrapError(originalError, message) {
108
108
  return new Error(String(originalError));
109
109
  }
110
110
  exports.wrapError = wrapError;
111
+ /**
112
+ * Ensures we have a proper Error object.
113
+ * If the input is already an Error, returns it unchanged.
114
+ * Otherwise, converts to an Error with an appropriate message and preserves
115
+ * the original value as the cause.
116
+ *
117
+ * @param error - The caught error (could be Error, string, or unknown).
118
+ * @returns A proper Error instance.
119
+ */
120
+ function ensureError(error) {
121
+ if (isError(error)) {
122
+ return error;
123
+ }
124
+ const newError = new Error('Unknown error');
125
+ newError.cause = error;
126
+ return newError;
127
+ }
128
+ exports.ensureError = ensureError;
111
129
  //# sourceMappingURL=errors.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.cjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAA,2CAA4C;AAE5C,qCAAqD;AAErD;;;;;;;GAOG;AACH,SAAS,OAAO,CAAC,KAAc;IAC7B,OAAO,CACL,KAAK,YAAY,KAAK;QACtB,CAAC,IAAA,eAAQ,EAAC,KAAK,CAAC,IAAI,KAAK,CAAC,WAAW,CAAC,IAAI,KAAK,OAAO,CAAC,CACxD,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,eAAe,CAAC,KAAc;IAC5C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,MAAM,IAAI,KAAK,CAAC;AACxE,CAAC;AAFD,0CAEC;AAED;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAChC,KAAc;IAEd,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,IAAI,KAAK,CAAC;AAC3E,CAAC;AAJD,gDAIC;AAED;;;;;;GAMG;AACH,SAAgB,gBAAgB,CAAC,KAAc;IAC7C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,CAAC;AACzE,CAAC;AAFD,4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, isObject } from './misc';\n\n/**\n * Type guard for determining whether the given value is an instance of Error.\n * For errors generated via `fs.promises`, `error instanceof Error` won't work,\n * so we have to come up with another way of testing.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nfunction isError(error: unknown): error is Error {\n return (\n error instanceof Error ||\n (isObject(error) && error.constructor.name === 'Error')\n );\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `code` property such as the type of error that Node throws for filesystem\n * operations, etc.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithCode(error: unknown): error is { code: string } {\n return typeof error === 'object' && error !== null && 'code' in error;\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `message` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithMessage(\n error: unknown,\n): error is { message: string } {\n return typeof error === 'object' && error !== null && 'message' in error;\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `stack` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithStack(error: unknown): error is { stack: string } {\n return typeof error === 'object' && error !== null && 'stack' in error;\n}\n\n/**\n * Attempts to obtain the message from a possible error object, defaulting to an\n * empty string if it is impossible to do so.\n *\n * @param error - The possible error to get the message from.\n * @returns The message if `error` is an object with a `message` property;\n * the string version of `error` if it is not `undefined` or `null`; otherwise\n * an empty string.\n */\nexport function getErrorMessage(error: unknown): string {\n if (isErrorWithMessage(error) && typeof error.message === 'string') {\n return error.message;\n }\n\n if (isNullOrUndefined(error)) {\n return '';\n }\n\n return String(error);\n}\n\n/**\n * Builds a new error object, linking it to the original error via the `cause`\n * property if it is an Error.\n *\n * This function is useful to reframe error messages in general, but is\n * _critical_ when interacting with any of Node's filesystem functions as\n * provided via `fs.promises`, because these do not produce stack traces in the\n * case of an I/O error (see <https://github.com/nodejs/node/issues/30944>).\n *\n * @param originalError - The error to be wrapped (something throwable).\n * @param message - The desired message of the new error.\n * @returns A new error object.\n */\nexport function wrapError<Throwable>(\n originalError: Throwable,\n message: string,\n): Error & { code?: string } {\n if (isError(originalError)) {\n let error: Error & { code?: string };\n if (Error.length === 2) {\n // for some reason `tsserver` is not complaining that the\n // Error constructor doesn't support a second argument in the editor,\n // but `tsc` does. Error causes are not supported by our current tsc target (ES2020, we need ES2022 to make this work)\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n error = new Error(message, { cause: originalError });\n } else {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n error = new ErrorWithCause(message, { cause: originalError });\n }\n\n if (isErrorWithCode(originalError)) {\n error.code = originalError.code;\n }\n\n return error;\n }\n\n if (message.length > 0) {\n return new Error(`${String(originalError)}: ${message}`);\n }\n\n return new Error(String(originalError));\n}\n"]}
1
+ {"version":3,"file":"errors.cjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAA,2CAA4C;AAE5C,qCAAqD;AAErD;;;;;;;GAOG;AACH,SAAS,OAAO,CAAC,KAAc;IAC7B,OAAO,CACL,KAAK,YAAY,KAAK;QACtB,CAAC,IAAA,eAAQ,EAAC,KAAK,CAAC,IAAI,KAAK,CAAC,WAAW,CAAC,IAAI,KAAK,OAAO,CAAC,CACxD,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,eAAe,CAAC,KAAc;IAC5C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,MAAM,IAAI,KAAK,CAAC;AACxE,CAAC;AAFD,0CAEC;AAED;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAChC,KAAc;IAEd,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,IAAI,KAAK,CAAC;AAC3E,CAAC;AAJD,gDAIC;AAED;;;;;;GAMG;AACH,SAAgB,gBAAgB,CAAC,KAAc;IAC7C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KA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A+BC;AAED;;;;;;;;GAQG;AACH,SAAgB,WAAW,CAAC,KAAc;IACxC,IAAI,OAAO,CAAC,KAAK,CAAC,EAAE;QAClB,OAAO,KAAK,CAAC;KACd;IAED,MAAM,QAAQ,GAAgC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC;IACzE,QAAQ,CAAC,KAAK,GAAG,KAAK,CAAC;IACvB,OAAO,QAAQ,CAAC;AAClB,CAAC;AARD,kCAQC","sourcesContent":["import { ErrorWithCause } from 'pony-cause';\n\nimport { isNullOrUndefined, isObject } from './misc';\n\n/**\n * Type guard for determining whether the given value is an instance of Error.\n * For errors generated via `fs.promises`, `error instanceof Error` won't work,\n * so we have to come up with another way of testing.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nfunction isError(error: unknown): error is Error {\n return (\n error instanceof Error ||\n (isObject(error) && error.constructor.name === 'Error')\n );\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `code` property such as the type of error that Node throws for filesystem\n * operations, etc.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithCode(error: unknown): error is { code: string } {\n return typeof error === 'object' && error !== null && 'code' in error;\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `message` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithMessage(\n error: unknown,\n): error is { message: string } {\n return typeof error === 'object' && error !== null && 'message' in error;\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `stack` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithStack(error: unknown): error is { stack: string } {\n return typeof error === 'object' && error !== null && 'stack' in error;\n}\n\n/**\n * Attempts to obtain the message from a possible error object, defaulting to an\n * empty string if it is impossible to do so.\n *\n * @param error - The possible error to get the message from.\n * @returns The message if `error` is an object with a `message` property;\n * the string version of `error` if it is not `undefined` or `null`; otherwise\n * an empty string.\n */\nexport function getErrorMessage(error: unknown): string {\n if (isErrorWithMessage(error) && typeof error.message === 'string') {\n return error.message;\n }\n\n if (isNullOrUndefined(error)) {\n return '';\n }\n\n return String(error);\n}\n\n/**\n * Builds a new error object, linking it to the original error via the `cause`\n * property if it is an Error.\n *\n * This function is useful to reframe error messages in general, but is\n * _critical_ when interacting with any of Node's filesystem functions as\n * provided via `fs.promises`, because these do not produce stack traces in the\n * case of an I/O error (see <https://github.com/nodejs/node/issues/30944>).\n *\n * @param originalError - The error to be wrapped (something throwable).\n * @param message - The desired message of the new error.\n * @returns A new error object.\n */\nexport function wrapError<Throwable>(\n originalError: Throwable,\n message: string,\n): Error & { code?: string } {\n if (isError(originalError)) {\n let error: Error & { code?: string };\n if (Error.length === 2) {\n // for some reason `tsserver` is not complaining that the\n // Error constructor doesn't support a second argument in the editor,\n // but `tsc` does. Error causes are not supported by our current tsc target (ES2020, we need ES2022 to make this work)\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n error = new Error(message, { cause: originalError });\n } else {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n error = new ErrorWithCause(message, { cause: originalError });\n }\n\n if (isErrorWithCode(originalError)) {\n error.code = originalError.code;\n }\n\n return error;\n }\n\n if (message.length > 0) {\n return new Error(`${String(originalError)}: ${message}`);\n }\n\n return new Error(String(originalError));\n}\n\n/**\n * Ensures we have a proper Error object.\n * If the input is already an Error, returns it unchanged.\n * Otherwise, converts to an Error with an appropriate message and preserves\n * the original value as the cause.\n *\n * @param error - The caught error (could be Error, string, or unknown).\n * @returns A proper Error instance.\n */\nexport function ensureError(error: unknown): Error {\n if (isError(error)) {\n return error;\n }\n\n const newError: Error & { cause?: unknown } = new Error('Unknown error');\n newError.cause = error;\n return newError;\n}\n"]}
package/dist/errors.d.cts CHANGED
@@ -55,4 +55,14 @@ export declare function getErrorMessage(error: unknown): string;
55
55
  export declare function wrapError<Throwable>(originalError: Throwable, message: string): Error & {
56
56
  code?: string;
57
57
  };
58
+ /**
59
+ * Ensures we have a proper Error object.
60
+ * If the input is already an Error, returns it unchanged.
61
+ * Otherwise, converts to an Error with an appropriate message and preserves
62
+ * the original value as the cause.
63
+ *
64
+ * @param error - The caught error (could be Error, string, or unknown).
65
+ * @returns A proper Error instance.
66
+ */
67
+ export declare function ensureError(error: unknown): Error;
58
68
  //# sourceMappingURL=errors.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.cts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAmBA;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAEzE;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAE9B;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAE3E;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAUtD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,SAAS,EACjC,aAAa,EAAE,SAAS,EACxB,OAAO,EAAE,MAAM,GACd,KAAK,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CA4B3B"}
1
+ {"version":3,"file":"errors.d.cts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAmBA;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAEzE;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAE9B;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAE3E;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAUtD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,SAAS,EACjC,aAAa,EAAE,SAAS,EACxB,OAAO,EAAE,MAAM,GACd,KAAK,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CA4B3B;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,CAQjD"}
package/dist/errors.d.mts CHANGED
@@ -55,4 +55,14 @@ export declare function getErrorMessage(error: unknown): string;
55
55
  export declare function wrapError<Throwable>(originalError: Throwable, message: string): Error & {
56
56
  code?: string;
57
57
  };
58
+ /**
59
+ * Ensures we have a proper Error object.
60
+ * If the input is already an Error, returns it unchanged.
61
+ * Otherwise, converts to an Error with an appropriate message and preserves
62
+ * the original value as the cause.
63
+ *
64
+ * @param error - The caught error (could be Error, string, or unknown).
65
+ * @returns A proper Error instance.
66
+ */
67
+ export declare function ensureError(error: unknown): Error;
58
68
  //# sourceMappingURL=errors.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.mts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAmBA;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAEzE;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAE9B;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAE3E;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAUtD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,SAAS,EACjC,aAAa,EAAE,SAAS,EACxB,OAAO,EAAE,MAAM,GACd,KAAK,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CA4B3B"}
1
+ {"version":3,"file":"errors.d.mts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAmBA;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAEzE;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAE9B;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAE3E;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAUtD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,SAAS,EACjC,aAAa,EAAE,SAAS,EACxB,OAAO,EAAE,MAAM,GACd,KAAK,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CA4B3B;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,CAQjD"}
package/dist/errors.mjs CHANGED
@@ -100,4 +100,21 @@ export function wrapError(originalError, message) {
100
100
  }
101
101
  return new Error(String(originalError));
102
102
  }
103
+ /**
104
+ * Ensures we have a proper Error object.
105
+ * If the input is already an Error, returns it unchanged.
106
+ * Otherwise, converts to an Error with an appropriate message and preserves
107
+ * the original value as the cause.
108
+ *
109
+ * @param error - The caught error (could be Error, string, or unknown).
110
+ * @returns A proper Error instance.
111
+ */
112
+ export function ensureError(error) {
113
+ if (isError(error)) {
114
+ return error;
115
+ }
116
+ const newError = new Error('Unknown error');
117
+ newError.cause = error;
118
+ return newError;
119
+ }
103
120
  //# sourceMappingURL=errors.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.mjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,mBAAmB;AAE5C,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,mBAAe;AAErD;;;;;;;GAOG;AACH,SAAS,OAAO,CAAC,KAAc;IAC7B,OAAO,CACL,KAAK,YAAY,KAAK;QACtB,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,WAAW,CAAC,IAAI,KAAK,OAAO,CAAC,CACxD,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC5C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,MAAM,IAAI,KAAK,CAAC;AACxE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAChC,KAAc;IAEd,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,IAAI,KAAK,CAAC;AAC3E,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAc;IAC7C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,CAAC;AACzE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC5C,IAAI,kBAAkB,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,EAAE;QAClE,OAAO,KAAK,CAAC,OAAO,CAAC;KACtB;IAED,IAAI,iBAAiB,CAAC,KAAK,CAAC,EAAE;QAC5B,OAAO,EAAE,CAAC;KACX;IAED,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,SAAS,CACvB,aAAwB,EACxB,OAAe;IAEf,IAAI,OAAO,CAAC,aAAa,CAAC,EAAE;QAC1B,IAAI,KAAgC,CAAC;QACrC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE;YACtB,yDAAyD;YACzD,qEAAqE;YACrE,sHAAsH;YACtH,6DAA6D;YAC7D,aAAa;YACb,KAAK,GAAG,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,aAAa,EAAE,CAAC,CAAC;SACtD;aAAM;YACL,6DAA6D;YAC7D,aAAa;YACb,KAAK,GAAG,IAAI,cAAc,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,aAAa,EAAE,CAAC,CAAC;SAC/D;QAED,IAAI,eAAe,CAAC,aAAa,CAAC,EAAE;YAClC,KAAK,CAAC,IAAI,GAAG,aAAa,CAAC,IAAI,CAAC;SACjC;QAED,OAAO,KAAK,CAAC;KACd;IAED,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE;QACtB,OAAO,IAAI,KAAK,CAAC,GAAG,MAAM,CAAC,aAAa,CAAC,KAAK,OAAO,EAAE,CAAC,CAAC;KAC1D;IAED,OAAO,IAAI,KAAK,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["import { ErrorWithCause } from 'pony-cause';\n\nimport { isNullOrUndefined, isObject } from './misc';\n\n/**\n * Type guard for determining whether the given value is an instance of Error.\n * For errors generated via `fs.promises`, `error instanceof Error` won't work,\n * so we have to come up with another way of testing.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nfunction isError(error: unknown): error is Error {\n return (\n error instanceof Error ||\n (isObject(error) && error.constructor.name === 'Error')\n );\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `code` property such as the type of error that Node throws for filesystem\n * operations, etc.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithCode(error: unknown): error is { code: string } {\n return typeof error === 'object' && error !== null && 'code' in error;\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `message` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithMessage(\n error: unknown,\n): error is { message: string } {\n return typeof error === 'object' && error !== null && 'message' in error;\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `stack` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithStack(error: unknown): error is { stack: string } {\n return typeof error === 'object' && error !== null && 'stack' in error;\n}\n\n/**\n * Attempts to obtain the message from a possible error object, defaulting to an\n * empty string if it is impossible to do so.\n *\n * @param error - The possible error to get the message from.\n * @returns The message if `error` is an object with a `message` property;\n * the string version of `error` if it is not `undefined` or `null`; otherwise\n * an empty string.\n */\nexport function getErrorMessage(error: unknown): string {\n if (isErrorWithMessage(error) && typeof error.message === 'string') {\n return error.message;\n }\n\n if (isNullOrUndefined(error)) {\n return '';\n }\n\n return String(error);\n}\n\n/**\n * Builds a new error object, linking it to the original error via the `cause`\n * property if it is an Error.\n *\n * This function is useful to reframe error messages in general, but is\n * _critical_ when interacting with any of Node's filesystem functions as\n * provided via `fs.promises`, because these do not produce stack traces in the\n * case of an I/O error (see <https://github.com/nodejs/node/issues/30944>).\n *\n * @param originalError - The error to be wrapped (something throwable).\n * @param message - The desired message of the new error.\n * @returns A new error object.\n */\nexport function wrapError<Throwable>(\n originalError: Throwable,\n message: string,\n): Error & { code?: string } {\n if (isError(originalError)) {\n let error: Error & { code?: string };\n if (Error.length === 2) {\n // for some reason `tsserver` is not complaining that the\n // Error constructor doesn't support a second argument in the editor,\n // but `tsc` does. Error causes are not supported by our current tsc target (ES2020, we need ES2022 to make this work)\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n error = new Error(message, { cause: originalError });\n } else {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n error = new ErrorWithCause(message, { cause: originalError });\n }\n\n if (isErrorWithCode(originalError)) {\n error.code = originalError.code;\n }\n\n return error;\n }\n\n if (message.length > 0) {\n return new Error(`${String(originalError)}: ${message}`);\n }\n\n return new Error(String(originalError));\n}\n"]}
1
+ {"version":3,"file":"errors.mjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,mBAAmB;AAE5C,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,mBAAe;AAErD;;;;;;;GAOG;AACH,SAAS,OAAO,CAAC,KAAc;IAC7B,OAAO,CACL,KAAK,YAAY,KAAK;QACtB,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,WAAW,CAAC,IAAI,KAAK,OAAO,CAAC,CACxD,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC5C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,MAAM,IAAI,KAAK,CAAC;AACxE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAChC,KAAc;IAEd,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,IAAI,KAAK,CAAC;AAC3E,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAc;IAC7C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,CAAC;AACzE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc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AQ,GAAgC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC;IACzE,QAAQ,CAAC,KAAK,GAAG,KAAK,CAAC;IACvB,OAAO,QAAQ,CAAC;AAClB,CAAC","sourcesContent":["import { ErrorWithCause } from 'pony-cause';\n\nimport { isNullOrUndefined, isObject } from './misc';\n\n/**\n * Type guard for determining whether the given value is an instance of Error.\n * For errors generated via `fs.promises`, `error instanceof Error` won't work,\n * so we have to come up with another way of testing.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nfunction isError(error: unknown): error is Error {\n return (\n error instanceof Error ||\n (isObject(error) && error.constructor.name === 'Error')\n );\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `code` property such as the type of error that Node throws for filesystem\n * operations, etc.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithCode(error: unknown): error is { code: string } {\n return typeof error === 'object' && error !== null && 'code' in error;\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `message` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithMessage(\n error: unknown,\n): error is { message: string } {\n return typeof error === 'object' && error !== null && 'message' in error;\n}\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `stack` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns A boolean.\n */\nexport function isErrorWithStack(error: unknown): error is { stack: string } {\n return typeof error === 'object' && error !== null && 'stack' in error;\n}\n\n/**\n * Attempts to obtain the message from a possible error object, defaulting to an\n * empty string if it is impossible to do so.\n *\n * @param error - The possible error to get the message from.\n * @returns The message if `error` is an object with a `message` property;\n * the string version of `error` if it is not `undefined` or `null`; otherwise\n * an empty string.\n */\nexport function getErrorMessage(error: unknown): string {\n if (isErrorWithMessage(error) && typeof error.message === 'string') {\n return error.message;\n }\n\n if (isNullOrUndefined(error)) {\n return '';\n }\n\n return String(error);\n}\n\n/**\n * Builds a new error object, linking it to the original error via the `cause`\n * property if it is an Error.\n *\n * This function is useful to reframe error messages in general, but is\n * _critical_ when interacting with any of Node's filesystem functions as\n * provided via `fs.promises`, because these do not produce stack traces in the\n * case of an I/O error (see <https://github.com/nodejs/node/issues/30944>).\n *\n * @param originalError - The error to be wrapped (something throwable).\n * @param message - The desired message of the new error.\n * @returns A new error object.\n */\nexport function wrapError<Throwable>(\n originalError: Throwable,\n message: string,\n): Error & { code?: string } {\n if (isError(originalError)) {\n let error: Error & { code?: string };\n if (Error.length === 2) {\n // for some reason `tsserver` is not complaining that the\n // Error constructor doesn't support a second argument in the editor,\n // but `tsc` does. Error causes are not supported by our current tsc target (ES2020, we need ES2022 to make this work)\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n error = new Error(message, { cause: originalError });\n } else {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n error = new ErrorWithCause(message, { cause: originalError });\n }\n\n if (isErrorWithCode(originalError)) {\n error.code = originalError.code;\n }\n\n return error;\n }\n\n if (message.length > 0) {\n return new Error(`${String(originalError)}: ${message}`);\n }\n\n return new Error(String(originalError));\n}\n\n/**\n * Ensures we have a proper Error object.\n * If the input is already an Error, returns it unchanged.\n * Otherwise, converts to an Error with an appropriate message and preserves\n * the original value as the cause.\n *\n * @param error - The caught error (could be Error, string, or unknown).\n * @returns A proper Error instance.\n */\nexport function ensureError(error: unknown): Error {\n if (isError(error)) {\n return error;\n }\n\n const newError: Error & { cause?: unknown } = new Error('Unknown error');\n newError.cause = error;\n return newError;\n}\n"]}
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sha256 = void 0;
4
+ const sha256_1 = require("@noble/hashes/sha256");
5
+ /**
6
+ * Compute a SHA-256 digest for a given byte array.
7
+ *
8
+ * Uses the native crypto implementation and falls back to noble.
9
+ *
10
+ * @param bytes - A byte array.
11
+ * @returns The SHA-256 hash as a byte array.
12
+ */
13
+ async function sha256(bytes) {
14
+ // Use crypto.subtle.digest whenever possible as it is faster.
15
+ if ('crypto' in globalThis &&
16
+ typeof globalThis.crypto === 'object' &&
17
+ // eslint-disable-next-line no-restricted-globals
18
+ globalThis.crypto.subtle?.digest) {
19
+ // eslint-disable-next-line no-restricted-globals
20
+ return new Uint8Array(await globalThis.crypto.subtle.digest('SHA-256', bytes));
21
+ }
22
+ return (0, sha256_1.sha256)(bytes);
23
+ }
24
+ exports.sha256 = sha256;
25
+ //# sourceMappingURL=hashing.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hashing.cjs","sourceRoot":"","sources":["../src/hashing.ts"],"names":[],"mappings":";;;AAAA,iDAA6D;AAE7D;;;;;;;GAOG;AACI,KAAK,UAAU,MAAM,CAAC,KAAiB;IAC5C,8DAA8D;IAC9D,IACE,QAAQ,IAAI,UAAU;QACtB,OAAO,UAAU,CAAC,MAAM,KAAK,QAAQ;QACrC,iDAAiD;QACjD,UAAU,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,EAChC;QACA,iDAAiD;QACjD,OAAO,IAAI,UAAU,CACnB,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,CACxD,CAAC;KACH;IACD,OAAO,IAAA,eAAW,EAAC,KAAK,CAAC,CAAC;AAC5B,CAAC;AAdD,wBAcC","sourcesContent":["import { sha256 as nobleSha256 } from '@noble/hashes/sha256';\n\n/**\n * Compute a SHA-256 digest for a given byte array.\n *\n * Uses the native crypto implementation and falls back to noble.\n *\n * @param bytes - A byte array.\n * @returns The SHA-256 hash as a byte array.\n */\nexport async function sha256(bytes: Uint8Array): Promise<Uint8Array> {\n // Use crypto.subtle.digest whenever possible as it is faster.\n if (\n 'crypto' in globalThis &&\n typeof globalThis.crypto === 'object' &&\n // eslint-disable-next-line no-restricted-globals\n globalThis.crypto.subtle?.digest\n ) {\n // eslint-disable-next-line no-restricted-globals\n return new Uint8Array(\n await globalThis.crypto.subtle.digest('SHA-256', bytes),\n );\n }\n return nobleSha256(bytes);\n}\n"]}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Compute a SHA-256 digest for a given byte array.
3
+ *
4
+ * Uses the native crypto implementation and falls back to noble.
5
+ *
6
+ * @param bytes - A byte array.
7
+ * @returns The SHA-256 hash as a byte array.
8
+ */
9
+ export declare function sha256(bytes: Uint8Array): Promise<Uint8Array>;
10
+ //# sourceMappingURL=hashing.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hashing.d.cts","sourceRoot":"","sources":["../src/hashing.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,wBAAsB,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAcnE"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Compute a SHA-256 digest for a given byte array.
3
+ *
4
+ * Uses the native crypto implementation and falls back to noble.
5
+ *
6
+ * @param bytes - A byte array.
7
+ * @returns The SHA-256 hash as a byte array.
8
+ */
9
+ export declare function sha256(bytes: Uint8Array): Promise<Uint8Array>;
10
+ //# sourceMappingURL=hashing.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hashing.d.mts","sourceRoot":"","sources":["../src/hashing.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,wBAAsB,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAcnE"}
@@ -0,0 +1,21 @@
1
+ import { sha256 as nobleSha256 } from "@noble/hashes/sha256";
2
+ /**
3
+ * Compute a SHA-256 digest for a given byte array.
4
+ *
5
+ * Uses the native crypto implementation and falls back to noble.
6
+ *
7
+ * @param bytes - A byte array.
8
+ * @returns The SHA-256 hash as a byte array.
9
+ */
10
+ export async function sha256(bytes) {
11
+ // Use crypto.subtle.digest whenever possible as it is faster.
12
+ if ('crypto' in globalThis &&
13
+ typeof globalThis.crypto === 'object' &&
14
+ // eslint-disable-next-line no-restricted-globals
15
+ globalThis.crypto.subtle?.digest) {
16
+ // eslint-disable-next-line no-restricted-globals
17
+ return new Uint8Array(await globalThis.crypto.subtle.digest('SHA-256', bytes));
18
+ }
19
+ return nobleSha256(bytes);
20
+ }
21
+ //# sourceMappingURL=hashing.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hashing.mjs","sourceRoot":"","sources":["../src/hashing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,WAAW,EAAE,6BAA6B;AAE7D;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,KAAiB;IAC5C,8DAA8D;IAC9D,IACE,QAAQ,IAAI,UAAU;QACtB,OAAO,UAAU,CAAC,MAAM,KAAK,QAAQ;QACrC,iDAAiD;QACjD,UAAU,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,EAChC;QACA,iDAAiD;QACjD,OAAO,IAAI,UAAU,CACnB,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,CACxD,CAAC;KACH;IACD,OAAO,WAAW,CAAC,KAAK,CAAC,CAAC;AAC5B,CAAC","sourcesContent":["import { sha256 as nobleSha256 } from '@noble/hashes/sha256';\n\n/**\n * Compute a SHA-256 digest for a given byte array.\n *\n * Uses the native crypto implementation and falls back to noble.\n *\n * @param bytes - A byte array.\n * @returns The SHA-256 hash as a byte array.\n */\nexport async function sha256(bytes: Uint8Array): Promise<Uint8Array> {\n // Use crypto.subtle.digest whenever possible as it is faster.\n if (\n 'crypto' in globalThis &&\n typeof globalThis.crypto === 'object' &&\n // eslint-disable-next-line no-restricted-globals\n globalThis.crypto.subtle?.digest\n ) {\n // eslint-disable-next-line no-restricted-globals\n return new Uint8Array(\n await globalThis.crypto.subtle.digest('SHA-256', bytes),\n );\n }\n return nobleSha256(bytes);\n}\n"]}
package/dist/index.cjs CHANGED
@@ -24,6 +24,7 @@ __exportStar(require("./coercers.cjs"), exports);
24
24
  __exportStar(require("./collections.cjs"), exports);
25
25
  __exportStar(require("./encryption-types.cjs"), exports);
26
26
  __exportStar(require("./errors.cjs"), exports);
27
+ __exportStar(require("./hashing.cjs"), exports);
27
28
  var hex_1 = require("./hex.cjs");
28
29
  Object.defineProperty(exports, "HexStruct", { enumerable: true, get: function () { return hex_1.HexStruct; } });
29
30
  Object.defineProperty(exports, "StrictHexStruct", { enumerable: true, get: function () { return hex_1.StrictHexStruct; } });
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,+CAAyB;AACzB,+CAAyB;AACzB,8CAAwB;AACxB,mDAA6B;AAC7B,iDAA2B;AAC3B,iDAA2B;AAC3B,oDAA8B;AAC9B,yDAAmC;AACnC,+CAAyB;AAEzB,iCAgBe;AAfb,gGAAA,SAAS,OAAA;AACT,sGAAA,eAAe,OAAA;AACf,uGAAA,gBAAgB,OAAA;AAChB,+GAAA,wBAAwB,OAAA;AACxB,kGAAA,WAAW,OAAA;AACX,wGAAA,iBAAiB,OAAA;AACjB,mGAAA,YAAY,OAAA;AACZ,2GAAA,oBAAoB,OAAA;AACpB,wGAAA,iBAAiB,OAAA;AACjB,8GAAA,uBAAuB,OAAA;AACvB,wGAAA,iBAAiB,OAAA;AACjB,yGAAA,kBAAkB,OAAA;AAClB,6GAAA,sBAAsB,OAAA;AACtB,4FAAA,KAAK,OAAA;AACL,+FAAA,QAAQ,OAAA;AAEV,6CAAuB;AACvB,gDAA0B;AAC1B,gDAA0B;AAC1B,6CAAuB;AACvB,+CAAyB;AACzB,+CAAyB;AACzB,gDAA0B;AAC1B,oDAA8B;AAC9B,6CAAuB;AACvB,0DAAoC;AACpC,iDAA2B;AAC3B,yDAM2B;AALzB,wGAAA,KAAK,OAAA;AACL,0GAAA,OAAO,OAAA;AACP,iHAAA,cAAc,OAAA;AACd,iHAAA,cAAc,OAAA;AACd,0GAAA,OAAO,OAAA","sourcesContent":["export * from './assert';\nexport * from './base64';\nexport * from './bytes';\nexport * from './caip-types';\nexport * from './checksum';\nexport * from './coercers';\nexport * from './collections';\nexport * from './encryption-types';\nexport * from './errors';\nexport type { Hex } from './hex';\nexport {\n HexStruct,\n StrictHexStruct,\n HexAddressStruct,\n HexChecksumAddressStruct,\n isHexString,\n isStrictHexString,\n isHexAddress,\n isHexChecksumAddress,\n assertIsHexString,\n assertIsStrictHexString,\n isValidHexAddress,\n getChecksumAddress,\n isValidChecksumAddress,\n add0x,\n remove0x,\n} from './hex';\nexport * from './json';\nexport * from './keyring';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './opaque';\nexport * from './promise';\nexport * from './superstruct';\nexport * from './time';\nexport * from './transaction-types';\nexport * from './versions';\nexport {\n toWei,\n fromWei,\n numberToString,\n getValueOfUnit,\n unitMap,\n} from './unitsConversion';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,+CAAyB;AACzB,+CAAyB;AACzB,8CAAwB;AACxB,mDAA6B;AAC7B,iDAA2B;AAC3B,iDAA2B;AAC3B,oDAA8B;AAC9B,yDAAmC;AACnC,+CAAyB;AACzB,gDAA0B;AAE1B,iCAgBe;AAfb,gGAAA,SAAS,OAAA;AACT,sGAAA,eAAe,OAAA;AACf,uGAAA,gBAAgB,OAAA;AAChB,+GAAA,wBAAwB,OAAA;AACxB,kGAAA,WAAW,OAAA;AACX,wGAAA,iBAAiB,OAAA;AACjB,mGAAA,YAAY,OAAA;AACZ,2GAAA,oBAAoB,OAAA;AACpB,wGAAA,iBAAiB,OAAA;AACjB,8GAAA,uBAAuB,OAAA;AACvB,wGAAA,iBAAiB,OAAA;AACjB,yGAAA,kBAAkB,OAAA;AAClB,6GAAA,sBAAsB,OAAA;AACtB,4FAAA,KAAK,OAAA;AACL,+FAAA,QAAQ,OAAA;AAEV,6CAAuB;AACvB,gDAA0B;AAC1B,gDAA0B;AAC1B,6CAAuB;AACvB,+CAAyB;AACzB,+CAAyB;AACzB,gDAA0B;AAC1B,oDAA8B;AAC9B,6CAAuB;AACvB,0DAAoC;AACpC,iDAA2B;AAC3B,yDAM2B;AALzB,wGAAA,KAAK,OAAA;AACL,0GAAA,OAAO,OAAA;AACP,iHAAA,cAAc,OAAA;AACd,iHAAA,cAAc,OAAA;AACd,0GAAA,OAAO,OAAA","sourcesContent":["export * from './assert';\nexport * from './base64';\nexport * from './bytes';\nexport * from './caip-types';\nexport * from './checksum';\nexport * from './coercers';\nexport * from './collections';\nexport * from './encryption-types';\nexport * from './errors';\nexport * from './hashing';\nexport type { Hex } from './hex';\nexport {\n HexStruct,\n StrictHexStruct,\n HexAddressStruct,\n HexChecksumAddressStruct,\n isHexString,\n isStrictHexString,\n isHexAddress,\n isHexChecksumAddress,\n assertIsHexString,\n assertIsStrictHexString,\n isValidHexAddress,\n getChecksumAddress,\n isValidChecksumAddress,\n add0x,\n remove0x,\n} from './hex';\nexport * from './json';\nexport * from './keyring';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './opaque';\nexport * from './promise';\nexport * from './superstruct';\nexport * from './time';\nexport * from './transaction-types';\nexport * from './versions';\nexport {\n toWei,\n fromWei,\n numberToString,\n getValueOfUnit,\n unitMap,\n} from './unitsConversion';\n"]}
package/dist/index.d.cts CHANGED
@@ -7,6 +7,7 @@ export * from "./coercers.cjs";
7
7
  export * from "./collections.cjs";
8
8
  export * from "./encryption-types.cjs";
9
9
  export * from "./errors.cjs";
10
+ export * from "./hashing.cjs";
10
11
  export type { Hex } from "./hex.cjs";
11
12
  export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, isHexAddress, isHexChecksumAddress, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x, } from "./hex.cjs";
12
13
  export * from "./json.cjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AACzB,YAAY,EAAE,GAAG,EAAE,kBAAc;AACjC,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,wBAAwB,EACxB,WAAW,EACX,iBAAiB,EACjB,YAAY,EACZ,oBAAoB,EACpB,iBAAiB,EACjB,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,EACL,QAAQ,GACT,kBAAc;AACf,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,kCAA8B;AAC9B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B;AAC3B,OAAO,EACL,KAAK,EACL,OAAO,EACP,cAAc,EACd,cAAc,EACd,OAAO,GACR,8BAA0B"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AACzB,8BAA0B;AAC1B,YAAY,EAAE,GAAG,EAAE,kBAAc;AACjC,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,wBAAwB,EACxB,WAAW,EACX,iBAAiB,EACjB,YAAY,EACZ,oBAAoB,EACpB,iBAAiB,EACjB,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,EACL,QAAQ,GACT,kBAAc;AACf,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,kCAA8B;AAC9B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B;AAC3B,OAAO,EACL,KAAK,EACL,OAAO,EACP,cAAc,EACd,cAAc,EACd,OAAO,GACR,8BAA0B"}
package/dist/index.d.mts CHANGED
@@ -7,6 +7,7 @@ export * from "./coercers.mjs";
7
7
  export * from "./collections.mjs";
8
8
  export * from "./encryption-types.mjs";
9
9
  export * from "./errors.mjs";
10
+ export * from "./hashing.mjs";
10
11
  export type { Hex } from "./hex.mjs";
11
12
  export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, isHexAddress, isHexChecksumAddress, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x, } from "./hex.mjs";
12
13
  export * from "./json.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AACzB,YAAY,EAAE,GAAG,EAAE,kBAAc;AACjC,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,wBAAwB,EACxB,WAAW,EACX,iBAAiB,EACjB,YAAY,EACZ,oBAAoB,EACpB,iBAAiB,EACjB,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,EACL,QAAQ,GACT,kBAAc;AACf,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,kCAA8B;AAC9B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B;AAC3B,OAAO,EACL,KAAK,EACL,OAAO,EACP,cAAc,EACd,cAAc,EACd,OAAO,GACR,8BAA0B"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AACzB,8BAA0B;AAC1B,YAAY,EAAE,GAAG,EAAE,kBAAc;AACjC,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,wBAAwB,EACxB,WAAW,EACX,iBAAiB,EACjB,YAAY,EACZ,oBAAoB,EACpB,iBAAiB,EACjB,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,EACL,QAAQ,GACT,kBAAc;AACf,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,kCAA8B;AAC9B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B;AAC3B,OAAO,EACL,KAAK,EACL,OAAO,EACP,cAAc,EACd,cAAc,EACd,OAAO,GACR,8BAA0B"}
package/dist/index.mjs CHANGED
@@ -7,6 +7,7 @@ export * from "./coercers.mjs";
7
7
  export * from "./collections.mjs";
8
8
  export * from "./encryption-types.mjs";
9
9
  export * from "./errors.mjs";
10
+ export * from "./hashing.mjs";
10
11
  export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, isHexAddress, isHexChecksumAddress, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x } from "./hex.mjs";
11
12
  export * from "./json.mjs";
12
13
  export * from "./keyring.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AAEzB,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,wBAAwB,EACxB,WAAW,EACX,iBAAiB,EACjB,YAAY,EACZ,oBAAoB,EACpB,iBAAiB,EACjB,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,EACL,QAAQ,EACT,kBAAc;AACf,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,kCAA8B;AAC9B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B;AAC3B,OAAO,EACL,KAAK,EACL,OAAO,EACP,cAAc,EACd,cAAc,EACd,OAAO,EACR,8BAA0B","sourcesContent":["export * from './assert';\nexport * from './base64';\nexport * from './bytes';\nexport * from './caip-types';\nexport * from './checksum';\nexport * from './coercers';\nexport * from './collections';\nexport * from './encryption-types';\nexport * from './errors';\nexport type { Hex } from './hex';\nexport {\n HexStruct,\n StrictHexStruct,\n HexAddressStruct,\n HexChecksumAddressStruct,\n isHexString,\n isStrictHexString,\n isHexAddress,\n isHexChecksumAddress,\n assertIsHexString,\n assertIsStrictHexString,\n isValidHexAddress,\n getChecksumAddress,\n isValidChecksumAddress,\n add0x,\n remove0x,\n} from './hex';\nexport * from './json';\nexport * from './keyring';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './opaque';\nexport * from './promise';\nexport * from './superstruct';\nexport * from './time';\nexport * from './transaction-types';\nexport * from './versions';\nexport {\n toWei,\n fromWei,\n numberToString,\n getValueOfUnit,\n unitMap,\n} from './unitsConversion';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AACzB,8BAA0B;AAE1B,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,wBAAwB,EACxB,WAAW,EACX,iBAAiB,EACjB,YAAY,EACZ,oBAAoB,EACpB,iBAAiB,EACjB,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,EACL,QAAQ,EACT,kBAAc;AACf,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,kCAA8B;AAC9B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B;AAC3B,OAAO,EACL,KAAK,EACL,OAAO,EACP,cAAc,EACd,cAAc,EACd,OAAO,EACR,8BAA0B","sourcesContent":["export * from './assert';\nexport * from './base64';\nexport * from './bytes';\nexport * from './caip-types';\nexport * from './checksum';\nexport * from './coercers';\nexport * from './collections';\nexport * from './encryption-types';\nexport * from './errors';\nexport * from './hashing';\nexport type { Hex } from './hex';\nexport {\n HexStruct,\n StrictHexStruct,\n HexAddressStruct,\n HexChecksumAddressStruct,\n isHexString,\n isStrictHexString,\n isHexAddress,\n isHexChecksumAddress,\n assertIsHexString,\n assertIsStrictHexString,\n isValidHexAddress,\n getChecksumAddress,\n isValidChecksumAddress,\n add0x,\n remove0x,\n} from './hex';\nexport * from './json';\nexport * from './keyring';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './opaque';\nexport * from './promise';\nexport * from './superstruct';\nexport * from './time';\nexport * from './transaction-types';\nexport * from './versions';\nexport {\n toWei,\n fromWei,\n numberToString,\n getValueOfUnit,\n unitMap,\n} from './unitsConversion';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/utils",
3
- "version": "11.8.1",
3
+ "version": "11.10.0",
4
4
  "description": "Various JavaScript/TypeScript utilities of wide relevance to the MetaMask codebase",
5
5
  "homepage": "https://github.com/MetaMask/utils#readme",
6
6
  "bugs": {