@aztec/foundation 0.0.1-commit.96dac018d → 0.0.1-commit.993d240

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.
Files changed (269) hide show
  1. package/dest/bigint-buffer/index.d.ts +1 -1
  2. package/dest/bigint-buffer/index.d.ts.map +1 -1
  3. package/dest/bigint-buffer/index.js +6 -1
  4. package/dest/branded-types/block_number.d.ts +3 -3
  5. package/dest/branded-types/block_number.d.ts.map +1 -1
  6. package/dest/branded-types/block_number.js +15 -4
  7. package/dest/branded-types/buffer32_hash.d.ts +27 -0
  8. package/dest/branded-types/buffer32_hash.d.ts.map +1 -0
  9. package/dest/branded-types/buffer32_hash.js +12 -0
  10. package/dest/branded-types/checkpoint_number.d.ts +9 -3
  11. package/dest/branded-types/checkpoint_number.d.ts.map +1 -1
  12. package/dest/branded-types/checkpoint_number.js +11 -0
  13. package/dest/branded-types/epoch.d.ts +2 -2
  14. package/dest/branded-types/epoch.d.ts.map +1 -1
  15. package/dest/branded-types/index.d.ts +2 -1
  16. package/dest/branded-types/index.d.ts.map +1 -1
  17. package/dest/branded-types/index.js +1 -0
  18. package/dest/branded-types/index_within_checkpoint.d.ts +2 -2
  19. package/dest/branded-types/index_within_checkpoint.d.ts.map +1 -1
  20. package/dest/branded-types/slot.d.ts +5 -2
  21. package/dest/branded-types/slot.d.ts.map +1 -1
  22. package/dest/branded-types/slot.js +3 -0
  23. package/dest/buffer/buffer16.d.ts +3 -1
  24. package/dest/buffer/buffer16.d.ts.map +1 -1
  25. package/dest/buffer/buffer32.d.ts +28 -61
  26. package/dest/buffer/buffer32.d.ts.map +1 -1
  27. package/dest/buffer/buffer32.js +29 -63
  28. package/dest/buffer/index.d.ts +2 -1
  29. package/dest/buffer/index.d.ts.map +1 -1
  30. package/dest/buffer/index.js +1 -0
  31. package/dest/buffer/utils.d.ts +3 -0
  32. package/dest/buffer/utils.d.ts.map +1 -0
  33. package/dest/buffer/utils.js +7 -0
  34. package/dest/collection/array.d.ts +11 -1
  35. package/dest/collection/array.d.ts.map +1 -1
  36. package/dest/collection/array.js +33 -0
  37. package/dest/collection/index.d.ts +3 -1
  38. package/dest/collection/index.d.ts.map +1 -1
  39. package/dest/collection/index.js +2 -0
  40. package/dest/collection/lru_map.d.ts +41 -0
  41. package/dest/collection/lru_map.d.ts.map +1 -0
  42. package/dest/collection/lru_map.js +119 -0
  43. package/dest/collection/lru_set.d.ts +35 -0
  44. package/dest/collection/lru_set.d.ts.map +1 -0
  45. package/dest/collection/lru_set.js +95 -0
  46. package/dest/committable/committable.js +1 -1
  47. package/dest/config/env_var.d.ts +2 -2
  48. package/dest/config/env_var.d.ts.map +1 -1
  49. package/dest/config/index.d.ts +47 -30
  50. package/dest/config/index.d.ts.map +1 -1
  51. package/dest/config/index.js +44 -44
  52. package/dest/config/network_config.d.ts +15 -41
  53. package/dest/config/network_config.d.ts.map +1 -1
  54. package/dest/config/network_config.js +4 -1
  55. package/dest/config/network_name.d.ts +2 -2
  56. package/dest/config/network_name.d.ts.map +1 -1
  57. package/dest/config/network_name.js +0 -2
  58. package/dest/config/secret_value.d.ts +2 -2
  59. package/dest/config/secret_value.d.ts.map +1 -1
  60. package/dest/crypto/aes128/index.d.ts +2 -1
  61. package/dest/crypto/aes128/index.d.ts.map +1 -1
  62. package/dest/crypto/aes128/index.js +11 -2
  63. package/dest/crypto/bls/bn254_keystore.d.ts +9 -176
  64. package/dest/crypto/bls/bn254_keystore.d.ts.map +1 -1
  65. package/dest/crypto/bls/bn254_keystore.js +0 -17
  66. package/dest/crypto/poseidon/index.d.ts +1 -1
  67. package/dest/crypto/poseidon/index.d.ts.map +1 -1
  68. package/dest/crypto/poseidon/index.js +40 -33
  69. package/dest/crypto/schnorr/index.d.ts +8 -7
  70. package/dest/crypto/schnorr/index.d.ts.map +1 -1
  71. package/dest/crypto/schnorr/index.js +6 -6
  72. package/dest/crypto/schnorr/signature.d.ts +9 -29
  73. package/dest/crypto/schnorr/signature.d.ts.map +1 -1
  74. package/dest/crypto/schnorr/signature.js +20 -36
  75. package/dest/crypto/secp256k1-signer/utils.d.ts +12 -1
  76. package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -1
  77. package/dest/crypto/secp256k1-signer/utils.js +26 -0
  78. package/dest/curves/bls12/field.d.ts +3 -3
  79. package/dest/curves/bls12/field.d.ts.map +1 -1
  80. package/dest/curves/bls12/field.js +0 -5
  81. package/dest/curves/bls12/point.d.ts +2 -2
  82. package/dest/curves/bls12/point.d.ts.map +1 -1
  83. package/dest/curves/bn254/field.d.ts +24 -19
  84. package/dest/curves/bn254/field.d.ts.map +1 -1
  85. package/dest/curves/bn254/field.js +28 -19
  86. package/dest/curves/grumpkin/point.d.ts +14 -28
  87. package/dest/curves/grumpkin/point.d.ts.map +1 -1
  88. package/dest/curves/grumpkin/point.js +29 -43
  89. package/dest/eth-address/index.d.ts +2 -2
  90. package/dest/eth-address/index.d.ts.map +1 -1
  91. package/dest/eth-address/index.js +0 -3
  92. package/dest/eth-signature/eth_signature.d.ts +3 -2
  93. package/dest/eth-signature/eth_signature.d.ts.map +1 -1
  94. package/dest/eth-signature/eth_signature.js +7 -2
  95. package/dest/fifo/fifo_frame_reader.d.ts +41 -0
  96. package/dest/fifo/fifo_frame_reader.d.ts.map +1 -0
  97. package/dest/fifo/fifo_frame_reader.js +74 -0
  98. package/dest/fifo/index.d.ts +2 -0
  99. package/dest/fifo/index.d.ts.map +1 -0
  100. package/dest/fifo/index.js +1 -0
  101. package/dest/fifo_set/fifo_set.d.ts +15 -0
  102. package/dest/fifo_set/fifo_set.d.ts.map +1 -0
  103. package/dest/fifo_set/fifo_set.js +39 -0
  104. package/dest/fifo_set/index.d.ts +2 -0
  105. package/dest/fifo_set/index.d.ts.map +1 -0
  106. package/dest/fifo_set/index.js +1 -0
  107. package/dest/jest/setup.js +24 -0
  108. package/dest/json-rpc/client/fetch.d.ts +1 -1
  109. package/dest/json-rpc/client/fetch.d.ts.map +1 -1
  110. package/dest/json-rpc/client/fetch.js +4 -3
  111. package/dest/json-rpc/client/safe_json_rpc_client.d.ts +1 -1
  112. package/dest/json-rpc/client/safe_json_rpc_client.d.ts.map +1 -1
  113. package/dest/json-rpc/client/safe_json_rpc_client.js +2 -2
  114. package/dest/json-rpc/client/undici.d.ts +1 -1
  115. package/dest/json-rpc/client/undici.d.ts.map +1 -1
  116. package/dest/json-rpc/client/undici.js +5 -3
  117. package/dest/json-rpc/fixtures/test_state.d.ts +4 -8
  118. package/dest/json-rpc/fixtures/test_state.d.ts.map +1 -1
  119. package/dest/json-rpc/fixtures/test_state.js +57 -17
  120. package/dest/json-rpc/server/safe_json_rpc_server.d.ts +8 -3
  121. package/dest/json-rpc/server/safe_json_rpc_server.d.ts.map +1 -1
  122. package/dest/json-rpc/server/safe_json_rpc_server.js +27 -13
  123. package/dest/log/bigint-utils.d.ts +1 -1
  124. package/dest/log/bigint-utils.d.ts.map +1 -1
  125. package/dest/log/bigint-utils.js +3 -0
  126. package/dest/log/pino-logger.d.ts +1 -1
  127. package/dest/log/pino-logger.d.ts.map +1 -1
  128. package/dest/log/pino-logger.js +17 -2
  129. package/dest/noir/noir_package_config.d.ts +19 -104
  130. package/dest/noir/noir_package_config.d.ts.map +1 -1
  131. package/dest/noir/noir_package_config.js +1 -1
  132. package/dest/queue/batch_queue.d.ts +1 -1
  133. package/dest/queue/batch_queue.d.ts.map +1 -1
  134. package/dest/queue/batch_queue.js +1 -0
  135. package/dest/retry/index.d.ts +12 -1
  136. package/dest/retry/index.d.ts.map +1 -1
  137. package/dest/retry/index.js +21 -0
  138. package/dest/schemas/api.d.ts +12 -10
  139. package/dest/schemas/api.d.ts.map +1 -1
  140. package/dest/schemas/api.js +7 -1
  141. package/dest/schemas/parse.d.ts +4 -4
  142. package/dest/schemas/parse.d.ts.map +1 -1
  143. package/dest/schemas/parse.js +6 -5
  144. package/dest/schemas/schemas.d.ts +15 -15
  145. package/dest/schemas/types.d.ts +3 -3
  146. package/dest/schemas/types.d.ts.map +1 -1
  147. package/dest/schemas/utils.d.ts +7 -11
  148. package/dest/schemas/utils.d.ts.map +1 -1
  149. package/dest/schemas/utils.js +2 -18
  150. package/dest/serialize/buffer_sink.d.ts +134 -0
  151. package/dest/serialize/buffer_sink.d.ts.map +1 -0
  152. package/dest/serialize/buffer_sink.js +297 -0
  153. package/dest/serialize/index.d.ts +2 -1
  154. package/dest/serialize/index.d.ts.map +1 -1
  155. package/dest/serialize/index.js +1 -0
  156. package/dest/sleep/index.d.ts +2 -1
  157. package/dest/sleep/index.d.ts.map +1 -1
  158. package/dest/sleep/index.js +10 -1
  159. package/dest/string/index.js +1 -1
  160. package/dest/timer/index.d.ts +2 -2
  161. package/dest/timer/index.d.ts.map +1 -1
  162. package/dest/timer/index.js +1 -1
  163. package/dest/timer/timeout.d.ts +3 -1
  164. package/dest/timer/timeout.d.ts.map +1 -1
  165. package/dest/timer/timeout.js +26 -0
  166. package/dest/transport/dispatch/create_dispatch_proxy.d.ts +11 -2
  167. package/dest/transport/dispatch/create_dispatch_proxy.d.ts.map +1 -1
  168. package/dest/transport/index.d.ts +1 -2
  169. package/dest/transport/index.d.ts.map +1 -1
  170. package/dest/transport/index.js +0 -1
  171. package/dest/trees/balanced_merkle_tree_root.d.ts +2 -3
  172. package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -1
  173. package/dest/trees/balanced_merkle_tree_root.js +2 -3
  174. package/dest/trees/hasher.d.ts +3 -2
  175. package/dest/trees/hasher.d.ts.map +1 -1
  176. package/dest/trees/hasher.js +5 -5
  177. package/dest/trees/indexed_merkle_tree_calculator.d.ts +1 -1
  178. package/dest/trees/indexed_merkle_tree_calculator.d.ts.map +1 -1
  179. package/dest/trees/indexed_merkle_tree_calculator.js +5 -1
  180. package/dest/trees/membership_witness.d.ts +2 -2
  181. package/dest/trees/membership_witness.d.ts.map +1 -1
  182. package/dest/trees/merkle_tree_calculator.d.ts +4 -2
  183. package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
  184. package/dest/trees/merkle_tree_calculator.js +1 -5
  185. package/dest/trees/sibling_path.d.ts +5 -4
  186. package/dest/trees/sibling_path.d.ts.map +1 -1
  187. package/dest/trees/sibling_path.js +10 -8
  188. package/dest/trees/unbalanced_merkle_tree_root.d.ts +2 -3
  189. package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -1
  190. package/dest/trees/unbalanced_merkle_tree_root.js +2 -3
  191. package/dest/types/index.d.ts +9 -1
  192. package/dest/types/index.d.ts.map +1 -1
  193. package/package.json +6 -4
  194. package/src/bigint-buffer/index.ts +6 -1
  195. package/src/branded-types/block_number.ts +20 -2
  196. package/src/branded-types/buffer32_hash.ts +38 -0
  197. package/src/branded-types/checkpoint_number.ts +16 -1
  198. package/src/branded-types/epoch.ts +1 -1
  199. package/src/branded-types/index.ts +1 -0
  200. package/src/branded-types/index_within_checkpoint.ts +1 -1
  201. package/src/branded-types/slot.ts +6 -1
  202. package/src/buffer/buffer16.ts +3 -0
  203. package/src/buffer/buffer32.ts +39 -68
  204. package/src/buffer/index.ts +1 -0
  205. package/src/buffer/utils.ts +8 -0
  206. package/src/collection/array.ts +34 -0
  207. package/src/collection/index.ts +2 -0
  208. package/src/collection/lru_map.ts +143 -0
  209. package/src/collection/lru_set.ts +115 -0
  210. package/src/committable/committable.ts +1 -1
  211. package/src/config/env_var.ts +41 -23
  212. package/src/config/index.ts +116 -96
  213. package/src/config/network_config.ts +3 -0
  214. package/src/config/network_name.ts +0 -3
  215. package/src/config/secret_value.ts +1 -1
  216. package/src/crypto/aes128/index.ts +11 -2
  217. package/src/crypto/bls/bn254_keystore.ts +0 -23
  218. package/src/crypto/poseidon/index.ts +42 -34
  219. package/src/crypto/schnorr/index.ts +9 -8
  220. package/src/crypto/schnorr/signature.ts +17 -46
  221. package/src/crypto/secp256k1-signer/utils.ts +32 -0
  222. package/src/curves/bls12/field.ts +0 -7
  223. package/src/curves/bn254/field.ts +41 -36
  224. package/src/curves/grumpkin/point.ts +23 -40
  225. package/src/eth-address/index.ts +0 -4
  226. package/src/eth-signature/eth_signature.ts +7 -1
  227. package/src/fifo/fifo_frame_reader.ts +98 -0
  228. package/src/fifo/index.ts +1 -0
  229. package/src/fifo_set/fifo_set.ts +52 -0
  230. package/src/fifo_set/index.ts +1 -0
  231. package/src/jest/setup.mjs +27 -0
  232. package/src/json-rpc/client/fetch.ts +4 -3
  233. package/src/json-rpc/client/safe_json_rpc_client.ts +2 -2
  234. package/src/json-rpc/client/undici.ts +5 -3
  235. package/src/json-rpc/fixtures/test_state.ts +10 -10
  236. package/src/json-rpc/server/safe_json_rpc_server.ts +31 -13
  237. package/src/log/bigint-utils.ts +3 -0
  238. package/src/log/pino-logger.ts +19 -2
  239. package/src/noir/noir_package_config.ts +32 -20
  240. package/src/queue/batch_queue.ts +1 -0
  241. package/src/retry/index.ts +30 -0
  242. package/src/schemas/api.ts +25 -25
  243. package/src/schemas/parse.ts +9 -6
  244. package/src/schemas/schemas.ts +4 -4
  245. package/src/schemas/types.ts +2 -2
  246. package/src/schemas/utils.ts +4 -38
  247. package/src/serialize/buffer_sink.ts +350 -0
  248. package/src/serialize/index.ts +1 -0
  249. package/src/sleep/index.ts +10 -1
  250. package/src/string/index.ts +1 -1
  251. package/src/timer/index.ts +1 -1
  252. package/src/timer/timeout.ts +31 -0
  253. package/src/transport/dispatch/create_dispatch_proxy.ts +11 -1
  254. package/src/transport/index.ts +0 -1
  255. package/src/trees/balanced_merkle_tree_root.ts +2 -5
  256. package/src/trees/hasher.ts +6 -3
  257. package/src/trees/indexed_merkle_tree_calculator.ts +5 -1
  258. package/src/trees/merkle_tree_calculator.ts +4 -10
  259. package/src/trees/sibling_path.ts +11 -6
  260. package/src/trees/unbalanced_merkle_tree_root.ts +2 -5
  261. package/src/types/index.ts +14 -0
  262. package/dest/crypto/serialize.d.ts +0 -51
  263. package/dest/crypto/serialize.d.ts.map +0 -1
  264. package/dest/crypto/serialize.js +0 -68
  265. package/dest/transport/dispatch/create_dispatch_fn.d.ts +0 -25
  266. package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +0 -1
  267. package/dest/transport/dispatch/create_dispatch_fn.js +0 -17
  268. package/src/crypto/serialize.ts +0 -85
  269. package/src/transport/dispatch/create_dispatch_fn.ts +0 -35
@@ -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 z.tuple(schemas).parse(args);
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 tuplues with optional items. See https://github.com/colinhacks/zod/discussions/949.
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.AnyZodTuple>(args: any[], schema: T): Promise<T['_output']> {
15
- const missingCount = schema.items.length - args.length;
16
- const optionalCount = schema.items.filter(isOptional).length;
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._def.typeName === 'ZodOptional';
30
+ return schema.def.type === 'optional';
28
31
  }
29
32
  }
@@ -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
  ),
@@ -1,6 +1,6 @@
1
1
  import type { ZodType } from 'zod';
2
2
 
3
- export type ZodFor<T> = ZodType<T, any, 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, 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
@@ -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 class ZodNullableOptional<T extends ZodTypeAny> extends ZodOptional<T> {
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 ZodNullableOptional.create(schema);
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
 
@@ -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
+ }
@@ -1,5 +1,6 @@
1
1
  export * from './free_funcs.js';
2
2
  export * from './buffer_reader.js';
3
+ export * from './buffer_sink.js';
3
4
  export * from './field_reader.js';
4
5
  export * from './types.js';
5
6
  export * from './serialize.js';
@@ -22,6 +22,7 @@ import { InterruptError } from '../error/index.js';
22
22
  */
23
23
  export class InterruptibleSleep {
24
24
  private interrupts: Array<(shouldThrow: boolean) => void> = [];
25
+ private timeoutIds: NodeJS.Timeout[] = [];
25
26
 
26
27
  /**
27
28
  * Sleep for a specified amount of time in milliseconds.
@@ -38,9 +39,15 @@ export class InterruptibleSleep {
38
39
  this.interrupts.push(resolve);
39
40
  });
40
41
 
41
- const timeoutPromise = new Promise<boolean>(resolve => setTimeout(() => resolve(false), ms));
42
+ let timeoutId: NodeJS.Timeout;
43
+ const timeoutPromise = new Promise<boolean>(resolve => {
44
+ timeoutId = setTimeout(() => resolve(false), ms);
45
+ this.timeoutIds.push(timeoutId);
46
+ });
42
47
  const shouldThrow = await Promise.race([interruptPromise, timeoutPromise]);
43
48
 
49
+ clearTimeout(timeoutId!);
50
+ this.timeoutIds = this.timeoutIds.filter(id => id !== timeoutId);
44
51
  this.interrupts = this.interrupts.filter(res => res !== interruptResolve);
45
52
 
46
53
  if (shouldThrow) {
@@ -58,6 +65,8 @@ export class InterruptibleSleep {
58
65
  public interrupt(sleepShouldThrow = false): void {
59
66
  this.interrupts.forEach(resolve => resolve(sleepShouldThrow));
60
67
  this.interrupts = [];
68
+ this.timeoutIds.forEach(id => clearTimeout(id));
69
+ this.timeoutIds = [];
61
70
  }
62
71
  }
63
72
 
@@ -58,7 +58,7 @@ export function urlJoin(...args: string[]): string {
58
58
  end--;
59
59
  }
60
60
 
61
- if (start < end) {
61
+ if (start <= end) {
62
62
  processed.push(arg.slice(start, end + 1));
63
63
  }
64
64
  }
@@ -1,4 +1,4 @@
1
1
  export * from './date.js';
2
2
  export { elapsed, elapsedSync } from './elapsed.js';
3
- export { TimeoutTask, executeTimeout, timeoutPromise } from './timeout.js';
3
+ export { TimeoutTask, execWithSignal, executeTimeout, timeoutPromise } from './timeout.js';
4
4
  export { Timer } from './timer.js';
@@ -92,6 +92,37 @@ export async function executeTimeout<T>(
92
92
  return await task.exec();
93
93
  }
94
94
 
95
+ function defaultSignalError(signal: AbortSignal) {
96
+ if (signal.reason instanceof Error) {
97
+ return signal.reason;
98
+ }
99
+
100
+ return new Error(signal.reason ? String(signal.reason) : 'Operation aborted');
101
+ }
102
+
103
+ /** Executes a function until it completes or the given signal aborts. */
104
+ export async function execWithSignal<T>(
105
+ fn: (signal: AbortSignal) => Promise<T>,
106
+ signal: AbortSignal,
107
+ errorFn: (signal: AbortSignal) => Error = defaultSignalError,
108
+ ) {
109
+ if (signal.aborted) {
110
+ throw errorFn(signal);
111
+ }
112
+
113
+ const abortPromise = promiseWithResolvers<never>();
114
+ const abortListener = () => abortPromise.reject(errorFn(signal));
115
+ signal.addEventListener('abort', abortListener, { once: true });
116
+
117
+ try {
118
+ return await Promise.race<T>([fn(signal), abortPromise.promise]);
119
+ } finally {
120
+ if (abortListener) {
121
+ signal.removeEventListener('abort', abortListener);
122
+ }
123
+ }
124
+ }
125
+
95
126
  /** Returns a promise that rejects after the given timeout */
96
127
  export function timeoutPromise(timeoutMs: number, errorMessage?: string) {
97
128
  const promise = promiseWithResolvers<never>();
@@ -2,7 +2,17 @@ import { EventEmitter } from 'events';
2
2
 
3
3
  import { type TransferDescriptor, isTransferDescriptor } from '../interface/transferable.js';
4
4
  import type { TransportClient } from '../transport_client.js';
5
- import type { DispatchMsg } from './create_dispatch_fn.js';
5
+
6
+ /**
7
+ * Represents a message object for dispatching function calls.
8
+ * Contains the function name ('fn') and an array of arguments ('args') required to call the target method.
9
+ */
10
+ export interface DispatchMsg {
11
+ /** Name of the target method to be called. */
12
+ fn: string;
13
+ /** An array of arguments to be passed to the target method. */
14
+ args: any[];
15
+ }
6
16
 
7
17
  /**
8
18
  * FilterOutAttributes type filters out all non-method properties of an object, leaving only the attributes
@@ -1,4 +1,3 @@
1
- export * from './dispatch/create_dispatch_fn.js';
2
1
  export * from './dispatch/create_dispatch_proxy.js';
3
2
  export * from './dispatch/messages.js';
4
3
  export * from './interface/connector.js';
@@ -1,10 +1,7 @@
1
- import { poseidonMerkleHash, shaMerkleHash } from './hasher.js';
1
+ import { shaMerkleHash } from './hasher.js';
2
2
 
3
3
  export const computeBalancedShaRoot = (leaves: Buffer[]) => computeBalancedMerkleTreeRoot(leaves);
4
4
 
5
- export const computeBalancedPoseidonRoot = async (leaves: Buffer[]) =>
6
- await computeBalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);
7
-
8
5
  /**
9
6
  * Computes the Merkle root with the provided leaves **synchronously**.
10
7
  * This method uses a synchronous hash function (defaults to `sha256Trunc`).
@@ -36,7 +33,7 @@ export function computeBalancedMerkleTreeRoot(leaves: Buffer[], hasher = shaMerk
36
33
  */
37
34
  export async function computeBalancedMerkleTreeRootAsync(
38
35
  leaves: Buffer[],
39
- hasher = poseidonMerkleHash,
36
+ hasher: (left: Buffer, right: Buffer) => Promise<Buffer<ArrayBuffer>>,
40
37
  ): Promise<Buffer> {
41
38
  const height = getTreeHeight(leaves);
42
39
  let nodes = leaves.slice();
@@ -1,4 +1,4 @@
1
- import { poseidon2Hash } from '../crypto/poseidon/index.js';
1
+ import { poseidon2HashWithSeparator } from '../crypto/poseidon/index.js';
2
2
  import { sha256Trunc } from '../crypto/sha256/index.js';
3
3
 
4
4
  /**
@@ -44,5 +44,8 @@ export interface AsyncHasher {
44
44
  export const shaMerkleHash: Hasher['hash'] = (left: Buffer, right: Buffer) =>
45
45
  sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>;
46
46
 
47
- export const poseidonMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
48
- (await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>;
47
+ /** Creates a poseidon2 merkle hasher with the given domain separator. */
48
+ export const makePoseidonMerkleHash =
49
+ (separator: number): AsyncHasher['hash'] =>
50
+ async (left: Buffer, right: Buffer) =>
51
+ (await poseidon2HashWithSeparator([left, right], separator)).toBuffer() as Buffer<ArrayBuffer>;
@@ -40,7 +40,11 @@ export class IndexedMerkleTreeCalculator<T extends IndexedTreeLeafPreimage, N ex
40
40
  }
41
41
  const sorted = values
42
42
  .map((v, i) => ({ value: v, index: i }))
43
- .sort((a, b) => Number(toBigIntBE(b.value) - toBigIntBE(a.value)));
43
+ .sort((a, b): -1 | 0 | 1 => {
44
+ const aBigInt = toBigIntBE(a.value);
45
+ const bBigInt = toBigIntBE(b.value);
46
+ return aBigInt < bBigInt ? 1 : aBigInt > bBigInt ? -1 : 0;
47
+ });
44
48
  const indexedLeaves = sorted.map((item, i) => ({
45
49
  leaf: this.factory.fromBuffer(
46
50
  Buffer.concat([