@aztec/validator-client 0.0.1-commit.1142ef1 → 0.0.1-commit.11bf3dd6e

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.
Files changed (73) hide show
  1. package/README.md +92 -24
  2. package/dest/checkpoint_builder.d.ts +35 -26
  3. package/dest/checkpoint_builder.d.ts.map +1 -1
  4. package/dest/checkpoint_builder.js +144 -48
  5. package/dest/config.d.ts +1 -1
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +33 -19
  8. package/dest/duties/validation_service.d.ts +21 -10
  9. package/dest/duties/validation_service.d.ts.map +1 -1
  10. package/dest/duties/validation_service.js +67 -32
  11. package/dest/factory.d.ts +8 -5
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +6 -5
  14. package/dest/index.d.ts +2 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +1 -2
  17. package/dest/key_store/ha_key_store.d.ts +99 -0
  18. package/dest/key_store/ha_key_store.d.ts.map +1 -0
  19. package/dest/key_store/ha_key_store.js +208 -0
  20. package/dest/key_store/index.d.ts +2 -1
  21. package/dest/key_store/index.d.ts.map +1 -1
  22. package/dest/key_store/index.js +1 -0
  23. package/dest/key_store/interface.d.ts +36 -6
  24. package/dest/key_store/interface.d.ts.map +1 -1
  25. package/dest/key_store/local_key_store.d.ts +10 -5
  26. package/dest/key_store/local_key_store.d.ts.map +1 -1
  27. package/dest/key_store/local_key_store.js +8 -4
  28. package/dest/key_store/node_keystore_adapter.d.ts +18 -5
  29. package/dest/key_store/node_keystore_adapter.d.ts.map +1 -1
  30. package/dest/key_store/node_keystore_adapter.js +18 -4
  31. package/dest/key_store/web3signer_key_store.d.ts +10 -5
  32. package/dest/key_store/web3signer_key_store.d.ts.map +1 -1
  33. package/dest/key_store/web3signer_key_store.js +8 -4
  34. package/dest/metrics.d.ts +16 -3
  35. package/dest/metrics.d.ts.map +1 -1
  36. package/dest/metrics.js +58 -5
  37. package/dest/proposal_handler.d.ts +108 -0
  38. package/dest/proposal_handler.d.ts.map +1 -0
  39. package/dest/proposal_handler.js +974 -0
  40. package/dest/validator.d.ts +47 -25
  41. package/dest/validator.d.ts.map +1 -1
  42. package/dest/validator.js +225 -229
  43. package/package.json +21 -17
  44. package/src/checkpoint_builder.ts +211 -61
  45. package/src/config.ts +32 -18
  46. package/src/duties/validation_service.ts +92 -36
  47. package/src/factory.ts +11 -3
  48. package/src/index.ts +1 -2
  49. package/src/key_store/ha_key_store.ts +269 -0
  50. package/src/key_store/index.ts +1 -0
  51. package/src/key_store/interface.ts +44 -5
  52. package/src/key_store/local_key_store.ts +13 -4
  53. package/src/key_store/node_keystore_adapter.ts +27 -4
  54. package/src/key_store/web3signer_key_store.ts +17 -4
  55. package/src/metrics.ts +81 -6
  56. package/src/proposal_handler.ts +1042 -0
  57. package/src/validator.ts +312 -265
  58. package/dest/block_proposal_handler.d.ts +0 -63
  59. package/dest/block_proposal_handler.d.ts.map +0 -1
  60. package/dest/block_proposal_handler.js +0 -551
  61. package/dest/tx_validator/index.d.ts +0 -3
  62. package/dest/tx_validator/index.d.ts.map +0 -1
  63. package/dest/tx_validator/index.js +0 -2
  64. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  65. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  66. package/dest/tx_validator/nullifier_cache.js +0 -24
  67. package/dest/tx_validator/tx_validator_factory.d.ts +0 -18
  68. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  69. package/dest/tx_validator/tx_validator_factory.js +0 -53
  70. package/src/block_proposal_handler.ts +0 -556
  71. package/src/tx_validator/index.ts +0 -2
  72. package/src/tx_validator/nullifier_cache.ts +0 -30
  73. package/src/tx_validator/tx_validator_factory.ts +0 -133
@@ -1,3 +1,4 @@
1
+ import { type CheckpointNumber, IndexWithinCheckpoint, type SlotNumber } from '@aztec/foundation/branded-types';
1
2
  import { Buffer32 } from '@aztec/foundation/buffer';
2
3
  import { keccak256 } from '@aztec/foundation/crypto/keccak';
3
4
  import { Fr } from '@aztec/foundation/curves/bn254';
@@ -5,7 +6,6 @@ import type { EthAddress } from '@aztec/foundation/eth-address';
5
6
  import type { Signature } from '@aztec/foundation/eth-signature';
6
7
  import { createLogger } from '@aztec/foundation/log';
7
8
  import type { CommitteeAttestationsAndSigners } from '@aztec/stdlib/block';
8
- import type { CreateCheckpointProposalLastBlockData } from '@aztec/stdlib/interfaces/server';
9
9
  import {
10
10
  BlockProposal,
11
11
  type BlockProposalOptions,
@@ -18,6 +18,8 @@ import {
18
18
  } from '@aztec/stdlib/p2p';
19
19
  import type { CheckpointHeader } from '@aztec/stdlib/rollup';
20
20
  import type { BlockHeader, Tx } from '@aztec/stdlib/tx';
21
+ import { DutyAlreadySignedError, SlashingProtectionError } from '@aztec/validator-ha-signer/errors';
22
+ import { DutyType, type SigningContext } from '@aztec/validator-ha-signer/types';
21
23
 
22
24
  import type { ValidatorKeyStore } from '../key_store/interface.js';
23
25
 
@@ -31,34 +33,42 @@ export class ValidationService {
31
33
  * Create a block proposal with the given header, archive, and transactions
32
34
  *
33
35
  * @param blockHeader - The block header
34
- * @param indexWithinCheckpoint - Index of this block within the checkpoint (0-indexed)
36
+ * @param blockIndexWithinCheckpoint - The block index within checkpoint for HA signing context
35
37
  * @param inHash - Hash of L1 to L2 messages for this checkpoint
36
38
  * @param archive - The archive of the current block
37
- * @param txs - TxHash[] ordered list of transactions
39
+ * @param txs - Ordered list of transactions (Tx[])
40
+ * @param proposerAttesterAddress - The address of the proposer/attester, or undefined
38
41
  * @param options - Block proposal options (including broadcastInvalidBlockProposal for testing)
39
42
  *
40
43
  * @returns A block proposal signing the above information
44
+ * @throws DutyAlreadySignedError if HA signer indicates duty already signed by another node
45
+ * @throws SlashingProtectionError if attempting to sign different data for same slot
41
46
  */
42
47
  public createBlockProposal(
43
48
  blockHeader: BlockHeader,
44
- indexWithinCheckpoint: number,
49
+ checkpointNumber: CheckpointNumber,
50
+ blockIndexWithinCheckpoint: IndexWithinCheckpoint,
45
51
  inHash: Fr,
46
52
  archive: Fr,
47
53
  txs: Tx[],
48
54
  proposerAttesterAddress: EthAddress | undefined,
49
55
  options: BlockProposalOptions,
50
56
  ): Promise<BlockProposal> {
51
- const payloadSigner = this.getPayloadSigner(proposerAttesterAddress);
52
-
53
57
  // For testing: change the new archive to trigger state_mismatch validation failure
54
58
  if (options.broadcastInvalidBlockProposal) {
55
59
  archive = Fr.random();
56
60
  this.log.warn(`Creating INVALID block proposal for slot ${blockHeader.globalVariables.slotNumber}`);
57
61
  }
58
62
 
63
+ // Create a signer that uses the appropriate address
64
+ const address = proposerAttesterAddress ?? this.keyStore.getAddress(0);
65
+ const payloadSigner = (payload: Buffer32, context: SigningContext) =>
66
+ this.keyStore.signMessageWithAddress(address, payload, context);
67
+
59
68
  return BlockProposal.createProposalFromSigner(
60
69
  blockHeader,
61
- indexWithinCheckpoint,
70
+ checkpointNumber,
71
+ blockIndexWithinCheckpoint,
62
72
  inHash,
63
73
  archive,
64
74
  txs.map(tx => tx.getTxHash()),
@@ -72,7 +82,7 @@ export class ValidationService {
72
82
  *
73
83
  * @param checkpointHeader - The checkpoint header containing aggregated data
74
84
  * @param archive - The archive of the checkpoint
75
- * @param lastBlockInfo - Info about the last block (header, index, txs) or undefined
85
+ * @param lastBlockProposal - Signed block proposal for the last block in the checkpoint, or undefined
76
86
  * @param proposerAttesterAddress - The address of the proposer
77
87
  * @param options - Checkpoint proposal options
78
88
  *
@@ -81,37 +91,34 @@ export class ValidationService {
81
91
  public createCheckpointProposal(
82
92
  checkpointHeader: CheckpointHeader,
83
93
  archive: Fr,
84
- lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
94
+ checkpointNumber: CheckpointNumber,
95
+ feeAssetPriceModifier: bigint,
96
+ lastBlockProposal: BlockProposal | undefined,
85
97
  proposerAttesterAddress: EthAddress | undefined,
86
98
  options: CheckpointProposalOptions,
87
99
  ): Promise<CheckpointProposal> {
88
- const payloadSigner = this.getPayloadSigner(proposerAttesterAddress);
89
-
90
- // For testing: change the archive to trigger state_mismatch validation failure
100
+ // For testing: change the archive to trigger state_mismatch validation failure.
101
+ // If there's a last block proposal, use its (already invalid) archive to keep signatures consistent
102
+ // so P2P validation passes and the slasher can detect the offense.
91
103
  if (options.broadcastInvalidCheckpointProposal) {
92
- archive = Fr.random();
104
+ archive = lastBlockProposal?.archiveRoot ?? Fr.random();
93
105
  this.log.warn(`Creating INVALID checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
94
106
  }
95
107
 
96
- // Last block to include in the proposal
97
- const lastBlock = lastBlockInfo && {
98
- blockHeader: lastBlockInfo.blockHeader,
99
- indexWithinCheckpoint: lastBlockInfo.indexWithinCheckpoint,
100
- txHashes: lastBlockInfo.txs.map(tx => tx.getTxHash()),
101
- txs: options.publishFullTxs ? lastBlockInfo.txs : undefined,
108
+ // Create a signer that takes payload and context, and uses the appropriate address
109
+ const payloadSigner = (payload: Buffer32, context: SigningContext) => {
110
+ const address = proposerAttesterAddress ?? this.keyStore.getAddress(0);
111
+ return this.keyStore.signMessageWithAddress(address, payload, context);
102
112
  };
103
113
 
104
- return CheckpointProposal.createProposalFromSigner(checkpointHeader, archive, lastBlock, payloadSigner);
105
- }
106
-
107
- private getPayloadSigner(proposerAttesterAddress: EthAddress | undefined): (payload: Buffer32) => Promise<Signature> {
108
- if (proposerAttesterAddress !== undefined) {
109
- return (payload: Buffer32) => this.keyStore.signMessageWithAddress(proposerAttesterAddress, payload);
110
- } else {
111
- // if there is no proposer attester address, just use the first signer
112
- const signer = this.keyStore.getAddress(0);
113
- return (payload: Buffer32) => this.keyStore.signMessageWithAddress(signer, payload);
114
- }
114
+ return CheckpointProposal.createProposalFromSigner(
115
+ checkpointHeader,
116
+ archive,
117
+ checkpointNumber,
118
+ feeAssetPriceModifier,
119
+ lastBlockProposal,
120
+ payloadSigner,
121
+ );
115
122
  }
116
123
 
117
124
  /**
@@ -127,25 +134,74 @@ export class ValidationService {
127
134
  async attestToCheckpointProposal(
128
135
  proposal: CheckpointProposalCore,
129
136
  attestors: EthAddress[],
137
+ checkpointNumber: CheckpointNumber,
130
138
  ): Promise<CheckpointAttestation[]> {
131
139
  // Create the attestation payload from the checkpoint proposal
132
- const payload = new ConsensusPayload(proposal.checkpointHeader, proposal.archive);
140
+ const payload = new ConsensusPayload(proposal.checkpointHeader, proposal.archive, proposal.feeAssetPriceModifier);
133
141
  const buf = Buffer32.fromBuffer(
134
142
  keccak256(payload.getPayloadToSign(SignatureDomainSeparator.checkpointAttestation)),
135
143
  );
136
- const signatures = await Promise.all(
137
- attestors.map(attestor => this.keyStore.signMessageWithAddress(attestor, buf)),
144
+
145
+ const context: SigningContext = {
146
+ slot: proposal.slotNumber,
147
+ checkpointNumber,
148
+ dutyType: DutyType.ATTESTATION,
149
+ };
150
+
151
+ // Sign each attestor in parallel, catching HA errors per-attestor
152
+ const results = await Promise.allSettled(
153
+ attestors.map(async attestor => {
154
+ const sig = await this.keyStore.signMessageWithAddress(attestor, buf, context);
155
+ // return new BlockAttestation(proposal.payload, sig, proposal.signature);
156
+ return new CheckpointAttestation(payload, sig, proposal.signature);
157
+ }),
138
158
  );
139
- return signatures.map(sig => new CheckpointAttestation(payload, sig, proposal.signature));
159
+
160
+ const attestations: CheckpointAttestation[] = [];
161
+ for (let i = 0; i < results.length; i++) {
162
+ const result = results[i];
163
+ if (result.status === 'fulfilled') {
164
+ attestations.push(result.value);
165
+ } else {
166
+ const error = result.reason;
167
+ if (error instanceof DutyAlreadySignedError || error instanceof SlashingProtectionError) {
168
+ this.log.verbose(
169
+ `Attestation for slot ${proposal.slotNumber} by ${attestors[i]} already signed by another High-Availability node`,
170
+ );
171
+ // Continue with remaining attestors
172
+ } else {
173
+ throw error;
174
+ }
175
+ }
176
+ }
177
+
178
+ return attestations;
140
179
  }
141
180
 
142
- async signAttestationsAndSigners(
181
+ /**
182
+ * Sign attestations and signers payload
183
+ * @param attestationsAndSigners - The attestations and signers to sign
184
+ * @param proposer - The proposer address to sign with
185
+ * @param slot - The slot number for HA signing context
186
+ * @returns signature
187
+ * @throws DutyAlreadySignedError if already signed by another HA node
188
+ * @throws SlashingProtectionError if attempting to sign different data for same slot
189
+ */
190
+ signAttestationsAndSigners(
143
191
  attestationsAndSigners: CommitteeAttestationsAndSigners,
144
192
  proposer: EthAddress,
193
+ slot: SlotNumber,
194
+ checkpointNumber: CheckpointNumber,
145
195
  ): Promise<Signature> {
196
+ const context: SigningContext = {
197
+ slot,
198
+ checkpointNumber,
199
+ dutyType: DutyType.ATTESTATIONS_AND_SIGNERS,
200
+ };
201
+
146
202
  const buf = Buffer32.fromBuffer(
147
203
  keccak256(attestationsAndSigners.getPayloadToSign(SignatureDomainSeparator.attestationsAndSigners)),
148
204
  );
149
- return await this.keyStore.signMessageWithAddress(proposer, buf);
205
+ return this.keyStore.signMessageWithAddress(proposer, buf, context);
150
206
  }
151
207
  }
package/src/factory.ts CHANGED
@@ -7,13 +7,14 @@ import type { L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
7
7
  import type { ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
8
8
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
9
9
  import type { TelemetryClient } from '@aztec/telemetry-client';
10
+ import type { SlashingProtectionDatabase } from '@aztec/validator-ha-signer/types';
10
11
 
11
- import { BlockProposalHandler } from './block_proposal_handler.js';
12
12
  import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
13
13
  import { ValidatorMetrics } from './metrics.js';
14
+ import { ProposalHandler } from './proposal_handler.js';
14
15
  import { ValidatorClient } from './validator.js';
15
16
 
16
- export function createBlockProposalHandler(
17
+ export function createProposalHandler(
17
18
  config: ValidatorClientFullConfig,
18
19
  deps: {
19
20
  checkpointsBuilder: FullNodeCheckpointsBuilder;
@@ -22,6 +23,7 @@ export function createBlockProposalHandler(
22
23
  l1ToL2MessageSource: L1ToL2MessageSource;
23
24
  p2pClient: P2PClient;
24
25
  epochCache: EpochCache;
26
+ blobClient: BlobClientInterface;
25
27
  dateProvider: DateProvider;
26
28
  telemetry: TelemetryClient;
27
29
  },
@@ -29,18 +31,22 @@ export function createBlockProposalHandler(
29
31
  const metrics = new ValidatorMetrics(deps.telemetry);
30
32
  const blockProposalValidator = new BlockProposalValidator(deps.epochCache, {
31
33
  txsPermitted: !config.disableTransactions,
34
+ maxTxsPerBlock: config.validateMaxTxsPerBlock ?? config.validateMaxTxsPerCheckpoint,
32
35
  });
33
- return new BlockProposalHandler(
36
+ return new ProposalHandler(
34
37
  deps.checkpointsBuilder,
35
38
  deps.worldState,
36
39
  deps.blockSource,
37
40
  deps.l1ToL2MessageSource,
38
41
  deps.p2pClient.getTxProvider(),
39
42
  blockProposalValidator,
43
+ deps.epochCache,
40
44
  config,
45
+ deps.blobClient,
41
46
  metrics,
42
47
  deps.dateProvider,
43
48
  deps.telemetry,
49
+ undefined,
44
50
  );
45
51
  }
46
52
 
@@ -57,6 +63,7 @@ export function createValidatorClient(
57
63
  epochCache: EpochCache;
58
64
  keyStoreManager: KeystoreManager | undefined;
59
65
  blobClient: BlobClientInterface;
66
+ slashingProtectionDb?: SlashingProtectionDatabase;
60
67
  },
61
68
  ) {
62
69
  if (config.disableValidator || !deps.keyStoreManager) {
@@ -77,5 +84,6 @@ export function createValidatorClient(
77
84
  deps.blobClient,
78
85
  deps.dateProvider,
79
86
  deps.telemetry,
87
+ deps.slashingProtectionDb,
80
88
  );
81
89
  }
package/src/index.ts CHANGED
@@ -1,7 +1,6 @@
1
- export * from './block_proposal_handler.js';
1
+ export * from './proposal_handler.js';
2
2
  export * from './checkpoint_builder.js';
3
3
  export * from './config.js';
4
4
  export * from './factory.js';
5
5
  export * from './validator.js';
6
6
  export * from './key_store/index.js';
7
- export * from './tx_validator/index.js';
@@ -0,0 +1,269 @@
1
+ /**
2
+ * High Availability Key Store
3
+ *
4
+ * A ValidatorKeyStore wrapper that adds slashing protection for HA validator setups.
5
+ * When multiple validator nodes are running, only one node will sign for a given duty.
6
+ */
7
+ import { Buffer32 } from '@aztec/foundation/buffer';
8
+ import type { EthAddress } from '@aztec/foundation/eth-address';
9
+ import type { Signature } from '@aztec/foundation/eth-signature';
10
+ import { createLogger } from '@aztec/foundation/log';
11
+ import type { EthRemoteSignerConfig } from '@aztec/node-keystore';
12
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
13
+ import { DutyAlreadySignedError, SlashingProtectionError } from '@aztec/validator-ha-signer/errors';
14
+ import {
15
+ type HAProtectedSigningContext,
16
+ type SigningContext,
17
+ isHAProtectedContext,
18
+ } from '@aztec/validator-ha-signer/types';
19
+ import type { ValidatorHASigner } from '@aztec/validator-ha-signer/validator-ha-signer';
20
+
21
+ import { type TypedDataDefinition, hashTypedData } from 'viem';
22
+
23
+ import type { ExtendedValidatorKeyStore } from './interface.js';
24
+
25
+ /**
26
+ * High Availability Key Store
27
+ *
28
+ * Wraps a base ExtendedValidatorKeyStore and ValidatorHASigner to provide
29
+ * HA-protected signing operations (when context is provided).
30
+ *
31
+ * The extended interface methods (getAttesterAddresses, getCoinbaseAddress, etc.)
32
+ * are pure pass-through since they don't require HA coordination.
33
+ *
34
+ * Usage:
35
+ * ```typescript
36
+ * const baseKeyStore = NodeKeystoreAdapter.fromPrivateKeys(privateKeys);
37
+ * const haSigner = new ValidatorHASigner(db, config);
38
+ * const haKeyStore = new HAKeyStore(baseKeyStore, haSigner);
39
+ *
40
+ * // Without context - signs directly (no HA protection)
41
+ * const sig = await haKeyStore.signMessageWithAddress(addr, msg);
42
+ *
43
+ * // With context - HA protected, throws DutyAlreadySignedError if already signed
44
+ * const result = await haKeyStore.signMessageWithAddress(addr, msg, {
45
+ * slot: 100n,
46
+ * blockNumber: 50n,
47
+ * dutyType: DutyType.BLOCK_PROPOSAL,
48
+ * });
49
+ * ```
50
+ */
51
+ export class HAKeyStore implements ExtendedValidatorKeyStore {
52
+ private readonly log = createLogger('ha-key-store');
53
+
54
+ constructor(
55
+ private readonly baseKeyStore: ExtendedValidatorKeyStore,
56
+ private readonly haSigner: ValidatorHASigner,
57
+ ) {
58
+ this.log.info('HAKeyStore initialized', {
59
+ nodeId: haSigner.nodeId,
60
+ });
61
+ }
62
+
63
+ /**
64
+ * Sign typed data with all addresses.
65
+ * Coordinates across nodes to prevent double-signing for most duty types.
66
+ * AUTH_REQUEST and TXS duties bypass HA protection since signing multiple times is safe.
67
+ * Returns only signatures that were successfully claimed by this node.
68
+ */
69
+ async signTypedData(typedData: TypedDataDefinition, context: SigningContext): Promise<Signature[]> {
70
+ // no need for HA protection on auth request and txs signatures
71
+ if (!isHAProtectedContext(context)) {
72
+ return this.baseKeyStore.signTypedData(typedData, context);
73
+ }
74
+
75
+ // Sign each address with HA protection
76
+ const addresses = this.getAddresses();
77
+ const results = await Promise.allSettled(
78
+ addresses.map(addr => this.signTypedDataWithAddress(addr, typedData, context)),
79
+ );
80
+
81
+ // Filter out failures (already signed by other nodes or other errors)
82
+ return results
83
+ .filter((result): result is PromiseFulfilledResult<Signature> => {
84
+ if (result.status === 'fulfilled') {
85
+ return true;
86
+ }
87
+ // Log expected HA errors (already signed) at debug level
88
+ if (result.reason instanceof DutyAlreadySignedError) {
89
+ this.log.debug(`Duty already signed by another node`, {
90
+ dutyType: context.dutyType,
91
+ slot: context.slot,
92
+ signedByNode: result.reason.signedByNode,
93
+ });
94
+ return false;
95
+ }
96
+ // Re-throw unexpected errors
97
+ throw result.reason;
98
+ })
99
+ .map(result => result.value);
100
+ }
101
+
102
+ /**
103
+ * Sign a message with all addresses.
104
+ * Coordinates across nodes to prevent double-signing for most duty types.
105
+ * AUTH_REQUEST and TXS duties bypass HA protection since signing multiple times is safe.
106
+ * Returns only signatures that were successfully claimed by this node.
107
+ */
108
+ async signMessage(message: Buffer32, context: SigningContext): Promise<Signature[]> {
109
+ // no need for HA protection on auth request and txs signatures
110
+ if (!isHAProtectedContext(context)) {
111
+ return this.baseKeyStore.signMessage(message, context);
112
+ }
113
+
114
+ // Sign each address with HA protection
115
+ const addresses = this.getAddresses();
116
+ const results = await Promise.allSettled(
117
+ addresses.map(addr => this.signMessageWithAddress(addr, message, context)),
118
+ );
119
+
120
+ // Filter out failures (already signed by other nodes or other errors)
121
+ return results
122
+ .filter((result): result is PromiseFulfilledResult<Signature> => {
123
+ if (result.status === 'fulfilled') {
124
+ return true;
125
+ }
126
+ // Log expected HA errors (already signed) at debug level
127
+ if (result.reason instanceof DutyAlreadySignedError) {
128
+ this.log.debug(`Duty already signed by another node`, {
129
+ dutyType: context.dutyType,
130
+ slot: context.slot,
131
+ signedByNode: result.reason.signedByNode,
132
+ });
133
+ return false;
134
+ }
135
+ // Re-throw unexpected errors
136
+ throw result.reason;
137
+ })
138
+ .map(result => result.value);
139
+ }
140
+
141
+ /**
142
+ * Sign typed data with a specific address.
143
+ * Coordinates across nodes to prevent double-signing for most duty types.
144
+ * AUTH_REQUEST and TXS duties bypass HA protection since signing multiple times is safe.
145
+ * @throws DutyAlreadySignedError if the duty was already signed by another node
146
+ * @throws SlashingProtectionError if attempting to sign different data for the same slot
147
+ */
148
+ async signTypedDataWithAddress(
149
+ address: EthAddress,
150
+ typedData: TypedDataDefinition,
151
+ context: SigningContext,
152
+ ): Promise<Signature> {
153
+ // AUTH_REQUEST and TXS bypass HA protection - multiple signatures are safe
154
+ if (!isHAProtectedContext(context)) {
155
+ return this.baseKeyStore.signTypedDataWithAddress(address, typedData, context);
156
+ }
157
+
158
+ // Compute signing root from typed data for HA tracking
159
+ const digest = hashTypedData(typedData);
160
+ const messageHash = Buffer32.fromString(digest);
161
+
162
+ try {
163
+ return await this.haSigner.signWithProtection(address, messageHash, context, () =>
164
+ this.baseKeyStore.signTypedDataWithAddress(address, typedData, context),
165
+ );
166
+ } catch (error) {
167
+ this.processSigningError(error, context);
168
+ throw error;
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Sign a message with a specific address.
174
+ * Coordinates across nodes to prevent double-signing for most duty types.
175
+ * AUTH_REQUEST and TXS duties bypass HA protection since signing multiple times is safe.
176
+ * @throws DutyAlreadySignedError if the duty was already signed by another node
177
+ * @throws SlashingProtectionError if attempting to sign different data for the same slot
178
+ */
179
+ async signMessageWithAddress(address: EthAddress, message: Buffer32, context: SigningContext): Promise<Signature> {
180
+ // no need for HA protection on auth request and txs signatures
181
+ if (!isHAProtectedContext(context)) {
182
+ return this.baseKeyStore.signMessageWithAddress(address, message, context);
183
+ }
184
+
185
+ try {
186
+ return await this.haSigner.signWithProtection(address, message, context, messageHash =>
187
+ this.baseKeyStore.signMessageWithAddress(address, messageHash, context),
188
+ );
189
+ } catch (error) {
190
+ this.processSigningError(error, context);
191
+ throw error;
192
+ }
193
+ }
194
+
195
+ // ─────────────────────────────────────────────────────────────────────────────
196
+ // pass-through methods (no HA logic needed)
197
+ // ─────────────────────────────────────────────────────────────────────────────
198
+
199
+ getAddress(index: number): EthAddress {
200
+ return this.baseKeyStore.getAddress(index);
201
+ }
202
+
203
+ getAddresses(): EthAddress[] {
204
+ return this.baseKeyStore.getAddresses();
205
+ }
206
+
207
+ getAttesterAddresses(): EthAddress[] {
208
+ return this.baseKeyStore.getAttesterAddresses();
209
+ }
210
+
211
+ getCoinbaseAddress(attesterAddress: EthAddress): EthAddress {
212
+ return this.baseKeyStore.getCoinbaseAddress(attesterAddress);
213
+ }
214
+
215
+ getPublisherAddresses(attesterAddress: EthAddress): EthAddress[] {
216
+ return this.baseKeyStore.getPublisherAddresses(attesterAddress);
217
+ }
218
+
219
+ getFeeRecipient(attesterAddress: EthAddress): AztecAddress {
220
+ return this.baseKeyStore.getFeeRecipient(attesterAddress);
221
+ }
222
+
223
+ getRemoteSignerConfig(attesterAddress: EthAddress): EthRemoteSignerConfig | undefined {
224
+ return this.baseKeyStore.getRemoteSignerConfig(attesterAddress);
225
+ }
226
+
227
+ /**
228
+ * Process signing errors from the HA signer.
229
+ * Logs expected HA errors (already signed) at appropriate levels.
230
+ * Re-throws unexpected errors.
231
+ */
232
+ private processSigningError(error: unknown, context: HAProtectedSigningContext) {
233
+ if (error instanceof DutyAlreadySignedError) {
234
+ this.log.debug(`Duty already signed by another node with the same payload`, {
235
+ dutyType: context.dutyType,
236
+ slot: context.slot,
237
+ signedByNode: error.signedByNode,
238
+ });
239
+ return;
240
+ }
241
+
242
+ if (error instanceof SlashingProtectionError) {
243
+ this.log.info(`Duty already signed by another node with different payload`, {
244
+ dutyType: context.dutyType,
245
+ slot: context.slot,
246
+ existingMessageHash: error.existingMessageHash,
247
+ attemptedMessageHash: error.attemptedMessageHash,
248
+ });
249
+ return;
250
+ }
251
+
252
+ // Re-throw errors
253
+ throw error;
254
+ }
255
+
256
+ /**
257
+ * Start the high-availability key store
258
+ */
259
+ public async start() {
260
+ await this.haSigner.start();
261
+ }
262
+
263
+ /**
264
+ * Stop the high-availability key store
265
+ */
266
+ public async stop() {
267
+ await this.haSigner.stop();
268
+ }
269
+ }
@@ -2,3 +2,4 @@ export * from './interface.js';
2
2
  export * from './local_key_store.js';
3
3
  export * from './node_keystore_adapter.js';
4
4
  export * from './web3signer_key_store.js';
5
+ export * from './ha_key_store.js';
@@ -3,6 +3,7 @@ import type { EthAddress } from '@aztec/foundation/eth-address';
3
3
  import type { Signature } from '@aztec/foundation/eth-signature';
4
4
  import type { EthRemoteSignerConfig } from '@aztec/node-keystore';
5
5
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
6
+ import type { SigningContext } from '@aztec/validator-ha-signer/types';
6
7
 
7
8
  import type { TypedDataDefinition } from 'viem';
8
9
 
@@ -26,17 +27,45 @@ export interface ValidatorKeyStore {
26
27
  */
27
28
  getAddresses(): EthAddress[];
28
29
 
29
- signTypedData(typedData: TypedDataDefinition): Promise<Signature[]>;
30
- signTypedDataWithAddress(address: EthAddress, typedData: TypedDataDefinition): Promise<Signature>;
30
+ /**
31
+ * Sign typed data with all keystore private keys
32
+ * @param typedData - The complete EIP-712 typed data structure
33
+ * @param context - Signing context for HA slashing protection
34
+ * @returns signatures (when context provided with HA, only successfully claimed signatures are returned)
35
+ */
36
+ signTypedData(typedData: TypedDataDefinition, context: SigningContext): Promise<Signature[]>;
37
+
38
+ /**
39
+ * Sign typed data with a specific address's private key
40
+ * @param address - The address of the signer to use
41
+ * @param typedData - The complete EIP-712 typed data structure
42
+ * @param context - Signing context for HA slashing protection
43
+ * @returns signature
44
+ */
45
+ signTypedDataWithAddress(
46
+ address: EthAddress,
47
+ typedData: TypedDataDefinition,
48
+ context: SigningContext,
49
+ ): Promise<Signature>;
50
+
31
51
  /**
32
52
  * Flavor of sign message that followed EIP-712 eth signed message prefix
33
53
  * Note: this is only required when we are using ecdsa signatures over secp256k1
34
54
  *
35
55
  * @param message - The message to sign.
36
- * @returns The signatures.
56
+ * @param context - Signing context for HA slashing protection
57
+ * @returns The signatures (when context provided with HA, only successfully claimed signatures are returned).
37
58
  */
38
- signMessage(message: Buffer32): Promise<Signature[]>;
39
- signMessageWithAddress(address: EthAddress, message: Buffer32): Promise<Signature>;
59
+ signMessage(message: Buffer32, context: SigningContext): Promise<Signature[]>;
60
+
61
+ /**
62
+ * Sign a message with a specific address's private key
63
+ * @param address - The address of the signer to use
64
+ * @param message - The message to sign
65
+ * @param context - Signing context for HA slashing protection
66
+ * @returns signature
67
+ */
68
+ signMessageWithAddress(address: EthAddress, message: Buffer32, context: SigningContext): Promise<Signature>;
40
69
  }
41
70
 
42
71
  /**
@@ -79,4 +108,14 @@ export interface ExtendedValidatorKeyStore extends ValidatorKeyStore {
79
108
  * @returns the remote signer configuration or undefined
80
109
  */
81
110
  getRemoteSignerConfig(attesterAddress: EthAddress): EthRemoteSignerConfig | undefined;
111
+
112
+ /**
113
+ * Start the key store
114
+ */
115
+ start(): Promise<void>;
116
+
117
+ /**
118
+ * Stop the key store
119
+ */
120
+ stop(): Promise<void>;
82
121
  }