@aztec/node-keystore 0.0.1-commit.001888fc

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,763 @@
1
+ /**
2
+ * Keystore Manager
3
+ *
4
+ * Manages keystore configuration and delegates signing operations to appropriate signers.
5
+ */
6
+ import type { EthSigner } from '@aztec/ethereum/eth-signer';
7
+ import { Buffer32 } from '@aztec/foundation/buffer';
8
+ import { EthAddress } from '@aztec/foundation/eth-address';
9
+ import type { Signature } from '@aztec/foundation/eth-signature';
10
+ import { makeBackoff, retry } from '@aztec/foundation/retry';
11
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
12
+
13
+ import { Wallet } from '@ethersproject/wallet';
14
+ import { readFileSync, readdirSync, statSync } from 'fs';
15
+ import { extname, join } from 'path';
16
+ import type { TypedDataDefinition } from 'viem';
17
+ import { mnemonicToAccount } from 'viem/accounts';
18
+
19
+ import { ethPrivateKeySchema } from './schemas.js';
20
+ import { LocalSigner, RemoteSigner } from './signer.js';
21
+ import type {
22
+ AttesterAccounts,
23
+ EncryptedKeyFileConfig,
24
+ EthAccount,
25
+ EthAccounts,
26
+ EthRemoteSignerAccount,
27
+ EthRemoteSignerConfig,
28
+ KeyStore,
29
+ MnemonicConfig,
30
+ ProverKeyStore,
31
+ ValidatorKeyStore as ValidatorKeystoreConfig,
32
+ } from './types.js';
33
+
34
+ /**
35
+ * Error thrown when keystore operations fail
36
+ */
37
+ export class KeystoreError extends Error {
38
+ constructor(
39
+ message: string,
40
+ public override cause?: Error,
41
+ ) {
42
+ super(message);
43
+ this.name = 'KeystoreError';
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Keystore Manager - coordinates signing operations based on keystore configuration
49
+ */
50
+ export class KeystoreManager {
51
+ private readonly keystore: KeyStore;
52
+
53
+ /**
54
+ * Create a keystore manager from a parsed configuration.
55
+ * Performs a lightweight duplicate-attester check without decrypting JSON V3 or deriving mnemonics.
56
+ * @param keystore Parsed keystore configuration
57
+ */
58
+ constructor(keystore: KeyStore) {
59
+ this.keystore = keystore;
60
+ this.validateUniqueAttesterAddresses();
61
+ }
62
+
63
+ /**
64
+ * Validates all remote signers in the keystore are accessible and have the required addresses.
65
+ * Retries each web3signer URL with backoff to tolerate transient unavailability at boot time.
66
+ */
67
+ async validateSigners(): Promise<void> {
68
+ // Collect all remote signers with their addresses grouped by URL
69
+ const remoteSignersByUrl = new Map<string, Set<string>>();
70
+
71
+ // Helper to extract remote signer URL from config
72
+ const getUrl = (config: EthRemoteSignerConfig): string => {
73
+ return typeof config === 'string' ? config : config.remoteSignerUrl;
74
+ };
75
+
76
+ // Helper to collect remote signers from accounts
77
+ const collectRemoteSigners = (accounts: EthAccounts, defaultRemoteSigner?: EthRemoteSignerConfig): void => {
78
+ const processAccount = (account: EthAccount): void => {
79
+ if (typeof account === 'object' && !('path' in account) && !('mnemonic' in (account as any))) {
80
+ // This is a remote signer account
81
+ const remoteSigner = account as EthRemoteSignerAccount;
82
+ const address = 'address' in remoteSigner ? remoteSigner.address : remoteSigner;
83
+
84
+ let url: string;
85
+ if ('remoteSignerUrl' in remoteSigner && remoteSigner.remoteSignerUrl) {
86
+ url = remoteSigner.remoteSignerUrl;
87
+ } else if (defaultRemoteSigner) {
88
+ url = getUrl(defaultRemoteSigner);
89
+ } else {
90
+ return; // No remote signer URL available
91
+ }
92
+
93
+ if (!remoteSignersByUrl.has(url)) {
94
+ remoteSignersByUrl.set(url, new Set());
95
+ }
96
+ remoteSignersByUrl.get(url)!.add(address.toString());
97
+ }
98
+ };
99
+
100
+ if (Array.isArray(accounts)) {
101
+ accounts.forEach(account => collectRemoteSigners(account, defaultRemoteSigner));
102
+ } else if (typeof accounts === 'object' && 'mnemonic' in accounts) {
103
+ // Skip mnemonic configs
104
+ } else {
105
+ processAccount(accounts as EthAccount);
106
+ }
107
+ };
108
+
109
+ // Collect from validators
110
+ const validatorCount = this.getValidatorCount();
111
+ for (let i = 0; i < validatorCount; i++) {
112
+ const validator = this.getValidator(i);
113
+ const remoteSigner = validator.remoteSigner || this.keystore.remoteSigner;
114
+
115
+ collectRemoteSigners(this.extractEthAccountsFromAttester(validator.attester), remoteSigner);
116
+ if (validator.publisher) {
117
+ collectRemoteSigners(validator.publisher, remoteSigner);
118
+ }
119
+ }
120
+
121
+ // Collect from slasher
122
+ if (this.keystore.slasher) {
123
+ collectRemoteSigners(this.keystore.slasher, this.keystore.remoteSigner);
124
+ }
125
+
126
+ // Collect from prover
127
+ if (this.keystore.prover && typeof this.keystore.prover === 'object' && 'publisher' in this.keystore.prover) {
128
+ collectRemoteSigners(this.keystore.prover.publisher, this.keystore.remoteSigner);
129
+ }
130
+
131
+ // Validate each remote signer URL with all its addresses, retrying on transient failures
132
+ await Promise.all(
133
+ Array.from(remoteSignersByUrl.entries())
134
+ .filter(([, addresses]) => addresses.size > 0)
135
+ .map(([url, addresses]) =>
136
+ retry(
137
+ () => RemoteSigner.validateAccess(url, Array.from(addresses)),
138
+ `Validating web3signer at ${url}`,
139
+ makeBackoff([1, 2, 4, 8, 16]),
140
+ ),
141
+ ),
142
+ );
143
+ }
144
+
145
+ /**
146
+ * Validates that attester addresses are unique across all validators
147
+ * Only checks simple private key attesters, not JSON-V3 or mnemonic attesters,
148
+ * these are validated when decrypting the JSON-V3 keystore files
149
+ * @throws KeystoreError if duplicate attester addresses are found
150
+ */
151
+ private validateUniqueAttesterAddresses(): void {
152
+ const seenAddresses = new Set<string>();
153
+ const validatorCount = this.getValidatorCount();
154
+ for (let validatorIndex = 0; validatorIndex < validatorCount; validatorIndex++) {
155
+ const validator = this.getValidator(validatorIndex);
156
+ const addresses = this.extractAddressesWithoutSensitiveOperations(validator.attester);
157
+ for (const addr of addresses) {
158
+ const address = addr.toString().toLowerCase();
159
+ if (seenAddresses.has(address)) {
160
+ throw new KeystoreError(
161
+ `Duplicate attester address found: ${addr.toString()}. An attester address may only appear once across all configuration blocks.`,
162
+ );
163
+ }
164
+ seenAddresses.add(address);
165
+ }
166
+ }
167
+ }
168
+
169
+ /**
170
+ * Best-effort address extraction that avoids decryption/derivation (no JSON-V3 or mnemonic processing).
171
+ * This is used at construction time to check for obvious duplicates without throwing for invalid inputs.
172
+ */
173
+ private extractAddressesWithoutSensitiveOperations(accounts: AttesterAccounts): EthAddress[] {
174
+ const ethAccounts = this.extractEthAccountsFromAttester(accounts);
175
+ return this.extractAddressesFromEthAccountsNonSensitive(ethAccounts);
176
+ }
177
+
178
+ /**
179
+ * Extract addresses from EthAccounts without sensitive operations (no decryption/derivation).
180
+ */
181
+ private extractAddressesFromEthAccountsNonSensitive(accounts: EthAccounts): EthAddress[] {
182
+ const results: EthAddress[] = [];
183
+
184
+ const handleAccount = (account: EthAccount): void => {
185
+ if (typeof account === 'string') {
186
+ if (account.startsWith('0x') && account.length === 66) {
187
+ try {
188
+ const signer = new LocalSigner(Buffer32.fromString(ethPrivateKeySchema.parse(account)));
189
+ results.push(signer.address);
190
+ } catch {
191
+ // ignore invalid private key at construction time
192
+ }
193
+ }
194
+ return;
195
+ }
196
+
197
+ if ('path' in account) {
198
+ return;
199
+ }
200
+
201
+ if ('mnemonic' in (account as any)) {
202
+ return;
203
+ }
204
+
205
+ const remoteSigner: EthRemoteSignerAccount = account;
206
+ if ('address' in remoteSigner) {
207
+ results.push(remoteSigner.address);
208
+ return;
209
+ }
210
+ results.push(remoteSigner);
211
+ };
212
+
213
+ if (Array.isArray(accounts)) {
214
+ for (const account of accounts) {
215
+ handleAccount(account as EthAccount);
216
+ }
217
+ return results;
218
+ }
219
+
220
+ if (typeof accounts === 'object' && accounts !== null && 'mnemonic' in (accounts as any)) {
221
+ return results;
222
+ }
223
+
224
+ handleAccount(accounts as EthAccount);
225
+ return results;
226
+ }
227
+
228
+ /**
229
+ * Create signers for validator attester accounts
230
+ */
231
+ createAttesterSigners(validatorIndex: number): EthSigner[] {
232
+ const validator = this.getValidator(validatorIndex);
233
+ const ethAccounts = this.extractEthAccountsFromAttester(validator.attester);
234
+ return this.createSignersFromEthAccounts(ethAccounts, validator.remoteSigner || this.keystore.remoteSigner);
235
+ }
236
+
237
+ /**
238
+ * Create signers for validator publisher accounts (falls back to keystore-level publisher, then to attester if not specified)
239
+ */
240
+ createPublisherSigners(validatorIndex: number): EthSigner[] {
241
+ const validator = this.getValidator(validatorIndex);
242
+
243
+ if (validator.publisher) {
244
+ return this.createSignersFromEthAccounts(
245
+ validator.publisher,
246
+ validator.remoteSigner || this.keystore.remoteSigner,
247
+ );
248
+ }
249
+
250
+ // Fall back to keystore-level publisher
251
+ if (this.keystore.publisher) {
252
+ return this.createSignersFromEthAccounts(
253
+ this.keystore.publisher,
254
+ validator.remoteSigner || this.keystore.remoteSigner,
255
+ );
256
+ }
257
+
258
+ // Fall back to attester signers
259
+ return this.createAttesterSigners(validatorIndex);
260
+ }
261
+
262
+ createAllValidatorPublisherSigners(): EthSigner[] {
263
+ const numValidators = this.getValidatorCount();
264
+ const allPublishers = [];
265
+
266
+ for (let i = 0; i < numValidators; i++) {
267
+ allPublishers.push(...this.createPublisherSigners(i));
268
+ }
269
+
270
+ return allPublishers;
271
+ }
272
+
273
+ /**
274
+ * Create signers for slasher accounts
275
+ */
276
+ createSlasherSigners(): EthSigner[] {
277
+ if (!this.keystore.slasher) {
278
+ return [];
279
+ }
280
+
281
+ return this.createSignersFromEthAccounts(this.keystore.slasher, this.keystore.remoteSigner);
282
+ }
283
+
284
+ /**
285
+ * Create signers for prover accounts
286
+ */
287
+ createProverSigners(): { id: EthAddress | undefined; signers: EthSigner[] } | undefined {
288
+ if (!this.keystore.prover) {
289
+ return undefined;
290
+ }
291
+
292
+ // Handle prover being a private key, JSON key store or remote signer with nested address
293
+ if (
294
+ typeof this.keystore.prover === 'string' ||
295
+ 'path' in this.keystore.prover ||
296
+ 'address' in this.keystore.prover
297
+ ) {
298
+ const signers = this.createSignersFromEthAccounts(this.keystore.prover as EthAccount, this.keystore.remoteSigner);
299
+ return {
300
+ id: undefined,
301
+ signers,
302
+ };
303
+ }
304
+
305
+ // Handle prover as Id and specified publishers
306
+ if ('id' in this.keystore.prover) {
307
+ const id = this.keystore.prover.id;
308
+ const signers = this.createSignersFromEthAccounts(this.keystore.prover.publisher, this.keystore.remoteSigner);
309
+ return { id, signers };
310
+ }
311
+
312
+ // Here, prover is just an EthAddress for a remote signer
313
+ const signers = this.createSignersFromEthAccounts(this.keystore.prover, this.keystore.remoteSigner);
314
+ return {
315
+ id: undefined,
316
+ signers,
317
+ };
318
+ }
319
+
320
+ /**
321
+ * Get validator configuration by index
322
+ */
323
+ getValidator(index: number): ValidatorKeystoreConfig {
324
+ if (!this.keystore.validators || index >= this.keystore.validators.length || index < 0) {
325
+ throw new KeystoreError(`Validator index ${index} out of bounds`);
326
+ }
327
+ return this.keystore.validators[index];
328
+ }
329
+
330
+ /**
331
+ * Get validator count
332
+ */
333
+ getValidatorCount(): number {
334
+ return this.keystore.validators?.length || 0;
335
+ }
336
+
337
+ /**
338
+ * Get coinbase address for validator (falls back to keystore-level coinbase, then to the specific attester address)
339
+ */
340
+ getCoinbaseAddress(validatorIndex: number, attesterAddress: EthAddress): EthAddress {
341
+ const validator = this.getValidator(validatorIndex);
342
+
343
+ if (validator.coinbase) {
344
+ return validator.coinbase;
345
+ }
346
+
347
+ // Fall back to keystore-level coinbase
348
+ if (this.keystore.coinbase) {
349
+ return this.keystore.coinbase;
350
+ }
351
+
352
+ // Fall back to the specific attester address
353
+ return attesterAddress;
354
+ }
355
+
356
+ /**
357
+ * Get fee recipient for validator (falls back to keystore-level feeRecipient)
358
+ */
359
+ getFeeRecipient(validatorIndex: number): AztecAddress {
360
+ const validator = this.getValidator(validatorIndex);
361
+
362
+ if (validator.feeRecipient) {
363
+ return validator.feeRecipient;
364
+ }
365
+
366
+ // Fall back to keystore-level feeRecipient
367
+ if (this.keystore.feeRecipient) {
368
+ return this.keystore.feeRecipient;
369
+ }
370
+
371
+ throw new KeystoreError(
372
+ `No feeRecipient configured for validator ${validatorIndex}. You can set it at validator or keystore level.`,
373
+ );
374
+ }
375
+
376
+ /**
377
+ * Get the raw slasher configuration as provided in the keystore file.
378
+ * @returns The slasher accounts configuration or undefined if not set
379
+ */
380
+ getSlasherAccounts(): EthAccounts | undefined {
381
+ return this.keystore.slasher;
382
+ }
383
+
384
+ /**
385
+ * Get the raw prover configuration as provided in the keystore file.
386
+ * @returns The prover configuration or undefined if not set
387
+ */
388
+ getProverConfig(): ProverKeyStore | undefined {
389
+ return this.keystore.prover;
390
+ }
391
+
392
+ /**
393
+ * Resolves attester accounts (including JSON V3 and mnemonic) and checks for duplicate addresses across validators.
394
+ * Throws if the same resolved address appears in more than one validator configuration.
395
+ */
396
+ validateResolvedUniqueAttesterAddresses(): void {
397
+ const seenAddresses = new Set<string>();
398
+ const validatorCount = this.getValidatorCount();
399
+ for (let validatorIndex = 0; validatorIndex < validatorCount; validatorIndex++) {
400
+ const validator = this.getValidator(validatorIndex);
401
+ const signers = this.createSignersFromEthAccounts(
402
+ this.extractEthAccountsFromAttester(validator.attester),
403
+ validator.remoteSigner || this.keystore.remoteSigner,
404
+ );
405
+ for (const signer of signers) {
406
+ const address = signer.address.toString().toLowerCase();
407
+ if (seenAddresses.has(address)) {
408
+ throw new KeystoreError(
409
+ `Duplicate attester address found after resolving accounts: ${address}. An attester address may only appear once across all configuration blocks.`,
410
+ );
411
+ }
412
+ seenAddresses.add(address);
413
+ }
414
+ }
415
+ }
416
+
417
+ /**
418
+ * Create signers from EthAccounts configuration
419
+ */
420
+ private createSignersFromEthAccounts(
421
+ accounts: EthAccounts,
422
+ defaultRemoteSigner?: EthRemoteSignerConfig,
423
+ ): EthSigner[] {
424
+ if (typeof accounts === 'string') {
425
+ return [this.createSignerFromEthAccount(accounts, defaultRemoteSigner)];
426
+ }
427
+
428
+ if (Array.isArray(accounts)) {
429
+ const signers: EthSigner[] = [];
430
+ for (const account of accounts) {
431
+ const accountSigners = this.createSignersFromEthAccounts(account, defaultRemoteSigner);
432
+ signers.push(...accountSigners);
433
+ }
434
+ return signers;
435
+ }
436
+
437
+ // Mnemonic configuration
438
+ if ('mnemonic' in accounts) {
439
+ return this.createSignersFromMnemonic(accounts);
440
+ }
441
+
442
+ // Single account object - handle JSON V3 directory case
443
+ if ('path' in accounts) {
444
+ const result = this.createSignerFromJsonV3(accounts);
445
+ return result;
446
+ }
447
+
448
+ return [this.createSignerFromEthAccount(accounts, defaultRemoteSigner)];
449
+ }
450
+
451
+ /**
452
+ * Create a signer from a single EthAccount configuration
453
+ */
454
+ private createSignerFromEthAccount(account: EthAccount, defaultRemoteSigner?: EthRemoteSignerConfig): EthSigner {
455
+ // Private key (hex string)
456
+ if (typeof account === 'string') {
457
+ if (account.startsWith('0x') && account.length === 66) {
458
+ // Private key
459
+ return new LocalSigner(Buffer32.fromString(ethPrivateKeySchema.parse(account)));
460
+ } else {
461
+ throw new Error(`Invalid private key`);
462
+ }
463
+ }
464
+
465
+ // JSON V3 keystore
466
+ if ('path' in account) {
467
+ const result = this.createSignerFromJsonV3(account);
468
+ return result[0];
469
+ }
470
+
471
+ // Remote signer account
472
+ const remoteSigner: EthRemoteSignerAccount = account;
473
+
474
+ if ('address' in remoteSigner) {
475
+ // Remote signer with config
476
+ const config = remoteSigner.remoteSignerUrl
477
+ ? {
478
+ remoteSignerUrl: remoteSigner.remoteSignerUrl,
479
+ certPath: remoteSigner.certPath,
480
+ certPass: remoteSigner.certPass,
481
+ }
482
+ : defaultRemoteSigner;
483
+ if (!config) {
484
+ throw new KeystoreError(`No remote signer configuration found for address ${remoteSigner.address}`);
485
+ }
486
+
487
+ return new RemoteSigner(remoteSigner.address, config);
488
+ }
489
+
490
+ // Just an address - use default config
491
+ if (!defaultRemoteSigner) {
492
+ throw new KeystoreError(`No remote signer configuration found for address ${remoteSigner}`);
493
+ }
494
+ return new RemoteSigner(remoteSigner, defaultRemoteSigner);
495
+ }
496
+
497
+ /**
498
+ * Create signer from JSON V3 keystore file or directory
499
+ */
500
+ private createSignerFromJsonV3(config: EncryptedKeyFileConfig): EthSigner[] {
501
+ try {
502
+ const stats = statSync(config.path);
503
+
504
+ if (stats.isDirectory()) {
505
+ // Handle directory - load all JSON files
506
+ const files = readdirSync(config.path);
507
+ const signers: EthSigner[] = [];
508
+ const seenAddresses = new Map<string, string>(); // address -> file name
509
+
510
+ for (const file of files) {
511
+ // Only process .json files
512
+ if (extname(file).toLowerCase() !== '.json') {
513
+ continue;
514
+ }
515
+
516
+ const filePath = join(config.path, file);
517
+ try {
518
+ const signer = this.createSignerFromSingleJsonV3File(filePath, config.password);
519
+ const addressString = signer.address.toString().toLowerCase();
520
+ const existingFile = seenAddresses.get(addressString);
521
+ if (existingFile) {
522
+ throw new KeystoreError(
523
+ `Duplicate JSON V3 keystore address ${addressString} found in directory ${config.path} (files: ${existingFile} and ${file}). Each keystore must have a unique address.`,
524
+ );
525
+ }
526
+ seenAddresses.set(addressString, file);
527
+ signers.push(signer);
528
+ } catch (error) {
529
+ // Re-throw with file context
530
+ throw new KeystoreError(`Failed to load keystore file ${file}: ${error}`, error as Error);
531
+ }
532
+ }
533
+
534
+ if (signers.length === 0) {
535
+ throw new KeystoreError(`No JSON keystore files found in directory ${config.path}`);
536
+ }
537
+ return signers;
538
+ } else {
539
+ // Single file
540
+ return [this.createSignerFromSingleJsonV3File(config.path, config.password)];
541
+ }
542
+ } catch (error) {
543
+ if (error instanceof KeystoreError) {
544
+ throw error;
545
+ }
546
+ throw new KeystoreError(`Failed to access JSON V3 keystore ${config.path}: ${error}`, error as Error);
547
+ }
548
+ }
549
+
550
+ /**
551
+ * Create signer from a single JSON V3 keystore file
552
+ */
553
+ private createSignerFromSingleJsonV3File(filePath: string, password?: string): EthSigner {
554
+ try {
555
+ // Read the keystore file
556
+ const keystoreJson = readFileSync(filePath, 'utf8');
557
+
558
+ // Get password - prompt for it if not provided
559
+ const resolvedPassword = password;
560
+ if (!resolvedPassword) {
561
+ throw new KeystoreError(`No password provided for keystore ${filePath}. Provide password in config.`);
562
+ }
563
+
564
+ // Use @ethersproject/wallet to decrypt the JSON V3 keystore synchronously
565
+ const ethersWallet = Wallet.fromEncryptedJsonSync(keystoreJson, resolvedPassword);
566
+
567
+ // Convert the private key to our format
568
+ const privateKey = Buffer32.fromString(ethersWallet.privateKey);
569
+
570
+ return new LocalSigner(privateKey);
571
+ } catch (error) {
572
+ const err = error as Error;
573
+ throw new KeystoreError(`Failed to decrypt JSON V3 keystore ${filePath}: ${err.message}`, err);
574
+ }
575
+ }
576
+
577
+ /**
578
+ * Create signers from mnemonic configuration using BIP44 derivation
579
+ */
580
+ private createSignersFromMnemonic(config: MnemonicConfig): EthSigner[] {
581
+ const { mnemonic, addressIndex = 0, accountIndex = 0, addressCount = 1, accountCount = 1 } = config;
582
+ const signers: EthSigner[] = [];
583
+
584
+ try {
585
+ // Use viem's mnemonic derivation (imported at top of file)
586
+
587
+ // Normalize mnemonic by trimming whitespace
588
+ const normalizedMnemonic = mnemonic.trim();
589
+
590
+ for (let accIdx = accountIndex; accIdx < accountIndex + accountCount; accIdx++) {
591
+ for (let addrIdx = addressIndex; addrIdx < addressIndex + addressCount; addrIdx++) {
592
+ const viemAccount = mnemonicToAccount(normalizedMnemonic, {
593
+ accountIndex: accIdx,
594
+ addressIndex: addrIdx,
595
+ });
596
+
597
+ // Extract the private key from the viem account
598
+ const privateKeyBytes = viemAccount.getHdKey().privateKey!;
599
+ const privateKey = Buffer32.fromBuffer(Buffer.from(privateKeyBytes));
600
+ signers.push(new LocalSigner(privateKey));
601
+ }
602
+ }
603
+
604
+ return signers;
605
+ } catch (error) {
606
+ throw new KeystoreError(`Failed to derive accounts from mnemonic: ${error}`, error as Error);
607
+ }
608
+ }
609
+
610
+ /**
611
+ * Sign message with a specific signer
612
+ */
613
+ async signMessage(signer: EthSigner, message: Buffer32): Promise<Signature> {
614
+ return await signer.signMessage(message);
615
+ }
616
+
617
+ /**
618
+ * Sign typed data with a specific signer
619
+ */
620
+ async signTypedData(signer: EthSigner, typedData: TypedDataDefinition): Promise<Signature> {
621
+ return await signer.signTypedData(typedData);
622
+ }
623
+
624
+ /**
625
+ * Get the effective remote signer configuration for a specific attester address
626
+ * Precedence: account-level override > validator-level config > file-level default
627
+ */
628
+ getEffectiveRemoteSignerConfig(
629
+ validatorIndex: number,
630
+ attesterAddress: EthAddress,
631
+ ): EthRemoteSignerConfig | undefined {
632
+ const validator = this.getValidator(validatorIndex);
633
+
634
+ // Helper to get address from an account configuration
635
+ const getAddressFromAccount = (account: EthAccount): EthAddress | EthAddress[] | undefined => {
636
+ if (typeof account === 'string') {
637
+ if (account.startsWith('0x') && account.length === 66) {
638
+ // This is a private key - derive the address
639
+ try {
640
+ const signer = new LocalSigner(Buffer32.fromString(ethPrivateKeySchema.parse(account)));
641
+ return signer.address;
642
+ } catch {
643
+ return undefined;
644
+ }
645
+ }
646
+ return undefined;
647
+ }
648
+
649
+ // JSON V3 keystore
650
+ if ('path' in account) {
651
+ try {
652
+ const signers = this.createSignerFromJsonV3(account);
653
+ return signers.map(s => s.address);
654
+ } catch {
655
+ return undefined;
656
+ }
657
+ }
658
+
659
+ // Remote signer account, either it is an address or the address is nested
660
+ const remoteSigner: EthRemoteSignerAccount = account;
661
+ if ('address' in remoteSigner) {
662
+ return remoteSigner.address;
663
+ }
664
+ return remoteSigner;
665
+ };
666
+
667
+ // Helper to check if account matches and get its remote signer config
668
+ const checkAccount = (account: EthAccount): EthRemoteSignerConfig | undefined => {
669
+ const addresses = getAddressFromAccount(account);
670
+ if (!addresses) {
671
+ return undefined;
672
+ }
673
+
674
+ const addressArray = Array.isArray(addresses) ? addresses : [addresses];
675
+ const matches = addressArray.some(addr => addr.equals(attesterAddress));
676
+
677
+ if (!matches) {
678
+ return undefined;
679
+ }
680
+
681
+ // Found a match - determine the config to return
682
+ if (typeof account === 'string') {
683
+ return undefined;
684
+ }
685
+
686
+ // JSON V3 - local signer, no remote config
687
+ if ('path' in account) {
688
+ return undefined;
689
+ }
690
+
691
+ // Remote signer account with potential override
692
+ const remoteSigner: EthRemoteSignerAccount = account;
693
+
694
+ if ('address' in remoteSigner) {
695
+ // Has inline config
696
+ if (remoteSigner.remoteSignerUrl) {
697
+ return {
698
+ remoteSignerUrl: remoteSigner.remoteSignerUrl,
699
+ certPath: remoteSigner.certPath,
700
+ certPass: remoteSigner.certPass,
701
+ };
702
+ } else {
703
+ // No URL specified, use defaults
704
+ return validator.remoteSigner || this.keystore.remoteSigner;
705
+ }
706
+ }
707
+ // Just an address, use defaults
708
+ return validator.remoteSigner || this.keystore.remoteSigner;
709
+ };
710
+
711
+ // Normalize attester to EthAccounts and search
712
+ const normalized = this.extractEthAccountsFromAttester(validator.attester);
713
+
714
+ const findInEthAccounts = (accs: EthAccounts): EthRemoteSignerConfig | undefined => {
715
+ if (typeof accs === 'string') {
716
+ return checkAccount(accs);
717
+ }
718
+ if (Array.isArray(accs)) {
719
+ for (const a of accs as EthAccount[]) {
720
+ const res = checkAccount(a);
721
+ if (res !== undefined) {
722
+ return res;
723
+ }
724
+ }
725
+ return undefined;
726
+ }
727
+ if (typeof accs === 'object' && accs !== null && 'mnemonic' in accs) {
728
+ // mnemonic-derived keys are local signers; no remote signer config
729
+ return undefined;
730
+ }
731
+ return checkAccount(accs as EthAccount);
732
+ };
733
+
734
+ return findInEthAccounts(normalized);
735
+ }
736
+
737
+ /** Extract ETH accounts from AttesterAccounts */
738
+ private extractEthAccountsFromAttester(attester: AttesterAccounts): EthAccounts {
739
+ if (typeof attester === 'string') {
740
+ return attester;
741
+ }
742
+ if (Array.isArray(attester)) {
743
+ const out: EthAccount[] = [];
744
+ for (const item of attester) {
745
+ if (typeof item === 'string') {
746
+ out.push(item);
747
+ } else if ('eth' in (item as any)) {
748
+ out.push((item as any).eth as EthAccount);
749
+ } else if (!('mnemonic' in (item as any))) {
750
+ out.push(item as EthAccount);
751
+ }
752
+ }
753
+ return out;
754
+ }
755
+ if ('mnemonic' in (attester as any)) {
756
+ return attester as any;
757
+ }
758
+ if ('eth' in (attester as any)) {
759
+ return (attester as any).eth as EthAccount;
760
+ }
761
+ return attester as any;
762
+ }
763
+ }