@nomicfoundation/hardhat-utils 4.3.0 → 4.4.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 +10 -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/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/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/package.json +5 -3
- package/src/abi.ts +199 -0
- package/src/errors/abi.ts +156 -0
- package/src/internal/abi.ts +431 -0
- package/src/internal/bytes.ts +4 -4
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { getIntTypeRange, parseIntType } from "../abi.js";
|
|
2
|
+
import { AbiValueOutOfBoundsError, DuplicateAbiParameterNameError, InvalidAbiValueError, UnsupportedAbiTypeError, } from "../errors/abi.js";
|
|
3
|
+
import { isAddress, isValidChecksumAddress } from "../eth.js";
|
|
4
|
+
import { getPrefixedHexString, getUnprefixedHexString, hexStringToBytes, isHexString, isPrefixedHexString, } from "../hex.js";
|
|
5
|
+
import { isObject } from "../lang.js";
|
|
6
|
+
// In Solidity the rightmost suffix is the outermost array, so `uint256[2][]` is
|
|
7
|
+
// a dynamic array of `uint256[2]`. The greedy `.+` matches that last suffix.
|
|
8
|
+
const ARRAY_REGEX = /^(.+)(\[(\d+)?\])$/;
|
|
9
|
+
const BYTES_N_REGEX = /^bytes([0-9]{1,2})$/;
|
|
10
|
+
const DECIMAL_REGEX = /^\d+$/;
|
|
11
|
+
const HEX_REGEX = /^0x[0-9a-f]+$/i;
|
|
12
|
+
/**
|
|
13
|
+
* Throws if two of the given parameters share a name.
|
|
14
|
+
*
|
|
15
|
+
* This is checked against the parameter list rather than the value, so that the
|
|
16
|
+
* array and object input shapes fail the same way.
|
|
17
|
+
*
|
|
18
|
+
* @param parameters The parameters to check.
|
|
19
|
+
* @param path The path of their container, or undefined at the top level.
|
|
20
|
+
* @throws DuplicateAbiParameterNameError If a name is repeated.
|
|
21
|
+
*/
|
|
22
|
+
export function assertNoDuplicateAbiParameterNames(parameters, path) {
|
|
23
|
+
const seen = new Set();
|
|
24
|
+
for (const { name } of parameters) {
|
|
25
|
+
if (name === undefined || name === "") {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (seen.has(name)) {
|
|
29
|
+
throw new DuplicateAbiParameterNameError(path, name);
|
|
30
|
+
}
|
|
31
|
+
seen.add(name);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Removes the names of a list of ABI parameters, recursively.
|
|
36
|
+
*
|
|
37
|
+
* The encoder derives the shape of the value it expects from whether the
|
|
38
|
+
* parameters are named: named ones take an object keyed by name, unnamed ones
|
|
39
|
+
* take a positional array. Dropping the names makes every aggregate positional,
|
|
40
|
+
* at every nesting level.
|
|
41
|
+
*
|
|
42
|
+
* @param parameters The parameters to strip.
|
|
43
|
+
* @returns The same parameters, without their names.
|
|
44
|
+
*/
|
|
45
|
+
export function toUnnamedAbiParameters(parameters) {
|
|
46
|
+
return parameters.map(({ type, components }) => components === undefined
|
|
47
|
+
? { type }
|
|
48
|
+
: { type, components: toUnnamedAbiParameters(components) });
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Converts a value into the shape that the encoder expects for a parameter,
|
|
52
|
+
* validating it against the parameter's Solidity type.
|
|
53
|
+
*
|
|
54
|
+
* @param parameter The ABI parameter to normalize the value for.
|
|
55
|
+
* @param value The value to normalize.
|
|
56
|
+
* @param path The path of the parameter, used to report errors.
|
|
57
|
+
* @returns The normalized value.
|
|
58
|
+
* @throws InvalidAbiValueError If the value can't be encoded as the type.
|
|
59
|
+
* @throws AbiValueOutOfBoundsError If a numeric value is out of the type's range.
|
|
60
|
+
* @throws UnsupportedAbiTypeError If the type itself can't be encoded.
|
|
61
|
+
*/
|
|
62
|
+
export async function normalizeAbiValue(parameter, value, path) {
|
|
63
|
+
const { type } = parameter;
|
|
64
|
+
// Arrays must be checked first, as the check is recursive.
|
|
65
|
+
const arrayMatch = ARRAY_REGEX.exec(type);
|
|
66
|
+
if (arrayMatch !== null) {
|
|
67
|
+
return await normalizeArray(parameter, value, path, arrayMatch);
|
|
68
|
+
}
|
|
69
|
+
if (type === "tuple") {
|
|
70
|
+
return await normalizeTuple(parameter, value, path);
|
|
71
|
+
}
|
|
72
|
+
if (type === "address") {
|
|
73
|
+
return await normalizeAddress(value, path);
|
|
74
|
+
}
|
|
75
|
+
if (type === "bool") {
|
|
76
|
+
return normalizeBool(value, path);
|
|
77
|
+
}
|
|
78
|
+
if (type === "string") {
|
|
79
|
+
if (typeof value !== "string") {
|
|
80
|
+
throw new InvalidAbiValueError(path, type, value, "invalid string value");
|
|
81
|
+
}
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
if (type === "bytes") {
|
|
85
|
+
return normalizeBytes(value, path, type, undefined);
|
|
86
|
+
}
|
|
87
|
+
const bytesNMatch = BYTES_N_REGEX.exec(type);
|
|
88
|
+
if (bytesNMatch !== null) {
|
|
89
|
+
const length = Number(bytesNMatch[1]);
|
|
90
|
+
if (length < 1 || length > 32) {
|
|
91
|
+
throw new UnsupportedAbiTypeError(path, type);
|
|
92
|
+
}
|
|
93
|
+
return normalizeBytes(value, path, type, length);
|
|
94
|
+
}
|
|
95
|
+
const intType = parseIntType(type);
|
|
96
|
+
if (intType !== undefined) {
|
|
97
|
+
return normalizeInt(value, path, type, intType);
|
|
98
|
+
}
|
|
99
|
+
throw new UnsupportedAbiTypeError(path, type);
|
|
100
|
+
}
|
|
101
|
+
async function normalizeArray(parameter, value, path, arrayMatch) {
|
|
102
|
+
const { type } = parameter;
|
|
103
|
+
const elementType = arrayMatch[1];
|
|
104
|
+
const fixedLength = arrayMatch[3];
|
|
105
|
+
if (!Array.isArray(value)) {
|
|
106
|
+
throw new InvalidAbiValueError(path, type, value, "invalid array value, expected a JS array");
|
|
107
|
+
}
|
|
108
|
+
if (fixedLength !== undefined && value.length !== Number(fixedLength)) {
|
|
109
|
+
throw new InvalidAbiValueError(path, type, value, `invalid array length, expected ${fixedLength} elements but got ${value.length}`);
|
|
110
|
+
}
|
|
111
|
+
const elementParameter = { ...parameter, type: elementType };
|
|
112
|
+
const normalized = [];
|
|
113
|
+
for (const [i, element] of value.entries()) {
|
|
114
|
+
normalized.push(await normalizeAbiValue(elementParameter, element, `${path}[${i}]`));
|
|
115
|
+
}
|
|
116
|
+
return normalized;
|
|
117
|
+
}
|
|
118
|
+
async function normalizeTuple(parameter, value, path) {
|
|
119
|
+
const { type, components } = parameter;
|
|
120
|
+
// The encoder rejects zero-sized tuples, as arrays of them can be used to
|
|
121
|
+
// DoS the decoder.
|
|
122
|
+
if (components === undefined || components.length === 0) {
|
|
123
|
+
throw new UnsupportedAbiTypeError(path, type);
|
|
124
|
+
}
|
|
125
|
+
assertNoDuplicateAbiParameterNames(components, path);
|
|
126
|
+
if (Array.isArray(value)) {
|
|
127
|
+
if (value.length !== components.length) {
|
|
128
|
+
throw new InvalidAbiValueError(path, type, value, `invalid tuple length, expected ${components.length} components but got ${value.length}`);
|
|
129
|
+
}
|
|
130
|
+
const normalized = [];
|
|
131
|
+
for (const [i, component] of components.entries()) {
|
|
132
|
+
normalized.push(await normalizeAbiValue(component, value[i], component.name === undefined
|
|
133
|
+
? `${path}[${i}]`
|
|
134
|
+
: `${path}.${component.name}`));
|
|
135
|
+
}
|
|
136
|
+
return normalized;
|
|
137
|
+
}
|
|
138
|
+
if (isObject(value)) {
|
|
139
|
+
const normalized = [];
|
|
140
|
+
for (const component of components) {
|
|
141
|
+
const { name } = component;
|
|
142
|
+
if (name === undefined || name === "") {
|
|
143
|
+
throw new InvalidAbiValueError(path, type, value, "invalid tuple value, it has unnamed components so it must be provided as an array");
|
|
144
|
+
}
|
|
145
|
+
const componentPath = `${path}.${name}`;
|
|
146
|
+
if (!Object.hasOwn(value, name)) {
|
|
147
|
+
throw new InvalidAbiValueError(componentPath, component.type, undefined, `missing value for the tuple component "${name}"`);
|
|
148
|
+
}
|
|
149
|
+
normalized.push(await normalizeAbiValue(component, value[name], componentPath));
|
|
150
|
+
}
|
|
151
|
+
return normalized;
|
|
152
|
+
}
|
|
153
|
+
throw new InvalidAbiValueError(path, type, value, "invalid tuple value, expected an array or an object");
|
|
154
|
+
}
|
|
155
|
+
function normalizeBool(value, path) {
|
|
156
|
+
if (typeof value === "boolean") {
|
|
157
|
+
return value;
|
|
158
|
+
}
|
|
159
|
+
// Command line arguments always arrive as strings, so the two Solidity
|
|
160
|
+
// literals are accepted. Any other string is rejected.
|
|
161
|
+
if (value === "true") {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
if (value === "false") {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
throw new InvalidAbiValueError(path, "bool", value, "invalid boolean value");
|
|
168
|
+
}
|
|
169
|
+
async function normalizeAddress(value, path) {
|
|
170
|
+
if (typeof value !== "string") {
|
|
171
|
+
throw new InvalidAbiValueError(path, "address", value, "invalid address value, expected a string");
|
|
172
|
+
}
|
|
173
|
+
// The prefix is re-added in lowercase, so that a "0X"-prefixed address still
|
|
174
|
+
// matches the checksummed form it is compared against below.
|
|
175
|
+
const address = getPrefixedHexString(getUnprefixedHexString(value));
|
|
176
|
+
if (!isAddress(address)) {
|
|
177
|
+
throw new InvalidAbiValueError(path, "address", value, "invalid address value");
|
|
178
|
+
}
|
|
179
|
+
// A mixed-case address carries an EIP-55 checksum, so it is validated to
|
|
180
|
+
// catch mistyped addresses here instead of failing later, somewhere else.
|
|
181
|
+
// An all-lowercase or all-uppercase one carries no checksum to validate.
|
|
182
|
+
const hexAddress = getUnprefixedHexString(address);
|
|
183
|
+
const isCaseless = hexAddress === hexAddress.toLowerCase() ||
|
|
184
|
+
hexAddress === hexAddress.toUpperCase();
|
|
185
|
+
if (!isCaseless && !(await isValidChecksumAddress(address))) {
|
|
186
|
+
throw new InvalidAbiValueError(path, "address", value, "invalid address checksum");
|
|
187
|
+
}
|
|
188
|
+
return address.toLowerCase();
|
|
189
|
+
}
|
|
190
|
+
function normalizeBytes(value, path, type, length) {
|
|
191
|
+
let bytes;
|
|
192
|
+
if (value instanceof Uint8Array) {
|
|
193
|
+
bytes = value;
|
|
194
|
+
// The "0x" prefix is required, as an unprefixed string is more likely to
|
|
195
|
+
// be a value that was meant to be something other than bytes.
|
|
196
|
+
}
|
|
197
|
+
else if (typeof value === "string" &&
|
|
198
|
+
isPrefixedHexString(value) &&
|
|
199
|
+
isHexString(value)) {
|
|
200
|
+
const unprefixed = getUnprefixedHexString(value);
|
|
201
|
+
if (unprefixed.length % 2 !== 0) {
|
|
202
|
+
throw new InvalidAbiValueError(path, type, value, "invalid bytes value, the hex string has an odd length");
|
|
203
|
+
}
|
|
204
|
+
bytes =
|
|
205
|
+
unprefixed.length === 0 ? new Uint8Array() : hexStringToBytes(unprefixed);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
throw new InvalidAbiValueError(path, type, value, 'invalid bytes value, expected a "0x"-prefixed hex string or a Uint8Array');
|
|
209
|
+
}
|
|
210
|
+
if (length !== undefined && bytes.length !== length) {
|
|
211
|
+
throw new InvalidAbiValueError(path, type, value, `invalid bytes length, expected ${length} bytes but got ${bytes.length}`);
|
|
212
|
+
}
|
|
213
|
+
return bytes;
|
|
214
|
+
}
|
|
215
|
+
function normalizeInt(value, path, type, intType) {
|
|
216
|
+
let normalized;
|
|
217
|
+
if (typeof value === "bigint") {
|
|
218
|
+
normalized = value;
|
|
219
|
+
}
|
|
220
|
+
else if (typeof value === "number") {
|
|
221
|
+
if (!Number.isSafeInteger(value)) {
|
|
222
|
+
throw new InvalidAbiValueError(path, type, value, "invalid numeric value, it is not a safe integer; use a bigint or a string instead");
|
|
223
|
+
}
|
|
224
|
+
normalized = BigInt(value);
|
|
225
|
+
}
|
|
226
|
+
else if (typeof value === "string") {
|
|
227
|
+
normalized = parseIntValue(value, path, type);
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
throw new InvalidAbiValueError(path, type, value, "invalid numeric value");
|
|
231
|
+
}
|
|
232
|
+
const { min, max } = getIntTypeRange(intType);
|
|
233
|
+
if (normalized < min || normalized > max) {
|
|
234
|
+
throw new AbiValueOutOfBoundsError(path, type, value, min, max);
|
|
235
|
+
}
|
|
236
|
+
return normalized;
|
|
237
|
+
}
|
|
238
|
+
function parseIntValue(value, path, type) {
|
|
239
|
+
// BigInt() throws on a signed hex string, so the sign is handled here.
|
|
240
|
+
const negative = value.startsWith("-");
|
|
241
|
+
const magnitude = negative ? value.slice(1) : value;
|
|
242
|
+
if (!DECIMAL_REGEX.test(magnitude) && !HEX_REGEX.test(magnitude)) {
|
|
243
|
+
throw new InvalidAbiValueError(path, type, value, "invalid numeric value");
|
|
244
|
+
}
|
|
245
|
+
const parsed = BigInt(magnitude);
|
|
246
|
+
return negative ? -parsed : parsed;
|
|
247
|
+
}
|
|
248
|
+
//# sourceMappingURL=abi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abi.js","sourceRoot":"","sources":["../../../src/internal/abi.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC1D,OAAO,EACL,wBAAwB,EACxB,8BAA8B,EAC9B,oBAAoB,EACpB,uBAAuB,GACxB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,SAAS,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AAC9D,OAAO,EACL,oBAAoB,EACpB,sBAAsB,EACtB,gBAAgB,EAChB,WAAW,EACX,m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EAAE,CAAC;QACrC,UAAU,GAAG,aAAa,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IAChD,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,oBAAoB,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,uBAAuB,CAAC,CAAC;IAC7E,CAAC;IAED,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;IAC9C,IAAI,UAAU,GAAG,GAAG,IAAI,UAAU,GAAG,GAAG,EAAE,CAAC;QACzC,MAAM,IAAI,wBAAwB,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;IAClE,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,SAAS,aAAa,CAAC,KAAa,EAAE,IAAY,EAAE,IAAY;IAC9D,uEAAuE;IACvE,MAAM,QAAQ,GAAG,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IACvC,MAAM,SAAS,GAAG,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IAEpD,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;QACjE,MAAM,IAAI,oBAAoB,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,uBAAuB,CAAC,CAAC;IAC7E,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;IACjC,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC;AACrC,CAAC"}
|
|
@@ -13,10 +13,10 @@ export async function parseJsonStream(stream) {
|
|
|
13
13
|
if (streamParserJson === undefined) {
|
|
14
14
|
streamParserJson = await import("@streamparser/json-node");
|
|
15
15
|
}
|
|
16
|
-
//
|
|
17
|
-
//
|
|
16
|
+
// We only emit the root value, as it's the only one we use, and emitting
|
|
17
|
+
// every nested value is much slower.
|
|
18
18
|
const jsonParser = new streamParserJson.JSONParser({
|
|
19
|
-
|
|
19
|
+
paths: ["$"],
|
|
20
20
|
});
|
|
21
21
|
const result = await pipeline(stream, jsonParser, async (elements) => {
|
|
22
22
|
let value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bytes.js","sourceRoot":"","sources":["../../../src/internal/bytes.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD,qEAAqE;AACrE,6CAA6C;AAC7C,IAAI,gBAAqD,CAAC;AAE1D;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAI,MAAgB;IACvD,IAAI,gBAAgB,KAAK,SAAS,EAAE,CAAC;QACnC,gBAAgB,GAAG,MAAM,MAAM,CAAC,yBAAyB,CAAC,CAAC;IAC7D,CAAC;IAED,
|
|
1
|
+
{"version":3,"file":"bytes.js","sourceRoot":"","sources":["../../../src/internal/bytes.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD,qEAAqE;AACrE,6CAA6C;AAC7C,IAAI,gBAAqD,CAAC;AAE1D;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAI,MAAgB;IACvD,IAAI,gBAAgB,KAAK,SAAS,EAAE,CAAC;QACnC,gBAAgB,GAAG,MAAM,MAAM,CAAC,yBAAyB,CAAC,CAAC;IAC7D,CAAC;IAED,yEAAyE;IACzE,qCAAqC;IACrC,MAAM,UAAU,GAAG,IAAI,gBAAgB,CAAC,UAAU,CAAC;QACjD,KAAK,EAAE,CAAC,GAAG,CAAC;KACb,CAAC,CAAC;IAEH,MAAM,MAAM,GAAkB,MAAM,QAAQ,CAC1C,MAAM,EACN,UAAU,EACV,KAAK,EACH,QAA2D,EACjC,EAAE;QAC5B,IAAI,KAGS,CAAC;QACd,IAAI,KAAK,EAAE,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;YACrC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QACxB,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC,CACF,CAAC;IAEF,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;IAC7B,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,aAAa,CAAC,OAAmB;IAC/C,MAAM,GAAG,GAAG,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC9C,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,IAAI,CAAC,GAAG,CAAC,CAAC;IAEV,OAAO,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;QAC1B,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;YACrC,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,EAAE,QAAQ,CAAC;QACxB,CAAC;aAAM,IAAI,QAAQ,GAAG,CAAC,EAAE,CAAC;YACxB,QAAQ,GAAG,GAAG,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC;QAC/B,CAAC;aAAM,CAAC;YACN,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;QACf,CAAC;IACH,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nomicfoundation/hardhat-utils",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.0",
|
|
4
4
|
"description": "Utilities for Hardhat and its plugins",
|
|
5
5
|
"homepage": "https://github.com/NomicFoundation/hardhat/tree/main/packages/hardhat-utils",
|
|
6
6
|
"repository": {
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"type": "module",
|
|
14
14
|
"exports": {
|
|
15
|
+
"./abi": "./dist/src/abi.js",
|
|
15
16
|
"./bigint": "./dist/src/bigint.js",
|
|
16
17
|
"./bytecode": "./dist/src/bytecode.js",
|
|
17
18
|
"./bytes": "./dist/src/bytes.js",
|
|
@@ -62,17 +63,18 @@
|
|
|
62
63
|
"c8": "^12.0.0",
|
|
63
64
|
"eslint": "9.39.5",
|
|
64
65
|
"expect-type": "^1.4.0",
|
|
65
|
-
"prettier": "3.9.
|
|
66
|
+
"prettier": "3.9.8",
|
|
66
67
|
"rimraf": "^6.1.3",
|
|
67
68
|
"tsx": "^4.23.1",
|
|
68
69
|
"typescript": "~6.0.3"
|
|
69
70
|
},
|
|
70
71
|
"dependencies": {
|
|
71
|
-
"@streamparser/json-node": "^0.0.
|
|
72
|
+
"@streamparser/json-node": "^0.0.26",
|
|
72
73
|
"env-paths": "^4.0.0",
|
|
73
74
|
"ethereum-cryptography": "^3.0.0",
|
|
74
75
|
"fast-equals": "^6.0.0",
|
|
75
76
|
"json-stream-stringify": "^3.1.7",
|
|
77
|
+
"micro-eth-signer": "^0.19.0",
|
|
76
78
|
"rfdc": "^1.3.1",
|
|
77
79
|
"undici": "^7.29.0"
|
|
78
80
|
},
|
package/src/abi.ts
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import type { PrefixedHexString } from "./hex.js";
|
|
2
|
+
import type { deployContract as DeployContractT } from "micro-eth-signer/advanced/abi.js";
|
|
3
|
+
|
|
4
|
+
import { ensureError } from "./error.js";
|
|
5
|
+
import {
|
|
6
|
+
AbiEncodingFailedError,
|
|
7
|
+
AbiParametersLengthMismatchError,
|
|
8
|
+
} from "./errors/abi.js";
|
|
9
|
+
import { getPrefixedHexString } from "./hex.js";
|
|
10
|
+
import {
|
|
11
|
+
assertNoDuplicateAbiParameterNames,
|
|
12
|
+
normalizeAbiValue,
|
|
13
|
+
toUnnamedAbiParameters,
|
|
14
|
+
} from "./internal/abi.js";
|
|
15
|
+
|
|
16
|
+
// We don't load micro-eth-signer on startup because it's slow to initialize,
|
|
17
|
+
// and most callers of this module never encode anything.
|
|
18
|
+
let deployContractImpl: typeof DeployContractT | undefined;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A parameter of an ABI fragment.
|
|
22
|
+
*/
|
|
23
|
+
export interface AbiParameter {
|
|
24
|
+
readonly name?: string;
|
|
25
|
+
readonly type: string;
|
|
26
|
+
readonly components?: readonly AbiParameter[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* An entry of a contract's ABI. Only the fields needed to encode values are
|
|
31
|
+
* modeled.
|
|
32
|
+
*/
|
|
33
|
+
export interface AbiFragment {
|
|
34
|
+
readonly type?: string;
|
|
35
|
+
readonly name?: string;
|
|
36
|
+
readonly inputs?: readonly AbiParameter[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The signedness and size of a Solidity integer type.
|
|
41
|
+
*/
|
|
42
|
+
export interface IntType {
|
|
43
|
+
readonly signed: boolean;
|
|
44
|
+
readonly bits: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The inclusive range of values that a Solidity integer type can hold.
|
|
49
|
+
*/
|
|
50
|
+
export interface IntTypeRange {
|
|
51
|
+
readonly min: bigint;
|
|
52
|
+
readonly max: bigint;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const INT_TYPE_PATTERN = /^(u?)int([1-9]\d*)?$/;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Parses a Solidity integer type name, like `uint256` or `int128`, into its
|
|
59
|
+
* signedness and size in bits. As in Solidity, `uint` and `int` are accepted as
|
|
60
|
+
* aliases of the 256-bit types.
|
|
61
|
+
*
|
|
62
|
+
* @param type The type name to parse.
|
|
63
|
+
* @returns The parsed type, or `undefined` if it isn't a Solidity integer type.
|
|
64
|
+
*/
|
|
65
|
+
export function parseIntType(type: string): IntType | undefined {
|
|
66
|
+
const match = INT_TYPE_PATTERN.exec(type);
|
|
67
|
+
|
|
68
|
+
if (match === null) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const bits = match[2] === undefined ? 256 : Number(match[2]);
|
|
73
|
+
|
|
74
|
+
if (bits > 256 || bits % 8 !== 0) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return { signed: match[1] === "", bits };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Returns the inclusive range of values that a Solidity integer type can hold.
|
|
83
|
+
*
|
|
84
|
+
* @param intType The integer type, as returned by {@link parseIntType}.
|
|
85
|
+
* @returns The minimum and maximum values of the type.
|
|
86
|
+
*/
|
|
87
|
+
export function getIntTypeRange({ signed, bits }: IntType): IntTypeRange {
|
|
88
|
+
if (signed) {
|
|
89
|
+
const bound = 2n ** BigInt(bits - 1);
|
|
90
|
+
return { min: -bound, max: bound - 1n };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return { min: 0n, max: 2n ** BigInt(bits) - 1n };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Returns the parameters of the constructor declared in an ABI.
|
|
98
|
+
*
|
|
99
|
+
* An ABI that declares no constructor yields an empty array, matching the
|
|
100
|
+
* Solidity semantics of an implicit parameterless constructor.
|
|
101
|
+
*
|
|
102
|
+
* @param abi The ABI to read the constructor from.
|
|
103
|
+
* @returns The constructor's parameters.
|
|
104
|
+
*/
|
|
105
|
+
export function getAbiConstructorParameters(
|
|
106
|
+
abi: readonly AbiFragment[],
|
|
107
|
+
): readonly AbiParameter[] {
|
|
108
|
+
return abi.find(({ type }) => type === "constructor")?.inputs ?? [];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* ABI-encodes a list of values, as Solidity encodes a constructor's arguments
|
|
113
|
+
* (i.e. without a function selector).
|
|
114
|
+
*
|
|
115
|
+
* Values are accepted in the same shapes that most Ethereum libraries accept:
|
|
116
|
+
* integers as bigints, safe numbers, or decimal or hex strings; `bytes` as hex
|
|
117
|
+
* strings or `Uint8Array`s; tuples as arrays or as objects keyed by component
|
|
118
|
+
* name; and addresses with or without the "0x" prefix.
|
|
119
|
+
*
|
|
120
|
+
* @param parameters The ABI parameters to encode the values as.
|
|
121
|
+
* @param values The values to encode, in the order of the parameters.
|
|
122
|
+
* @returns The encoded values as a prefixed hex string, or "0x" if there are none.
|
|
123
|
+
* @throws AbiParametersLengthMismatchError If the counts don't match.
|
|
124
|
+
* @throws InvalidAbiValueError If a value can't be encoded as its type.
|
|
125
|
+
* @throws AbiValueOutOfBoundsError If a numeric value is out of its type's range.
|
|
126
|
+
* @throws UnsupportedAbiTypeError If a parameter's type can't be encoded.
|
|
127
|
+
* @throws AbiEncodingFailedError If the encoder fails for any other reason.
|
|
128
|
+
*/
|
|
129
|
+
export async function encodeAbiParameters(
|
|
130
|
+
parameters: readonly AbiParameter[],
|
|
131
|
+
values: readonly unknown[],
|
|
132
|
+
): Promise<PrefixedHexString> {
|
|
133
|
+
if (parameters.length !== values.length) {
|
|
134
|
+
throw new AbiParametersLengthMismatchError(
|
|
135
|
+
parameters.length,
|
|
136
|
+
values.length,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Returning here also keeps micro-eth-signer unloaded.
|
|
141
|
+
if (parameters.length === 0) {
|
|
142
|
+
return "0x";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
assertNoDuplicateAbiParameterNames(parameters);
|
|
146
|
+
|
|
147
|
+
// The values are normalized sequentially so that, when more than one is
|
|
148
|
+
// invalid, the error reported is always the one of the first.
|
|
149
|
+
const normalizedValues = [];
|
|
150
|
+
for (const [i, parameter] of parameters.entries()) {
|
|
151
|
+
normalizedValues.push(
|
|
152
|
+
await normalizeAbiValue(parameter, values[i], parameter.name ?? `[${i}]`),
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (deployContractImpl === undefined) {
|
|
157
|
+
({ deployContract: deployContractImpl } =
|
|
158
|
+
await import("micro-eth-signer/advanced/abi.js"));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// The encoder takes the bare value when there's a single parameter, and a
|
|
162
|
+
// positional array otherwise. The array is positional at every level because
|
|
163
|
+
// toUnnamedAbiParameters drops the names.
|
|
164
|
+
const value =
|
|
165
|
+
parameters.length === 1 ? normalizedValues[0] : normalizedValues;
|
|
166
|
+
|
|
167
|
+
// NOTE: this must not be annotated nor frozen with `as const`. With `type`
|
|
168
|
+
// widened to `string`, micro-eth-signer's generic overload resolves to its
|
|
169
|
+
// untyped form, which is what we want for a dynamically built ABI.
|
|
170
|
+
const abi = [
|
|
171
|
+
{ type: "constructor", inputs: toUnnamedAbiParameters(parameters) },
|
|
172
|
+
];
|
|
173
|
+
|
|
174
|
+
let encoded: string;
|
|
175
|
+
try {
|
|
176
|
+
// deployContract returns `<bytecode><encoded args>`, so we pass no bytecode.
|
|
177
|
+
encoded = deployContractImpl(abi, "0x", value);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
// Reached by types that pass validation but the encoder still refuses,
|
|
180
|
+
// such as `uint256[0][]`, whose zero-size element it treats as a DoS risk.
|
|
181
|
+
ensureError(error);
|
|
182
|
+
throw new AbiEncodingFailedError(error);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return getPrefixedHexString(encoded);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export {
|
|
189
|
+
AbiEncodingError,
|
|
190
|
+
AbiEncodingFailedError,
|
|
191
|
+
AbiParametersLengthMismatchError,
|
|
192
|
+
AbiValueOutOfBoundsError,
|
|
193
|
+
DuplicateAbiParameterNameError,
|
|
194
|
+
InvalidAbiValueError,
|
|
195
|
+
UnsupportedAbiTypeError,
|
|
196
|
+
isAbiEncodingError,
|
|
197
|
+
type AbiEncodingErrorKind,
|
|
198
|
+
type AnyAbiEncodingError,
|
|
199
|
+
} from "./errors/abi.js";
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { CustomError } from "../error.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Discriminates the errors that ABI encoding can throw.
|
|
5
|
+
*/
|
|
6
|
+
export type AbiEncodingErrorKind =
|
|
7
|
+
| "parameters-length-mismatch"
|
|
8
|
+
| "invalid-value"
|
|
9
|
+
| "value-out-of-bounds"
|
|
10
|
+
| "unsupported-type"
|
|
11
|
+
| "duplicate-parameter-name"
|
|
12
|
+
| "encoding-failed";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Base class of every error thrown while ABI-encoding values.
|
|
16
|
+
*
|
|
17
|
+
* Consumers should discriminate with `isAbiEncodingError` rather than
|
|
18
|
+
* `instanceof`, so that the checks keep working if more than one copy of this
|
|
19
|
+
* package ends up installed.
|
|
20
|
+
*/
|
|
21
|
+
export abstract class AbiEncodingError extends CustomError {
|
|
22
|
+
public abstract readonly kind: AbiEncodingErrorKind;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Thrown when the number of values to encode doesn't match the number of
|
|
27
|
+
* parameters.
|
|
28
|
+
*/
|
|
29
|
+
export class AbiParametersLengthMismatchError extends AbiEncodingError {
|
|
30
|
+
public readonly kind: "parameters-length-mismatch" =
|
|
31
|
+
"parameters-length-mismatch";
|
|
32
|
+
|
|
33
|
+
constructor(
|
|
34
|
+
public readonly expectedLength: number,
|
|
35
|
+
public readonly providedLength: number,
|
|
36
|
+
) {
|
|
37
|
+
super(
|
|
38
|
+
`Expected ${expectedLength} values to encode, but ${providedLength} were provided.`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Thrown when a value can't be encoded as its parameter's Solidity type.
|
|
45
|
+
*/
|
|
46
|
+
export class InvalidAbiValueError extends AbiEncodingError {
|
|
47
|
+
public readonly kind: "invalid-value" = "invalid-value";
|
|
48
|
+
|
|
49
|
+
constructor(
|
|
50
|
+
public readonly path: string,
|
|
51
|
+
public readonly solidityType: string,
|
|
52
|
+
public readonly value: unknown,
|
|
53
|
+
public readonly reason: string,
|
|
54
|
+
) {
|
|
55
|
+
super(
|
|
56
|
+
`The value provided for the parameter "${path}" of type "${solidityType}" cannot be encoded. Reason: ${reason}.`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Thrown when a numeric value is outside the range of its Solidity type.
|
|
63
|
+
*/
|
|
64
|
+
export class AbiValueOutOfBoundsError extends AbiEncodingError {
|
|
65
|
+
public readonly kind: "value-out-of-bounds" = "value-out-of-bounds";
|
|
66
|
+
|
|
67
|
+
constructor(
|
|
68
|
+
public readonly path: string,
|
|
69
|
+
public readonly solidityType: string,
|
|
70
|
+
public readonly value: unknown,
|
|
71
|
+
public readonly min: bigint,
|
|
72
|
+
public readonly max: bigint,
|
|
73
|
+
) {
|
|
74
|
+
super(
|
|
75
|
+
`The value provided for the parameter "${path}" is out of bounds for the type "${solidityType}". Expected a value between ${min} and ${max}.`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Thrown when a parameter's Solidity type can't be encoded at all.
|
|
82
|
+
*/
|
|
83
|
+
export class UnsupportedAbiTypeError extends AbiEncodingError {
|
|
84
|
+
public readonly kind: "unsupported-type" = "unsupported-type";
|
|
85
|
+
|
|
86
|
+
constructor(
|
|
87
|
+
public readonly path: string,
|
|
88
|
+
public readonly solidityType: string,
|
|
89
|
+
) {
|
|
90
|
+
super(
|
|
91
|
+
`The type "${solidityType}" of the parameter "${path}" is not supported.`,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Thrown when two parameters, or two components of the same tuple, share a
|
|
98
|
+
* name.
|
|
99
|
+
*
|
|
100
|
+
* Only a hand-written ABI can carry duplicate names; solc never emits one. The
|
|
101
|
+
* encoder's own guard covers named parameters only, and names are stripped
|
|
102
|
+
* before it runs, so it never sees them.
|
|
103
|
+
*/
|
|
104
|
+
export class DuplicateAbiParameterNameError extends AbiEncodingError {
|
|
105
|
+
public readonly kind: "duplicate-parameter-name" = "duplicate-parameter-name";
|
|
106
|
+
|
|
107
|
+
constructor(
|
|
108
|
+
// undefined when the duplicate is between top-level parameters
|
|
109
|
+
public readonly path: string | undefined,
|
|
110
|
+
public readonly duplicatedName: string,
|
|
111
|
+
) {
|
|
112
|
+
super(
|
|
113
|
+
path === undefined
|
|
114
|
+
? `Two or more parameters are named "${duplicatedName}".`
|
|
115
|
+
: `Two or more components of the parameter "${path}" are named "${duplicatedName}".`,
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Thrown when the encoder refuses values that already passed validation.
|
|
122
|
+
*/
|
|
123
|
+
export class AbiEncodingFailedError extends AbiEncodingError {
|
|
124
|
+
public readonly kind: "encoding-failed" = "encoding-failed";
|
|
125
|
+
|
|
126
|
+
constructor(cause: Error) {
|
|
127
|
+
// The encoder's messages don't end with a period, so one is added to keep
|
|
128
|
+
// this message punctuated like the rest.
|
|
129
|
+
super(`The values could not be ABI-encoded: ${cause.message}.`, cause);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* The union of every error that ABI encoding can throw.
|
|
135
|
+
*/
|
|
136
|
+
export type AnyAbiEncodingError =
|
|
137
|
+
| AbiParametersLengthMismatchError
|
|
138
|
+
| InvalidAbiValueError
|
|
139
|
+
| AbiValueOutOfBoundsError
|
|
140
|
+
| UnsupportedAbiTypeError
|
|
141
|
+
| DuplicateAbiParameterNameError
|
|
142
|
+
| AbiEncodingFailedError;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Checks whether a value is the ABI encoding error of the given kind.
|
|
146
|
+
*
|
|
147
|
+
* @param error The value to check.
|
|
148
|
+
* @param kind The kind of error to check for.
|
|
149
|
+
* @returns True if the value is an ABI encoding error of that kind.
|
|
150
|
+
*/
|
|
151
|
+
export function isAbiEncodingError<KindT extends AbiEncodingErrorKind>(
|
|
152
|
+
error: unknown,
|
|
153
|
+
kind: KindT,
|
|
154
|
+
): error is Extract<AnyAbiEncodingError, { kind: KindT }> {
|
|
155
|
+
return error instanceof Error && "kind" in error && error.kind === kind;
|
|
156
|
+
}
|