@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.
- package/dest/array/index.d.ts +2 -1
- package/dest/array/index.d.ts.map +1 -1
- package/dest/array/index.js +1 -0
- package/dest/array/sorted_array.d.ts +10 -0
- package/dest/array/sorted_array.d.ts.map +1 -0
- package/dest/array/sorted_array.js +106 -0
- package/dest/buffer/buffer16.js +3 -1
- package/dest/buffer/buffer32.js +3 -1
- package/dest/config/env_var.d.ts +2 -2
- package/dest/config/env_var.d.ts.map +1 -1
- package/dest/config/network_config.d.ts +13 -1
- package/dest/config/network_config.d.ts.map +1 -1
- package/dest/config/network_config.js +3 -1
- package/dest/config/secret_value.js +3 -1
- package/dest/curves/bls12/field.js +6 -3
- package/dest/curves/bls12/point.js +3 -1
- package/dest/curves/bn254/field.d.ts +6 -7
- package/dest/curves/bn254/field.d.ts.map +1 -1
- package/dest/curves/bn254/field.js +30 -43
- package/dest/eth-address/index.js +3 -1
- package/dest/iterator/filter.d.ts +3 -0
- package/dest/iterator/filter.d.ts.map +1 -0
- package/dest/iterator/filter.js +7 -0
- package/dest/iterator/index.d.ts +2 -0
- package/dest/iterator/index.d.ts.map +1 -0
- package/dest/iterator/index.js +1 -0
- package/dest/schemas/schemas.d.ts +3 -1
- package/dest/schemas/schemas.d.ts.map +1 -1
- package/dest/schemas/schemas.js +5 -0
- package/dest/schemas/types.d.ts +31 -1
- package/dest/schemas/types.d.ts.map +1 -1
- package/dest/schemas/types.js +25 -1
- package/dest/sleep/index.d.ts +3 -1
- package/dest/sleep/index.d.ts.map +1 -1
- package/dest/sleep/index.js +4 -0
- package/dest/timer/date.d.ts +2 -1
- package/dest/timer/date.d.ts.map +1 -1
- package/dest/timer/date.js +3 -0
- package/dest/types/index.d.ts +3 -1
- package/dest/types/index.d.ts.map +1 -1
- package/dest/types/index.js +3 -0
- package/package.json +3 -2
- package/src/array/index.ts +1 -0
- package/src/array/sorted_array.ts +133 -0
- package/src/config/env_var.ts +9 -4
- package/src/config/network_config.ts +2 -0
- package/src/curves/bn254/field.ts +28 -43
- package/src/iterator/filter.ts +11 -0
- package/src/iterator/index.ts +1 -0
- package/src/schemas/schemas.ts +8 -0
- package/src/schemas/types.ts +33 -0
- package/src/sleep/index.ts +6 -0
- package/src/timer/date.ts +4 -0
- package/src/types/index.ts +5 -0
- package/dest/json-rpc/server/telemetry.d.ts +0 -2
- package/dest/json-rpc/server/telemetry.d.ts.map +0 -1
- package/dest/json-rpc/server/telemetry.js +0 -0
- 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
|
-
*
|
|
29
|
-
*
|
|
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
|
|
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.
|
|
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
|
-
*
|
|
70
|
+
* Converts the bigint to a Buffer.
|
|
78
71
|
*/
|
|
79
72
|
toBuffer(): Buffer {
|
|
80
|
-
|
|
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.
|
|
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
|
|
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
|
-
|
|
110
|
-
|
|
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(
|
|
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
|
-
|
|
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.
|
|
113
|
+
return this.asBigInt === rhs.asBigInt;
|
|
132
114
|
}
|
|
133
115
|
|
|
134
116
|
lt(rhs: BaseField): boolean {
|
|
135
|
-
return this.
|
|
117
|
+
return this.asBigInt < rhs.asBigInt;
|
|
136
118
|
}
|
|
137
119
|
|
|
138
120
|
cmp(rhs: BaseField): -1 | 0 | 1 {
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
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.
|
|
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';
|
package/src/schemas/schemas.ts
CHANGED
|
@@ -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
|
|
package/src/schemas/types.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/sleep/index.ts
CHANGED
|
@@ -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
package/src/types/index.ts
CHANGED
|
@@ -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 +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
|