@aztec/foundation 0.41.0 → 0.43.0

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/decoder.d.ts +2 -2
  2. package/dest/abi/decoder.d.ts.map +1 -1
  3. package/dest/abi/decoder.js +8 -8
  4. package/dest/abi/encoder.d.ts.map +1 -1
  5. package/dest/abi/encoder.js +11 -2
  6. package/dest/array/array.js +2 -2
  7. package/dest/collection/array.d.ts +13 -0
  8. package/dest/collection/array.d.ts.map +1 -1
  9. package/dest/collection/array.js +12 -1
  10. package/dest/crypto/random/randomness_singleton.js +3 -3
  11. package/dest/error/index.d.ts +1 -1
  12. package/dest/error/index.d.ts.map +1 -1
  13. package/dest/error/index.js +2 -2
  14. package/dest/fields/fields.d.ts +5 -3
  15. package/dest/fields/fields.d.ts.map +1 -1
  16. package/dest/fields/fields.js +40 -15
  17. package/dest/fields/point.d.ts +7 -0
  18. package/dest/fields/point.d.ts.map +1 -1
  19. package/dest/fields/point.js +24 -2
  20. package/dest/fs/index.d.ts +2 -0
  21. package/dest/fs/index.d.ts.map +1 -0
  22. package/dest/fs/index.js +2 -0
  23. package/dest/fs/run_in_dir.d.ts +2 -0
  24. package/dest/fs/run_in_dir.d.ts.map +1 -0
  25. package/dest/fs/run_in_dir.js +18 -0
  26. package/dest/index.d.ts +1 -0
  27. package/dest/index.d.ts.map +1 -1
  28. package/dest/index.js +2 -1
  29. package/dest/json-rpc/client/json_rpc_client.js +2 -2
  30. package/dest/promise/running-promise.d.ts +1 -1
  31. package/dest/promise/running-promise.d.ts.map +1 -1
  32. package/dest/promise/running-promise.js +1 -1
  33. package/dest/serialize/free_funcs.d.ts +1 -0
  34. package/dest/serialize/free_funcs.d.ts.map +1 -1
  35. package/dest/serialize/free_funcs.js +8 -1
  36. package/dest/serialize/serialize.d.ts.map +1 -1
  37. package/dest/serialize/serialize.js +4 -1
  38. package/dest/testing/test_data.d.ts +2 -6
  39. package/dest/testing/test_data.d.ts.map +1 -1
  40. package/dest/testing/test_data.js +1 -1
  41. package/dest/types/index.d.ts +4 -0
  42. package/dest/types/index.d.ts.map +1 -1
  43. package/package.json +3 -2
  44. package/src/abi/decoder.ts +7 -7
  45. package/src/abi/encoder.ts +8 -1
  46. package/src/array/array.ts +2 -2
  47. package/src/collection/array.ts +14 -0
  48. package/src/crypto/random/randomness_singleton.ts +2 -2
  49. package/src/error/index.ts +1 -1
  50. package/src/fields/fields.ts +46 -15
  51. package/src/fields/point.ts +28 -2
  52. package/src/fs/index.ts +1 -0
  53. package/src/fs/run_in_dir.ts +23 -0
  54. package/src/index.ts +1 -0
  55. package/src/json-rpc/client/json_rpc_client.ts +1 -1
  56. package/src/promise/running-promise.ts +1 -1
  57. package/src/serialize/free_funcs.ts +8 -0
  58. package/src/serialize/serialize.ts +2 -0
  59. package/src/testing/test_data.ts +3 -3
  60. package/src/types/index.ts +3 -0
@@ -23,7 +23,9 @@ export class Point {
23
23
  * The point's y coordinate
24
24
  */
25
25
  public readonly y: Fr,
26
- ) {}
26
+ ) {
27
+ // TODO: Do we want to check if the point is on the curve here?
28
+ }
27
29
 
28
30
  /**
29
31
  * Generate a random Point instance.
@@ -44,7 +46,7 @@ export class Point {
44
46
  */
45
47
  static fromBuffer(buffer: Buffer | BufferReader) {
46
48
  const reader = BufferReader.asReader(buffer);
47
- return new this(Fr.fromBuffer(reader.readBytes(32)), Fr.fromBuffer(reader.readBytes(32)));
49
+ return new this(Fr.fromBuffer(reader), Fr.fromBuffer(reader));
48
50
  }
49
51
 
50
52
  /**
@@ -134,6 +136,30 @@ export class Point {
134
136
  hash() {
135
137
  return poseidon2Hash(this.toFields());
136
138
  }
139
+
140
+ /**
141
+ * Check if this is point at infinity.
142
+ * Check this is consistent with how bb is encoding the point at infinity
143
+ */
144
+ public get inf() {
145
+ return this.x == Fr.ZERO;
146
+ }
147
+ public toFieldsWithInf() {
148
+ return [this.x, this.y, new Fr(this.inf)];
149
+ }
150
+
151
+ isOnGrumpkin() {
152
+ // TODO: Check this against how bb handles curve check and infinity point check
153
+ if (this.inf) {
154
+ return true;
155
+ }
156
+
157
+ // p.y * p.y == p.x * p.x * p.x - 17
158
+ const A = new Fr(17);
159
+ const lhs = this.y.square();
160
+ const rhs = this.x.square().mul(this.x).sub(A);
161
+ return lhs.equals(rhs);
162
+ }
137
163
  }
138
164
 
139
165
  /**
@@ -0,0 +1 @@
1
+ export * from './run_in_dir.js';
@@ -0,0 +1,23 @@
1
+ import * as fs from 'fs/promises';
2
+ import * as path from 'path';
3
+
4
+ // Create a random directory underneath a 'base' directory
5
+ // Calls a provided method, ensures the random directory is cleaned up afterwards
6
+ export async function runInDirectory<T>(
7
+ workingDirBase: string,
8
+ fn: (dir: string) => Promise<T>,
9
+ cleanup: boolean = true,
10
+ ): Promise<T> {
11
+ // Create random directory to be used for temp files
12
+ const workingDirectory = await fs.mkdtemp(path.join(workingDirBase, 'tmp-'));
13
+
14
+ await fs.access(workingDirectory);
15
+
16
+ try {
17
+ return await fn(workingDirectory);
18
+ } finally {
19
+ if (cleanup) {
20
+ await fs.rm(workingDirectory, { recursive: true, force: true });
21
+ }
22
+ }
23
+ }
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@ export * as errors from './error/index.js';
10
10
  export * as ethAddress from './eth-address/index.js';
11
11
  export * as fields from './fields/index.js';
12
12
  export * as fifo from './fifo/index.js';
13
+ export * as fs from './fs/index.js';
13
14
  export * as jsonRpc from './json-rpc/index.js';
14
15
  export * as jsonRpcClient from './json-rpc/client/index.js';
15
16
  export * as jsonRpcServer from './json-rpc/server/index.js';
@@ -80,7 +80,7 @@ export function makeFetch(retries: number[], noRetry: boolean, log?: DebugLogger
80
80
  `JsonRpcClient request ${rpcMethod} to ${host}`,
81
81
  makeBackoff(retries),
82
82
  log,
83
- true,
83
+ false,
84
84
  );
85
85
  };
86
86
  }
@@ -10,7 +10,7 @@ export class RunningPromise {
10
10
  private runningPromise = Promise.resolve();
11
11
  private interruptibleSleep = new InterruptibleSleep();
12
12
 
13
- constructor(private fn: () => Promise<void>, private pollingIntervalMS = 10000) {}
13
+ constructor(private fn: () => void | Promise<void>, private pollingIntervalMS = 10000) {}
14
14
 
15
15
  /**
16
16
  * Starts the running promise.
@@ -194,3 +194,11 @@ export function fromTruncField(field: Fr): Buffer {
194
194
  export function fromFieldsTuple(fields: Tuple<Fr, 2>): Buffer {
195
195
  return from2Fields(fields[0], fields[1]);
196
196
  }
197
+
198
+ export function toHumanReadable(buf: Buffer, maxLen?: number): string {
199
+ const result = buf.every(byte => byte >= 32 && byte <= 126) ? buf.toString('ascii') : `0x${buf.toString('hex')}`;
200
+ if (maxLen && result.length > maxLen) {
201
+ return result.slice(0, maxLen) + '...';
202
+ }
203
+ return result;
204
+ }
@@ -242,6 +242,8 @@ export function toFriendlyJSON(obj: object): string {
242
242
  ).toFriendlyJSON
243
243
  ) {
244
244
  return value.toFriendlyJSON();
245
+ } else if (value && value.type && ['Fr', 'Fq', 'AztecAddress'].includes(value.type)) {
246
+ return value.value;
245
247
  } else {
246
248
  return value;
247
249
  }
@@ -4,7 +4,7 @@ import { dirname, join, resolve } from 'path';
4
4
  import { createConsoleLogger } from '../log/console.js';
5
5
  import { fileURLToPath } from '../url/index.js';
6
6
 
7
- const testData: { [key: string]: { toBuffer(): Buffer }[] } = {};
7
+ const testData: { [key: string]: unknown[] } = {};
8
8
 
9
9
  /** Returns whether test data generation is enabled */
10
10
  export function isGenerateTestDataEnabled() {
@@ -12,7 +12,7 @@ export function isGenerateTestDataEnabled() {
12
12
  }
13
13
 
14
14
  /** Pushes test data with the given name, only if test data generation is enabled. */
15
- export function pushTestData(itemName: string, data: { toBuffer(): Buffer }) {
15
+ export function pushTestData<T>(itemName: string, data: T) {
16
16
  if (!isGenerateTestDataEnabled()) {
17
17
  return;
18
18
  }
@@ -31,7 +31,7 @@ export function pushTestData(itemName: string, data: { toBuffer(): Buffer }) {
31
31
  }
32
32
 
33
33
  /** Returns all instances of pushed test data with the given name, or empty if test data generation is not enabled. */
34
- export function getTestData(itemName: string): { toBuffer(): Buffer }[] {
34
+ export function getTestData(itemName: string): unknown[] {
35
35
  if (!isGenerateTestDataEnabled()) {
36
36
  return [];
37
37
  }
@@ -12,3 +12,6 @@ export type FunctionsOf<T> = {
12
12
 
13
13
  /** Marks a set of properties of a type as optional. */
14
14
  export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
15
+
16
+ /** Removes readonly modifiers for a type. */
17
+ export type Writeable<T> = { -readonly [P in keyof T]: T[P] };