@nomicfoundation/hardhat-utils 5.1.1 → 5.2.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 +20 -0
- package/dist/src/abi.d.ts +110 -1
- package/dist/src/abi.d.ts.map +1 -1
- package/dist/src/abi.js +109 -3
- package/dist/src/abi.js.map +1 -1
- package/dist/src/bytes.js +1 -1
- package/dist/src/bytes.js.map +1 -1
- package/dist/src/errors/abi.d.ts +26 -3
- package/dist/src/errors/abi.d.ts.map +1 -1
- package/dist/src/errors/abi.js +42 -5
- package/dist/src/errors/abi.js.map +1 -1
- package/dist/src/fs.d.ts +16 -3
- package/dist/src/fs.d.ts.map +1 -1
- package/dist/src/fs.js +33 -5
- package/dist/src/fs.js.map +1 -1
- package/dist/src/internal/abi-signature.d.ts +34 -0
- package/dist/src/internal/abi-signature.d.ts.map +1 -0
- package/dist/src/internal/abi-signature.js +335 -0
- package/dist/src/internal/abi-signature.js.map +1 -0
- package/dist/src/internal/abi.d.ts +8 -0
- package/dist/src/internal/abi.d.ts.map +1 -1
- package/dist/src/internal/abi.js +19 -8
- package/dist/src/internal/abi.js.map +1 -1
- package/dist/src/internal/fs.d.ts +5 -0
- package/dist/src/internal/fs.d.ts.map +1 -1
- package/dist/src/internal/fs.js +11 -0
- package/dist/src/internal/fs.js.map +1 -1
- package/dist/src/internal/request.d.ts +1 -0
- package/dist/src/internal/request.d.ts.map +1 -1
- package/dist/src/internal/request.js +12 -0
- package/dist/src/internal/request.js.map +1 -1
- package/package.json +1 -1
- package/src/abi.ts +197 -1
- package/src/bytes.ts +1 -1
- package/src/errors/abi.ts +68 -11
- package/src/fs.ts +33 -3
- package/src/internal/abi-signature.ts +428 -0
- package/src/internal/abi.ts +23 -8
- package/src/internal/fs.ts +15 -0
- package/src/internal/request.ts +14 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @nomicfoundation/hardhat-utils
|
|
2
2
|
|
|
3
|
+
## 5.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#8834](https://github.com/NomicFoundation/hardhat/pull/8834) [`1f60b03`](https://github.com/NomicFoundation/hardhat/commit/1f60b034572b424e319f1d22bbb7e58afa4d4066) Thanks [@AliBaig-xD](https://github.com/AliBaig-xD)! - Added a `mode` option to `writeUtf8File` and `writeJsonFile` to set file permissions; the `flag` argument of `writeUtf8File` moved into an options object.
|
|
8
|
+
|
|
9
|
+
## 5.1.2
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#8748](https://github.com/NomicFoundation/hardhat/pull/8748) [`e2c81c4`](https://github.com/NomicFoundation/hardhat/commit/e2c81c4e8a2ddb951e525f8f91811b7911e5e2e7) Thanks [@ChristopherDedominici](https://github.com/ChristopherDedominici)! - Fixed `encodeAbiParameters` accepting the invalid types `bytes01` to `bytes09` as `bytes1` to `bytes9`.
|
|
14
|
+
|
|
15
|
+
- [#8755](https://github.com/NomicFoundation/hardhat/pull/8755) [`5c2887e`](https://github.com/NomicFoundation/hardhat/commit/5c2887ea05a2881b6196218493183927850c934e) Thanks [@ChristopherDedominici](https://github.com/ChristopherDedominici)! - Made the `kind` argument of `isAbiEncodingError` optional, so it can check for an ABI encoding error of any kind.
|
|
16
|
+
|
|
17
|
+
- [#8748](https://github.com/NomicFoundation/hardhat/pull/8748) [`cd4987c`](https://github.com/NomicFoundation/hardhat/commit/cd4987c947b1ce8f1a5da63b8b8e5f65950a2606) Thanks [@ChristopherDedominici](https://github.com/ChristopherDedominici)! - Added functions to parse human-readable ABI signatures and parameters, and compute their selectors and event topics.
|
|
18
|
+
|
|
19
|
+
- [#8712](https://github.com/NomicFoundation/hardhat/pull/8712) [`665c386`](https://github.com/NomicFoundation/hardhat/commit/665c386fc8f62c77e47d180ef31d41cfef5dc04d) Thanks [@schaable](https://github.com/schaable)! - Improved the reliability of network requests on slow or unstable connections.
|
|
20
|
+
|
|
21
|
+
- [#8838](https://github.com/NomicFoundation/hardhat/pull/8838) [`9d453de`](https://github.com/NomicFoundation/hardhat/commit/9d453de8770f394b966e4592dee22c79fb98f0b2) Thanks [@zjuncal](https://github.com/zjuncal)! - Fixed `setLengthLeft` returning the whole byte array instead of an empty array when truncating to a length of `0`.
|
|
22
|
+
|
|
3
23
|
## 5.1.1
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
package/dist/src/abi.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export interface AbiParameter {
|
|
|
6
6
|
readonly name?: string;
|
|
7
7
|
readonly type: string;
|
|
8
8
|
readonly components?: readonly AbiParameter[];
|
|
9
|
+
readonly indexed?: boolean;
|
|
9
10
|
}
|
|
10
11
|
/**
|
|
11
12
|
* An entry of a contract's ABI. Only the fields needed to encode values are
|
|
@@ -16,6 +17,49 @@ export interface AbiFragment {
|
|
|
16
17
|
readonly name?: string;
|
|
17
18
|
readonly inputs?: readonly AbiParameter[];
|
|
18
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* A parsed human-readable signature of a function, event or error.
|
|
22
|
+
*/
|
|
23
|
+
export interface AbiSignature {
|
|
24
|
+
/** The keyword the signature starts with, if any. */
|
|
25
|
+
readonly kind?: "function" | "event" | "error" | "constructor";
|
|
26
|
+
/**
|
|
27
|
+
* The name, or "" for a constructor or a bare parameter list, like
|
|
28
|
+
* `(uint256,bool)`.
|
|
29
|
+
*/
|
|
30
|
+
readonly name: string;
|
|
31
|
+
readonly inputs: readonly AbiParameter[];
|
|
32
|
+
/**
|
|
33
|
+
* The return types, written as `f(uint256)(bool)` or
|
|
34
|
+
* `f(uint256) returns (bool)`. They can hold custom types, like `IPool`, kept
|
|
35
|
+
* as written, which aren't ABI types and must be resolved before decoding.
|
|
36
|
+
*/
|
|
37
|
+
readonly outputs: readonly AbiParameter[];
|
|
38
|
+
/** Set to true if the signature has the `anonymous` modifier of events. */
|
|
39
|
+
readonly anonymous?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A parsed human-readable signature of a function or error.
|
|
43
|
+
*/
|
|
44
|
+
export interface AbiFunctionSignature {
|
|
45
|
+
/** The keyword the signature starts with, if any. */
|
|
46
|
+
readonly kind?: "function" | "error";
|
|
47
|
+
readonly name: string;
|
|
48
|
+
readonly inputs: readonly AbiParameter[];
|
|
49
|
+
/** The return types. See `AbiSignature#outputs`. */
|
|
50
|
+
readonly outputs: readonly AbiParameter[];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A parsed human-readable signature of an event.
|
|
54
|
+
*/
|
|
55
|
+
export interface AbiEventSignature {
|
|
56
|
+
/** The keyword the signature starts with, if any. */
|
|
57
|
+
readonly kind?: "event";
|
|
58
|
+
readonly name: string;
|
|
59
|
+
readonly inputs: readonly AbiParameter[];
|
|
60
|
+
/** Whether the event is anonymous, so it doesn't log its topic. */
|
|
61
|
+
readonly anonymous: boolean;
|
|
62
|
+
}
|
|
19
63
|
/**
|
|
20
64
|
* The signedness and size of a Solidity integer type.
|
|
21
65
|
*/
|
|
@@ -75,5 +119,70 @@ export declare function getAbiConstructorParameters(abi: readonly AbiFragment[])
|
|
|
75
119
|
* @throws AbiEncodingFailedError If the encoder fails for any other reason.
|
|
76
120
|
*/
|
|
77
121
|
export declare function encodeAbiParameters(parameters: readonly AbiParameter[], values: readonly unknown[]): Promise<PrefixedHexString>;
|
|
78
|
-
|
|
122
|
+
/**
|
|
123
|
+
* Parses a human-readable signature, like `transfer(address,uint256)`.
|
|
124
|
+
*
|
|
125
|
+
* It also accepts parameter names, the `indexed` and data location keywords,
|
|
126
|
+
* tuples written as `(...)` or `tuple(...)`, and whole Solidity declarations,
|
|
127
|
+
* like `function balanceOf(address owner) external view returns (uint256);`.
|
|
128
|
+
* Type aliases are normalized, so `uint` becomes `uint256` and `byte` becomes
|
|
129
|
+
* `bytes1`, and function types must be written as `function`. Custom types,
|
|
130
|
+
* like structs or contracts, are only accepted as return types, as resolving
|
|
131
|
+
* them to ABI types needs their definitions.
|
|
132
|
+
*
|
|
133
|
+
* @param signature The signature to parse.
|
|
134
|
+
* @returns The parsed signature.
|
|
135
|
+
* @throws InvalidAbiSignatureError If the signature is invalid.
|
|
136
|
+
*/
|
|
137
|
+
export declare function parseAbiSignature(signature: string): AbiSignature;
|
|
138
|
+
/**
|
|
139
|
+
* Parses a human-readable signature of a function or error, like
|
|
140
|
+
* `transfer(address,uint256)` or `error Unauthorized(address)`, accepting the
|
|
141
|
+
* same syntax as `parseAbiSignature`.
|
|
142
|
+
*
|
|
143
|
+
* @param signature The signature to parse.
|
|
144
|
+
* @returns The parsed signature.
|
|
145
|
+
* @throws InvalidAbiSignatureError If the signature is invalid, has no name, or
|
|
146
|
+
* is of an event or a constructor.
|
|
147
|
+
*/
|
|
148
|
+
export declare function parseAbiFunctionSignature(signature: string): AbiFunctionSignature;
|
|
149
|
+
/**
|
|
150
|
+
* Parses a human-readable signature of an event, like
|
|
151
|
+
* `Transfer(address,address,uint256)`, accepting the same syntax as
|
|
152
|
+
* `parseAbiSignature`.
|
|
153
|
+
*
|
|
154
|
+
* @param signature The signature to parse.
|
|
155
|
+
* @returns The parsed signature.
|
|
156
|
+
* @throws InvalidAbiSignatureError If the signature is invalid, has no name, has
|
|
157
|
+
* return types, or is of a function, error or constructor.
|
|
158
|
+
*/
|
|
159
|
+
export declare function parseAbiEventSignature(signature: string): AbiEventSignature;
|
|
160
|
+
/**
|
|
161
|
+
* Parses a single human-readable parameter, like `uint256`, `address payable`
|
|
162
|
+
* or `(address to, uint256 amount)[]`, accepting the same syntax as the
|
|
163
|
+
* parameters of `parseAbiSignature`.
|
|
164
|
+
*
|
|
165
|
+
* @param parameter The parameter to parse.
|
|
166
|
+
* @returns The parsed parameter.
|
|
167
|
+
* @throws InvalidAbiSignatureError If the parameter is invalid, or isn't a
|
|
168
|
+
* single one.
|
|
169
|
+
*/
|
|
170
|
+
export declare function parseAbiParameter(parameter: string): AbiParameter;
|
|
171
|
+
/**
|
|
172
|
+
* Returns the selector of a function or error: the first 4 bytes of the
|
|
173
|
+
* keccak256 hash of its canonical signature.
|
|
174
|
+
*
|
|
175
|
+
* @param signature The parsed signature.
|
|
176
|
+
* @returns The 4-byte selector, like `0xa9059cbb`.
|
|
177
|
+
*/
|
|
178
|
+
export declare function getAbiSelector(signature: AbiFunctionSignature): Promise<PrefixedHexString>;
|
|
179
|
+
/**
|
|
180
|
+
* Returns the topic of an event: the keccak256 hash of its canonical
|
|
181
|
+
* signature, which non-anonymous events log as their first topic.
|
|
182
|
+
*
|
|
183
|
+
* @param signature The parsed signature.
|
|
184
|
+
* @returns The 32-byte topic.
|
|
185
|
+
*/
|
|
186
|
+
export declare function getAbiEventTopic(signature: AbiEventSignature): Promise<PrefixedHexString>;
|
|
187
|
+
export { AbiEncodingError, AbiEncodingFailedError, AbiParametersLengthMismatchError, AbiValueOutOfBoundsError, DuplicateAbiParameterNameError, InvalidAbiSignatureError, InvalidAbiValueError, UnsupportedAbiTypeError, isAbiEncodingError, isInvalidAbiSignatureError, type AbiEncodingErrorKind, type AnyAbiEncodingError, } from "./errors/abi.js";
|
|
79
188
|
//# sourceMappingURL=abi.d.ts.map
|
package/dist/src/abi.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abi.d.ts","sourceRoot":"","sources":["../../src/abi.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"abi.d.ts","sourceRoot":"","sources":["../../src/abi.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAwBlD;;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,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iBAAiB,GAC3B,OAAO,CAAC,iBAAiB,CAAC,CAE5B;AAED,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,gCAAgC,EAChC,wBAAwB,EACxB,8BAA8B,EAC9B,wBAAwB,EACxB,oBAAoB,EACpB,uBAAuB,EACvB,kBAAkB,EAClB,0BAA0B,EAC1B,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,GACzB,MAAM,iBAAiB,CAAC"}
|
package/dist/src/abi.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ensureError } from "./error.js";
|
|
2
|
-
import { AbiEncodingFailedError, AbiParametersLengthMismatchError, } from "./errors/abi.js";
|
|
3
|
-
import { getPrefixedHexString } from "./hex.js";
|
|
2
|
+
import { AbiEncodingFailedError, AbiParametersLengthMismatchError, InvalidAbiSignatureError, } from "./errors/abi.js";
|
|
3
|
+
import { bytesToHexString, getPrefixedHexString } from "./hex.js";
|
|
4
|
+
import { AbiSignatureParser, hashCanonicalAbiSignature, } from "./internal/abi-signature.js";
|
|
4
5
|
import { assertNoDuplicateAbiParameterNames, normalizeAbiValue, toUnnamedAbiParameters, } from "./internal/abi.js";
|
|
5
6
|
// We don't load micro-eth-signer on startup because it's slow to initialize,
|
|
6
7
|
// and most callers of this module never encode anything.
|
|
@@ -110,5 +111,110 @@ export async function encodeAbiParameters(parameters, values) {
|
|
|
110
111
|
}
|
|
111
112
|
return getPrefixedHexString(encoded);
|
|
112
113
|
}
|
|
113
|
-
|
|
114
|
+
/**
|
|
115
|
+
* Parses a human-readable signature, like `transfer(address,uint256)`.
|
|
116
|
+
*
|
|
117
|
+
* It also accepts parameter names, the `indexed` and data location keywords,
|
|
118
|
+
* tuples written as `(...)` or `tuple(...)`, and whole Solidity declarations,
|
|
119
|
+
* like `function balanceOf(address owner) external view returns (uint256);`.
|
|
120
|
+
* Type aliases are normalized, so `uint` becomes `uint256` and `byte` becomes
|
|
121
|
+
* `bytes1`, and function types must be written as `function`. Custom types,
|
|
122
|
+
* like structs or contracts, are only accepted as return types, as resolving
|
|
123
|
+
* them to ABI types needs their definitions.
|
|
124
|
+
*
|
|
125
|
+
* @param signature The signature to parse.
|
|
126
|
+
* @returns The parsed signature.
|
|
127
|
+
* @throws InvalidAbiSignatureError If the signature is invalid.
|
|
128
|
+
*/
|
|
129
|
+
export function parseAbiSignature(signature) {
|
|
130
|
+
return new AbiSignatureParser(signature).parse();
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Parses a human-readable signature of a function or error, like
|
|
134
|
+
* `transfer(address,uint256)` or `error Unauthorized(address)`, accepting the
|
|
135
|
+
* same syntax as `parseAbiSignature`.
|
|
136
|
+
*
|
|
137
|
+
* @param signature The signature to parse.
|
|
138
|
+
* @returns The parsed signature.
|
|
139
|
+
* @throws InvalidAbiSignatureError If the signature is invalid, has no name, or
|
|
140
|
+
* is of an event or a constructor.
|
|
141
|
+
*/
|
|
142
|
+
export function parseAbiFunctionSignature(signature) {
|
|
143
|
+
const { kind, name, inputs, outputs, anonymous } = parseAbiSignature(signature);
|
|
144
|
+
if (kind === "event" ||
|
|
145
|
+
anonymous === true ||
|
|
146
|
+
inputs.some((input) => input.indexed === true)) {
|
|
147
|
+
throw new InvalidAbiSignatureError(signature, "it's an event, not a function or error");
|
|
148
|
+
}
|
|
149
|
+
if (kind === "constructor") {
|
|
150
|
+
throw new InvalidAbiSignatureError(signature, "it's a constructor, not a function or error");
|
|
151
|
+
}
|
|
152
|
+
if (name === "") {
|
|
153
|
+
throw new InvalidAbiSignatureError(signature, "it must include a name");
|
|
154
|
+
}
|
|
155
|
+
return { ...(kind !== undefined && { kind }), name, inputs, outputs };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Parses a human-readable signature of an event, like
|
|
159
|
+
* `Transfer(address,address,uint256)`, accepting the same syntax as
|
|
160
|
+
* `parseAbiSignature`.
|
|
161
|
+
*
|
|
162
|
+
* @param signature The signature to parse.
|
|
163
|
+
* @returns The parsed signature.
|
|
164
|
+
* @throws InvalidAbiSignatureError If the signature is invalid, has no name, has
|
|
165
|
+
* return types, or is of a function, error or constructor.
|
|
166
|
+
*/
|
|
167
|
+
export function parseAbiEventSignature(signature) {
|
|
168
|
+
const { kind, name, inputs, outputs, anonymous } = parseAbiSignature(signature);
|
|
169
|
+
if (kind === "function" || kind === "error" || kind === "constructor") {
|
|
170
|
+
throw new InvalidAbiSignatureError(signature, `it's ${kind === "error" ? "an" : "a"} ${kind}, not an event`);
|
|
171
|
+
}
|
|
172
|
+
if (outputs.length > 0) {
|
|
173
|
+
throw new InvalidAbiSignatureError(signature, "events can't have return types");
|
|
174
|
+
}
|
|
175
|
+
if (name === "") {
|
|
176
|
+
throw new InvalidAbiSignatureError(signature, "it must include a name");
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
...(kind !== undefined && { kind }),
|
|
180
|
+
name,
|
|
181
|
+
inputs,
|
|
182
|
+
anonymous: anonymous === true,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Parses a single human-readable parameter, like `uint256`, `address payable`
|
|
187
|
+
* or `(address to, uint256 amount)[]`, accepting the same syntax as the
|
|
188
|
+
* parameters of `parseAbiSignature`.
|
|
189
|
+
*
|
|
190
|
+
* @param parameter The parameter to parse.
|
|
191
|
+
* @returns The parsed parameter.
|
|
192
|
+
* @throws InvalidAbiSignatureError If the parameter is invalid, or isn't a
|
|
193
|
+
* single one.
|
|
194
|
+
*/
|
|
195
|
+
export function parseAbiParameter(parameter) {
|
|
196
|
+
return new AbiSignatureParser(parameter).parseSingleParameter();
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Returns the selector of a function or error: the first 4 bytes of the
|
|
200
|
+
* keccak256 hash of its canonical signature.
|
|
201
|
+
*
|
|
202
|
+
* @param signature The parsed signature.
|
|
203
|
+
* @returns The 4-byte selector, like `0xa9059cbb`.
|
|
204
|
+
*/
|
|
205
|
+
export async function getAbiSelector(signature) {
|
|
206
|
+
const hash = await hashCanonicalAbiSignature(signature);
|
|
207
|
+
return bytesToHexString(hash.subarray(0, 4));
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Returns the topic of an event: the keccak256 hash of its canonical
|
|
211
|
+
* signature, which non-anonymous events log as their first topic.
|
|
212
|
+
*
|
|
213
|
+
* @param signature The parsed signature.
|
|
214
|
+
* @returns The 32-byte topic.
|
|
215
|
+
*/
|
|
216
|
+
export async function getAbiEventTopic(signature) {
|
|
217
|
+
return bytesToHexString(await hashCanonicalAbiSignature(signature));
|
|
218
|
+
}
|
|
219
|
+
export { AbiEncodingError, AbiEncodingFailedError, AbiParametersLengthMismatchError, AbiValueOutOfBoundsError, DuplicateAbiParameterNameError, InvalidAbiSignatureError, InvalidAbiValueError, UnsupportedAbiTypeError, isAbiEncodingError, isInvalidAbiSignatureError, } from "./errors/abi.js";
|
|
114
220
|
//# sourceMappingURL=abi.js.map
|
package/dist/src/abi.js.map
CHANGED
|
@@ -1 +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,
|
|
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,EAChC,wBAAwB,GACzB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAClE,OAAO,EACL,kBAAkB,EAClB,yBAAyB,GAC1B,MAAM,6BAA6B,CAAC;AACrC,OAAO,EACL,kCAAkC,EAClC,iBAAiB,EACjB,sBAAsB,GACvB,MAAM,mBAAmB,CAAC;AAE3B,6EAA6E;AAC7E,yDAAyD;A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,EAAE,CAAC,CAAC,CAAC,CAAC;AAC/C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,SAA4B;IAE5B,OAAO,gBAAgB,CAAC,MAAM,yBAAyB,CAAC,SAAS,CAAC,CAAC,CAAC;AACtE,CAAC;AAED,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,gCAAgC,EAChC,wBAAwB,EACxB,8BAA8B,EAC9B,wBAAwB,EACxB,oBAAoB,EACpB,uBAAuB,EACvB,kBAAkB,EAClB,0BAA0B,GAG3B,MAAM,iBAAiB,CAAC"}
|
package/dist/src/bytes.js
CHANGED
|
@@ -27,7 +27,7 @@ export function setLengthLeft(bytes, length) {
|
|
|
27
27
|
padded.set(bytes, length - bytes.length);
|
|
28
28
|
return padded;
|
|
29
29
|
}
|
|
30
|
-
return bytes.subarray(-length);
|
|
30
|
+
return length === 0 ? bytes.subarray(0, 0) : bytes.subarray(-length);
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
33
|
* Checks if two Uint8Arrays are equal.
|
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,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;A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,CAAC"}
|
package/dist/src/errors/abi.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { CustomError } from "../error.js";
|
|
2
|
+
declare const ABI_ENCODING_ERROR_KINDS: readonly ["parameters-length-mismatch", "invalid-value", "value-out-of-bounds", "unsupported-type", "duplicate-parameter-name", "encoding-failed"];
|
|
2
3
|
/**
|
|
3
4
|
* Discriminates the errors that ABI encoding can throw.
|
|
4
5
|
*/
|
|
5
|
-
export type AbiEncodingErrorKind =
|
|
6
|
+
export type AbiEncodingErrorKind = (typeof ABI_ENCODING_ERROR_KINDS)[number];
|
|
6
7
|
/**
|
|
7
8
|
* Base class of every error thrown while ABI-encoding values.
|
|
8
9
|
*
|
|
@@ -81,13 +82,35 @@ export declare class AbiEncodingFailedError extends AbiEncodingError {
|
|
|
81
82
|
*/
|
|
82
83
|
export type AnyAbiEncodingError = AbiParametersLengthMismatchError | InvalidAbiValueError | AbiValueOutOfBoundsError | UnsupportedAbiTypeError | DuplicateAbiParameterNameError | AbiEncodingFailedError;
|
|
83
84
|
/**
|
|
84
|
-
* Checks whether a value is
|
|
85
|
+
* Checks whether a value is an ABI encoding error, optionally of a given kind.
|
|
85
86
|
*
|
|
86
87
|
* @param error The value to check.
|
|
87
|
-
* @param kind The kind of error to check for.
|
|
88
|
+
* @param kind The kind of error to check for. If omitted, any kind matches.
|
|
88
89
|
* @returns True if the value is an ABI encoding error of that kind.
|
|
89
90
|
*/
|
|
91
|
+
export declare function isAbiEncodingError(error: unknown): error is AnyAbiEncodingError;
|
|
90
92
|
export declare function isAbiEncodingError<KindT extends AbiEncodingErrorKind>(error: unknown, kind: KindT): error is Extract<AnyAbiEncodingError, {
|
|
91
93
|
kind: KindT;
|
|
92
94
|
}>;
|
|
95
|
+
/**
|
|
96
|
+
* Thrown when a human-readable ABI signature can't be parsed.
|
|
97
|
+
*
|
|
98
|
+
* Consumers should discriminate with `isInvalidAbiSignatureError` rather than
|
|
99
|
+
* `instanceof`, for the same reason as `isAbiEncodingError`.
|
|
100
|
+
*/
|
|
101
|
+
export declare class InvalidAbiSignatureError extends CustomError {
|
|
102
|
+
readonly input: string;
|
|
103
|
+
readonly reason: string;
|
|
104
|
+
readonly kind: "invalid-signature";
|
|
105
|
+
constructor(input: string, reason: string);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Whether an error is an `InvalidAbiSignatureError`, even if thrown by another
|
|
109
|
+
* copy of this package.
|
|
110
|
+
*
|
|
111
|
+
* @param error The value to check.
|
|
112
|
+
* @returns Whether it's an `InvalidAbiSignatureError`.
|
|
113
|
+
*/
|
|
114
|
+
export declare function isInvalidAbiSignatureError(error: unknown): error is InvalidAbiSignatureError;
|
|
115
|
+
export {};
|
|
93
116
|
//# sourceMappingURL=abi.d.ts.map
|
|
@@ -1 +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,
|
|
1
|
+
{"version":3,"file":"abi.d.ts","sourceRoot":"","sources":["../../../src/errors/abi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,QAAA,MAAM,wBAAwB,oJAOpB,CAAC;AAEX;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG,CAAC,OAAO,wBAAwB,CAAC,CAAC,MAAM,CAAC,CAAC;AAO7E;;;;;;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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GAChC,oBAAoB,GACpB,wBAAwB,GACxB,uBAAuB,GACvB,8BAA8B,GAC9B,sBAAsB,CAAC;AAE3B;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,mBAAmB,CAAC;AAChC,wBAAgB,kBAAkB,CAAC,KAAK,SAAS,oBAAoB,EACnE,KAAK,EAAE,OAAO,EACd,IAAI,EAAE,KAAK,GACV,KAAK,IAAI,OAAO,CAAC,mBAAmB,EAAE;IAAE,IAAI,EAAE,KAAK,CAAA;CAAE,CAAC,CAAC;AAgB1D;;;;;GAKG;AACH,qBAAa,wBAAyB,SAAQ,WAAW;aAIrC,KAAK,EAAE,MAAM;aACb,MAAM,EAAE,MAAM;IAJhC,SAAgB,IAAI,EAAE,mBAAmB,CAAuB;gBAG9C,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM;CAIjC;AAED;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CACxC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,wBAAwB,CAMnC"}
|
package/dist/src/errors/abi.js
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { CustomError } from "../error.js";
|
|
2
|
+
const ABI_ENCODING_ERROR_KINDS = [
|
|
3
|
+
"parameters-length-mismatch",
|
|
4
|
+
"invalid-value",
|
|
5
|
+
"value-out-of-bounds",
|
|
6
|
+
"unsupported-type",
|
|
7
|
+
"duplicate-parameter-name",
|
|
8
|
+
"encoding-failed",
|
|
9
|
+
];
|
|
10
|
+
// unknown so that any `kind` can be looked up without narrowing it first
|
|
11
|
+
const ABI_ENCODING_ERROR_KIND_SET = new Set(ABI_ENCODING_ERROR_KINDS);
|
|
2
12
|
/**
|
|
3
13
|
* Base class of every error thrown while ABI-encoding values.
|
|
4
14
|
*
|
|
@@ -104,14 +114,41 @@ export class AbiEncodingFailedError extends AbiEncodingError {
|
|
|
104
114
|
super(`The values could not be ABI-encoded: ${cause.message}.`, cause);
|
|
105
115
|
}
|
|
106
116
|
}
|
|
117
|
+
export function isAbiEncodingError(error, kind) {
|
|
118
|
+
if (!(error instanceof Error) || !("kind" in error)) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
if (kind === undefined) {
|
|
122
|
+
return ABI_ENCODING_ERROR_KIND_SET.has(error.kind);
|
|
123
|
+
}
|
|
124
|
+
return error.kind === kind;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Thrown when a human-readable ABI signature can't be parsed.
|
|
128
|
+
*
|
|
129
|
+
* Consumers should discriminate with `isInvalidAbiSignatureError` rather than
|
|
130
|
+
* `instanceof`, for the same reason as `isAbiEncodingError`.
|
|
131
|
+
*/
|
|
132
|
+
export class InvalidAbiSignatureError extends CustomError {
|
|
133
|
+
input;
|
|
134
|
+
reason;
|
|
135
|
+
kind = "invalid-signature";
|
|
136
|
+
constructor(input, reason) {
|
|
137
|
+
super(`The ABI signature "${input}" is invalid: ${reason}.`);
|
|
138
|
+
this.input = input;
|
|
139
|
+
this.reason = reason;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
107
142
|
/**
|
|
108
|
-
*
|
|
143
|
+
* Whether an error is an `InvalidAbiSignatureError`, even if thrown by another
|
|
144
|
+
* copy of this package.
|
|
109
145
|
*
|
|
110
146
|
* @param error The value to check.
|
|
111
|
-
* @
|
|
112
|
-
* @returns True if the value is an ABI encoding error of that kind.
|
|
147
|
+
* @returns Whether it's an `InvalidAbiSignatureError`.
|
|
113
148
|
*/
|
|
114
|
-
export function
|
|
115
|
-
return error instanceof Error &&
|
|
149
|
+
export function isInvalidAbiSignatureError(error) {
|
|
150
|
+
return (error instanceof Error &&
|
|
151
|
+
"kind" in error &&
|
|
152
|
+
error.kind === "invalid-signature");
|
|
116
153
|
}
|
|
117
154
|
//# sourceMappingURL=abi.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abi.js","sourceRoot":"","sources":["../../../src/errors/abi.ts"],"names":[],"mapp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CAChC,KAAc,EACd,IAA2B;IAE3B,IAAI,CAAC,CAAC,KAAK,YAAY,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,IAAI,KAAK,CAAC,EAAE,CAAC;QACpD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACvB,OAAO,2BAA2B,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACrD,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC;AAC7B,CAAC;AAED;;;;;GAKG;AACH,MAAM,OAAO,wBAAyB,SAAQ,WAAW;IAIrC;IACA;IAJF,IAAI,GAAwB,mBAAmB,CAAC;IAEhE,YACkB,KAAa,EACb,MAAc;QAE9B,KAAK,CAAC,sBAAsB,KAAK,iBAAiB,MAAM,GAAG,CAAC,CAAC;QAH7C,UAAK,GAAL,KAAK,CAAQ;QACb,WAAM,GAAN,MAAM,CAAQ;IAGhC,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,UAAU,0BAA0B,CACxC,KAAc;IAEd,OAAO,CACL,KAAK,YAAY,KAAK;QACtB,MAAM,IAAI,KAAK;QACf,KAAK,CAAC,IAAI,KAAK,mBAAmB,CACnC,CAAC;AACJ,CAAC"}
|
package/dist/src/fs.d.ts
CHANGED
|
@@ -113,10 +113,14 @@ export declare function readJsonFile<T>(absolutePathToFile: string): Promise<T>;
|
|
|
113
113
|
*
|
|
114
114
|
* @param absolutePathToFile The path to the file. If the file exists, it will be overwritten.
|
|
115
115
|
* @param object The object to write.
|
|
116
|
+
* @param options Optional settings for the write.
|
|
117
|
+
* @param options.mode The permissions to set on the file, e.g. `0o600`. See `writeUtf8File`.
|
|
116
118
|
* @throws JsonSerializationError if the object can't be serialized to JSON.
|
|
117
119
|
* @throws FileSystemAccessError for any other error.
|
|
118
120
|
*/
|
|
119
|
-
export declare function writeJsonFile<T>(absolutePathToFile: string, object: T
|
|
121
|
+
export declare function writeJsonFile<T>(absolutePathToFile: string, object: T, options?: {
|
|
122
|
+
mode?: number;
|
|
123
|
+
}): Promise<void>;
|
|
120
124
|
/**
|
|
121
125
|
* Writes an object to a JSON file, supporting objects whose JSON is too large
|
|
122
126
|
* to be held in a single string. The JSON is compact (not indented), the
|
|
@@ -160,12 +164,21 @@ export declare function readUtf8File(absolutePathToFile: string): Promise<string
|
|
|
160
164
|
*
|
|
161
165
|
* @param absolutePathToFile The path to the file.
|
|
162
166
|
* @param data The data to write.
|
|
163
|
-
* @param
|
|
167
|
+
* @param options Optional settings for the write.
|
|
168
|
+
* @param options.flag The flag to use when writing the file. If not provided, the file will be overwritten.
|
|
164
169
|
* See https://nodejs.org/docs/latest-v20.x/api/fs.html#file-system-flags for more information.
|
|
170
|
+
* @param options.mode The permissions to set on the file, e.g. `0o600`. It is applied even if the
|
|
171
|
+
* file already exists, so it can be used to restrict a file created earlier. If not provided, a new
|
|
172
|
+
* file gets the default permissions and an existing file keeps its own. If the filesystem doesn't
|
|
173
|
+
* support changing permissions and the file is owned by the current user, the mode is ignored and
|
|
174
|
+
* the write still succeeds. If the file is owned by another user, FileSystemAccessError is thrown.
|
|
165
175
|
* @throws FileAlreadyExistsError if the file already exists and the flag "x" is used.
|
|
166
176
|
* @throws FileSystemAccessError for any other error.
|
|
167
177
|
*/
|
|
168
|
-
export declare function writeUtf8File(absolutePathToFile: string, data: string,
|
|
178
|
+
export declare function writeUtf8File(absolutePathToFile: string, data: string, options?: {
|
|
179
|
+
flag?: string;
|
|
180
|
+
mode?: number;
|
|
181
|
+
}): Promise<void>;
|
|
169
182
|
/**
|
|
170
183
|
* Reads a file and returns its content as a Uint8Array.
|
|
171
184
|
*
|
package/dist/src/fs.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fs.d.ts","sourceRoot":"","sources":["../../src/fs.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fs.d.ts","sourceRoot":"","sources":["../../src/fs.ts"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AAC,EAAE,MAAM,GACZ,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAmB7B;AAED;;GAEG;AACH,wBAAsB,YAAY,CAChC,QAAQ,EAAE,MAAM,EAChB,YAAY,SAAO,GAClB,OAAO,CAAC,OAAO,CAAC,CAyBlB;AAED,OAAO,EACL,iBAAiB,EACjB,qBAAqB,EACrB,sBAAsB,EACtB,sBAAsB,EACtB,sBAAsB,EACtB,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,GACvB,MAAM,gBAAgB,CAAC"}
|
package/dist/src/fs.js
CHANGED
|
@@ -6,7 +6,7 @@ import { parseJsonBytes } from "./bytes.js";
|
|
|
6
6
|
import { ensureError, ensureNodeErrnoExceptionError } from "./error.js";
|
|
7
7
|
import { FileNotFoundError, FileSystemAccessError, InvalidFileFormatError, JsonSerializationError, FileAlreadyExistsError, NotADirectoryError, IsDirectoryError, DirectoryNotEmptyError, } from "./errors/fs.js";
|
|
8
8
|
import { parseJsonStream } from "./internal/bytes.js";
|
|
9
|
-
import { collectAllDirectoriesMatching, collectAllFilesMatching, streamJsonToFile, } from "./internal/fs.js";
|
|
9
|
+
import { collectAllDirectoriesMatching, collectAllFilesMatching, isOwnedByCurrentUser, streamJsonToFile, } from "./internal/fs.js";
|
|
10
10
|
import { hasHeapHeadroomFor, READ_HEAP_BYTES_PER_JSON_BYTE, WRITE_HEAP_BYTES_PER_JSON_BYTE, } from "./internal/json.js";
|
|
11
11
|
import { AsyncMutex } from "./synchronization.js";
|
|
12
12
|
// Lets only one call of writeLargeJsonFile use JSON.stringify at a time, so
|
|
@@ -337,10 +337,12 @@ export async function readJsonFile(absolutePathToFile) {
|
|
|
337
337
|
*
|
|
338
338
|
* @param absolutePathToFile The path to the file. If the file exists, it will be overwritten.
|
|
339
339
|
* @param object The object to write.
|
|
340
|
+
* @param options Optional settings for the write.
|
|
341
|
+
* @param options.mode The permissions to set on the file, e.g. `0o600`. See `writeUtf8File`.
|
|
340
342
|
* @throws JsonSerializationError if the object can't be serialized to JSON.
|
|
341
343
|
* @throws FileSystemAccessError for any other error.
|
|
342
344
|
*/
|
|
343
|
-
export async function writeJsonFile(absolutePathToFile, object) {
|
|
345
|
+
export async function writeJsonFile(absolutePathToFile, object, options = {}) {
|
|
344
346
|
let content;
|
|
345
347
|
try {
|
|
346
348
|
content = JSON.stringify(object, null, 2);
|
|
@@ -349,7 +351,7 @@ export async function writeJsonFile(absolutePathToFile, object) {
|
|
|
349
351
|
ensureError(e);
|
|
350
352
|
throw new JsonSerializationError(absolutePathToFile, e);
|
|
351
353
|
}
|
|
352
|
-
await writeUtf8File(absolutePathToFile, content);
|
|
354
|
+
await writeUtf8File(absolutePathToFile, content, options);
|
|
353
355
|
}
|
|
354
356
|
/**
|
|
355
357
|
* Writes an object to a JSON file, supporting objects whose JSON is too large
|
|
@@ -443,12 +445,19 @@ export async function readUtf8File(absolutePathToFile) {
|
|
|
443
445
|
*
|
|
444
446
|
* @param absolutePathToFile The path to the file.
|
|
445
447
|
* @param data The data to write.
|
|
446
|
-
* @param
|
|
448
|
+
* @param options Optional settings for the write.
|
|
449
|
+
* @param options.flag The flag to use when writing the file. If not provided, the file will be overwritten.
|
|
447
450
|
* See https://nodejs.org/docs/latest-v20.x/api/fs.html#file-system-flags for more information.
|
|
451
|
+
* @param options.mode The permissions to set on the file, e.g. `0o600`. It is applied even if the
|
|
452
|
+
* file already exists, so it can be used to restrict a file created earlier. If not provided, a new
|
|
453
|
+
* file gets the default permissions and an existing file keeps its own. If the filesystem doesn't
|
|
454
|
+
* support changing permissions and the file is owned by the current user, the mode is ignored and
|
|
455
|
+
* the write still succeeds. If the file is owned by another user, FileSystemAccessError is thrown.
|
|
448
456
|
* @throws FileAlreadyExistsError if the file already exists and the flag "x" is used.
|
|
449
457
|
* @throws FileSystemAccessError for any other error.
|
|
450
458
|
*/
|
|
451
|
-
export async function writeUtf8File(absolutePathToFile, data,
|
|
459
|
+
export async function writeUtf8File(absolutePathToFile, data, options = {}) {
|
|
460
|
+
const { flag, mode } = options;
|
|
452
461
|
const dirPath = path.dirname(absolutePathToFile);
|
|
453
462
|
const dirExists = await exists(dirPath);
|
|
454
463
|
if (!dirExists) {
|
|
@@ -458,7 +467,26 @@ export async function writeUtf8File(absolutePathToFile, data, flag) {
|
|
|
458
467
|
await fsPromises.writeFile(absolutePathToFile, data, {
|
|
459
468
|
encoding: "utf8",
|
|
460
469
|
flag,
|
|
470
|
+
mode,
|
|
461
471
|
});
|
|
472
|
+
// The mode passed to writeFile is masked by the umask and ignored when the
|
|
473
|
+
// file already exists, so set it explicitly.
|
|
474
|
+
if (mode !== undefined) {
|
|
475
|
+
try {
|
|
476
|
+
await fsPromises.chmod(absolutePathToFile, mode);
|
|
477
|
+
}
|
|
478
|
+
catch (chmodError) {
|
|
479
|
+
ensureError(chmodError);
|
|
480
|
+
// Filesystems without POSIX permissions (e.g. a Windows drive mounted
|
|
481
|
+
// in WSL, or a FAT disk) reject chmod. The data has already been
|
|
482
|
+
// written, so the mode is best-effort there. A file owned by another
|
|
483
|
+
// user also rejects chmod, but then the caller must know that the
|
|
484
|
+
// mode wasn't applied, so the error is rethrown.
|
|
485
|
+
if (!(await isOwnedByCurrentUser(absolutePathToFile))) {
|
|
486
|
+
throw chmodError;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
462
490
|
}
|
|
463
491
|
catch (e) {
|
|
464
492
|
ensureNodeErrnoExceptionError(e);
|