@nomicfoundation/hardhat-utils 4.3.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/dist/src/abi.d.ts +79 -0
- package/dist/src/abi.d.ts.map +1 -0
- package/dist/src/abi.js +114 -0
- package/dist/src/abi.js.map +1 -0
- package/dist/src/bytes.d.ts +5 -4
- package/dist/src/bytes.d.ts.map +1 -1
- package/dist/src/bytes.js +22 -13
- package/dist/src/bytes.js.map +1 -1
- package/dist/src/errors/abi.d.ts +93 -0
- package/dist/src/errors/abi.d.ts.map +1 -0
- package/dist/src/errors/abi.js +117 -0
- package/dist/src/errors/abi.js.map +1 -0
- package/dist/src/fs.d.ts +25 -29
- package/dist/src/fs.d.ts.map +1 -1
- package/dist/src/fs.js +84 -82
- package/dist/src/fs.js.map +1 -1
- package/dist/src/internal/abi.d.ts +50 -0
- package/dist/src/internal/abi.d.ts.map +1 -0
- package/dist/src/internal/abi.js +248 -0
- package/dist/src/internal/abi.js.map +1 -0
- package/dist/src/internal/bytes.js +3 -3
- package/dist/src/internal/bytes.js.map +1 -1
- package/dist/src/internal/fs.d.ts +6 -0
- package/dist/src/internal/fs.d.ts.map +1 -1
- package/dist/src/internal/fs.js +51 -3
- package/dist/src/internal/fs.js.map +1 -1
- package/dist/src/internal/json.d.ts +48 -0
- package/dist/src/internal/json.d.ts.map +1 -0
- package/dist/src/internal/json.js +54 -0
- package/dist/src/internal/json.js.map +1 -0
- package/dist/src/internal/request.d.ts.map +1 -1
- package/dist/src/internal/request.js +3 -2
- package/dist/src/internal/request.js.map +1 -1
- package/package.json +5 -3
- package/src/abi.ts +199 -0
- package/src/bytes.ts +31 -14
- package/src/errors/abi.ts +156 -0
- package/src/fs.ts +96 -87
- package/src/internal/abi.ts +431 -0
- package/src/internal/bytes.ts +4 -4
- package/src/internal/fs.ts +66 -3
- package/src/internal/json.ts +59 -0
- package/src/internal/request.ts +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# @nomicfoundation/hardhat-utils
|
|
2
2
|
|
|
3
|
+
## 5.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- [#8693](https://github.com/NomicFoundation/hardhat/pull/8693) [`98db1b3`](https://github.com/NomicFoundation/hardhat/commit/98db1b30c9471a5f3db6424909b1f2c3bc43e093) Thanks [@schaable](https://github.com/schaable)! - Removed the deprecated `readJsonFileAsStream`, `writeJsonFileAsStream` and `getFileTrueCase`; use `readJsonFile`, `writeLargeJsonFile` and `TrueCasePathResolver` instead.
|
|
8
|
+
|
|
9
|
+
### Minor Changes
|
|
10
|
+
|
|
11
|
+
- [#8690](https://github.com/NomicFoundation/hardhat/pull/8690) [`0058149`](https://github.com/NomicFoundation/hardhat/commit/0058149a39f2ac9063f140e85a7949bd7e42fff2) Thanks [@schaable](https://github.com/schaable)! - Added `writeLargeJsonFile` and made `readJsonFile` read files of any size, deprecating `writeJsonFileAsStream` and `readJsonFileAsStream`.
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- [#8690](https://github.com/NomicFoundation/hardhat/pull/8690) [`0058149`](https://github.com/NomicFoundation/hardhat/commit/0058149a39f2ac9063f140e85a7949bd7e42fff2) Thanks [@schaable](https://github.com/schaable)! - Sped up compilation by reading and writing solc's output with `JSON.parse` and `JSON.stringify` when memory allows ([#8565](https://github.com/NomicFoundation/hardhat/issues/8565)).
|
|
16
|
+
|
|
17
|
+
- [#8695](https://github.com/NomicFoundation/hardhat/pull/8695) [`38041a8`](https://github.com/NomicFoundation/hardhat/commit/38041a8200553583bbdf228956b48de2edb0cf5a) Thanks [@kanej](https://github.com/kanej)! - Reduced the startup time and memory use of Hardhat commands that make no network request.
|
|
18
|
+
|
|
19
|
+
## 4.4.0
|
|
20
|
+
|
|
21
|
+
### Minor Changes
|
|
22
|
+
|
|
23
|
+
- [#8632](https://github.com/NomicFoundation/hardhat/pull/8632) [`eb2b13d`](https://github.com/NomicFoundation/hardhat/commit/eb2b13da0a4395006e39bc5927403dd21be13b02) Thanks [@schaable](https://github.com/schaable)! - Added an `abi` module with `encodeAbiParameters()` for ABI-encoding values against a list of Solidity parameter types.
|
|
24
|
+
|
|
25
|
+
### Patch Changes
|
|
26
|
+
|
|
27
|
+
- [#8646](https://github.com/NomicFoundation/hardhat/pull/8646) [`ac45dab`](https://github.com/NomicFoundation/hardhat/commit/ac45dab636432dbe98da5eb2909913dab0e202f5) Thanks [@renovate](https://github.com/apps/renovate)! - Updated the `@streamparser/json-node` dependency to 0.0.26.
|
|
28
|
+
|
|
3
29
|
## 4.3.0
|
|
4
30
|
|
|
5
31
|
### Minor Changes
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { PrefixedHexString } from "./hex.js";
|
|
2
|
+
/**
|
|
3
|
+
* A parameter of an ABI fragment.
|
|
4
|
+
*/
|
|
5
|
+
export interface AbiParameter {
|
|
6
|
+
readonly name?: string;
|
|
7
|
+
readonly type: string;
|
|
8
|
+
readonly components?: readonly AbiParameter[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* An entry of a contract's ABI. Only the fields needed to encode values are
|
|
12
|
+
* modeled.
|
|
13
|
+
*/
|
|
14
|
+
export interface AbiFragment {
|
|
15
|
+
readonly type?: string;
|
|
16
|
+
readonly name?: string;
|
|
17
|
+
readonly inputs?: readonly AbiParameter[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The signedness and size of a Solidity integer type.
|
|
21
|
+
*/
|
|
22
|
+
export interface IntType {
|
|
23
|
+
readonly signed: boolean;
|
|
24
|
+
readonly bits: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The inclusive range of values that a Solidity integer type can hold.
|
|
28
|
+
*/
|
|
29
|
+
export interface IntTypeRange {
|
|
30
|
+
readonly min: bigint;
|
|
31
|
+
readonly max: bigint;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Parses a Solidity integer type name, like `uint256` or `int128`, into its
|
|
35
|
+
* signedness and size in bits. As in Solidity, `uint` and `int` are accepted as
|
|
36
|
+
* aliases of the 256-bit types.
|
|
37
|
+
*
|
|
38
|
+
* @param type The type name to parse.
|
|
39
|
+
* @returns The parsed type, or `undefined` if it isn't a Solidity integer type.
|
|
40
|
+
*/
|
|
41
|
+
export declare function parseIntType(type: string): IntType | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Returns the inclusive range of values that a Solidity integer type can hold.
|
|
44
|
+
*
|
|
45
|
+
* @param intType The integer type, as returned by {@link parseIntType}.
|
|
46
|
+
* @returns The minimum and maximum values of the type.
|
|
47
|
+
*/
|
|
48
|
+
export declare function getIntTypeRange({ signed, bits }: IntType): IntTypeRange;
|
|
49
|
+
/**
|
|
50
|
+
* Returns the parameters of the constructor declared in an ABI.
|
|
51
|
+
*
|
|
52
|
+
* An ABI that declares no constructor yields an empty array, matching the
|
|
53
|
+
* Solidity semantics of an implicit parameterless constructor.
|
|
54
|
+
*
|
|
55
|
+
* @param abi The ABI to read the constructor from.
|
|
56
|
+
* @returns The constructor's parameters.
|
|
57
|
+
*/
|
|
58
|
+
export declare function getAbiConstructorParameters(abi: readonly AbiFragment[]): readonly AbiParameter[];
|
|
59
|
+
/**
|
|
60
|
+
* ABI-encodes a list of values, as Solidity encodes a constructor's arguments
|
|
61
|
+
* (i.e. without a function selector).
|
|
62
|
+
*
|
|
63
|
+
* Values are accepted in the same shapes that most Ethereum libraries accept:
|
|
64
|
+
* integers as bigints, safe numbers, or decimal or hex strings; `bytes` as hex
|
|
65
|
+
* strings or `Uint8Array`s; tuples as arrays or as objects keyed by component
|
|
66
|
+
* name; and addresses with or without the "0x" prefix.
|
|
67
|
+
*
|
|
68
|
+
* @param parameters The ABI parameters to encode the values as.
|
|
69
|
+
* @param values The values to encode, in the order of the parameters.
|
|
70
|
+
* @returns The encoded values as a prefixed hex string, or "0x" if there are none.
|
|
71
|
+
* @throws AbiParametersLengthMismatchError If the counts don't match.
|
|
72
|
+
* @throws InvalidAbiValueError If a value can't be encoded as its type.
|
|
73
|
+
* @throws AbiValueOutOfBoundsError If a numeric value is out of its type's range.
|
|
74
|
+
* @throws UnsupportedAbiTypeError If a parameter's type can't be encoded.
|
|
75
|
+
* @throws AbiEncodingFailedError If the encoder fails for any other reason.
|
|
76
|
+
*/
|
|
77
|
+
export declare function encodeAbiParameters(parameters: readonly AbiParameter[], values: readonly unknown[]): Promise<PrefixedHexString>;
|
|
78
|
+
export { AbiEncodingError, AbiEncodingFailedError, AbiParametersLengthMismatchError, AbiValueOutOfBoundsError, DuplicateAbiParameterNameError, InvalidAbiValueError, UnsupportedAbiTypeError, isAbiEncodingError, type AbiEncodingErrorKind, type AnyAbiEncodingError, } from "./errors/abi.js";
|
|
79
|
+
//# sourceMappingURL=abi.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abi.d.ts","sourceRoot":"","sources":["../../src/abi.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAmBlD;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,UAAU,CAAC,EAAE,SAAS,YAAY,EAAE,CAAC;CAC/C;AAED;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,YAAY,EAAE,CAAC;CAC3C;AAED;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAID;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS,CAc9D;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,OAAO,GAAG,YAAY,CAOvE;AAED;;;;;;;;GAQG;AACH,wBAAgB,2BAA2B,CACzC,GAAG,EAAE,SAAS,WAAW,EAAE,GAC1B,SAAS,YAAY,EAAE,CAEzB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,mBAAmB,CACvC,UAAU,EAAE,SAAS,YAAY,EAAE,EACnC,MAAM,EAAE,SAAS,OAAO,EAAE,GACzB,OAAO,CAAC,iBAAiB,CAAC,CAsD5B;AAED,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,gCAAgC,EAChC,wBAAwB,EACxB,8BAA8B,EAC9B,oBAAoB,EACpB,uBAAuB,EACvB,kBAAkB,EAClB,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,GACzB,MAAM,iBAAiB,CAAC"}
|
package/dist/src/abi.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { ensureError } from "./error.js";
|
|
2
|
+
import { AbiEncodingFailedError, AbiParametersLengthMismatchError, } from "./errors/abi.js";
|
|
3
|
+
import { getPrefixedHexString } from "./hex.js";
|
|
4
|
+
import { assertNoDuplicateAbiParameterNames, normalizeAbiValue, toUnnamedAbiParameters, } from "./internal/abi.js";
|
|
5
|
+
// We don't load micro-eth-signer on startup because it's slow to initialize,
|
|
6
|
+
// and most callers of this module never encode anything.
|
|
7
|
+
let deployContractImpl;
|
|
8
|
+
const INT_TYPE_PATTERN = /^(u?)int([1-9]\d*)?$/;
|
|
9
|
+
/**
|
|
10
|
+
* Parses a Solidity integer type name, like `uint256` or `int128`, into its
|
|
11
|
+
* signedness and size in bits. As in Solidity, `uint` and `int` are accepted as
|
|
12
|
+
* aliases of the 256-bit types.
|
|
13
|
+
*
|
|
14
|
+
* @param type The type name to parse.
|
|
15
|
+
* @returns The parsed type, or `undefined` if it isn't a Solidity integer type.
|
|
16
|
+
*/
|
|
17
|
+
export function parseIntType(type) {
|
|
18
|
+
const match = INT_TYPE_PATTERN.exec(type);
|
|
19
|
+
if (match === null) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
const bits = match[2] === undefined ? 256 : Number(match[2]);
|
|
23
|
+
if (bits > 256 || bits % 8 !== 0) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
return { signed: match[1] === "", bits };
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Returns the inclusive range of values that a Solidity integer type can hold.
|
|
30
|
+
*
|
|
31
|
+
* @param intType The integer type, as returned by {@link parseIntType}.
|
|
32
|
+
* @returns The minimum and maximum values of the type.
|
|
33
|
+
*/
|
|
34
|
+
export function getIntTypeRange({ signed, bits }) {
|
|
35
|
+
if (signed) {
|
|
36
|
+
const bound = 2n ** BigInt(bits - 1);
|
|
37
|
+
return { min: -bound, max: bound - 1n };
|
|
38
|
+
}
|
|
39
|
+
return { min: 0n, max: 2n ** BigInt(bits) - 1n };
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns the parameters of the constructor declared in an ABI.
|
|
43
|
+
*
|
|
44
|
+
* An ABI that declares no constructor yields an empty array, matching the
|
|
45
|
+
* Solidity semantics of an implicit parameterless constructor.
|
|
46
|
+
*
|
|
47
|
+
* @param abi The ABI to read the constructor from.
|
|
48
|
+
* @returns The constructor's parameters.
|
|
49
|
+
*/
|
|
50
|
+
export function getAbiConstructorParameters(abi) {
|
|
51
|
+
return abi.find(({ type }) => type === "constructor")?.inputs ?? [];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* ABI-encodes a list of values, as Solidity encodes a constructor's arguments
|
|
55
|
+
* (i.e. without a function selector).
|
|
56
|
+
*
|
|
57
|
+
* Values are accepted in the same shapes that most Ethereum libraries accept:
|
|
58
|
+
* integers as bigints, safe numbers, or decimal or hex strings; `bytes` as hex
|
|
59
|
+
* strings or `Uint8Array`s; tuples as arrays or as objects keyed by component
|
|
60
|
+
* name; and addresses with or without the "0x" prefix.
|
|
61
|
+
*
|
|
62
|
+
* @param parameters The ABI parameters to encode the values as.
|
|
63
|
+
* @param values The values to encode, in the order of the parameters.
|
|
64
|
+
* @returns The encoded values as a prefixed hex string, or "0x" if there are none.
|
|
65
|
+
* @throws AbiParametersLengthMismatchError If the counts don't match.
|
|
66
|
+
* @throws InvalidAbiValueError If a value can't be encoded as its type.
|
|
67
|
+
* @throws AbiValueOutOfBoundsError If a numeric value is out of its type's range.
|
|
68
|
+
* @throws UnsupportedAbiTypeError If a parameter's type can't be encoded.
|
|
69
|
+
* @throws AbiEncodingFailedError If the encoder fails for any other reason.
|
|
70
|
+
*/
|
|
71
|
+
export async function encodeAbiParameters(parameters, values) {
|
|
72
|
+
if (parameters.length !== values.length) {
|
|
73
|
+
throw new AbiParametersLengthMismatchError(parameters.length, values.length);
|
|
74
|
+
}
|
|
75
|
+
// Returning here also keeps micro-eth-signer unloaded.
|
|
76
|
+
if (parameters.length === 0) {
|
|
77
|
+
return "0x";
|
|
78
|
+
}
|
|
79
|
+
assertNoDuplicateAbiParameterNames(parameters);
|
|
80
|
+
// The values are normalized sequentially so that, when more than one is
|
|
81
|
+
// invalid, the error reported is always the one of the first.
|
|
82
|
+
const normalizedValues = [];
|
|
83
|
+
for (const [i, parameter] of parameters.entries()) {
|
|
84
|
+
normalizedValues.push(await normalizeAbiValue(parameter, values[i], parameter.name ?? `[${i}]`));
|
|
85
|
+
}
|
|
86
|
+
if (deployContractImpl === undefined) {
|
|
87
|
+
({ deployContract: deployContractImpl } =
|
|
88
|
+
await import("micro-eth-signer/advanced/abi.js"));
|
|
89
|
+
}
|
|
90
|
+
// The encoder takes the bare value when there's a single parameter, and a
|
|
91
|
+
// positional array otherwise. The array is positional at every level because
|
|
92
|
+
// toUnnamedAbiParameters drops the names.
|
|
93
|
+
const value = parameters.length === 1 ? normalizedValues[0] : normalizedValues;
|
|
94
|
+
// NOTE: this must not be annotated nor frozen with `as const`. With `type`
|
|
95
|
+
// widened to `string`, micro-eth-signer's generic overload resolves to its
|
|
96
|
+
// untyped form, which is what we want for a dynamically built ABI.
|
|
97
|
+
const abi = [
|
|
98
|
+
{ type: "constructor", inputs: toUnnamedAbiParameters(parameters) },
|
|
99
|
+
];
|
|
100
|
+
let encoded;
|
|
101
|
+
try {
|
|
102
|
+
// deployContract returns `<bytecode><encoded args>`, so we pass no bytecode.
|
|
103
|
+
encoded = deployContractImpl(abi, "0x", value);
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
// Reached by types that pass validation but the encoder still refuses,
|
|
107
|
+
// such as `uint256[0][]`, whose zero-size element it treats as a DoS risk.
|
|
108
|
+
ensureError(error);
|
|
109
|
+
throw new AbiEncodingFailedError(error);
|
|
110
|
+
}
|
|
111
|
+
return getPrefixedHexString(encoded);
|
|
112
|
+
}
|
|
113
|
+
export { AbiEncodingError, AbiEncodingFailedError, AbiParametersLengthMismatchError, AbiValueOutOfBoundsError, DuplicateAbiParameterNameError, InvalidAbiValueError, UnsupportedAbiTypeError, isAbiEncodingError, } from "./errors/abi.js";
|
|
114
|
+
//# sourceMappingURL=abi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abi.js","sourceRoot":"","sources":["../../src/abi.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EACL,sBAAsB,EACtB,gCAAgC,GACjC,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAA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EAAmE;IACnE,MAAM,GAAG,GAAG;QACV,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,EAAE,sBAAsB,CAAC,UAAU,CAAC,EAAE;KACpE,CAAC;IAEF,IAAI,OAAe,CAAC;IACpB,IAAI,CAAC;QACH,6EAA6E;QAC7E,OAAO,GAAG,kBAAkB,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;IACjD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,uEAAuE;QACvE,2EAA2E;QAC3E,WAAW,CAAC,KAAK,CAAC,CAAC;QACnB,MAAM,IAAI,sBAAsB,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IAED,OAAO,oBAAoB,CAAC,OAAO,CAAC,CAAC;AACvC,CAAC;AAED,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,gCAAgC,EAChC,wBAAwB,EACxB,8BAA8B,EAC9B,oBAAoB,EACpB,uBAAuB,EACvB,kBAAkB,GAGnB,MAAM,iBAAiB,CAAC"}
|
package/dist/src/bytes.d.ts
CHANGED
|
@@ -50,13 +50,14 @@ export declare function bytesIncludesUtf8String(haystack: Uint8Array, needle: st
|
|
|
50
50
|
/**
|
|
51
51
|
* Parses UTF-8 encoded JSON bytes into a value.
|
|
52
52
|
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
53
|
+
* The bytes are decoded and parsed with `JSON.parse`, which is much faster,
|
|
54
|
+
* when the decoded text fits in a single string and the heap has room for it.
|
|
55
|
+
* Otherwise, they are parsed as a stream, which never holds the whole text as
|
|
56
|
+
* one string. Both give the same result.
|
|
57
57
|
*
|
|
58
58
|
* @param bytes The UTF-8 encoded JSON bytes.
|
|
59
59
|
* @returns The parsed JSON object.
|
|
60
|
+
* @throws InvalidParameterError if the bytes aren't valid UTF-8 encoded JSON.
|
|
60
61
|
*/
|
|
61
62
|
export declare function parseJsonBytes<T>(bytes: Uint8Array): Promise<T>;
|
|
62
63
|
export { bytesToBigInt, bytesToNumber, numberToBytes } from "./number.js";
|
package/dist/src/bytes.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../../src/bytes.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../../src/bytes.ts"],"names":[],"mappings":"AAWA;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,UAAU,CAE3D;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,UAAU,CAQ3E;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CAEjE;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU,CAEhE;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAE3D;AAED;;;;;;;;;GASG;AACH,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,UAAU,EACpB,MAAM,EAAE,MAAM,GACb,OAAO,CA+BT;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,cAAc,CAAC,CAAC,EAAE,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,CAAC,CAAC,CAsBrE;AAED,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC"}
|
package/dist/src/bytes.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import { constants as bufferConstants } from "node:buffer";
|
|
1
2
|
import { Readable } from "node:stream";
|
|
3
|
+
import { InvalidParameterError } from "./common-errors.js";
|
|
4
|
+
import { ensureError } from "./error.js";
|
|
2
5
|
import { buildLpsTable, parseJsonStream } from "./internal/bytes.js";
|
|
6
|
+
import { hasHeapHeadroomFor, READ_HEAP_BYTES_PER_JSON_BYTE, } from "./internal/json.js";
|
|
3
7
|
/**
|
|
4
8
|
* Checks if a value is an instance of Uint8Array.
|
|
5
9
|
*
|
|
@@ -96,27 +100,32 @@ export function bytesIncludesUtf8String(haystack, needle) {
|
|
|
96
100
|
/**
|
|
97
101
|
* Parses UTF-8 encoded JSON bytes into a value.
|
|
98
102
|
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
+
* The bytes are decoded and parsed with `JSON.parse`, which is much faster,
|
|
104
|
+
* when the decoded text fits in a single string and the heap has room for it.
|
|
105
|
+
* Otherwise, they are parsed as a stream, which never holds the whole text as
|
|
106
|
+
* one string. Both give the same result.
|
|
103
107
|
*
|
|
104
108
|
* @param bytes The UTF-8 encoded JSON bytes.
|
|
105
109
|
* @returns The parsed JSON object.
|
|
110
|
+
* @throws InvalidParameterError if the bytes aren't valid UTF-8 encoded JSON.
|
|
106
111
|
*/
|
|
107
112
|
export async function parseJsonBytes(bytes) {
|
|
108
|
-
let json;
|
|
109
113
|
try {
|
|
110
|
-
|
|
114
|
+
// Each UTF-8 byte decodes to at most one character, so this also
|
|
115
|
+
// guarantees that the text fits in a string. Everything from the heap
|
|
116
|
+
// check on is synchronous, so no other code can use memory in between.
|
|
117
|
+
if (bytes.length > bufferConstants.MAX_STRING_LENGTH ||
|
|
118
|
+
!hasHeapHeadroomFor(bytes.length * READ_HEAP_BYTES_PER_JSON_BYTE)) {
|
|
119
|
+
return await parseJsonStream(Readable.from([bytes]));
|
|
120
|
+
}
|
|
121
|
+
// Like the stream parser, this skips a leading BOM and rejects invalid
|
|
122
|
+
// UTF-8, so both paths behave the same.
|
|
123
|
+
return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
111
124
|
}
|
|
112
|
-
catch {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
// length the runtime can hold. Stream parsing handles those, as it never
|
|
116
|
-
// materializes the whole payload as a single string.
|
|
117
|
-
return await parseJsonStream(Readable.from([bytes]));
|
|
125
|
+
catch (e) {
|
|
126
|
+
ensureError(e);
|
|
127
|
+
throw new InvalidParameterError("The bytes are not valid UTF-8 encoded JSON", e);
|
|
118
128
|
}
|
|
119
|
-
return JSON.parse(json);
|
|
120
129
|
}
|
|
121
130
|
export { bytesToBigInt, bytesToNumber, numberToBytes } from "./number.js";
|
|
122
131
|
export { bytesToHexString, hexStringToBytes } from "./hex.js";
|
package/dist/src/bytes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bytes.js","sourceRoot":"","sources":["../../src/bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"bytes.js","sourceRoot":"","sources":["../../src/bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,IAAI,eAAe,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACrE,OAAO,EACL,kBAAkB,EAClB,6BAA6B,GAC9B,MAAM,oBAAoB,CAAC;AAE5B;;;;;GAKG;AACH,MAAM,UAAU,OAAO,CAAC,KAAc;IACpC,OAAO,KAAK,YAAY,UAAU,CAAC;AACrC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,KAAiB,EAAE,MAAc;IAC7D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5C,CAAC,CACF,CAAC;IACJ,CAAC;AACH,CAAC;AAED,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { CustomError } from "../error.js";
|
|
2
|
+
/**
|
|
3
|
+
* Discriminates the errors that ABI encoding can throw.
|
|
4
|
+
*/
|
|
5
|
+
export type AbiEncodingErrorKind = "parameters-length-mismatch" | "invalid-value" | "value-out-of-bounds" | "unsupported-type" | "duplicate-parameter-name" | "encoding-failed";
|
|
6
|
+
/**
|
|
7
|
+
* Base class of every error thrown while ABI-encoding values.
|
|
8
|
+
*
|
|
9
|
+
* Consumers should discriminate with `isAbiEncodingError` rather than
|
|
10
|
+
* `instanceof`, so that the checks keep working if more than one copy of this
|
|
11
|
+
* package ends up installed.
|
|
12
|
+
*/
|
|
13
|
+
export declare abstract class AbiEncodingError extends CustomError {
|
|
14
|
+
abstract readonly kind: AbiEncodingErrorKind;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Thrown when the number of values to encode doesn't match the number of
|
|
18
|
+
* parameters.
|
|
19
|
+
*/
|
|
20
|
+
export declare class AbiParametersLengthMismatchError extends AbiEncodingError {
|
|
21
|
+
readonly expectedLength: number;
|
|
22
|
+
readonly providedLength: number;
|
|
23
|
+
readonly kind: "parameters-length-mismatch";
|
|
24
|
+
constructor(expectedLength: number, providedLength: number);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Thrown when a value can't be encoded as its parameter's Solidity type.
|
|
28
|
+
*/
|
|
29
|
+
export declare class InvalidAbiValueError extends AbiEncodingError {
|
|
30
|
+
readonly path: string;
|
|
31
|
+
readonly solidityType: string;
|
|
32
|
+
readonly value: unknown;
|
|
33
|
+
readonly reason: string;
|
|
34
|
+
readonly kind: "invalid-value";
|
|
35
|
+
constructor(path: string, solidityType: string, value: unknown, reason: string);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Thrown when a numeric value is outside the range of its Solidity type.
|
|
39
|
+
*/
|
|
40
|
+
export declare class AbiValueOutOfBoundsError extends AbiEncodingError {
|
|
41
|
+
readonly path: string;
|
|
42
|
+
readonly solidityType: string;
|
|
43
|
+
readonly value: unknown;
|
|
44
|
+
readonly min: bigint;
|
|
45
|
+
readonly max: bigint;
|
|
46
|
+
readonly kind: "value-out-of-bounds";
|
|
47
|
+
constructor(path: string, solidityType: string, value: unknown, min: bigint, max: bigint);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Thrown when a parameter's Solidity type can't be encoded at all.
|
|
51
|
+
*/
|
|
52
|
+
export declare class UnsupportedAbiTypeError extends AbiEncodingError {
|
|
53
|
+
readonly path: string;
|
|
54
|
+
readonly solidityType: string;
|
|
55
|
+
readonly kind: "unsupported-type";
|
|
56
|
+
constructor(path: string, solidityType: string);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Thrown when two parameters, or two components of the same tuple, share a
|
|
60
|
+
* name.
|
|
61
|
+
*
|
|
62
|
+
* Only a hand-written ABI can carry duplicate names; solc never emits one. The
|
|
63
|
+
* encoder's own guard covers named parameters only, and names are stripped
|
|
64
|
+
* before it runs, so it never sees them.
|
|
65
|
+
*/
|
|
66
|
+
export declare class DuplicateAbiParameterNameError extends AbiEncodingError {
|
|
67
|
+
readonly path: string | undefined;
|
|
68
|
+
readonly duplicatedName: string;
|
|
69
|
+
readonly kind: "duplicate-parameter-name";
|
|
70
|
+
constructor(path: string | undefined, duplicatedName: string);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Thrown when the encoder refuses values that already passed validation.
|
|
74
|
+
*/
|
|
75
|
+
export declare class AbiEncodingFailedError extends AbiEncodingError {
|
|
76
|
+
readonly kind: "encoding-failed";
|
|
77
|
+
constructor(cause: Error);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The union of every error that ABI encoding can throw.
|
|
81
|
+
*/
|
|
82
|
+
export type AnyAbiEncodingError = AbiParametersLengthMismatchError | InvalidAbiValueError | AbiValueOutOfBoundsError | UnsupportedAbiTypeError | DuplicateAbiParameterNameError | AbiEncodingFailedError;
|
|
83
|
+
/**
|
|
84
|
+
* Checks whether a value is the ABI encoding error of the given kind.
|
|
85
|
+
*
|
|
86
|
+
* @param error The value to check.
|
|
87
|
+
* @param kind The kind of error to check for.
|
|
88
|
+
* @returns True if the value is an ABI encoding error of that kind.
|
|
89
|
+
*/
|
|
90
|
+
export declare function isAbiEncodingError<KindT extends AbiEncodingErrorKind>(error: unknown, kind: KindT): error is Extract<AnyAbiEncodingError, {
|
|
91
|
+
kind: KindT;
|
|
92
|
+
}>;
|
|
93
|
+
//# sourceMappingURL=abi.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abi.d.ts","sourceRoot":"","sources":["../../../src/errors/abi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAC5B,4BAA4B,GAC5B,eAAe,GACf,qBAAqB,GACrB,kBAAkB,GAClB,0BAA0B,GAC1B,iBAAiB,CAAC;AAEtB;;;;;;GAMG;AACH,8BAAsB,gBAAiB,SAAQ,WAAW;IACxD,kBAAyB,IAAI,EAAE,oBAAoB,CAAC;CACrD;AAED;;;GAGG;AACH,qBAAa,gCAAiC,SAAQ,gBAAgB;aAKlD,cAAc,EAAE,MAAM;aACtB,cAAc,EAAE,MAAM;IALxC,SAAgB,IAAI,EAAE,4BAA4B,CACnB;gBAGb,cAAc,EAAE,MAAM,EACtB,cAAc,EAAE,MAAM;CAMzC;AAED;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,gBAAgB;aAItC,IAAI,EAAE,MAAM;aACZ,YAAY,EAAE,MAAM;aACpB,KAAK,EAAE,OAAO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AAK,SAAS,oBAAoB,EACnE,KAAK,EAAE,OAAO,EACd,IAAI,EAAE,KAAK,GACV,KAAK,IAAI,OAAO,CAAC,mBAAmB,EAAE;IAAE,IAAI,EAAE,KAAK,CAAA;CAAE,CAAC,CAExD"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { CustomError } from "../error.js";
|
|
2
|
+
/**
|
|
3
|
+
* Base class of every error thrown while ABI-encoding values.
|
|
4
|
+
*
|
|
5
|
+
* Consumers should discriminate with `isAbiEncodingError` rather than
|
|
6
|
+
* `instanceof`, so that the checks keep working if more than one copy of this
|
|
7
|
+
* package ends up installed.
|
|
8
|
+
*/
|
|
9
|
+
export class AbiEncodingError extends CustomError {
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Thrown when the number of values to encode doesn't match the number of
|
|
13
|
+
* parameters.
|
|
14
|
+
*/
|
|
15
|
+
export class AbiParametersLengthMismatchError extends AbiEncodingError {
|
|
16
|
+
expectedLength;
|
|
17
|
+
providedLength;
|
|
18
|
+
kind = "parameters-length-mismatch";
|
|
19
|
+
constructor(expectedLength, providedLength) {
|
|
20
|
+
super(`Expected ${expectedLength} values to encode, but ${providedLength} were provided.`);
|
|
21
|
+
this.expectedLength = expectedLength;
|
|
22
|
+
this.providedLength = providedLength;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Thrown when a value can't be encoded as its parameter's Solidity type.
|
|
27
|
+
*/
|
|
28
|
+
export class InvalidAbiValueError extends AbiEncodingError {
|
|
29
|
+
path;
|
|
30
|
+
solidityType;
|
|
31
|
+
value;
|
|
32
|
+
reason;
|
|
33
|
+
kind = "invalid-value";
|
|
34
|
+
constructor(path, solidityType, value, reason) {
|
|
35
|
+
super(`The value provided for the parameter "${path}" of type "${solidityType}" cannot be encoded. Reason: ${reason}.`);
|
|
36
|
+
this.path = path;
|
|
37
|
+
this.solidityType = solidityType;
|
|
38
|
+
this.value = value;
|
|
39
|
+
this.reason = reason;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Thrown when a numeric value is outside the range of its Solidity type.
|
|
44
|
+
*/
|
|
45
|
+
export class AbiValueOutOfBoundsError extends AbiEncodingError {
|
|
46
|
+
path;
|
|
47
|
+
solidityType;
|
|
48
|
+
value;
|
|
49
|
+
min;
|
|
50
|
+
max;
|
|
51
|
+
kind = "value-out-of-bounds";
|
|
52
|
+
constructor(path, solidityType, value, min, max) {
|
|
53
|
+
super(`The value provided for the parameter "${path}" is out of bounds for the type "${solidityType}". Expected a value between ${min} and ${max}.`);
|
|
54
|
+
this.path = path;
|
|
55
|
+
this.solidityType = solidityType;
|
|
56
|
+
this.value = value;
|
|
57
|
+
this.min = min;
|
|
58
|
+
this.max = max;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Thrown when a parameter's Solidity type can't be encoded at all.
|
|
63
|
+
*/
|
|
64
|
+
export class UnsupportedAbiTypeError extends AbiEncodingError {
|
|
65
|
+
path;
|
|
66
|
+
solidityType;
|
|
67
|
+
kind = "unsupported-type";
|
|
68
|
+
constructor(path, solidityType) {
|
|
69
|
+
super(`The type "${solidityType}" of the parameter "${path}" is not supported.`);
|
|
70
|
+
this.path = path;
|
|
71
|
+
this.solidityType = solidityType;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Thrown when two parameters, or two components of the same tuple, share a
|
|
76
|
+
* name.
|
|
77
|
+
*
|
|
78
|
+
* Only a hand-written ABI can carry duplicate names; solc never emits one. The
|
|
79
|
+
* encoder's own guard covers named parameters only, and names are stripped
|
|
80
|
+
* before it runs, so it never sees them.
|
|
81
|
+
*/
|
|
82
|
+
export class DuplicateAbiParameterNameError extends AbiEncodingError {
|
|
83
|
+
path;
|
|
84
|
+
duplicatedName;
|
|
85
|
+
kind = "duplicate-parameter-name";
|
|
86
|
+
constructor(
|
|
87
|
+
// undefined when the duplicate is between top-level parameters
|
|
88
|
+
path, duplicatedName) {
|
|
89
|
+
super(path === undefined
|
|
90
|
+
? `Two or more parameters are named "${duplicatedName}".`
|
|
91
|
+
: `Two or more components of the parameter "${path}" are named "${duplicatedName}".`);
|
|
92
|
+
this.path = path;
|
|
93
|
+
this.duplicatedName = duplicatedName;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Thrown when the encoder refuses values that already passed validation.
|
|
98
|
+
*/
|
|
99
|
+
export class AbiEncodingFailedError extends AbiEncodingError {
|
|
100
|
+
kind = "encoding-failed";
|
|
101
|
+
constructor(cause) {
|
|
102
|
+
// The encoder's messages don't end with a period, so one is added to keep
|
|
103
|
+
// this message punctuated like the rest.
|
|
104
|
+
super(`The values could not be ABI-encoded: ${cause.message}.`, cause);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Checks whether a value is the ABI encoding error of the given kind.
|
|
109
|
+
*
|
|
110
|
+
* @param error The value to check.
|
|
111
|
+
* @param kind The kind of error to check for.
|
|
112
|
+
* @returns True if the value is an ABI encoding error of that kind.
|
|
113
|
+
*/
|
|
114
|
+
export function isAbiEncodingError(error, kind) {
|
|
115
|
+
return error instanceof Error && "kind" in error && error.kind === kind;
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=abi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abi.js","sourceRoot":"","sources":["../../../src/errors/abi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAa1C;;;;;;GAMG;AACH,MAAM,OAAgB,gBAAiB,SAAQ,WAAW;CAEzD;AAED;;;GAGG;AACH,MAAM,OAAO,gCAAiC,SAAQ,gBAAgB;IAKlD;IACA;IALF,IAAI,GAClB,4BAA4B,CAAC;IAE/B,YACkB,cAAsB,EACtB,cAAsB;QAEtC,KAAK,CACH,YAAY,cAAc,0BAA0B,cAAc,iBAAiB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