@aztec/foundation 0.24.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.
Files changed (60) hide show
  1. package/dest/abi/abi.d.ts +1 -1
  2. package/dest/abi/abi.d.ts.map +1 -1
  3. package/dest/abi/event_selector.d.ts +45 -0
  4. package/dest/abi/event_selector.d.ts.map +1 -0
  5. package/dest/abi/event_selector.js +60 -0
  6. package/dest/abi/function_selector.d.ts +69 -0
  7. package/dest/abi/function_selector.d.ts.map +1 -0
  8. package/dest/abi/function_selector.js +86 -0
  9. package/dest/abi/index.d.ts +2 -1
  10. package/dest/abi/index.d.ts.map +1 -1
  11. package/dest/abi/index.js +3 -2
  12. package/dest/abi/selector.d.ts +4 -106
  13. package/dest/abi/selector.d.ts.map +1 -1
  14. package/dest/abi/selector.js +8 -143
  15. package/dest/abi/utils.js +3 -3
  16. package/dest/array/array.d.ts +8 -0
  17. package/dest/array/array.d.ts.map +1 -1
  18. package/dest/array/array.js +26 -1
  19. package/dest/aztec-address/index.d.ts +8 -1
  20. package/dest/aztec-address/index.d.ts.map +1 -1
  21. package/dest/aztec-address/index.js +16 -2
  22. package/dest/crypto/index.d.ts +1 -0
  23. package/dest/crypto/index.d.ts.map +1 -1
  24. package/dest/crypto/index.js +2 -1
  25. package/dest/crypto/pedersen/pedersen.wasm.d.ts +2 -1
  26. package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -1
  27. package/dest/crypto/pedersen/pedersen.wasm.js +7 -6
  28. package/dest/crypto/poseidon/index.d.ts +8 -0
  29. package/dest/crypto/poseidon/index.d.ts.map +1 -0
  30. package/dest/crypto/poseidon/index.js +12 -0
  31. package/dest/eth-address/index.d.ts +3 -0
  32. package/dest/eth-address/index.d.ts.map +1 -1
  33. package/dest/eth-address/index.js +5 -2
  34. package/dest/fields/fields.d.ts +19 -0
  35. package/dest/fields/fields.d.ts.map +1 -1
  36. package/dest/fields/fields.js +9 -2
  37. package/dest/json-rpc/client/json_rpc_client.js +2 -2
  38. package/dest/serialize/buffer_reader.d.ts +15 -0
  39. package/dest/serialize/buffer_reader.d.ts.map +1 -1
  40. package/dest/serialize/buffer_reader.js +18 -1
  41. package/dest/serialize/serialize.d.ts +3 -5
  42. package/dest/serialize/serialize.d.ts.map +1 -1
  43. package/dest/serialize/serialize.js +17 -8
  44. package/package.json +2 -2
  45. package/src/abi/abi.ts +1 -1
  46. package/src/abi/event_selector.ts +73 -0
  47. package/src/abi/function_selector.ts +122 -0
  48. package/src/abi/index.ts +2 -1
  49. package/src/abi/selector.ts +8 -188
  50. package/src/abi/utils.ts +2 -2
  51. package/src/array/array.ts +33 -0
  52. package/src/aztec-address/index.ts +22 -2
  53. package/src/crypto/index.ts +1 -0
  54. package/src/crypto/pedersen/pedersen.wasm.ts +14 -10
  55. package/src/crypto/poseidon/index.ts +17 -0
  56. package/src/eth-address/index.ts +6 -1
  57. package/src/fields/fields.ts +11 -1
  58. package/src/json-rpc/client/json_rpc_client.ts +1 -1
  59. package/src/serialize/buffer_reader.ts +23 -0
  60. package/src/serialize/serialize.ts +14 -7
@@ -1,5 +1,7 @@
1
- import { Fr } from '../fields/index.js';
2
- import { FieldReader } from '../serialize/index.js';
1
+ import { inspect } from 'util';
2
+
3
+ import { Fr, fromBuffer } from '../fields/index.js';
4
+ import { BufferReader, FieldReader } from '../serialize/index.js';
3
5
 
4
6
  /**
5
7
  * AztecAddress represents a 32-byte address in the Aztec Protocol.
@@ -16,6 +18,20 @@ export class AztecAddress extends Fr {
16
18
  super(buffer);
17
19
  }
18
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
+
19
35
  static fromField(fr: Fr) {
20
36
  return new AztecAddress(fr.toBuffer());
21
37
  }
@@ -33,4 +49,8 @@ export class AztecAddress extends Fr {
33
49
  const buffer = Buffer.from(buf.replace(/^0x/i, ''), 'hex');
34
50
  return new AztecAddress(buffer);
35
51
  }
52
+
53
+ static random() {
54
+ return new AztecAddress(super.random().toBuffer());
55
+ }
36
56
  }
@@ -4,6 +4,7 @@ export * from './keccak/index.js';
4
4
  export * from './random/index.js';
5
5
  export * from './sha256/index.js';
6
6
  export * from './pedersen/index.js';
7
+ export * from './poseidon/index.js';
7
8
 
8
9
  /**
9
10
  * Init the bb singleton. This constructs (if not already) the barretenberg sync api within bb.js itself.
@@ -1,4 +1,6 @@
1
- import { BarretenbergSync, Fr } from '@aztec/bb.js';
1
+ import { BarretenbergSync, Fr as FrBarretenberg } from '@aztec/bb.js';
2
+
3
+ import { Fr } from '../../fields/fields.js';
2
4
 
3
5
  /**
4
6
  * Create a pedersen commitment (point) from an array of input fields.
@@ -9,7 +11,7 @@ export function pedersenCommit(input: Buffer[]) {
9
11
  throw new Error('All Pedersen Commit input buffers must be <= 32 bytes.');
10
12
  }
11
13
  input = input.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i));
12
- const point = BarretenbergSync.getSingleton().pedersenCommit(input.map(i => new Fr(i)));
14
+ const point = BarretenbergSync.getSingleton().pedersenCommit(input.map(i => new FrBarretenberg(i)));
13
15
  // toBuffer returns Uint8Arrays (browser/worker-boundary friendly).
14
16
  // TODO: rename toTypedArray()?
15
17
  return [Buffer.from(point.x.toBuffer()), Buffer.from(point.y.toBuffer())];
@@ -19,18 +21,20 @@ export function pedersenCommit(input: Buffer[]) {
19
21
  * Create a pedersen hash (field) from an array of input fields.
20
22
  * Left pads any inputs less than 32 bytes.
21
23
  */
22
- export function pedersenHash(input: Buffer[], index = 0) {
24
+ export function pedersenHash(input: Buffer[], index = 0): Fr {
23
25
  if (!input.every(i => i.length <= 32)) {
24
26
  throw new Error('All Pedersen Hash input buffers must be <= 32 bytes.');
25
27
  }
26
28
  input = input.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i));
27
- return Buffer.from(
28
- BarretenbergSync.getSingleton()
29
- .pedersenHash(
30
- input.map(i => new Fr(i)),
31
- index,
32
- )
33
- .toBuffer(),
29
+ return Fr.fromBuffer(
30
+ Buffer.from(
31
+ BarretenbergSync.getSingleton()
32
+ .pedersenHash(
33
+ input.map(i => new FrBarretenberg(i)),
34
+ index,
35
+ )
36
+ .toBuffer(),
37
+ ),
34
38
  );
35
39
  }
36
40
 
@@ -0,0 +1,17 @@
1
+ import { BarretenbergSync, Fr as FrBarretenberg } from '@aztec/bb.js';
2
+
3
+ import { Fr } from '../../fields/fields.js';
4
+
5
+ /**
6
+ * Create a poseidon hash (field) from an array of input fields.
7
+ * Left pads any inputs less than 32 bytes.
8
+ */
9
+ export function poseidonHash(input: Buffer[]): Fr {
10
+ return Fr.fromBuffer(
11
+ Buffer.from(
12
+ BarretenbergSync.getSingleton()
13
+ .poseidonHash(input.map(i => new FrBarretenberg(i)))
14
+ .toBuffer(),
15
+ ),
16
+ );
17
+ }
@@ -1,3 +1,5 @@
1
+ import { inspect } from 'util';
2
+
1
3
  import { keccak256String } from '../crypto/keccak/index.js';
2
4
  import { randomBytes } from '../crypto/random/index.js';
3
5
  import { Fr } from '../fields/index.js';
@@ -158,6 +160,10 @@ export class EthAddress {
158
160
  return `0x${this.buffer.toString('hex')}` as `0x${string}`;
159
161
  }
160
162
 
163
+ [inspect.custom]() {
164
+ return `EthAddress<${this.toString()}>`;
165
+ }
166
+
161
167
  /**
162
168
  * Returns the Ethereum address as a checksummed string.
163
169
  * The output string will have characters in the correct upper or lowercase form, according to EIP-55.
@@ -184,7 +190,6 @@ export class EthAddress {
184
190
  *
185
191
  * @returns A 32-byte Buffer containing the padded Ethereum address.
186
192
  */
187
- // TODO(#3938): nuke this
188
193
  public toBuffer32() {
189
194
  const buffer = Buffer.alloc(32);
190
195
  this.buffer.copy(buffer, 12);
@@ -1,3 +1,5 @@
1
+ import { inspect } from 'util';
2
+
1
3
  import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
2
4
  import { randomBytes } from '../crypto/random/index.js';
3
5
  import { BufferReader } from '../serialize/buffer_reader.js';
@@ -137,7 +139,7 @@ abstract class BaseField {
137
139
  * Constructs a field from a Buffer of BufferReader.
138
140
  * It maybe not read the full 32 bytes if the Buffer is shorter, but it will padded in BaseField constructor.
139
141
  */
140
- function fromBuffer<T extends BaseField>(buffer: Buffer | BufferReader, f: DerivedField<T>) {
142
+ export function fromBuffer<T extends BaseField>(buffer: Buffer | BufferReader, f: DerivedField<T>) {
141
143
  const reader = BufferReader.asReader(buffer);
142
144
  return new f(reader.readBytes(BaseField.SIZE_IN_BYTES));
143
145
  }
@@ -185,6 +187,10 @@ export class Fr extends BaseField {
185
187
  super(value);
186
188
  }
187
189
 
190
+ [inspect.custom]() {
191
+ return `Fr<${this.toString()}>`;
192
+ }
193
+
188
194
  protected modulus() {
189
195
  return Fr.MODULUS;
190
196
  }
@@ -251,6 +257,10 @@ export class Fq extends BaseField {
251
257
  private static HIGH_SHIFT = BigInt((BaseField.SIZE_IN_BYTES / 2) * 8);
252
258
  private static LOW_MASK = (1n << Fq.HIGH_SHIFT) - 1n;
253
259
 
260
+ [inspect.custom]() {
261
+ return `Fq<${this.toString()}>`;
262
+ }
263
+
254
264
  get low(): Fr {
255
265
  return new Fr(this.toBigInt() & Fq.LOW_MASK);
256
266
  }
@@ -77,7 +77,7 @@ export function makeFetch(retries: number[], noRetry: boolean, log?: DebugLogger
77
77
  return async (host: string, rpcMethod: string, body: any, useApiEndpoints: boolean) => {
78
78
  return await retry(
79
79
  () => defaultFetch(host, rpcMethod, body, useApiEndpoints, noRetry),
80
- `JsonRpcClient request to ${host}`,
80
+ `JsonRpcClient request ${rpcMethod} to ${host}`,
81
81
  makeBackoff(retries),
82
82
  log,
83
83
  true,
@@ -153,6 +153,29 @@ export class BufferReader {
153
153
  return result;
154
154
  }
155
155
 
156
+ /**
157
+ * Reads a vector of fixed size from the buffer and deserializes its elements using the provided itemDeserializer object.
158
+ * The 'itemDeserializer' object should have a 'fromBuffer' method that takes a BufferReader instance and returns the deserialized element.
159
+ * The method first reads the size of the vector (a number) from the buffer, then iterates through its elements,
160
+ * deserializing each one using the 'fromBuffer' method of 'itemDeserializer'.
161
+ *
162
+ * @param itemDeserializer - Object with 'fromBuffer' method to deserialize vector elements.
163
+ * @returns An array of deserialized elements of type T.
164
+ */
165
+ public readVectorUint8Prefix<T>(itemDeserializer: {
166
+ /**
167
+ * A method to deserialize data from a buffer.
168
+ */
169
+ fromBuffer: (reader: BufferReader) => T;
170
+ }): T[] {
171
+ const size = this.readUInt8();
172
+ const result = new Array<T>(size);
173
+ for (let i = 0; i < size; i++) {
174
+ result[i] = itemDeserializer.fromBuffer(this);
175
+ }
176
+ return result;
177
+ }
178
+
156
179
  /**
157
180
  * Read an array of a fixed size with elements of type T from the buffer.
158
181
  * The 'itemDeserializer' object should have a 'fromBuffer' method that takes a BufferReader instance as input,
@@ -4,13 +4,22 @@ import { numToUInt32BE } from './free_funcs.js';
4
4
 
5
5
  /**
6
6
  * For serializing an array of fixed length buffers.
7
- * TODO move to foundation pkg.
8
- * @param arr - Array of buffers.
7
+ * @param arr - Array of bufferable.
8
+ * @param prefixLength - The length of the prefix (denominated in bytes).
9
9
  * @returns The serialized buffers.
10
10
  */
11
- export function serializeBufferArrayToVector(arr: Buffer[]): Buffer {
12
- const lengthBuf = Buffer.alloc(4);
13
- lengthBuf.writeUInt32BE(arr.length, 0);
11
+ export function serializeArrayOfBufferableToVector(objs: Bufferable[], prefixLength = 4): Buffer {
12
+ const arr = serializeToBufferArray(objs);
13
+ let lengthBuf: Buffer;
14
+ if (prefixLength === 1) {
15
+ lengthBuf = Buffer.alloc(1);
16
+ lengthBuf.writeUInt8(arr.length, 0);
17
+ } else if (prefixLength === 4) {
18
+ lengthBuf = Buffer.alloc(4);
19
+ lengthBuf.writeUInt32BE(arr.length, 0);
20
+ } else {
21
+ throw new Error(`Unsupported prefix length. Got ${prefixLength}, expected 1 or 4`);
22
+ }
14
23
  return Buffer.concat([lengthBuf, ...arr]);
15
24
  }
16
25
 
@@ -37,8 +46,6 @@ type DeserializeFn<T> = (
37
46
  * @param vector - The vector to deserialize.
38
47
  * @param offset - The position in the vector to start deserializing from.
39
48
  * @returns Deserialized array and how many bytes we advanced by.
40
- *
41
- * TODO: move to foundation pkg.
42
49
  */
43
50
  export function deserializeArrayFromVector<T>(
44
51
  deserialize: DeserializeFn<T>,