@aztec/foundation 0.0.1-commit.023c3e5 → 0.0.1-commit.031e54b
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 +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 +47 -30
- package/dest/config/index.d.ts.map +1 -1
- package/dest/config/index.js +44 -44
- 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 +9 -176
- package/dest/crypto/bls/bn254_keystore.d.ts.map +1 -1
- package/dest/crypto/bls/bn254_keystore.js +0 -17
- 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/crypto/secp256k1-signer/utils.d.ts +12 -1
- package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -1
- package/dest/crypto/secp256k1-signer/utils.js +26 -0
- package/dest/curves/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 +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 +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 +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/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 +27 -13
- 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/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.d.ts +1 -1
- package/dest/log/pino-logger.d.ts.map +1 -1
- package/dest/log/pino-logger.js +23 -4
- 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/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/retry/index.d.ts +12 -1
- package/dest/retry/index.d.ts.map +1 -1
- package/dest/retry/index.js +21 -0
- 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/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 +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 -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 +23 -1
- package/dest/types/index.d.ts.map +1 -1
- package/dest/types/index.js +15 -0
- package/package.json +26 -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 +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 +69 -22
- package/src/config/index.ts +116 -96
- 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 +0 -23
- 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/crypto/secp256k1-signer/utils.ts +32 -0
- 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 +27 -0
- 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 +5 -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 +31 -13
- package/src/log/bigint-utils.ts +25 -0
- package/src/log/gcloud-logger-config.ts +5 -0
- package/src/log/log-filters.ts +29 -11
- package/src/log/pino-logger.ts +25 -4
- package/src/noir/noir_package_config.ts +32 -20
- package/src/queue/base_memory_queue.ts +1 -1
- package/src/queue/batch_queue.ts +1 -0
- package/src/retry/index.ts +30 -0
- 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/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 +2 -5
- package/src/trees/hasher.ts +6 -3
- 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 +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
package/src/schemas/api.ts
CHANGED
|
@@ -2,32 +2,19 @@ import type { z } from 'zod';
|
|
|
2
2
|
|
|
3
3
|
import type { ZodNullableOptional } from './utils.js';
|
|
4
4
|
|
|
5
|
-
// Forces usage of
|
|
6
|
-
type ZodParameterTypeFor<T> = undefined extends T
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
type
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// The complexity is required to satisfy ZodTuple which requires a fixed length tuple and
|
|
14
|
-
// has a very annoying type of [] | [ZodTypeAny, ...ZodTypeAny], and most types fail to match
|
|
15
|
-
// the second option. While a purely recursive approach works, it fails when trying to deal
|
|
16
|
-
// with optional arguments (ie optional items in the tuple), and ts does not really like them
|
|
17
|
-
// during a recursion and fails with infinite stack depth.
|
|
18
|
-
// This type appears to satisfy everyone. Everyone but me.
|
|
19
|
-
type ZodMapParameterTypes<T> = T extends []
|
|
20
|
-
? []
|
|
21
|
-
: T extends [item: infer Head, ...infer Rest]
|
|
22
|
-
? [ZodParameterTypeFor<Head>, ...{ [K in keyof Rest]: ZodParameterTypeFor<Rest[K]> }]
|
|
23
|
-
: T extends [item?: infer Head, ...infer Rest]
|
|
24
|
-
? [ZodNullableOptional<ZodParameterTypeFor<Head>>, ...{ [K in keyof Rest]: ZodParameterTypeFor<Rest[K]> }]
|
|
25
|
-
: never;
|
|
5
|
+
// Forces usage of schemas that accept nulls for optional parameters.
|
|
6
|
+
type ZodParameterTypeFor<T> = undefined extends T ? ZodNullableOptional<z.ZodType<T, any>> : z.ZodType<T, any>;
|
|
7
|
+
|
|
8
|
+
type ZodReturnTypeFor<T> = z.ZodType<T, any>;
|
|
9
|
+
|
|
10
|
+
type ZodMapParameterTypes<T extends readonly unknown[]> = {
|
|
11
|
+
[K in keyof T]-?: ZodParameterTypeFor<T[K]>;
|
|
12
|
+
};
|
|
26
13
|
|
|
27
14
|
/** Maps all functions in an interface to their schema representation. */
|
|
28
15
|
export type ApiSchemaFor<T> = {
|
|
29
16
|
[K in keyof T]: T[K] extends (...args: infer Args) => Promise<infer Ret>
|
|
30
|
-
? z.ZodFunction<z.ZodTuple<ZodMapParameterTypes<Args>,
|
|
17
|
+
? z.ZodFunction<z.ZodTuple<ZodMapParameterTypes<Args>, null>, ZodReturnTypeFor<Ret>>
|
|
31
18
|
: never;
|
|
32
19
|
};
|
|
33
20
|
|
|
@@ -37,11 +24,24 @@ export type ApiSchema = {
|
|
|
37
24
|
};
|
|
38
25
|
|
|
39
26
|
/** Return whether an API schema defines a valid function schema for a given method name. */
|
|
40
|
-
export function schemaHasMethod
|
|
27
|
+
export function schemaHasMethod<T extends ApiSchema>(
|
|
28
|
+
schema: T,
|
|
29
|
+
methodName: string,
|
|
30
|
+
): methodName is Extract<keyof T, string> {
|
|
41
31
|
return (
|
|
42
32
|
typeof methodName === 'string' &&
|
|
43
33
|
Object.hasOwn(schema, methodName) &&
|
|
44
|
-
|
|
45
|
-
|
|
34
|
+
schema[methodName].def.input !== undefined &&
|
|
35
|
+
schema[methodName].def.output !== undefined
|
|
46
36
|
);
|
|
47
37
|
}
|
|
38
|
+
|
|
39
|
+
/** Returns the parameter tuple schema for an API method schema. */
|
|
40
|
+
export function getSchemaParameters<T extends z.ZodFunction<z.ZodTuple<any, any>, z.ZodTypeAny>>(schema: T) {
|
|
41
|
+
return schema.def.input;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Returns the return value schema for an API method schema. */
|
|
45
|
+
export function getSchemaReturnType<T extends z.ZodFunction<z.ZodTuple<any, any>, z.ZodTypeAny>>(schema: T) {
|
|
46
|
+
return schema.def.output;
|
|
47
|
+
}
|
package/src/schemas/parse.ts
CHANGED
|
@@ -4,16 +4,19 @@ import { times } from '../collection/array.js';
|
|
|
4
4
|
|
|
5
5
|
/** Parses the given arguments using a tuple from the provided schemas. */
|
|
6
6
|
export function parse<T extends [] | [z.ZodTypeAny, ...z.ZodTypeAny[]]>(args: IArguments, ...schemas: T) {
|
|
7
|
-
return
|
|
7
|
+
return schemas.length === 0
|
|
8
|
+
? z.tuple([]).parse(args)
|
|
9
|
+
: z.tuple(schemas as [z.ZodTypeAny, ...z.ZodTypeAny[]]).parse(args);
|
|
8
10
|
}
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Parses the given arguments against a tuple, allowing empty for optional items.
|
|
12
|
-
* @dev Zod doesn't like
|
|
14
|
+
* @dev Zod doesn't like tuples with optional items. See https://github.com/colinhacks/zod/discussions/949.
|
|
13
15
|
*/
|
|
14
|
-
export function parseWithOptionals<T extends z.
|
|
15
|
-
const
|
|
16
|
-
const
|
|
16
|
+
export function parseWithOptionals<T extends z.ZodTuple<any, any>>(args: any[], schema: T): Promise<z.output<T>> {
|
|
17
|
+
const items = schema.def.items;
|
|
18
|
+
const missingCount = items.length - args.length;
|
|
19
|
+
const optionalCount = items.filter(isOptional).length;
|
|
17
20
|
const toParse =
|
|
18
21
|
missingCount > 0 && missingCount <= optionalCount ? args.concat(times(missingCount, () => undefined)) : args;
|
|
19
22
|
return schema.parseAsync(toParse);
|
|
@@ -24,6 +27,6 @@ function isOptional(schema: z.ZodTypeAny) {
|
|
|
24
27
|
return schema.isOptional();
|
|
25
28
|
} catch {
|
|
26
29
|
// See https://github.com/colinhacks/zod/issues/1911
|
|
27
|
-
return schema.
|
|
30
|
+
return schema.def.type === 'optional';
|
|
28
31
|
}
|
|
29
32
|
}
|
package/src/schemas/schemas.ts
CHANGED
|
@@ -44,15 +44,15 @@ export const schemas = {
|
|
|
44
44
|
]),
|
|
45
45
|
|
|
46
46
|
/** Coerces any input to bigint. */
|
|
47
|
-
BigInt: z.union([z.bigint(), z.number(), z.string()]).pipe(z.coerce.bigint()),
|
|
47
|
+
BigInt: z.union([z.bigint(), z.number(), z.string()]).pipe(z.coerce.bigint<string | number | bigint>()),
|
|
48
48
|
|
|
49
49
|
/** Coerces any input to integer number. */
|
|
50
|
-
Integer: z.union([z.bigint(), z.number(), z.string()]).pipe(z.coerce.number().int()),
|
|
50
|
+
Integer: z.union([z.bigint(), z.number(), z.string()]).pipe(z.coerce.number<string | number | bigint>().int()),
|
|
51
51
|
|
|
52
52
|
/** Coerces input to UInt32. */
|
|
53
53
|
UInt32: z.union([z.bigint(), z.number(), z.string()]).pipe(
|
|
54
54
|
z.coerce
|
|
55
|
-
.number()
|
|
55
|
+
.number<string | number | bigint>()
|
|
56
56
|
.int()
|
|
57
57
|
.min(0)
|
|
58
58
|
.max(2 ** 32 - 1),
|
|
@@ -61,7 +61,7 @@ export const schemas = {
|
|
|
61
61
|
/** Coerces input to UInt64. */
|
|
62
62
|
UInt64: z.union([z.bigint(), z.number(), z.string()]).pipe(
|
|
63
63
|
z.coerce
|
|
64
|
-
.bigint()
|
|
64
|
+
.bigint<string | number | bigint>()
|
|
65
65
|
.min(0n)
|
|
66
66
|
.max(2n ** 64n - 1n),
|
|
67
67
|
),
|
package/src/schemas/types.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ZodType } from 'zod';
|
|
2
2
|
|
|
3
|
-
export type ZodFor<T> = ZodType<T, any
|
|
3
|
+
export type ZodFor<T> = ZodType<T, any>;
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Creates a schema validator that enforces all properties of type T are present in the schema.
|
|
@@ -26,7 +26,7 @@ export type ZodFor<T> = ZodType<T, any, any>;
|
|
|
26
26
|
* ```
|
|
27
27
|
*/
|
|
28
28
|
export function zodFor<T>() {
|
|
29
|
-
return (schema => schema) as <S extends ZodType<any, any
|
|
29
|
+
return (schema => schema) as <S extends ZodType<any, any>>(
|
|
30
30
|
schema: keyof T extends keyof S['_output']
|
|
31
31
|
? keyof S['_output'] extends keyof T
|
|
32
32
|
? S
|
package/src/schemas/utils.ts
CHANGED
|
@@ -1,16 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
OK,
|
|
3
|
-
type ParseInput,
|
|
4
|
-
type ParseReturnType,
|
|
5
|
-
ZodFirstPartyTypeKind,
|
|
6
|
-
type ZodObject,
|
|
7
|
-
ZodOptional,
|
|
8
|
-
ZodParsedType,
|
|
9
|
-
type ZodRawShape,
|
|
10
|
-
type ZodType,
|
|
11
|
-
type ZodTypeAny,
|
|
12
|
-
z,
|
|
13
|
-
} from 'zod';
|
|
1
|
+
import { type ZodObject, type ZodRawShape, type ZodType, type ZodTypeAny, z } from 'zod';
|
|
14
2
|
|
|
15
3
|
import { pick } from '../collection/object.js';
|
|
16
4
|
import { isHex, withoutHexPrefix } from '../string/index.js';
|
|
@@ -41,24 +29,7 @@ export const bufferSchema: ZodFor<Buffer> = z.union([
|
|
|
41
29
|
z.instanceof(Buffer),
|
|
42
30
|
]);
|
|
43
31
|
|
|
44
|
-
export
|
|
45
|
-
_isNullableOptional = true;
|
|
46
|
-
|
|
47
|
-
override _parse(input: ParseInput): ParseReturnType<this['_output']> {
|
|
48
|
-
const parsedType = this._getType(input);
|
|
49
|
-
if (parsedType === ZodParsedType.undefined || parsedType === ZodParsedType.null) {
|
|
50
|
-
return OK(undefined);
|
|
51
|
-
}
|
|
52
|
-
return this._def.innerType._parse(input);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
static override create<T extends ZodTypeAny>(type: T): ZodNullableOptional<T> {
|
|
56
|
-
return new ZodNullableOptional({
|
|
57
|
-
innerType: type,
|
|
58
|
-
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
59
|
-
}) as any;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
32
|
+
export type ZodNullableOptional<T extends ZodTypeAny> = ZodType<z.output<T> | undefined, z.input<T> | null | undefined>;
|
|
62
33
|
|
|
63
34
|
/**
|
|
64
35
|
* Declares a parameter as optional. Use this over z.optional in order to accept nulls as undefineds.
|
|
@@ -66,7 +37,7 @@ export class ZodNullableOptional<T extends ZodTypeAny> extends ZodOptional<T> {
|
|
|
66
37
|
* need to convert nulls to undefineds as we parse.
|
|
67
38
|
*/
|
|
68
39
|
export function optional<T extends ZodTypeAny>(schema: T) {
|
|
69
|
-
return
|
|
40
|
+
return schema.nullish().transform(value => value ?? undefined);
|
|
70
41
|
}
|
|
71
42
|
|
|
72
43
|
type ToJsonIs<T, TRet> = T extends { toJSON(): TRet } ? T : never;
|
|
@@ -83,7 +54,6 @@ export function hexSchemaFor<TClass extends { fromString(str: string): any } | {
|
|
|
83
54
|
TClass extends { fromString(str: string): infer TInstance } | { fromBuffer(buf: Buffer): infer TInstance }
|
|
84
55
|
? ToJsonIs<TInstance, string>
|
|
85
56
|
: never,
|
|
86
|
-
any,
|
|
87
57
|
string
|
|
88
58
|
> {
|
|
89
59
|
const stringSchema = refinement ? z.string().refine(refinement, `Not a valid instance`) : z.string();
|
|
@@ -101,11 +71,7 @@ export function hexSchemaFor<TClass extends { fromString(str: string): any } | {
|
|
|
101
71
|
export function bufferSchemaFor<TClass extends { fromBuffer(buf: Buffer): any }>(
|
|
102
72
|
klazz: TClass,
|
|
103
73
|
refinement?: (buf: Buffer) => boolean,
|
|
104
|
-
): ZodType<
|
|
105
|
-
TClass extends { fromBuffer(buf: Buffer): infer TInstance } ? ToJsonIs<TInstance, Buffer> : never,
|
|
106
|
-
any,
|
|
107
|
-
string
|
|
108
|
-
> {
|
|
74
|
+
): ZodType<TClass extends { fromBuffer(buf: Buffer): infer TInstance } ? ToJsonIs<TInstance, Buffer> : never, string> {
|
|
109
75
|
return bufferSchema.refine(refinement ?? (() => true), 'Not a valid buffer').transform(klazz.fromBuffer.bind(klazz));
|
|
110
76
|
}
|
|
111
77
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { toBigIntBE } from '../bigint-buffer/index.js';
|
|
1
2
|
import type { Tuple } from './types.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -130,6 +131,20 @@ export class BufferReader {
|
|
|
130
131
|
return result;
|
|
131
132
|
}
|
|
132
133
|
|
|
134
|
+
/**
|
|
135
|
+
* Reads a 256-bit signed integer (two's complement) from the buffer at the current index position.
|
|
136
|
+
* Updates the index position by 32 bytes after reading the number.
|
|
137
|
+
*
|
|
138
|
+
* @returns The read 256 bit signed value as a bigint.
|
|
139
|
+
*/
|
|
140
|
+
public readInt256(): bigint {
|
|
141
|
+
this.#rangeCheck(32);
|
|
142
|
+
const unsigned = toBigIntBE(this.buffer.subarray(this.index, this.index + 32));
|
|
143
|
+
this.index += 32;
|
|
144
|
+
const signBit = 1n << 255n;
|
|
145
|
+
return unsigned >= signBit ? unsigned - (1n << 256n) : unsigned;
|
|
146
|
+
}
|
|
147
|
+
|
|
133
148
|
/** Alias for readUInt256 */
|
|
134
149
|
public readBigInt(): bigint {
|
|
135
150
|
return this.readUInt256();
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A value that can be appended to a {@link BufferSink}. Mirrors `Bufferable`, but the object arm allows the
|
|
3
|
+
* optional-sink overload of `toBuffer` so a legacy `toBuffer(): Buffer` and a migrated `toBuffer(sink?)` both fit.
|
|
4
|
+
*/
|
|
5
|
+
export type Sinkable =
|
|
6
|
+
| boolean
|
|
7
|
+
| Buffer
|
|
8
|
+
| Uint8Array
|
|
9
|
+
| number
|
|
10
|
+
| bigint
|
|
11
|
+
| string
|
|
12
|
+
| { toBuffer(sink?: BufferSink): Buffer | void }
|
|
13
|
+
| Sinkable[];
|
|
14
|
+
|
|
15
|
+
const DEFAULT_INITIAL_CAPACITY = 1024;
|
|
16
|
+
const MASK_64 = (1n << 64n) - 1n;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A growable, append-only binary sink backed by a single `ArrayBuffer`.
|
|
20
|
+
*
|
|
21
|
+
* It exists to replace the recursive `toBuffer()` chain — which allocates a `Buffer` at every node and
|
|
22
|
+
* `Buffer.concat`s at every level — with one buffer that the whole object graph streams into and that is sliced
|
|
23
|
+
* exactly once at the root. Encodings are big-endian, matching `serializeToBuffer` byte-for-byte.
|
|
24
|
+
*
|
|
25
|
+
* The migration contract is the optional-sink `toBuffer`:
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* toBuffer(): Buffer;
|
|
29
|
+
* toBuffer(sink: BufferSink): void;
|
|
30
|
+
* toBuffer(sink?: BufferSink): Buffer | void {
|
|
31
|
+
* if (!sink) return BufferSink.serialize(this); // own a sink, fill it, slice once
|
|
32
|
+
* serializeToSink(sink, this.a, this.b, this.c); // stream into the caller's sink
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* When a sink is passed it writes and returns `undefined`; otherwise it returns its own buffer. Nodes that have
|
|
37
|
+
* not been migrated keep their plain `toBuffer(): Buffer` and are folded in via the return-value fallback in
|
|
38
|
+
* {@link serializeToSink}, so the tree stays valid at every step of an incremental migration.
|
|
39
|
+
*
|
|
40
|
+
* Performance note: bigints are written via `DataView.setBigUint64` limbs ({@link writeBigInt},
|
|
41
|
+
* {@link writeField}), NOT a per-byte shift loop. The limb form is ~16x faster than the legacy hex round-trip
|
|
42
|
+
* on field-heavy structures, and faster than the legacy path at every width — a naive per-byte shift loop is
|
|
43
|
+
* actually the slowest option, since each byte allocates a fresh BigInt. See the spike benchmark.
|
|
44
|
+
*/
|
|
45
|
+
export class BufferSink {
|
|
46
|
+
private buffer: Uint8Array;
|
|
47
|
+
private view: DataView;
|
|
48
|
+
private offset = 0;
|
|
49
|
+
|
|
50
|
+
constructor(initialCapacity: number = DEFAULT_INITIAL_CAPACITY) {
|
|
51
|
+
const ab = new ArrayBuffer(initialCapacity);
|
|
52
|
+
this.buffer = new Uint8Array(ab);
|
|
53
|
+
this.view = new DataView(ab);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Number of bytes written so far. */
|
|
57
|
+
public get length(): number {
|
|
58
|
+
return this.offset;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Serialize a migrated value into a fresh sink and return the single resulting buffer. This is the no-sink
|
|
63
|
+
* branch of every migrated `toBuffer`: one allocation, one slice, only at the root.
|
|
64
|
+
*
|
|
65
|
+
* @param obj - The value to serialize via its sink-aware `toBuffer`.
|
|
66
|
+
* @param initialCapacity - Optional size hint (e.g. from `getSize()`) to skip reallocations on hot paths.
|
|
67
|
+
*/
|
|
68
|
+
public static serialize(obj: { toBuffer(sink: BufferSink): void }, initialCapacity?: number): Buffer {
|
|
69
|
+
const sink = new BufferSink(initialCapacity);
|
|
70
|
+
obj.toBuffer(sink);
|
|
71
|
+
return sink.toBuffer();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private ensure(extra: number): void {
|
|
75
|
+
const required = this.offset + extra;
|
|
76
|
+
if (required <= this.buffer.length) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
let next = this.buffer.length * 2;
|
|
80
|
+
while (next < required) {
|
|
81
|
+
next *= 2;
|
|
82
|
+
}
|
|
83
|
+
const grown = new Uint8Array(next);
|
|
84
|
+
grown.set(this.buffer.subarray(0, this.offset));
|
|
85
|
+
this.buffer = grown;
|
|
86
|
+
this.view = new DataView(grown.buffer);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Append a single byte (0/1) for a boolean, matching `boolToBuffer`. */
|
|
90
|
+
public writeBool(value: boolean): void {
|
|
91
|
+
this.ensure(1);
|
|
92
|
+
this.buffer[this.offset++] = value ? 1 : 0;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Append an unsigned 8-bit integer. */
|
|
96
|
+
public writeUInt8(value: number): void {
|
|
97
|
+
this.ensure(1);
|
|
98
|
+
this.buffer[this.offset++] = value & 0xff;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Append a big-endian unsigned 16-bit integer. */
|
|
102
|
+
public writeUInt16(value: number): void {
|
|
103
|
+
this.ensure(2);
|
|
104
|
+
this.view.setUint16(this.offset, value, false);
|
|
105
|
+
this.offset += 2;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Append a big-endian unsigned 32-bit integer, matching `numToUInt32BE`. */
|
|
109
|
+
public writeNumber(value: number): void {
|
|
110
|
+
this.ensure(4);
|
|
111
|
+
this.view.setUint32(this.offset, value, false);
|
|
112
|
+
this.offset += 4;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Append a big-endian unsigned 64-bit integer, matching `bigintToUInt64BE`. */
|
|
116
|
+
public writeUInt64(value: bigint): void {
|
|
117
|
+
this.ensure(8);
|
|
118
|
+
this.view.setBigUint64(this.offset, value, false);
|
|
119
|
+
this.offset += 8;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Append a non-negative bigint as a big-endian unsigned integer of `width` bytes (default 32), matching
|
|
124
|
+
* `serializeBigInt` / `toBufferBE`. Every width is written limb-wise via `setBigUint64` rather than the
|
|
125
|
+
* legacy per-byte BigInt shift loop — that loop allocated a fresh BigInt per byte and benchmarked as the
|
|
126
|
+
* *slowest* option (slower even than the legacy hex round-trip); the limb form is faster at every width.
|
|
127
|
+
*
|
|
128
|
+
* The bulk of the value is written as 64-bit big-endian limbs from the least-significant tail upward, and
|
|
129
|
+
* a final `width % 8` (at most 7) high bytes that don't fill a whole limb are written individually. The
|
|
130
|
+
* common widths (8/16/32) are exact multiples of 8, so they take the pure-limb path with no remainder.
|
|
131
|
+
* `width === 32` keeps its own straight-line four-limb form (shared with {@link writeField}) since it is
|
|
132
|
+
* the `Fr`/`Fq`/Chonk-proof hot path and the unrolled body is marginally faster than the loop.
|
|
133
|
+
*
|
|
134
|
+
* @param value - The non-negative bigint to serialize.
|
|
135
|
+
* @param width - Output width in bytes.
|
|
136
|
+
*/
|
|
137
|
+
public writeBigInt(value: bigint, width = 32): void {
|
|
138
|
+
if (value < 0n) {
|
|
139
|
+
throw new Error(`Cannot serialize negative bigint ${value}`);
|
|
140
|
+
}
|
|
141
|
+
this.ensure(width);
|
|
142
|
+
const o = this.offset;
|
|
143
|
+
if (width === 32) {
|
|
144
|
+
let x = value;
|
|
145
|
+
this.view.setBigUint64(o + 24, x & MASK_64, false);
|
|
146
|
+
x >>= 64n;
|
|
147
|
+
this.view.setBigUint64(o + 16, x & MASK_64, false);
|
|
148
|
+
x >>= 64n;
|
|
149
|
+
this.view.setBigUint64(o + 8, x & MASK_64, false);
|
|
150
|
+
x >>= 64n;
|
|
151
|
+
this.view.setBigUint64(o, x & MASK_64, false);
|
|
152
|
+
x >>= 64n;
|
|
153
|
+
if (x !== 0n) {
|
|
154
|
+
throw new Error(`BigInt ${value} does not fit into 32 bytes`);
|
|
155
|
+
}
|
|
156
|
+
this.offset += 32;
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
let x = value;
|
|
160
|
+
let pos = o + width;
|
|
161
|
+
while (pos - 8 >= o) {
|
|
162
|
+
pos -= 8;
|
|
163
|
+
this.view.setBigUint64(pos, x & MASK_64, false);
|
|
164
|
+
x >>= 64n;
|
|
165
|
+
}
|
|
166
|
+
for (let i = pos - 1; i >= o; i--) {
|
|
167
|
+
this.buffer[i] = Number(x & 0xffn);
|
|
168
|
+
x >>= 8n;
|
|
169
|
+
}
|
|
170
|
+
if (x !== 0n) {
|
|
171
|
+
throw new Error(`BigInt ${value} does not fit into ${width} bytes`);
|
|
172
|
+
}
|
|
173
|
+
this.offset += width;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Append a 32-byte (256-bit) big-endian non-negative bigint. Specialized form of {@link writeBigInt}
|
|
178
|
+
* with no width parameter and no width-branch, so V8 can inline the four `setBigUint64` limb writes
|
|
179
|
+
* without the wider branchy form's instability. This is the Fr/Fq leaf hot path; prefer it over
|
|
180
|
+
* `writeBigInt(value, 32)` in callers that always serialize 32-byte fields.
|
|
181
|
+
*/
|
|
182
|
+
public writeField(value: bigint): void {
|
|
183
|
+
if (value < 0n) {
|
|
184
|
+
throw new Error(`Cannot serialize negative bigint ${value}`);
|
|
185
|
+
}
|
|
186
|
+
this.ensure(32);
|
|
187
|
+
const o = this.offset;
|
|
188
|
+
let x = value;
|
|
189
|
+
this.view.setBigUint64(o + 24, x & MASK_64, false);
|
|
190
|
+
x >>= 64n;
|
|
191
|
+
this.view.setBigUint64(o + 16, x & MASK_64, false);
|
|
192
|
+
x >>= 64n;
|
|
193
|
+
this.view.setBigUint64(o + 8, x & MASK_64, false);
|
|
194
|
+
x >>= 64n;
|
|
195
|
+
this.view.setBigUint64(o, x & MASK_64, false);
|
|
196
|
+
if (x >> 64n !== 0n) {
|
|
197
|
+
throw new Error(`BigInt ${value} does not fit into 32 bytes`);
|
|
198
|
+
}
|
|
199
|
+
this.offset = o + 32;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Append an array of field elements (or any objects exposing `toBigInt(): bigint`) as a contiguous
|
|
204
|
+
* sequence of 32-byte big-endian limbs. Iterates inline — no per-element `toBuffer` dispatch and no
|
|
205
|
+
* per-element function-call indirection through the generic sinkable dispatcher. Use this in classes
|
|
206
|
+
* that hold large flat field arrays (e.g. the 1632-element ChonkProof field list).
|
|
207
|
+
*/
|
|
208
|
+
public writeFields(fields: ReadonlyArray<{ toBigInt(): bigint }>): void {
|
|
209
|
+
this.ensure(32 * fields.length);
|
|
210
|
+
for (let i = 0, n = fields.length; i < n; i++) {
|
|
211
|
+
const value = fields[i].toBigInt();
|
|
212
|
+
if (value < 0n) {
|
|
213
|
+
throw new Error(`Cannot serialize negative bigint ${value}`);
|
|
214
|
+
}
|
|
215
|
+
const o = this.offset;
|
|
216
|
+
let x = value;
|
|
217
|
+
this.view.setBigUint64(o + 24, x & MASK_64, false);
|
|
218
|
+
x >>= 64n;
|
|
219
|
+
this.view.setBigUint64(o + 16, x & MASK_64, false);
|
|
220
|
+
x >>= 64n;
|
|
221
|
+
this.view.setBigUint64(o + 8, x & MASK_64, false);
|
|
222
|
+
x >>= 64n;
|
|
223
|
+
this.view.setBigUint64(o, x & MASK_64, false);
|
|
224
|
+
if (x >> 64n !== 0n) {
|
|
225
|
+
throw new Error(`BigInt ${value} does not fit into 32 bytes`);
|
|
226
|
+
}
|
|
227
|
+
this.offset = o + 32;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Append raw bytes verbatim (no length prefix), matching how a `Buffer`/`Uint8Array` is serialized inline. */
|
|
232
|
+
public writeBytes(bytes: Uint8Array): void {
|
|
233
|
+
this.ensure(bytes.length);
|
|
234
|
+
this.buffer.set(bytes, this.offset);
|
|
235
|
+
this.offset += bytes.length;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Append a string as a 4-byte big-endian length prefix followed by its UTF-8 bytes, matching the string case. */
|
|
239
|
+
public writeString(value: string): void {
|
|
240
|
+
const bytes = Buffer.from(value);
|
|
241
|
+
this.writeNumber(bytes.length);
|
|
242
|
+
this.writeBytes(bytes);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Reset the write cursor without releasing the backing buffer. Lets the same sink be reused across many
|
|
247
|
+
* serializations without reallocating — the per-call cost drops to the bytes written (no `new ArrayBuffer`).
|
|
248
|
+
* Any `Buffer` previously returned by {@link toBuffer} is a fresh copy and remains valid; any view from
|
|
249
|
+
* {@link asUint8Array} aliases the buffer and is invalidated.
|
|
250
|
+
*/
|
|
251
|
+
public reset(): void {
|
|
252
|
+
this.offset = 0;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Copy the written region into a freshly allocated `Buffer`. The single allocation of the whole serialization. */
|
|
256
|
+
public toBuffer(): Buffer {
|
|
257
|
+
return Buffer.from(this.buffer.subarray(0, this.offset));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** A zero-copy view of the written region. Valid only until the next write (which may reallocate). */
|
|
261
|
+
public asUint8Array(): Uint8Array {
|
|
262
|
+
return this.buffer.subarray(0, this.offset);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Streaming counterpart to `serializeToBufferArray`: walk a list of {@link Sinkable}s and append each to the sink.
|
|
268
|
+
* Dispatch matches `serializeToBufferArray` byte-for-byte. An object is serialized via `obj.toBuffer(sink)`; a
|
|
269
|
+
* migrated node writes into the sink and returns `undefined`, while a not-yet-migrated node ignores the argument
|
|
270
|
+
* and returns its `Buffer`, which we copy in. That return-value fallback is what makes the migration incremental.
|
|
271
|
+
*
|
|
272
|
+
* Dispatch is hot-pathed for the common case of objects exposing `toBuffer` (Fr/Fq and every other migrated leaf),
|
|
273
|
+
* and arrays recurse via an inner-array helper to avoid the per-call rest-args allocation that a spread-recurse
|
|
274
|
+
* would force on large flat arrays like the 1632-element ChonkProof field list.
|
|
275
|
+
*
|
|
276
|
+
* @param sink - The destination sink.
|
|
277
|
+
* @param objs - The values to serialize, in order.
|
|
278
|
+
*/
|
|
279
|
+
export function serializeToSink(sink: BufferSink, ...objs: Sinkable[]): void {
|
|
280
|
+
serializeArrayToSinkInner(sink, objs);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function serializeArrayToSinkInner(sink: BufferSink, objs: Sinkable[]): void {
|
|
284
|
+
for (let i = 0, n = objs.length; i < n; i++) {
|
|
285
|
+
serializeOneToSink(sink, objs[i]);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function serializeOneToSink(sink: BufferSink, obj: Sinkable): void {
|
|
290
|
+
if (typeof obj === 'object' && obj !== null) {
|
|
291
|
+
// Hot path: migrated nodes (anything implementing toBuffer, including Fr/Fq via BaseField). This branch
|
|
292
|
+
// is taken for the vast majority of recursive serialization, so it is checked before the typeof primitives.
|
|
293
|
+
if ((obj as any).toBuffer !== undefined) {
|
|
294
|
+
const ret = (obj as { toBuffer(sink: BufferSink): Buffer | void }).toBuffer(sink);
|
|
295
|
+
if (ret !== undefined) {
|
|
296
|
+
sink.writeBytes(ret);
|
|
297
|
+
}
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
if (Array.isArray(obj)) {
|
|
301
|
+
serializeArrayToSinkInner(sink, obj);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (Buffer.isBuffer(obj) || obj instanceof Uint8Array) {
|
|
305
|
+
sink.writeBytes(obj);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
throw new Error(`Cannot serialize input to sink: ${typeof obj} ${(obj as any).constructor?.name}`);
|
|
309
|
+
}
|
|
310
|
+
if (typeof obj === 'boolean') {
|
|
311
|
+
sink.writeBool(obj);
|
|
312
|
+
} else if (typeof obj === 'bigint') {
|
|
313
|
+
sink.writeBigInt(obj);
|
|
314
|
+
} else if (typeof obj === 'number') {
|
|
315
|
+
sink.writeNumber(obj);
|
|
316
|
+
} else if (typeof obj === 'string') {
|
|
317
|
+
sink.writeString(obj);
|
|
318
|
+
} else {
|
|
319
|
+
throw new Error(`Cannot serialize input to sink: ${typeof obj}`);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Streaming counterpart to `serializeArrayOfBufferableToVector`: write a length prefix (1 or 4 bytes) giving the
|
|
325
|
+
* number of serialized elements, then each element. Byte-identical to the buffer version.
|
|
326
|
+
*
|
|
327
|
+
* @param sink - The destination sink.
|
|
328
|
+
* @param objs - The vector elements.
|
|
329
|
+
* @param prefixLength - Width of the count prefix in bytes (1 or 4).
|
|
330
|
+
*/
|
|
331
|
+
export function serializeArrayToSink(sink: BufferSink, objs: Sinkable[], prefixLength = 4): void {
|
|
332
|
+
const count = countSinkables(objs);
|
|
333
|
+
if (prefixLength === 1) {
|
|
334
|
+
sink.writeUInt8(count);
|
|
335
|
+
} else if (prefixLength === 4) {
|
|
336
|
+
sink.writeNumber(count);
|
|
337
|
+
} else {
|
|
338
|
+
throw new Error(`Unsupported prefix length. Got ${prefixLength}, expected 1 or 4`);
|
|
339
|
+
}
|
|
340
|
+
serializeArrayToSinkInner(sink, objs);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** Count how many top-level buffers `serializeToBufferArray` would emit, flattening nested arrays as it does. */
|
|
344
|
+
function countSinkables(objs: Sinkable[]): number {
|
|
345
|
+
let count = 0;
|
|
346
|
+
for (const obj of objs) {
|
|
347
|
+
count += Array.isArray(obj) ? countSinkables(obj) : 1;
|
|
348
|
+
}
|
|
349
|
+
return count;
|
|
350
|
+
}
|
package/src/serialize/index.ts
CHANGED
|
@@ -269,6 +269,22 @@ export function serializeBigInt(n: bigint, width = 32) {
|
|
|
269
269
|
return toBufferBE(n, width);
|
|
270
270
|
}
|
|
271
271
|
|
|
272
|
+
/**
|
|
273
|
+
* Serialize a signed BigInt value into a Buffer of specified width using two's complement.
|
|
274
|
+
* @param n - The signed BigInt value to be serialized.
|
|
275
|
+
* @param width - The width (in bytes) of the output Buffer, optional with default value 32.
|
|
276
|
+
* @returns A Buffer containing the serialized signed BigInt value in big-endian format.
|
|
277
|
+
*/
|
|
278
|
+
export function serializeSignedBigInt(n: bigint, width = 32) {
|
|
279
|
+
const widthBits = BigInt(width * 8);
|
|
280
|
+
const max = 1n << (widthBits - 1n);
|
|
281
|
+
if (n < -max || n >= max) {
|
|
282
|
+
throw new Error(`Signed BigInt ${n.toString()} does not fit into ${width} bytes`);
|
|
283
|
+
}
|
|
284
|
+
const unsigned = n < 0n ? (1n << widthBits) + n : n;
|
|
285
|
+
return toBufferBE(unsigned, width);
|
|
286
|
+
}
|
|
287
|
+
|
|
272
288
|
/**
|
|
273
289
|
* Deserialize a big integer from a buffer, given an offset and width.
|
|
274
290
|
* Reads the specified number of bytes from the buffer starting at the offset, converts it to a big integer, and returns the deserialized result along with the number of bytes read (advanced).
|
|
@@ -282,6 +298,22 @@ export function deserializeBigInt(buf: Buffer, offset = 0, width = 32) {
|
|
|
282
298
|
return { elem: toBigIntBE(buf.subarray(offset, offset + width)), adv: width };
|
|
283
299
|
}
|
|
284
300
|
|
|
301
|
+
/**
|
|
302
|
+
* Deserialize a signed BigInt from a buffer (two's complement).
|
|
303
|
+
* @param buf - The buffer containing the signed big integer to be deserialized.
|
|
304
|
+
* @param offset - The position in the buffer where the integer starts. Defaults to 0.
|
|
305
|
+
* @param width - The number of bytes to read from the buffer for the integer. Defaults to 32.
|
|
306
|
+
* @returns An object containing the deserialized signed bigint value ('elem') and bytes advanced ('adv').
|
|
307
|
+
*/
|
|
308
|
+
export function deserializeSignedBigInt(buf: Buffer, offset = 0, width = 32) {
|
|
309
|
+
const { elem, adv } = deserializeBigInt(buf, offset, width);
|
|
310
|
+
const widthBits = BigInt(width * 8);
|
|
311
|
+
const signBit = 1n << (widthBits - 1n);
|
|
312
|
+
const fullRange = 1n << widthBits;
|
|
313
|
+
const signed = elem >= signBit ? elem - fullRange : elem;
|
|
314
|
+
return { elem: signed, adv };
|
|
315
|
+
}
|
|
316
|
+
|
|
285
317
|
/**
|
|
286
318
|
* Serializes a Date object into a Buffer containing its timestamp as a big integer value.
|
|
287
319
|
* The resulting Buffer has a fixed width of 8 bytes, representing a 64-bit big-endian integer.
|