@aztec/foundation 0.0.1-commit.936cb2cae → 0.0.1-commit.949a33fd8

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 (107) hide show
  1. package/dest/branded-types/buffer32_hash.d.ts +24 -0
  2. package/dest/branded-types/buffer32_hash.d.ts.map +1 -0
  3. package/dest/branded-types/buffer32_hash.js +19 -0
  4. package/dest/branded-types/checkpoint_number.d.ts +4 -1
  5. package/dest/branded-types/checkpoint_number.d.ts.map +1 -1
  6. package/dest/branded-types/checkpoint_number.js +3 -0
  7. package/dest/branded-types/index.d.ts +2 -1
  8. package/dest/branded-types/index.d.ts.map +1 -1
  9. package/dest/branded-types/index.js +1 -0
  10. package/dest/buffer/buffer16.d.ts +3 -1
  11. package/dest/buffer/buffer16.d.ts.map +1 -1
  12. package/dest/buffer/buffer32.d.ts +28 -61
  13. package/dest/buffer/buffer32.d.ts.map +1 -1
  14. package/dest/buffer/buffer32.js +26 -60
  15. package/dest/config/env_var.d.ts +2 -2
  16. package/dest/config/env_var.d.ts.map +1 -1
  17. package/dest/config/index.d.ts +48 -30
  18. package/dest/config/index.d.ts.map +1 -1
  19. package/dest/config/index.js +21 -30
  20. package/dest/crypto/aes128/index.d.ts +2 -1
  21. package/dest/crypto/aes128/index.d.ts.map +1 -1
  22. package/dest/crypto/aes128/index.js +11 -2
  23. package/dest/crypto/bls/bn254_keystore.d.ts +1 -10
  24. package/dest/crypto/bls/bn254_keystore.d.ts.map +1 -1
  25. package/dest/crypto/bls/bn254_keystore.js +0 -17
  26. package/dest/curves/bls12/field.d.ts +1 -1
  27. package/dest/curves/bls12/field.d.ts.map +1 -1
  28. package/dest/curves/bls12/field.js +0 -5
  29. package/dest/curves/bn254/field.d.ts +17 -16
  30. package/dest/curves/bn254/field.d.ts.map +1 -1
  31. package/dest/curves/bn254/field.js +18 -13
  32. package/dest/curves/grumpkin/point.d.ts +4 -4
  33. package/dest/curves/grumpkin/point.d.ts.map +1 -1
  34. package/dest/curves/grumpkin/point.js +7 -4
  35. package/dest/eth-address/index.d.ts +1 -1
  36. package/dest/eth-address/index.d.ts.map +1 -1
  37. package/dest/eth-address/index.js +0 -3
  38. package/dest/json-rpc/client/fetch.d.ts +1 -1
  39. package/dest/json-rpc/client/fetch.d.ts.map +1 -1
  40. package/dest/json-rpc/client/fetch.js +4 -3
  41. package/dest/json-rpc/client/undici.d.ts +1 -1
  42. package/dest/json-rpc/client/undici.d.ts.map +1 -1
  43. package/dest/json-rpc/client/undici.js +5 -3
  44. package/dest/log/pino-logger.d.ts +1 -1
  45. package/dest/log/pino-logger.d.ts.map +1 -1
  46. package/dest/log/pino-logger.js +17 -2
  47. package/dest/queue/batch_queue.d.ts +1 -1
  48. package/dest/queue/batch_queue.d.ts.map +1 -1
  49. package/dest/queue/batch_queue.js +1 -0
  50. package/dest/retry/index.d.ts +12 -1
  51. package/dest/retry/index.d.ts.map +1 -1
  52. package/dest/retry/index.js +21 -0
  53. package/dest/string/index.js +1 -1
  54. package/dest/transport/dispatch/create_dispatch_proxy.d.ts +11 -2
  55. package/dest/transport/dispatch/create_dispatch_proxy.d.ts.map +1 -1
  56. package/dest/transport/index.d.ts +1 -2
  57. package/dest/transport/index.d.ts.map +1 -1
  58. package/dest/transport/index.js +0 -1
  59. package/dest/trees/balanced_merkle_tree_root.d.ts +2 -3
  60. package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -1
  61. package/dest/trees/balanced_merkle_tree_root.js +2 -3
  62. package/dest/trees/hasher.d.ts +3 -2
  63. package/dest/trees/hasher.d.ts.map +1 -1
  64. package/dest/trees/hasher.js +5 -5
  65. package/dest/trees/merkle_tree_calculator.d.ts +4 -2
  66. package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
  67. package/dest/trees/merkle_tree_calculator.js +1 -5
  68. package/dest/trees/sibling_path.d.ts +2 -2
  69. package/dest/trees/sibling_path.d.ts.map +1 -1
  70. package/dest/trees/sibling_path.js +1 -5
  71. package/dest/trees/unbalanced_merkle_tree_root.d.ts +2 -3
  72. package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -1
  73. package/dest/trees/unbalanced_merkle_tree_root.js +2 -3
  74. package/dest/types/index.d.ts +9 -1
  75. package/dest/types/index.d.ts.map +1 -1
  76. package/package.json +2 -2
  77. package/src/branded-types/buffer32_hash.ts +42 -0
  78. package/src/branded-types/checkpoint_number.ts +5 -0
  79. package/src/branded-types/index.ts +1 -0
  80. package/src/buffer/buffer16.ts +3 -0
  81. package/src/buffer/buffer32.ts +36 -65
  82. package/src/config/env_var.ts +4 -5
  83. package/src/config/index.ts +96 -84
  84. package/src/crypto/aes128/index.ts +11 -2
  85. package/src/crypto/bls/bn254_keystore.ts +0 -23
  86. package/src/curves/bls12/field.ts +0 -7
  87. package/src/curves/bn254/field.ts +25 -31
  88. package/src/curves/grumpkin/point.ts +6 -3
  89. package/src/eth-address/index.ts +0 -4
  90. package/src/json-rpc/client/fetch.ts +4 -3
  91. package/src/json-rpc/client/undici.ts +5 -3
  92. package/src/log/pino-logger.ts +19 -2
  93. package/src/queue/batch_queue.ts +1 -0
  94. package/src/retry/index.ts +30 -0
  95. package/src/string/index.ts +1 -1
  96. package/src/transport/dispatch/create_dispatch_proxy.ts +11 -1
  97. package/src/transport/index.ts +0 -1
  98. package/src/trees/balanced_merkle_tree_root.ts +2 -5
  99. package/src/trees/hasher.ts +6 -3
  100. package/src/trees/merkle_tree_calculator.ts +4 -10
  101. package/src/trees/sibling_path.ts +1 -2
  102. package/src/trees/unbalanced_merkle_tree_root.ts +2 -5
  103. package/src/types/index.ts +14 -0
  104. package/dest/transport/dispatch/create_dispatch_fn.d.ts +0 -25
  105. package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +0 -1
  106. package/dest/transport/dispatch/create_dispatch_fn.js +0 -17
  107. package/src/transport/dispatch/create_dispatch_fn.ts +0 -35
@@ -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
  }
@@ -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>;
@@ -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: AsyncHasher['hash'],
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,5 +1,4 @@
1
1
  import { makeTuple } from '../array/array.js';
2
- import { poseidon2Hash } from '../crypto/poseidon/index.js';
3
2
  import { Fr } from '../curves/bn254/index.js';
4
3
  import { schemas } from '../schemas/index.js';
5
4
  import {
@@ -172,7 +171,7 @@ export async function computeRootFromSiblingPath(
172
171
  leaf: Buffer,
173
172
  siblingPath: Buffer[],
174
173
  index: number,
175
- hasher = async (left: Buffer, right: Buffer) => (await poseidon2Hash([left, right])).toBuffer(),
174
+ hasher: (left: Buffer, right: Buffer) => Promise<Buffer>,
176
175
  ) {
177
176
  let result = leaf;
178
177
  for (const sibling of siblingPath) {
@@ -1,12 +1,9 @@
1
1
  import { computeBalancedMerkleTreeRoot, computeBalancedMerkleTreeRootAsync } from './balanced_merkle_tree_root.js';
2
- import { poseidonMerkleHash, shaMerkleHash } from './hasher.js';
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 = poseidonMerkleHash,
67
+ hasher: (left: Uint8Array, right: Uint8Array) => Promise<Buffer<ArrayBuffer>>,
71
68
  emptyRoot = Buffer.alloc(32),
72
69
  ): Promise<Buffer> {
73
70
  if (!leaves.length) {
@@ -34,6 +34,20 @@ export type Prettify<T> = {
34
34
  [K in keyof T]: T[K];
35
35
  } & {};
36
36
 
37
+ /** Returns a type with fields conditionally required based on a flag */
38
+ export type IfFlag<
39
+ OptsSchema,
40
+ Opts extends OptsSchema,
41
+ Key extends keyof OptsSchema,
42
+ Field extends object,
43
+ > = Opts extends { [K in Key]: true }
44
+ ? Field
45
+ : Opts extends { [K in Key]: false }
46
+ ? {}
47
+ : Opts extends { [K in Key]?: boolean }
48
+ ? Partial<Field>
49
+ : {};
50
+
37
51
  /**
38
52
  * Type-safe Event Emitter type
39
53
  * @example
@@ -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
- }
@@ -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
- }