@aztec/foundation 0.23.0 → 0.26.1
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/abi/abi.d.ts +1 -1
- package/dest/abi/abi.d.ts.map +1 -1
- package/dest/abi/event_selector.d.ts +45 -0
- package/dest/abi/event_selector.d.ts.map +1 -0
- package/dest/abi/event_selector.js +60 -0
- package/dest/abi/function_selector.d.ts +69 -0
- package/dest/abi/function_selector.d.ts.map +1 -0
- package/dest/abi/function_selector.js +86 -0
- package/dest/abi/index.d.ts +2 -1
- package/dest/abi/index.d.ts.map +1 -1
- package/dest/abi/index.js +3 -2
- package/dest/abi/selector.d.ts +4 -106
- package/dest/abi/selector.d.ts.map +1 -1
- package/dest/abi/selector.js +8 -143
- package/dest/abi/utils.js +3 -3
- package/dest/array/array.d.ts +8 -0
- package/dest/array/array.d.ts.map +1 -1
- package/dest/array/array.js +26 -1
- package/dest/aztec-address/index.d.ts +8 -1
- package/dest/aztec-address/index.d.ts.map +1 -1
- package/dest/aztec-address/index.js +16 -2
- package/dest/crypto/index.d.ts +1 -0
- package/dest/crypto/index.d.ts.map +1 -1
- package/dest/crypto/index.js +2 -1
- package/dest/crypto/pedersen/pedersen.wasm.d.ts +2 -1
- package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -1
- package/dest/crypto/pedersen/pedersen.wasm.js +7 -6
- package/dest/crypto/poseidon/index.d.ts +8 -0
- package/dest/crypto/poseidon/index.d.ts.map +1 -0
- package/dest/crypto/poseidon/index.js +12 -0
- package/dest/eth-address/index.d.ts +3 -0
- package/dest/eth-address/index.d.ts.map +1 -1
- package/dest/eth-address/index.js +5 -2
- package/dest/fields/fields.d.ts +19 -0
- package/dest/fields/fields.d.ts.map +1 -1
- package/dest/fields/fields.js +11 -4
- package/dest/json-rpc/client/json_rpc_client.js +2 -2
- package/dest/serialize/buffer_reader.d.ts +15 -0
- package/dest/serialize/buffer_reader.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.js +18 -1
- package/dest/serialize/serialize.d.ts +3 -5
- package/dest/serialize/serialize.d.ts.map +1 -1
- package/dest/serialize/serialize.js +17 -8
- package/package.json +2 -2
- package/src/abi/abi.ts +337 -0
- package/src/abi/buffer.ts +36 -0
- package/src/abi/decoder.ts +176 -0
- package/src/abi/encoder.ts +143 -0
- package/src/abi/event_selector.ts +73 -0
- package/src/abi/function_selector.ts +122 -0
- package/src/abi/index.ts +7 -0
- package/src/abi/selector.ts +63 -0
- package/src/abi/utils.ts +50 -0
- package/src/array/array.ts +119 -0
- package/src/array/index.ts +1 -0
- package/src/async-map/index.ts +18 -0
- package/src/aztec-address/index.ts +56 -0
- package/src/bigint-buffer/index.ts +87 -0
- package/src/collection/array.ts +64 -0
- package/src/collection/index.ts +1 -0
- package/src/committable/committable.ts +46 -0
- package/src/committable/index.ts +1 -0
- package/src/crypto/index.ts +17 -0
- package/src/crypto/keccak/index.ts +33 -0
- package/src/crypto/pedersen/index.ts +1 -0
- package/src/crypto/pedersen/pedersen.elliptic.ts +584 -0
- package/src/crypto/pedersen/pedersen.noble.ts +573 -0
- package/src/crypto/pedersen/pedersen.wasm.ts +46 -0
- package/src/crypto/poseidon/index.ts +17 -0
- package/src/crypto/random/index.ts +42 -0
- package/src/crypto/sha256/index.ts +3 -0
- package/src/errors/index.ts +6 -0
- package/src/eth-address/index.ts +239 -0
- package/src/fields/coordinate.ts +104 -0
- package/src/fields/fields.ts +338 -0
- package/src/fields/index.ts +3 -0
- package/src/fields/point.ts +145 -0
- package/src/fifo/bounded_serial_queue.ts +100 -0
- package/src/fifo/index.ts +4 -0
- package/src/fifo/memory_fifo.ts +118 -0
- package/src/fifo/semaphore.ts +33 -0
- package/src/fifo/serial_queue.ts +81 -0
- package/src/index.ts +29 -0
- package/src/json-rpc/README.md +55 -0
- package/src/json-rpc/class_converter.ts +213 -0
- package/src/json-rpc/client/index.ts +1 -0
- package/src/json-rpc/client/json_rpc_client.ts +147 -0
- package/src/json-rpc/convert.ts +163 -0
- package/src/json-rpc/fixtures/class_a.ts +15 -0
- package/src/json-rpc/fixtures/class_b.ts +15 -0
- package/src/json-rpc/fixtures/test_state.ts +59 -0
- package/src/json-rpc/index.ts +8 -0
- package/src/json-rpc/js_utils.ts +20 -0
- package/src/json-rpc/server/index.ts +2 -0
- package/src/json-rpc/server/json_proxy.ts +60 -0
- package/src/json-rpc/server/json_rpc_server.ts +269 -0
- package/src/log/console.ts +39 -0
- package/src/log/debug.ts +83 -0
- package/src/log/index.ts +5 -0
- package/src/log/log_fn.ts +5 -0
- package/src/log/log_history.ts +44 -0
- package/src/log/logger.ts +137 -0
- package/src/mutex/index.ts +83 -0
- package/src/mutex/mutex_database.ts +12 -0
- package/src/noir/index.ts +1 -0
- package/src/noir/noir_package_config.ts +54 -0
- package/src/retry/index.ts +99 -0
- package/src/running-promise/index.ts +60 -0
- package/src/serialize/buffer_reader.ts +309 -0
- package/src/serialize/field_reader.ts +143 -0
- package/src/serialize/free_funcs.ts +147 -0
- package/src/serialize/index.ts +5 -0
- package/src/serialize/serialize.ts +310 -0
- package/src/serialize/types.ts +40 -0
- package/src/sleep/index.ts +71 -0
- package/src/testing/index.ts +1 -0
- package/src/testing/test_data.ts +36 -0
- package/src/timer/elapsed.ts +23 -0
- package/src/timer/index.ts +3 -0
- package/src/timer/timeout.ts +64 -0
- package/src/timer/timer.ts +48 -0
- package/src/transport/browser/index.ts +4 -0
- package/src/transport/browser/message_port_socket.ts +48 -0
- package/src/transport/browser/shared_worker_connector.ts +21 -0
- package/src/transport/browser/shared_worker_listener.ts +53 -0
- package/src/transport/browser/worker_connector.ts +30 -0
- package/src/transport/browser/worker_listener.ts +54 -0
- package/src/transport/dispatch/create_dispatch_fn.ts +35 -0
- package/src/transport/dispatch/create_dispatch_proxy.ts +141 -0
- package/src/transport/dispatch/messages.ts +58 -0
- package/src/transport/index.ts +11 -0
- package/src/transport/interface/connector.ts +9 -0
- package/src/transport/interface/listener.ts +16 -0
- package/src/transport/interface/socket.ts +15 -0
- package/src/transport/interface/transferable.ts +125 -0
- package/src/transport/node/index.ts +2 -0
- package/src/transport/node/node_connector.ts +30 -0
- package/src/transport/node/node_connector_socket.ts +52 -0
- package/src/transport/node/node_listener.ts +34 -0
- package/src/transport/node/node_listener_socket.ts +48 -0
- package/src/transport/transport_client.ts +131 -0
- package/src/transport/transport_server.ts +108 -0
- package/src/trees/index.ts +54 -0
- package/src/types/index.ts +8 -0
- package/src/url/index.ts +73 -0
- package/src/wasm/README.md +6 -0
- package/src/wasm/empty_wasi_sdk.ts +166 -0
- package/src/wasm/fixtures/gcd.wasm +0 -0
- package/src/wasm/fixtures/gcd.wat +27 -0
- package/src/wasm/index.ts +1 -0
- package/src/wasm/wasm_module.ts +260 -0
- package/src/worker/browser/index.ts +2 -0
- package/src/worker/browser/start_web_module.ts +23 -0
- package/src/worker/browser/web_data_store.ts +38 -0
- package/src/worker/browser/web_worker.ts +24 -0
- package/src/worker/data_store.ts +19 -0
- package/src/worker/index.ts +2 -0
- package/src/worker/node/index.ts +2 -0
- package/src/worker/node/node_data_store.ts +27 -0
- package/src/worker/node/node_worker.ts +22 -0
- package/src/worker/node/start_node_module.ts +29 -0
- package/src/worker/wasm_worker.ts +7 -0
- package/src/worker/worker_pool.ts +73 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { Tuple } from '../serialize/index.js';
|
|
2
|
+
|
|
3
|
+
export type { FieldsOf } from '../types/index.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Create an array over an integer range.
|
|
7
|
+
* @param n - The number of integers.
|
|
8
|
+
* @param offset - The starting number.
|
|
9
|
+
* @returns The array of numbers.
|
|
10
|
+
*/
|
|
11
|
+
export function range(n: number, offset = 0) {
|
|
12
|
+
const ret: number[] = [];
|
|
13
|
+
for (let i = 0; i < n; i++) {
|
|
14
|
+
ret.push(offset + i);
|
|
15
|
+
}
|
|
16
|
+
return ret;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create an array over an integer range, filled with a function 'fn'.
|
|
21
|
+
* This is used over e.g. lodash because it resolved to a tuple type, needed for our fixed array type safety.
|
|
22
|
+
* @param n - The number of integers.
|
|
23
|
+
* @param fn - The generator function.
|
|
24
|
+
* @returns The array of numbers.
|
|
25
|
+
*/
|
|
26
|
+
export function makeTuple<T, N extends number>(length: N, fn: (i: number) => T, offset = 0) {
|
|
27
|
+
return Array.from({ length }, (v: any, i: number) => fn(i + offset)) as Tuple<T, N>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Create an array over an integer range, filled with a function 'fn'. However, the latter half of the array are set to zeros.
|
|
32
|
+
* see `makeTuple` above.
|
|
33
|
+
* @param n - The number of integers.
|
|
34
|
+
* @param fn - The generator function.
|
|
35
|
+
* @returns The array of numbers.
|
|
36
|
+
*/
|
|
37
|
+
export function makeHalfFullTuple<T, N extends number>(length: N, fn: (i: number) => T, offset = 0) {
|
|
38
|
+
return Array.from({ length }, (v: any, i: number) => (i < length / 2 ? fn(i + offset) : fn(0))) as Tuple<T, N>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Assert a member of an object is a certain length.
|
|
43
|
+
* @param obj - An object.
|
|
44
|
+
* @param member - A member string.
|
|
45
|
+
* @param length - The length.
|
|
46
|
+
*/
|
|
47
|
+
export function assertMemberLength<
|
|
48
|
+
F extends string,
|
|
49
|
+
T extends {
|
|
50
|
+
[f in F]: {
|
|
51
|
+
/**
|
|
52
|
+
* A property which the tested member of the object T has to have.
|
|
53
|
+
*/
|
|
54
|
+
length: number;
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
>(obj: T, member: F, length: number) {
|
|
58
|
+
if (obj[member].length !== length) {
|
|
59
|
+
throw new Error(`Expected ${member} to have length ${length} but was ${obj[member].length}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Assert all subarrays in a member of an object are a certain length.
|
|
65
|
+
* @param obj - An object.
|
|
66
|
+
* @param member - A member string.
|
|
67
|
+
* @param length - The expected length for each subarray.
|
|
68
|
+
*/
|
|
69
|
+
export function assertItemsLength<
|
|
70
|
+
F extends string,
|
|
71
|
+
T extends {
|
|
72
|
+
[f in F]: {
|
|
73
|
+
/**
|
|
74
|
+
* A property which the tested member of the object T has to have.
|
|
75
|
+
*/
|
|
76
|
+
length: number;
|
|
77
|
+
}[];
|
|
78
|
+
},
|
|
79
|
+
>(obj: T, member: F, length: number) {
|
|
80
|
+
const arrays = obj[member];
|
|
81
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
82
|
+
if (arrays[i].length !== length) {
|
|
83
|
+
throw new Error(`Expected ${member}[${i}] to have length ${length} but was ${arrays[i].length}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Checks that the permutation is valid. Throws an error if it is not.
|
|
90
|
+
* @param original - The original array.
|
|
91
|
+
* @param permutation - The array which is allegedly a permutation of the original.
|
|
92
|
+
* @param indexes - The indices of the original array which the permutation should map to.
|
|
93
|
+
* @param isEqual - A function to compare the elements of the original and permutation arrays.
|
|
94
|
+
*/
|
|
95
|
+
export function assertPermutation<T>(
|
|
96
|
+
original: T[],
|
|
97
|
+
permutation: T[],
|
|
98
|
+
indexes: number[],
|
|
99
|
+
isEqual: (a: T, b: T) => boolean,
|
|
100
|
+
): void {
|
|
101
|
+
if (original.length !== permutation.length || original.length !== indexes.length) {
|
|
102
|
+
throw new Error(`Invalid lengths: ${original.length}, ${permutation.length}, ${indexes.length}`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const seenValue = new Set<number>();
|
|
106
|
+
for (let i = 0; i < indexes.length; i++) {
|
|
107
|
+
const index = indexes[i];
|
|
108
|
+
const permutedValue = permutation[i];
|
|
109
|
+
const originalValueAtIndex = original[index];
|
|
110
|
+
|
|
111
|
+
if (!isEqual(permutedValue, originalValueAtIndex)) {
|
|
112
|
+
throw new Error(`Invalid permutation at index ${index}: ${permutedValue} !== ${originalValueAtIndex}`);
|
|
113
|
+
}
|
|
114
|
+
if (seenValue.has(index)) {
|
|
115
|
+
throw new Error(`Duplicate index in permutation: ${index}`);
|
|
116
|
+
}
|
|
117
|
+
seenValue.add(index);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './array.js';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps an array of elements by applying an asynchronous function to each element in sequence,
|
|
3
|
+
* and returns a new array with the results. The function receives each element of the array
|
|
4
|
+
* and its index as arguments, and should return a Promise that resolves to the desired value.
|
|
5
|
+
*
|
|
6
|
+
* @typeParam T - The original array element type.
|
|
7
|
+
* @typeParam U - The resulting array element type.
|
|
8
|
+
* @param arr - The array to map.
|
|
9
|
+
* @param fn - The async function to apply on each element of the array.
|
|
10
|
+
* @returns A Promise that resolves to a new array containing the mapped values.
|
|
11
|
+
*/
|
|
12
|
+
export async function asyncMap<T, U>(arr: T[], fn: (e: T, i: number) => Promise<U>): Promise<U[]> {
|
|
13
|
+
const results: U[] = [];
|
|
14
|
+
for (let i = 0; i < arr.length; ++i) {
|
|
15
|
+
results.push(await fn(arr[i], i));
|
|
16
|
+
}
|
|
17
|
+
return results;
|
|
18
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { inspect } from 'util';
|
|
2
|
+
|
|
3
|
+
import { Fr, fromBuffer } from '../fields/index.js';
|
|
4
|
+
import { BufferReader, FieldReader } from '../serialize/index.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* AztecAddress represents a 32-byte address in the Aztec Protocol.
|
|
8
|
+
* It provides methods to create, manipulate, and compare addresses.
|
|
9
|
+
* The maximum value of an address is determined by the field modulus and all instances of AztecAddress.
|
|
10
|
+
* It should have a value less than or equal to this max value.
|
|
11
|
+
* This class also provides helper functions to convert addresses from strings, buffers, and other formats.
|
|
12
|
+
*/
|
|
13
|
+
export class AztecAddress extends Fr {
|
|
14
|
+
constructor(buffer: Buffer) {
|
|
15
|
+
if (buffer.length !== 32) {
|
|
16
|
+
throw new Error(`Invalid AztecAddress length ${buffer.length}.`);
|
|
17
|
+
}
|
|
18
|
+
super(buffer);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
[inspect.custom]() {
|
|
22
|
+
return `AztecAddress<${this.toString()}>`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static ZERO = new AztecAddress(Buffer.alloc(32));
|
|
26
|
+
|
|
27
|
+
static zero(): AztecAddress {
|
|
28
|
+
return AztecAddress.ZERO;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static fromBuffer(buffer: Buffer | BufferReader) {
|
|
32
|
+
return fromBuffer(buffer, AztecAddress);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static fromField(fr: Fr) {
|
|
36
|
+
return new AztecAddress(fr.toBuffer());
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static fromFields(fields: Fr[] | FieldReader) {
|
|
40
|
+
const reader = FieldReader.asReader(fields);
|
|
41
|
+
return AztecAddress.fromField(reader.readField());
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static fromBigInt(value: bigint) {
|
|
45
|
+
return AztecAddress.fromField(new Fr(value));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static fromString(buf: string) {
|
|
49
|
+
const buffer = Buffer.from(buf.replace(/^0x/i, ''), 'hex');
|
|
50
|
+
return new AztecAddress(buffer);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static random() {
|
|
54
|
+
return new AztecAddress(super.random().toBuffer());
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a little-endian buffer into a BigInt.
|
|
3
|
+
* @param buf - The little-endian buffer to convert.
|
|
4
|
+
* @returns A BigInt with the little-endian representation of buf.
|
|
5
|
+
*/
|
|
6
|
+
export function toBigIntLE(buf: Buffer): bigint {
|
|
7
|
+
const reversed = Buffer.from(buf);
|
|
8
|
+
reversed.reverse();
|
|
9
|
+
const hex = reversed.toString('hex');
|
|
10
|
+
if (hex.length === 0) {
|
|
11
|
+
return BigInt(0);
|
|
12
|
+
}
|
|
13
|
+
return BigInt(`0x${hex}`);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Convert a big-endian buffer into a BigInt.
|
|
18
|
+
* @param buf - The big-endian buffer to convert.
|
|
19
|
+
* @returns A BigInt with the big-endian representation of buf.
|
|
20
|
+
*/
|
|
21
|
+
export function toBigIntBE(buf: Buffer): bigint {
|
|
22
|
+
const hex = buf.toString('hex');
|
|
23
|
+
if (hex.length === 0) {
|
|
24
|
+
return BigInt(0);
|
|
25
|
+
}
|
|
26
|
+
return BigInt(`0x${hex}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Convert a BigInt to a little-endian buffer.
|
|
31
|
+
* @param num - The BigInt to convert.
|
|
32
|
+
* @param width - The number of bytes that the resulting buffer should be.
|
|
33
|
+
* @returns A little-endian buffer representation of num.
|
|
34
|
+
*/
|
|
35
|
+
export function toBufferLE(num: bigint, width: number): Buffer {
|
|
36
|
+
if (num < BigInt(0)) {
|
|
37
|
+
throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferLE.`);
|
|
38
|
+
}
|
|
39
|
+
const hex = num.toString(16);
|
|
40
|
+
const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
|
|
41
|
+
buffer.reverse();
|
|
42
|
+
return buffer;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Convert a BigInt to a big-endian buffer.
|
|
47
|
+
* @param num - The BigInt to convert.
|
|
48
|
+
* @param width - The number of bytes that the resulting buffer should be.
|
|
49
|
+
* @returns A big-endian buffer representation of num.
|
|
50
|
+
*/
|
|
51
|
+
export function toBufferBE(num: bigint, width: number): Buffer {
|
|
52
|
+
if (num < BigInt(0)) {
|
|
53
|
+
throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferBE.`);
|
|
54
|
+
}
|
|
55
|
+
const hex = num.toString(16);
|
|
56
|
+
const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
|
|
57
|
+
if (buffer.length > width) {
|
|
58
|
+
throw new Error(`Number ${num.toString(16)} does not fit in ${width}`);
|
|
59
|
+
}
|
|
60
|
+
return buffer;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Converts a BigInt to its hex representation.
|
|
65
|
+
* @param num - The BigInt to convert.
|
|
66
|
+
* @param padTo32 - Whether to pad the resulting string to 32 bytes.
|
|
67
|
+
* @returns An even-length 0x-prefixed string.
|
|
68
|
+
*/
|
|
69
|
+
export function toHex(num: bigint, padTo32 = false): `0x${string}` {
|
|
70
|
+
const str = num.toString(16);
|
|
71
|
+
const targetLen = str.length % 2 === 0 ? str.length : str.length + 1;
|
|
72
|
+
const paddedStr = str.padStart(padTo32 ? 64 : targetLen, '0');
|
|
73
|
+
return `0x${paddedStr}`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Converts a hex string to a buffer. Throws if input is not a valid hex string.
|
|
78
|
+
* @param value - The hex string to convert. May be 0x prefixed or not.
|
|
79
|
+
* @returns A buffer.
|
|
80
|
+
*/
|
|
81
|
+
export function fromHex(value: string): Buffer {
|
|
82
|
+
const hexRegex = /^(0x)?[0-9a-fA-F]*$/;
|
|
83
|
+
if (!hexRegex.test(value) || value.length % 2 !== 0) {
|
|
84
|
+
throw new Error(`Invalid hex string: ${value}`);
|
|
85
|
+
}
|
|
86
|
+
return Buffer.from(value.replace(/^0x/i, ''), 'hex');
|
|
87
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Tuple } from '../serialize/types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Pads an array to the target length by appending an element to its end. Throws if target length exceeds the input array length. Does not modify the input array.
|
|
5
|
+
* @param arr - Array with elements to pad.
|
|
6
|
+
* @param elem - Element to use for padding.
|
|
7
|
+
* @param length - Target length.
|
|
8
|
+
* @returns A new padded array.
|
|
9
|
+
*/
|
|
10
|
+
export function padArrayEnd<T, N extends number>(arr: T[], elem: T, length: N): Tuple<T, N> {
|
|
11
|
+
if (arr.length > length) {
|
|
12
|
+
throw new Error(`Array size exceeds target length`);
|
|
13
|
+
}
|
|
14
|
+
// Since typescript cannot always deduce that something is a tuple, we cast
|
|
15
|
+
return [...arr, ...Array(length - arr.length).fill(elem)] as Tuple<T, N>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Pads an array to the target length by prepending elements at the beginning. Throws if target length exceeds the input array length. Does not modify the input array.
|
|
20
|
+
* @param arr - Array with elements to pad.
|
|
21
|
+
* @param elem - Element to use for padding.
|
|
22
|
+
* @param length - Target length.
|
|
23
|
+
* @returns A new padded array.
|
|
24
|
+
*/
|
|
25
|
+
export function padArrayStart<T, N extends number>(arr: T[], elem: T, length: N): Tuple<T, N> {
|
|
26
|
+
if (arr.length > length) {
|
|
27
|
+
throw new Error(`Array size exceeds target length`);
|
|
28
|
+
}
|
|
29
|
+
// Since typescript cannot always deduce that something is a tuple, we cast
|
|
30
|
+
return [...Array(length - arr.length).fill(elem), ...arr] as Tuple<T, N>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Returns if an array is composed of empty items.
|
|
35
|
+
* @param arr - Array to check.
|
|
36
|
+
* @returns True if every item in the array isEmpty.
|
|
37
|
+
*/
|
|
38
|
+
export function isArrayEmpty<T>(arr: T[], isEmpty: (item: T) => boolean): boolean {
|
|
39
|
+
for (const item of arr) {
|
|
40
|
+
if (!isEmpty(item)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Returns the number of non-empty items in an array.
|
|
49
|
+
* @param arr - Array to check.
|
|
50
|
+
* @returns Number of non-empty items in an array.
|
|
51
|
+
*/
|
|
52
|
+
export function arrayNonEmptyLength<T>(arr: T[], isEmpty: (item: T) => boolean): number {
|
|
53
|
+
return arr.reduce((sum, item) => (isEmpty(item) ? sum : sum + 1), 0);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Executes the given function n times and returns the results in an array.
|
|
58
|
+
* @param n - How many times to repeat.
|
|
59
|
+
* @param fn - Mapper from index to value.
|
|
60
|
+
* @returns The array with the result from all executions.
|
|
61
|
+
*/
|
|
62
|
+
export function times<T>(n: number, fn: (i: number) => T): T[] {
|
|
63
|
+
return [...Array(n).keys()].map(i => fn(i));
|
|
64
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './array.js';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A class that allows for a value to be committed or rolled back.
|
|
3
|
+
*/
|
|
4
|
+
export class Committable<T> {
|
|
5
|
+
private currentValue: T;
|
|
6
|
+
private nextValue: T | undefined = undefined;
|
|
7
|
+
|
|
8
|
+
constructor(initialValue: T) {
|
|
9
|
+
this.currentValue = initialValue;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Commits the uncommitted value.
|
|
14
|
+
*/
|
|
15
|
+
public commit() {
|
|
16
|
+
if (this.nextValue === undefined) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
this.currentValue = this.nextValue;
|
|
20
|
+
this.nextValue = undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Rolls back the uncommitted value.
|
|
25
|
+
*/
|
|
26
|
+
public rollback() {
|
|
27
|
+
this.nextValue === undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Gets the current value.
|
|
32
|
+
* @param includeUncommitted - Whether to include the uncommitted value.
|
|
33
|
+
* @returns The current value if includeUncommitted is false, otherwise the uncommitted value.
|
|
34
|
+
*/
|
|
35
|
+
public get(includeUncommitted: boolean = false): T {
|
|
36
|
+
return includeUncommitted && this.nextValue ? this.nextValue : this.currentValue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Sets the next value to be committed to.
|
|
41
|
+
* @param value - The new value to be set.
|
|
42
|
+
*/
|
|
43
|
+
public set(value: T) {
|
|
44
|
+
this.nextValue = value;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './committable.js';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BarretenbergSync } from '@aztec/bb.js';
|
|
2
|
+
|
|
3
|
+
export * from './keccak/index.js';
|
|
4
|
+
export * from './random/index.js';
|
|
5
|
+
export * from './sha256/index.js';
|
|
6
|
+
export * from './pedersen/index.js';
|
|
7
|
+
export * from './poseidon/index.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Init the bb singleton. This constructs (if not already) the barretenberg sync api within bb.js itself.
|
|
11
|
+
* It takes about 100-200ms to initialize. It may not seem like much, but when in conjunction with many other things
|
|
12
|
+
* initializing, developers may want to pick precisely when to incur this cost.
|
|
13
|
+
* If in a test environment, we'll just do it on module load.
|
|
14
|
+
*/
|
|
15
|
+
export async function init() {
|
|
16
|
+
await BarretenbergSync.initSingleton();
|
|
17
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Keccak } from 'sha3';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Computes the Keccak-256 hash of the given input buffer.
|
|
5
|
+
*
|
|
6
|
+
* @param input - The input buffer to be hashed.
|
|
7
|
+
* @returns The computed Keccak-256 hash as a Buffer.
|
|
8
|
+
*/
|
|
9
|
+
export function keccak(input: Buffer) {
|
|
10
|
+
const hash = new Keccak(256);
|
|
11
|
+
return hash.update(input).digest();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Computes the keccak-256 hash of a given input string and returns the result as a hexadecimal string.
|
|
16
|
+
*/
|
|
17
|
+
export function keccak256String(input: string) {
|
|
18
|
+
const hash = new Keccak(256);
|
|
19
|
+
hash.reset();
|
|
20
|
+
hash.update(input);
|
|
21
|
+
return hash.digest('hex');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Computes the Keccak-224 hash of the given input buffer.
|
|
26
|
+
*
|
|
27
|
+
* @param input - The input buffer to be hashed.
|
|
28
|
+
* @returns The computed Keccak-224 hash as a Buffer.
|
|
29
|
+
*/
|
|
30
|
+
export function keccak224(input: Buffer) {
|
|
31
|
+
const hash = new Keccak(224);
|
|
32
|
+
return hash.update(input).digest();
|
|
33
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './pedersen.wasm.js';
|