@aztec/foundation 0.0.1-commit.993d52e → 0.0.1-commit.9a89641
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/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 +11 -1
- package/dest/collection/array.d.ts.map +1 -1
- package/dest/collection/array.js +33 -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 +72 -71
- package/dest/config/network_config.d.ts +13 -45
- package/dest/config/network_config.d.ts.map +1 -1
- package/dest/config/network_config.js +2 -0
- 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 -1
- package/dest/crypto/poseidon/index.d.ts.map +1 -1
- package/dest/crypto/poseidon/index.js +40 -33
- 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/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 +23 -19
- package/dest/curves/bn254/field.d.ts.map +1 -1
- package/dest/curves/bn254/field.js +23 -17
- 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 +2 -2
- package/dest/eth-signature/eth_signature.d.ts.map +1 -1
- 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 +24 -0
- 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 +1 -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 +5 -4
- 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 +5 -3
- 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/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 +32 -14
- package/dest/log/aws-logger-config.d.ts +8 -0
- package/dest/log/aws-logger-config.d.ts.map +1 -0
- package/dest/log/aws-logger-config.js +55 -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 +29 -8
- 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/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/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 +1 -1
- package/dest/serialize/buffer_reader.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.js +5 -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 +1 -32
- package/dest/serialize/serialize.d.ts.map +1 -1
- package/dest/serialize/serialize.js +0 -21
- 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.d.ts +3 -1
- package/dest/string/index.d.ts.map +1 -1
- package/dest/string/index.js +13 -1
- package/dest/testing/files/index.d.ts +3 -4
- package/dest/testing/files/index.d.ts.map +1 -1
- package/dest/testing/files/index.js +3 -4
- 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/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/trees/balanced_merkle_tree_root.d.ts +2 -3
- package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -1
- package/dest/trees/balanced_merkle_tree_root.js +2 -3
- package/dest/trees/hasher.d.ts +3 -2
- package/dest/trees/hasher.d.ts.map +1 -1
- package/dest/trees/hasher.js +5 -5
- 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 -6
- package/dest/trees/membership_witness.d.ts.map +1 -1
- package/dest/trees/membership_witness.js +0 -9
- 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 +13 -14
- package/dest/trees/sibling_path.d.ts.map +1 -1
- package/dest/trees/sibling_path.js +23 -28
- 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 +23 -1
- package/dest/types/index.d.ts.map +1 -1
- package/dest/types/index.js +15 -0
- package/package.json +6 -4
- 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 +34 -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 +63 -25
- package/src/config/index.ts +143 -122
- package/src/config/network_config.ts +2 -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 -34
- package/src/crypto/schnorr/index.ts +9 -8
- package/src/crypto/schnorr/signature.ts +17 -46
- package/src/curves/bls12/field.ts +0 -7
- package/src/curves/bn254/field.ts +35 -34
- package/src/curves/grumpkin/point.ts +23 -40
- package/src/eth-address/index.ts +0 -4
- 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 +27 -0
- package/src/json-rpc/client/fetch.ts +4 -3
- package/src/json-rpc/client/safe_json_rpc_client.ts +5 -4
- package/src/json-rpc/client/undici.ts +5 -3
- package/src/json-rpc/fixtures/test_state.ts +10 -10
- package/src/json-rpc/server/safe_json_rpc_server.ts +36 -14
- package/src/log/aws-logger-config.ts +32 -0
- package/src/log/pino-logger.ts +33 -6
- package/src/noir/noir_package_config.ts +32 -20
- package/src/promise/running-promise.ts +8 -5
- package/src/queue/batch_queue.ts +1 -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 +5 -0
- package/src/serialize/buffer_sink.ts +350 -0
- package/src/serialize/index.ts +1 -0
- package/src/serialize/serialize.ts +0 -50
- package/src/sleep/index.ts +10 -1
- package/src/string/index.ts +15 -1
- package/src/testing/files/index.ts +3 -4
- package/src/timer/date.ts +6 -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/trees/balanced_merkle_tree_root.ts +2 -5
- package/src/trees/hasher.ts +6 -3
- package/src/trees/indexed_merkle_tree_calculator.ts +5 -1
- package/src/trees/membership_witness.ts +0 -8
- package/src/trees/merkle_tree_calculator.ts +4 -10
- package/src/trees/sibling_path.ts +24 -30
- package/src/trees/unbalanced_merkle_tree_root.ts +2 -5
- package/src/types/index.ts +47 -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,8 +1,7 @@
|
|
|
1
|
-
import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
|
|
2
|
-
|
|
3
|
-
import type { AsyncHasher } from './hasher.js';
|
|
4
1
|
import { MerkleTree } from './merkle_tree.js';
|
|
5
2
|
|
|
3
|
+
type MerkleHashFn = (left: Buffer, right: Buffer) => Promise<Buffer<ArrayBuffer>>;
|
|
4
|
+
|
|
6
5
|
/**
|
|
7
6
|
* Merkle tree calculator.
|
|
8
7
|
*/
|
|
@@ -10,17 +9,12 @@ export class MerkleTreeCalculator {
|
|
|
10
9
|
private constructor(
|
|
11
10
|
private height: number,
|
|
12
11
|
private zeroHashes: Buffer[],
|
|
13
|
-
private hasher:
|
|
12
|
+
private hasher: MerkleHashFn,
|
|
14
13
|
) {
|
|
15
14
|
this.hasher = hasher;
|
|
16
15
|
}
|
|
17
16
|
|
|
18
|
-
static async create(
|
|
19
|
-
height: number,
|
|
20
|
-
zeroLeaf: Buffer = Buffer.alloc(32),
|
|
21
|
-
hasher = async (left: Buffer, right: Buffer) =>
|
|
22
|
-
(await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>,
|
|
23
|
-
) {
|
|
17
|
+
static async create(height: number, zeroLeaf: Buffer = Buffer.alloc(32), hasher: MerkleHashFn) {
|
|
24
18
|
const zeroHashes = [zeroLeaf];
|
|
25
19
|
for (let i = 0; i < height; i++) {
|
|
26
20
|
zeroHashes.push((await hasher(zeroHashes[i], zeroHashes[i])) as Buffer<ArrayBuffer>);
|
|
@@ -1,16 +1,20 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
1
3
|
import { makeTuple } from '../array/array.js';
|
|
2
|
-
import { poseidon2Hash } from '../crypto/poseidon/index.js';
|
|
3
4
|
import { Fr } from '../curves/bn254/index.js';
|
|
4
5
|
import { schemas } from '../schemas/index.js';
|
|
5
|
-
import {
|
|
6
|
-
type Tuple,
|
|
7
|
-
assertLength,
|
|
8
|
-
deserializeArrayFromVector,
|
|
9
|
-
serializeArrayOfBufferableToVector,
|
|
10
|
-
} from '../serialize/index.js';
|
|
6
|
+
import { BufferReader, type Tuple, assertLength, serializeArrayOfBufferableToVector } from '../serialize/index.js';
|
|
11
7
|
import { bufferToHex, hexToBuffer } from '../string/index.js';
|
|
12
8
|
import type { Hasher } from './hasher.js';
|
|
13
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Upper bound on the elements a serialized sibling path may declare. The deepest protocol tree is 42 levels,
|
|
12
|
+
* and the stacked path proving L2-to-L1 message inclusion spans four unbalanced trees, so this leaves room to
|
|
13
|
+
* spare while keeping a malformed length prefix from driving a large allocation. `stdlib` asserts that it stays
|
|
14
|
+
* above every protocol tree height, which this package cannot check itself without depending on `@aztec/constants`.
|
|
15
|
+
*/
|
|
16
|
+
export const MAX_SIBLING_PATH_LENGTH = 128;
|
|
17
|
+
|
|
14
18
|
/**
|
|
15
19
|
* Contains functionality to compute and serialize/deserialize a sibling path.
|
|
16
20
|
* E.g. Sibling path for a leaf at index 3 in a tree of depth 3 consists of:
|
|
@@ -43,10 +47,14 @@ export class SiblingPath<N extends number> {
|
|
|
43
47
|
}
|
|
44
48
|
|
|
45
49
|
static schemaFor<N extends number>(size: N) {
|
|
46
|
-
return schemas.Buffer.transform(b => SiblingPath.fromBuffer(b) as SiblingPath<N>).
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
return schemas.Buffer.transform(b => SiblingPath.fromBuffer(b) as SiblingPath<N>).superRefine((path, ctx) => {
|
|
51
|
+
if (path.pathSize !== size) {
|
|
52
|
+
ctx.addIssue({
|
|
53
|
+
code: z.ZodIssueCode.custom,
|
|
54
|
+
message: `Expected sibling path size ${size} but got ${path.pathSize}`,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
50
58
|
}
|
|
51
59
|
|
|
52
60
|
toJSON() {
|
|
@@ -113,26 +121,12 @@ export class SiblingPath<N extends number> {
|
|
|
113
121
|
* @param buf - A buffer containing the buffer representation of SiblingPath.
|
|
114
122
|
* @param offset - An offset to start deserializing from.
|
|
115
123
|
* @returns A SiblingPath object.
|
|
124
|
+
* @throws If the length prefix exceeds MAX_SIBLING_PATH_LENGTH or the buffer holds fewer elements than it declares.
|
|
116
125
|
*/
|
|
117
126
|
static fromBuffer<N extends number>(buf: Buffer, offset = 0): SiblingPath<N> {
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Deserializes a SiblingPath object from a slice of a part of a buffer and returns the amount of bytes advanced.
|
|
124
|
-
* @param buf - A buffer representation of the sibling path.
|
|
125
|
-
* @param offset - An offset to start deserializing from.
|
|
126
|
-
* @returns The deserialized sibling path and the number of bytes advanced.
|
|
127
|
-
*/
|
|
128
|
-
static deserialize<N extends number>(buf: Buffer, offset = 0) {
|
|
129
|
-
const deserializePath = (buf: Buffer, offset: number) => ({
|
|
130
|
-
elem: buf.slice(offset, offset + 32),
|
|
131
|
-
adv: 32,
|
|
132
|
-
});
|
|
133
|
-
const { elem, adv } = deserializeArrayFromVector(deserializePath, buf, offset);
|
|
134
|
-
const size = elem.length;
|
|
135
|
-
return { elem: new SiblingPath<N>(size as N, elem), adv };
|
|
127
|
+
const reader = new BufferReader(buf, offset);
|
|
128
|
+
const path = reader.readVector({ fromBuffer: r => r.readBytes(Fr.SIZE_IN_BYTES) }, MAX_SIBLING_PATH_LENGTH);
|
|
129
|
+
return new SiblingPath<N>(path.length as N, path);
|
|
136
130
|
}
|
|
137
131
|
|
|
138
132
|
/**
|
|
@@ -172,7 +166,7 @@ export async function computeRootFromSiblingPath(
|
|
|
172
166
|
leaf: Buffer,
|
|
173
167
|
siblingPath: Buffer[],
|
|
174
168
|
index: number,
|
|
175
|
-
hasher
|
|
169
|
+
hasher: (left: Buffer, right: Buffer) => Promise<Buffer>,
|
|
176
170
|
) {
|
|
177
171
|
let result = leaf;
|
|
178
172
|
for (const sibling of siblingPath) {
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { computeBalancedMerkleTreeRoot, computeBalancedMerkleTreeRootAsync } from './balanced_merkle_tree_root.js';
|
|
2
|
-
import {
|
|
2
|
+
import { shaMerkleHash } from './hasher.js';
|
|
3
3
|
import { UnbalancedMerkleTreeCalculator } from './unbalanced_merkle_tree_calculator.js';
|
|
4
4
|
|
|
5
5
|
export const computeUnbalancedShaRoot = (leaves: Buffer[]) => computeUnbalancedMerkleTreeRoot(leaves, shaMerkleHash);
|
|
6
6
|
|
|
7
|
-
export const computeUnbalancedPoseidonRoot = async (leaves: Buffer[]) =>
|
|
8
|
-
await computeUnbalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);
|
|
9
|
-
|
|
10
7
|
export const computeWonkyShaRoot = (leaves: Buffer[]) => computeWonkyMerkleTreeRoot(leaves);
|
|
11
8
|
|
|
12
9
|
/**
|
|
@@ -67,7 +64,7 @@ export function computeUnbalancedMerkleTreeRoot(
|
|
|
67
64
|
|
|
68
65
|
export async function computeUnbalancedMerkleTreeRootAsync(
|
|
69
66
|
leaves: Buffer[],
|
|
70
|
-
hasher
|
|
67
|
+
hasher: (left: Uint8Array, right: Uint8Array) => Promise<Buffer<ArrayBuffer>>,
|
|
71
68
|
emptyRoot = Buffer.alloc(32),
|
|
72
69
|
): Promise<Buffer> {
|
|
73
70
|
if (!leaves.length) {
|
package/src/types/index.ts
CHANGED
|
@@ -29,11 +29,58 @@ export function isErrorClass<T extends Error>(value: unknown, errorClass: new (.
|
|
|
29
29
|
return value instanceof errorClass || (value instanceof Error && value.name === errorClass.name);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
const MAX_ERR_DEPTH = 10;
|
|
33
|
+
|
|
34
|
+
/** Returns the first error in the cause chain matching the given error class. */
|
|
35
|
+
export function getErrorCause<T extends Error>(err: unknown, errorClass: new (...args: any[]) => T): T | undefined {
|
|
36
|
+
let current = err;
|
|
37
|
+
for (let i = 0; current !== undefined && current !== null && i < MAX_ERR_DEPTH; i++) {
|
|
38
|
+
if (isErrorClass(current, errorClass)) {
|
|
39
|
+
return current;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (typeof current === 'object' && Object.hasOwn(current, 'cause')) {
|
|
43
|
+
current = (current as { cause: unknown }).cause;
|
|
44
|
+
} else {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
32
51
|
/** Resolves a record-like type. Lifted from viem. */
|
|
33
52
|
export type Prettify<T> = {
|
|
34
53
|
[K in keyof T]: T[K];
|
|
35
54
|
} & {};
|
|
36
55
|
|
|
56
|
+
/** Returns a type T based on a flag: T if true, undefined if false, optional otherwise. */
|
|
57
|
+
export type DefineIfFlag<Opts, Key extends keyof Opts, T> = Opts extends {
|
|
58
|
+
[K in Key]: true;
|
|
59
|
+
}
|
|
60
|
+
? T
|
|
61
|
+
: Opts extends { [K in Key]: false }
|
|
62
|
+
? never
|
|
63
|
+
: Opts extends { [K in Key]?: boolean }
|
|
64
|
+
? T | undefined
|
|
65
|
+
: never;
|
|
66
|
+
|
|
67
|
+
/** Returns a type with fields conditionally required based on a flag */
|
|
68
|
+
export type PickIfFlag<
|
|
69
|
+
OptsSchema,
|
|
70
|
+
Opts extends OptsSchema,
|
|
71
|
+
Key extends keyof OptsSchema,
|
|
72
|
+
Field extends object,
|
|
73
|
+
> = Opts extends { [K in Key]: true }
|
|
74
|
+
? Field
|
|
75
|
+
: Opts extends { [K in Key]: false }
|
|
76
|
+
? {}
|
|
77
|
+
: Opts extends { [K in Key]?: boolean }
|
|
78
|
+
? Partial<Field>
|
|
79
|
+
: {};
|
|
80
|
+
|
|
81
|
+
/** Picks only the defined (non-undefined) properties of a type. */
|
|
82
|
+
export type PickDefined<T> = Prettify<Pick<T, { [K in keyof T]: T[K] extends undefined ? never : K }[keyof T]>>;
|
|
83
|
+
|
|
37
84
|
/**
|
|
38
85
|
* Type-safe Event Emitter type
|
|
39
86
|
* @example
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { Buffer } from 'buffer';
|
|
2
|
-
/**
|
|
3
|
-
* For serializing an array of fixed length buffers.
|
|
4
|
-
* TODO move to foundation pkg.
|
|
5
|
-
* @param arr - Array of bufffers.
|
|
6
|
-
* @returns The serialized buffers.
|
|
7
|
-
*/
|
|
8
|
-
export declare function serializeBufferArrayToVector(arr: Buffer[]): Buffer<ArrayBuffer>;
|
|
9
|
-
/**
|
|
10
|
-
* Helper function for deserializeArrayFromVector.
|
|
11
|
-
*/
|
|
12
|
-
type DeserializeFn<T> = (buf: Buffer, offset: number) => {
|
|
13
|
-
/**
|
|
14
|
-
* The deserialized type.
|
|
15
|
-
*/
|
|
16
|
-
elem: T;
|
|
17
|
-
/**
|
|
18
|
-
* How many bytes to advance by.
|
|
19
|
-
*/
|
|
20
|
-
adv: number;
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
* For deserializing numbers to 32-bit little-endian form.
|
|
24
|
-
* TODO move to foundation pkg.
|
|
25
|
-
* @param n - The number.
|
|
26
|
-
* @returns The endian-corrected number.
|
|
27
|
-
*/
|
|
28
|
-
export declare function deserializeArrayFromVector<T>(deserialize: DeserializeFn<T>, vector: Buffer, offset?: number): {
|
|
29
|
-
elem: T[];
|
|
30
|
-
adv: number;
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* For serializing numbers to 32 bit little-endian form.
|
|
34
|
-
* TODO move to foundation pkg.
|
|
35
|
-
* @param n - The number.
|
|
36
|
-
* @returns The endian-corrected number.
|
|
37
|
-
*/
|
|
38
|
-
export declare function numToUInt32LE(n: number, bufferSize?: number): Buffer<ArrayBuffer>;
|
|
39
|
-
/**
|
|
40
|
-
* Deserialize the 256-bit number at address `offset`.
|
|
41
|
-
* @param buf - The buffer.
|
|
42
|
-
* @param offset - The address.
|
|
43
|
-
* @returns The derserialized 256-bit field.
|
|
44
|
-
*/
|
|
45
|
-
export declare function deserializeField(buf: Buffer, offset?: number): {
|
|
46
|
-
elem: Buffer<ArrayBuffer>;
|
|
47
|
-
adv: number;
|
|
48
|
-
};
|
|
49
|
-
export declare function concatenateUint8Arrays(arrayOfUint8Arrays: Uint8Array[]): Uint8Array<ArrayBuffer>;
|
|
50
|
-
export {};
|
|
51
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2VyaWFsaXplLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvY3J5cHRvL3NlcmlhbGl6ZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFFQSxPQUFPLEVBQUUsTUFBTSxFQUFFLE1BQU0sUUFBUSxDQUFDO0FBRWhDOzs7OztHQUtHO0FBQ0gsd0JBQWdCLDRCQUE0QixDQUFDLEdBQUcsRUFBRSxNQUFNLEVBQUUsdUJBSXpEO0FBRUQ7O0dBRUc7QUFDSCxLQUFLLGFBQWEsQ0FBQyxDQUFDLElBQUksQ0FDdEIsR0FBRyxFQUFFLE1BQU0sRUFDWCxNQUFNLEVBQUUsTUFBTSxLQUNYO0lBQ0g7O09BRUc7SUFDSCxJQUFJLEVBQUUsQ0FBQyxDQUFDO0lBQ1I7O09BRUc7SUFDSCxHQUFHLEVBQUUsTUFBTSxDQUFDO0NBQ2IsQ0FBQztBQUVGOzs7OztHQUtHO0FBQ0gsd0JBQWdCLDBCQUEwQixDQUFDLENBQUMsRUFBRSxXQUFXLEVBQUUsYUFBYSxDQUFDLENBQUMsQ0FBQyxFQUFFLE1BQU0sRUFBRSxNQUFNLEVBQUUsTUFBTSxTQUFJOzs7RUFXdEc7QUFFRDs7Ozs7R0FLRztBQUNILHdCQUFnQixhQUFhLENBQUMsQ0FBQyxFQUFFLE1BQU0sRUFBRSxVQUFVLFNBQUksdUJBSXREO0FBRUQ7Ozs7O0dBS0c7QUFDSCx3QkFBZ0IsZ0JBQWdCLENBQUMsR0FBRyxFQUFFLE1BQU0sRUFBRSxNQUFNLFNBQUk7OztFQUd2RDtBQUVELHdCQUFnQixzQkFBc0IsQ0FBQyxrQkFBa0IsRUFBRSxVQUFVLEVBQUUsMkJBU3RFIn0=
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"serialize.d.ts","sourceRoot":"","sources":["../../src/crypto/serialize.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC;;;;;GAKG;AACH,wBAAgB,4BAA4B,CAAC,GAAG,EAAE,MAAM,EAAE,uBAIzD;AAED;;GAEG;AACH,KAAK,aAAa,CAAC,CAAC,IAAI,CACtB,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,MAAM,KACX;IACH;;OAEG;IACH,IAAI,EAAE,CAAC,CAAC;IACR;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,EAAE,WAAW,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,SAAI;;;EAWtG;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,UAAU,SAAI,uBAItD;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,SAAI;;;EAGvD;AAED,wBAAgB,sBAAsB,CAAC,kBAAkB,EAAE,UAAU,EAAE,2BAStE"}
|
package/dest/crypto/serialize.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
// TODO find a new home for this as we move to external bb.js
|
|
2
|
-
// See https://github.com/AztecProtocol/aztec-packages/issues/782
|
|
3
|
-
import { Buffer } from 'buffer';
|
|
4
|
-
/**
|
|
5
|
-
* For serializing an array of fixed length buffers.
|
|
6
|
-
* TODO move to foundation pkg.
|
|
7
|
-
* @param arr - Array of bufffers.
|
|
8
|
-
* @returns The serialized buffers.
|
|
9
|
-
*/ export function serializeBufferArrayToVector(arr) {
|
|
10
|
-
const lengthBuf = Buffer.alloc(4);
|
|
11
|
-
lengthBuf.writeUInt32BE(arr.length, 0);
|
|
12
|
-
return Buffer.concat([
|
|
13
|
-
lengthBuf,
|
|
14
|
-
...arr
|
|
15
|
-
]);
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* For deserializing numbers to 32-bit little-endian form.
|
|
19
|
-
* TODO move to foundation pkg.
|
|
20
|
-
* @param n - The number.
|
|
21
|
-
* @returns The endian-corrected number.
|
|
22
|
-
*/ export function deserializeArrayFromVector(deserialize, vector, offset = 0) {
|
|
23
|
-
let pos = offset;
|
|
24
|
-
const size = vector.readUInt32BE(pos);
|
|
25
|
-
pos += 4;
|
|
26
|
-
const arr = new Array(size);
|
|
27
|
-
for(let i = 0; i < size; ++i){
|
|
28
|
-
const { elem, adv } = deserialize(vector, pos);
|
|
29
|
-
pos += adv;
|
|
30
|
-
arr[i] = elem;
|
|
31
|
-
}
|
|
32
|
-
return {
|
|
33
|
-
elem: arr,
|
|
34
|
-
adv: pos - offset
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* For serializing numbers to 32 bit little-endian form.
|
|
39
|
-
* TODO move to foundation pkg.
|
|
40
|
-
* @param n - The number.
|
|
41
|
-
* @returns The endian-corrected number.
|
|
42
|
-
*/ export function numToUInt32LE(n, bufferSize = 4) {
|
|
43
|
-
const buf = Buffer.alloc(bufferSize);
|
|
44
|
-
buf.writeUInt32LE(n, bufferSize - 4);
|
|
45
|
-
return buf;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Deserialize the 256-bit number at address `offset`.
|
|
49
|
-
* @param buf - The buffer.
|
|
50
|
-
* @param offset - The address.
|
|
51
|
-
* @returns The derserialized 256-bit field.
|
|
52
|
-
*/ export function deserializeField(buf, offset = 0) {
|
|
53
|
-
const adv = 32;
|
|
54
|
-
return {
|
|
55
|
-
elem: buf.slice(offset, offset + adv),
|
|
56
|
-
adv
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
export function concatenateUint8Arrays(arrayOfUint8Arrays) {
|
|
60
|
-
const totalLength = arrayOfUint8Arrays.reduce((prev, curr)=>prev + curr.length, 0);
|
|
61
|
-
const result = new Uint8Array(totalLength);
|
|
62
|
-
let length = 0;
|
|
63
|
-
for (const array of arrayOfUint8Arrays){
|
|
64
|
-
result.set(array, length);
|
|
65
|
-
length += array.length;
|
|
66
|
-
}
|
|
67
|
-
return result;
|
|
68
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents a message object for dispatching function calls.
|
|
3
|
-
* Contains the function name ('fn') and an array of arguments ('args') required to call the target method.
|
|
4
|
-
*/
|
|
5
|
-
export interface DispatchMsg {
|
|
6
|
-
/**
|
|
7
|
-
* Name of the target method to be called.
|
|
8
|
-
*/
|
|
9
|
-
fn: string;
|
|
10
|
-
/**
|
|
11
|
-
* An array of arguments to be passed to the target method.
|
|
12
|
-
*/
|
|
13
|
-
args: any[];
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Creates a dispatch function that calls the target's specified method with provided arguments.
|
|
17
|
-
* The created dispatch function takes a DispatchMsg object as input, which contains the name of
|
|
18
|
-
* the method to be called ('fn') and an array of arguments to be passed to the method ('args').
|
|
19
|
-
*
|
|
20
|
-
* @param targetFn - A function that returns the target object containing the methods to be dispatched.
|
|
21
|
-
* @param log - Optional logging function for debugging purposes.
|
|
22
|
-
* @returns A dispatch function that accepts a DispatchMsg object and calls the target's method with provided arguments.
|
|
23
|
-
*/
|
|
24
|
-
export declare function createDispatchFn(targetFn: () => any, log?: import("../../log/pino-logger.js").Logger): ({ fn, args }: DispatchMsg) => Promise<any>;
|
|
25
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY3JlYXRlX2Rpc3BhdGNoX2ZuLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvdHJhbnNwb3J0L2Rpc3BhdGNoL2NyZWF0ZV9kaXNwYXRjaF9mbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFJQTs7O0dBR0c7QUFDSCxNQUFNLFdBQVcsV0FBVztJQUMxQjs7T0FFRztJQUNILEVBQUUsRUFBRSxNQUFNLENBQUM7SUFDWDs7T0FFRztJQUNILElBQUksRUFBRSxHQUFHLEVBQUUsQ0FBQztDQUNiO0FBRUQ7Ozs7Ozs7O0dBUUc7QUFDSCx3QkFBZ0IsZ0JBQWdCLENBQUMsUUFBUSxFQUFFLE1BQU0sR0FBRyxFQUFFLEdBQUcsNENBQXNDLCtDQU05RiJ9
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"create_dispatch_fn.d.ts","sourceRoot":"","sources":["../../../src/transport/dispatch/create_dispatch_fn.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IACX;;OAEG;IACH,IAAI,EAAE,GAAG,EAAE,CAAC;CACb;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,EAAE,GAAG,4CAAsC,+CAM9F"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { format } from 'util';
|
|
2
|
-
import { createLogger } from '../../log/index.js';
|
|
3
|
-
/**
|
|
4
|
-
* Creates a dispatch function that calls the target's specified method with provided arguments.
|
|
5
|
-
* The created dispatch function takes a DispatchMsg object as input, which contains the name of
|
|
6
|
-
* the method to be called ('fn') and an array of arguments to be passed to the method ('args').
|
|
7
|
-
*
|
|
8
|
-
* @param targetFn - A function that returns the target object containing the methods to be dispatched.
|
|
9
|
-
* @param log - Optional logging function for debugging purposes.
|
|
10
|
-
* @returns A dispatch function that accepts a DispatchMsg object and calls the target's method with provided arguments.
|
|
11
|
-
*/ export function createDispatchFn(targetFn, log = createLogger('foundation:dispatch')) {
|
|
12
|
-
return async ({ fn, args })=>{
|
|
13
|
-
const target = targetFn();
|
|
14
|
-
log.debug(format(`dispatching to ${target}: ${fn}`, args));
|
|
15
|
-
return await target[fn](...args);
|
|
16
|
-
};
|
|
17
|
-
}
|
package/src/crypto/serialize.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
// TODO find a new home for this as we move to external bb.js
|
|
2
|
-
// See https://github.com/AztecProtocol/aztec-packages/issues/782
|
|
3
|
-
import { Buffer } from 'buffer';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* For serializing an array of fixed length buffers.
|
|
7
|
-
* TODO move to foundation pkg.
|
|
8
|
-
* @param arr - Array of bufffers.
|
|
9
|
-
* @returns The serialized buffers.
|
|
10
|
-
*/
|
|
11
|
-
export function serializeBufferArrayToVector(arr: Buffer[]) {
|
|
12
|
-
const lengthBuf = Buffer.alloc(4);
|
|
13
|
-
lengthBuf.writeUInt32BE(arr.length, 0);
|
|
14
|
-
return Buffer.concat([lengthBuf, ...arr]);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Helper function for deserializeArrayFromVector.
|
|
19
|
-
*/
|
|
20
|
-
type DeserializeFn<T> = (
|
|
21
|
-
buf: Buffer,
|
|
22
|
-
offset: number,
|
|
23
|
-
) => {
|
|
24
|
-
/**
|
|
25
|
-
* The deserialized type.
|
|
26
|
-
*/
|
|
27
|
-
elem: T;
|
|
28
|
-
/**
|
|
29
|
-
* How many bytes to advance by.
|
|
30
|
-
*/
|
|
31
|
-
adv: number;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* For deserializing numbers to 32-bit little-endian form.
|
|
36
|
-
* TODO move to foundation pkg.
|
|
37
|
-
* @param n - The number.
|
|
38
|
-
* @returns The endian-corrected number.
|
|
39
|
-
*/
|
|
40
|
-
export function deserializeArrayFromVector<T>(deserialize: DeserializeFn<T>, vector: Buffer, offset = 0) {
|
|
41
|
-
let pos = offset;
|
|
42
|
-
const size = vector.readUInt32BE(pos);
|
|
43
|
-
pos += 4;
|
|
44
|
-
const arr = new Array<T>(size);
|
|
45
|
-
for (let i = 0; i < size; ++i) {
|
|
46
|
-
const { elem, adv } = deserialize(vector, pos);
|
|
47
|
-
pos += adv;
|
|
48
|
-
arr[i] = elem;
|
|
49
|
-
}
|
|
50
|
-
return { elem: arr, adv: pos - offset };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* For serializing numbers to 32 bit little-endian form.
|
|
55
|
-
* TODO move to foundation pkg.
|
|
56
|
-
* @param n - The number.
|
|
57
|
-
* @returns The endian-corrected number.
|
|
58
|
-
*/
|
|
59
|
-
export function numToUInt32LE(n: number, bufferSize = 4) {
|
|
60
|
-
const buf = Buffer.alloc(bufferSize);
|
|
61
|
-
buf.writeUInt32LE(n, bufferSize - 4);
|
|
62
|
-
return buf;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Deserialize the 256-bit number at address `offset`.
|
|
67
|
-
* @param buf - The buffer.
|
|
68
|
-
* @param offset - The address.
|
|
69
|
-
* @returns The derserialized 256-bit field.
|
|
70
|
-
*/
|
|
71
|
-
export function deserializeField(buf: Buffer, offset = 0) {
|
|
72
|
-
const adv = 32;
|
|
73
|
-
return { elem: buf.slice(offset, offset + adv), adv };
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function concatenateUint8Arrays(arrayOfUint8Arrays: Uint8Array[]) {
|
|
77
|
-
const totalLength = arrayOfUint8Arrays.reduce((prev, curr) => prev + curr.length, 0);
|
|
78
|
-
const result = new Uint8Array(totalLength);
|
|
79
|
-
let length = 0;
|
|
80
|
-
for (const array of arrayOfUint8Arrays) {
|
|
81
|
-
result.set(array, length);
|
|
82
|
-
length += array.length;
|
|
83
|
-
}
|
|
84
|
-
return result;
|
|
85
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { format } from 'util';
|
|
2
|
-
|
|
3
|
-
import { createLogger } from '../../log/index.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Represents a message object for dispatching function calls.
|
|
7
|
-
* Contains the function name ('fn') and an array of arguments ('args') required to call the target method.
|
|
8
|
-
*/
|
|
9
|
-
export interface DispatchMsg {
|
|
10
|
-
/**
|
|
11
|
-
* Name of the target method to be called.
|
|
12
|
-
*/
|
|
13
|
-
fn: string;
|
|
14
|
-
/**
|
|
15
|
-
* An array of arguments to be passed to the target method.
|
|
16
|
-
*/
|
|
17
|
-
args: any[];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Creates a dispatch function that calls the target's specified method with provided arguments.
|
|
22
|
-
* The created dispatch function takes a DispatchMsg object as input, which contains the name of
|
|
23
|
-
* the method to be called ('fn') and an array of arguments to be passed to the method ('args').
|
|
24
|
-
*
|
|
25
|
-
* @param targetFn - A function that returns the target object containing the methods to be dispatched.
|
|
26
|
-
* @param log - Optional logging function for debugging purposes.
|
|
27
|
-
* @returns A dispatch function that accepts a DispatchMsg object and calls the target's method with provided arguments.
|
|
28
|
-
*/
|
|
29
|
-
export function createDispatchFn(targetFn: () => any, log = createLogger('foundation:dispatch')) {
|
|
30
|
-
return async ({ fn, args }: DispatchMsg) => {
|
|
31
|
-
const target = targetFn();
|
|
32
|
-
log.debug(format(`dispatching to ${target}: ${fn}`, args));
|
|
33
|
-
return await target[fn](...args);
|
|
34
|
-
};
|
|
35
|
-
}
|