@aztec/foundation 3.0.0-rc.5 → 4.0.0-nightly.20260107

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 (58) hide show
  1. package/dest/array/index.d.ts +2 -1
  2. package/dest/array/index.d.ts.map +1 -1
  3. package/dest/array/index.js +1 -0
  4. package/dest/array/sorted_array.d.ts +10 -0
  5. package/dest/array/sorted_array.d.ts.map +1 -0
  6. package/dest/array/sorted_array.js +106 -0
  7. package/dest/buffer/buffer16.js +3 -1
  8. package/dest/buffer/buffer32.js +3 -1
  9. package/dest/config/env_var.d.ts +2 -2
  10. package/dest/config/env_var.d.ts.map +1 -1
  11. package/dest/config/network_config.d.ts +13 -1
  12. package/dest/config/network_config.d.ts.map +1 -1
  13. package/dest/config/network_config.js +3 -1
  14. package/dest/config/secret_value.js +3 -1
  15. package/dest/curves/bls12/field.js +6 -3
  16. package/dest/curves/bls12/point.js +3 -1
  17. package/dest/curves/bn254/field.d.ts +6 -7
  18. package/dest/curves/bn254/field.d.ts.map +1 -1
  19. package/dest/curves/bn254/field.js +30 -43
  20. package/dest/eth-address/index.js +3 -1
  21. package/dest/iterator/filter.d.ts +3 -0
  22. package/dest/iterator/filter.d.ts.map +1 -0
  23. package/dest/iterator/filter.js +7 -0
  24. package/dest/iterator/index.d.ts +2 -0
  25. package/dest/iterator/index.d.ts.map +1 -0
  26. package/dest/iterator/index.js +1 -0
  27. package/dest/schemas/schemas.d.ts +3 -1
  28. package/dest/schemas/schemas.d.ts.map +1 -1
  29. package/dest/schemas/schemas.js +5 -0
  30. package/dest/schemas/types.d.ts +31 -1
  31. package/dest/schemas/types.d.ts.map +1 -1
  32. package/dest/schemas/types.js +25 -1
  33. package/dest/sleep/index.d.ts +3 -1
  34. package/dest/sleep/index.d.ts.map +1 -1
  35. package/dest/sleep/index.js +4 -0
  36. package/dest/timer/date.d.ts +2 -1
  37. package/dest/timer/date.d.ts.map +1 -1
  38. package/dest/timer/date.js +3 -0
  39. package/dest/types/index.d.ts +3 -1
  40. package/dest/types/index.d.ts.map +1 -1
  41. package/dest/types/index.js +3 -0
  42. package/package.json +3 -2
  43. package/src/array/index.ts +1 -0
  44. package/src/array/sorted_array.ts +133 -0
  45. package/src/config/env_var.ts +9 -4
  46. package/src/config/network_config.ts +2 -0
  47. package/src/curves/bn254/field.ts +28 -43
  48. package/src/iterator/filter.ts +11 -0
  49. package/src/iterator/index.ts +1 -0
  50. package/src/schemas/schemas.ts +8 -0
  51. package/src/schemas/types.ts +33 -0
  52. package/src/sleep/index.ts +6 -0
  53. package/src/timer/date.ts +4 -0
  54. package/src/types/index.ts +5 -0
  55. package/dest/json-rpc/server/telemetry.d.ts +0 -2
  56. package/dest/json-rpc/server/telemetry.d.ts.map +0 -1
  57. package/dest/json-rpc/server/telemetry.js +0 -0
  58. package/src/json-rpc/server/telemetry.ts +0 -0
@@ -8,8 +8,6 @@ import { hexSchemaFor } from '../../schemas/utils.js';
8
8
  import { BufferReader } from '../../serialize/buffer_reader.js';
9
9
  import { TypeRegistry } from '../../serialize/type_registry.js';
10
10
 
11
- const ZERO_BUFFER = Buffer.alloc(32);
12
-
13
11
  /* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
14
12
 
15
13
  /**
@@ -25,14 +23,12 @@ type DerivedField<T extends BaseField> = {
25
23
 
26
24
  /**
27
25
  * Base field class.
28
- * Conversions from Buffer to BigInt and vice-versa are not cheap.
29
- * We allow construction with either form and lazily convert to other as needed.
30
- * We only check we are within the field modulus when initializing with bigint.
26
+ * Uses bigint as the internal representation.
27
+ * Buffers are generated on demand from the bigint value.
31
28
  */
32
29
  abstract class BaseField {
33
30
  static SIZE_IN_BYTES = 32;
34
- private asBuffer?: Buffer;
35
- private asBigInt?: bigint;
31
+ private readonly asBigInt: bigint;
36
32
 
37
33
  /**
38
34
  * Return bigint representation.
@@ -52,53 +48,41 @@ abstract class BaseField {
52
48
  if (value.length > BaseField.SIZE_IN_BYTES) {
53
49
  throw new Error(`Value length ${value.length} exceeds ${BaseField.SIZE_IN_BYTES}`);
54
50
  }
55
- this.asBuffer =
56
- value.length === BaseField.SIZE_IN_BYTES
57
- ? value
58
- : Buffer.concat([Buffer.alloc(BaseField.SIZE_IN_BYTES - value.length), value]);
51
+ this.asBigInt = toBigIntBE(value);
59
52
  } else if (typeof value === 'bigint' || typeof value === 'number' || typeof value === 'boolean') {
60
53
  this.asBigInt = BigInt(value);
61
- if (this.asBigInt >= this.modulus()) {
62
- throw new Error(`Value 0x${this.asBigInt.toString(16)} is greater or equal to field modulus.`);
63
- } else if (this.asBigInt < 0n) {
64
- throw new Error(`Value 0x${this.asBigInt.toString(16)} is negative.`);
65
- }
66
54
  } else if (value instanceof BaseField) {
67
- this.asBuffer = value.asBuffer;
68
55
  this.asBigInt = value.asBigInt;
69
56
  } else {
70
57
  throw new Error(`Type '${typeof value}' with value '${value}' passed to BaseField ctor.`);
71
58
  }
59
+
60
+ if (this.asBigInt < 0n) {
61
+ throw new Error(`Value 0x${this.asBigInt.toString(16)} is negative.`);
62
+ } else if (this.asBigInt >= this.modulus()) {
63
+ throw new Error(`Value 0x${this.asBigInt.toString(16)} is greater or equal to field modulus.`);
64
+ }
72
65
  }
73
66
 
74
67
  protected abstract modulus(): bigint;
75
68
 
76
69
  /**
77
- * We return a copy of the Buffer to ensure this remains immutable.
70
+ * Converts the bigint to a Buffer.
78
71
  */
79
72
  toBuffer(): Buffer {
80
- if (!this.asBuffer) {
81
- this.asBuffer = toBufferBE(this.asBigInt!, 32);
82
- }
83
- return Buffer.from(this.asBuffer);
73
+ return toBufferBE(this.asBigInt, 32);
84
74
  }
85
75
 
86
76
  toString(): `0x${string}` {
87
- return `0x${this.toBuffer().toString('hex')}`;
77
+ return `0x${this.asBigInt.toString(16).padStart(64, '0')}`;
88
78
  }
89
79
 
90
80
  toBigInt(): bigint {
91
- if (this.asBigInt === undefined) {
92
- this.asBigInt = toBigIntBE(this.asBuffer!);
93
- if (this.asBigInt >= this.modulus()) {
94
- throw new Error(`Value 0x${this.asBigInt.toString(16)} is greater or equal to field modulus.`);
95
- }
96
- }
97
81
  return this.asBigInt;
98
82
  }
99
83
 
100
84
  toBool(): boolean {
101
- return Boolean(this.toBigInt());
85
+ return this.asBigInt !== 0n;
102
86
  }
103
87
 
104
88
  /**
@@ -106,11 +90,10 @@ abstract class BaseField {
106
90
  * Throws if the underlying value is greater than MAX_SAFE_INTEGER.
107
91
  */
108
92
  toNumber(): number {
109
- const value = this.toBigInt();
110
- if (value > Number.MAX_SAFE_INTEGER) {
111
- throw new Error(`Value ${value.toString(16)} greater than than max safe integer`);
93
+ if (this.asBigInt > Number.MAX_SAFE_INTEGER) {
94
+ throw new Error(`Value ${this.asBigInt.toString(16)} greater than than max safe integer`);
112
95
  }
113
- return Number(value);
96
+ return Number(this.asBigInt);
114
97
  }
115
98
 
116
99
  /**
@@ -118,8 +101,7 @@ abstract class BaseField {
118
101
  * May cause loss of precision if the underlying value is greater than MAX_SAFE_INTEGER.
119
102
  */
120
103
  toNumberUnsafe(): number {
121
- const value = this.toBigInt();
122
- return Number(value);
104
+ return Number(this.asBigInt);
123
105
  }
124
106
 
125
107
  toShortString(): string {
@@ -128,21 +110,24 @@ abstract class BaseField {
128
110
  }
129
111
 
130
112
  equals(rhs: BaseField): boolean {
131
- return this.toBuffer().equals(rhs.toBuffer());
113
+ return this.asBigInt === rhs.asBigInt;
132
114
  }
133
115
 
134
116
  lt(rhs: BaseField): boolean {
135
- return this.toBigInt() < rhs.toBigInt();
117
+ return this.asBigInt < rhs.asBigInt;
136
118
  }
137
119
 
138
120
  cmp(rhs: BaseField): -1 | 0 | 1 {
139
- const lhsBigInt = this.toBigInt();
140
- const rhsBigInt = rhs.toBigInt();
141
- return lhsBigInt === rhsBigInt ? 0 : lhsBigInt < rhsBigInt ? -1 : 1;
121
+ const rhsBigInt = rhs.asBigInt;
122
+ return this.asBigInt === rhsBigInt ? 0 : this.asBigInt < rhsBigInt ? -1 : 1;
123
+ }
124
+
125
+ static cmp(lhs: BaseField, rhs: BaseField): -1 | 0 | 1 {
126
+ return lhs.cmp(rhs);
142
127
  }
143
128
 
144
129
  isZero(): boolean {
145
- return this.toBuffer().equals(ZERO_BUFFER);
130
+ return this.asBigInt === 0n;
146
131
  }
147
132
 
148
133
  isEmpty(): boolean {
@@ -195,7 +180,7 @@ function fromHexString<T extends BaseField>(buf: string, f: DerivedField<T>) {
195
180
 
196
181
  const buffer = Buffer.from(checked.length % 2 === 1 ? '0' + checked : checked, 'hex');
197
182
 
198
- return new f(buffer);
183
+ return new f(toBigIntBE(buffer));
199
184
  }
200
185
 
201
186
  /** Branding to ensure fields are not interchangeable types. */
@@ -0,0 +1,11 @@
1
+ /** Wraps an async iterable iterator such that it filters values based on a predicate. */
2
+ export async function* filter<T>(
3
+ iterator: AsyncIterableIterator<T>,
4
+ predicate: (item: T) => boolean | Promise<boolean>,
5
+ ): AsyncIterableIterator<T> {
6
+ for await (const item of iterator) {
7
+ if (await predicate(item)) {
8
+ yield item;
9
+ }
10
+ }
11
+ }
@@ -0,0 +1 @@
1
+ export { filter } from './filter.js';
@@ -58,6 +58,14 @@ export const schemas = {
58
58
  .max(2 ** 32 - 1),
59
59
  ),
60
60
 
61
+ /** Coerces input to UInt64. */
62
+ UInt64: z.union([z.bigint(), z.number(), z.string()]).pipe(
63
+ z.coerce
64
+ .bigint()
65
+ .min(0n)
66
+ .max(2n ** 64n - 1n),
67
+ ),
68
+
61
69
  /** Accepts a hex string as a Buffer32 type. */
62
70
  Buffer32: z.string().refine(isHex, 'Not a valid hex string').transform(Buffer32.fromString),
63
71
 
@@ -1,3 +1,36 @@
1
1
  import type { ZodType } from 'zod';
2
2
 
3
3
  export type ZodFor<T> = ZodType<T, any, any>;
4
+
5
+ /**
6
+ * Creates a schema validator that enforces all properties of type T are present in the schema.
7
+ * This provides compile-time safety to ensure schemas don't miss optional properties.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * interface MyConfig {
12
+ * foo: string;
13
+ * bar?: number;
14
+ * }
15
+ *
16
+ * // ✅ This will work - all keys present
17
+ * const schema1 = zodFor<MyConfig>()(z.object({
18
+ * foo: z.string(),
19
+ * bar: z.number().optional(),
20
+ * }));
21
+ *
22
+ * // ❌ This will error - 'bar' is missing
23
+ * const schema2 = zodFor<MyConfig>()(z.object({
24
+ * foo: z.string(),
25
+ * }));
26
+ * ```
27
+ */
28
+ export function zodFor<T>() {
29
+ return (schema => schema) as <S extends ZodType<any, any, any>>(
30
+ schema: keyof T extends keyof S['_output']
31
+ ? keyof S['_output'] extends keyof T
32
+ ? S
33
+ : S & { __error__: 'Schema has extra keys not in type'; __extra__: Exclude<keyof S['_output'], keyof T> }
34
+ : S & { __error__: 'Schema is missing keys from type'; __missing__: Exclude<keyof T, keyof S['_output']> },
35
+ ) => S;
36
+ }
@@ -73,3 +73,9 @@ export class InterruptibleSleep {
73
73
  export function sleep<T>(ms: number, returnValue?: T): Promise<T> {
74
74
  return new Promise(resolve => setTimeout(() => resolve(returnValue as T), ms));
75
75
  }
76
+
77
+ /** Sleeps until the target date */
78
+ export function sleepUntil<T>(target: Date, now: Date, returnValue?: T): Promise<T> {
79
+ const ms = target.getTime() - now.getTime();
80
+ return sleep(ms, returnValue);
81
+ }
package/src/timer/date.ts CHANGED
@@ -9,6 +9,10 @@ export class DateProvider {
9
9
  public nowInSeconds(): number {
10
10
  return Math.floor(this.now() / 1000);
11
11
  }
12
+
13
+ public nowAsDate(): Date {
14
+ return new Date(this.now());
15
+ }
12
16
  }
13
17
 
14
18
  /** Returns current datetime and allows to override it. */
@@ -19,6 +19,11 @@ export function unfreeze<T>(obj: T): Writeable<T> {
19
19
  return obj as Writeable<T>;
20
20
  }
21
21
 
22
+ /** Is defined type guard */
23
+ export function isDefined<T>(value: T | undefined): value is T {
24
+ return value !== undefined;
25
+ }
26
+
22
27
  /** Resolves a record-like type. Lifted from viem. */
23
28
  export type Prettify<T> = {
24
29
  [K in keyof T]: T[K];
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVsZW1ldHJ5LmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvanNvbi1ycGMvc2VydmVyL3RlbGVtZXRyeS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0=
@@ -1 +0,0 @@
1
- {"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../../../src/json-rpc/server/telemetry.ts"],"names":[],"mappings":""}
File without changes
File without changes