@aztec/foundation 0.0.1-commit.f2ce05ee → 0.0.1-commit.f5d02921e
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/dest/array/sorted_array.d.ts +6 -1
- package/dest/array/sorted_array.d.ts.map +1 -1
- package/dest/array/sorted_array.js +18 -15
- package/dest/branded-types/slot.d.ts +4 -1
- package/dest/branded-types/slot.d.ts.map +1 -1
- package/dest/branded-types/slot.js +3 -0
- package/dest/buffer/buffer16.d.ts +3 -1
- package/dest/buffer/buffer16.d.ts.map +1 -1
- package/dest/buffer/buffer32.d.ts +3 -1
- package/dest/buffer/buffer32.d.ts.map +1 -1
- package/dest/buffer/index.d.ts +2 -1
- package/dest/buffer/index.d.ts.map +1 -1
- package/dest/buffer/index.js +1 -0
- package/dest/buffer/utils.d.ts +3 -0
- package/dest/buffer/utils.d.ts.map +1 -0
- package/dest/buffer/utils.js +7 -0
- package/dest/collection/array.d.ts +3 -1
- package/dest/collection/array.d.ts.map +1 -1
- package/dest/collection/array.js +15 -0
- package/dest/collection/index.d.ts +2 -1
- package/dest/collection/index.d.ts.map +1 -1
- package/dest/collection/index.js +1 -0
- package/dest/collection/lru_set.d.ts +35 -0
- package/dest/collection/lru_set.d.ts.map +1 -0
- package/dest/collection/lru_set.js +95 -0
- package/dest/config/env_var.d.ts +2 -2
- package/dest/config/env_var.d.ts.map +1 -1
- package/dest/config/index.d.ts +1 -1
- package/dest/config/index.d.ts.map +1 -1
- package/dest/config/index.js +15 -0
- package/dest/config/network_config.d.ts +19 -1
- package/dest/config/network_config.d.ts.map +1 -1
- package/dest/config/network_config.js +4 -1
- package/dest/config/network_name.d.ts +2 -2
- package/dest/config/network_name.d.ts.map +1 -1
- package/dest/config/network_name.js +2 -0
- package/dest/crypto/poseidon/index.d.ts +1 -1
- package/dest/crypto/poseidon/index.d.ts.map +1 -1
- package/dest/crypto/poseidon/index.js +40 -33
- package/dest/crypto/secp256k1-signer/utils.d.ts +12 -1
- package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -1
- package/dest/crypto/secp256k1-signer/utils.js +26 -0
- package/dest/curves/bn254/field.d.ts +2 -1
- package/dest/curves/bn254/field.d.ts.map +1 -1
- package/dest/curves/bn254/field.js +5 -2
- package/dest/eth-signature/eth_signature.d.ts +2 -1
- package/dest/eth-signature/eth_signature.d.ts.map +1 -1
- package/dest/eth-signature/eth_signature.js +7 -2
- package/dest/fifo/fifo_frame_reader.d.ts +41 -0
- package/dest/fifo/fifo_frame_reader.d.ts.map +1 -0
- package/dest/fifo/fifo_frame_reader.js +74 -0
- package/dest/fifo/index.d.ts +2 -0
- package/dest/fifo/index.d.ts.map +1 -0
- package/dest/fifo/index.js +1 -0
- package/dest/jest/setup.js +24 -0
- package/dest/json-rpc/client/safe_json_rpc_client.d.ts +2 -1
- package/dest/json-rpc/client/safe_json_rpc_client.d.ts.map +1 -1
- package/dest/json-rpc/client/safe_json_rpc_client.js +1 -1
- package/dest/json-rpc/server/api_key_auth.d.ts +19 -0
- package/dest/json-rpc/server/api_key_auth.d.ts.map +1 -0
- package/dest/json-rpc/server/api_key_auth.js +57 -0
- package/dest/json-rpc/server/index.d.ts +2 -1
- package/dest/json-rpc/server/index.d.ts.map +1 -1
- package/dest/json-rpc/server/index.js +1 -0
- package/dest/log/bigint-utils.d.ts +5 -0
- package/dest/log/bigint-utils.d.ts.map +1 -0
- package/dest/log/bigint-utils.js +21 -0
- package/dest/log/gcloud-logger-config.d.ts +1 -1
- package/dest/log/gcloud-logger-config.d.ts.map +1 -1
- package/dest/log/gcloud-logger-config.js +3 -0
- package/dest/log/pino-logger.d.ts +1 -1
- package/dest/log/pino-logger.d.ts.map +1 -1
- package/dest/log/pino-logger.js +6 -1
- package/dest/queue/base_memory_queue.d.ts +2 -2
- package/dest/queue/base_memory_queue.d.ts.map +1 -1
- package/dest/schemas/api.d.ts +2 -2
- package/dest/schemas/api.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.d.ts +8 -1
- package/dest/serialize/buffer_reader.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.js +13 -0
- package/dest/serialize/serialize.d.ts +19 -1
- package/dest/serialize/serialize.d.ts.map +1 -1
- package/dest/serialize/serialize.js +31 -0
- package/dest/sleep/index.d.ts +2 -1
- package/dest/sleep/index.d.ts.map +1 -1
- package/dest/sleep/index.js +10 -1
- package/dest/timer/date.d.ts +3 -1
- package/dest/timer/date.d.ts.map +1 -1
- package/dest/timer/date.js +4 -0
- package/dest/transport/transport_client.js +2 -2
- package/dest/trees/indexed_merkle_tree_calculator.d.ts +1 -1
- package/dest/trees/indexed_merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/indexed_merkle_tree_calculator.js +5 -1
- package/package.json +3 -2
- package/src/array/sorted_array.ts +22 -17
- package/src/branded-types/slot.ts +5 -0
- package/src/buffer/buffer16.ts +3 -0
- package/src/buffer/buffer32.ts +3 -0
- package/src/buffer/index.ts +1 -0
- package/src/buffer/utils.ts +8 -0
- package/src/collection/array.ts +14 -0
- package/src/collection/index.ts +1 -0
- package/src/collection/lru_set.ts +115 -0
- package/src/config/env_var.ts +45 -8
- package/src/config/index.ts +15 -0
- package/src/config/network_config.ts +3 -0
- package/src/config/network_name.ts +4 -1
- package/src/crypto/poseidon/index.ts +42 -34
- package/src/crypto/secp256k1-signer/utils.ts +32 -0
- package/src/curves/bn254/field.ts +6 -2
- package/src/eth-signature/eth_signature.ts +7 -1
- package/src/fifo/fifo_frame_reader.ts +98 -0
- package/src/fifo/index.ts +1 -0
- package/src/jest/setup.mjs +27 -0
- package/src/json-rpc/client/safe_json_rpc_client.ts +2 -0
- package/src/json-rpc/server/api_key_auth.ts +63 -0
- package/src/json-rpc/server/index.ts +1 -0
- package/src/log/bigint-utils.ts +25 -0
- package/src/log/gcloud-logger-config.ts +5 -0
- package/src/log/pino-logger.ts +7 -1
- package/src/queue/base_memory_queue.ts +1 -1
- package/src/schemas/api.ts +4 -1
- package/src/serialize/buffer_reader.ts +15 -0
- package/src/serialize/serialize.ts +32 -0
- package/src/sleep/index.ts +10 -1
- package/src/timer/date.ts +6 -0
- package/src/transport/transport_client.ts +2 -2
- package/src/trees/indexed_merkle_tree_calculator.ts +5 -1
- package/dest/crypto/serialize.d.ts +0 -51
- package/dest/crypto/serialize.d.ts.map +0 -1
- package/dest/crypto/serialize.js +0 -68
- package/src/crypto/serialize.ts +0 -85
|
@@ -1,21 +1,35 @@
|
|
|
1
|
-
import { BarretenbergSync } from '@aztec/bb.js';
|
|
1
|
+
import { Barretenberg, BarretenbergSync } from '@aztec/bb.js';
|
|
2
2
|
|
|
3
3
|
import { Fr } from '../../curves/bn254/field.js';
|
|
4
4
|
import { type Fieldable, serializeToFields } from '../../serialize/serialize.js';
|
|
5
5
|
|
|
6
|
+
const IS_BROWSER = typeof self !== 'undefined';
|
|
7
|
+
|
|
8
|
+
async function poseidon2HashFields(inputFields: Fr[]): Promise<Fr> {
|
|
9
|
+
if (IS_BROWSER) {
|
|
10
|
+
await BarretenbergSync.initSingleton();
|
|
11
|
+
const api = BarretenbergSync.getSingleton();
|
|
12
|
+
const response = api.poseidon2Hash({
|
|
13
|
+
inputs: inputFields.map(i => i.toBuffer()),
|
|
14
|
+
});
|
|
15
|
+
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
16
|
+
} else {
|
|
17
|
+
await Barretenberg.initSingleton();
|
|
18
|
+
const api = Barretenberg.getSingleton();
|
|
19
|
+
const response = await api.poseidon2Hash({
|
|
20
|
+
inputs: inputFields.map(i => i.toBuffer()),
|
|
21
|
+
});
|
|
22
|
+
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
6
26
|
/**
|
|
7
27
|
* Create a poseidon hash (field) from an array of input fields.
|
|
8
28
|
* @param input - The input fields to hash.
|
|
9
29
|
* @returns The poseidon hash.
|
|
10
30
|
*/
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
await BarretenbergSync.initSingleton();
|
|
14
|
-
const api = BarretenbergSync.getSingleton();
|
|
15
|
-
const response = api.poseidon2Hash({
|
|
16
|
-
inputs: inputFields.map(i => i.toBuffer()),
|
|
17
|
-
});
|
|
18
|
-
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
31
|
+
export function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
|
|
32
|
+
return poseidon2HashFields(serializeToFields(input));
|
|
19
33
|
}
|
|
20
34
|
|
|
21
35
|
/**
|
|
@@ -24,15 +38,10 @@ export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
|
|
|
24
38
|
* @param separator - The domain separator.
|
|
25
39
|
* @returns The poseidon hash.
|
|
26
40
|
*/
|
|
27
|
-
export
|
|
41
|
+
export function poseidon2HashWithSeparator(input: Fieldable[], separator: number): Promise<Fr> {
|
|
28
42
|
const inputFields = serializeToFields(input);
|
|
29
43
|
inputFields.unshift(new Fr(separator));
|
|
30
|
-
|
|
31
|
-
const api = BarretenbergSync.getSingleton();
|
|
32
|
-
const response = api.poseidon2Hash({
|
|
33
|
-
inputs: inputFields.map(i => i.toBuffer()),
|
|
34
|
-
});
|
|
35
|
-
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
44
|
+
return poseidon2HashFields(inputFields);
|
|
36
45
|
}
|
|
37
46
|
|
|
38
47
|
/**
|
|
@@ -42,19 +51,24 @@ export async function poseidon2HashWithSeparator(input: Fieldable[], separator:
|
|
|
42
51
|
*/
|
|
43
52
|
export async function poseidon2Permutation(input: Fieldable[]): Promise<Fr[]> {
|
|
44
53
|
const inputFields = serializeToFields(input);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
if (IS_BROWSER) {
|
|
55
|
+
await BarretenbergSync.initSingleton();
|
|
56
|
+
const api = BarretenbergSync.getSingleton();
|
|
57
|
+
const response = api.poseidon2Permutation({
|
|
58
|
+
inputs: inputFields.map(i => i.toBuffer()),
|
|
59
|
+
});
|
|
60
|
+
return response.outputs.map(o => Fr.fromBuffer(Buffer.from(o)));
|
|
61
|
+
} else {
|
|
62
|
+
await Barretenberg.initSingleton();
|
|
63
|
+
const api = Barretenberg.getSingleton();
|
|
64
|
+
const response = await api.poseidon2Permutation({
|
|
65
|
+
inputs: inputFields.map(i => i.toBuffer()),
|
|
66
|
+
});
|
|
67
|
+
return response.outputs.map(o => Fr.fromBuffer(Buffer.from(o)));
|
|
68
|
+
}
|
|
55
69
|
}
|
|
56
70
|
|
|
57
|
-
export
|
|
71
|
+
export function poseidon2HashBytes(input: Buffer): Promise<Fr> {
|
|
58
72
|
const inputFields = [];
|
|
59
73
|
for (let i = 0; i < input.length; i += 31) {
|
|
60
74
|
const fieldBytes = Buffer.alloc(32, 0);
|
|
@@ -65,11 +79,5 @@ export async function poseidon2HashBytes(input: Buffer): Promise<Fr> {
|
|
|
65
79
|
inputFields.push(Fr.fromBuffer(fieldBytes));
|
|
66
80
|
}
|
|
67
81
|
|
|
68
|
-
|
|
69
|
-
const api = BarretenbergSync.getSingleton();
|
|
70
|
-
const response = api.poseidon2Hash({
|
|
71
|
-
inputs: inputFields.map(i => i.toBuffer()),
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
82
|
+
return poseidon2HashFields(inputFields);
|
|
75
83
|
}
|
|
@@ -210,3 +210,35 @@ export function recoverPublicKey(hash: Buffer32, signature: Signature, opts: Rec
|
|
|
210
210
|
const publicKey = sig.recoverPublicKey(hash.buffer).toHex(false);
|
|
211
211
|
return Buffer.from(publicKey, 'hex');
|
|
212
212
|
}
|
|
213
|
+
|
|
214
|
+
/** Arbitrary hash used for testing signature recoverability. */
|
|
215
|
+
const PROBE_HASH = Buffer32.fromBuffer(keccak256(Buffer.from('signature-recoverability-probe')));
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Generates a random valid ECDSA signature that is recoverable to some address.
|
|
219
|
+
* Since Signature.random() produces real signatures via secp256k1 signing, the result is always
|
|
220
|
+
* recoverable, but we verify defensively by checking tryRecoverAddress.
|
|
221
|
+
*/
|
|
222
|
+
export function generateRecoverableSignature(): Signature {
|
|
223
|
+
for (let i = 0; i < 100; i++) {
|
|
224
|
+
const sig = Signature.random();
|
|
225
|
+
if (tryRecoverAddress(PROBE_HASH, sig) !== undefined) {
|
|
226
|
+
return sig;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
throw new Secp256k1Error('Failed to generate a recoverable signature after 100 attempts');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Generates a random signature where ECDSA address recovery fails.
|
|
234
|
+
* Uses random r/s values (not from real signing) so that r is unlikely to be a valid secp256k1 x-coordinate.
|
|
235
|
+
*/
|
|
236
|
+
export function generateUnrecoverableSignature(): Signature {
|
|
237
|
+
for (let i = 0; i < 100; i++) {
|
|
238
|
+
const sig = new Signature(Buffer32.random(), Buffer32.random(), 27);
|
|
239
|
+
if (tryRecoverAddress(PROBE_HASH, sig) === undefined) {
|
|
240
|
+
return sig;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
throw new Secp256k1Error('Failed to generate an unrecoverable signature after 100 attempts');
|
|
244
|
+
}
|
|
@@ -118,14 +118,18 @@ abstract class BaseField {
|
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
cmp(rhs: BaseField): -1 | 0 | 1 {
|
|
121
|
-
|
|
122
|
-
return this.asBigInt === rhsBigInt ? 0 : this.asBigInt < rhsBigInt ? -1 : 1;
|
|
121
|
+
return BaseField.cmpAsBigInt(this.asBigInt, rhs.asBigInt);
|
|
123
122
|
}
|
|
124
123
|
|
|
125
124
|
static cmp(lhs: BaseField, rhs: BaseField): -1 | 0 | 1 {
|
|
126
125
|
return lhs.cmp(rhs);
|
|
127
126
|
}
|
|
128
127
|
|
|
128
|
+
// Actual bigint comparison. Arguments must have been validated previously.
|
|
129
|
+
static cmpAsBigInt(lhs: bigint, rhs: bigint): -1 | 0 | 1 {
|
|
130
|
+
return lhs === rhs ? 0 : lhs < rhs ? -1 : 1;
|
|
131
|
+
}
|
|
132
|
+
|
|
129
133
|
isZero(): boolean {
|
|
130
134
|
return this.asBigInt === 0n;
|
|
131
135
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Buffer32 } from '@aztec/foundation/buffer';
|
|
2
2
|
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
3
3
|
|
|
4
|
+
import { secp256k1 } from '@noble/curves/secp256k1';
|
|
4
5
|
import { z } from 'zod';
|
|
5
6
|
|
|
7
|
+
import { randomBytes } from '../crypto/random/index.js';
|
|
6
8
|
import { hasHexPrefix, hexToBuffer } from '../string/index.js';
|
|
7
9
|
|
|
8
10
|
/**
|
|
@@ -77,8 +79,12 @@ export class Signature {
|
|
|
77
79
|
return new Signature(Buffer32.fromBuffer(hexToBuffer(sig.r)), Buffer32.fromBuffer(hexToBuffer(sig.s)), sig.yParity);
|
|
78
80
|
}
|
|
79
81
|
|
|
82
|
+
/** Generates a random valid ECDSA signature with a low s-value by signing a random message with a random key. */
|
|
80
83
|
static random(): Signature {
|
|
81
|
-
|
|
84
|
+
const privateKey = randomBytes(32);
|
|
85
|
+
const message = randomBytes(32);
|
|
86
|
+
const { r, s, recovery } = secp256k1.sign(message, privateKey);
|
|
87
|
+
return new Signature(Buffer32.fromBigInt(r), Buffer32.fromBigInt(s), recovery ? 28 : 27);
|
|
82
88
|
}
|
|
83
89
|
|
|
84
90
|
static empty(): Signature {
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import EventEmitter from 'node:events';
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import type { Readable } from 'node:stream';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Events emitted by FifoFrameReader.
|
|
7
|
+
*
|
|
8
|
+
* - `frame`: A complete frame payload (without the 4-byte length header).
|
|
9
|
+
* - `error`: An unrecoverable error (invalid frame length, stream error).
|
|
10
|
+
* - `end`: The underlying stream has ended.
|
|
11
|
+
*/
|
|
12
|
+
export interface FifoFrameReaderEvents {
|
|
13
|
+
frame: [payload: Buffer];
|
|
14
|
+
error: [error: Error];
|
|
15
|
+
end: [];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Reads length-delimited frames from a readable stream (typically a named FIFO pipe).
|
|
20
|
+
*
|
|
21
|
+
* Wire format: `[4-byte big-endian payload length][payload bytes]`
|
|
22
|
+
*
|
|
23
|
+
* Emits a `frame` event for each complete frame with the raw payload buffer.
|
|
24
|
+
* Callers are responsible for deserializing the payload (e.g., via msgpack).
|
|
25
|
+
*
|
|
26
|
+
* On encountering an invalid payload length (0 or >maxPayloadSize), emits `error`
|
|
27
|
+
* and destroys the stream.
|
|
28
|
+
*/
|
|
29
|
+
export class FifoFrameReader extends EventEmitter<FifoFrameReaderEvents> {
|
|
30
|
+
private stream: Readable | null = null;
|
|
31
|
+
private pendingBuf: Buffer = Buffer.alloc(0);
|
|
32
|
+
private running = false;
|
|
33
|
+
|
|
34
|
+
constructor(private readonly maxPayloadSize = 10 * 1024 * 1024) {
|
|
35
|
+
super();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Open a FIFO at the given path and start reading frames. */
|
|
39
|
+
start(fifoPath: string, highWaterMark = 64 * 1024): void {
|
|
40
|
+
this.startFromStream(fs.createReadStream(fifoPath, { highWaterMark }));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Start reading frames from an existing readable stream. */
|
|
44
|
+
startFromStream(stream: Readable): void {
|
|
45
|
+
if (this.running) {
|
|
46
|
+
throw new Error('FifoFrameReader is already running');
|
|
47
|
+
}
|
|
48
|
+
this.running = true;
|
|
49
|
+
this.pendingBuf = Buffer.alloc(0);
|
|
50
|
+
this.stream = stream;
|
|
51
|
+
|
|
52
|
+
stream.on('data', (chunk: Buffer | string) => {
|
|
53
|
+
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
54
|
+
this.pendingBuf = this.pendingBuf.length > 0 ? Buffer.concat([this.pendingBuf, buf]) : buf;
|
|
55
|
+
this.drainFrames();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
stream.on('error', (err: Error) => {
|
|
59
|
+
if (this.running) {
|
|
60
|
+
this.emit('error', err);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
stream.on('end', () => {
|
|
65
|
+
this.emit('end');
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Stop reading and destroy the underlying stream. */
|
|
70
|
+
stop(): void {
|
|
71
|
+
this.running = false;
|
|
72
|
+
if (this.stream) {
|
|
73
|
+
this.stream.destroy();
|
|
74
|
+
this.stream = null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Parse complete frames out of the pending buffer. */
|
|
79
|
+
private drainFrames(): void {
|
|
80
|
+
while (this.pendingBuf.length >= 4) {
|
|
81
|
+
const payloadLen = this.pendingBuf.readUInt32BE(0);
|
|
82
|
+
if (payloadLen === 0 || payloadLen > this.maxPayloadSize) {
|
|
83
|
+
this.emit('error', new Error(`Invalid payload length: ${payloadLen}`));
|
|
84
|
+
this.stop();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const frameLen = 4 + payloadLen;
|
|
89
|
+
if (this.pendingBuf.length < frameLen) {
|
|
90
|
+
break; // Wait for more data
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const payload = this.pendingBuf.subarray(4, frameLen);
|
|
94
|
+
this.pendingBuf = this.pendingBuf.subarray(frameLen);
|
|
95
|
+
this.emit('frame', Buffer.from(payload));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FifoFrameReader } from './fifo_frame_reader.js';
|
package/src/jest/setup.mjs
CHANGED
|
@@ -10,3 +10,30 @@ import pretty from 'pino-pretty';
|
|
|
10
10
|
if (!parseBooleanEnv(process.env.LOG_JSON)) {
|
|
11
11
|
overwriteLoggingStream(pretty(pinoPrettyOpts));
|
|
12
12
|
}
|
|
13
|
+
|
|
14
|
+
// Prevent timers from keeping the process alive after tests complete.
|
|
15
|
+
// Libraries like viem create internal polling loops (via setTimeout) that
|
|
16
|
+
// reschedule themselves indefinitely. In test environments we never want a
|
|
17
|
+
// timer to be the reason the process can't exit. We also unref stdout/stderr
|
|
18
|
+
// which, when they are pipes (as in Jest workers), remain ref'd by default.
|
|
19
|
+
{
|
|
20
|
+
const origSetTimeout = globalThis.setTimeout;
|
|
21
|
+
const origSetInterval = globalThis.setInterval;
|
|
22
|
+
globalThis.setTimeout = function unrefSetTimeout(...args) {
|
|
23
|
+
const id = origSetTimeout.apply(this, args);
|
|
24
|
+
id?.unref?.();
|
|
25
|
+
return id;
|
|
26
|
+
};
|
|
27
|
+
// Preserve .unref, .__promisify__ etc. that may exist on the original
|
|
28
|
+
Object.setPrototypeOf(globalThis.setTimeout, origSetTimeout);
|
|
29
|
+
|
|
30
|
+
globalThis.setInterval = function unrefSetInterval(...args) {
|
|
31
|
+
const id = origSetInterval.apply(this, args);
|
|
32
|
+
id?.unref?.();
|
|
33
|
+
return id;
|
|
34
|
+
};
|
|
35
|
+
Object.setPrototypeOf(globalThis.setInterval, origSetInterval);
|
|
36
|
+
|
|
37
|
+
if (process.stdout?._handle?.unref) process.stdout._handle.unref();
|
|
38
|
+
if (process.stderr?._handle?.unref) process.stderr._handle.unref();
|
|
39
|
+
}
|
|
@@ -24,6 +24,7 @@ export type SafeJsonRpcClientOptions = {
|
|
|
24
24
|
batchWindowMS?: number;
|
|
25
25
|
maxBatchSize?: number;
|
|
26
26
|
maxRequestBodySize?: number;
|
|
27
|
+
extraHeaders?: Record<string, string>;
|
|
27
28
|
onResponse?: (res: {
|
|
28
29
|
response: any;
|
|
29
30
|
headers: { get: (header: string) => string | null | undefined };
|
|
@@ -129,6 +130,7 @@ export function createSafeJsonRpcClient<T extends object>(
|
|
|
129
130
|
const { response, headers } = await fetch(
|
|
130
131
|
host,
|
|
131
132
|
rpcCalls.map(({ request }) => request),
|
|
133
|
+
config.extraHeaders,
|
|
132
134
|
);
|
|
133
135
|
|
|
134
136
|
if (config.onResponse) {
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { timingSafeEqual } from 'crypto';
|
|
2
|
+
import type Koa from 'koa';
|
|
3
|
+
|
|
4
|
+
import { sha256 } from '../../crypto/sha256/index.js';
|
|
5
|
+
import { createLogger } from '../../log/index.js';
|
|
6
|
+
|
|
7
|
+
const log = createLogger('json-rpc:api-key-auth');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Computes the SHA-256 hash of a string and returns it as a Buffer.
|
|
11
|
+
* @param input - The input string to hash.
|
|
12
|
+
* @returns The SHA-256 hash as a Buffer.
|
|
13
|
+
*/
|
|
14
|
+
export function sha256Hash(input: string): Buffer {
|
|
15
|
+
return sha256(Buffer.from(input));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Creates a Koa middleware that enforces API key authentication on all requests
|
|
20
|
+
* except the health check endpoint (GET /status).
|
|
21
|
+
*
|
|
22
|
+
* The API key can be provided via the `x-api-key` header or the `Authorization: Bearer <key>` header.
|
|
23
|
+
* Comparison is done by hashing the provided key with SHA-256 and comparing against the stored hash.
|
|
24
|
+
*
|
|
25
|
+
* @param apiKeyHash - The SHA-256 hash of the expected API key as a Buffer.
|
|
26
|
+
* @returns A Koa middleware that rejects requests without a valid API key.
|
|
27
|
+
*/
|
|
28
|
+
export function getApiKeyAuthMiddleware(
|
|
29
|
+
apiKeyHash: Buffer,
|
|
30
|
+
): (ctx: Koa.Context, next: () => Promise<void>) => Promise<void> {
|
|
31
|
+
return async (ctx: Koa.Context, next: () => Promise<void>) => {
|
|
32
|
+
// Allow health check through without auth
|
|
33
|
+
if (ctx.path === '/status' && ctx.method === 'GET') {
|
|
34
|
+
return next();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const providedKey = ctx.get('x-api-key') || ctx.get('authorization')?.replace(/^Bearer\s+/i, '');
|
|
38
|
+
if (!providedKey) {
|
|
39
|
+
log.warn(`Rejected admin RPC request from ${ctx.ip}: missing API key`);
|
|
40
|
+
ctx.status = 401;
|
|
41
|
+
ctx.body = {
|
|
42
|
+
jsonrpc: '2.0',
|
|
43
|
+
id: null,
|
|
44
|
+
error: { code: -32000, message: 'Unauthorized: invalid or missing API key' },
|
|
45
|
+
};
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const providedHashBuf = sha256Hash(providedKey);
|
|
50
|
+
if (!timingSafeEqual(apiKeyHash, providedHashBuf)) {
|
|
51
|
+
log.warn(`Rejected admin RPC request from ${ctx.ip}: invalid API key`);
|
|
52
|
+
ctx.status = 401;
|
|
53
|
+
ctx.body = {
|
|
54
|
+
jsonrpc: '2.0',
|
|
55
|
+
id: null,
|
|
56
|
+
error: { code: -32000, message: 'Unauthorized: invalid or missing API key' },
|
|
57
|
+
};
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
await next();
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts bigint values to strings recursively in a log object to avoid serialization issues.
|
|
3
|
+
*/
|
|
4
|
+
export function convertBigintsToStrings(obj: unknown): unknown {
|
|
5
|
+
if (typeof obj === 'bigint') {
|
|
6
|
+
return String(obj);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (Array.isArray(obj)) {
|
|
10
|
+
return obj.map(item => convertBigintsToStrings(item));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (obj !== null && typeof obj === 'object') {
|
|
14
|
+
if (typeof (obj as any).toJSON === 'function') {
|
|
15
|
+
return convertBigintsToStrings((obj as any).toJSON());
|
|
16
|
+
}
|
|
17
|
+
const result: Record<string, unknown> = {};
|
|
18
|
+
for (const key in obj) {
|
|
19
|
+
result[key] = convertBigintsToStrings((obj as Record<string, unknown>)[key]);
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return obj;
|
|
25
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { pino } from 'pino';
|
|
2
2
|
|
|
3
|
+
import { convertBigintsToStrings } from './bigint-utils.js';
|
|
4
|
+
|
|
3
5
|
/* eslint-disable camelcase */
|
|
4
6
|
|
|
5
7
|
const GOOGLE_CLOUD_TRACE_ID = 'logging.googleapis.com/trace';
|
|
@@ -15,6 +17,9 @@ export const GoogleCloudLoggerConfig = {
|
|
|
15
17
|
messageKey: 'message',
|
|
16
18
|
formatters: {
|
|
17
19
|
log(object: Record<string, unknown>): Record<string, unknown> {
|
|
20
|
+
// Convert bigints to strings recursively to avoid serialization issues
|
|
21
|
+
object = convertBigintsToStrings(object) as Record<string, unknown>;
|
|
22
|
+
|
|
18
23
|
// Add trace context attributes following Cloud Logging structured log format described
|
|
19
24
|
// in https://cloud.google.com/logging/docs/structured-logging#special-payload-fields
|
|
20
25
|
const { trace_id, span_id, trace_flags, ...rest } = object;
|
package/src/log/pino-logger.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { inspect } from 'util';
|
|
|
7
7
|
import { compactArray } from '../collection/array.js';
|
|
8
8
|
import type { EnvVar } from '../config/index.js';
|
|
9
9
|
import { parseBooleanEnv } from '../config/parse-env.js';
|
|
10
|
+
import { convertBigintsToStrings } from './bigint-utils.js';
|
|
10
11
|
import { GoogleCloudLoggerConfig } from './gcloud-logger-config.js';
|
|
11
12
|
import { getLogLevelFromFilters, parseLogLevelEnvVar } from './log-filters.js';
|
|
12
13
|
import type { LogLevel } from './log-levels.js';
|
|
@@ -20,6 +21,8 @@ export type LoggerBindings = {
|
|
|
20
21
|
instanceId?: string;
|
|
21
22
|
};
|
|
22
23
|
|
|
24
|
+
const MAX_MODULE_NAME_LENGTH = 256;
|
|
25
|
+
|
|
23
26
|
// Allow global hooks for providing default bindings.
|
|
24
27
|
// Used by withLoggerBindings in pino-logger-server to propagate bindings via AsyncLocalStorage.
|
|
25
28
|
type LogBindingsHandler = () => LoggerBindings | undefined;
|
|
@@ -47,7 +50,7 @@ function getBindingsFromHandlers(): LoggerBindings | undefined {
|
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
export function createLogger(module: string, bindings?: LoggerBindings): Logger {
|
|
50
|
-
module = module.replace(/^aztec:/, '');
|
|
53
|
+
module = module.slice(0, MAX_MODULE_NAME_LENGTH).replace(/^aztec:/, '');
|
|
51
54
|
|
|
52
55
|
const resolvedBindings = { ...getBindingsFromHandlers(), ...bindings };
|
|
53
56
|
const actor = resolvedBindings?.actor;
|
|
@@ -165,6 +168,9 @@ const pinoOpts: pino.LoggerOptions<keyof typeof customLevels> = {
|
|
|
165
168
|
...redactedPaths.map(p => `opts.${p}`),
|
|
166
169
|
],
|
|
167
170
|
},
|
|
171
|
+
formatters: {
|
|
172
|
+
log: obj => convertBigintsToStrings(obj) as Record<string, unknown>,
|
|
173
|
+
},
|
|
168
174
|
...(useGcloudLogging ? GoogleCloudLoggerConfig : {}),
|
|
169
175
|
};
|
|
170
176
|
|
|
@@ -122,7 +122,7 @@ export abstract class BaseMemoryQueue<T> {
|
|
|
122
122
|
* @param handler - A function that takes an item of type T and returns a Promise<void> after processing the item.
|
|
123
123
|
* @returns A Promise<void> that resolves when the queue is finished processing.
|
|
124
124
|
*/
|
|
125
|
-
public async process(handler: (item: T) => Promise<void>) {
|
|
125
|
+
public async process(handler: (item: T) => Promise<void> | void) {
|
|
126
126
|
try {
|
|
127
127
|
while (true) {
|
|
128
128
|
const item = await this.get();
|
package/src/schemas/api.ts
CHANGED
|
@@ -37,7 +37,10 @@ export type ApiSchema = {
|
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
/** Return whether an API schema defines a valid function schema for a given method name. */
|
|
40
|
-
export function schemaHasMethod
|
|
40
|
+
export function schemaHasMethod<T extends ApiSchema>(
|
|
41
|
+
schema: T,
|
|
42
|
+
methodName: string,
|
|
43
|
+
): methodName is Extract<keyof T, string> {
|
|
41
44
|
return (
|
|
42
45
|
typeof methodName === 'string' &&
|
|
43
46
|
Object.hasOwn(schema, methodName) &&
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { toBigIntBE } from '../bigint-buffer/index.js';
|
|
1
2
|
import type { Tuple } from './types.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -130,6 +131,20 @@ export class BufferReader {
|
|
|
130
131
|
return result;
|
|
131
132
|
}
|
|
132
133
|
|
|
134
|
+
/**
|
|
135
|
+
* Reads a 256-bit signed integer (two's complement) from the buffer at the current index position.
|
|
136
|
+
* Updates the index position by 32 bytes after reading the number.
|
|
137
|
+
*
|
|
138
|
+
* @returns The read 256 bit signed value as a bigint.
|
|
139
|
+
*/
|
|
140
|
+
public readInt256(): bigint {
|
|
141
|
+
this.#rangeCheck(32);
|
|
142
|
+
const unsigned = toBigIntBE(this.buffer.subarray(this.index, this.index + 32));
|
|
143
|
+
this.index += 32;
|
|
144
|
+
const signBit = 1n << 255n;
|
|
145
|
+
return unsigned >= signBit ? unsigned - (1n << 256n) : unsigned;
|
|
146
|
+
}
|
|
147
|
+
|
|
133
148
|
/** Alias for readUInt256 */
|
|
134
149
|
public readBigInt(): bigint {
|
|
135
150
|
return this.readUInt256();
|
|
@@ -269,6 +269,22 @@ export function serializeBigInt(n: bigint, width = 32) {
|
|
|
269
269
|
return toBufferBE(n, width);
|
|
270
270
|
}
|
|
271
271
|
|
|
272
|
+
/**
|
|
273
|
+
* Serialize a signed BigInt value into a Buffer of specified width using two's complement.
|
|
274
|
+
* @param n - The signed BigInt value to be serialized.
|
|
275
|
+
* @param width - The width (in bytes) of the output Buffer, optional with default value 32.
|
|
276
|
+
* @returns A Buffer containing the serialized signed BigInt value in big-endian format.
|
|
277
|
+
*/
|
|
278
|
+
export function serializeSignedBigInt(n: bigint, width = 32) {
|
|
279
|
+
const widthBits = BigInt(width * 8);
|
|
280
|
+
const max = 1n << (widthBits - 1n);
|
|
281
|
+
if (n < -max || n >= max) {
|
|
282
|
+
throw new Error(`Signed BigInt ${n.toString()} does not fit into ${width} bytes`);
|
|
283
|
+
}
|
|
284
|
+
const unsigned = n < 0n ? (1n << widthBits) + n : n;
|
|
285
|
+
return toBufferBE(unsigned, width);
|
|
286
|
+
}
|
|
287
|
+
|
|
272
288
|
/**
|
|
273
289
|
* Deserialize a big integer from a buffer, given an offset and width.
|
|
274
290
|
* Reads the specified number of bytes from the buffer starting at the offset, converts it to a big integer, and returns the deserialized result along with the number of bytes read (advanced).
|
|
@@ -282,6 +298,22 @@ export function deserializeBigInt(buf: Buffer, offset = 0, width = 32) {
|
|
|
282
298
|
return { elem: toBigIntBE(buf.subarray(offset, offset + width)), adv: width };
|
|
283
299
|
}
|
|
284
300
|
|
|
301
|
+
/**
|
|
302
|
+
* Deserialize a signed BigInt from a buffer (two's complement).
|
|
303
|
+
* @param buf - The buffer containing the signed big integer to be deserialized.
|
|
304
|
+
* @param offset - The position in the buffer where the integer starts. Defaults to 0.
|
|
305
|
+
* @param width - The number of bytes to read from the buffer for the integer. Defaults to 32.
|
|
306
|
+
* @returns An object containing the deserialized signed bigint value ('elem') and bytes advanced ('adv').
|
|
307
|
+
*/
|
|
308
|
+
export function deserializeSignedBigInt(buf: Buffer, offset = 0, width = 32) {
|
|
309
|
+
const { elem, adv } = deserializeBigInt(buf, offset, width);
|
|
310
|
+
const widthBits = BigInt(width * 8);
|
|
311
|
+
const signBit = 1n << (widthBits - 1n);
|
|
312
|
+
const fullRange = 1n << widthBits;
|
|
313
|
+
const signed = elem >= signBit ? elem - fullRange : elem;
|
|
314
|
+
return { elem: signed, adv };
|
|
315
|
+
}
|
|
316
|
+
|
|
285
317
|
/**
|
|
286
318
|
* Serializes a Date object into a Buffer containing its timestamp as a big integer value.
|
|
287
319
|
* The resulting Buffer has a fixed width of 8 bytes, representing a 64-bit big-endian integer.
|
package/src/sleep/index.ts
CHANGED
|
@@ -22,6 +22,7 @@ import { InterruptError } from '../error/index.js';
|
|
|
22
22
|
*/
|
|
23
23
|
export class InterruptibleSleep {
|
|
24
24
|
private interrupts: Array<(shouldThrow: boolean) => void> = [];
|
|
25
|
+
private timeoutIds: NodeJS.Timeout[] = [];
|
|
25
26
|
|
|
26
27
|
/**
|
|
27
28
|
* Sleep for a specified amount of time in milliseconds.
|
|
@@ -38,9 +39,15 @@ export class InterruptibleSleep {
|
|
|
38
39
|
this.interrupts.push(resolve);
|
|
39
40
|
});
|
|
40
41
|
|
|
41
|
-
|
|
42
|
+
let timeoutId: NodeJS.Timeout;
|
|
43
|
+
const timeoutPromise = new Promise<boolean>(resolve => {
|
|
44
|
+
timeoutId = setTimeout(() => resolve(false), ms);
|
|
45
|
+
this.timeoutIds.push(timeoutId);
|
|
46
|
+
});
|
|
42
47
|
const shouldThrow = await Promise.race([interruptPromise, timeoutPromise]);
|
|
43
48
|
|
|
49
|
+
clearTimeout(timeoutId!);
|
|
50
|
+
this.timeoutIds = this.timeoutIds.filter(id => id !== timeoutId);
|
|
44
51
|
this.interrupts = this.interrupts.filter(res => res !== interruptResolve);
|
|
45
52
|
|
|
46
53
|
if (shouldThrow) {
|
|
@@ -58,6 +65,8 @@ export class InterruptibleSleep {
|
|
|
58
65
|
public interrupt(sleepShouldThrow = false): void {
|
|
59
66
|
this.interrupts.forEach(resolve => resolve(sleepShouldThrow));
|
|
60
67
|
this.interrupts = [];
|
|
68
|
+
this.timeoutIds.forEach(id => clearTimeout(id));
|
|
69
|
+
this.timeoutIds = [];
|
|
61
70
|
}
|
|
62
71
|
}
|
|
63
72
|
|
package/src/timer/date.ts
CHANGED
|
@@ -32,6 +32,12 @@ export class TestDateProvider extends DateProvider {
|
|
|
32
32
|
this.logger.warn(`Time set to ${new Date(timeMs).toISOString()}`, { offset: this.offset, timeMs });
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
/** Resets the time back to real time (offset = 0). */
|
|
36
|
+
public reset() {
|
|
37
|
+
this.offset = 0;
|
|
38
|
+
this.logger.warn('Time reset to real time');
|
|
39
|
+
}
|
|
40
|
+
|
|
35
41
|
/** Advances the time by the given number of seconds. */
|
|
36
42
|
public advanceTime(seconds: number) {
|
|
37
43
|
this.offset += seconds * 1000;
|
|
@@ -91,7 +91,7 @@ export class TransportClient<Payload> extends EventEmitter {
|
|
|
91
91
|
}
|
|
92
92
|
const msgId = this.msgId++;
|
|
93
93
|
const msg = { msgId, payload };
|
|
94
|
-
log.
|
|
94
|
+
log.trace(format(`->`, msg));
|
|
95
95
|
return new Promise<any>((resolve, reject) => {
|
|
96
96
|
this.pendingRequests.push({ resolve, reject, msgId });
|
|
97
97
|
this.socket!.send(msg, transfer).catch(reject);
|
|
@@ -111,7 +111,7 @@ export class TransportClient<Payload> extends EventEmitter {
|
|
|
111
111
|
this.close();
|
|
112
112
|
return;
|
|
113
113
|
}
|
|
114
|
-
log.
|
|
114
|
+
log.trace(format(`<-`, msg));
|
|
115
115
|
if (isEventMessage(msg)) {
|
|
116
116
|
this.emit('event_msg', msg.payload);
|
|
117
117
|
return;
|
|
@@ -40,7 +40,11 @@ export class IndexedMerkleTreeCalculator<T extends IndexedTreeLeafPreimage, N ex
|
|
|
40
40
|
}
|
|
41
41
|
const sorted = values
|
|
42
42
|
.map((v, i) => ({ value: v, index: i }))
|
|
43
|
-
.sort((a, b)
|
|
43
|
+
.sort((a, b): -1 | 0 | 1 => {
|
|
44
|
+
const aBigInt = toBigIntBE(a.value);
|
|
45
|
+
const bBigInt = toBigIntBE(b.value);
|
|
46
|
+
return aBigInt < bBigInt ? 1 : aBigInt > bBigInt ? -1 : 0;
|
|
47
|
+
});
|
|
44
48
|
const indexedLeaves = sorted.map((item, i) => ({
|
|
45
49
|
leaf: this.factory.fromBuffer(
|
|
46
50
|
Buffer.concat([
|