@aztec/validator-client 0.0.0-test.1 → 0.0.1-commit.b655e406

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 (57) hide show
  1. package/dest/block_proposal_handler.d.ts +52 -0
  2. package/dest/block_proposal_handler.d.ts.map +1 -0
  3. package/dest/block_proposal_handler.js +286 -0
  4. package/dest/config.d.ts +2 -13
  5. package/dest/config.d.ts.map +1 -1
  6. package/dest/config.js +31 -7
  7. package/dest/duties/validation_service.d.ts +16 -8
  8. package/dest/duties/validation_service.d.ts.map +1 -1
  9. package/dest/duties/validation_service.js +35 -11
  10. package/dest/factory.d.ts +21 -4
  11. package/dest/factory.d.ts.map +1 -1
  12. package/dest/factory.js +13 -6
  13. package/dest/index.d.ts +3 -1
  14. package/dest/index.d.ts.map +1 -1
  15. package/dest/index.js +3 -1
  16. package/dest/key_store/index.d.ts +2 -0
  17. package/dest/key_store/index.d.ts.map +1 -1
  18. package/dest/key_store/index.js +2 -0
  19. package/dest/key_store/interface.d.ts +54 -5
  20. package/dest/key_store/interface.d.ts.map +1 -1
  21. package/dest/key_store/interface.js +3 -3
  22. package/dest/key_store/local_key_store.d.ts +40 -10
  23. package/dest/key_store/local_key_store.d.ts.map +1 -1
  24. package/dest/key_store/local_key_store.js +63 -16
  25. package/dest/key_store/node_keystore_adapter.d.ts +138 -0
  26. package/dest/key_store/node_keystore_adapter.d.ts.map +1 -0
  27. package/dest/key_store/node_keystore_adapter.js +316 -0
  28. package/dest/key_store/web3signer_key_store.d.ts +67 -0
  29. package/dest/key_store/web3signer_key_store.d.ts.map +1 -0
  30. package/dest/key_store/web3signer_key_store.js +152 -0
  31. package/dest/metrics.d.ts +11 -4
  32. package/dest/metrics.d.ts.map +1 -1
  33. package/dest/metrics.js +52 -15
  34. package/dest/validator.d.ts +48 -61
  35. package/dest/validator.d.ts.map +1 -1
  36. package/dest/validator.js +262 -165
  37. package/package.json +25 -19
  38. package/src/block_proposal_handler.ts +343 -0
  39. package/src/config.ts +42 -22
  40. package/src/duties/validation_service.ts +69 -14
  41. package/src/factory.ts +56 -11
  42. package/src/index.ts +3 -1
  43. package/src/key_store/index.ts +2 -0
  44. package/src/key_store/interface.ts +61 -5
  45. package/src/key_store/local_key_store.ts +67 -17
  46. package/src/key_store/node_keystore_adapter.ts +375 -0
  47. package/src/key_store/web3signer_key_store.ts +192 -0
  48. package/src/metrics.ts +68 -17
  49. package/src/validator.ts +381 -233
  50. package/dest/errors/index.d.ts +0 -2
  51. package/dest/errors/index.d.ts.map +0 -1
  52. package/dest/errors/index.js +0 -1
  53. package/dest/errors/validator.error.d.ts +0 -29
  54. package/dest/errors/validator.error.d.ts.map +0 -1
  55. package/dest/errors/validator.error.js +0 -45
  56. package/src/errors/index.ts +0 -1
  57. package/src/errors/validator.error.ts +0 -55
@@ -0,0 +1,316 @@
1
+ import { KeystoreManager, loadKeystoreFile } from '@aztec/node-keystore';
2
+ import { InvalidValidatorPrivateKeyError } from '@aztec/stdlib/validators';
3
+ import { privateKeyToAccount } from 'viem/accounts';
4
+ export class NodeKeystoreAdapter {
5
+ keystoreManager;
6
+ // Per-validator cache (lazy)
7
+ validators = new Map();
8
+ addressIndex = new Map();
9
+ constructor(keystoreManager){
10
+ this.keystoreManager = keystoreManager;
11
+ }
12
+ /**
13
+ * Create an adapter from a keystore JSON file on disk.
14
+ * @param keystoreFilePath Absolute or relative path to a keystore JSON file
15
+ * @returns A configured NodeKeystoreAdapter instance
16
+ * @throws Error when the file fails schema validation or cannot be read
17
+ */ static fromKeystoreFile(keystoreFilePath) {
18
+ const keystoreConfig = loadKeystoreFile(keystoreFilePath);
19
+ return NodeKeystoreAdapter.fromKeystoreConfig(keystoreConfig);
20
+ }
21
+ /**
22
+ * Create an adapter from an in-memory keystore-like object.
23
+ * Validates resolved duplicate attester addresses across validators and sources.
24
+ * @param keystoreConfig Parsed config object (typically result of loadKeystoreFile)
25
+ * @returns A configured NodeKeystoreAdapter instance
26
+ * @throws Error when resolved duplicate attester addresses are detected
27
+ */ static fromKeystoreConfig(keystoreConfig) {
28
+ const keystoreManager = new KeystoreManager(keystoreConfig);
29
+ // Validate resolved attester addresses (covers JSON V3 and mnemonic duplicates across validators)
30
+ keystoreManager.validateResolvedUniqueAttesterAddresses();
31
+ return new NodeKeystoreAdapter(keystoreManager);
32
+ }
33
+ /**
34
+ * Build an adapter directly from a list of validator private keys.
35
+ * Each key becomes a separate validator using the same key for attester and publisher,
36
+ * coinbase defaults to the derived EOA address, and feeRecipient is a 32-byte padded address.
37
+ * Note: Fee recipient is a temporary placeholder, replace with actual fee recipient when implemented.
38
+ */ static fromPrivateKeys(privateKeys) {
39
+ // Minimal validation: 0x + 64 hex
40
+ const isPk = (s)=>/^0x[0-9a-fA-F]{64}$/.test(s);
41
+ for (const pk of privateKeys){
42
+ if (!isPk(pk)) {
43
+ throw new InvalidValidatorPrivateKeyError();
44
+ }
45
+ }
46
+ const validators = privateKeys.map((pk)=>{
47
+ const account = privateKeyToAccount(pk);
48
+ const ethAddress = account.address;
49
+ // TODO: Temporary fee recipient, replace with actual fee recipient when implemented
50
+ const feeRecipient = `0x${ethAddress.slice(2).padStart(64, '0')}`;
51
+ return {
52
+ attester: pk,
53
+ publisher: pk,
54
+ coinbase: ethAddress,
55
+ feeRecipient
56
+ };
57
+ });
58
+ const cfg = {
59
+ schemaVersion: 1,
60
+ validators
61
+ };
62
+ return NodeKeystoreAdapter.fromKeystoreConfig(cfg);
63
+ }
64
+ /**
65
+ * Build an adapter for a Web3Signer setup by providing the signer URL and the EOA addresses.
66
+ * Each address becomes a separate validator; attester and publisher point to the same remote signer entry.
67
+ * Note: Fee recipient is a temporary placeholder, replace with actual fee recipient when implemented.
68
+ */ static fromWeb3Signer(web3SignerUrl, addresses) {
69
+ const validators = addresses.map((address)=>{
70
+ const ethAddress = address.toString();
71
+ // TODO: Temporary fee recipient, replace with actual fee recipient when implemented
72
+ const feeRecipient = `0x${ethAddress.slice(2).padStart(64, '0')}`;
73
+ return {
74
+ attester: {
75
+ address: ethAddress,
76
+ remoteSignerUrl: web3SignerUrl
77
+ },
78
+ publisher: {
79
+ address: ethAddress,
80
+ remoteSignerUrl: web3SignerUrl
81
+ },
82
+ coinbase: ethAddress,
83
+ feeRecipient
84
+ };
85
+ });
86
+ const cfg = {
87
+ schemaVersion: 1,
88
+ validators
89
+ };
90
+ return NodeKeystoreAdapter.fromKeystoreConfig(cfg);
91
+ }
92
+ static fromKeyStoreManager(manager) {
93
+ return new NodeKeystoreAdapter(manager);
94
+ }
95
+ /**
96
+ * Normalize address keys to lowercase hex strings for map/set usage.
97
+ */ static key(addr) {
98
+ return typeof addr === 'string' ? addr.toLowerCase() : addr.toString().toLowerCase();
99
+ }
100
+ /**
101
+ * Ensure per-validator signer cache exists; build it by creating
102
+ * attester/publisher signers and populating indices when missing.
103
+ * @param validatorIndex Index of the validator in the keystore
104
+ * @returns The cached validator entry
105
+ */ ensureValidator(validatorIndex) {
106
+ const cached = this.validators.get(validatorIndex);
107
+ if (cached) {
108
+ return cached;
109
+ }
110
+ const attesters = this.keystoreManager.createAttesterSigners(validatorIndex);
111
+ const publishers = this.keystoreManager.createPublisherSigners(validatorIndex);
112
+ // Build 'all' + indices
113
+ const byAddress = new Map();
114
+ const attesterSet = new Set();
115
+ for (const s of attesters){
116
+ const k = NodeKeystoreAdapter.key(s.address);
117
+ byAddress.set(k, s);
118
+ attesterSet.add(k);
119
+ }
120
+ for (const s of publishers){
121
+ const k = NodeKeystoreAdapter.key(s.address);
122
+ if (!byAddress.has(k)) {
123
+ byAddress.set(k, s);
124
+ }
125
+ }
126
+ const all = Array.from(byAddress.values());
127
+ // Populate global index
128
+ for (const [k, signer] of byAddress.entries()){
129
+ this.addressIndex.set(k, {
130
+ signer,
131
+ validatorIndex
132
+ });
133
+ }
134
+ const built = {
135
+ attesters,
136
+ publishers,
137
+ all,
138
+ byAddress,
139
+ attesterSet
140
+ };
141
+ this.validators.set(validatorIndex, built);
142
+ return built;
143
+ }
144
+ /**
145
+ * Iterate all validator indices in the keystore manager.
146
+ */ *validatorIndices() {
147
+ const n = this.keystoreManager.getValidatorCount();
148
+ for(let i = 0; i < n; i++){
149
+ yield i;
150
+ }
151
+ }
152
+ /**
153
+ * Find the validator index that contains the given attester address.
154
+ * @param attesterAddress Address to locate
155
+ * @returns Validator index
156
+ * @throws Error when no validator contains the attester
157
+ */ findValidatorIndexForAttester(attesterAddress) {
158
+ const key = NodeKeystoreAdapter.key(attesterAddress);
159
+ // Fast path: if we’ve already cached any validator that includes this as attester
160
+ for (const i of this.validatorIndices()){
161
+ const v = this.ensureValidator(i);
162
+ if (v.attesterSet.has(key)) {
163
+ return i;
164
+ }
165
+ }
166
+ throw new Error(`Attester address ${attesterAddress.toString()} not found in any validator configuration`);
167
+ }
168
+ /**
169
+ * Get attester address by flat index across all validators' attester sets.
170
+ * @param index Zero-based flat index across all attesters
171
+ * @returns EthAddress for the indexed attester
172
+ * @throws Error when index is out of bounds
173
+ */ getAddress(index) {
174
+ const all = this.getAddresses();
175
+ if (index < 0 || index >= all.length) {
176
+ throw new Error(`Index ${index} is out of bounds (0..${all.length - 1}).`);
177
+ }
178
+ return all[index];
179
+ }
180
+ /**
181
+ * Get all attester addresses across validators (legacy-compatible view).
182
+ */ getAddresses() {
183
+ const out = [];
184
+ for (const i of this.validatorIndices()){
185
+ const v = this.ensureValidator(i);
186
+ // attester addresses only for backward compatibility
187
+ for (const s of v.attesters){
188
+ out.push(s.address);
189
+ }
190
+ }
191
+ return out;
192
+ }
193
+ /**
194
+ * Sign typed data with all attester signers across validators.
195
+ * @param typedData EIP-712 typed data
196
+ * @returns Array of signatures in validator order, flattened
197
+ */ async signTypedData(typedData) {
198
+ const jobs = [];
199
+ for (const i of this.validatorIndices()){
200
+ const v = this.ensureValidator(i);
201
+ for (const s of v.attesters){
202
+ jobs.push(this.keystoreManager.signTypedData(s, typedData));
203
+ }
204
+ }
205
+ return await Promise.all(jobs);
206
+ }
207
+ /**
208
+ * Sign a message with all attester signers across validators.
209
+ * @param message 32-byte message (already hashed/padded as needed)
210
+ * @returns Array of signatures in validator order, flattened
211
+ */ async signMessage(message) {
212
+ const jobs = [];
213
+ for (const i of this.validatorIndices()){
214
+ const v = this.ensureValidator(i);
215
+ for (const s of v.attesters){
216
+ jobs.push(this.keystoreManager.signMessage(s, message));
217
+ }
218
+ }
219
+ return await Promise.all(jobs);
220
+ }
221
+ /**
222
+ * Sign typed data with a signer identified by address (any role).
223
+ * Hydrates caches on-demand when the address is first seen.
224
+ * @param address Address to sign with
225
+ * @param typedData EIP-712 typed data
226
+ * @returns Signature from the signer matching the address
227
+ * @throws Error when no signer exists for the address
228
+ */ async signTypedDataWithAddress(address, typedData) {
229
+ const entry = this.addressIndex.get(NodeKeystoreAdapter.key(address));
230
+ if (entry) {
231
+ return await this.keystoreManager.signTypedData(entry.signer, typedData);
232
+ }
233
+ // If not in global index yet, lazily hydrate all validators once and retry
234
+ for (const i of this.validatorIndices()){
235
+ this.ensureValidator(i);
236
+ }
237
+ const second = this.addressIndex.get(NodeKeystoreAdapter.key(address));
238
+ if (second) {
239
+ return await this.keystoreManager.signTypedData(second.signer, typedData);
240
+ }
241
+ throw new Error(`No signer found for address ${address.toString()}`);
242
+ }
243
+ /**
244
+ * Sign a message with a signer identified by address (any role).
245
+ * Hydrates caches on-demand when the address is first seen.
246
+ * @param address Address to sign with
247
+ * @param message 32-byte message
248
+ * @returns Signature from the signer matching the address
249
+ * @throws Error when no signer exists for the address
250
+ */ async signMessageWithAddress(address, message) {
251
+ const entry = this.addressIndex.get(NodeKeystoreAdapter.key(address));
252
+ if (entry) {
253
+ return await this.keystoreManager.signMessage(entry.signer, message);
254
+ }
255
+ for (const i of this.validatorIndices()){
256
+ this.ensureValidator(i);
257
+ }
258
+ const second = this.addressIndex.get(NodeKeystoreAdapter.key(address));
259
+ if (second) {
260
+ return await this.keystoreManager.signMessage(second.signer, message);
261
+ }
262
+ throw new Error(`No signer found for address ${address.toString()}`);
263
+ }
264
+ /**
265
+ * Get all attester addresses across validators (alias of getAddresses).
266
+ */ getAttesterAddresses() {
267
+ return this.getAddresses();
268
+ }
269
+ /**
270
+ * Get the effective coinbase address for the validator that contains the given attester.
271
+ * @param attesterAddress Address of an attester belonging to the validator
272
+ * @returns Coinbase EthAddress
273
+ */ getCoinbaseAddress(attesterAddress) {
274
+ const validatorIndex = this.findValidatorIndexForAttester(attesterAddress);
275
+ return this.keystoreManager.getCoinbaseAddress(validatorIndex, attesterAddress);
276
+ }
277
+ /**
278
+ * Get the publisher addresses for the validator that contains the given attester.
279
+ * @param attesterAddress Address of an attester belonging to the validator
280
+ * @returns Array of publisher addresses
281
+ */ getPublisherAddresses(attesterAddress) {
282
+ const validatorIndex = this.findValidatorIndexForAttester(attesterAddress);
283
+ const v = this.ensureValidator(validatorIndex);
284
+ return v.publishers.map((s)=>s.address);
285
+ }
286
+ getAttestorForPublisher(publisherAddress) {
287
+ const attestorAddresses = this.getAttesterAddresses();
288
+ for (const attestor of attestorAddresses){
289
+ const publishers = this.getPublisherAddresses(attestor);
290
+ const found = publishers.some((publisher)=>publisher.equals(publisherAddress));
291
+ if (found) {
292
+ return attestor;
293
+ }
294
+ }
295
+ // Could not find an attestor for this publisher
296
+ throw new Error(`Failed to find attestor for publisher ${publisherAddress.toString()}`);
297
+ }
298
+ /**
299
+ * Get the fee recipient for the validator that contains the given attester.
300
+ * @param attesterAddress Address of an attester belonging to the validator
301
+ * @returns Fee recipient as AztecAddress
302
+ */ getFeeRecipient(attesterAddress) {
303
+ const validatorIndex = this.findValidatorIndexForAttester(attesterAddress);
304
+ return this.keystoreManager.getFeeRecipient(validatorIndex);
305
+ }
306
+ /**
307
+ * Get the effective remote signer configuration for the attester.
308
+ * Precedence: account-level override > validator-level override > file-level default.
309
+ * Returns undefined for local signers (private key / JSON-V3 / mnemonic).
310
+ * @param attesterAddress Address of an attester belonging to the validator
311
+ * @returns Effective remote signer configuration or undefined
312
+ */ getRemoteSignerConfig(attesterAddress) {
313
+ const validatorIndex = this.findValidatorIndexForAttester(attesterAddress);
314
+ return this.keystoreManager.getEffectiveRemoteSignerConfig(validatorIndex, attesterAddress);
315
+ }
316
+ }
@@ -0,0 +1,67 @@
1
+ import type { Buffer32 } from '@aztec/foundation/buffer';
2
+ import { EthAddress } from '@aztec/foundation/eth-address';
3
+ import { Signature } from '@aztec/foundation/eth-signature';
4
+ import type { TypedDataDefinition } from 'viem';
5
+ import type { ValidatorKeyStore } from './interface.js';
6
+ /**
7
+ * Web3Signer Key Store
8
+ *
9
+ * An implementation of the Key store using Web3Signer remote signing service.
10
+ * This implementation uses the Web3Signer JSON-RPC API for secp256k1 signatures.
11
+ */
12
+ export declare class Web3SignerKeyStore implements ValidatorKeyStore {
13
+ private addresses;
14
+ private baseUrl;
15
+ constructor(addresses: EthAddress[], baseUrl: string);
16
+ /**
17
+ * Get the address of a signer by index
18
+ *
19
+ * @param index - The index of the signer
20
+ * @returns the address
21
+ */
22
+ getAddress(index: number): EthAddress;
23
+ /**
24
+ * Get all addresses
25
+ *
26
+ * @returns all addresses
27
+ */
28
+ getAddresses(): EthAddress[];
29
+ /**
30
+ * Sign EIP-712 typed data with all keystore addresses
31
+ * @param typedData - The complete EIP-712 typed data structure (domain, types, primaryType, message)
32
+ * @return signatures
33
+ */
34
+ signTypedData(typedData: TypedDataDefinition): Promise<Signature[]>;
35
+ /**
36
+ * Sign EIP-712 typed data with a specific address
37
+ * @param address - The address of the signer to use
38
+ * @param typedData - The complete EIP-712 typed data structure (domain, types, primaryType, message)
39
+ * @returns signature for the specified address
40
+ * @throws Error if the address is not found in the keystore or signing fails
41
+ */
42
+ signTypedDataWithAddress(address: EthAddress, typedData: TypedDataDefinition): Promise<Signature>;
43
+ /**
44
+ * Sign a message with all keystore addresses using EIP-191 prefix
45
+ *
46
+ * @param message - The message to sign
47
+ * @return signatures
48
+ */
49
+ signMessage(message: Buffer32): Promise<Signature[]>;
50
+ /**
51
+ * Sign a message with a specific address using EIP-191 prefix
52
+ * @param address - The address of the signer to use
53
+ * @param message - The message to sign
54
+ * @returns signature for the specified address
55
+ * @throws Error if the address is not found in the keystore or signing fails
56
+ */
57
+ signMessageWithAddress(address: EthAddress, message: Buffer32): Promise<Signature>;
58
+ /**
59
+ * Make a JSON-RPC sign request to Web3Signer using eth_sign
60
+ * @param address - The Ethereum address to sign with
61
+ * @param data - The data to sign
62
+ * @returns The signature
63
+ */
64
+ private makeJsonRpcSignRequest;
65
+ private makeJsonRpcSignTypedDataRequest;
66
+ }
67
+ //# sourceMappingURL=web3signer_key_store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web3signer_key_store.d.ts","sourceRoot":"","sources":["../../src/key_store/web3signer_key_store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAEzD,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAC;AAE5D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,MAAM,CAAC;AAEhD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAExD;;;;;GAKG;AACH,qBAAa,kBAAmB,YAAW,iBAAiB;IAExD,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,OAAO;gBADP,SAAS,EAAE,UAAU,EAAE,EACvB,OAAO,EAAE,MAAM;IAGzB;;;;;OAKG;IACI,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU;IAO5C;;;;OAIG;IACI,YAAY,IAAI,UAAU,EAAE;IAInC;;;;OAIG;IACI,aAAa,CAAC,SAAS,EAAE,mBAAmB,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;IAI1E;;;;;;OAMG;IACU,wBAAwB,CAAC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,mBAAmB,GAAG,OAAO,CAAC,SAAS,CAAC;IAQ9G;;;;;OAKG;IACI,WAAW,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;IAI3D;;;;;;OAMG;IACU,sBAAsB,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,SAAS,CAAC;IAO/F;;;;;OAKG;YACW,sBAAsB;YAiDtB,+BAA+B;CA6C9C"}
@@ -0,0 +1,152 @@
1
+ import { normalizeSignature } from '@aztec/foundation/crypto';
2
+ import { Signature } from '@aztec/foundation/eth-signature';
3
+ /**
4
+ * Web3Signer Key Store
5
+ *
6
+ * An implementation of the Key store using Web3Signer remote signing service.
7
+ * This implementation uses the Web3Signer JSON-RPC API for secp256k1 signatures.
8
+ */ export class Web3SignerKeyStore {
9
+ addresses;
10
+ baseUrl;
11
+ constructor(addresses, baseUrl){
12
+ this.addresses = addresses;
13
+ this.baseUrl = baseUrl;
14
+ }
15
+ /**
16
+ * Get the address of a signer by index
17
+ *
18
+ * @param index - The index of the signer
19
+ * @returns the address
20
+ */ getAddress(index) {
21
+ if (index >= this.addresses.length) {
22
+ throw new Error(`Index ${index} is out of bounds.`);
23
+ }
24
+ return this.addresses[index];
25
+ }
26
+ /**
27
+ * Get all addresses
28
+ *
29
+ * @returns all addresses
30
+ */ getAddresses() {
31
+ return this.addresses;
32
+ }
33
+ /**
34
+ * Sign EIP-712 typed data with all keystore addresses
35
+ * @param typedData - The complete EIP-712 typed data structure (domain, types, primaryType, message)
36
+ * @return signatures
37
+ */ signTypedData(typedData) {
38
+ return Promise.all(this.addresses.map((address)=>this.makeJsonRpcSignTypedDataRequest(address, typedData)));
39
+ }
40
+ /**
41
+ * Sign EIP-712 typed data with a specific address
42
+ * @param address - The address of the signer to use
43
+ * @param typedData - The complete EIP-712 typed data structure (domain, types, primaryType, message)
44
+ * @returns signature for the specified address
45
+ * @throws Error if the address is not found in the keystore or signing fails
46
+ */ async signTypedDataWithAddress(address, typedData) {
47
+ if (!this.addresses.some((addr)=>addr.equals(address))) {
48
+ throw new Error(`Address ${address.toString()} not found in keystore`);
49
+ }
50
+ return await this.makeJsonRpcSignTypedDataRequest(address, typedData);
51
+ }
52
+ /**
53
+ * Sign a message with all keystore addresses using EIP-191 prefix
54
+ *
55
+ * @param message - The message to sign
56
+ * @return signatures
57
+ */ signMessage(message) {
58
+ return Promise.all(this.addresses.map((address)=>this.makeJsonRpcSignRequest(address, message)));
59
+ }
60
+ /**
61
+ * Sign a message with a specific address using EIP-191 prefix
62
+ * @param address - The address of the signer to use
63
+ * @param message - The message to sign
64
+ * @returns signature for the specified address
65
+ * @throws Error if the address is not found in the keystore or signing fails
66
+ */ async signMessageWithAddress(address, message) {
67
+ if (!this.addresses.some((addr)=>addr.equals(address))) {
68
+ throw new Error(`Address ${address.toString()} not found in keystore`);
69
+ }
70
+ return await this.makeJsonRpcSignRequest(address, message);
71
+ }
72
+ /**
73
+ * Make a JSON-RPC sign request to Web3Signer using eth_sign
74
+ * @param address - The Ethereum address to sign with
75
+ * @param data - The data to sign
76
+ * @returns The signature
77
+ */ async makeJsonRpcSignRequest(address, data) {
78
+ const url = this.baseUrl;
79
+ // Use JSON-RPC eth_sign method which automatically applies Ethereum message prefixing
80
+ const body = {
81
+ jsonrpc: '2.0',
82
+ method: 'eth_sign',
83
+ params: [
84
+ address.toString(),
85
+ data.toString()
86
+ ],
87
+ id: 1
88
+ };
89
+ const response = await fetch(url, {
90
+ method: 'POST',
91
+ headers: {
92
+ 'Content-Type': 'application/json'
93
+ },
94
+ body: JSON.stringify(body)
95
+ });
96
+ if (!response.ok) {
97
+ const errorText = await response.text();
98
+ throw new Error(`Web3Signer request failed: ${response.status} ${response.statusText} - ${errorText}`);
99
+ }
100
+ const result = await response.json();
101
+ // Handle JSON-RPC response format
102
+ if (result.error) {
103
+ throw new Error(`Web3Signer JSON-RPC error: ${result.error.code} - ${result.error.message}`);
104
+ }
105
+ if (!result.result) {
106
+ throw new Error('Invalid response from Web3Signer: no result found');
107
+ }
108
+ let signatureHex = result.result;
109
+ // Ensure the signature has the 0x prefix
110
+ if (!signatureHex.startsWith('0x')) {
111
+ signatureHex = '0x' + signatureHex;
112
+ }
113
+ // Parse the signature from the hex string
114
+ return normalizeSignature(Signature.fromString(signatureHex));
115
+ }
116
+ async makeJsonRpcSignTypedDataRequest(address, typedData) {
117
+ const url = this.baseUrl;
118
+ const body = {
119
+ jsonrpc: '2.0',
120
+ method: 'eth_signTypedData',
121
+ params: [
122
+ address.toString(),
123
+ JSON.stringify(typedData)
124
+ ],
125
+ id: 1
126
+ };
127
+ const response = await fetch(url, {
128
+ method: 'POST',
129
+ headers: {
130
+ 'Content-Type': 'application/json'
131
+ },
132
+ body: JSON.stringify(body)
133
+ });
134
+ if (!response.ok) {
135
+ const errorText = await response.text();
136
+ throw new Error(`Web3Signer request failed: ${response.status} ${response.statusText} - ${errorText}`);
137
+ }
138
+ const result = await response.json();
139
+ if (result.error) {
140
+ throw new Error(`Web3Signer JSON-RPC error: ${result.error.code} - ${result.error.message}`);
141
+ }
142
+ if (!result.result) {
143
+ throw new Error('Invalid response from Web3Signer: no result found');
144
+ }
145
+ let signatureHex = result.result;
146
+ // Ensure the signature has the 0x prefix
147
+ if (!signatureHex.startsWith('0x')) {
148
+ signatureHex = '0x' + signatureHex;
149
+ }
150
+ return normalizeSignature(Signature.fromString(signatureHex));
151
+ }
152
+ }
package/dest/metrics.d.ts CHANGED
@@ -1,11 +1,18 @@
1
1
  import type { BlockProposal } from '@aztec/stdlib/p2p';
2
2
  import { type TelemetryClient } from '@aztec/telemetry-client';
3
3
  export declare class ValidatorMetrics {
4
- private reExecutionTime;
5
4
  private failedReexecutionCounter;
5
+ private successfulAttestationsCount;
6
+ private failedAttestationsBadProposalCount;
7
+ private failedAttestationsNodeIssueCount;
8
+ private reexMana;
9
+ private reexTx;
10
+ private reexDuration;
6
11
  constructor(telemetryClient: TelemetryClient);
7
- reExecutionTimer(): () => void;
8
- recordReExecutionTime(time: number): void;
9
- recordFailedReexecution(proposal: BlockProposal): Promise<void>;
12
+ recordReex(time: number, txs: number, mManaTotal: number): void;
13
+ recordFailedReexecution(proposal: BlockProposal): void;
14
+ incSuccessfulAttestations(num: number): void;
15
+ incFailedAttestationsBadProposal(num: number, reason: string, inCommittee: boolean): void;
16
+ incFailedAttestationsNodeIssue(num: number, reason: string, inCommittee: boolean): void;
10
17
  }
11
18
  //# sourceMappingURL=metrics.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"metrics.d.ts","sourceRoot":"","sources":["../src/metrics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAIL,KAAK,eAAe,EAGrB,MAAM,yBAAyB,CAAC;AAEjC,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,eAAe,CAAQ;IAC/B,OAAO,CAAC,wBAAwB,CAAgB;gBAEpC,eAAe,EAAE,eAAe;IAgBrC,gBAAgB,IAAI,MAAM,IAAI;IAQ9B,qBAAqB,CAAC,IAAI,EAAE,MAAM;IAI5B,uBAAuB,CAAC,QAAQ,EAAE,aAAa;CAO7D"}
1
+ {"version":3,"file":"metrics.d.ts","sourceRoot":"","sources":["../src/metrics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAIL,KAAK,eAAe,EAGrB,MAAM,yBAAyB,CAAC;AAEjC,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,wBAAwB,CAAgB;IAChD,OAAO,CAAC,2BAA2B,CAAgB;IACnD,OAAO,CAAC,kCAAkC,CAAgB;IAC1D,OAAO,CAAC,gCAAgC,CAAgB;IAExD,OAAO,CAAC,QAAQ,CAAY;IAC5B,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,YAAY,CAAY;gBAEpB,eAAe,EAAE,eAAe;IAiDrC,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;IAMxD,uBAAuB,CAAC,QAAQ,EAAE,aAAa;IAQ/C,yBAAyB,CAAC,GAAG,EAAE,MAAM;IAIrC,gCAAgC,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO;IAOlF,8BAA8B,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO;CAMxF"}
package/dest/metrics.js CHANGED
@@ -1,7 +1,12 @@
1
1
  import { Attributes, Metrics, ValueType } from '@aztec/telemetry-client';
2
2
  export class ValidatorMetrics {
3
- reExecutionTime;
4
3
  failedReexecutionCounter;
4
+ successfulAttestationsCount;
5
+ failedAttestationsBadProposalCount;
6
+ failedAttestationsNodeIssueCount;
7
+ reexMana;
8
+ reexTx;
9
+ reexDuration;
5
10
  constructor(telemetryClient){
6
11
  const meter = telemetryClient.getMeter('Validator');
7
12
  this.failedReexecutionCounter = meter.createUpDownCounter(Metrics.VALIDATOR_FAILED_REEXECUTION_COUNT, {
@@ -9,27 +14,59 @@ export class ValidatorMetrics {
9
14
  unit: 'count',
10
15
  valueType: ValueType.INT
11
16
  });
12
- this.reExecutionTime = meter.createGauge(Metrics.VALIDATOR_RE_EXECUTION_TIME, {
17
+ this.successfulAttestationsCount = meter.createUpDownCounter(Metrics.VALIDATOR_ATTESTATION_SUCCESS_COUNT, {
18
+ description: 'The number of successful attestations',
19
+ valueType: ValueType.INT
20
+ });
21
+ this.failedAttestationsBadProposalCount = meter.createUpDownCounter(Metrics.VALIDATOR_ATTESTATION_FAILED_BAD_PROPOSAL_COUNT, {
22
+ description: 'The number of failed attestations due to invalid block proposals',
23
+ valueType: ValueType.INT
24
+ });
25
+ this.failedAttestationsNodeIssueCount = meter.createUpDownCounter(Metrics.VALIDATOR_ATTESTATION_FAILED_NODE_ISSUE_COUNT, {
26
+ description: 'The number of failed attestations due to node issues (timeout, missing data, etc.)',
27
+ valueType: ValueType.INT
28
+ });
29
+ this.reexMana = meter.createHistogram(Metrics.VALIDATOR_RE_EXECUTION_MANA, {
30
+ description: 'The mana consumed by blocks',
31
+ valueType: ValueType.DOUBLE,
32
+ unit: 'Mmana'
33
+ });
34
+ this.reexTx = meter.createHistogram(Metrics.VALIDATOR_RE_EXECUTION_TX_COUNT, {
35
+ description: 'The number of txs in a block proposal',
36
+ valueType: ValueType.INT,
37
+ unit: 'tx'
38
+ });
39
+ this.reexDuration = meter.createGauge(Metrics.VALIDATOR_RE_EXECUTION_TIME, {
13
40
  description: 'The time taken to re-execute a transaction',
14
41
  unit: 'ms',
15
- valueType: ValueType.DOUBLE
42
+ valueType: ValueType.INT
16
43
  });
17
44
  }
18
- reExecutionTimer() {
19
- const start = performance.now();
20
- return ()=>{
21
- const end = performance.now();
22
- this.recordReExecutionTime(end - start);
23
- };
24
- }
25
- recordReExecutionTime(time) {
26
- this.reExecutionTime.record(time);
45
+ recordReex(time, txs, mManaTotal) {
46
+ this.reexDuration.record(Math.ceil(time));
47
+ this.reexTx.record(txs);
48
+ this.reexMana.record(mManaTotal);
27
49
  }
28
- async recordFailedReexecution(proposal) {
50
+ recordFailedReexecution(proposal) {
51
+ const proposer = proposal.getSender();
29
52
  this.failedReexecutionCounter.add(1, {
30
53
  [Attributes.STATUS]: 'failed',
31
- [Attributes.BLOCK_NUMBER]: proposal.payload.header.globalVariables.blockNumber.toString(),
32
- [Attributes.BLOCK_PROPOSER]: (await proposal.getSender())?.toString()
54
+ [Attributes.BLOCK_PROPOSER]: proposer?.toString() ?? 'unknown'
55
+ });
56
+ }
57
+ incSuccessfulAttestations(num) {
58
+ this.successfulAttestationsCount.add(num);
59
+ }
60
+ incFailedAttestationsBadProposal(num, reason, inCommittee) {
61
+ this.failedAttestationsBadProposalCount.add(num, {
62
+ [Attributes.ERROR_TYPE]: reason,
63
+ [Attributes.IS_COMMITTEE_MEMBER]: inCommittee
64
+ });
65
+ }
66
+ incFailedAttestationsNodeIssue(num, reason, inCommittee) {
67
+ this.failedAttestationsNodeIssueCount.add(num, {
68
+ [Attributes.ERROR_TYPE]: reason,
69
+ [Attributes.IS_COMMITTEE_MEMBER]: inCommittee
33
70
  });
34
71
  }
35
72
  }