@webevm/devnode 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +221 -2
- package/dist/accounts.d.ts +82 -0
- package/dist/accounts.d.ts.map +1 -0
- package/dist/accounts.js +129 -0
- package/dist/accounts.js.map +1 -0
- package/dist/allocs.d.ts +26 -0
- package/dist/allocs.d.ts.map +1 -0
- package/dist/allocs.js +99 -0
- package/dist/allocs.js.map +1 -0
- package/dist/dev-node.d.ts +63 -0
- package/dist/dev-node.d.ts.map +1 -0
- package/dist/dev-node.js +166 -0
- package/dist/dev-node.js.map +1 -0
- package/dist/eip712.d.ts +23 -0
- package/dist/eip712.d.ts.map +1 -0
- package/dist/eip712.js +333 -0
- package/dist/eip712.js.map +1 -0
- package/dist/impersonation.d.ts +20 -0
- package/dist/impersonation.d.ts.map +1 -0
- package/dist/impersonation.js +95 -0
- package/dist/impersonation.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/send.d.ts +20 -0
- package/dist/send.d.ts.map +1 -0
- package/dist/send.js +385 -0
- package/dist/send.js.map +1 -0
- package/dist/sign.d.ts +14 -0
- package/dist/sign.d.ts.map +1 -0
- package/dist/sign.js +155 -0
- package/dist/sign.js.map +1 -0
- package/dist/worker-host.d.ts +87 -0
- package/dist/worker-host.d.ts.map +1 -0
- package/dist/worker-host.js +112 -0
- package/dist/worker-host.js.map +1 -0
- package/package.json +79 -3
- package/src/accounts.ts +218 -0
- package/src/allocs.ts +134 -0
- package/src/dev-node.ts +220 -0
- package/src/eip712.ts +390 -0
- package/src/impersonation.ts +119 -0
- package/src/index.ts +25 -0
- package/src/send.ts +469 -0
- package/src/sign.ts +199 -0
- package/src/worker-host.ts +151 -0
package/src/eip712.ts
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* eip712.ts: EIP-712 typed structured data hashing, this package's own code (a
|
|
3
|
+
* maintainer decision, ADR 0017: "EIP-712 hashing is the package's own code"),
|
|
4
|
+
* with no dependency beyond the keccak the package already has.
|
|
5
|
+
*
|
|
6
|
+
* It follows the EIP's text (https://eips.ethereum.org/EIPS/eip-712):
|
|
7
|
+
*
|
|
8
|
+
* - `encode(domainSeparator, message) = "\x19\x01" ‖ domainSeparator ‖ hashStruct(message)`;
|
|
9
|
+
* - `hashStruct(s) = keccak256(typeHash ‖ encodeData(s))`, `typeHash = keccak256(encodeType(typeOf(s)))`;
|
|
10
|
+
* - `encodeType` is `Name(type1 name1,...)` for the primary type, followed by
|
|
11
|
+
* every type it references (directly or through other types or arrays),
|
|
12
|
+
* SORTED BY NAME, each once;
|
|
13
|
+
* - `encodeData` concatenates one 32-byte word per member, in declaration
|
|
14
|
+
* order: `bool`, `address`, `uintN`, `intN` (sign-extended) and `bytesN`
|
|
15
|
+
* (right-padded) encoded as the ABI encodes them; `bytes` and `string` as the
|
|
16
|
+
* keccak256 of their content (a string's UTF-8 bytes); a struct as its
|
|
17
|
+
* `hashStruct`; an array, fixed or dynamic, as the keccak256 of its elements'
|
|
18
|
+
* encodings concatenated.
|
|
19
|
+
*
|
|
20
|
+
* It is verified in tests against the EIP's own published `Mail` digest and
|
|
21
|
+
* against viem's `hashTypedData`, never against itself.
|
|
22
|
+
*
|
|
23
|
+
* DECISIONS made here (task `devnode-signing-methods-and-eip-712`), each
|
|
24
|
+
* user-visible and reversible before 1.0:
|
|
25
|
+
*
|
|
26
|
+
* - A MISSING `EIP712Domain` TYPE IS DERIVED from the fields the domain has, in
|
|
27
|
+
* the EIP's order (`name`, `version`, `chainId`, `verifyingContract`, `salt`),
|
|
28
|
+
* as viem and ethers derive it. Alternative: `@metamask/eth-sig-util`'s
|
|
29
|
+
* `EIP712Domain()` with no fields, which hashes the domain as empty and so
|
|
30
|
+
* ignores a chain ID the caller passed. A request that declares the type is
|
|
31
|
+
* hashed with exactly the declaration.
|
|
32
|
+
* - `primaryType: 'EIP712Domain'` hashes the domain alone, `"\x19\x01" ‖
|
|
33
|
+
* domainSeparator`, as viem and eth-sig-util do.
|
|
34
|
+
* - VALUES ARE CHECKED, NEVER COERCED INTO A DIFFERENT MEANING: a member
|
|
35
|
+
* missing from the message, an integer out of its type's range, a `bytesN`
|
|
36
|
+
* that is not exactly N bytes, an address that is not 20 bytes, a `bool` that
|
|
37
|
+
* is not a boolean, or a fixed-size array of the wrong length is an error
|
|
38
|
+
* naming the member's path. Integers are accepted as JSON numbers (safe
|
|
39
|
+
* integers only), decimal strings, hex strings (`0x`, `-0x` for a negative
|
|
40
|
+
* `intN`) and bigints. Members the message has beyond its type are ignored, as
|
|
41
|
+
* the EIP encodes only the type's members.
|
|
42
|
+
*/
|
|
43
|
+
import {keccak_256} from '@noble/hashes/sha3.js';
|
|
44
|
+
|
|
45
|
+
/** A struct type's members, as the `types` field of a request declares them. */
|
|
46
|
+
export type TypedDataField = {name: string; type: string};
|
|
47
|
+
|
|
48
|
+
/** The `eth_signTypedData_v4` payload. */
|
|
49
|
+
export interface TypedData {
|
|
50
|
+
types: Record<string, readonly TypedDataField[]>;
|
|
51
|
+
primaryType: string;
|
|
52
|
+
domain: Record<string, unknown>;
|
|
53
|
+
message: Record<string, unknown>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The fields an `EIP712Domain` may have, in the order the EIP lists them. */
|
|
57
|
+
const DOMAIN_FIELDS: readonly TypedDataField[] = [
|
|
58
|
+
{name: 'name', type: 'string'},
|
|
59
|
+
{name: 'version', type: 'string'},
|
|
60
|
+
{name: 'chainId', type: 'uint256'},
|
|
61
|
+
{name: 'verifyingContract', type: 'address'},
|
|
62
|
+
{name: 'salt', type: 'bytes32'},
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
66
|
+
const ARRAY_SUFFIX = /^(.*)\[(\d*)\]$/;
|
|
67
|
+
const HEX_DATA = /^0x([0-9a-fA-F]{2})*$/;
|
|
68
|
+
const ADDRESS = /^0x[0-9a-fA-F]{40}$/;
|
|
69
|
+
const UINT = /^uint(\d{1,3})$/;
|
|
70
|
+
const INT = /^int(\d{1,3})$/;
|
|
71
|
+
const BYTES_N = /^bytes(\d{1,2})$/;
|
|
72
|
+
|
|
73
|
+
const encoder = new TextEncoder();
|
|
74
|
+
|
|
75
|
+
function fail(path: string, message: string): never {
|
|
76
|
+
throw new Error(`${path}: ${message}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function concat(parts: readonly Uint8Array[]): Uint8Array {
|
|
80
|
+
let length = 0;
|
|
81
|
+
for (const p of parts) length += p.length;
|
|
82
|
+
const out = new Uint8Array(length);
|
|
83
|
+
let offset = 0;
|
|
84
|
+
for (const p of parts) {
|
|
85
|
+
out.set(p, offset);
|
|
86
|
+
offset += p.length;
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function hexToBytes(hex: string): Uint8Array {
|
|
92
|
+
const out = new Uint8Array((hex.length - 2) / 2);
|
|
93
|
+
for (let i = 0; i < out.length; i++)
|
|
94
|
+
out[i] = parseInt(hex.slice(2 + 2 * i, 4 + 2 * i), 16);
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** A non-negative bigint below 2^256 as one big-endian 32-byte word. */
|
|
99
|
+
function word(value: bigint): Uint8Array {
|
|
100
|
+
const out = new Uint8Array(32);
|
|
101
|
+
for (let i = 31; i >= 0; i--) {
|
|
102
|
+
out[i] = Number(value & 0xffn);
|
|
103
|
+
value >>= 8n;
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The type an array type holds, or `undefined` for a non-array type. */
|
|
109
|
+
function arrayOf(type: string): {item: string; length?: number} | undefined {
|
|
110
|
+
const m = ARRAY_SUFFIX.exec(type);
|
|
111
|
+
if (!m) return undefined;
|
|
112
|
+
return {item: m[1]!, length: m[2] === '' ? undefined : Number(m[2])};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The struct (or atomic) type at the root of a possibly-array type. */
|
|
116
|
+
function baseType(type: string): string {
|
|
117
|
+
let t = type;
|
|
118
|
+
for (let a = arrayOf(t); a; a = arrayOf(t)) t = a.item;
|
|
119
|
+
return t;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function isAtomic(type: string): boolean {
|
|
123
|
+
if (type === 'bool' || type === 'address' || type === 'string') return true;
|
|
124
|
+
if (type === 'bytes') return true;
|
|
125
|
+
const u = UINT.exec(type) ?? INT.exec(type);
|
|
126
|
+
if (u) {
|
|
127
|
+
const bits = Number(u[1]);
|
|
128
|
+
return bits >= 8 && bits <= 256 && bits % 8 === 0;
|
|
129
|
+
}
|
|
130
|
+
const b = BYTES_N.exec(type);
|
|
131
|
+
if (b) {
|
|
132
|
+
const n = Number(b[1]);
|
|
133
|
+
return n >= 1 && n <= 32;
|
|
134
|
+
}
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** An integer value as a bigint, from the forms a JSON-RPC request carries. */
|
|
139
|
+
function integer(path: string, value: unknown): bigint {
|
|
140
|
+
if (typeof value === 'bigint') return value;
|
|
141
|
+
if (typeof value === 'number') {
|
|
142
|
+
if (!Number.isSafeInteger(value))
|
|
143
|
+
fail(
|
|
144
|
+
path,
|
|
145
|
+
`${value} is not a safe integer; pass a large integer as a decimal or hex string.`,
|
|
146
|
+
);
|
|
147
|
+
return BigInt(value);
|
|
148
|
+
}
|
|
149
|
+
if (typeof value === 'string') {
|
|
150
|
+
if (/^-?[0-9]+$/.test(value)) return BigInt(value);
|
|
151
|
+
if (/^0x[0-9a-fA-F]+$/.test(value)) return BigInt(value);
|
|
152
|
+
if (/^-0x[0-9a-fA-F]+$/.test(value)) return -BigInt(value.slice(1));
|
|
153
|
+
}
|
|
154
|
+
return fail(
|
|
155
|
+
path,
|
|
156
|
+
`expected an integer (a number, a decimal string or a hex string), received ${JSON.stringify(value)}.`,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function hexData(path: string, value: unknown): Uint8Array {
|
|
161
|
+
if (typeof value !== 'string' || !HEX_DATA.test(value))
|
|
162
|
+
fail(
|
|
163
|
+
path,
|
|
164
|
+
`expected even-length 0x hex, received ${JSON.stringify(value)}.`,
|
|
165
|
+
);
|
|
166
|
+
return hexToBytes(value);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The encoder for one typed-data payload: its struct types, checked once.
|
|
171
|
+
*/
|
|
172
|
+
class Encoder {
|
|
173
|
+
private readonly typeHashes = new Map<string, Uint8Array>();
|
|
174
|
+
|
|
175
|
+
constructor(
|
|
176
|
+
private readonly types: Record<string, readonly TypedDataField[]>,
|
|
177
|
+
) {
|
|
178
|
+
for (const [name, fields] of Object.entries(types)) {
|
|
179
|
+
if (!IDENTIFIER.test(name))
|
|
180
|
+
fail(`types.${name}`, 'is not a valid struct type name.');
|
|
181
|
+
if (isAtomic(name))
|
|
182
|
+
fail(`types.${name}`, 'is the name of an atomic type, not a struct.');
|
|
183
|
+
if (!Array.isArray(fields))
|
|
184
|
+
fail(`types.${name}`, 'must be an array of {name, type}.');
|
|
185
|
+
const seen = new Set<string>();
|
|
186
|
+
fields.forEach((field, i) => {
|
|
187
|
+
const at = `types.${name}[${i}]`;
|
|
188
|
+
if (
|
|
189
|
+
field === null ||
|
|
190
|
+
typeof field !== 'object' ||
|
|
191
|
+
typeof field.name !== 'string' ||
|
|
192
|
+
typeof field.type !== 'string'
|
|
193
|
+
)
|
|
194
|
+
fail(at, 'must be {name, type} with both strings.');
|
|
195
|
+
if (!IDENTIFIER.test(field.name))
|
|
196
|
+
fail(at, `${JSON.stringify(field.name)} is not a valid member name.`);
|
|
197
|
+
if (seen.has(field.name))
|
|
198
|
+
fail(at, `member ${field.name} is declared twice.`);
|
|
199
|
+
seen.add(field.name);
|
|
200
|
+
const base = baseType(field.type);
|
|
201
|
+
if (!isAtomic(base) && !(base in types))
|
|
202
|
+
fail(
|
|
203
|
+
at,
|
|
204
|
+
`type ${JSON.stringify(field.type)} is neither atomic nor declared in \`types\`.`,
|
|
205
|
+
);
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** The struct types `primary` references, itself included, each once. */
|
|
211
|
+
private dependencies(
|
|
212
|
+
primary: string,
|
|
213
|
+
found = new Set<string>(),
|
|
214
|
+
): Set<string> {
|
|
215
|
+
if (found.has(primary) || !(primary in this.types)) return found;
|
|
216
|
+
found.add(primary);
|
|
217
|
+
for (const field of this.types[primary]!)
|
|
218
|
+
this.dependencies(baseType(field.type), found);
|
|
219
|
+
return found;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** `encodeType`: the primary type, then its references sorted by name. */
|
|
223
|
+
encodeType(primary: string): string {
|
|
224
|
+
const deps = [...this.dependencies(primary)].filter((t) => t !== primary);
|
|
225
|
+
deps.sort();
|
|
226
|
+
return [primary, ...deps]
|
|
227
|
+
.map(
|
|
228
|
+
(t) =>
|
|
229
|
+
`${t}(${this.types[t]!.map((f) => `${f.type} ${f.name}`).join(',')})`,
|
|
230
|
+
)
|
|
231
|
+
.join('');
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
typeHash(primary: string): Uint8Array {
|
|
235
|
+
let hash = this.typeHashes.get(primary);
|
|
236
|
+
if (!hash) {
|
|
237
|
+
hash = keccak_256(encoder.encode(this.encodeType(primary)));
|
|
238
|
+
this.typeHashes.set(primary, hash);
|
|
239
|
+
}
|
|
240
|
+
return hash;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** `hashStruct`: keccak256(typeHash ‖ encodeData(data)). */
|
|
244
|
+
hashStruct(primary: string, data: unknown, path: string): Uint8Array {
|
|
245
|
+
if (!(primary in this.types))
|
|
246
|
+
fail(
|
|
247
|
+
path,
|
|
248
|
+
`type ${JSON.stringify(primary)} is not declared in \`types\`.`,
|
|
249
|
+
);
|
|
250
|
+
if (data === null || typeof data !== 'object' || Array.isArray(data))
|
|
251
|
+
fail(path, `expected an object of type ${primary}.`);
|
|
252
|
+
const record = data as Record<string, unknown>;
|
|
253
|
+
const words = [this.typeHash(primary)];
|
|
254
|
+
for (const field of this.types[primary]!) {
|
|
255
|
+
const at = `${path}.${field.name}`;
|
|
256
|
+
if (record[field.name] === undefined)
|
|
257
|
+
fail(at, `member of ${primary} is missing.`);
|
|
258
|
+
words.push(this.encodeValue(field.type, record[field.name], at));
|
|
259
|
+
}
|
|
260
|
+
return keccak_256(concat(words));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** One member's 32-byte encoding. */
|
|
264
|
+
encodeValue(type: string, value: unknown, path: string): Uint8Array {
|
|
265
|
+
const array = arrayOf(type);
|
|
266
|
+
if (array) {
|
|
267
|
+
if (!Array.isArray(value)) fail(path, `expected an array (${type}).`);
|
|
268
|
+
if (array.length !== undefined && value.length !== array.length)
|
|
269
|
+
fail(
|
|
270
|
+
path,
|
|
271
|
+
`expected ${array.length} elements (${type}), received ${value.length}.`,
|
|
272
|
+
);
|
|
273
|
+
return keccak_256(
|
|
274
|
+
concat(
|
|
275
|
+
value.map((v, i) => this.encodeValue(array.item, v, `${path}[${i}]`)),
|
|
276
|
+
),
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
if (type in this.types) return this.hashStruct(type, value, path);
|
|
280
|
+
if (type === 'string') {
|
|
281
|
+
if (typeof value !== 'string') fail(path, 'expected a string.');
|
|
282
|
+
return keccak_256(encoder.encode(value));
|
|
283
|
+
}
|
|
284
|
+
if (type === 'bytes') return keccak_256(hexData(path, value));
|
|
285
|
+
if (type === 'bool') {
|
|
286
|
+
if (typeof value !== 'boolean') fail(path, 'expected true or false.');
|
|
287
|
+
return word(value ? 1n : 0n);
|
|
288
|
+
}
|
|
289
|
+
if (type === 'address') {
|
|
290
|
+
if (typeof value !== 'string' || !ADDRESS.test(value))
|
|
291
|
+
fail(
|
|
292
|
+
path,
|
|
293
|
+
`expected a 20-byte 0x address, received ${JSON.stringify(value)}.`,
|
|
294
|
+
);
|
|
295
|
+
return word(BigInt(value));
|
|
296
|
+
}
|
|
297
|
+
const u = UINT.exec(type);
|
|
298
|
+
if (u) {
|
|
299
|
+
const bits = BigInt(u[1]!);
|
|
300
|
+
const v = integer(path, value);
|
|
301
|
+
if (v < 0n || v >= 1n << bits)
|
|
302
|
+
fail(path, `${v} is out of range for ${type}.`);
|
|
303
|
+
return word(v);
|
|
304
|
+
}
|
|
305
|
+
const s = INT.exec(type);
|
|
306
|
+
if (s) {
|
|
307
|
+
const bits = BigInt(s[1]!);
|
|
308
|
+
const v = integer(path, value);
|
|
309
|
+
const half = 1n << (bits - 1n);
|
|
310
|
+
if (v < -half || v >= half)
|
|
311
|
+
fail(path, `${v} is out of range for ${type}.`);
|
|
312
|
+
// Sign-extended to 256 bits: two's complement.
|
|
313
|
+
return word(v < 0n ? (1n << 256n) + v : v);
|
|
314
|
+
}
|
|
315
|
+
const b = BYTES_N.exec(type);
|
|
316
|
+
if (b) {
|
|
317
|
+
const n = Number(b[1]);
|
|
318
|
+
const bytes = hexData(path, value);
|
|
319
|
+
if (bytes.length !== n)
|
|
320
|
+
fail(
|
|
321
|
+
path,
|
|
322
|
+
`expected exactly ${n} bytes (${type}), received ${bytes.length}.`,
|
|
323
|
+
);
|
|
324
|
+
const out = new Uint8Array(32);
|
|
325
|
+
out.set(bytes);
|
|
326
|
+
return out;
|
|
327
|
+
}
|
|
328
|
+
return fail(path, `unknown type ${JSON.stringify(type)}.`);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** The domain's struct type: the one declared, or derived from its fields. */
|
|
333
|
+
function domainFields(typed: TypedData): readonly TypedDataField[] {
|
|
334
|
+
const declared = typed.types.EIP712Domain;
|
|
335
|
+
if (declared) return declared;
|
|
336
|
+
return DOMAIN_FIELDS.filter((f) => typed.domain[f.name] !== undefined);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** Check the payload's shape, so the encoder can read it as {@link TypedData}. */
|
|
340
|
+
function asTypedData(value: unknown): TypedData {
|
|
341
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
342
|
+
fail('typedData', 'expected {types, primaryType, domain, message}.');
|
|
343
|
+
const t = value as Record<string, unknown>;
|
|
344
|
+
if (t.types === null || typeof t.types !== 'object' || Array.isArray(t.types))
|
|
345
|
+
fail('types', 'expected an object of struct types.');
|
|
346
|
+
if (typeof t.primaryType !== 'string')
|
|
347
|
+
fail('primaryType', 'expected the name of a type in `types`.');
|
|
348
|
+
const domain = t.domain ?? {};
|
|
349
|
+
if (domain === null || typeof domain !== 'object' || Array.isArray(domain))
|
|
350
|
+
fail('domain', 'expected an object.');
|
|
351
|
+
return {
|
|
352
|
+
types: t.types as TypedData['types'],
|
|
353
|
+
primaryType: t.primaryType,
|
|
354
|
+
domain: domain as Record<string, unknown>,
|
|
355
|
+
message: (t.message ?? {}) as Record<string, unknown>,
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** The domain separator: `hashStruct(eip712Domain)`. */
|
|
360
|
+
export function hashDomain(value: unknown): Uint8Array {
|
|
361
|
+
const typed = asTypedData(value);
|
|
362
|
+
const types = {...typed.types, EIP712Domain: domainFields(typed)};
|
|
363
|
+
return new Encoder(types).hashStruct('EIP712Domain', typed.domain, 'domain');
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* The EIP-712 digest a typed-data signature signs:
|
|
368
|
+
* `keccak256("\x19\x01" ‖ domainSeparator ‖ hashStruct(message))`.
|
|
369
|
+
* Throws an `Error` naming the offending path on a malformed payload.
|
|
370
|
+
*/
|
|
371
|
+
export function hashTypedData(value: unknown): Uint8Array {
|
|
372
|
+
const typed = asTypedData(value);
|
|
373
|
+
const types = {...typed.types, EIP712Domain: domainFields(typed)};
|
|
374
|
+
const enc = new Encoder(types);
|
|
375
|
+
const parts = [
|
|
376
|
+
Uint8Array.of(0x19, 0x01),
|
|
377
|
+
enc.hashStruct('EIP712Domain', typed.domain, 'domain'),
|
|
378
|
+
];
|
|
379
|
+
if (typed.primaryType !== 'EIP712Domain')
|
|
380
|
+
parts.push(enc.hashStruct(typed.primaryType, typed.message, 'message'));
|
|
381
|
+
return keccak_256(concat(parts));
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** `encodeType` for one of the payload's types (exposed for tests). */
|
|
385
|
+
export function encodeType(
|
|
386
|
+
types: Record<string, readonly TypedDataField[]>,
|
|
387
|
+
primary: string,
|
|
388
|
+
): string {
|
|
389
|
+
return new Encoder(types).encodeType(primary);
|
|
390
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* impersonation.ts: IMPERSONATION, opt-in, over webevm's trusted-sender
|
|
3
|
+
* primitive (ADR 0002, ADR 0017). `hardhat_impersonateAccount` /
|
|
4
|
+
* `hardhat_stopImpersonatingAccount` and their `anvil_` names maintain a set of
|
|
5
|
+
* impersonated addresses; `eth_sendTransaction` from one (./send.ts) is filled
|
|
6
|
+
* as a dev account's is, given a FABRICATED signature, and sent through
|
|
7
|
+
* `evm_sendRawTransactionAs(raw, from)`, which is why the node must be
|
|
8
|
+
* `senderMode: 'trusted'`.
|
|
9
|
+
*
|
|
10
|
+
* THE FABRICATED SIGNATURE IS UNIQUE PER SENDER, as ADR 0002's caller contract
|
|
11
|
+
* requires: `from` is not part of a transaction, so its hash comes from the
|
|
12
|
+
* bytes alone, and two impersonated senders sending otherwise identical
|
|
13
|
+
* transactions would collide under one dummy signature (anvil's bug, foundry
|
|
14
|
+
* #4210). So `r` is `keccak256(address) mod n` (secp256k1's group order) and
|
|
15
|
+
* `s` is fixed at 1, which is low (EIP-2), with y-parity 0. It is a signature
|
|
16
|
+
* of NOTHING: a `'recover'` node recovers some unrelated address from it, or
|
|
17
|
+
* none, so a dump holding such transactions is not replayable chain history.
|
|
18
|
+
*
|
|
19
|
+
* NO SIGNATURE THAT IS NOT ONE. The fabricated signature only ever reaches the
|
|
20
|
+
* node inside a transaction it is told the sender of; no signing method
|
|
21
|
+
* returns one (./sign.ts and `eth_signTransaction` refuse an impersonated
|
|
22
|
+
* address with {@link impersonatedNoKeyMessage}).
|
|
23
|
+
*
|
|
24
|
+
* DECISIONS made here (task `devnode-impersonation`), each user-visible and
|
|
25
|
+
* reversible before 1.0:
|
|
26
|
+
*
|
|
27
|
+
* - THE METHODS RETURN `null` UNDER BOTH NAME SETS, as anvil answers them
|
|
28
|
+
* (Hardhat answers `true`). This follows the core's rule for the `hardhat_`
|
|
29
|
+
* state-cheat aliases (`hardhat-and-anvil-names-for-the-state-cheats`: "what
|
|
30
|
+
* anvil returns under both names"); viem's `impersonateAccount` and rocketh
|
|
31
|
+
* ignore the result. Alternative: `true` for `hardhat_`, `null` for `anvil_`.
|
|
32
|
+
* - STOPPING AN ADDRESS THAT IS NOT IMPERSONATED IS NO ERROR, as on anvil and
|
|
33
|
+
* Hardhat. A malformed address is `-32602`.
|
|
34
|
+
* - WHILE THE OPTION IS OFF, the four methods are answered HERE with `-32601`
|
|
35
|
+
* naming the option, rather than forwarded to the core's generic `-32601`:
|
|
36
|
+
* rocketh swallows this error, so its text is the only place a user learns
|
|
37
|
+
* what to switch on.
|
|
38
|
+
* - THE METHODS RUN ON THE SEND QUEUE (./send.ts), so a send issued before a
|
|
39
|
+
* `stopImpersonatingAccount` is decided by the set as it was when the send
|
|
40
|
+
* was issued, not when it reached the front of the queue.
|
|
41
|
+
* - THE SET IS LAYER STATE, not chain state: it is not in `dumpState`, and
|
|
42
|
+
* `loadState` (which reaches the node, not this layer) leaves it as it is.
|
|
43
|
+
*/
|
|
44
|
+
import {keccak_256} from '@noble/hashes/sha3.js';
|
|
45
|
+
import {RpcError} from 'webevm';
|
|
46
|
+
|
|
47
|
+
const ADDRESS = /^0x[0-9a-fA-F]{40}$/;
|
|
48
|
+
|
|
49
|
+
/** secp256k1's group order: a signature's `r` and `s` lie in [1, n). */
|
|
50
|
+
const SECP256K1_N =
|
|
51
|
+
0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n;
|
|
52
|
+
|
|
53
|
+
/** The fabricated signature's scalars for `address` (lowercase 0x hex). */
|
|
54
|
+
export function fabricatedSignature(address: string): {r: bigint; s: bigint} {
|
|
55
|
+
const bytes = new Uint8Array(20);
|
|
56
|
+
for (let i = 0; i < 20; i++)
|
|
57
|
+
bytes[i] = parseInt(address.slice(2 + 2 * i, 4 + 2 * i), 16);
|
|
58
|
+
let hash = 0n;
|
|
59
|
+
for (const b of keccak_256(bytes)) hash = (hash << 8n) | BigInt(b);
|
|
60
|
+
// `r = 0` is no signature at all; keccak lands on a multiple of n with
|
|
61
|
+
// negligible probability, and 1 keeps the scalar valid if it ever does.
|
|
62
|
+
return {r: hash % SECP256K1_N || 1n, s: 1n};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The message of a `4100` refusal to SIGN for an impersonated address. */
|
|
66
|
+
export function impersonatedNoKeyMessage(
|
|
67
|
+
method: string,
|
|
68
|
+
address: string,
|
|
69
|
+
): string {
|
|
70
|
+
return (
|
|
71
|
+
`${method}: ${address} is impersonated, and @webevm/devnode holds no key for it: ` +
|
|
72
|
+
`an impersonated address can send (eth_sendTransaction), but nothing is ever signed for it, ` +
|
|
73
|
+
`since any signature would be fabricated. Configure its private key in the dev node's \`accounts\` option to sign with it.`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The impersonation methods, under both name sets. */
|
|
78
|
+
export const IMPERSONATION_METHODS = new Set([
|
|
79
|
+
'hardhat_impersonateAccount',
|
|
80
|
+
'anvil_impersonateAccount',
|
|
81
|
+
'hardhat_stopImpersonatingAccount',
|
|
82
|
+
'anvil_stopImpersonatingAccount',
|
|
83
|
+
]);
|
|
84
|
+
|
|
85
|
+
/** The impersonation methods' state and their answers. */
|
|
86
|
+
export interface Impersonation {
|
|
87
|
+
/** Whether the option is on. */
|
|
88
|
+
readonly enabled: boolean;
|
|
89
|
+
/** The impersonated addresses (lowercase), empty while off. */
|
|
90
|
+
readonly addresses: ReadonlySet<string>;
|
|
91
|
+
/** Answer one of {@link IMPERSONATION_METHODS}. */
|
|
92
|
+
request(method: string, params: unknown): null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function createImpersonation(enabled: boolean): Impersonation {
|
|
96
|
+
const addresses = new Set<string>();
|
|
97
|
+
return {
|
|
98
|
+
enabled,
|
|
99
|
+
addresses,
|
|
100
|
+
request(method, params) {
|
|
101
|
+
if (!enabled)
|
|
102
|
+
throw new RpcError(
|
|
103
|
+
-32601,
|
|
104
|
+
`${method}: impersonation is off on this dev node. Turn on the \`impersonation\` option: ` +
|
|
105
|
+
`createDevNode({impersonation: true}), or wrapDevNode(node, {impersonation: true}) on a node created with senderMode: 'trusted'.`,
|
|
106
|
+
);
|
|
107
|
+
const address = Array.isArray(params) ? params[0] : undefined;
|
|
108
|
+
if (typeof address !== 'string' || !ADDRESS.test(address))
|
|
109
|
+
throw new RpcError(
|
|
110
|
+
-32602,
|
|
111
|
+
`${method}: expected [address], a 20-byte hex address (received ${JSON.stringify(address ?? null)}).`,
|
|
112
|
+
);
|
|
113
|
+
const lower = address.toLowerCase();
|
|
114
|
+
if (method.endsWith('_impersonateAccount')) addresses.add(lower);
|
|
115
|
+
else addresses.delete(lower);
|
|
116
|
+
return null;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @webevm/devnode: dev accounts for webevm, as a `SlimNode` decorator funded at
|
|
3
|
+
* genesis. See `docs/adr/0017-dev-accounts-live-in-webevm-devnode-a-slimnode-decorator-funded-at-genesis.md`.
|
|
4
|
+
*/
|
|
5
|
+
export {createDevNode, wrapDevNode} from './dev-node.js';
|
|
6
|
+
export type {
|
|
7
|
+
DevNodeOptions,
|
|
8
|
+
ImpersonationOptions,
|
|
9
|
+
WrapDevNodeOptions,
|
|
10
|
+
} from './dev-node.js';
|
|
11
|
+
export {toAllocs} from './allocs.js';
|
|
12
|
+
export type {AllocAccount, Allocs} from './allocs.js';
|
|
13
|
+
export {
|
|
14
|
+
devAccountBalances,
|
|
15
|
+
DEFAULT_ACCOUNT_COUNT,
|
|
16
|
+
DEFAULT_BALANCE,
|
|
17
|
+
DEFAULT_DERIVATION_PATH,
|
|
18
|
+
DEFAULT_MNEMONIC,
|
|
19
|
+
} from './accounts.js';
|
|
20
|
+
export type {
|
|
21
|
+
DevAccountsConfig,
|
|
22
|
+
DevAccountsOptions,
|
|
23
|
+
DevFundingOptions,
|
|
24
|
+
MnemonicAccounts,
|
|
25
|
+
} from './accounts.js';
|