@metamask-previews/utils 11.12.1-preview-9962b7e
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 +584 -0
- package/LICENSE +15 -0
- package/README.md +106 -0
- package/dist/assert.d.ts +61 -0
- package/dist/assert.d.ts.map +1 -0
- package/dist/assert.js +115 -0
- package/dist/assert.js.map +1 -0
- package/dist/base64.d.ts +25 -0
- package/dist/base64.d.ts.map +1 -0
- package/dist/base64.js +30 -0
- package/dist/base64.js.map +1 -0
- package/dist/bytes.d.ts +198 -0
- package/dist/bytes.d.ts.map +1 -0
- package/dist/bytes.js +406 -0
- package/dist/bytes.js.map +1 -0
- package/dist/caip-types.d.ts +294 -0
- package/dist/caip-types.d.ts.map +1 -0
- package/dist/caip-types.js +369 -0
- package/dist/caip-types.js.map +1 -0
- package/dist/checksum.d.ts +2 -0
- package/dist/checksum.d.ts.map +1 -0
- package/dist/checksum.js +4 -0
- package/dist/checksum.js.map +1 -0
- package/dist/coercers.d.ts +97 -0
- package/dist/coercers.d.ts.map +1 -0
- package/dist/coercers.js +159 -0
- package/dist/coercers.js.map +1 -0
- package/dist/collections.d.ts +39 -0
- package/dist/collections.d.ts.map +1 -0
- package/dist/collections.js +105 -0
- package/dist/collections.js.map +1 -0
- package/dist/encryption-types.d.ts +7 -0
- package/dist/encryption-types.d.ts.map +1 -0
- package/dist/encryption-types.js +2 -0
- package/dist/encryption-types.js.map +1 -0
- package/dist/errors.d.ts +68 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +121 -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/hashing.d.ts +28 -0
- package/dist/hashing.d.ts.map +1 -0
- package/dist/hashing.js +59 -0
- package/dist/hashing.js.map +1 -0
- package/dist/hex.d.ts +117 -0
- package/dist/hex.d.ts.map +1 -0
- package/dist/hex.js +174 -0
- package/dist/hex.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/json.d.ts +398 -0
- package/dist/json.d.ts.map +1 -0
- package/dist/json.js +402 -0
- package/dist/json.js.map +1 -0
- package/dist/keyring.d.ts +243 -0
- package/dist/keyring.d.ts.map +1 -0
- package/dist/keyring.js +2 -0
- package/dist/keyring.js.map +1 -0
- package/dist/logging.d.ts +30 -0
- package/dist/logging.d.ts.map +1 -0
- package/dist/logging.js +35 -0
- package/dist/logging.js.map +1 -0
- package/dist/misc.d.ts +127 -0
- package/dist/misc.d.ts.map +1 -0
- package/dist/misc.js +142 -0
- package/dist/misc.js.map +1 -0
- package/dist/mnemonic.d.ts +14 -0
- package/dist/mnemonic.d.ts.map +1 -0
- package/dist/mnemonic.js +25 -0
- package/dist/mnemonic.js.map +1 -0
- package/dist/node.d.ts +3 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +3 -0
- package/dist/node.js.map +1 -0
- 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/opaque.d.ts +6 -0
- package/dist/opaque.d.ts.map +1 -0
- package/dist/opaque.js +2 -0
- package/dist/opaque.js.map +1 -0
- package/dist/promise.d.ts +45 -0
- package/dist/promise.d.ts.map +1 -0
- package/dist/promise.js +40 -0
- package/dist/promise.js.map +1 -0
- package/dist/superstruct.d.ts +20 -0
- package/dist/superstruct.d.ts.map +1 -0
- package/dist/superstruct.js +24 -0
- package/dist/superstruct.js.map +1 -0
- package/dist/time.d.ts +49 -0
- package/dist/time.d.ts.map +1 -0
- package/dist/time.js +62 -0
- package/dist/time.js.map +1 -0
- package/dist/transaction-types.d.ts +117 -0
- package/dist/transaction-types.d.ts.map +1 -0
- package/dist/transaction-types.js +2 -0
- package/dist/transaction-types.js.map +1 -0
- package/dist/unitsConversion.d.ts +80 -0
- package/dist/unitsConversion.d.ts.map +1 -0
- package/dist/unitsConversion.js +209 -0
- package/dist/unitsConversion.js.map +1 -0
- package/dist/versions.d.ts +101 -0
- package/dist/versions.d.ts.map +1 -0
- package/dist/versions.js +85 -0
- package/dist/versions.js.map +1 -0
- package/package.json +122 -0
package/README.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
<table><tr><td><p align="center"><b>⚠️ PLEASE READ ⚠️</b></p><p align="center">This package is currently being migrated to our <a href="https://github.com/MetaMask/core"><code>core</code></a> monorepo. Please do not make any commits to this repository while this migration is taking place, as they will not be transferred over. Also, please re-open PRs that are under active development in the core repo.</p></td></tr></table>
|
|
2
|
+
|
|
3
|
+
# MetaMask Utils
|
|
4
|
+
|
|
5
|
+
Various JavaScript/TypeScript utilities of wide relevance to the MetaMask codebase.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
`yarn add @metamask/utils`
|
|
10
|
+
|
|
11
|
+
or
|
|
12
|
+
|
|
13
|
+
`npm install @metamask/utils`
|
|
14
|
+
|
|
15
|
+
## API
|
|
16
|
+
|
|
17
|
+
The full API documentation for the latest published version of this library is [available here](https://metamask.github.io/utils/index.html).
|
|
18
|
+
|
|
19
|
+
## Contributing
|
|
20
|
+
|
|
21
|
+
### Setup
|
|
22
|
+
|
|
23
|
+
- Install [Node.js](https://nodejs.org) version 16
|
|
24
|
+
- If you are using [nvm](https://github.com/creationix/nvm#installation) (recommended) running `nvm use` will automatically choose the right node version for you.
|
|
25
|
+
- Install [Yarn v3](https://yarnpkg.com/getting-started/install)
|
|
26
|
+
- Run `yarn install` to install dependencies and run any required post-install scripts
|
|
27
|
+
|
|
28
|
+
### Testing and Linting
|
|
29
|
+
|
|
30
|
+
Run `yarn test` to run the tests once. To run tests on file changes, run `yarn test:watch`.
|
|
31
|
+
|
|
32
|
+
Run `yarn lint` to run the linter, or run `yarn lint:fix` to run the linter and fix any automatically fixable issues.
|
|
33
|
+
|
|
34
|
+
### Documentation
|
|
35
|
+
|
|
36
|
+
The API documentation can be generated with the command `yarn docs`, which saves it in the `./docs` directory. Open the `./docs/index.html` file to browse the documentation.
|
|
37
|
+
|
|
38
|
+
### Release & Publishing
|
|
39
|
+
|
|
40
|
+
The project follows the same release process as the other libraries in the MetaMask organization. The GitHub Actions [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) and [`action-publish-release`](https://github.com/MetaMask/action-publish-release) are used to automate the release process; see those repositories for more information about how they work.
|
|
41
|
+
|
|
42
|
+
1. Choose a release version.
|
|
43
|
+
- The release version should be chosen according to SemVer. Analyze the changes to see whether they include any breaking changes, new features, or deprecations, then choose the appropriate SemVer version. See [the SemVer specification](https://semver.org/) for more information.
|
|
44
|
+
|
|
45
|
+
2. If this release is backporting changes onto a previous release, then ensure there is a major version branch for that version (e.g. `1.x` for a `v1` backport release).
|
|
46
|
+
- The major version branch should be set to the most recent release with that major version. For example, when backporting a `v1.0.2` release, you'd want to ensure there was a `1.x` branch that was set to the `v1.0.1` tag.
|
|
47
|
+
|
|
48
|
+
3. Trigger the [`workflow_dispatch`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch) event [manually](https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow) for the `Create Release Pull Request` action to create the release PR.
|
|
49
|
+
- For a backport release, the base branch should be the major version branch that you ensured existed in step 2. For a normal release, the base branch should be the main branch for that repository (which should be the default value).
|
|
50
|
+
- This should trigger the [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) workflow to create the release PR.
|
|
51
|
+
|
|
52
|
+
4. Update the changelog to move each change entry into the appropriate change category ([See here](https://keepachangelog.com/en/1.0.0/#types) for the full list of change categories, and the correct ordering), and edit them to be more easily understood by users of the package.
|
|
53
|
+
- Generally any changes that don't affect consumers of the package (e.g. lockfile changes or development environment changes) are omitted. Exceptions may be made for changes that might be of interest despite not having an effect upon the published package (e.g. major test improvements, security improvements, improved documentation, etc.).
|
|
54
|
+
- Try to explain each change in terms that users of the package would understand (e.g. avoid referencing internal variables/concepts).
|
|
55
|
+
- Consolidate related changes into one change entry if it makes it easier to explain.
|
|
56
|
+
- Run `yarn auto-changelog validate --rc` to check that the changelog is correctly formatted.
|
|
57
|
+
|
|
58
|
+
5. Review and QA the release.
|
|
59
|
+
- If changes are made to the base branch, the release branch will need to be updated with these changes and review/QA will need to restart again. As such, it's probably best to avoid merging other PRs into the base branch while review is underway.
|
|
60
|
+
|
|
61
|
+
6. Squash & Merge the release.
|
|
62
|
+
- This should trigger the [`action-publish-release`](https://github.com/MetaMask/action-publish-release) workflow to tag the final release commit and publish the release on GitHub.
|
|
63
|
+
|
|
64
|
+
7. Publish the release on npm.
|
|
65
|
+
- Be very careful to use a clean local environment to publish the release, and follow exactly the same steps used during CI.
|
|
66
|
+
- Use `npm publish --dry-run` to examine the release contents to ensure the correct files are included. Compare to previous releases if necessary (e.g. using `https://unpkg.com/browse/[package name]@[package version]/`).
|
|
67
|
+
- Once you are confident the release contents are correct, publish the release using `npm publish`.
|
|
68
|
+
|
|
69
|
+
### Testing changes in other projects using preview builds
|
|
70
|
+
|
|
71
|
+
If you are working on a pull request and want to test changes in another project before you publish them, you can create a _preview build_ and then configure your project to use it.
|
|
72
|
+
|
|
73
|
+
#### Creating a preview build
|
|
74
|
+
|
|
75
|
+
1. Within your pull request, post a comment with the text `@metamaskbot publish-preview`. This starts the `publish-preview` GitHub action, which will create a preview build and publish it to NPM.
|
|
76
|
+
2. After a few minutes, the action should complete and you will see a new comment. Note two things:
|
|
77
|
+
- The name is scoped to `@metamask-previews` instead of `@metamask`.
|
|
78
|
+
- The ID of the last commit in the branch is appended to the version, e.g. `1.2.3-preview-e2df9b4` instead of `1.2.3`.
|
|
79
|
+
|
|
80
|
+
#### Using a preview build
|
|
81
|
+
|
|
82
|
+
To use a preview build within a project, you need to override the resolution logic for your package manager so that the "production" version of that package is replaced with the preview version. Here's how you do that:
|
|
83
|
+
|
|
84
|
+
1. Open `package.json` in the project and locate the entry for this package in `dependencies`.
|
|
85
|
+
2. Locate the section responsible for resolution overrides (or create it if it doesn't exist). If you're using Yarn, this is `resolutions`; if you're using NPM or any other package manager, this is `overrides`.
|
|
86
|
+
3. Add a line to this section that mirrors the dependency entry on the left-hand side and points to the preview version on the right-hand side. Note the exact format of the left-hand side will differ based on which version of Yarn or NPM you are using. For example:
|
|
87
|
+
- For Yarn Modern, you will add something like this to `resolutions`:
|
|
88
|
+
```
|
|
89
|
+
"@metamask/utils@^1.2.3": "npm:@metamask-previews/utils@1.2.3-preview-abcdefg"
|
|
90
|
+
```
|
|
91
|
+
- For Yarn Classic, you will add something like this to `resolutions`:
|
|
92
|
+
```
|
|
93
|
+
"@metamask/utils": "npm:@metamask-previews/utils@1.2.3-preview-abcdefg"
|
|
94
|
+
```
|
|
95
|
+
- For NPM, you will add something like this to `overrides`:
|
|
96
|
+
```
|
|
97
|
+
"@metamask/utils": "npm:@metamask-previews/utils@1.2.3-preview-abcdefg"
|
|
98
|
+
```
|
|
99
|
+
4. Run `yarn install`.
|
|
100
|
+
|
|
101
|
+
#### Updating a preview build
|
|
102
|
+
|
|
103
|
+
If you make more changes to your pull request and want to create a new preview build:
|
|
104
|
+
|
|
105
|
+
1. Post another `@metamaskbot` comment on the pull request and wait for the response.
|
|
106
|
+
2. Update the version of the preview build in your project's `package.json`. Make sure to re-run `yarn install`!
|
package/dist/assert.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Struct } from '@metamask/superstruct';
|
|
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;
|
|
29
|
+
/**
|
|
30
|
+
* Assert a value against a Superstruct struct.
|
|
31
|
+
*
|
|
32
|
+
* @param value - The value to validate.
|
|
33
|
+
* @param struct - The struct to validate against.
|
|
34
|
+
* @param errorPrefix - A prefix to add to the error message. Defaults to
|
|
35
|
+
* "Assertion failed".
|
|
36
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
37
|
+
* Defaults to {@link AssertionError}.
|
|
38
|
+
* @throws If the value is not valid.
|
|
39
|
+
*/
|
|
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;
|
|
61
|
+
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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;AAkD3C;;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,EAE7C,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,EAEhC,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
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { assert as assertSuperstruct } from '@metamask/superstruct';
|
|
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
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Attempts to obtain the message from a possible error object. If it is
|
|
16
|
+
* possible to do so, any trailing period will be removed from the message;
|
|
17
|
+
* otherwise an empty string is returned.
|
|
18
|
+
*
|
|
19
|
+
* @param error - The error object to get the message from.
|
|
20
|
+
* @returns The message without any trailing period if `error` is an object
|
|
21
|
+
* with a `message` property; the string version of `error` without any trailing
|
|
22
|
+
* period if it is not `undefined` or `null`; otherwise an empty string.
|
|
23
|
+
*/
|
|
24
|
+
function getErrorMessageWithoutTrailingPeriod(error) {
|
|
25
|
+
// We'll add our own period.
|
|
26
|
+
return getErrorMessage(error).replace(/\.$/u, '');
|
|
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
|
+
constructor(options) {
|
|
50
|
+
super(options.message);
|
|
51
|
+
this.code = 'ERR_ASSERTION';
|
|
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
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Assert a value against a Superstruct struct.
|
|
76
|
+
*
|
|
77
|
+
* @param value - The value to validate.
|
|
78
|
+
* @param struct - The struct to validate against.
|
|
79
|
+
* @param errorPrefix - A prefix to add to the error message. Defaults to
|
|
80
|
+
* "Assertion failed".
|
|
81
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
82
|
+
* Defaults to {@link AssertionError}.
|
|
83
|
+
* @throws If the value is not valid.
|
|
84
|
+
*/
|
|
85
|
+
export function assertStruct(value, struct, errorPrefix = 'Assertion failed', ErrorWrapper = AssertionError) {
|
|
86
|
+
try {
|
|
87
|
+
assertSuperstruct(value, struct);
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
throw getError(ErrorWrapper, `${errorPrefix}: ${getErrorMessageWithoutTrailingPeriod(error)}.`);
|
|
91
|
+
}
|
|
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
|
+
}
|
|
115
|
+
//# sourceMappingURL=assert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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;AAEH,SAAS,QAAQ,CAAC,YAAuC,EAAE,OAAe;IACxE,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;IAGvC,YAAY,OAA4B;QACtC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAHhB,SAAI,GAAG,eAAe,CAAC;IAIhC,CAAC;CACF;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,MAAM,CACpB,KAAU,EACV,OAAO,GAAmB,mBAAmB,EAE7C,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,EAEhC,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 */\n\nfunction getError(ErrorWrapper: AssertionErrorConstructor, message: string) {\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\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\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
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Struct } from '@metamask/superstruct';
|
|
2
|
+
export type Base64Options = {
|
|
3
|
+
/**
|
|
4
|
+
* Is the `=` padding at the end required or not.
|
|
5
|
+
*
|
|
6
|
+
* @default false
|
|
7
|
+
*/
|
|
8
|
+
paddingRequired?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Which character set should be used.
|
|
11
|
+
* The sets are based on {@link https://datatracker.ietf.org/doc/html/rfc4648 RFC 4648}.
|
|
12
|
+
*
|
|
13
|
+
* @default 'base64'
|
|
14
|
+
*/
|
|
15
|
+
characterSet?: 'base64' | 'base64url';
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Ensure that a provided string-based struct is valid base64.
|
|
19
|
+
*
|
|
20
|
+
* @param struct - The string based struct.
|
|
21
|
+
* @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.
|
|
22
|
+
* @returns A superstruct validating base64.
|
|
23
|
+
*/
|
|
24
|
+
export declare const base64: <Type extends string, Schema>(struct: Struct<Type, Schema>, options?: Base64Options) => Struct<Type, Schema>;
|
|
25
|
+
//# sourceMappingURL=base64.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,yBA2BvB,CAAC"}
|
package/dist/base64.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { pattern } from '@metamask/superstruct';
|
|
2
|
+
import { assert } from './assert.js';
|
|
3
|
+
/**
|
|
4
|
+
* Ensure that a provided string-based struct is valid base64.
|
|
5
|
+
*
|
|
6
|
+
* @param struct - The string based struct.
|
|
7
|
+
* @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.
|
|
8
|
+
* @returns A superstruct validating base64.
|
|
9
|
+
*/
|
|
10
|
+
export const base64 = (struct, options = {}) => {
|
|
11
|
+
const paddingRequired = options.paddingRequired ?? false;
|
|
12
|
+
const characterSet = options.characterSet ?? 'base64';
|
|
13
|
+
let letters;
|
|
14
|
+
if (characterSet === 'base64') {
|
|
15
|
+
letters = String.raw `[A-Za-z0-9+\/]`;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
assert(characterSet === 'base64url');
|
|
19
|
+
letters = String.raw `[-_A-Za-z0-9]`;
|
|
20
|
+
}
|
|
21
|
+
let re;
|
|
22
|
+
if (paddingRequired) {
|
|
23
|
+
re = new RegExp(`^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`, 'u');
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
re = new RegExp(`^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`, 'u');
|
|
27
|
+
}
|
|
28
|
+
return pattern(struct, re);
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=base64.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,EAC3B,EAAE;IACF,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) => {\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;
|
|
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
|
+
/**
|
|
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;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,UAAU,GAAG,MAAM,CAIvD;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAoCpD;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAMvD;AAkBD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,MAAM,GACjB,UAAU,CAqBZ;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAUvD;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAIvD;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAIvD;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,UAAU,CAsBrD;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,UAAU,CAqBvD;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,UAAU,GAAG,QAAQ,CAe1D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CAazE"}
|