@aztec/foundation 0.0.1-commit.3469e52 → 0.0.1-commit.350e0a4d
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/bigint-buffer/index.d.ts +1 -1
- package/dest/bigint-buffer/index.d.ts.map +1 -1
- package/dest/bigint-buffer/index.js +6 -1
- package/dest/branded-types/block_number.d.ts +3 -3
- package/dest/branded-types/block_number.d.ts.map +1 -1
- package/dest/branded-types/block_number.js +15 -4
- package/dest/branded-types/buffer32_hash.d.ts +27 -0
- package/dest/branded-types/buffer32_hash.d.ts.map +1 -0
- package/dest/branded-types/buffer32_hash.js +12 -0
- package/dest/branded-types/checkpoint_number.d.ts +9 -3
- package/dest/branded-types/checkpoint_number.d.ts.map +1 -1
- package/dest/branded-types/checkpoint_number.js +11 -0
- package/dest/branded-types/epoch.d.ts +2 -2
- package/dest/branded-types/epoch.d.ts.map +1 -1
- package/dest/branded-types/index.d.ts +2 -1
- package/dest/branded-types/index.d.ts.map +1 -1
- package/dest/branded-types/index.js +1 -0
- package/dest/branded-types/index_within_checkpoint.d.ts +2 -2
- package/dest/branded-types/index_within_checkpoint.d.ts.map +1 -1
- package/dest/branded-types/slot.d.ts +5 -2
- 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 +28 -61
- package/dest/buffer/buffer32.d.ts.map +1 -1
- package/dest/buffer/buffer32.js +29 -63
- 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 +22 -1
- package/dest/collection/array.d.ts.map +1 -1
- package/dest/collection/array.js +84 -0
- package/dest/collection/index.d.ts +3 -1
- package/dest/collection/index.d.ts.map +1 -1
- package/dest/collection/index.js +2 -0
- package/dest/collection/lru_map.d.ts +41 -0
- package/dest/collection/lru_map.d.ts.map +1 -0
- package/dest/collection/lru_map.js +119 -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/committable/committable.js +1 -1
- 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 +67 -37
- package/dest/config/index.d.ts.map +1 -1
- package/dest/config/index.js +86 -70
- package/dest/config/network_config.d.ts +15 -41
- 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 +3 -3
- package/dest/config/secret_value.d.ts +2 -2
- package/dest/config/secret_value.d.ts.map +1 -1
- package/dest/crypto/aes128/index.d.ts +2 -1
- package/dest/crypto/aes128/index.d.ts.map +1 -1
- package/dest/crypto/aes128/index.js +11 -2
- package/dest/crypto/bls/bn254_keystore.d.ts +10 -177
- package/dest/crypto/bls/bn254_keystore.d.ts.map +1 -1
- package/dest/crypto/bls/bn254_keystore.js +22 -34
- package/dest/crypto/keys/index.d.ts +3 -2
- package/dest/crypto/keys/index.d.ts.map +1 -1
- package/dest/crypto/keys/index.js +30 -6
- package/dest/crypto/poseidon/index.d.ts +1 -2
- package/dest/crypto/poseidon/index.d.ts.map +1 -1
- package/dest/crypto/poseidon/index.js +40 -42
- 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 +4 -4
- package/dest/crypto/schnorr/index.d.ts +8 -7
- package/dest/crypto/schnorr/index.d.ts.map +1 -1
- package/dest/crypto/schnorr/index.js +6 -6
- package/dest/crypto/schnorr/signature.d.ts +9 -29
- package/dest/crypto/schnorr/signature.d.ts.map +1 -1
- package/dest/crypto/schnorr/signature.js +20 -36
- 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/crypto/sync/poseidon/index.d.ts +1 -2
- package/dest/crypto/sync/poseidon/index.d.ts.map +1 -1
- package/dest/crypto/sync/poseidon/index.js +0 -8
- package/dest/curves/bls12/field.d.ts +3 -3
- package/dest/curves/bls12/field.d.ts.map +1 -1
- package/dest/curves/bls12/field.js +0 -5
- package/dest/curves/bls12/point.d.ts +2 -2
- package/dest/curves/bls12/point.d.ts.map +1 -1
- package/dest/curves/bn254/field.d.ts +24 -19
- package/dest/curves/bn254/field.d.ts.map +1 -1
- package/dest/curves/bn254/field.js +28 -19
- package/dest/curves/grumpkin/point.d.ts +14 -28
- package/dest/curves/grumpkin/point.d.ts.map +1 -1
- package/dest/curves/grumpkin/point.js +29 -43
- package/dest/eth-address/index.d.ts +2 -2
- package/dest/eth-address/index.d.ts.map +1 -1
- package/dest/eth-address/index.js +0 -3
- package/dest/eth-signature/eth_signature.d.ts +3 -2
- 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 +91 -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/fifo_set/fifo_set.d.ts +15 -0
- package/dest/fifo_set/fifo_set.d.ts.map +1 -0
- package/dest/fifo_set/fifo_set.js +39 -0
- package/dest/fifo_set/index.d.ts +2 -0
- package/dest/fifo_set/index.d.ts.map +1 -0
- package/dest/fifo_set/index.js +1 -0
- package/dest/jest/setup.js +28 -1
- package/dest/json-rpc/client/fetch.d.ts +1 -1
- package/dest/json-rpc/client/fetch.d.ts.map +1 -1
- package/dest/json-rpc/client/fetch.js +4 -3
- 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 +3 -3
- 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 +23 -4
- package/dest/json-rpc/fixtures/test_state.d.ts +4 -8
- package/dest/json-rpc/fixtures/test_state.d.ts.map +1 -1
- package/dest/json-rpc/fixtures/test_state.js +57 -17
- 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/json-rpc/server/safe_json_rpc_server.d.ts +8 -3
- package/dest/json-rpc/server/safe_json_rpc_server.d.ts.map +1 -1
- package/dest/json-rpc/server/safe_json_rpc_server.js +28 -14
- 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/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/log-filters.d.ts +17 -4
- package/dest/log/log-filters.d.ts.map +1 -1
- package/dest/log/log-filters.js +26 -12
- 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 +137 -29
- package/dest/noir/noir_package_config.d.ts +19 -104
- package/dest/noir/noir_package_config.d.ts.map +1 -1
- package/dest/noir/noir_package_config.js +1 -1
- package/dest/promise/running-promise.d.ts +5 -4
- package/dest/promise/running-promise.d.ts.map +1 -1
- package/dest/promise/running-promise.js +7 -3
- package/dest/queue/base_memory_queue.d.ts +2 -2
- package/dest/queue/base_memory_queue.d.ts.map +1 -1
- package/dest/queue/batch_queue.d.ts +1 -1
- package/dest/queue/batch_queue.d.ts.map +1 -1
- package/dest/queue/batch_queue.js +1 -0
- package/dest/queue/semaphore.d.ts +5 -1
- package/dest/queue/semaphore.d.ts.map +1 -1
- package/dest/retry/index.d.ts +31 -3
- package/dest/retry/index.d.ts.map +1 -1
- package/dest/retry/index.js +44 -2
- package/dest/schemas/api.d.ts +12 -10
- package/dest/schemas/api.d.ts.map +1 -1
- package/dest/schemas/api.js +7 -1
- package/dest/schemas/parse.d.ts +4 -4
- package/dest/schemas/parse.d.ts.map +1 -1
- package/dest/schemas/parse.js +6 -5
- package/dest/schemas/schemas.d.ts +15 -15
- package/dest/schemas/types.d.ts +3 -3
- package/dest/schemas/types.d.ts.map +1 -1
- package/dest/schemas/utils.d.ts +7 -11
- package/dest/schemas/utils.d.ts.map +1 -1
- package/dest/schemas/utils.js +2 -18
- 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/buffer_sink.d.ts +134 -0
- package/dest/serialize/buffer_sink.d.ts.map +1 -0
- package/dest/serialize/buffer_sink.js +297 -0
- package/dest/serialize/index.d.ts +2 -1
- package/dest/serialize/index.d.ts.map +1 -1
- package/dest/serialize/index.js +1 -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/string/index.js +1 -1
- package/dest/testing/files/index.d.ts +2 -3
- package/dest/testing/files/index.d.ts.map +1 -1
- package/dest/testing/files/index.js +1 -2
- package/dest/timer/date.d.ts +25 -1
- package/dest/timer/date.d.ts.map +1 -1
- package/dest/timer/date.js +33 -0
- package/dest/timer/index.d.ts +2 -2
- package/dest/timer/index.d.ts.map +1 -1
- package/dest/timer/index.js +1 -1
- package/dest/timer/timeout.d.ts +3 -1
- package/dest/timer/timeout.d.ts.map +1 -1
- package/dest/timer/timeout.js +26 -0
- package/dest/transport/dispatch/create_dispatch_proxy.d.ts +11 -2
- package/dest/transport/dispatch/create_dispatch_proxy.d.ts.map +1 -1
- package/dest/transport/index.d.ts +1 -2
- package/dest/transport/index.d.ts.map +1 -1
- package/dest/transport/index.js +0 -1
- package/dest/transport/transport_client.js +2 -2
- package/dest/trees/balanced_merkle_tree_root.d.ts +3 -5
- package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -1
- package/dest/trees/balanced_merkle_tree_root.js +3 -5
- package/dest/trees/hasher.d.ts +3 -3
- package/dest/trees/hasher.d.ts.map +1 -1
- package/dest/trees/hasher.js +5 -10
- 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/dest/trees/membership_witness.d.ts +2 -2
- package/dest/trees/membership_witness.d.ts.map +1 -1
- package/dest/trees/merkle_tree_calculator.d.ts +4 -2
- package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/merkle_tree_calculator.js +1 -5
- package/dest/trees/sibling_path.d.ts +5 -4
- package/dest/trees/sibling_path.d.ts.map +1 -1
- package/dest/trees/sibling_path.js +10 -8
- package/dest/trees/unbalanced_merkle_tree_root.d.ts +2 -3
- package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree_root.js +2 -3
- package/dest/types/index.d.ts +25 -1
- package/dest/types/index.d.ts.map +1 -1
- package/dest/types/index.js +18 -0
- package/package.json +27 -4
- package/src/array/sorted_array.ts +22 -17
- package/src/bigint-buffer/index.ts +6 -1
- package/src/branded-types/block_number.ts +20 -2
- package/src/branded-types/buffer32_hash.ts +38 -0
- package/src/branded-types/checkpoint_number.ts +16 -1
- package/src/branded-types/epoch.ts +1 -1
- package/src/branded-types/index.ts +1 -0
- package/src/branded-types/index_within_checkpoint.ts +1 -1
- package/src/branded-types/slot.ts +6 -1
- package/src/buffer/buffer16.ts +3 -0
- package/src/buffer/buffer32.ts +39 -68
- package/src/buffer/index.ts +1 -0
- package/src/buffer/utils.ts +8 -0
- package/src/collection/array.ts +86 -0
- package/src/collection/index.ts +2 -0
- package/src/collection/lru_map.ts +143 -0
- package/src/collection/lru_set.ts +115 -0
- package/src/committable/committable.ts +1 -1
- package/src/config/env_var.ts +97 -23
- package/src/config/index.ts +157 -121
- package/src/config/network_config.ts +3 -0
- package/src/config/network_name.ts +6 -6
- package/src/config/secret_value.ts +1 -1
- package/src/crypto/aes128/index.ts +11 -2
- package/src/crypto/bls/bn254_keystore.ts +33 -43
- package/src/crypto/keys/index.ts +20 -4
- package/src/crypto/poseidon/index.ts +42 -44
- package/src/crypto/random/randomness_singleton.ts +9 -5
- package/src/crypto/schnorr/index.ts +9 -8
- package/src/crypto/schnorr/signature.ts +17 -46
- package/src/crypto/secp256k1-signer/utils.ts +32 -0
- package/src/crypto/sync/poseidon/index.ts +0 -9
- package/src/curves/bls12/field.ts +0 -7
- package/src/curves/bn254/field.ts +41 -36
- package/src/curves/grumpkin/point.ts +23 -40
- package/src/eth-address/index.ts +0 -4
- package/src/eth-signature/eth_signature.ts +7 -1
- package/src/fifo/fifo_frame_reader.ts +113 -0
- package/src/fifo/index.ts +1 -0
- package/src/fifo_set/fifo_set.ts +52 -0
- package/src/fifo_set/index.ts +1 -0
- package/src/jest/setup.mjs +31 -1
- package/src/json-rpc/client/fetch.ts +4 -3
- package/src/json-rpc/client/safe_json_rpc_client.ts +4 -2
- package/src/json-rpc/client/undici.ts +23 -3
- package/src/json-rpc/fixtures/test_state.ts +10 -10
- package/src/json-rpc/server/api_key_auth.ts +63 -0
- package/src/json-rpc/server/index.ts +1 -0
- package/src/json-rpc/server/safe_json_rpc_server.ts +32 -14
- package/src/log/bigint-utils.ts +25 -0
- package/src/log/gcloud-logger-config.ts +5 -0
- package/src/log/libp2p_logger.ts +12 -5
- package/src/log/log-filters.ts +29 -11
- package/src/log/pino-logger-server.ts +25 -0
- package/src/log/pino-logger.ts +160 -39
- package/src/noir/noir_package_config.ts +32 -20
- package/src/promise/running-promise.ts +8 -5
- package/src/queue/base_memory_queue.ts +1 -1
- package/src/queue/batch_queue.ts +1 -0
- package/src/queue/semaphore.ts +5 -0
- package/src/retry/index.ts +66 -4
- package/src/schemas/api.ts +25 -25
- package/src/schemas/parse.ts +9 -6
- package/src/schemas/schemas.ts +4 -4
- package/src/schemas/types.ts +2 -2
- package/src/schemas/utils.ts +4 -38
- package/src/serialize/buffer_reader.ts +15 -0
- package/src/serialize/buffer_sink.ts +350 -0
- package/src/serialize/index.ts +1 -0
- package/src/serialize/serialize.ts +32 -0
- package/src/sleep/index.ts +10 -1
- package/src/string/index.ts +1 -1
- package/src/testing/files/index.ts +1 -2
- package/src/timer/date.ts +48 -0
- package/src/timer/index.ts +1 -1
- package/src/timer/timeout.ts +31 -0
- package/src/transport/dispatch/create_dispatch_proxy.ts +11 -1
- package/src/transport/index.ts +0 -1
- package/src/transport/transport_client.ts +2 -2
- package/src/trees/balanced_merkle_tree_root.ts +3 -9
- package/src/trees/hasher.ts +6 -7
- package/src/trees/indexed_merkle_tree_calculator.ts +5 -1
- package/src/trees/merkle_tree_calculator.ts +4 -10
- package/src/trees/sibling_path.ts +11 -6
- package/src/trees/unbalanced_merkle_tree_root.ts +2 -5
- package/src/types/index.ts +52 -0
- 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/dest/transport/dispatch/create_dispatch_fn.d.ts +0 -25
- package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +0 -1
- package/dest/transport/dispatch/create_dispatch_fn.js +0 -17
- package/src/crypto/serialize.ts +0 -85
- package/src/transport/dispatch/create_dispatch_fn.ts +0 -35
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { toBigIntBE } from '../../bigint-buffer/index.js';
|
|
2
|
-
import { poseidon2Hash } from '../../crypto/poseidon/index.js';
|
|
3
2
|
import { randomBoolean } from '../../crypto/random/index.js';
|
|
4
3
|
import { hexSchemaFor } from '../../schemas/utils.js';
|
|
5
4
|
import { BufferReader, FieldReader, serializeToBuffer } from '../../serialize/index.js';
|
|
@@ -13,13 +12,16 @@ import { Fr } from '../bn254/field.js';
|
|
|
13
12
|
* TODO(#7386): Clean up this class.
|
|
14
13
|
*/
|
|
15
14
|
export class Point {
|
|
16
|
-
static
|
|
15
|
+
static INFINITY = new Point(Fr.ZERO, Fr.ZERO);
|
|
17
16
|
static SIZE_IN_BYTES = Fr.SIZE_IN_BYTES * 2;
|
|
18
17
|
static COMPRESSED_SIZE_IN_BYTES = Fr.SIZE_IN_BYTES;
|
|
19
18
|
|
|
20
19
|
/** Used to differentiate this class from AztecAddress */
|
|
21
20
|
public readonly kind = 'point';
|
|
22
21
|
|
|
22
|
+
/** Whether the point is at infinity */
|
|
23
|
+
public readonly isInfinite: boolean;
|
|
24
|
+
|
|
23
25
|
constructor(
|
|
24
26
|
/**
|
|
25
27
|
* The point's x coordinate
|
|
@@ -29,12 +31,10 @@ export class Point {
|
|
|
29
31
|
* The point's y coordinate
|
|
30
32
|
*/
|
|
31
33
|
public readonly y: Fr,
|
|
32
|
-
/**
|
|
33
|
-
* Whether the point is at infinity
|
|
34
|
-
*/
|
|
35
|
-
public readonly isInfinite: boolean,
|
|
36
34
|
) {
|
|
37
35
|
// TODO(#7386): check if on curve
|
|
36
|
+
// NOTE: now there is no isInfinite in the struct, empty == inf, so an empty class would pass an on-curve check. This may be fine depending on the usage.
|
|
37
|
+
this.isInfinite = x.isZero() && y.isZero();
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
toJSON() {
|
|
@@ -61,11 +61,11 @@ export class Point {
|
|
|
61
61
|
if (obj instanceof Buffer || Buffer.isBuffer(obj)) {
|
|
62
62
|
return Point.fromBuffer(obj);
|
|
63
63
|
}
|
|
64
|
-
return new
|
|
64
|
+
return new this(Fr.fromPlainObject(obj.x), Fr.fromPlainObject(obj.y));
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
|
-
* Generate a random Point instance.
|
|
68
|
+
* Generate a random Point instance that is on the curve.
|
|
69
69
|
*
|
|
70
70
|
* @returns A randomly generated Point instance.
|
|
71
71
|
*/
|
|
@@ -92,7 +92,7 @@ export class Point {
|
|
|
92
92
|
*/
|
|
93
93
|
static fromBuffer(buffer: Buffer | BufferReader) {
|
|
94
94
|
const reader = BufferReader.asReader(buffer);
|
|
95
|
-
return new this(Fr.fromBuffer(reader), Fr.fromBuffer(reader)
|
|
95
|
+
return new this(Fr.fromBuffer(reader), Fr.fromBuffer(reader));
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
/**
|
|
@@ -129,12 +129,13 @@ export class Point {
|
|
|
129
129
|
* @returns The point as an array of 2 fields
|
|
130
130
|
*/
|
|
131
131
|
toFields() {
|
|
132
|
-
return [this.x, this.y
|
|
132
|
+
return [this.x, this.y];
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
static fromFields(fields: Fr[] | FieldReader) {
|
|
136
136
|
const reader = FieldReader.asReader(fields);
|
|
137
|
-
|
|
137
|
+
const [x, y] = [reader.readField(), reader.readField()];
|
|
138
|
+
return new this(x, y);
|
|
138
139
|
}
|
|
139
140
|
|
|
140
141
|
/**
|
|
@@ -159,11 +160,12 @@ export class Point {
|
|
|
159
160
|
const finalY = sign ? new Fr(yPositiveBigInt) : new Fr(yNegativeBigInt);
|
|
160
161
|
|
|
161
162
|
// Create and return the new Point
|
|
162
|
-
return new this(x, finalY
|
|
163
|
+
return new this(x, finalY);
|
|
163
164
|
}
|
|
164
165
|
|
|
165
166
|
/**
|
|
166
|
-
* @
|
|
167
|
+
* @param x - The x coordinate of the point
|
|
168
|
+
* @returns y^2 such that y^2 = x^3 - 17
|
|
167
169
|
*/
|
|
168
170
|
static YFromX(x: Fr): Promise<Fr | null> {
|
|
169
171
|
// Calculate y^2 = x^3 - 17 (i.e. the Grumpkin curve equation)
|
|
@@ -191,24 +193,15 @@ export class Point {
|
|
|
191
193
|
return {
|
|
192
194
|
x: this.x.toBigInt(),
|
|
193
195
|
y: this.y.toBigInt(),
|
|
194
|
-
isInfinite: this.isInfinite ? 1n : 0n,
|
|
195
196
|
};
|
|
196
197
|
}
|
|
197
198
|
|
|
198
199
|
/**
|
|
199
200
|
* Converts the Point instance to a Buffer representation of the coordinates.
|
|
200
201
|
* @returns A Buffer representation of the Point instance.
|
|
201
|
-
* @dev Note that toBuffer does not include the isInfinite flag
|
|
202
|
-
* This is because currently when we work with point as bytes we don't want to populate the extra bytes for
|
|
203
|
-
* isInfinite flag because:
|
|
204
|
-
* 1. Our Grumpkin BB API currently does not handle point at infinity,
|
|
205
|
-
* 2. we use toBuffer when serializing notes and events and there we only work with public keys and point at infinity
|
|
206
|
-
* is not considered a valid public key and the extra byte would raise DA cost.
|
|
202
|
+
* @dev Note that toBuffer does not include the isInfinite flag. The point at infinity is serialized as (0, 0).
|
|
207
203
|
*/
|
|
208
204
|
toBuffer() {
|
|
209
|
-
if (this.isInfinite) {
|
|
210
|
-
throw new Error('Cannot serialize infinite point with isInfinite flag');
|
|
211
|
-
}
|
|
212
205
|
const buf = serializeToBuffer([this.x, this.y]);
|
|
213
206
|
if (buf.length !== Point.SIZE_IN_BYTES) {
|
|
214
207
|
throw new Error(`Invalid buffer length for Point: ${buf.length}`);
|
|
@@ -258,12 +251,10 @@ export class Point {
|
|
|
258
251
|
}
|
|
259
252
|
|
|
260
253
|
toNoirStruct() {
|
|
261
|
-
|
|
262
|
-
return { x: this.x, y: this.y, is_infinite: this.isInfinite };
|
|
263
|
-
/* eslint-enable camelcase */
|
|
254
|
+
return { x: this.x, y: this.y };
|
|
264
255
|
}
|
|
265
256
|
|
|
266
|
-
// Used for IvpkM
|
|
257
|
+
// Used for IvpkM. TODO(#8124): Consider removing this method.
|
|
267
258
|
toWrappedNoirStruct() {
|
|
268
259
|
return { inner: this.toNoirStruct() };
|
|
269
260
|
}
|
|
@@ -283,21 +274,13 @@ export class Point {
|
|
|
283
274
|
return this.x.isZero() && this.y.isZero();
|
|
284
275
|
}
|
|
285
276
|
|
|
286
|
-
hash() {
|
|
287
|
-
return poseidon2Hash(this.toFields());
|
|
288
|
-
}
|
|
289
|
-
|
|
290
277
|
/**
|
|
291
|
-
*
|
|
292
|
-
*
|
|
278
|
+
* @param x - The x coordinate of the point
|
|
279
|
+
* @param y - The y coordinate of the point
|
|
280
|
+
* @returns Whether the point exists on Grumpkin
|
|
293
281
|
*/
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
isOnGrumpkin() {
|
|
299
|
-
// TODO: Check this against how bb handles curve check and infinity point check
|
|
300
|
-
if (this.inf) {
|
|
282
|
+
isOnCurve() {
|
|
283
|
+
if (this.isInfinite) {
|
|
301
284
|
return true;
|
|
302
285
|
}
|
|
303
286
|
|
package/src/eth-address/index.ts
CHANGED
|
@@ -5,7 +5,6 @@ import { randomBytes } from '../crypto/random/index.js';
|
|
|
5
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
|
-
import { TypeRegistry } from '../serialize/type_registry.js';
|
|
9
8
|
import { bufferToHex } from '../string/index.js';
|
|
10
9
|
|
|
11
10
|
/**
|
|
@@ -268,6 +267,3 @@ export class EthAddress {
|
|
|
268
267
|
return addrA.equals(addrB);
|
|
269
268
|
}
|
|
270
269
|
}
|
|
271
|
-
|
|
272
|
-
// For deserializing JSON.
|
|
273
|
-
TypeRegistry.register('EthAddress', EthAddress);
|
|
@@ -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,113 @@
|
|
|
1
|
+
import EventEmitter from 'node:events';
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import * as net from 'node:net';
|
|
4
|
+
import type { Readable } from 'node:stream';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Events emitted by FifoFrameReader.
|
|
8
|
+
*
|
|
9
|
+
* - `frame`: A complete frame payload (without the 4-byte length header).
|
|
10
|
+
* - `error`: An unrecoverable error (invalid frame length, stream error).
|
|
11
|
+
* - `end`: The underlying stream has ended.
|
|
12
|
+
*/
|
|
13
|
+
export interface FifoFrameReaderEvents {
|
|
14
|
+
frame: [payload: Buffer];
|
|
15
|
+
error: [error: Error];
|
|
16
|
+
end: [];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Reads length-delimited frames from a readable stream (typically a named FIFO pipe).
|
|
21
|
+
*
|
|
22
|
+
* Wire format: `[4-byte big-endian payload length][payload bytes]`
|
|
23
|
+
*
|
|
24
|
+
* Emits a `frame` event for each complete frame with the raw payload buffer.
|
|
25
|
+
* Callers are responsible for deserializing the payload (e.g., via msgpack).
|
|
26
|
+
*
|
|
27
|
+
* On encountering an invalid payload length (0 or >maxPayloadSize), emits `error`
|
|
28
|
+
* and destroys the stream.
|
|
29
|
+
*/
|
|
30
|
+
export class FifoFrameReader extends EventEmitter<FifoFrameReaderEvents> {
|
|
31
|
+
private stream: Readable | null = null;
|
|
32
|
+
private pendingBuf: Buffer = Buffer.alloc(0);
|
|
33
|
+
private running = false;
|
|
34
|
+
|
|
35
|
+
constructor(private readonly maxPayloadSize = 10 * 1024 * 1024) {
|
|
36
|
+
super();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Open a FIFO at the given path and start reading frames. */
|
|
40
|
+
start(fifoPath: string): void {
|
|
41
|
+
// Read the FIFO through a libuv pipe handle (net.Socket) rather than fs.createReadStream.
|
|
42
|
+
// An fs read stream services the pipe with a blocking threadpool read that destroy() cannot
|
|
43
|
+
// cancel: if a writer never closes, that read stays parked and keeps the host process alive
|
|
44
|
+
// (manifesting as Jest "did not exit"). Opening O_NONBLOCK and wrapping the fd in a pipe
|
|
45
|
+
// handle makes reads epoll-based, so stop()'s destroy() releases the handle immediately,
|
|
46
|
+
// regardless of whether the writer is still attached.
|
|
47
|
+
const fd = fs.openSync(fifoPath, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK);
|
|
48
|
+
let socket: net.Socket;
|
|
49
|
+
try {
|
|
50
|
+
socket = new net.Socket({ fd, readable: true, writable: false });
|
|
51
|
+
} catch (err) {
|
|
52
|
+
fs.closeSync(fd);
|
|
53
|
+
throw err;
|
|
54
|
+
}
|
|
55
|
+
this.startFromStream(socket);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Start reading frames from an existing readable stream. */
|
|
59
|
+
startFromStream(stream: Readable): void {
|
|
60
|
+
if (this.running) {
|
|
61
|
+
throw new Error('FifoFrameReader is already running');
|
|
62
|
+
}
|
|
63
|
+
this.running = true;
|
|
64
|
+
this.pendingBuf = Buffer.alloc(0);
|
|
65
|
+
this.stream = stream;
|
|
66
|
+
|
|
67
|
+
stream.on('data', (chunk: Buffer | string) => {
|
|
68
|
+
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
69
|
+
this.pendingBuf = this.pendingBuf.length > 0 ? Buffer.concat([this.pendingBuf, buf]) : buf;
|
|
70
|
+
this.drainFrames();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
stream.on('error', (err: Error) => {
|
|
74
|
+
if (this.running) {
|
|
75
|
+
this.emit('error', err);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
stream.on('end', () => {
|
|
80
|
+
this.emit('end');
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Stop reading and destroy the underlying stream. */
|
|
85
|
+
stop(): void {
|
|
86
|
+
this.running = false;
|
|
87
|
+
if (this.stream) {
|
|
88
|
+
this.stream.destroy();
|
|
89
|
+
this.stream = null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Parse complete frames out of the pending buffer. */
|
|
94
|
+
private drainFrames(): void {
|
|
95
|
+
while (this.pendingBuf.length >= 4) {
|
|
96
|
+
const payloadLen = this.pendingBuf.readUInt32BE(0);
|
|
97
|
+
if (payloadLen === 0 || payloadLen > this.maxPayloadSize) {
|
|
98
|
+
this.emit('error', new Error(`Invalid payload length: ${payloadLen}`));
|
|
99
|
+
this.stop();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const frameLen = 4 + payloadLen;
|
|
104
|
+
if (this.pendingBuf.length < frameLen) {
|
|
105
|
+
break; // Wait for more data
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const payload = this.pendingBuf.subarray(4, frameLen);
|
|
109
|
+
this.pendingBuf = this.pendingBuf.subarray(frameLen);
|
|
110
|
+
this.emit('frame', Buffer.from(payload));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FifoFrameReader } from './fifo_frame_reader.js';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** A Set capped to a fixed number of entries, evicting the oldest inserted value when full. */
|
|
2
|
+
export class FifoSet<T> extends Set<T> {
|
|
3
|
+
private _limit: number | undefined;
|
|
4
|
+
|
|
5
|
+
private constructor(values?: Iterable<T>) {
|
|
6
|
+
super(values);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Creates a bounded set with a positive integer limit. */
|
|
10
|
+
static withLimit<T>(limit: number, values?: Iterable<T>): FifoSet<T> {
|
|
11
|
+
if (!Number.isSafeInteger(limit) || limit <= 0) {
|
|
12
|
+
throw new TypeError(`FifoSet limit must be a positive safe integer: ${limit}`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const set = new FifoSet(values);
|
|
16
|
+
set._limit = limit;
|
|
17
|
+
set.compact();
|
|
18
|
+
|
|
19
|
+
return set;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
override add(value: T): this {
|
|
23
|
+
super.add(value);
|
|
24
|
+
this.compact();
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Maximum number of entries retained by this set. */
|
|
29
|
+
public get limit(): number {
|
|
30
|
+
return this._limit ?? Infinity;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Evicts oldest entries until the set is within its limit. */
|
|
34
|
+
public compact(): void {
|
|
35
|
+
while (this.size > this.limit) {
|
|
36
|
+
const head = this.values().next();
|
|
37
|
+
if (head.done) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
this.delete(head.value);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Adds a value only if it is absent, returning whether the set changed. */
|
|
45
|
+
public addIfAbsent(value: T): boolean {
|
|
46
|
+
if (this.has(value)) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
this.add(value);
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './fifo_set.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,33 @@ 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
|
+
}
|
|
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
|
+
}
|
|
@@ -43,17 +43,18 @@ export async function defaultFetch(
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
let responseJson;
|
|
46
|
+
const responseText = await resp.text();
|
|
46
47
|
try {
|
|
47
|
-
responseJson =
|
|
48
|
+
responseJson = JSON.parse(responseText);
|
|
48
49
|
} catch {
|
|
49
50
|
if (!resp.ok) {
|
|
50
51
|
throw new Error(resp.statusText);
|
|
51
52
|
}
|
|
52
|
-
throw new Error(`Failed to parse body as JSON: ${
|
|
53
|
+
throw new Error(`Failed to parse body as JSON: ${responseText}`);
|
|
53
54
|
}
|
|
54
55
|
|
|
55
56
|
if (!resp.ok) {
|
|
56
|
-
const errorMessage = `Error ${resp.status} from server ${host}: ${responseJson
|
|
57
|
+
const errorMessage = `Error ${resp.status} from server ${host}: ${responseJson?.error?.message ?? JSON.stringify(responseJson)}`;
|
|
57
58
|
if (noRetry || (resp.status >= 400 && resp.status < 500)) {
|
|
58
59
|
throw new NoRetryError(errorMessage);
|
|
59
60
|
} else {
|
|
@@ -2,7 +2,7 @@ import { format } from 'util';
|
|
|
2
2
|
|
|
3
3
|
import { type Logger, createLogger } from '../../log/pino-logger.js';
|
|
4
4
|
import { type PromiseWithResolvers, promiseWithResolvers } from '../../promise/utils.js';
|
|
5
|
-
import { type ApiSchema, type ApiSchemaFor, schemaHasMethod } from '../../schemas/api.js';
|
|
5
|
+
import { type ApiSchema, type ApiSchemaFor, getSchemaReturnType, schemaHasMethod } from '../../schemas/api.js';
|
|
6
6
|
import { jsonStringify } from '../convert.js';
|
|
7
7
|
import { type JsonRpcFetch, defaultFetch } from './fetch.js';
|
|
8
8
|
|
|
@@ -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) {
|
|
@@ -234,7 +236,7 @@ export function createSafeJsonRpcClient<T extends object>(
|
|
|
234
236
|
if ([null, undefined, 'null', 'undefined'].includes(response.result)) {
|
|
235
237
|
return;
|
|
236
238
|
}
|
|
237
|
-
return (schema as ApiSchema)[methodName]
|
|
239
|
+
return getSchemaReturnType((schema as ApiSchema)[methodName]).parseAsync(response.result);
|
|
238
240
|
};
|
|
239
241
|
|
|
240
242
|
const clientId = nextClientId++;
|
|
@@ -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,21 @@ 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'];
|
|
47
|
+
let responseText: string;
|
|
34
48
|
try {
|
|
35
|
-
|
|
49
|
+
if (contentEncoding === 'gzip') {
|
|
50
|
+
const jsonBuffer = await gunzip(await resp.body.arrayBuffer());
|
|
51
|
+
responseText = jsonBuffer.toString('utf-8');
|
|
52
|
+
} else {
|
|
53
|
+
responseText = await resp.body.text();
|
|
54
|
+
}
|
|
55
|
+
responseJson = JSON.parse(responseText);
|
|
36
56
|
} catch {
|
|
37
57
|
if (!responseOk) {
|
|
38
58
|
throw new Error('HTTP ' + resp.statusCode);
|
|
39
59
|
}
|
|
40
|
-
throw new Error(`Failed to parse body as JSON: ${
|
|
60
|
+
throw new Error(`Failed to parse body as JSON. encoding: ${contentEncoding}, body: ${responseText!}`);
|
|
41
61
|
}
|
|
42
62
|
|
|
43
63
|
if (!responseOk) {
|
|
@@ -130,14 +130,14 @@ export class TestState implements TestStateApi {
|
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
export const TestStateSchema: ApiSchemaFor<TestStateApi> = {
|
|
133
|
-
getNote: z.function(
|
|
134
|
-
getNotes: z.function(
|
|
135
|
-
getNotes2: z.function(
|
|
136
|
-
getNotes3: z.function(
|
|
137
|
-
clear: z.function(
|
|
138
|
-
addNotes: z.function(
|
|
139
|
-
fail: z.function(
|
|
140
|
-
count: z.function(
|
|
141
|
-
getStatus: z.function(
|
|
142
|
-
getTuple: z.function(
|
|
133
|
+
getNote: z.function({ input: z.tuple([z.number()]), output: TestNote.schema.optional() }),
|
|
134
|
+
getNotes: z.function({ input: z.tuple([optional(schemas.Integer)]), output: z.array(TestNote.schema) }),
|
|
135
|
+
getNotes2: z.function({ input: z.tuple([optional(schemas.BigInt)]), output: z.array(TestNote.schema) }),
|
|
136
|
+
getNotes3: z.function({ input: z.tuple([optional(schemas.Integer)]), output: z.array(TestNote.schema) }),
|
|
137
|
+
clear: z.function({ input: z.tuple([]), output: z.void() }),
|
|
138
|
+
addNotes: z.function({ input: z.tuple([z.array(TestNote.schema)]), output: z.array(TestNote.schema) }),
|
|
139
|
+
fail: z.function({ input: z.tuple([]), output: z.void() }),
|
|
140
|
+
count: z.function({ input: z.tuple([]), output: z.number() }),
|
|
141
|
+
getStatus: z.function({ input: z.tuple([]), output: z.object({ status: z.string(), count: schemas.BigInt }) }),
|
|
142
|
+
getTuple: z.function({ input: z.tuple([]), output: z.tuple([z.string(), optional(z.string()), z.number()]) }),
|
|
143
143
|
};
|
|
@@ -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
|
+
}
|