@aztec/cli 2.1.0-rc.24 → 2.1.0-rc.27

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.
@@ -0,0 +1,321 @@
1
+ import { prettyPrintJSON } from '@aztec/cli/utils';
2
+ import { computeBn254G1PublicKeyCompressed, deriveBlsPrivateKey } from '@aztec/foundation/crypto';
3
+ import { createBn254Keystore } from '@aztec/foundation/crypto/bls/bn254_keystore';
4
+ import type { EthAddress } from '@aztec/foundation/eth-address';
5
+ import type { LogFn } from '@aztec/foundation/log';
6
+ import type { EthAccount, EthPrivateKey, ValidatorKeyStore } from '@aztec/node-keystore/types';
7
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
8
+
9
+ import { Wallet } from '@ethersproject/wallet';
10
+ import { constants as fsConstants, mkdirSync } from 'fs';
11
+ import { access, writeFile } from 'fs/promises';
12
+ import { homedir } from 'os';
13
+ import { dirname, isAbsolute, join } from 'path';
14
+ import { mnemonicToAccount } from 'viem/accounts';
15
+
16
+ export type ValidatorSummary = { attesterEth?: string; attesterBls?: string; publisherEth?: string[] };
17
+
18
+ export type BuildValidatorsInput = {
19
+ validatorCount: number;
20
+ publisherCount?: number;
21
+ accountIndex: number;
22
+ baseAddressIndex: number;
23
+ mnemonic: string;
24
+ ikm?: string;
25
+ blsPath?: string;
26
+ blsOnly?: boolean;
27
+ feeRecipient: AztecAddress;
28
+ coinbase?: EthAddress;
29
+ remoteSigner?: string;
30
+ fundingAccount?: EthAddress;
31
+ };
32
+
33
+ export function withValidatorIndex(path: string, index: number) {
34
+ const parts = path.split('/');
35
+ if (parts.length >= 4 && parts[0] === 'm' && parts[1] === '12381' && parts[2] === '3600') {
36
+ parts[3] = String(index);
37
+ return parts.join('/');
38
+ }
39
+ return path;
40
+ }
41
+
42
+ /**
43
+ * Compute a compressed BN254 G1 public key from a private key.
44
+ * @param privateKeyHex - Private key as 0x-prefixed hex string
45
+ * @returns Compressed G1 point (32 bytes with sign bit in MSB)
46
+ */
47
+ export async function computeBlsPublicKeyCompressed(privateKeyHex: string): Promise<string> {
48
+ return await computeBn254G1PublicKeyCompressed(privateKeyHex);
49
+ }
50
+
51
+ export function deriveEthAttester(
52
+ mnemonic: string,
53
+ baseAccountIndex: number,
54
+ addressIndex: number,
55
+ remoteSigner?: string,
56
+ ): EthAccount | EthPrivateKey {
57
+ const acct = mnemonicToAccount(mnemonic, { accountIndex: baseAccountIndex, addressIndex });
58
+ return remoteSigner
59
+ ? ({ address: acct.address as unknown as EthAddress, remoteSignerUrl: remoteSigner } as EthAccount)
60
+ : (('0x' + Buffer.from(acct.getHdKey().privateKey!).toString('hex')) as EthPrivateKey);
61
+ }
62
+
63
+ export async function buildValidatorEntries(input: BuildValidatorsInput) {
64
+ const {
65
+ validatorCount,
66
+ publisherCount = 0,
67
+ accountIndex,
68
+ baseAddressIndex,
69
+ mnemonic,
70
+ ikm,
71
+ blsPath,
72
+ blsOnly,
73
+ feeRecipient,
74
+ coinbase,
75
+ remoteSigner,
76
+ fundingAccount,
77
+ } = input;
78
+
79
+ const defaultBlsPath = 'm/12381/3600/0/0/0';
80
+ const summaries: ValidatorSummary[] = [];
81
+
82
+ const validators = await Promise.all(
83
+ Array.from({ length: validatorCount }, async (_unused, i) => {
84
+ const addressIndex = baseAddressIndex + i;
85
+ const basePath = blsPath ?? defaultBlsPath;
86
+ const perValidatorPath = withValidatorIndex(basePath, addressIndex);
87
+
88
+ const blsPrivKey = blsOnly || ikm || mnemonic ? deriveBlsPrivateKey(mnemonic, ikm, perValidatorPath) : undefined;
89
+ const blsPubCompressed = blsPrivKey ? await computeBlsPublicKeyCompressed(blsPrivKey) : undefined;
90
+
91
+ if (blsOnly) {
92
+ const attester = { bls: blsPrivKey! };
93
+ summaries.push({ attesterBls: blsPubCompressed });
94
+ return { attester, feeRecipient } as ValidatorKeyStore;
95
+ }
96
+
97
+ const ethAttester = deriveEthAttester(mnemonic, accountIndex, addressIndex, remoteSigner);
98
+ const attester = blsPrivKey ? { eth: ethAttester, bls: blsPrivKey } : ethAttester;
99
+
100
+ let publisherField: EthAccount | EthPrivateKey | (EthAccount | EthPrivateKey)[] | undefined;
101
+ const publisherAddresses: string[] = [];
102
+ if (publisherCount > 0) {
103
+ const publishersBaseIndex = baseAddressIndex + validatorCount + i * publisherCount;
104
+ const publisherAccounts = Array.from({ length: publisherCount }, (_unused2, j) => {
105
+ const publisherAddressIndex = publishersBaseIndex + j;
106
+ const pubAcct = mnemonicToAccount(mnemonic, {
107
+ accountIndex,
108
+ addressIndex: publisherAddressIndex,
109
+ });
110
+ publisherAddresses.push(pubAcct.address as unknown as string);
111
+ return remoteSigner
112
+ ? ({ address: pubAcct.address as unknown as EthAddress, remoteSignerUrl: remoteSigner } as EthAccount)
113
+ : (('0x' + Buffer.from(pubAcct.getHdKey().privateKey!).toString('hex')) as EthPrivateKey);
114
+ });
115
+ publisherField = publisherCount === 1 ? publisherAccounts[0] : publisherAccounts;
116
+ }
117
+
118
+ const acct = mnemonicToAccount(mnemonic, {
119
+ accountIndex,
120
+ addressIndex,
121
+ });
122
+ const attesterEthAddress = acct.address as unknown as string;
123
+ summaries.push({
124
+ attesterEth: attesterEthAddress,
125
+ attesterBls: blsPubCompressed,
126
+ publisherEth: publisherAddresses.length > 0 ? publisherAddresses : undefined,
127
+ });
128
+
129
+ return {
130
+ attester,
131
+ ...(publisherField !== undefined ? { publisher: publisherField } : {}),
132
+ feeRecipient,
133
+ coinbase,
134
+ fundingAccount,
135
+ } as ValidatorKeyStore;
136
+ }),
137
+ );
138
+
139
+ return { validators, summaries };
140
+ }
141
+
142
+ export async function resolveKeystoreOutputPath(dataDir?: string, file?: string) {
143
+ const defaultDataDir = join(homedir(), '.aztec', 'keystore');
144
+ const resolvedDir = dataDir && dataDir.length > 0 ? dataDir : defaultDataDir;
145
+ let outputPath: string;
146
+ if (file && file.length > 0) {
147
+ outputPath = isAbsolute(file) ? file : join(resolvedDir, file);
148
+ } else {
149
+ let index = 1;
150
+ while (true) {
151
+ const candidate = join(resolvedDir, `key${index}.json`);
152
+ try {
153
+ await access(candidate, fsConstants.F_OK);
154
+ index += 1;
155
+ } catch {
156
+ outputPath = candidate;
157
+ break;
158
+ }
159
+ }
160
+ }
161
+ return { resolvedDir, outputPath: outputPath! };
162
+ }
163
+
164
+ export async function writeKeystoreFile(path: string, keystore: unknown) {
165
+ mkdirSync(dirname(path), { recursive: true });
166
+ await writeFile(path, JSON.stringify(keystore, null, 2), { encoding: 'utf-8' });
167
+ }
168
+
169
+ export function logValidatorSummaries(log: LogFn, summaries: ValidatorSummary[]) {
170
+ const lines: string[] = [];
171
+ for (let i = 0; i < summaries.length; i++) {
172
+ const v = summaries[i];
173
+ lines.push(`acc${i + 1}:`);
174
+ lines.push(` attester:`);
175
+ if (v.attesterEth) {
176
+ lines.push(` eth: ${v.attesterEth}`);
177
+ }
178
+ if (v.attesterBls) {
179
+ lines.push(` bls: ${v.attesterBls}`);
180
+ }
181
+ if (v.publisherEth && v.publisherEth.length > 0) {
182
+ lines.push(` publisher:`);
183
+ for (const addr of v.publisherEth) {
184
+ lines.push(` - ${addr}`);
185
+ }
186
+ }
187
+ }
188
+ if (lines.length > 0) {
189
+ log(lines.join('\n'));
190
+ }
191
+ }
192
+
193
+ export function maybePrintJson(log: LogFn, jsonFlag: boolean | undefined, obj: unknown) {
194
+ if (jsonFlag) {
195
+ log(prettyPrintJSON(obj as Record<string, any>));
196
+ }
197
+ }
198
+
199
+ /**
200
+ * Writes a BN254 keystore file for a BN254 BLS private key.
201
+ * Returns the absolute path to the written file.
202
+ *
203
+ * @param outDir - Directory to write the keystore file to
204
+ * @param fileNameBase - Base name for the keystore file (will be sanitized)
205
+ * @param password - Password for encrypting the private key
206
+ * @param privateKeyHex - Private key as 0x-prefixed hex string (32 bytes)
207
+ * @param pubkeyHex - Public key as hex string
208
+ * @param derivationPath - BIP-44 style derivation path
209
+ * @returns Absolute path to the written keystore file
210
+ */
211
+ export async function writeBn254BlsKeystore(
212
+ outDir: string,
213
+ fileNameBase: string,
214
+ password: string,
215
+ privateKeyHex: string,
216
+ pubkeyHex: string,
217
+ derivationPath: string,
218
+ ): Promise<string> {
219
+ mkdirSync(outDir, { recursive: true });
220
+
221
+ const keystore = createBn254Keystore(password, privateKeyHex, pubkeyHex, derivationPath);
222
+
223
+ const safeBase = fileNameBase.replace(/[^a-zA-Z0-9_-]/g, '_');
224
+ const outPath = join(outDir, `keystore-${safeBase}.json`);
225
+ await writeFile(outPath, JSON.stringify(keystore, null, 2), { encoding: 'utf-8' });
226
+ return outPath;
227
+ }
228
+
229
+ /** Replace plaintext BLS keys in validators with { path, password } pointing to BN254 keystore files. */
230
+ export async function writeBlsBn254ToFile(
231
+ validators: ValidatorKeyStore[],
232
+ options: { outDir: string; password: string },
233
+ ): Promise<void> {
234
+ for (let i = 0; i < validators.length; i++) {
235
+ const v = validators[i];
236
+ if (!v || typeof v !== 'object' || !('attester' in v)) {
237
+ continue;
238
+ }
239
+ const att = (v as any).attester;
240
+
241
+ // Shapes: { bls: <hex> } or { eth: <ethAccount>, bls?: <hex> } or plain EthAccount
242
+ const blsKey: string | undefined = typeof att === 'object' && 'bls' in att ? (att as any).bls : undefined;
243
+ if (!blsKey || typeof blsKey !== 'string') {
244
+ continue;
245
+ }
246
+
247
+ const pub = await computeBlsPublicKeyCompressed(blsKey);
248
+ const path = 'm/12381/3600/0/0/0';
249
+ const fileBase = `${String(i + 1)}_${pub.slice(2, 18)}`;
250
+ const keystorePath = await writeBn254BlsKeystore(options.outDir, fileBase, options.password, blsKey, pub, path);
251
+
252
+ if (typeof att === 'object') {
253
+ (att as any).bls = { path: keystorePath, password: options.password };
254
+ }
255
+ }
256
+ }
257
+
258
+ /** Writes an Ethereum JSON V3 keystore using ethers, returns absolute path */
259
+ export async function writeEthJsonV3Keystore(
260
+ outDir: string,
261
+ fileNameBase: string,
262
+ password: string,
263
+ privateKeyHex: string,
264
+ ): Promise<string> {
265
+ const safeBase = fileNameBase.replace(/[^a-zA-Z0-9_-]/g, '_');
266
+ mkdirSync(outDir, { recursive: true });
267
+ const wallet = new Wallet(privateKeyHex);
268
+ const json = await wallet.encrypt(password);
269
+ const outPath = join(outDir, `keystore-eth-${safeBase}.json`);
270
+ await writeFile(outPath, json, { encoding: 'utf-8' });
271
+ return outPath;
272
+ }
273
+
274
+ /** Replace plaintext ETH keys in validators with { path, password } pointing to JSON V3 files. */
275
+ export async function writeEthJsonV3ToFile(
276
+ validators: ValidatorKeyStore[],
277
+ options: { outDir: string; password: string },
278
+ ): Promise<void> {
279
+ const maybeEncryptEth = async (account: any, label: string) => {
280
+ if (typeof account === 'string' && account.startsWith('0x') && account.length === 66) {
281
+ const fileBase = `${label}_${account.slice(2, 10)}`;
282
+ const p = await writeEthJsonV3Keystore(options.outDir, fileBase, options.password, account);
283
+ return { path: p, password: options.password };
284
+ }
285
+ return account;
286
+ };
287
+
288
+ for (let i = 0; i < validators.length; i++) {
289
+ const v = validators[i];
290
+ if (!v || typeof v !== 'object') {
291
+ continue;
292
+ }
293
+
294
+ // attester may be string (eth), object with eth, or remote signer
295
+ const att = (v as any).attester;
296
+ if (typeof att === 'string') {
297
+ (v as any).attester = await maybeEncryptEth(att, `attester_${i + 1}`);
298
+ } else if (att && typeof att === 'object' && 'eth' in att) {
299
+ (att as any).eth = await maybeEncryptEth((att as any).eth, `attester_${i + 1}`);
300
+ }
301
+
302
+ // publisher can be single or array
303
+ if ('publisher' in v) {
304
+ const pub = (v as any).publisher;
305
+ if (Array.isArray(pub)) {
306
+ const out: any[] = [];
307
+ for (let j = 0; j < pub.length; j++) {
308
+ out.push(await maybeEncryptEth(pub[j], `publisher_${i + 1}_${j + 1}`));
309
+ }
310
+ (v as any).publisher = out;
311
+ } else if (pub !== undefined) {
312
+ (v as any).publisher = await maybeEncryptEth(pub, `publisher_${i + 1}`);
313
+ }
314
+ }
315
+
316
+ // Optional fundingAccount within validator
317
+ if ('fundingAccount' in v) {
318
+ (v as any).fundingAccount = await maybeEncryptEth((v as any).fundingAccount, `funding_${i + 1}`);
319
+ }
320
+ }
321
+ }
@@ -114,6 +114,34 @@ export async function getTxSender(pxe: PXE, _from?: string) {
114
114
  return from;
115
115
  }
116
116
 
117
+ /**
118
+ * Parses and validates a hex string. Removes leading 0x if present, checks for hex validity,
119
+ * and enforces an optional minimum length.
120
+ * @param hex - The hex string to validate.
121
+ * @param minLen - Optional minimum length (in hex characters, after stripping '0x').
122
+ * @returns The normalized hex string (without leading 0x).
123
+ * @throws InvalidArgumentError if the string is not valid hex or does not meet the minimum length.
124
+ */
125
+ // minLen is now interpreted as the minimum number of bytes (2 hex characters per byte)
126
+ export function parseHex(hex: string, minLen?: number): `0x${string}` {
127
+ const normalized = hex.startsWith('0x') ? hex.slice(2) : hex;
128
+
129
+ if (!/^[0-9a-fA-F]*$/.test(normalized)) {
130
+ throw new InvalidArgumentError('Invalid hex string');
131
+ }
132
+
133
+ if (minLen !== undefined) {
134
+ const minHexLen = minLen * 2;
135
+ if (normalized.length < minHexLen) {
136
+ throw new InvalidArgumentError(
137
+ `Hex string is too short (length ${normalized.length}), minimum byte length is ${minLen} (hex chars: ${minHexLen})`,
138
+ );
139
+ }
140
+ }
141
+
142
+ return `0x${normalized}`;
143
+ }
144
+
117
145
  /**
118
146
  * Removes the leading 0x from a hex string. If no leading 0x is found the string is returned unchanged.
119
147
  * @param hex - A hex string
@@ -168,7 +196,7 @@ export function parseAztecAddress(address: string): AztecAddress {
168
196
  try {
169
197
  return AztecAddress.fromString(address);
170
198
  } catch {
171
- throw new InvalidArgumentError(`Invalid address: ${address}`);
199
+ throw new InvalidArgumentError(`Invalid Aztec address: ${address}`);
172
200
  }
173
201
  }
174
202
 
@@ -182,7 +210,7 @@ export function parseEthereumAddress(address: string): EthAddress {
182
210
  try {
183
211
  return EthAddress.fromString(address);
184
212
  } catch {
185
- throw new InvalidArgumentError(`Invalid address: ${address}`);
213
+ throw new InvalidArgumentError(`Invalid Ethereumaddress: ${address}`);
186
214
  }
187
215
  }
188
216
 
@@ -236,7 +264,11 @@ export function parseOptionalSelector(selector: string): FunctionSelector | unde
236
264
  * @returns The parsed integer, or undefined if the input string is falsy.
237
265
  * @throws If the input is not a valid integer.
238
266
  */
239
- export function parseOptionalInteger(value: string): number | undefined {
267
+ export function parseOptionalInteger(
268
+ value: string,
269
+ min: number = Number.MIN_SAFE_INTEGER,
270
+ max: number = Number.MAX_SAFE_INTEGER,
271
+ ): number | undefined {
240
272
  if (!value) {
241
273
  return undefined;
242
274
  }
@@ -244,6 +276,12 @@ export function parseOptionalInteger(value: string): number | undefined {
244
276
  if (!Number.isInteger(parsed)) {
245
277
  throw new InvalidArgumentError('Invalid integer.');
246
278
  }
279
+ if (parsed < min) {
280
+ throw new InvalidArgumentError(`Value must be greater than ${min}.`);
281
+ }
282
+ if (parsed > max) {
283
+ throw new InvalidArgumentError(`Value must be less than ${max}.`);
284
+ }
247
285
  return parsed;
248
286
  }
249
287