@aztec/foundation 0.0.1-commit.9b94fc1 → 0.0.1-commit.b468ad8
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/index.d.ts +2 -1
- package/dest/array/index.d.ts.map +1 -1
- package/dest/array/index.js +1 -0
- package/dest/array/sorted_array.d.ts +10 -0
- package/dest/array/sorted_array.d.ts.map +1 -0
- package/dest/array/sorted_array.js +106 -0
- package/dest/branded-types/block_number.d.ts +56 -0
- package/dest/branded-types/block_number.d.ts.map +1 -0
- package/dest/branded-types/block_number.js +78 -0
- package/dest/branded-types/checkpoint_number.d.ts +54 -0
- package/dest/branded-types/checkpoint_number.d.ts.map +1 -0
- package/dest/branded-types/checkpoint_number.js +77 -0
- package/dest/branded-types/index.d.ts +4 -1
- package/dest/branded-types/index.d.ts.map +1 -1
- package/dest/branded-types/index.js +3 -0
- package/dest/branded-types/index_within_checkpoint.d.ts +42 -0
- package/dest/branded-types/index_within_checkpoint.d.ts.map +1 -0
- package/dest/branded-types/index_within_checkpoint.js +59 -0
- package/dest/buffer/buffer16.d.ts +5 -2
- package/dest/buffer/buffer16.d.ts.map +1 -1
- package/dest/buffer/buffer16.js +4 -2
- package/dest/buffer/buffer32.d.ts +5 -2
- package/dest/buffer/buffer32.d.ts.map +1 -1
- package/dest/buffer/buffer32.js +4 -2
- package/dest/collection/array.d.ts +12 -1
- package/dest/collection/array.d.ts.map +1 -1
- package/dest/collection/array.js +51 -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 +15 -4
- package/dest/config/index.d.ts.map +1 -1
- package/dest/config/index.js +32 -9
- package/dest/config/network_config.d.ts +13 -1
- package/dest/config/network_config.d.ts.map +1 -1
- package/dest/config/network_config.js +3 -1
- package/dest/config/parse-env.d.ts +3 -0
- package/dest/config/parse-env.d.ts.map +1 -0
- package/dest/config/parse-env.js +7 -0
- package/dest/config/secret_value.js +3 -1
- package/dest/crypto/bls/bn254_keystore.js +1 -1
- package/dest/crypto/bls/index.js +1 -1
- package/dest/crypto/bn254/index.d.ts +35 -51
- package/dest/crypto/bn254/index.d.ts.map +1 -1
- package/dest/crypto/bn254/index.js +51 -140
- package/dest/crypto/ecdsa/signature.d.ts +11 -2
- package/dest/crypto/ecdsa/signature.d.ts.map +1 -1
- package/dest/crypto/ecdsa/signature.js +2 -2
- package/dest/crypto/grumpkin/index.d.ts +4 -2
- package/dest/crypto/grumpkin/index.d.ts.map +1 -1
- package/dest/crypto/grumpkin/index.js +2 -1
- package/dest/crypto/keys/index.d.ts +2 -2
- package/dest/crypto/keys/index.d.ts.map +1 -1
- package/dest/crypto/keys/index.js +1 -1
- package/dest/crypto/pedersen/pedersen.wasm.d.ts +2 -2
- package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -1
- package/dest/crypto/pedersen/pedersen.wasm.js +1 -1
- package/dest/crypto/poseidon/index.d.ts +2 -3
- package/dest/crypto/poseidon/index.d.ts.map +1 -1
- package/dest/crypto/poseidon/index.js +1 -10
- package/dest/crypto/random/randomness_singleton.d.ts +4 -3
- package/dest/crypto/random/randomness_singleton.d.ts.map +1 -1
- package/dest/crypto/random/randomness_singleton.js +5 -5
- package/dest/crypto/schnorr/index.d.ts +3 -2
- package/dest/crypto/schnorr/index.d.ts.map +1 -1
- package/dest/crypto/schnorr/index.js +1 -1
- package/dest/crypto/schnorr/signature.d.ts +2 -2
- package/dest/crypto/schnorr/signature.d.ts.map +1 -1
- package/dest/crypto/schnorr/signature.js +2 -2
- package/dest/crypto/sha256/index.d.ts +2 -2
- package/dest/crypto/sha256/index.d.ts.map +1 -1
- package/dest/crypto/sha256/index.js +1 -1
- package/dest/crypto/sha512/index.d.ts +2 -2
- package/dest/crypto/sha512/index.d.ts.map +1 -1
- package/dest/crypto/sha512/index.js +1 -1
- package/dest/crypto/signature/index.d.ts +2 -2
- package/dest/crypto/signature/index.d.ts.map +1 -1
- package/dest/crypto/sync/pedersen/index.d.ts +2 -2
- package/dest/crypto/sync/pedersen/index.d.ts.map +1 -1
- package/dest/crypto/sync/pedersen/index.js +1 -1
- package/dest/crypto/sync/poseidon/index.d.ts +2 -3
- package/dest/crypto/sync/poseidon/index.d.ts.map +1 -1
- package/dest/crypto/sync/poseidon/index.js +1 -9
- package/dest/{fields/bls12_fields.d.ts → curves/bls12/field.d.ts} +3 -3
- package/dest/curves/bls12/field.d.ts.map +1 -0
- package/dest/{fields/bls12_fields.js → curves/bls12/field.js} +12 -9
- package/dest/curves/bls12/index.d.ts +3 -0
- package/dest/curves/bls12/index.d.ts.map +1 -0
- package/dest/curves/bls12/index.js +2 -0
- package/dest/{fields/bls12_point.d.ts → curves/bls12/point.d.ts} +13 -4
- package/dest/curves/bls12/point.d.ts.map +1 -0
- package/dest/{fields/bls12_point.js → curves/bls12/point.js} +10 -8
- package/dest/{fields/fields.d.ts → curves/bn254/field.d.ts} +7 -15
- package/dest/curves/bn254/field.d.ts.map +1 -0
- package/dest/{fields/fields.js → curves/bn254/field.js} +35 -49
- package/dest/curves/bn254/index.d.ts +3 -0
- package/dest/curves/bn254/index.d.ts.map +1 -0
- package/dest/curves/bn254/index.js +2 -0
- package/dest/curves/bn254/point.d.ts +54 -0
- package/dest/curves/bn254/point.d.ts.map +1 -0
- package/dest/curves/bn254/point.js +143 -0
- package/dest/curves/grumpkin/index.d.ts +10 -0
- package/dest/curves/grumpkin/index.d.ts.map +1 -0
- package/dest/curves/grumpkin/index.js +3 -0
- package/dest/{fields → curves/grumpkin}/point.d.ts +13 -4
- package/dest/curves/grumpkin/point.d.ts.map +1 -0
- package/dest/{fields → curves/grumpkin}/point.js +7 -7
- package/dest/error/index.d.ts +4 -4
- package/dest/error/index.d.ts.map +1 -1
- package/dest/eth-address/index.d.ts +3 -2
- package/dest/eth-address/index.d.ts.map +1 -1
- package/dest/eth-address/index.js +10 -3
- package/dest/eth-signature/eth_signature.d.ts +4 -1
- package/dest/eth-signature/eth_signature.d.ts.map +1 -1
- package/dest/iterator/filter.d.ts +3 -0
- package/dest/iterator/filter.d.ts.map +1 -0
- package/dest/iterator/filter.js +7 -0
- package/dest/iterator/index.d.ts +2 -0
- package/dest/iterator/index.d.ts.map +1 -0
- package/dest/iterator/index.js +1 -0
- package/dest/jest/setup.js +4 -1
- package/dest/json-rpc/client/undici.d.ts +1 -1
- package/dest/json-rpc/client/undici.d.ts.map +1 -1
- package/dest/json-rpc/client/undici.js +21 -4
- package/dest/json-rpc/fixtures/class_a.d.ts +3 -3
- package/dest/json-rpc/fixtures/class_a.d.ts.map +1 -1
- package/dest/json-rpc/fixtures/class_b.d.ts +3 -3
- package/dest/json-rpc/fixtures/class_b.d.ts.map +1 -1
- package/dest/json-rpc/server/safe_json_rpc_server.js +1 -1
- package/dest/log/libp2p_logger.d.ts +5 -2
- package/dest/log/libp2p_logger.d.ts.map +1 -1
- package/dest/log/libp2p_logger.js +14 -4
- package/dest/log/pino-logger-server.d.ts +9 -0
- package/dest/log/pino-logger-server.d.ts.map +1 -0
- package/dest/log/pino-logger-server.js +18 -0
- package/dest/log/pino-logger.d.ts +37 -8
- package/dest/log/pino-logger.d.ts.map +1 -1
- package/dest/log/pino-logger.js +116 -27
- package/dest/queue/semaphore.d.ts +5 -1
- package/dest/queue/semaphore.d.ts.map +1 -1
- package/dest/retry/index.d.ts +11 -1
- package/dest/retry/index.d.ts.map +1 -1
- package/dest/retry/index.js +11 -0
- package/dest/schemas/schemas.d.ts +7 -3
- package/dest/schemas/schemas.d.ts.map +1 -1
- package/dest/schemas/schemas.js +9 -3
- package/dest/schemas/types.d.ts +31 -1
- package/dest/schemas/types.d.ts.map +1 -1
- package/dest/schemas/types.js +25 -1
- package/dest/serialize/buffer_reader.d.ts +7 -4
- package/dest/serialize/buffer_reader.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.js +13 -4
- package/dest/serialize/field_reader.d.ts +2 -2
- package/dest/serialize/field_reader.d.ts.map +1 -1
- package/dest/serialize/field_reader.js +1 -1
- package/dest/serialize/free_funcs.d.ts +2 -2
- package/dest/serialize/free_funcs.d.ts.map +1 -1
- package/dest/serialize/free_funcs.js +1 -1
- package/dest/serialize/serialize.d.ts +2 -2
- package/dest/serialize/serialize.d.ts.map +1 -1
- package/dest/serialize/serialize.js +1 -1
- package/dest/sleep/index.d.ts +3 -1
- package/dest/sleep/index.d.ts.map +1 -1
- package/dest/sleep/index.js +4 -0
- package/dest/timer/date.d.ts +2 -1
- package/dest/timer/date.d.ts.map +1 -1
- package/dest/timer/date.js +3 -0
- package/dest/trees/balanced_merkle_tree_root.d.ts +17 -0
- package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -0
- package/dest/trees/{balanced_merkle_tree.js → balanced_merkle_tree_root.js} +2 -15
- package/dest/trees/hasher.d.ts +3 -1
- package/dest/trees/hasher.d.ts.map +1 -1
- package/dest/trees/hasher.js +10 -5
- package/dest/trees/index.d.ts +4 -4
- package/dest/trees/index.d.ts.map +1 -1
- package/dest/trees/index.js +3 -3
- package/dest/trees/indexed_merkle_tree.js +1 -1
- package/dest/trees/membership_witness.d.ts +12 -2
- package/dest/trees/membership_witness.d.ts.map +1 -1
- package/dest/trees/membership_witness.js +10 -1
- package/dest/trees/merkle_tree_calculator.d.ts +1 -1
- package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/merkle_tree_calculator.js +2 -2
- package/dest/trees/sibling_path.d.ts +3 -2
- package/dest/trees/sibling_path.d.ts.map +1 -1
- package/dest/trees/sibling_path.js +3 -3
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +2 -3
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree_calculator.js +1 -5
- package/dest/trees/{unbalanced_merkle_tree.d.ts → unbalanced_merkle_tree_root.d.ts} +3 -5
- package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -0
- package/dest/trees/{unbalanced_merkle_tree.js → unbalanced_merkle_tree_root.js} +11 -49
- package/dest/trees/unbalanced_tree_store.d.ts +5 -1
- package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
- package/dest/trees/unbalanced_tree_store.js +49 -1
- package/dest/types/index.d.ts +6 -2
- package/dest/types/index.d.ts.map +1 -1
- package/dest/types/index.js +6 -0
- package/package.json +28 -9
- package/src/array/index.ts +1 -0
- package/src/array/sorted_array.ts +133 -0
- package/src/branded-types/block_number.ts +117 -0
- package/src/branded-types/checkpoint_number.ts +115 -0
- package/src/branded-types/index.ts +3 -0
- package/src/branded-types/index_within_checkpoint.ts +88 -0
- package/src/buffer/buffer16.ts +2 -2
- package/src/buffer/buffer32.ts +2 -2
- package/src/collection/array.ts +52 -0
- package/src/config/env_var.ts +39 -12
- package/src/config/index.ts +39 -6
- package/src/config/network_config.ts +2 -0
- package/src/config/parse-env.ts +4 -0
- package/src/crypto/bls/bn254_keystore.ts +1 -1
- package/src/crypto/bls/index.ts +1 -1
- package/src/crypto/bn254/index.ts +50 -159
- package/src/crypto/ecdsa/signature.ts +2 -2
- package/src/crypto/grumpkin/index.ts +3 -1
- package/src/crypto/keys/index.ts +1 -1
- package/src/crypto/pedersen/pedersen.wasm.ts +1 -1
- package/src/crypto/poseidon/index.ts +1 -11
- package/src/crypto/random/randomness_singleton.ts +6 -4
- package/src/crypto/schnorr/index.ts +2 -1
- package/src/crypto/schnorr/signature.ts +2 -2
- package/src/crypto/sha256/index.ts +1 -1
- package/src/crypto/sha512/index.ts +1 -1
- package/src/crypto/signature/index.ts +1 -1
- package/src/crypto/sync/pedersen/index.ts +1 -1
- package/src/crypto/sync/poseidon/index.ts +1 -10
- package/src/{fields/bls12_fields.ts → curves/bls12/field.ts} +6 -6
- package/src/curves/bls12/index.ts +2 -0
- package/src/{fields/bls12_point.ts → curves/bls12/point.ts} +7 -7
- package/src/{fields/fields.ts → curves/bn254/field.ts} +33 -56
- package/src/curves/bn254/index.ts +2 -0
- package/src/curves/bn254/point.ts +170 -0
- package/src/curves/grumpkin/index.ts +11 -0
- package/src/{fields → curves/grumpkin}/point.ts +7 -7
- package/src/eth-address/index.ts +8 -2
- package/src/iterator/filter.ts +11 -0
- package/src/iterator/index.ts +1 -0
- package/src/jest/setup.mjs +4 -1
- package/src/json-rpc/client/undici.ts +21 -3
- package/src/json-rpc/server/safe_json_rpc_server.ts +1 -1
- package/src/log/libp2p_logger.ts +12 -5
- package/src/log/pino-logger-server.ts +25 -0
- package/src/log/pino-logger.ts +138 -37
- package/src/queue/semaphore.ts +5 -0
- package/src/retry/index.ts +18 -0
- package/src/schemas/schemas.ts +14 -3
- package/src/schemas/types.ts +33 -0
- package/src/serialize/buffer_reader.ts +21 -9
- package/src/serialize/field_reader.ts +1 -1
- package/src/serialize/free_funcs.ts +1 -1
- package/src/serialize/serialize.ts +1 -1
- package/src/sleep/index.ts +6 -0
- package/src/timer/date.ts +4 -0
- package/src/trees/{balanced_merkle_tree.ts → balanced_merkle_tree_root.ts} +2 -16
- package/src/trees/hasher.ts +9 -0
- package/src/trees/index.ts +3 -3
- package/src/trees/indexed_merkle_tree.ts +1 -1
- package/src/trees/membership_witness.ts +9 -1
- package/src/trees/merkle_tree_calculator.ts +2 -2
- package/src/trees/sibling_path.ts +3 -3
- package/src/trees/unbalanced_merkle_tree_calculator.ts +1 -12
- package/src/trees/{unbalanced_merkle_tree.ts → unbalanced_merkle_tree_root.ts} +17 -61
- package/src/trees/unbalanced_tree_store.ts +57 -2
- package/src/types/index.ts +11 -1
- package/dest/crypto/bn254/bn254_utils.d.ts +0 -39
- package/dest/crypto/bn254/bn254_utils.d.ts.map +0 -1
- package/dest/crypto/bn254/bn254_utils.js +0 -56
- package/dest/crypto/index.d.ts +0 -17
- package/dest/crypto/index.d.ts.map +0 -1
- package/dest/crypto/index.js +0 -16
- package/dest/fields/bls12_fields.d.ts.map +0 -1
- package/dest/fields/bls12_point.d.ts.map +0 -1
- package/dest/fields/coordinate.d.ts +0 -63
- package/dest/fields/coordinate.d.ts.map +0 -1
- package/dest/fields/coordinate.js +0 -96
- package/dest/fields/fields.d.ts.map +0 -1
- package/dest/fields/index.d.ts +0 -6
- package/dest/fields/index.d.ts.map +0 -1
- package/dest/fields/index.js +0 -5
- package/dest/fields/point.d.ts.map +0 -1
- package/dest/index.d.ts +0 -31
- package/dest/index.d.ts.map +0 -1
- package/dest/index.js +0 -31
- package/dest/json-rpc/server/telemetry.d.ts +0 -2
- package/dest/json-rpc/server/telemetry.d.ts.map +0 -1
- package/dest/json-rpc/server/telemetry.js +0 -0
- package/dest/trees/balanced_merkle_tree.d.ts +0 -22
- package/dest/trees/balanced_merkle_tree.d.ts.map +0 -1
- package/dest/trees/unbalanced_merkle_tree.d.ts.map +0 -1
- package/src/crypto/bn254/bn254_utils.ts +0 -64
- package/src/crypto/index.ts +0 -16
- package/src/fields/coordinate.ts +0 -104
- package/src/fields/index.ts +0 -5
- package/src/index.ts +0 -31
- package/src/json-rpc/server/telemetry.ts +0 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BN254_G1_GENERATOR,
|
|
3
|
+
BN254_G2_GENERATOR,
|
|
4
|
+
BarretenbergSync,
|
|
5
|
+
type Bn254G1Point as BbApiBn254G1Point,
|
|
6
|
+
type Bn254G2Point as BbApiBn254G2Point,
|
|
7
|
+
} from '@aztec/bb.js';
|
|
8
|
+
|
|
9
|
+
import { Fq, Fr } from './field.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* BN254 G1 point using foundation field classes.
|
|
13
|
+
* Represents a point on the BN254 elliptic curve in affine coordinates.
|
|
14
|
+
*/
|
|
15
|
+
export class Bn254G1Point {
|
|
16
|
+
constructor(
|
|
17
|
+
public readonly x: Fq,
|
|
18
|
+
public readonly y: Fq,
|
|
19
|
+
) {}
|
|
20
|
+
|
|
21
|
+
private toBbApiPoint(): BbApiBn254G1Point {
|
|
22
|
+
return {
|
|
23
|
+
x: this.x.toBuffer(),
|
|
24
|
+
y: this.y.toBuffer(),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private static fromBbApiPoint(point: BbApiBn254G1Point): Bn254G1Point {
|
|
29
|
+
return new Bn254G1Point(Fq.fromBuffer(Buffer.from(point.x)), Fq.fromBuffer(Buffer.from(point.y)));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async isOnCurve(): Promise<boolean> {
|
|
33
|
+
await BarretenbergSync.initSingleton();
|
|
34
|
+
const api = BarretenbergSync.getSingleton();
|
|
35
|
+
|
|
36
|
+
const apiPoint = this.toBbApiPoint();
|
|
37
|
+
const response = api.bn254G1IsOnCurve({ point: apiPoint });
|
|
38
|
+
return response.isOnCurve;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Get the generator point for BN254 G1, or perform scalar multiplication.
|
|
43
|
+
* When called without arguments, returns the base generator point.
|
|
44
|
+
* When called with a scalar, returns scalar * generator (useful for public key derivation).
|
|
45
|
+
*/
|
|
46
|
+
static async generator(scalar?: Fr): Promise<Bn254G1Point> {
|
|
47
|
+
if (!scalar) {
|
|
48
|
+
return new Bn254G1Point(
|
|
49
|
+
Fq.fromBuffer(Buffer.from(BN254_G1_GENERATOR.x)),
|
|
50
|
+
Fq.fromBuffer(Buffer.from(BN254_G1_GENERATOR.y)),
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
await BarretenbergSync.initSingleton();
|
|
55
|
+
const api = BarretenbergSync.getSingleton();
|
|
56
|
+
|
|
57
|
+
const response = api.bn254G1Mul({
|
|
58
|
+
point: BN254_G1_GENERATOR,
|
|
59
|
+
scalar: scalar.toBuffer(),
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return Bn254G1Point.fromBbApiPoint(response.point);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Decompress a BN254 G1 point from compressed form (32 bytes).
|
|
67
|
+
* The compressed format encodes the x-coordinate and the sign bit of the y-coordinate
|
|
68
|
+
* in the most significant bit.
|
|
69
|
+
*/
|
|
70
|
+
static async fromCompressed(compressed: Buffer): Promise<Bn254G1Point> {
|
|
71
|
+
if (compressed.length !== 32) {
|
|
72
|
+
throw new Error('Invalid compressed point length');
|
|
73
|
+
}
|
|
74
|
+
await BarretenbergSync.initSingleton();
|
|
75
|
+
const api = BarretenbergSync.getSingleton();
|
|
76
|
+
|
|
77
|
+
const response = api.bn254G1FromCompressed({
|
|
78
|
+
compressed: new Uint8Array(compressed),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return Bn254G1Point.fromBbApiPoint(response.point);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Compress this BN254 G1 point to 32 bytes.
|
|
86
|
+
* The compressed format encodes the x-coordinate and the sign bit of the y-coordinate
|
|
87
|
+
* in the most significant bit (bit 255).
|
|
88
|
+
*/
|
|
89
|
+
compress(): Buffer {
|
|
90
|
+
const xBytes = this.x.toBuffer();
|
|
91
|
+
// Get the least significant bit of y to determine the sign
|
|
92
|
+
const yLsb = this.y.toBigInt() & 1n;
|
|
93
|
+
// If y is odd, set the most significant bit (bit 255) of the output
|
|
94
|
+
if (yLsb === 1n) {
|
|
95
|
+
xBytes[0] |= 0x80;
|
|
96
|
+
}
|
|
97
|
+
return xBytes;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
equals(other: Bn254G1Point): boolean {
|
|
101
|
+
return this.x.equals(other.x) && this.y.equals(other.y);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
toString(): string {
|
|
105
|
+
return `Bn254G1Point(x: ${this.x.toString()}, y: ${this.y.toString()})`;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* BN254 G2 point using foundation field classes.
|
|
111
|
+
* Represents a point on the BN254 G2 curve (twist curve) in affine coordinates.
|
|
112
|
+
* G2 points use extension field coordinates (Fq2).
|
|
113
|
+
*/
|
|
114
|
+
export class Bn254G2Point {
|
|
115
|
+
constructor(
|
|
116
|
+
public readonly x: [Fq, Fq],
|
|
117
|
+
public readonly y: [Fq, Fq],
|
|
118
|
+
) {}
|
|
119
|
+
|
|
120
|
+
private toBbApiPoint(): BbApiBn254G2Point {
|
|
121
|
+
return {
|
|
122
|
+
x: [this.x[0].toBuffer(), this.x[1].toBuffer()],
|
|
123
|
+
y: [this.y[0].toBuffer(), this.y[1].toBuffer()],
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private static fromBbApiPoint(point: BbApiBn254G2Point): Bn254G2Point {
|
|
128
|
+
return new Bn254G2Point(
|
|
129
|
+
[Fq.fromBuffer(Buffer.from(point.x[0])), Fq.fromBuffer(Buffer.from(point.x[1]))],
|
|
130
|
+
[Fq.fromBuffer(Buffer.from(point.y[0])), Fq.fromBuffer(Buffer.from(point.y[1]))],
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Get the generator point for BN254 G2, or perform scalar multiplication.
|
|
136
|
+
* When called without arguments, returns the base generator point.
|
|
137
|
+
* When called with a scalar, returns scalar * generator.
|
|
138
|
+
*/
|
|
139
|
+
static async generator(scalar?: Fr): Promise<Bn254G2Point> {
|
|
140
|
+
if (!scalar) {
|
|
141
|
+
return new Bn254G2Point(
|
|
142
|
+
[Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.x[0])), Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.x[1]))],
|
|
143
|
+
[Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.y[0])), Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.y[1]))],
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
await BarretenbergSync.initSingleton();
|
|
148
|
+
const api = BarretenbergSync.getSingleton();
|
|
149
|
+
|
|
150
|
+
const response = api.bn254G2Mul({
|
|
151
|
+
point: BN254_G2_GENERATOR as BbApiBn254G2Point,
|
|
152
|
+
scalar: scalar.toBuffer(),
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
return Bn254G2Point.fromBbApiPoint(response.point);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
equals(other: Bn254G2Point): boolean {
|
|
159
|
+
return (
|
|
160
|
+
this.x[0].equals(other.x[0]) &&
|
|
161
|
+
this.x[1].equals(other.x[1]) &&
|
|
162
|
+
this.y[0].equals(other.y[0]) &&
|
|
163
|
+
this.y[1].equals(other.y[1])
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
toString(): string {
|
|
168
|
+
return `Bn254G2Point(x: (${this.x[0].toString()}, ${this.x[1].toString()}), y: (${this.y[0].toString()}, ${this.y[1].toString()}))`;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Fq } from '../bn254/field.js';
|
|
2
|
+
|
|
3
|
+
export * from './point.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* GrumpkinScalar is an Fq.
|
|
7
|
+
* @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
|
|
8
|
+
* the Aztec Protocol Specs.
|
|
9
|
+
*/
|
|
10
|
+
export type GrumpkinScalar = Fq;
|
|
11
|
+
export const GrumpkinScalar = Fq;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { toBigIntBE } from '
|
|
2
|
-
import { poseidon2Hash } from '
|
|
3
|
-
import { randomBoolean } from '
|
|
4
|
-
import { hexSchemaFor } from '
|
|
5
|
-
import { BufferReader, FieldReader, serializeToBuffer } from '
|
|
6
|
-
import { bufferToHex, hexToBuffer } from '
|
|
7
|
-
import { Fr } from '
|
|
1
|
+
import { toBigIntBE } from '../../bigint-buffer/index.js';
|
|
2
|
+
import { poseidon2Hash } from '../../crypto/poseidon/index.js';
|
|
3
|
+
import { randomBoolean } from '../../crypto/random/index.js';
|
|
4
|
+
import { hexSchemaFor } from '../../schemas/utils.js';
|
|
5
|
+
import { BufferReader, FieldReader, serializeToBuffer } from '../../serialize/index.js';
|
|
6
|
+
import { bufferToHex, hexToBuffer } from '../../string/index.js';
|
|
7
|
+
import { Fr } from '../bn254/field.js';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Represents a Point on an elliptic curve with x and y coordinates.
|
package/src/eth-address/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { inspect } from 'util';
|
|
|
2
2
|
|
|
3
3
|
import { keccak256String } from '../crypto/keccak/index.js';
|
|
4
4
|
import { randomBytes } from '../crypto/random/index.js';
|
|
5
|
-
import { Fr } from '../
|
|
5
|
+
import { Fr } from '../curves/bn254/index.js';
|
|
6
6
|
import { hexSchemaFor } from '../schemas/utils.js';
|
|
7
7
|
import { BufferReader, FieldReader } from '../serialize/index.js';
|
|
8
8
|
import { TypeRegistry } from '../serialize/type_registry.js';
|
|
@@ -249,7 +249,7 @@ export class EthAddress {
|
|
|
249
249
|
/** Converts a number into an address. Useful for testing. */
|
|
250
250
|
static fromNumber(num: bigint | number): EthAddress {
|
|
251
251
|
const buffer = Buffer.alloc(EthAddress.SIZE_IN_BYTES);
|
|
252
|
-
buffer.writeBigUInt64BE(BigInt(num),
|
|
252
|
+
buffer.writeBigUInt64BE(BigInt(num), EthAddress.SIZE_IN_BYTES - 8);
|
|
253
253
|
return new EthAddress(buffer);
|
|
254
254
|
}
|
|
255
255
|
|
|
@@ -261,6 +261,12 @@ export class EthAddress {
|
|
|
261
261
|
// Serialization from hex string.
|
|
262
262
|
return hexSchemaFor(EthAddress, EthAddress.isAddress);
|
|
263
263
|
}
|
|
264
|
+
|
|
265
|
+
static areEqual(a: EthAddress | string, b: EthAddress | string) {
|
|
266
|
+
const addrA = typeof a === 'string' ? EthAddress.fromString(a) : a;
|
|
267
|
+
const addrB = typeof b === 'string' ? EthAddress.fromString(b) : b;
|
|
268
|
+
return addrA.equals(addrB);
|
|
269
|
+
}
|
|
264
270
|
}
|
|
265
271
|
|
|
266
272
|
// For deserializing JSON.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Wraps an async iterable iterator such that it filters values based on a predicate. */
|
|
2
|
+
export async function* filter<T>(
|
|
3
|
+
iterator: AsyncIterableIterator<T>,
|
|
4
|
+
predicate: (item: T) => boolean | Promise<boolean>,
|
|
5
|
+
): AsyncIterableIterator<T> {
|
|
6
|
+
for await (const item of iterator) {
|
|
7
|
+
if (await predicate(item)) {
|
|
8
|
+
yield item;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { filter } from './filter.js';
|
package/src/jest/setup.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parseBooleanEnv } from '@aztec/foundation/config';
|
|
1
2
|
import { overwriteLoggingStream, pinoPrettyOpts } from '@aztec/foundation/log';
|
|
2
3
|
|
|
3
4
|
import pretty from 'pino-pretty';
|
|
@@ -6,4 +7,6 @@ import pretty from 'pino-pretty';
|
|
|
6
7
|
// file so we don't mess up with dependencies in non-testing environments,
|
|
7
8
|
// since pino-pretty messes up with browser bundles.
|
|
8
9
|
// See also https://www.npmjs.com/package/pino-pretty?activeTab=readme#user-content-usage-with-jest
|
|
9
|
-
|
|
10
|
+
if (!parseBooleanEnv(process.env.LOG_JSON)) {
|
|
11
|
+
overwriteLoggingStream(pretty(pinoPrettyOpts));
|
|
12
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { promisify } from 'node:util';
|
|
2
|
+
import { gunzip as gunzipCb, gzip as gzipCb } from 'node:zlib';
|
|
1
3
|
import { Agent, type Dispatcher } from 'undici';
|
|
2
4
|
|
|
3
5
|
import { createLogger } from '../../log/pino-logger.js';
|
|
@@ -5,8 +7,14 @@ import { NoRetryError } from '../../retry/index.js';
|
|
|
5
7
|
import { jsonStringify } from '../convert.js';
|
|
6
8
|
import type { JsonRpcFetch } from './fetch.js';
|
|
7
9
|
|
|
10
|
+
const gzip = promisify(gzipCb);
|
|
11
|
+
const gunzip = promisify(gunzipCb);
|
|
12
|
+
|
|
8
13
|
const log = createLogger('json-rpc:json_rpc_client:undici');
|
|
9
14
|
|
|
15
|
+
/** Minimum request size in bytes to trigger compression. */
|
|
16
|
+
const COMPRESSION_THRESHOLD = 1024;
|
|
17
|
+
|
|
10
18
|
export { Agent };
|
|
11
19
|
|
|
12
20
|
export function makeUndiciFetch(client = new Agent()): JsonRpcFetch {
|
|
@@ -14,14 +22,18 @@ export function makeUndiciFetch(client = new Agent()): JsonRpcFetch {
|
|
|
14
22
|
log.trace(`JsonRpcClient.fetch: ${host}`, { host, body });
|
|
15
23
|
let resp: Dispatcher.ResponseData;
|
|
16
24
|
try {
|
|
25
|
+
const jsonBody = Buffer.from(jsonStringify(body));
|
|
26
|
+
const shouldCompress = jsonBody.length >= COMPRESSION_THRESHOLD;
|
|
17
27
|
resp = await client.request({
|
|
18
28
|
method: 'POST',
|
|
19
29
|
origin: new URL(host),
|
|
20
30
|
path: '/',
|
|
21
|
-
body:
|
|
31
|
+
body: shouldCompress ? await gzip(jsonBody) : jsonBody,
|
|
22
32
|
headers: {
|
|
23
33
|
...extraHeaders,
|
|
24
34
|
'content-type': 'application/json',
|
|
35
|
+
...(shouldCompress && { 'content-encoding': 'gzip' }),
|
|
36
|
+
'accept-encoding': 'gzip',
|
|
25
37
|
},
|
|
26
38
|
});
|
|
27
39
|
} catch (err) {
|
|
@@ -31,13 +43,19 @@ export function makeUndiciFetch(client = new Agent()): JsonRpcFetch {
|
|
|
31
43
|
|
|
32
44
|
let responseJson: any;
|
|
33
45
|
const responseOk = resp.statusCode >= 200 && resp.statusCode <= 299;
|
|
46
|
+
const contentEncoding = resp.headers['content-encoding'];
|
|
34
47
|
try {
|
|
35
|
-
|
|
48
|
+
if (contentEncoding === 'gzip') {
|
|
49
|
+
const jsonBuffer = await gunzip(await resp.body.arrayBuffer());
|
|
50
|
+
responseJson = JSON.parse(jsonBuffer.toString('utf-8'));
|
|
51
|
+
} else {
|
|
52
|
+
responseJson = await resp.body.json();
|
|
53
|
+
}
|
|
36
54
|
} catch {
|
|
37
55
|
if (!responseOk) {
|
|
38
56
|
throw new Error('HTTP ' + resp.statusCode);
|
|
39
57
|
}
|
|
40
|
-
throw new Error(`Failed to parse body as JSON: ${await resp.body.text()}`);
|
|
58
|
+
throw new Error(`Failed to parse body as JSON. encoding: ${contentEncoding}, body: ${await resp.body.text()}`);
|
|
41
59
|
}
|
|
42
60
|
|
|
43
61
|
if (!responseOk) {
|
package/src/log/libp2p_logger.ts
CHANGED
|
@@ -2,15 +2,17 @@ import type { ComponentLogger, Logger } from '@libp2p/interface';
|
|
|
2
2
|
|
|
3
3
|
import { getLogLevelFromFilters } from './log-filters.js';
|
|
4
4
|
import type { LogLevel } from './log-levels.js';
|
|
5
|
-
import { logFilters, logger } from './pino-logger.js';
|
|
5
|
+
import { type LoggerBindings, logFilters, logger } from './pino-logger.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Creates a libp2p compatible logger that wraps our pino logger.
|
|
9
9
|
* This adapter implements the ComponentLogger interface required by libp2p.
|
|
10
|
+
* @param namespace - Base namespace for the logger
|
|
11
|
+
* @param bindings - Optional bindings to pass to the logger (actor, instanceId)
|
|
10
12
|
*/
|
|
11
|
-
export function createLibp2pComponentLogger(namespace: string): ComponentLogger {
|
|
13
|
+
export function createLibp2pComponentLogger(namespace: string, bindings?: LoggerBindings): ComponentLogger {
|
|
12
14
|
return {
|
|
13
|
-
forComponent: (component: string) => createLibp2pLogger(`${namespace}:${component}
|
|
15
|
+
forComponent: (component: string) => createLibp2pLogger(`${namespace}:${component}`, bindings),
|
|
14
16
|
};
|
|
15
17
|
}
|
|
16
18
|
|
|
@@ -24,9 +26,14 @@ function replaceFormatting(message: string) {
|
|
|
24
26
|
return message.replace(/(%p|%a)/g, '%s');
|
|
25
27
|
}
|
|
26
28
|
|
|
27
|
-
function createLibp2pLogger(component: string): Logger {
|
|
29
|
+
function createLibp2pLogger(component: string, bindings?: LoggerBindings): Logger {
|
|
28
30
|
// Create a direct pino logger instance for libp2p that supports string interpolation
|
|
29
|
-
const
|
|
31
|
+
const actor = bindings?.actor;
|
|
32
|
+
const instanceId = bindings?.instanceId;
|
|
33
|
+
const log = logger.child(
|
|
34
|
+
{ module: component, ...(actor && { actor }), ...(instanceId && { instanceId }) },
|
|
35
|
+
{ level: getLogLevelFromFilters(logFilters, component) },
|
|
36
|
+
);
|
|
30
37
|
|
|
31
38
|
const logIfEnabled = (level: LogLevel, message: string, ...args: unknown[]) => {
|
|
32
39
|
if (!log.isLevelEnabled(level)) {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
|
|
3
|
+
import { type LoggerBindings, addLogBindingsHandler, removeLogBindingsHandler } from './pino-logger.js';
|
|
4
|
+
|
|
5
|
+
/** AsyncLocalStorage for logger bindings context propagation (Node.js only). */
|
|
6
|
+
const bindingsStorage = new AsyncLocalStorage<LoggerBindings>();
|
|
7
|
+
|
|
8
|
+
/** Returns the current bindings from AsyncLocalStorage, if any. */
|
|
9
|
+
export function getBindings(): LoggerBindings | undefined {
|
|
10
|
+
return bindingsStorage.getStore();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Runs a callback within a bindings context. All loggers created within the callback
|
|
15
|
+
* will automatically inherit the bindings (actor, instanceId) via the log bindings handler.
|
|
16
|
+
*/
|
|
17
|
+
export async function withLoggerBindings<T>(bindings: LoggerBindings, callback: () => Promise<T>): Promise<T> {
|
|
18
|
+
const handler = () => bindingsStorage.getStore();
|
|
19
|
+
addLogBindingsHandler(handler);
|
|
20
|
+
try {
|
|
21
|
+
return await bindingsStorage.run(bindings, callback);
|
|
22
|
+
} finally {
|
|
23
|
+
removeLogBindingsHandler(handler);
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/log/pino-logger.ts
CHANGED
|
@@ -1,19 +1,62 @@
|
|
|
1
|
-
import { createColors, isColorSupported } from 'colorette';
|
|
1
|
+
import { type Color, createColors, isColorSupported } from 'colorette';
|
|
2
2
|
import isNode from 'detect-node';
|
|
3
3
|
import { pino, symbols } from 'pino';
|
|
4
4
|
import type { Writable } from 'stream';
|
|
5
5
|
import { inspect } from 'util';
|
|
6
6
|
|
|
7
7
|
import { compactArray } from '../collection/array.js';
|
|
8
|
-
import {
|
|
8
|
+
import type { EnvVar } from '../config/index.js';
|
|
9
|
+
import { parseBooleanEnv } from '../config/parse-env.js';
|
|
9
10
|
import { GoogleCloudLoggerConfig } from './gcloud-logger-config.js';
|
|
10
11
|
import { getLogLevelFromFilters, parseEnv } from './log-filters.js';
|
|
11
12
|
import type { LogLevel } from './log-levels.js';
|
|
12
13
|
import type { LogData, LogFn } from './log_fn.js';
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
/** Optional bindings to pass to createLogger for additional context. */
|
|
16
|
+
export type LoggerBindings = {
|
|
17
|
+
/** Actor label shown in logs (e.g., 'MAIN', 'prover-node'). */
|
|
18
|
+
actor?: string;
|
|
19
|
+
/** Instance identifier for distinguishing multiple instances of the same component. */
|
|
20
|
+
instanceId?: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// Allow global hooks for providing default bindings.
|
|
24
|
+
// Used by withLoggerBindings in pino-logger-server to propagate bindings via AsyncLocalStorage.
|
|
25
|
+
type LogBindingsHandler = () => LoggerBindings | undefined;
|
|
26
|
+
const logBindingsHandlers: LogBindingsHandler[] = [];
|
|
27
|
+
|
|
28
|
+
export function addLogBindingsHandler(handler: LogBindingsHandler): void {
|
|
29
|
+
logBindingsHandlers.push(handler);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function removeLogBindingsHandler(handler: LogBindingsHandler) {
|
|
33
|
+
const index = logBindingsHandlers.indexOf(handler);
|
|
34
|
+
if (index !== -1) {
|
|
35
|
+
logBindingsHandlers.splice(index, 1);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function getBindingsFromHandlers(): LoggerBindings | undefined {
|
|
40
|
+
for (const handler of logBindingsHandlers) {
|
|
41
|
+
const bindings = handler();
|
|
42
|
+
if (bindings) {
|
|
43
|
+
return bindings;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function createLogger(module: string, bindings?: LoggerBindings): Logger {
|
|
50
|
+
module = module.replace(/^aztec:/, '');
|
|
51
|
+
|
|
52
|
+
const resolvedBindings = { ...getBindingsFromHandlers(), ...bindings };
|
|
53
|
+
const actor = resolvedBindings?.actor;
|
|
54
|
+
const instanceId = resolvedBindings?.instanceId;
|
|
55
|
+
|
|
56
|
+
const pinoLogger = logger.child(
|
|
57
|
+
{ module, ...(actor && { actor }), ...(instanceId && { instanceId }) },
|
|
58
|
+
{ level: getLogLevelFromFilters(logFilters, module) },
|
|
59
|
+
);
|
|
17
60
|
|
|
18
61
|
// We check manually for isLevelEnabled to avoid calling processLogData unnecessarily.
|
|
19
62
|
// Note that isLevelEnabled is missing from the browser version of pino.
|
|
@@ -43,11 +86,24 @@ export function createLogger(module: string): Logger {
|
|
|
43
86
|
isLevelEnabled: (level: LogLevel) => isLevelEnabled(pinoLogger, level),
|
|
44
87
|
/** Module name for the logger. */
|
|
45
88
|
module,
|
|
46
|
-
/** Creates another logger by extending this logger module name. */
|
|
47
|
-
createChild: (childModule: string) => createLogger(`${module}:${childModule}
|
|
89
|
+
/** Creates another logger by extending this logger module name and preserving bindings. */
|
|
90
|
+
createChild: (childModule: string) => createLogger(`${module}:${childModule}`, { actor, instanceId }),
|
|
91
|
+
/** Returns the bindings (actor, instanceId) for this logger. */
|
|
92
|
+
getBindings: () => ({ actor, instanceId }),
|
|
48
93
|
};
|
|
49
94
|
}
|
|
50
95
|
|
|
96
|
+
/**
|
|
97
|
+
* Returns a logger for the given module. If loggerOrBindings is already a Logger, returns it directly.
|
|
98
|
+
* Otherwise, creates a new logger with the given module name and bindings.
|
|
99
|
+
*/
|
|
100
|
+
export function resolveLogger(module: string, loggerOrBindings?: Logger | LoggerBindings): Logger {
|
|
101
|
+
if (loggerOrBindings && 'info' in loggerOrBindings) {
|
|
102
|
+
return loggerOrBindings as Logger;
|
|
103
|
+
}
|
|
104
|
+
return createLogger(module, loggerOrBindings);
|
|
105
|
+
}
|
|
106
|
+
|
|
51
107
|
// Allow global hooks for processing log data.
|
|
52
108
|
// Used for injecting OTEL trace_id in telemetry client.
|
|
53
109
|
type LogDataHandler = (data: LogData) => LogData;
|
|
@@ -61,31 +117,6 @@ function processLogData(data: LogData): LogData {
|
|
|
61
117
|
return logDataHandlers.reduce((accum, handler) => handler(accum), data);
|
|
62
118
|
}
|
|
63
119
|
|
|
64
|
-
// Allow global hooks for tweaking module names.
|
|
65
|
-
// Used in tests to add a uid to modules, so we can differentiate multiple nodes in the same process.
|
|
66
|
-
type LogNameHandler = (module: string) => string;
|
|
67
|
-
const logNameHandlers: LogNameHandler[] = [];
|
|
68
|
-
|
|
69
|
-
export function addLogNameHandler(handler: LogNameHandler): void {
|
|
70
|
-
logNameHandlers.push(handler);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export function removeLogNameHandler(handler: LogNameHandler) {
|
|
74
|
-
const index = logNameHandlers.indexOf(handler);
|
|
75
|
-
if (index !== -1) {
|
|
76
|
-
logNameHandlers.splice(index, 1);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Creates all loggers within the given callback with the suffix appended to the module name. */
|
|
81
|
-
export async function withLogNameSuffix<T>(suffix: string, callback: () => Promise<T>): Promise<T> {
|
|
82
|
-
const logNameHandler = (module: string) => `${module}:${suffix}`;
|
|
83
|
-
addLogNameHandler(logNameHandler);
|
|
84
|
-
const result = await callback();
|
|
85
|
-
removeLogNameHandler(logNameHandler);
|
|
86
|
-
return result;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
120
|
// Patch isLevelEnabled missing from pino/browser.
|
|
90
121
|
function isLevelEnabled(logger: pino.Logger<'verbose', boolean>, level: LogLevel): boolean {
|
|
91
122
|
return typeof logger.isLevelEnabled === 'function'
|
|
@@ -145,22 +176,90 @@ export const levels = {
|
|
|
145
176
|
// Transport options for pretty logging to stderr via pino-pretty.
|
|
146
177
|
const colorEnv = process.env['FORCE_COLOR' satisfies EnvVar];
|
|
147
178
|
const useColor = colorEnv === undefined ? isColorSupported : parseBooleanEnv(colorEnv);
|
|
148
|
-
const { bold, reset
|
|
149
|
-
|
|
179
|
+
const { bold, reset, cyan, magenta, yellow, blue, green, magentaBright, yellowBright, blueBright, greenBright } =
|
|
180
|
+
createColors({ useColor });
|
|
181
|
+
|
|
182
|
+
// Per-actor coloring: each unique actor gets a different color for easier visual distinction.
|
|
183
|
+
// Disabled when LOG_NO_COLOR_PER_ACTOR is set to a truthy value.
|
|
184
|
+
const useColorPerActor = useColor && !parseBooleanEnv(process.env['LOG_NO_COLOR_PER_ACTOR' satisfies EnvVar]);
|
|
185
|
+
const actorColors: Color[] = [yellow, magenta, blue, green, magentaBright, yellowBright, blueBright, greenBright];
|
|
186
|
+
const actorColorMap = new Map<string, Color>();
|
|
187
|
+
let nextColorIndex = 0;
|
|
188
|
+
|
|
189
|
+
/** Returns the color function assigned to a given actor, assigning a new one if needed. */
|
|
190
|
+
export function getActorColor(actor: string): Color {
|
|
191
|
+
let color = actorColorMap.get(actor);
|
|
192
|
+
if (!color) {
|
|
193
|
+
color = actorColors[nextColorIndex % actorColors.length];
|
|
194
|
+
actorColorMap.set(actor, color);
|
|
195
|
+
nextColorIndex++;
|
|
196
|
+
}
|
|
197
|
+
return color;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Resets the actor-to-color mapping. Useful for testing. */
|
|
201
|
+
export function resetActorColors(): void {
|
|
202
|
+
actorColorMap.clear();
|
|
203
|
+
nextColorIndex = 0;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// String template for messageFormat (used in worker threads and when per-actor coloring is disabled).
|
|
207
|
+
const messageFormatString = `${bold('{module}')}{if actor} ${cyan('{actor}')}{end}{if instanceId} ${reset(cyan('{instanceId}'))}{end} ${reset('{msg}')}`;
|
|
208
|
+
|
|
209
|
+
// Function for messageFormat when per-actor coloring is enabled (can only be used in-process, not worker threads).
|
|
210
|
+
type LogObject = { actor?: string; module?: string; instanceId?: string; msg?: string };
|
|
211
|
+
|
|
212
|
+
/** Formats a log message with per-actor coloring. Actor, module, and instanceId share the same color. */
|
|
213
|
+
export function formatLogMessage(log: LogObject, messageKey: string): string {
|
|
214
|
+
const actor = log.actor;
|
|
215
|
+
const module = log.module ?? '';
|
|
216
|
+
const instanceId = log.instanceId;
|
|
217
|
+
const msg = log[messageKey as keyof LogObject] ?? '';
|
|
218
|
+
|
|
219
|
+
// Use actor color for actor, module, and instanceId when actor is present
|
|
220
|
+
const color = actor ? getActorColor(actor) : cyan;
|
|
221
|
+
|
|
222
|
+
let result = bold(color(module));
|
|
223
|
+
if (actor) {
|
|
224
|
+
result += ' ' + color(actor);
|
|
225
|
+
}
|
|
226
|
+
if (instanceId) {
|
|
227
|
+
result += ' ' + reset(color(instanceId));
|
|
228
|
+
}
|
|
229
|
+
result += ' ' + reset(String(msg));
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Base options for pino-pretty (shared between transport and direct use).
|
|
234
|
+
const pinoPrettyBaseOpts = {
|
|
150
235
|
destination: 2,
|
|
151
236
|
sync: true,
|
|
152
237
|
colorize: useColor,
|
|
153
|
-
ignore: 'module,pid,hostname,trace_id,span_id,trace_flags,severity',
|
|
154
|
-
messageFormat: `${bold('{module}')} ${reset('{msg}')}`,
|
|
238
|
+
ignore: 'module,actor,instanceId,pid,hostname,trace_id,span_id,trace_flags,severity',
|
|
155
239
|
customLevels: 'fatal:60,error:50,warn:40,info:30,verbose:25,debug:20,trace:10',
|
|
156
240
|
customColors: 'fatal:bgRed,error:red,warn:yellow,info:green,verbose:magenta,debug:blue,trace:gray',
|
|
157
241
|
minimumLevel: 'trace' as const,
|
|
158
242
|
singleLine: !parseBooleanEnv(process.env['LOG_MULTILINE' satisfies EnvVar]),
|
|
159
243
|
};
|
|
160
244
|
|
|
245
|
+
/**
|
|
246
|
+
* Pino-pretty options for direct use (e.g., jest/setup.mjs).
|
|
247
|
+
* Includes function-based messageFormat for per-actor coloring when enabled.
|
|
248
|
+
*/
|
|
249
|
+
export const pinoPrettyOpts = {
|
|
250
|
+
...pinoPrettyBaseOpts,
|
|
251
|
+
messageFormat: useColorPerActor ? formatLogMessage : messageFormatString,
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// Transport options use string template only (functions can't be serialized to worker threads).
|
|
255
|
+
const prettyTransportOpts = {
|
|
256
|
+
...pinoPrettyBaseOpts,
|
|
257
|
+
messageFormat: messageFormatString,
|
|
258
|
+
};
|
|
259
|
+
|
|
161
260
|
const prettyTransport: pino.TransportTargetOptions = {
|
|
162
261
|
target: 'pino-pretty',
|
|
163
|
-
options:
|
|
262
|
+
options: prettyTransportOpts,
|
|
164
263
|
level: 'trace',
|
|
165
264
|
};
|
|
166
265
|
|
|
@@ -261,6 +360,8 @@ export type Logger = { [K in LogLevel]: LogFn } & { /** Error log function */ er
|
|
|
261
360
|
isLevelEnabled: (level: LogLevel) => boolean;
|
|
262
361
|
module: string;
|
|
263
362
|
createChild: (childModule: string) => Logger;
|
|
363
|
+
/** Returns the bindings (actor, instanceId) for this logger. */
|
|
364
|
+
getBindings: () => LoggerBindings;
|
|
264
365
|
};
|
|
265
366
|
|
|
266
367
|
/**
|
package/src/queue/semaphore.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { FifoMemoryQueue } from './fifo_memory_queue.js';
|
|
2
2
|
|
|
3
|
+
export interface ISemaphore {
|
|
4
|
+
acquire(): Promise<void>;
|
|
5
|
+
release(): void;
|
|
6
|
+
}
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* Allows the acquiring of up to `size` tokens before calls to acquire block, waiting for a call to release().
|
|
5
10
|
*/
|
package/src/retry/index.ts
CHANGED
|
@@ -103,3 +103,21 @@ export async function retryUntil<T>(
|
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Convenience wrapper around retryUntil with fast polling for tests.
|
|
109
|
+
* Uses 10s timeout and 100ms polling interval by default.
|
|
110
|
+
*
|
|
111
|
+
* @param fn - The function to retry until it returns a truthy value.
|
|
112
|
+
* @param name - Description of what we're waiting for (for error messages).
|
|
113
|
+
* @param timeout - Optional timeout in seconds. Defaults to 10s.
|
|
114
|
+
* @param interval - Optional interval in seconds. Defaults to 0.1s (100ms).
|
|
115
|
+
*/
|
|
116
|
+
export function retryFastUntil<T>(
|
|
117
|
+
fn: () => (T | undefined) | Promise<T | undefined>,
|
|
118
|
+
name = '',
|
|
119
|
+
timeout = 10,
|
|
120
|
+
interval = 0.1,
|
|
121
|
+
) {
|
|
122
|
+
return retryUntil(fn, name, timeout, interval);
|
|
123
|
+
}
|