@aztec/foundation 0.65.0 → 0.65.2

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 (82) hide show
  1. package/dest/abi/abi.d.ts +9 -9
  2. package/dest/abi/abi.js +2 -2
  3. package/dest/abi/event_selector.d.ts +2 -4
  4. package/dest/abi/event_selector.d.ts.map +1 -1
  5. package/dest/abi/event_selector.js +6 -5
  6. package/dest/abi/function_selector.d.ts +2 -4
  7. package/dest/abi/function_selector.d.ts.map +1 -1
  8. package/dest/abi/function_selector.js +6 -5
  9. package/dest/abi/note_selector.d.ts +2 -5
  10. package/dest/abi/note_selector.d.ts.map +1 -1
  11. package/dest/abi/note_selector.js +5 -7
  12. package/dest/abi/selector.d.ts.map +1 -1
  13. package/dest/abi/selector.js +3 -2
  14. package/dest/aztec-address/index.d.ts +2 -4
  15. package/dest/aztec-address/index.d.ts.map +1 -1
  16. package/dest/aztec-address/index.js +6 -5
  17. package/dest/buffer/buffer32.d.ts +2 -12
  18. package/dest/buffer/buffer32.d.ts.map +1 -1
  19. package/dest/buffer/buffer32.js +3 -16
  20. package/dest/config/env_var.d.ts +1 -1
  21. package/dest/config/env_var.d.ts.map +1 -1
  22. package/dest/config/index.d.ts +1 -1
  23. package/dest/config/index.d.ts.map +1 -1
  24. package/dest/config/index.js +2 -2
  25. package/dest/crypto/poseidon/index.d.ts +1 -0
  26. package/dest/crypto/poseidon/index.d.ts.map +1 -1
  27. package/dest/crypto/poseidon/index.js +7 -1
  28. package/dest/eth-address/index.d.ts +2 -9
  29. package/dest/eth-address/index.d.ts.map +1 -1
  30. package/dest/eth-address/index.js +7 -12
  31. package/dest/eth-signature/eth_signature.d.ts +5 -3
  32. package/dest/eth-signature/eth_signature.d.ts.map +1 -1
  33. package/dest/eth-signature/eth_signature.js +18 -9
  34. package/dest/fields/fields.d.ts +4 -8
  35. package/dest/fields/fields.d.ts.map +1 -1
  36. package/dest/fields/fields.js +10 -10
  37. package/dest/fields/point.d.ts +6 -4
  38. package/dest/fields/point.d.ts.map +1 -1
  39. package/dest/fields/point.js +14 -6
  40. package/dest/json-rpc/client/fetch.d.ts.map +1 -1
  41. package/dest/json-rpc/client/fetch.js +20 -14
  42. package/dest/json-rpc/convert.d.ts.map +1 -1
  43. package/dest/json-rpc/convert.js +5 -2
  44. package/dest/json-rpc/index.d.ts +1 -1
  45. package/dest/json-rpc/index.d.ts.map +1 -1
  46. package/dest/json-rpc/index.js +2 -2
  47. package/dest/noir/noir_package_config.d.ts +2 -2
  48. package/dest/schemas/schemas.d.ts +24 -43
  49. package/dest/schemas/schemas.d.ts.map +1 -1
  50. package/dest/schemas/schemas.js +29 -48
  51. package/dest/schemas/utils.d.ts +18 -7
  52. package/dest/schemas/utils.d.ts.map +1 -1
  53. package/dest/schemas/utils.js +20 -8
  54. package/dest/serialize/type_registry.d.ts.map +1 -1
  55. package/dest/serialize/type_registry.js +29 -2
  56. package/dest/string/index.d.ts +1 -0
  57. package/dest/string/index.d.ts.map +1 -1
  58. package/dest/string/index.js +4 -1
  59. package/dest/testing/test_data.js +2 -2
  60. package/package.json +2 -2
  61. package/src/abi/abi.ts +1 -1
  62. package/src/abi/event_selector.ts +6 -4
  63. package/src/abi/function_selector.ts +6 -4
  64. package/src/abi/note_selector.ts +4 -6
  65. package/src/abi/selector.ts +2 -1
  66. package/src/aztec-address/index.ts +6 -4
  67. package/src/buffer/buffer32.ts +3 -17
  68. package/src/config/env_var.ts +11 -2
  69. package/src/config/index.ts +1 -1
  70. package/src/crypto/poseidon/index.ts +11 -0
  71. package/src/eth-address/index.ts +6 -11
  72. package/src/eth-signature/eth_signature.ts +20 -9
  73. package/src/fields/fields.ts +11 -9
  74. package/src/fields/point.ts +15 -5
  75. package/src/json-rpc/client/fetch.ts +18 -13
  76. package/src/json-rpc/convert.ts +3 -1
  77. package/src/json-rpc/index.ts +1 -1
  78. package/src/schemas/schemas.ts +28 -51
  79. package/src/schemas/utils.ts +29 -10
  80. package/src/serialize/type_registry.ts +33 -1
  81. package/src/string/index.ts +4 -0
  82. package/src/testing/test_data.ts +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/foundation",
3
- "version": "0.65.0",
3
+ "version": "0.65.2",
4
4
  "packageManager": "yarn@3.4.1",
5
5
  "type": "module",
6
6
  "main": "./dest/index.js",
@@ -98,7 +98,7 @@
98
98
  ]
99
99
  },
100
100
  "dependencies": {
101
- "@aztec/bb.js": "0.65.0",
101
+ "@aztec/bb.js": "0.65.2",
102
102
  "@koa/cors": "^5.0.0",
103
103
  "@noble/curves": "^1.2.0",
104
104
  "bn.js": "^5.2.1",
package/src/abi/abi.ts CHANGED
@@ -230,7 +230,7 @@ export interface FunctionArtifact extends FunctionAbi {
230
230
 
231
231
  export const FunctionArtifactSchema = FunctionAbiSchema.and(
232
232
  z.object({
233
- bytecode: schemas.BufferB64,
233
+ bytecode: schemas.Buffer,
234
234
  verificationKey: z.string().optional(),
235
235
  debugSymbols: z.string(),
236
236
  debug: FunctionDebugMetadataSchema.optional(),
@@ -1,6 +1,7 @@
1
1
  import { fromHex, toBigIntBE } from '../bigint-buffer/index.js';
2
2
  import { poseidon2HashBytes, randomBytes } from '../crypto/index.js';
3
3
  import { type Fr } from '../fields/fields.js';
4
+ import { hexSchemaFor } from '../schemas/utils.js';
4
5
  import { BufferReader } from '../serialize/buffer_reader.js';
5
6
  import { Selector } from './selector.js';
6
7
 
@@ -83,9 +84,10 @@ export class EventSelector extends Selector {
83
84
  }
84
85
 
85
86
  toJSON() {
86
- return {
87
- type: 'EventSelector',
88
- value: this.toString(),
89
- };
87
+ return this.toString();
88
+ }
89
+
90
+ static get schema() {
91
+ return hexSchemaFor(EventSelector);
90
92
  }
91
93
  }
@@ -1,6 +1,7 @@
1
1
  import { fromHex, toBigIntBE } from '../bigint-buffer/index.js';
2
2
  import { poseidon2HashBytes, randomBytes } from '../crypto/index.js';
3
3
  import { type Fr } from '../fields/fields.js';
4
+ import { hexSchemaFor } from '../schemas/utils.js';
4
5
  import { BufferReader } from '../serialize/buffer_reader.js';
5
6
  import { FieldReader } from '../serialize/field_reader.js';
6
7
  import { TypeRegistry } from '../serialize/type_registry.js';
@@ -132,10 +133,11 @@ export class FunctionSelector extends Selector {
132
133
  }
133
134
 
134
135
  toJSON() {
135
- return {
136
- type: 'FunctionSelector',
137
- value: this.toString(),
138
- };
136
+ return this.toString();
137
+ }
138
+
139
+ static get schema() {
140
+ return hexSchemaFor(FunctionSelector);
139
141
  }
140
142
  }
141
143
 
@@ -1,6 +1,7 @@
1
1
  import { toBigIntBE } from '../bigint-buffer/index.js';
2
2
  import { randomBytes } from '../crypto/index.js';
3
3
  import { type Fr } from '../fields/fields.js';
4
+ import { hexSchemaFor } from '../schemas/utils.js';
4
5
  import { BufferReader } from '../serialize/buffer_reader.js';
5
6
  import { TypeRegistry } from '../serialize/type_registry.js';
6
7
  import { Selector } from './selector.js';
@@ -58,14 +59,11 @@ export class NoteSelector extends Selector {
58
59
  }
59
60
 
60
61
  toJSON() {
61
- return {
62
- type: 'NoteSelector',
63
- value: this.toString(),
64
- };
62
+ return this.toString();
65
63
  }
66
64
 
67
- static fromJSON(json: any): NoteSelector {
68
- return NoteSelector.fromString(json.value);
65
+ static get schema() {
66
+ return hexSchemaFor(NoteSelector);
69
67
  }
70
68
  }
71
69
 
@@ -2,6 +2,7 @@ import { inspect } from 'util';
2
2
 
3
3
  import { toBufferBE } from '../bigint-buffer/index.js';
4
4
  import { Fr } from '../fields/index.js';
5
+ import { bufferToHex } from '../string/index.js';
5
6
 
6
7
  /** A selector is the first 4 bytes of the hash of a signature. */
7
8
  export abstract class Selector {
@@ -36,7 +37,7 @@ export abstract class Selector {
36
37
  * @returns The string.
37
38
  */
38
39
  toString(): string {
39
- return '0x' + this.toBuffer().toString('hex');
40
+ return bufferToHex(this.toBuffer());
40
41
  }
41
42
 
42
43
  [inspect.custom]() {
@@ -2,6 +2,7 @@
2
2
  import { inspect } from 'util';
3
3
 
4
4
  import { Fr, Point, fromBuffer } from '../fields/index.js';
5
+ import { hexSchemaFor } from '../schemas/utils.js';
5
6
  import { type BufferReader, FieldReader } from '../serialize/index.js';
6
7
  import { TypeRegistry } from '../serialize/type_registry.js';
7
8
  import { hexToBuffer } from '../string/index.js';
@@ -133,10 +134,11 @@ export class AztecAddress {
133
134
  }
134
135
 
135
136
  toJSON() {
136
- return {
137
- type: 'AztecAddress',
138
- value: this.toString(),
139
- };
137
+ return this.toString();
138
+ }
139
+
140
+ static get schema() {
141
+ return hexSchemaFor(AztecAddress, AztecAddress.isAddress);
140
142
  }
141
143
  }
142
144
 
@@ -2,6 +2,8 @@ import { randomBytes } from '@aztec/foundation/crypto';
2
2
  import { type Fr } from '@aztec/foundation/fields';
3
3
  import { BufferReader, deserializeBigInt, serializeBigInt } from '@aztec/foundation/serialize';
4
4
 
5
+ import { bufferToHex } from '../string/index.js';
6
+
5
7
  /**
6
8
  * A class representing a 32 byte Buffer.
7
9
  */
@@ -67,17 +69,13 @@ export class Buffer32 {
67
69
  * @returns The hex string.
68
70
  */
69
71
  public toString() {
70
- return this.buffer.toString('hex');
72
+ return bufferToHex(this.buffer);
71
73
  }
72
74
 
73
75
  toJSON() {
74
76
  return this.toString();
75
77
  }
76
78
 
77
- public to0xString(): `0x${string}` {
78
- return `0x${this.buffer.toString('hex')}`;
79
- }
80
-
81
79
  /**
82
80
  * Convert this hash to a big int.
83
81
  * @returns The big int.
@@ -117,18 +115,6 @@ export class Buffer32 {
117
115
  * @param str - The TX hash in string format.
118
116
  * @returns A new Buffer32 object.
119
117
  */
120
- public static fromStringUnchecked(str: string): Buffer32 {
121
- return new Buffer32(Buffer.from(str, 'hex'));
122
- }
123
-
124
- /**
125
- * Converts a string into a Buffer32 object.
126
- * NOTE: this method includes checks for the 0x prefix and the length of the string.
127
- * if you dont need this checks, use fromStringUnchecked instead.
128
- *
129
- * @param str - The TX hash in string format.
130
- * @returns A new Buffer32 object.
131
- */
132
118
  public static fromString(str: string): Buffer32 {
133
119
  if (str.startsWith('0x')) {
134
120
  str = str.slice(2);
@@ -97,15 +97,22 @@ export type EnvVar =
97
97
  | 'PEER_ID_PRIVATE_KEY'
98
98
  | 'PROOF_VERIFIER_L1_START_BLOCK'
99
99
  | 'PROOF_VERIFIER_POLL_INTERVAL_MS'
100
- | 'PROVER_AGENT_CONCURRENCY'
101
100
  | 'PROVER_AGENT_ENABLED'
101
+ | 'PROVER_AGENT_CONCURRENCY'
102
+ | 'PROVER_AGENT_COUNT'
103
+ | 'PROVER_AGENT_PROOF_TYPES'
102
104
  | 'PROVER_AGENT_POLL_INTERVAL_MS'
105
+ | 'PROVER_BROKER_HOST'
106
+ | 'PROVER_BROKER_ENABLED'
107
+ | 'PROVER_BROKER_JOB_TIMEOUT_MS'
108
+ | 'PROVER_BROKER_POLL_INTERVAL_MS'
109
+ | 'PROVER_BROKER_JOB_MAX_RETRIES'
110
+ | 'PROVER_BROKER_DATA_DIRECTORY'
103
111
  | 'PROVER_COORDINATION_NODE_URL'
104
112
  | 'PROVER_DISABLED'
105
113
  | 'PROVER_ID'
106
114
  | 'PROVER_JOB_POLL_INTERVAL_MS'
107
115
  | 'PROVER_JOB_TIMEOUT_MS'
108
- | 'PROVER_JOB_SOURCE_URL'
109
116
  | 'PROVER_NODE_POLLING_INTERVAL_MS'
110
117
  | 'PROVER_NODE_MAX_PENDING_JOBS'
111
118
  | 'PROVER_PUBLISH_RETRY_INTERVAL_MS'
@@ -113,6 +120,7 @@ export type EnvVar =
113
120
  | 'PROVER_REAL_PROOFS'
114
121
  | 'PROVER_REQUIRED_CONFIRMATIONS'
115
122
  | 'PROVER_TEST_DELAY_MS'
123
+ | 'PROVER_CACHE_DIR'
116
124
  | 'PXE_BLOCK_POLLING_INTERVAL_MS'
117
125
  | 'PXE_L2_STARTING_BLOCK'
118
126
  | 'PXE_PROVER_ENABLED'
@@ -155,6 +163,7 @@ export type EnvVar =
155
163
  | 'SEQ_VIEM_POLLING_INTERVAL_MS'
156
164
  | 'WS_DB_MAP_SIZE_KB'
157
165
  | 'WS_DATA_DIRECTORY'
166
+ | 'WS_NUM_HISTORIC_BLOCKS'
158
167
  | 'ETHEREUM_SLOT_DURATION'
159
168
  | 'AZTEC_SLOT_DURATION'
160
169
  | 'AZTEC_EPOCH_DURATION'
@@ -46,7 +46,7 @@ export function getConfigFromMappings<T>(configMappings: ConfigMappingsType<T>):
46
46
  * @param keysToFilter - The keys to filter out
47
47
  * @returns The filtered config mappings
48
48
  */
49
- export function filterConfigMappings<T, K extends keyof T>(
49
+ export function omitConfigMappings<T, K extends keyof T>(
50
50
  configMappings: ConfigMappingsType<T>,
51
51
  keysToFilter: K[],
52
52
  ): ConfigMappingsType<Omit<T, K>> {
@@ -41,6 +41,17 @@ export function poseidon2HashWithSeparator(input: Fieldable[], separator: number
41
41
  );
42
42
  }
43
43
 
44
+ export function poseidon2HashAccumulate(input: Fieldable[]): Fr {
45
+ const inputFields = serializeToFields(input);
46
+ return Fr.fromBuffer(
47
+ Buffer.from(
48
+ BarretenbergSync.getSingleton()
49
+ .poseidon2HashAccumulate(inputFields.map(i => new FrBarretenberg(i.toBuffer())))
50
+ .toBuffer(),
51
+ ),
52
+ );
53
+ }
54
+
44
55
  /**
45
56
  * Runs a Poseidon2 permutation.
46
57
  * @param input the input state. Expected to be of size 4.
@@ -3,8 +3,10 @@ import { inspect } from 'util';
3
3
  import { keccak256String } from '../crypto/keccak/index.js';
4
4
  import { randomBytes } from '../crypto/random/index.js';
5
5
  import { Fr } from '../fields/index.js';
6
+ import { hexSchemaFor } from '../schemas/utils.js';
6
7
  import { BufferReader, FieldReader } from '../serialize/index.js';
7
8
  import { TypeRegistry } from '../serialize/type_registry.js';
9
+ import { bufferToHex } from '../string/index.js';
8
10
 
9
11
  /**
10
12
  * Represents an Ethereum address as a 20-byte buffer and provides various utility methods
@@ -154,7 +156,7 @@ export class EthAddress {
154
156
  * @returns A hex-encoded string representation of the Ethereum address.
155
157
  */
156
158
  public toString() {
157
- return `0x${this.buffer.toString('hex')}` as `0x${string}`;
159
+ return bufferToHex(this.buffer);
158
160
  }
159
161
 
160
162
  [inspect.custom]() {
@@ -226,19 +228,12 @@ export class EthAddress {
226
228
  return new EthAddress(reader.readBytes(EthAddress.SIZE_IN_BYTES));
227
229
  }
228
230
 
229
- /**
230
- * Friendly representation for debugging purposes.
231
- * @returns A hex string representing the address.
232
- */
233
- toFriendlyJSON() {
231
+ toJSON() {
234
232
  return this.toString();
235
233
  }
236
234
 
237
- toJSON() {
238
- return {
239
- type: 'EthAddress',
240
- value: this.toString(),
241
- };
235
+ static get schema() {
236
+ return hexSchemaFor(EthAddress, EthAddress.isAddress);
242
237
  }
243
238
  }
244
239
 
@@ -1,6 +1,10 @@
1
1
  import { Buffer32 } from '@aztec/foundation/buffer';
2
2
  import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
3
3
 
4
+ import { z } from 'zod';
5
+
6
+ import { hasHexPrefix, hexToBuffer } from '../string/index.js';
7
+
4
8
  /**Viem Signature
5
9
  *
6
10
  * A version of the Signature class that uses `0x${string}` values for r and s rather than
@@ -45,7 +49,7 @@ export class Signature {
45
49
  return new Signature(r, s, v, isEmpty);
46
50
  }
47
51
 
48
- static isValid0xString(sig: `0x${string}`): boolean {
52
+ static isValidString(sig: `0x${string}`): boolean {
49
53
  return /^0x[0-9a-f]{129,}$/i.test(sig);
50
54
  }
51
55
 
@@ -54,10 +58,9 @@ export class Signature {
54
58
  * parsing from viem, we can expect the v value to be a u8, rather than our
55
59
  * default serialization of u32
56
60
  */
57
- static from0xString(sig: `0x${string}`): Signature {
58
- const buf = Buffer.from(sig.slice(2), 'hex');
61
+ static fromString(sig: `0x${string}`): Signature {
62
+ const buf = hexToBuffer(sig);
59
63
  const reader = BufferReader.asReader(buf);
60
-
61
64
  const r = reader.readObject(Buffer32);
62
65
  const s = reader.readObject(Buffer32);
63
66
  const v = parseInt(sig.slice(2 + 64 * 2), 16);
@@ -95,8 +98,8 @@ export class Signature {
95
98
  return this.size;
96
99
  }
97
100
 
98
- to0xString(): `0x${string}` {
99
- return `0x${this.r.toString()}${this.s.toString()}${this.v.toString(16)}`;
101
+ toString(): `0x${string}` {
102
+ return `0x${this.r.buffer.toString('hex')}${this.s.buffer.toString('hex')}${this.v.toString(16)}`;
100
103
  }
101
104
 
102
105
  /**
@@ -104,14 +107,22 @@ export class Signature {
104
107
  */
105
108
  toViemSignature(): ViemSignature {
106
109
  return {
107
- r: this.r.to0xString(),
108
- s: this.s.to0xString(),
110
+ r: this.r.toString(),
111
+ s: this.s.toString(),
109
112
  v: this.v,
110
113
  isEmpty: this.isEmpty,
111
114
  };
112
115
  }
113
116
 
114
117
  toJSON() {
115
- return this.to0xString();
118
+ return this.toString();
119
+ }
120
+
121
+ static get schema() {
122
+ return z
123
+ .string()
124
+ .refine(hasHexPrefix, 'No hex prefix')
125
+ .refine(Signature.isValidString, 'Not a valid Ethereum signature')
126
+ .transform(Signature.fromString);
116
127
  }
117
128
  }
@@ -4,6 +4,7 @@ import { inspect } from 'util';
4
4
 
5
5
  import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
6
6
  import { randomBytes } from '../crypto/random/index.js';
7
+ import { hexSchemaFor } from '../schemas/utils.js';
7
8
  import { BufferReader } from '../serialize/buffer_reader.js';
8
9
  import { TypeRegistry } from '../serialize/type_registry.js';
9
10
 
@@ -300,12 +301,12 @@ export class Fr extends BaseField {
300
301
  return Fr.fromBuffer(rootBuf);
301
302
  }
302
303
 
303
- // TODO(palla/schemas): Use toString instead of structured type
304
304
  toJSON() {
305
- return {
306
- type: 'Fr',
307
- value: this.toString(),
308
- };
305
+ return this.toString();
306
+ }
307
+
308
+ static get schema() {
309
+ return hexSchemaFor(Fr);
309
310
  }
310
311
  }
311
312
 
@@ -385,10 +386,11 @@ export class Fq extends BaseField {
385
386
  }
386
387
 
387
388
  toJSON() {
388
- return {
389
- type: 'Fq',
390
- value: this.toString(),
391
- };
389
+ return this.toString();
390
+ }
391
+
392
+ static get schema() {
393
+ return hexSchemaFor(Fq);
392
394
  }
393
395
  }
394
396
 
@@ -1,7 +1,9 @@
1
1
  import { toBigIntBE } from '../bigint-buffer/index.js';
2
2
  import { poseidon2Hash } from '../crypto/poseidon/index.js';
3
3
  import { randomBoolean } from '../crypto/random/index.js';
4
+ import { hexSchemaFor } from '../schemas/utils.js';
4
5
  import { BufferReader, FieldReader, serializeToBuffer } from '../serialize/index.js';
6
+ import { bufferToHex, hexToBuffer } from '../string/index.js';
5
7
  import { Fr } from './fields.js';
6
8
 
7
9
  /**
@@ -34,6 +36,14 @@ export class Point {
34
36
  // TODO(#7386): check if on curve
35
37
  }
36
38
 
39
+ toJSON() {
40
+ return this.toString();
41
+ }
42
+
43
+ static get schema() {
44
+ return hexSchemaFor(Point);
45
+ }
46
+
37
47
  /**
38
48
  * Generate a random Point instance.
39
49
  *
@@ -84,14 +94,14 @@ export class Point {
84
94
 
85
95
  /**
86
96
  * Create a Point instance from a hex-encoded string.
87
- * The input 'address' should be prefixed with '0x' or not, and have exactly 128 hex characters representing the x and y coordinates.
97
+ * The input should be prefixed with '0x' or not, and have exactly 128 hex characters representing the x and y coordinates.
88
98
  * Throws an error if the input length is invalid or coordinate values are out of range.
89
99
  *
90
- * @param address - The hex-encoded string representing the Point coordinates.
100
+ * @param str - The hex-encoded string representing the Point coordinates.
91
101
  * @returns A Point instance.
92
102
  */
93
- static fromString(address: string) {
94
- return this.fromBuffer(Buffer.from(address.replace(/^0x/i, ''), 'hex'));
103
+ static fromString(str: string) {
104
+ return this.fromBuffer(hexToBuffer(str));
95
105
  }
96
106
 
97
107
  /**
@@ -211,7 +221,7 @@ export class Point {
211
221
  * @returns A hex-encoded string representing the Point instance.
212
222
  */
213
223
  toString() {
214
- return '0x' + this.toBuffer().toString('hex');
224
+ return bufferToHex(this.toBuffer());
215
225
  }
216
226
 
217
227
  /**
@@ -1,4 +1,4 @@
1
- import { format } from 'util';
1
+ import { format, inspect } from 'util';
2
2
 
3
3
  import { type DebugLogger, createDebugLogger } from '../../log/index.js';
4
4
  import { NoRetryError, makeBackoff, retry } from '../../retry/index.js';
@@ -25,18 +25,23 @@ export async function defaultFetch(
25
25
  ) {
26
26
  log.debug(format(`JsonRpcClient.fetch`, host, rpcMethod, '->', body));
27
27
  let resp: Response;
28
- if (useApiEndpoints) {
29
- resp = await fetch(`${host}/${rpcMethod}`, {
30
- method: 'POST',
31
- body: jsonStringify(body),
32
- headers: { 'content-type': 'application/json' },
33
- });
34
- } else {
35
- resp = await fetch(host, {
36
- method: 'POST',
37
- body: jsonStringify({ ...body, method: rpcMethod }),
38
- headers: { 'content-type': 'application/json' },
39
- });
28
+ try {
29
+ if (useApiEndpoints) {
30
+ resp = await fetch(`${host}/${rpcMethod}`, {
31
+ method: 'POST',
32
+ body: jsonStringify(body),
33
+ headers: { 'content-type': 'application/json' },
34
+ });
35
+ } else {
36
+ resp = await fetch(host, {
37
+ method: 'POST',
38
+ body: jsonStringify({ ...body, method: rpcMethod }),
39
+ headers: { 'content-type': 'application/json' },
40
+ });
41
+ }
42
+ } catch (err) {
43
+ const errorMessage = `Error fetching from host ${host} with method ${rpcMethod}: ${inspect(err)}`;
44
+ throw new Error(errorMessage);
40
45
  }
41
46
 
42
47
  let responseJson;
@@ -23,7 +23,9 @@ export function jsonStringify(obj: object, prettify?: boolean): string {
23
23
  (_key, value) => {
24
24
  if (typeof value === 'bigint') {
25
25
  return value.toString();
26
- } else if (typeof value === 'object' && Buffer.isBuffer(value)) {
26
+ } else if (typeof value === 'object' && value && value.type === 'Buffer' && Array.isArray(value.data)) {
27
+ return Buffer.from(value.data).toString('base64');
28
+ } else if (typeof value === 'object' && value && Buffer.isBuffer(value)) {
27
29
  return value.toString('base64');
28
30
  } else if (typeof value === 'object' && value instanceof Map) {
29
31
  return Array.from(value.entries());
@@ -1 +1 @@
1
- export { jsonStringify } from './convert.js';
1
+ export { jsonStringify, jsonParseWithSchema, tryJsonStringify } from './convert.js';
@@ -7,65 +7,45 @@ import { NoteSelector } from '../abi/note_selector.js';
7
7
  import { AztecAddress } from '../aztec-address/index.js';
8
8
  import { Buffer32 } from '../buffer/buffer32.js';
9
9
  import { EthAddress } from '../eth-address/index.js';
10
- import { Signature } from '../eth-signature/eth_signature.js';
11
10
  import { Fq, Fr } from '../fields/fields.js';
12
11
  import { Point } from '../fields/point.js';
13
- import { hasHexPrefix, isHex, withoutHexPrefix } from '../string/index.js';
12
+ import { isHex, withoutHexPrefix } from '../string/index.js';
14
13
  import { type ZodFor } from './types.js';
15
- import { hexSchema, maybeStructuredStringSchemaFor } from './utils.js';
16
-
17
- const FrSchema = maybeStructuredStringSchemaFor('Fr', Fr, isHex);
18
- const FqSchema = maybeStructuredStringSchemaFor('Fq', Fq, isHex);
14
+ import { bufferSchema, hexSchema } from './utils.js';
19
15
 
20
16
  /** Validation schemas for common types. Every schema must match its toJSON. */
21
17
  export const schemas = {
22
- /** Accepts both a 0x string and a structured `{ type: EthAddress, value: '0x...' }` */
23
- EthAddress: maybeStructuredStringSchemaFor('EthAddress', EthAddress, EthAddress.isAddress),
24
-
25
- /** Accepts both a 0x string and a structured `{ type: AztecAddress, value: '0x...' }` */
26
- AztecAddress: maybeStructuredStringSchemaFor('AztecAddress', AztecAddress, AztecAddress.isAddress),
18
+ /** Accepts a hex string. */
19
+ EthAddress: EthAddress.schema,
27
20
 
28
- /** Accepts both a 0x string and a structured type. */
29
- FunctionSelector: maybeStructuredStringSchemaFor('FunctionSelector', FunctionSelector),
21
+ /** Accepts a hex string. */
22
+ AztecAddress: AztecAddress.schema,
30
23
 
31
- /** Accepts both a 0x string and a structured type. */
32
- NoteSelector: maybeStructuredStringSchemaFor('NoteSelector', NoteSelector),
24
+ /** Accepts a hex string. */
25
+ FunctionSelector: FunctionSelector.schema,
33
26
 
34
- /** Accepts both a 0x string and a structured type. */
35
- EventSelector: maybeStructuredStringSchemaFor('EventSelector', EventSelector),
27
+ /** Accepts a hex string. */
28
+ NoteSelector: NoteSelector.schema,
36
29
 
37
- /** Field element. Accepts a 0x prefixed hex string or a structured type. */
38
- Fr: FrSchema,
30
+ /** Accepts a hex string. */
31
+ EventSelector: EventSelector.schema,
39
32
 
40
- /** Field element. Accepts a 0x prefixed hex string or a structured type. */
41
- Fq: FqSchema,
33
+ /** Accepts a hex string. */
34
+ Fr: Fr.schema,
42
35
 
43
- /** Point. Serialized as 0x prefixed string or a type. */
44
- Point: z
45
- .object({
46
- x: FrSchema,
47
- y: FrSchema,
48
- isInfinite: z.boolean().optional(),
49
- })
50
- .or(hexSchema)
51
- .transform(value =>
52
- typeof value === 'string' ? Point.fromString(value) : new Point(value.x, value.y, value.isInfinite ?? false),
53
- ),
36
+ /** Accepts a hex string. */
37
+ Fq: Fq.schema,
54
38
 
55
- /** Accepts a 0x string */
56
- Signature: z
57
- .string()
58
- .refine(hasHexPrefix, 'No hex prefix')
59
- .refine(Signature.isValid0xString, 'Not a valid Ethereum signature')
60
- .transform(Signature.from0xString),
39
+ /** Point. Serialized as a hex string. */
40
+ Point: Point.schema,
61
41
 
62
- /** Coerces any input to bigint */
42
+ /** Coerces any input to bigint. */
63
43
  BigInt: z.union([z.bigint(), z.number(), z.string()]).pipe(z.coerce.bigint()),
64
44
 
65
- /** Coerces any input to integer number */
45
+ /** Coerces any input to integer number. */
66
46
  Integer: z.union([z.bigint(), z.number(), z.string()]).pipe(z.coerce.number().int()),
67
47
 
68
- /** Coerces input to UInt32 */
48
+ /** Coerces input to UInt32. */
69
49
  UInt32: z.union([z.bigint(), z.number(), z.string()]).pipe(
70
50
  z.coerce
71
51
  .number()
@@ -74,31 +54,28 @@ export const schemas = {
74
54
  .max(2 ** 32 - 1),
75
55
  ),
76
56
 
77
- /** Accepts a hex string as a Buffer32 type */
57
+ /** Accepts a hex string as a Buffer32 type. */
78
58
  Buffer32: z.string().refine(isHex, 'Not a valid hex string').transform(Buffer32.fromString),
79
59
 
80
- /** Accepts a base64 string or a structured `{ type: 'Buffer', data: [byte, byte...] }` as a buffer */
81
- BufferB64: z.union([
82
- z
83
- .string()
84
- .base64()
85
- .transform(data => Buffer.from(data, 'base64')),
60
+ /** Accepts a base64 string or an object `{ type: 'Buffer', data: [byte, byte...] }` as a buffer. */
61
+ Buffer: z.union([
62
+ bufferSchema,
86
63
  z
87
64
  .object({
88
65
  type: z.literal('Buffer'),
89
- data: z.array(z.number().int().max(255)),
66
+ data: z.array(z.number().int().min(0).max(255)),
90
67
  })
91
68
  .transform(({ data }) => Buffer.from(data)),
92
69
  ]),
93
70
 
94
- /** Accepts a hex string with optional 0x prefix as a buffer */
71
+ /** Accepts a hex string as a buffer. */
95
72
  BufferHex: z
96
73
  .string()
97
74
  .refine(isHex, 'Not a valid hex string')
98
75
  .transform(withoutHexPrefix)
99
76
  .transform(data => Buffer.from(data, 'hex')),
100
77
 
101
- /** Hex string with an optional 0x prefix, which gets removed as part of the parsing */
78
+ /** Hex string with an optional 0x prefix which gets removed as part of the parsing. */
102
79
  HexString: hexSchema,
103
80
  };
104
81