|
| 1 | +import { EthAddress } from '@aztec/foundation/eth-address'; |
| 2 | +import type { AztecAddressHex, EthAddressHex, EthPrivateKey } from '@aztec/node-keystore'; |
| 3 | +import type { SharedNodeConfig } from '@aztec/node-lib/config'; |
| 4 | +import type { SequencerClientConfig, TxSenderConfig } from '@aztec/sequencer-client/config'; |
| 5 | +import { AztecAddress } from '@aztec/stdlib/aztec-address'; |
| 6 | +import type { ValidatorClientConfig } from '@aztec/validator-client/config'; |
| 7 | + |
| 8 | +import { generatePrivateKey, privateKeyToAddress } from 'viem/accounts'; |
| 9 | + |
| 10 | +import { createKeyStoreForValidator } from './config.js'; |
| 11 | + |
| 12 | +describe('createKeyStoreForValidator', () => { |
| 13 | + const mockValidatorKey1 = generatePrivateKey() as EthPrivateKey; |
| 14 | + const mockValidatorKey2 = generatePrivateKey() as EthPrivateKey; |
| 15 | + const mockPublisherKey1 = generatePrivateKey() as EthPrivateKey; |
| 16 | + const mockPublisherKey2 = generatePrivateKey() as EthPrivateKey; |
| 17 | + const mockCoinbase = EthAddress.random().toString() as EthAddressHex; |
| 18 | + const web3SignerUrl = 'http://web3signer:1000'; |
| 19 | + const mockValidatorAddresses = [mockValidatorKey1, mockValidatorKey2].map(privateKeyToAddress); |
| 20 | + const mockPublisherAddresses = [mockPublisherKey1, mockPublisherKey2].map(privateKeyToAddress); |
| 21 | + let mockFeeRecipient: AztecAddressHex; |
| 22 | + |
| 23 | + const createMockConfig = ( |
| 24 | + validatorKeys: string[] = [], |
| 25 | + publisherKeys: string[] = [], |
| 26 | + coinbase?: string, |
| 27 | + feeRecipient?: string, |
| 28 | + web3SignerUrl?: string, |
| 29 | + validatorAddresses: string[] = [], |
| 30 | + publisherAddresses: string[] = [], |
| 31 | + ): TxSenderConfig & ValidatorClientConfig & SequencerClientConfig & SharedNodeConfig => { |
| 32 | + const mockValidatorPrivateKeys = |
| 33 | + validatorKeys.length > 0 |
| 34 | + ? { |
| 35 | + getValue: () => validatorKeys, |
| 36 | + } |
| 37 | + : undefined; |
| 38 | + |
| 39 | + const mockPublisherPrivateKeys = |
| 40 | + publisherKeys.length > 0 ? publisherKeys.map(key => ({ getValue: () => key })) : undefined; |
| 41 | + |
| 42 | + return { |
| 43 | + validatorPrivateKeys: mockValidatorPrivateKeys, |
| 44 | + publisherPrivateKeys: mockPublisherPrivateKeys, |
| 45 | + coinbase: coinbase ? { toString: () => coinbase } : undefined, |
| 46 | + feeRecipient: feeRecipient ? { toString: () => feeRecipient } : undefined, |
| 47 | + web3SignerUrl, |
| 48 | + validatorAddresses: validatorAddresses.map(addr => EthAddress.fromString(addr)), |
| 49 | + publisherAddresses: publisherAddresses.map(addr => EthAddress.fromString(addr)), |
| 50 | + } as TxSenderConfig & ValidatorClientConfig & SequencerClientConfig & SharedNodeConfig; |
| 51 | + }; |
| 52 | + |
| 53 | + beforeAll(async () => { |
| 54 | + mockFeeRecipient = (await AztecAddress.random()).toString() as AztecAddressHex; |
| 55 | + }); |
| 56 | + |
| 57 | + it('should return undefined when no validator keys are provided', () => { |
| 58 | + const config = createMockConfig([]); |
| 59 | + const result = createKeyStoreForValidator(config); |
| 60 | + expect(result).toBeUndefined(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should return undefined when validatorPrivateKeys is undefined', () => { |
| 64 | + const config = { |
| 65 | + validatorPrivateKeys: undefined, |
| 66 | + publisherPrivateKeys: undefined, |
| 67 | + coinbase: undefined, |
| 68 | + feeRecipient: undefined, |
| 69 | + } as unknown as TxSenderConfig & ValidatorClientConfig & SequencerClientConfig & SharedNodeConfig; |
| 70 | + const result = createKeyStoreForValidator(config); |
| 71 | + expect(result).toBeUndefined(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should create keystore with single validator key and default coinbase/feeRecipient', () => { |
| 75 | + const config = createMockConfig([mockValidatorKey1]); |
| 76 | + const result = createKeyStoreForValidator(config); |
| 77 | + |
| 78 | + const expectedCoinbase = privateKeyToAddress(mockValidatorKey1); |
| 79 | + const expectedFeeRecipient = AztecAddress.ZERO.toString(); |
| 80 | + |
| 81 | + expect(result).toEqual({ |
| 82 | + schemaVersion: 1, |
| 83 | + slasher: undefined, |
| 84 | + prover: undefined, |
| 85 | + remoteSigner: undefined, |
| 86 | + validators: [ |
| 87 | + { |
| 88 | + attester: [mockValidatorKey1], |
| 89 | + feeRecipient: expectedFeeRecipient, |
| 90 | + coinbase: expectedCoinbase, |
| 91 | + remoteSigner: undefined, |
| 92 | + publisher: [], |
| 93 | + }, |
| 94 | + ], |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should create keystore with multiple validator keys', () => { |
| 99 | + const config = createMockConfig([mockValidatorKey1, mockValidatorKey2]); |
| 100 | + const result = createKeyStoreForValidator(config); |
| 101 | + |
| 102 | + const expectedCoinbase = privateKeyToAddress(mockValidatorKey1); |
| 103 | + |
| 104 | + expect(result).toEqual({ |
| 105 | + schemaVersion: 1, |
| 106 | + slasher: undefined, |
| 107 | + prover: undefined, |
| 108 | + remoteSigner: undefined, |
| 109 | + validators: [ |
| 110 | + { |
| 111 | + attester: [mockValidatorKey1, mockValidatorKey2], |
| 112 | + feeRecipient: AztecAddress.ZERO.toString(), |
| 113 | + coinbase: expectedCoinbase, |
| 114 | + remoteSigner: undefined, |
| 115 | + publisher: [], |
| 116 | + }, |
| 117 | + ], |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should create keystore with custom coinbase and feeRecipient', () => { |
| 122 | + const config = createMockConfig([mockValidatorKey1], [], mockCoinbase, mockFeeRecipient); |
| 123 | + const result = createKeyStoreForValidator(config); |
| 124 | + |
| 125 | + expect(result).toEqual({ |
| 126 | + schemaVersion: 1, |
| 127 | + slasher: undefined, |
| 128 | + prover: undefined, |
| 129 | + remoteSigner: undefined, |
| 130 | + validators: [ |
| 131 | + { |
| 132 | + attester: [mockValidatorKey1], |
| 133 | + feeRecipient: mockFeeRecipient, |
| 134 | + coinbase: mockCoinbase, |
| 135 | + remoteSigner: undefined, |
| 136 | + publisher: [], |
| 137 | + }, |
| 138 | + ], |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should create keystore with publisher keys', () => { |
| 143 | + const config = createMockConfig([mockValidatorKey1], [mockPublisherKey1, mockPublisherKey2]); |
| 144 | + const result = createKeyStoreForValidator(config); |
| 145 | + |
| 146 | + const expectedCoinbase = privateKeyToAddress(mockValidatorKey1); |
| 147 | + |
| 148 | + expect(result).toEqual({ |
| 149 | + schemaVersion: 1, |
| 150 | + slasher: undefined, |
| 151 | + prover: undefined, |
| 152 | + remoteSigner: undefined, |
| 153 | + validators: [ |
| 154 | + { |
| 155 | + attester: [mockValidatorKey1], |
| 156 | + feeRecipient: AztecAddress.ZERO.toString(), |
| 157 | + coinbase: expectedCoinbase, |
| 158 | + remoteSigner: undefined, |
| 159 | + publisher: [mockPublisherKey1, mockPublisherKey2], |
| 160 | + }, |
| 161 | + ], |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should create keystore with all fields populated', () => { |
| 166 | + const config = createMockConfig( |
| 167 | + [mockValidatorKey1, mockValidatorKey2], |
| 168 | + [mockPublisherKey1], |
| 169 | + mockCoinbase, |
| 170 | + mockFeeRecipient, |
| 171 | + ); |
| 172 | + const result = createKeyStoreForValidator(config); |
| 173 | + |
| 174 | + expect(result).toEqual({ |
| 175 | + schemaVersion: 1, |
| 176 | + slasher: undefined, |
| 177 | + prover: undefined, |
| 178 | + remoteSigner: undefined, |
| 179 | + validators: [ |
| 180 | + { |
| 181 | + attester: [mockValidatorKey1, mockValidatorKey2], |
| 182 | + feeRecipient: mockFeeRecipient, |
| 183 | + coinbase: mockCoinbase, |
| 184 | + remoteSigner: undefined, |
| 185 | + publisher: [mockPublisherKey1], |
| 186 | + }, |
| 187 | + ], |
| 188 | + }); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should handle empty publisher keys array', () => { |
| 192 | + const config = createMockConfig([mockValidatorKey1], []); |
| 193 | + const result = createKeyStoreForValidator(config); |
| 194 | + |
| 195 | + expect(result?.validators?.[0]?.publisher).toEqual([]); |
| 196 | + }); |
| 197 | + |
| 198 | + it('should use first validator key for coinbase when no coinbase provided', () => { |
| 199 | + const config = createMockConfig([mockValidatorKey1, mockValidatorKey2]); |
| 200 | + const result = createKeyStoreForValidator(config); |
| 201 | + |
| 202 | + const expectedCoinbase = privateKeyToAddress(mockValidatorKey1); |
| 203 | + expect(result?.validators?.[0]?.coinbase).toBe(expectedCoinbase); |
| 204 | + }); |
| 205 | + |
| 206 | + it('should use AztecAddress.ZERO for feeRecipient when not provided', () => { |
| 207 | + const config = createMockConfig([mockValidatorKey1]); |
| 208 | + const result = createKeyStoreForValidator(config); |
| 209 | + |
| 210 | + expect(result?.validators?.[0]?.feeRecipient).toBe(AztecAddress.ZERO.toString()); |
| 211 | + }); |
| 212 | + |
| 213 | + it('should create keystore with remote signer details', () => { |
| 214 | + const config = createMockConfig( |
| 215 | + [], |
| 216 | + [], |
| 217 | + mockCoinbase, |
| 218 | + mockFeeRecipient, |
| 219 | + web3SignerUrl, |
| 220 | + mockValidatorAddresses, |
| 221 | + mockPublisherAddresses, |
| 222 | + ); |
| 223 | + const result = createKeyStoreForValidator(config); |
| 224 | + |
| 225 | + expect(result).toEqual({ |
| 226 | + schemaVersion: 1, |
| 227 | + slasher: undefined, |
| 228 | + prover: undefined, |
| 229 | + remoteSigner: undefined, |
| 230 | + validators: [ |
| 231 | + { |
| 232 | + attester: mockValidatorAddresses, |
| 233 | + feeRecipient: mockFeeRecipient, |
| 234 | + coinbase: mockCoinbase, |
| 235 | + remoteSigner: web3SignerUrl, |
| 236 | + publisher: mockPublisherAddresses, |
| 237 | + }, |
| 238 | + ], |
| 239 | + }); |
| 240 | + }); |
| 241 | +}); |
0 commit comments