@aztec/validator-client 0.0.1-commit.7d4e6cd → 0.0.1-commit.808bf7f90
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/README.md +53 -24
- package/dest/block_proposal_handler.d.ts +9 -9
- package/dest/block_proposal_handler.d.ts.map +1 -1
- package/dest/block_proposal_handler.js +35 -54
- package/dest/checkpoint_builder.d.ts +24 -25
- package/dest/checkpoint_builder.d.ts.map +1 -1
- package/dest/checkpoint_builder.js +60 -34
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +12 -14
- package/dest/duties/validation_service.d.ts +20 -7
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +75 -22
- package/dest/factory.d.ts +2 -2
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +1 -1
- package/dest/key_store/ha_key_store.d.ts +99 -0
- package/dest/key_store/ha_key_store.d.ts.map +1 -0
- package/dest/key_store/ha_key_store.js +208 -0
- package/dest/key_store/index.d.ts +2 -1
- package/dest/key_store/index.d.ts.map +1 -1
- package/dest/key_store/index.js +1 -0
- package/dest/key_store/interface.d.ts +36 -6
- package/dest/key_store/interface.d.ts.map +1 -1
- package/dest/key_store/local_key_store.d.ts +10 -5
- package/dest/key_store/local_key_store.d.ts.map +1 -1
- package/dest/key_store/local_key_store.js +8 -4
- package/dest/key_store/node_keystore_adapter.d.ts +18 -5
- package/dest/key_store/node_keystore_adapter.d.ts.map +1 -1
- package/dest/key_store/node_keystore_adapter.js +18 -4
- package/dest/key_store/web3signer_key_store.d.ts +10 -5
- package/dest/key_store/web3signer_key_store.d.ts.map +1 -1
- package/dest/key_store/web3signer_key_store.js +8 -4
- package/dest/metrics.d.ts +4 -3
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +34 -5
- package/dest/tx_validator/tx_validator_factory.d.ts +4 -3
- package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -1
- package/dest/tx_validator/tx_validator_factory.js +17 -16
- package/dest/validator.d.ts +43 -18
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +233 -94
- package/package.json +21 -17
- package/src/block_proposal_handler.ts +48 -69
- package/src/checkpoint_builder.ts +101 -38
- package/src/config.ts +11 -13
- package/src/duties/validation_service.ts +100 -25
- package/src/factory.ts +1 -0
- package/src/key_store/ha_key_store.ts +269 -0
- package/src/key_store/index.ts +1 -0
- package/src/key_store/interface.ts +44 -5
- package/src/key_store/local_key_store.ts +13 -4
- package/src/key_store/node_keystore_adapter.ts +27 -4
- package/src/key_store/web3signer_key_store.ts +17 -4
- package/src/metrics.ts +45 -6
- package/src/tx_validator/tx_validator_factory.ts +52 -31
- package/src/validator.ts +303 -114
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BlockNumber,
|
|
3
|
+
type CheckpointNumber,
|
|
4
|
+
IndexWithinCheckpoint,
|
|
5
|
+
type SlotNumber,
|
|
6
|
+
} from '@aztec/foundation/branded-types';
|
|
1
7
|
import { Buffer32 } from '@aztec/foundation/buffer';
|
|
2
8
|
import { keccak256 } from '@aztec/foundation/crypto/keccak';
|
|
3
9
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
@@ -18,6 +24,8 @@ import {
|
|
|
18
24
|
} from '@aztec/stdlib/p2p';
|
|
19
25
|
import type { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
20
26
|
import type { BlockHeader, Tx } from '@aztec/stdlib/tx';
|
|
27
|
+
import { DutyAlreadySignedError, SlashingProtectionError } from '@aztec/validator-ha-signer/errors';
|
|
28
|
+
import { DutyType, type SigningContext } from '@aztec/validator-ha-signer/types';
|
|
21
29
|
|
|
22
30
|
import type { ValidatorKeyStore } from '../key_store/interface.js';
|
|
23
31
|
|
|
@@ -31,34 +39,40 @@ export class ValidationService {
|
|
|
31
39
|
* Create a block proposal with the given header, archive, and transactions
|
|
32
40
|
*
|
|
33
41
|
* @param blockHeader - The block header
|
|
34
|
-
* @param
|
|
42
|
+
* @param blockIndexWithinCheckpoint - The block index within checkpoint for HA signing context
|
|
35
43
|
* @param inHash - Hash of L1 to L2 messages for this checkpoint
|
|
36
44
|
* @param archive - The archive of the current block
|
|
37
|
-
* @param txs -
|
|
45
|
+
* @param txs - Ordered list of transactions (Tx[])
|
|
46
|
+
* @param proposerAttesterAddress - The address of the proposer/attester, or undefined
|
|
38
47
|
* @param options - Block proposal options (including broadcastInvalidBlockProposal for testing)
|
|
39
48
|
*
|
|
40
49
|
* @returns A block proposal signing the above information
|
|
50
|
+
* @throws DutyAlreadySignedError if HA signer indicates duty already signed by another node
|
|
51
|
+
* @throws SlashingProtectionError if attempting to sign different data for same slot
|
|
41
52
|
*/
|
|
42
53
|
public createBlockProposal(
|
|
43
54
|
blockHeader: BlockHeader,
|
|
44
|
-
|
|
55
|
+
blockIndexWithinCheckpoint: IndexWithinCheckpoint,
|
|
45
56
|
inHash: Fr,
|
|
46
57
|
archive: Fr,
|
|
47
58
|
txs: Tx[],
|
|
48
59
|
proposerAttesterAddress: EthAddress | undefined,
|
|
49
60
|
options: BlockProposalOptions,
|
|
50
61
|
): Promise<BlockProposal> {
|
|
51
|
-
const payloadSigner = this.getPayloadSigner(proposerAttesterAddress);
|
|
52
|
-
|
|
53
62
|
// For testing: change the new archive to trigger state_mismatch validation failure
|
|
54
63
|
if (options.broadcastInvalidBlockProposal) {
|
|
55
64
|
archive = Fr.random();
|
|
56
65
|
this.log.warn(`Creating INVALID block proposal for slot ${blockHeader.globalVariables.slotNumber}`);
|
|
57
66
|
}
|
|
58
67
|
|
|
68
|
+
// Create a signer that uses the appropriate address
|
|
69
|
+
const address = proposerAttesterAddress ?? this.keyStore.getAddress(0);
|
|
70
|
+
const payloadSigner = (payload: Buffer32, context: SigningContext) =>
|
|
71
|
+
this.keyStore.signMessageWithAddress(address, payload, context);
|
|
72
|
+
|
|
59
73
|
return BlockProposal.createProposalFromSigner(
|
|
60
74
|
blockHeader,
|
|
61
|
-
|
|
75
|
+
blockIndexWithinCheckpoint,
|
|
62
76
|
inHash,
|
|
63
77
|
archive,
|
|
64
78
|
txs.map(tx => tx.getTxHash()),
|
|
@@ -81,18 +95,23 @@ export class ValidationService {
|
|
|
81
95
|
public createCheckpointProposal(
|
|
82
96
|
checkpointHeader: CheckpointHeader,
|
|
83
97
|
archive: Fr,
|
|
98
|
+
feeAssetPriceModifier: bigint,
|
|
84
99
|
lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
|
|
85
100
|
proposerAttesterAddress: EthAddress | undefined,
|
|
86
101
|
options: CheckpointProposalOptions,
|
|
87
102
|
): Promise<CheckpointProposal> {
|
|
88
|
-
const payloadSigner = this.getPayloadSigner(proposerAttesterAddress);
|
|
89
|
-
|
|
90
103
|
// For testing: change the archive to trigger state_mismatch validation failure
|
|
91
104
|
if (options.broadcastInvalidCheckpointProposal) {
|
|
92
105
|
archive = Fr.random();
|
|
93
106
|
this.log.warn(`Creating INVALID checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
|
|
94
107
|
}
|
|
95
108
|
|
|
109
|
+
// Create a signer that takes payload and context, and uses the appropriate address
|
|
110
|
+
const payloadSigner = (payload: Buffer32, context: SigningContext) => {
|
|
111
|
+
const address = proposerAttesterAddress ?? this.keyStore.getAddress(0);
|
|
112
|
+
return this.keyStore.signMessageWithAddress(address, payload, context);
|
|
113
|
+
};
|
|
114
|
+
|
|
96
115
|
// Last block to include in the proposal
|
|
97
116
|
const lastBlock = lastBlockInfo && {
|
|
98
117
|
blockHeader: lastBlockInfo.blockHeader,
|
|
@@ -101,17 +120,13 @@ export class ValidationService {
|
|
|
101
120
|
txs: options.publishFullTxs ? lastBlockInfo.txs : undefined,
|
|
102
121
|
};
|
|
103
122
|
|
|
104
|
-
return CheckpointProposal.createProposalFromSigner(
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
}
|
|
123
|
+
return CheckpointProposal.createProposalFromSigner(
|
|
124
|
+
checkpointHeader,
|
|
125
|
+
archive,
|
|
126
|
+
feeAssetPriceModifier,
|
|
127
|
+
lastBlock,
|
|
128
|
+
payloadSigner,
|
|
129
|
+
);
|
|
115
130
|
}
|
|
116
131
|
|
|
117
132
|
/**
|
|
@@ -129,23 +144,83 @@ export class ValidationService {
|
|
|
129
144
|
attestors: EthAddress[],
|
|
130
145
|
): Promise<CheckpointAttestation[]> {
|
|
131
146
|
// Create the attestation payload from the checkpoint proposal
|
|
132
|
-
const payload = new ConsensusPayload(proposal.checkpointHeader, proposal.archive);
|
|
147
|
+
const payload = new ConsensusPayload(proposal.checkpointHeader, proposal.archive, proposal.feeAssetPriceModifier);
|
|
133
148
|
const buf = Buffer32.fromBuffer(
|
|
134
149
|
keccak256(payload.getPayloadToSign(SignatureDomainSeparator.checkpointAttestation)),
|
|
135
150
|
);
|
|
136
|
-
|
|
137
|
-
|
|
151
|
+
|
|
152
|
+
// TODO(spy/ha): Use checkpointNumber instead of blockNumber once CheckpointHeader includes it.
|
|
153
|
+
// Currently using lastBlock.blockNumber as a proxy for checkpoint identification in HA signing.
|
|
154
|
+
// blockNumber is NOT used for the primary key so it's safe to use here.
|
|
155
|
+
// See CheckpointHeader TODO and SigningContext types documentation.
|
|
156
|
+
let blockNumber: BlockNumber;
|
|
157
|
+
try {
|
|
158
|
+
blockNumber = proposal.blockNumber;
|
|
159
|
+
} catch {
|
|
160
|
+
// Checkpoint proposal may not have lastBlock, use 0 as fallback
|
|
161
|
+
blockNumber = BlockNumber(0);
|
|
162
|
+
}
|
|
163
|
+
const context: SigningContext = {
|
|
164
|
+
slot: proposal.slotNumber,
|
|
165
|
+
blockNumber,
|
|
166
|
+
dutyType: DutyType.ATTESTATION,
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
// Sign each attestor in parallel, catching HA errors per-attestor
|
|
170
|
+
const results = await Promise.allSettled(
|
|
171
|
+
attestors.map(async attestor => {
|
|
172
|
+
const sig = await this.keyStore.signMessageWithAddress(attestor, buf, context);
|
|
173
|
+
// return new BlockAttestation(proposal.payload, sig, proposal.signature);
|
|
174
|
+
return new CheckpointAttestation(payload, sig, proposal.signature);
|
|
175
|
+
}),
|
|
138
176
|
);
|
|
139
|
-
|
|
177
|
+
|
|
178
|
+
const attestations: CheckpointAttestation[] = [];
|
|
179
|
+
for (let i = 0; i < results.length; i++) {
|
|
180
|
+
const result = results[i];
|
|
181
|
+
if (result.status === 'fulfilled') {
|
|
182
|
+
attestations.push(result.value);
|
|
183
|
+
} else {
|
|
184
|
+
const error = result.reason;
|
|
185
|
+
if (error instanceof DutyAlreadySignedError || error instanceof SlashingProtectionError) {
|
|
186
|
+
this.log.info(
|
|
187
|
+
`Attestation for slot ${proposal.slotNumber} by ${attestors[i]} already signed by another High-Availability node`,
|
|
188
|
+
);
|
|
189
|
+
// Continue with remaining attestors
|
|
190
|
+
} else {
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return attestations;
|
|
140
197
|
}
|
|
141
198
|
|
|
142
|
-
|
|
199
|
+
/**
|
|
200
|
+
* Sign attestations and signers payload
|
|
201
|
+
* @param attestationsAndSigners - The attestations and signers to sign
|
|
202
|
+
* @param proposer - The proposer address to sign with
|
|
203
|
+
* @param slot - The slot number for HA signing context
|
|
204
|
+
* @param blockNumber - The block or checkpoint number for HA signing context
|
|
205
|
+
* @returns signature
|
|
206
|
+
* @throws DutyAlreadySignedError if already signed by another HA node
|
|
207
|
+
* @throws SlashingProtectionError if attempting to sign different data for same slot
|
|
208
|
+
*/
|
|
209
|
+
signAttestationsAndSigners(
|
|
143
210
|
attestationsAndSigners: CommitteeAttestationsAndSigners,
|
|
144
211
|
proposer: EthAddress,
|
|
212
|
+
slot: SlotNumber,
|
|
213
|
+
blockNumber: BlockNumber | CheckpointNumber,
|
|
145
214
|
): Promise<Signature> {
|
|
215
|
+
const context: SigningContext = {
|
|
216
|
+
slot,
|
|
217
|
+
blockNumber,
|
|
218
|
+
dutyType: DutyType.ATTESTATIONS_AND_SIGNERS,
|
|
219
|
+
};
|
|
220
|
+
|
|
146
221
|
const buf = Buffer32.fromBuffer(
|
|
147
222
|
keccak256(attestationsAndSigners.getPayloadToSign(SignatureDomainSeparator.attestationsAndSigners)),
|
|
148
223
|
);
|
|
149
|
-
return
|
|
224
|
+
return this.keyStore.signMessageWithAddress(proposer, buf, context);
|
|
150
225
|
}
|
|
151
226
|
}
|
package/src/factory.ts
CHANGED
|
@@ -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.warn(`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
|
+
}
|
package/src/key_store/index.ts
CHANGED
|
@@ -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
|
-
|
|
30
|
-
|
|
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
|
-
* @
|
|
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
|
-
|
|
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
|
}
|
|
@@ -2,6 +2,7 @@ import { Buffer32 } from '@aztec/foundation/buffer';
|
|
|
2
2
|
import { Secp256k1Signer } from '@aztec/foundation/crypto/secp256k1-signer';
|
|
3
3
|
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
4
4
|
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
5
|
+
import type { SigningContext } from '@aztec/validator-ha-signer/types';
|
|
5
6
|
|
|
6
7
|
import { type TypedDataDefinition, hashTypedData } from 'viem';
|
|
7
8
|
|
|
@@ -46,9 +47,10 @@ export class LocalKeyStore implements ValidatorKeyStore {
|
|
|
46
47
|
/**
|
|
47
48
|
* Sign a message with all keystore private keys
|
|
48
49
|
* @param typedData - The complete EIP-712 typed data structure (domain, types, primaryType, message)
|
|
50
|
+
* @param _context - Signing context (ignored by LocalKeyStore, used for HA protection)
|
|
49
51
|
* @return signature
|
|
50
52
|
*/
|
|
51
|
-
public signTypedData(typedData: TypedDataDefinition): Promise<Signature[]> {
|
|
53
|
+
public signTypedData(typedData: TypedDataDefinition, _context: SigningContext): Promise<Signature[]> {
|
|
52
54
|
const digest = hashTypedData(typedData);
|
|
53
55
|
return Promise.all(this.signers.map(signer => signer.sign(Buffer32.fromString(digest))));
|
|
54
56
|
}
|
|
@@ -57,10 +59,15 @@ export class LocalKeyStore implements ValidatorKeyStore {
|
|
|
57
59
|
* Sign a message with a specific address's private key
|
|
58
60
|
* @param address - The address of the signer to use
|
|
59
61
|
* @param typedData - The complete EIP-712 typed data structure (domain, types, primaryType, message)
|
|
62
|
+
* @param _context - Signing context (ignored by LocalKeyStore, used for HA protection)
|
|
60
63
|
* @returns signature for the specified address
|
|
61
64
|
* @throws Error if the address is not found in the keystore
|
|
62
65
|
*/
|
|
63
|
-
public signTypedDataWithAddress(
|
|
66
|
+
public signTypedDataWithAddress(
|
|
67
|
+
address: EthAddress,
|
|
68
|
+
typedData: TypedDataDefinition,
|
|
69
|
+
_context: SigningContext,
|
|
70
|
+
): Promise<Signature> {
|
|
64
71
|
const signer = this.signersByAddress.get(address.toString());
|
|
65
72
|
if (!signer) {
|
|
66
73
|
throw new Error(`No signer found for address ${address.toString()}`);
|
|
@@ -73,9 +80,10 @@ export class LocalKeyStore implements ValidatorKeyStore {
|
|
|
73
80
|
* Sign a message using eth_sign with all keystore private keys
|
|
74
81
|
*
|
|
75
82
|
* @param message - The message to sign
|
|
83
|
+
* @param _context - Signing context (ignored by LocalKeyStore, used for HA protection)
|
|
76
84
|
* @return signatures
|
|
77
85
|
*/
|
|
78
|
-
public signMessage(message: Buffer32): Promise<Signature[]> {
|
|
86
|
+
public signMessage(message: Buffer32, _context: SigningContext): Promise<Signature[]> {
|
|
79
87
|
return Promise.all(this.signers.map(signer => signer.signMessage(message)));
|
|
80
88
|
}
|
|
81
89
|
|
|
@@ -83,10 +91,11 @@ export class LocalKeyStore implements ValidatorKeyStore {
|
|
|
83
91
|
* Sign a message using eth_sign with a specific address's private key
|
|
84
92
|
* @param address - The address of the signer to use
|
|
85
93
|
* @param message - The message to sign
|
|
94
|
+
* @param _context - Signing context (ignored by LocalKeyStore, used for HA protection)
|
|
86
95
|
* @returns signature for the specified address
|
|
87
96
|
* @throws Error if the address is not found in the keystore
|
|
88
97
|
*/
|
|
89
|
-
public signMessageWithAddress(address: EthAddress, message: Buffer32): Promise<Signature> {
|
|
98
|
+
public signMessageWithAddress(address: EthAddress, message: Buffer32, _context: SigningContext): Promise<Signature> {
|
|
90
99
|
const signer = this.signersByAddress.get(address.toString());
|
|
91
100
|
if (!signer) {
|
|
92
101
|
throw new Error(`No signer found for address ${address.toString()}`);
|