@aztec/txe 0.0.0-test.1 → 0.0.1-commit.0b941701

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 (100) hide show
  1. package/dest/bin/index.d.ts +1 -1
  2. package/dest/bin/index.js +3 -2
  3. package/dest/constants.d.ts +3 -0
  4. package/dest/constants.d.ts.map +1 -0
  5. package/dest/constants.js +2 -0
  6. package/dest/index.d.ts +1 -1
  7. package/dest/index.d.ts.map +1 -1
  8. package/dest/index.js +81 -51
  9. package/dest/oracle/interfaces.d.ts +57 -0
  10. package/dest/oracle/interfaces.d.ts.map +1 -0
  11. package/dest/oracle/interfaces.js +3 -0
  12. package/dest/oracle/txe_oracle_public_context.d.ts +34 -0
  13. package/dest/oracle/txe_oracle_public_context.d.ts.map +1 -0
  14. package/dest/oracle/txe_oracle_public_context.js +123 -0
  15. package/dest/oracle/txe_oracle_top_level_context.d.ts +66 -0
  16. package/dest/oracle/txe_oracle_top_level_context.d.ts.map +1 -0
  17. package/dest/oracle/txe_oracle_top_level_context.js +469 -0
  18. package/dest/rpc_translator.d.ts +252 -0
  19. package/dest/rpc_translator.d.ts.map +1 -0
  20. package/dest/rpc_translator.js +680 -0
  21. package/dest/state_machine/archiver.d.ts +31 -0
  22. package/dest/state_machine/archiver.d.ts.map +1 -0
  23. package/dest/state_machine/archiver.js +85 -0
  24. package/dest/state_machine/dummy_p2p_client.d.ts +55 -0
  25. package/dest/state_machine/dummy_p2p_client.d.ts.map +1 -0
  26. package/dest/state_machine/dummy_p2p_client.js +141 -0
  27. package/dest/state_machine/global_variable_builder.d.ts +11 -0
  28. package/dest/state_machine/global_variable_builder.d.ts.map +1 -0
  29. package/dest/state_machine/global_variable_builder.js +22 -0
  30. package/dest/state_machine/index.d.ts +16 -0
  31. package/dest/state_machine/index.d.ts.map +1 -0
  32. package/dest/state_machine/index.js +63 -0
  33. package/dest/state_machine/mock_epoch_cache.d.ts +27 -0
  34. package/dest/state_machine/mock_epoch_cache.d.ts.map +1 -0
  35. package/dest/state_machine/mock_epoch_cache.js +54 -0
  36. package/dest/state_machine/synchronizer.d.ts +33 -0
  37. package/dest/state_machine/synchronizer.d.ts.map +1 -0
  38. package/dest/state_machine/synchronizer.js +59 -0
  39. package/dest/txe_session.d.ts +75 -0
  40. package/dest/txe_session.d.ts.map +1 -0
  41. package/dest/txe_session.js +302 -0
  42. package/dest/util/encoding.d.ts +641 -24
  43. package/dest/util/encoding.d.ts.map +1 -1
  44. package/dest/util/encoding.js +66 -11
  45. package/dest/util/expected_failure_error.d.ts +1 -1
  46. package/dest/util/expected_failure_error.d.ts.map +1 -1
  47. package/dest/util/txe_account_store.d.ts +10 -0
  48. package/dest/util/txe_account_store.d.ts.map +1 -0
  49. package/dest/util/{txe_account_data_provider.js → txe_account_store.js} +1 -1
  50. package/dest/util/txe_contract_store.d.ts +12 -0
  51. package/dest/util/txe_contract_store.d.ts.map +1 -0
  52. package/dest/util/txe_contract_store.js +22 -0
  53. package/dest/util/txe_public_contract_data_source.d.ts +12 -11
  54. package/dest/util/txe_public_contract_data_source.d.ts.map +1 -1
  55. package/dest/util/txe_public_contract_data_source.js +37 -58
  56. package/dest/utils/block_creation.d.ts +28 -0
  57. package/dest/utils/block_creation.d.ts.map +1 -0
  58. package/dest/utils/block_creation.js +56 -0
  59. package/dest/utils/tx_effect_creation.d.ts +5 -0
  60. package/dest/utils/tx_effect_creation.d.ts.map +1 -0
  61. package/dest/utils/tx_effect_creation.js +13 -0
  62. package/package.json +30 -24
  63. package/src/bin/index.ts +3 -2
  64. package/src/constants.ts +3 -0
  65. package/src/index.ts +105 -74
  66. package/src/oracle/interfaces.ts +86 -0
  67. package/src/oracle/txe_oracle_public_context.ts +160 -0
  68. package/src/oracle/txe_oracle_top_level_context.ts +728 -0
  69. package/src/rpc_translator.ts +1091 -0
  70. package/src/state_machine/archiver.ts +95 -0
  71. package/src/state_machine/dummy_p2p_client.ts +209 -0
  72. package/src/state_machine/global_variable_builder.ts +38 -0
  73. package/src/state_machine/index.ts +100 -0
  74. package/src/state_machine/mock_epoch_cache.ts +67 -0
  75. package/src/state_machine/synchronizer.ts +88 -0
  76. package/src/txe_session.ts +545 -0
  77. package/src/util/encoding.ts +85 -11
  78. package/src/util/{txe_account_data_provider.ts → txe_account_store.ts} +1 -1
  79. package/src/util/txe_contract_store.ts +36 -0
  80. package/src/util/txe_public_contract_data_source.ts +43 -58
  81. package/src/utils/block_creation.ts +95 -0
  82. package/src/utils/tx_effect_creation.ts +30 -0
  83. package/dest/node/txe_node.d.ts +0 -358
  84. package/dest/node/txe_node.d.ts.map +0 -1
  85. package/dest/node/txe_node.js +0 -504
  86. package/dest/oracle/txe_oracle.d.ts +0 -152
  87. package/dest/oracle/txe_oracle.d.ts.map +0 -1
  88. package/dest/oracle/txe_oracle.js +0 -833
  89. package/dest/txe_service/txe_service.d.ts +0 -212
  90. package/dest/txe_service/txe_service.d.ts.map +0 -1
  91. package/dest/txe_service/txe_service.js +0 -572
  92. package/dest/util/txe_account_data_provider.d.ts +0 -10
  93. package/dest/util/txe_account_data_provider.d.ts.map +0 -1
  94. package/dest/util/txe_world_state_db.d.ts +0 -14
  95. package/dest/util/txe_world_state_db.d.ts.map +0 -1
  96. package/dest/util/txe_world_state_db.js +0 -27
  97. package/src/node/txe_node.ts +0 -725
  98. package/src/oracle/txe_oracle.ts +0 -1241
  99. package/src/txe_service/txe_service.ts +0 -749
  100. package/src/util/txe_world_state_db.ts +0 -38
@@ -0,0 +1,680 @@
1
+ import { Fr, Point } from '@aztec/aztec.js/fields';
2
+ import { MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX } from '@aztec/constants';
3
+ import { BlockNumber } from '@aztec/foundation/branded-types';
4
+ import { packAsHintedNote } from '@aztec/pxe/simulator';
5
+ import { EventSelector, FunctionSelector, NoteSelector } from '@aztec/stdlib/abi';
6
+ import { AztecAddress } from '@aztec/stdlib/aztec-address';
7
+ import { L2BlockHash } from '@aztec/stdlib/block';
8
+ import { addressFromSingle, arrayOfArraysToBoundedVecOfArrays, arrayToBoundedVec, bufferToU8Array, fromArray, fromSingle, fromUintArray, fromUintBoundedVec, toArray, toForeignCallResult, toSingle } from './util/encoding.js';
9
+ const MAX_EVENT_LEN = 12; // This is MAX_MESSAGE_CONTENT_LEN - PRIVATE_EVENT_RESERVED_FIELDS
10
+ const MAX_PRIVATE_EVENTS_PER_TXE_QUERY = 5;
11
+ export class UnavailableOracleError extends Error {
12
+ constructor(oracleName){
13
+ super(`${oracleName} oracles not available with the current handler`);
14
+ }
15
+ }
16
+ export class RPCTranslator {
17
+ stateHandler;
18
+ oracleHandler;
19
+ /**
20
+ * Create a new instance of `RPCTranslator` that will translate all TXE RPC calls to and from the foreign
21
+ * (`ForeignCallSingle`, `ForeignCallResult`, etc.) and native TS types, delegating actual execution of the oracles
22
+ * to the different handlers.
23
+ * @param stateHandler The handler that will process TXE session state transitions, such as entering a private or
24
+ * public context.
25
+ * @param oracleHandler The handler that will process all other oracle calls that are not directly related to session
26
+ * state.
27
+ */ constructor(stateHandler, oracleHandler){
28
+ this.stateHandler = stateHandler;
29
+ this.oracleHandler = oracleHandler;
30
+ }
31
+ // Note: If you rename the following functions to not start with "handlerAs", you must also update the validation
32
+ // check in `TXESession.processFunction`.
33
+ handlerAsMisc() {
34
+ if (!('isMisc' in this.oracleHandler)) {
35
+ throw new UnavailableOracleError('Misc');
36
+ }
37
+ return this.oracleHandler;
38
+ }
39
+ handlerAsUtility() {
40
+ if (!('isUtility' in this.oracleHandler)) {
41
+ throw new UnavailableOracleError('Utility');
42
+ }
43
+ return this.oracleHandler;
44
+ }
45
+ handlerAsPrivate() {
46
+ if (!('isPrivate' in this.oracleHandler)) {
47
+ throw new UnavailableOracleError('Private');
48
+ }
49
+ return this.oracleHandler;
50
+ }
51
+ handlerAsAvm() {
52
+ if (!('isAvm' in this.oracleHandler)) {
53
+ throw new UnavailableOracleError('Avm');
54
+ }
55
+ return this.oracleHandler;
56
+ }
57
+ handlerAsTxe() {
58
+ if (!('isTxe' in this.oracleHandler)) {
59
+ throw new UnavailableOracleError('Txe');
60
+ }
61
+ return this.oracleHandler;
62
+ }
63
+ // TXE session state transition functions - these get handled by the state handler
64
+ async txeSetTopLevelTXEContext() {
65
+ await this.stateHandler.enterTopLevelState();
66
+ return toForeignCallResult([]);
67
+ }
68
+ async txeSetPrivateTXEContext(foreignContractAddressIsSome, foreignContractAddressValue, foreignAnchorBlockNumberIsSome, foreignAnchorBlockNumberValue) {
69
+ const contractAddress = fromSingle(foreignContractAddressIsSome).toBool() ? AztecAddress.fromField(fromSingle(foreignContractAddressValue)) : undefined;
70
+ const anchorBlockNumber = fromSingle(foreignAnchorBlockNumberIsSome).toBool() ? BlockNumber(fromSingle(foreignAnchorBlockNumberValue).toNumber()) : undefined;
71
+ const privateContextInputs = await this.stateHandler.enterPrivateState(contractAddress, anchorBlockNumber);
72
+ return toForeignCallResult(privateContextInputs.toFields().map(toSingle));
73
+ }
74
+ async txeSetPublicTXEContext(foreignContractAddressIsSome, foreignContractAddressValue) {
75
+ const contractAddress = fromSingle(foreignContractAddressIsSome).toBool() ? AztecAddress.fromField(fromSingle(foreignContractAddressValue)) : undefined;
76
+ await this.stateHandler.enterPublicState(contractAddress);
77
+ return toForeignCallResult([]);
78
+ }
79
+ async txeSetUtilityTXEContext(foreignContractAddressIsSome, foreignContractAddressValue) {
80
+ const contractAddress = fromSingle(foreignContractAddressIsSome).toBool() ? AztecAddress.fromField(fromSingle(foreignContractAddressValue)) : undefined;
81
+ await this.stateHandler.enterUtilityState(contractAddress);
82
+ return toForeignCallResult([]);
83
+ }
84
+ // Other oracles - these get handled by the oracle handler
85
+ // TXE-specific oracles
86
+ txeGetDefaultAddress() {
87
+ const defaultAddress = this.handlerAsTxe().txeGetDefaultAddress();
88
+ return toForeignCallResult([
89
+ toSingle(defaultAddress)
90
+ ]);
91
+ }
92
+ async txeGetNextBlockNumber() {
93
+ const nextBlockNumber = await this.handlerAsTxe().txeGetNextBlockNumber();
94
+ return toForeignCallResult([
95
+ toSingle(nextBlockNumber)
96
+ ]);
97
+ }
98
+ async txeGetNextBlockTimestamp() {
99
+ const nextBlockTimestamp = await this.handlerAsTxe().txeGetNextBlockTimestamp();
100
+ return toForeignCallResult([
101
+ toSingle(nextBlockTimestamp)
102
+ ]);
103
+ }
104
+ async txeAdvanceBlocksBy(foreignBlocks) {
105
+ const blocks = fromSingle(foreignBlocks).toNumber();
106
+ await this.handlerAsTxe().txeAdvanceBlocksBy(blocks);
107
+ return toForeignCallResult([]);
108
+ }
109
+ txeAdvanceTimestampBy(foreignDuration) {
110
+ const duration = fromSingle(foreignDuration).toBigInt();
111
+ this.handlerAsTxe().txeAdvanceTimestampBy(duration);
112
+ return toForeignCallResult([]);
113
+ }
114
+ async txeDeploy(artifact, instance, foreignSecret) {
115
+ const secret = fromSingle(foreignSecret);
116
+ await this.handlerAsTxe().txeDeploy(artifact, instance, secret);
117
+ return toForeignCallResult([
118
+ toArray([
119
+ instance.salt,
120
+ instance.deployer.toField(),
121
+ instance.currentContractClassId,
122
+ instance.initializationHash,
123
+ ...instance.publicKeys.toFields()
124
+ ])
125
+ ]);
126
+ }
127
+ async txeCreateAccount(foreignSecret) {
128
+ const secret = fromSingle(foreignSecret);
129
+ const completeAddress = await this.handlerAsTxe().txeCreateAccount(secret);
130
+ return toForeignCallResult([
131
+ toSingle(completeAddress.address),
132
+ ...completeAddress.publicKeys.toFields().map(toSingle)
133
+ ]);
134
+ }
135
+ async txeAddAccount(artifact, instance, foreignSecret) {
136
+ const secret = fromSingle(foreignSecret);
137
+ const completeAddress = await this.handlerAsTxe().txeAddAccount(artifact, instance, secret);
138
+ return toForeignCallResult([
139
+ toSingle(completeAddress.address),
140
+ ...completeAddress.publicKeys.toFields().map(toSingle)
141
+ ]);
142
+ }
143
+ async txeAddAuthWitness(foreignAddress, foreignMessageHash) {
144
+ const address = addressFromSingle(foreignAddress);
145
+ const messageHash = fromSingle(foreignMessageHash);
146
+ await this.handlerAsTxe().txeAddAuthWitness(address, messageHash);
147
+ return toForeignCallResult([]);
148
+ }
149
+ // PXE oracles
150
+ utilityAssertCompatibleOracleVersion(foreignVersion) {
151
+ const version = fromSingle(foreignVersion).toNumber();
152
+ this.handlerAsMisc().utilityAssertCompatibleOracleVersion(version);
153
+ return toForeignCallResult([]);
154
+ }
155
+ utilityGetRandomField() {
156
+ const randomField = this.handlerAsMisc().utilityGetRandomField();
157
+ return toForeignCallResult([
158
+ toSingle(randomField)
159
+ ]);
160
+ }
161
+ async txeGetLastBlockTimestamp() {
162
+ const timestamp = await this.handlerAsTxe().txeGetLastBlockTimestamp();
163
+ return toForeignCallResult([
164
+ toSingle(new Fr(timestamp))
165
+ ]);
166
+ }
167
+ async txeGetLastTxEffects() {
168
+ const { txHash, noteHashes, nullifiers } = await this.handlerAsTxe().txeGetLastTxEffects();
169
+ return toForeignCallResult([
170
+ toSingle(txHash.hash),
171
+ ...arrayToBoundedVec(toArray(noteHashes), MAX_NOTE_HASHES_PER_TX),
172
+ ...arrayToBoundedVec(toArray(nullifiers), MAX_NULLIFIERS_PER_TX)
173
+ ]);
174
+ }
175
+ async txeGetPrivateEvents(foreignSelector, foreignContractAddress, foreignScope) {
176
+ const selector = EventSelector.fromField(fromSingle(foreignSelector));
177
+ const contractAddress = addressFromSingle(foreignContractAddress);
178
+ const scope = addressFromSingle(foreignScope);
179
+ const events = await this.handlerAsTxe().txeGetPrivateEvents(selector, contractAddress, scope);
180
+ if (events.length > MAX_PRIVATE_EVENTS_PER_TXE_QUERY) {
181
+ throw new Error(`Array of length ${events.length} larger than maxLen ${MAX_PRIVATE_EVENTS_PER_TXE_QUERY}`);
182
+ }
183
+ if (events.some((e)=>e.length > MAX_EVENT_LEN)) {
184
+ throw new Error(`Some private event has length larger than maxLen ${MAX_EVENT_LEN}`);
185
+ }
186
+ // This is a workaround as Noir does not currently let us return nested structs with arrays. We instead return a raw
187
+ // multidimensional array in get_private_events_oracle and create the BoundedVecs here.
188
+ const rawArrayStorage = events.map((e)=>e.concat(Array(MAX_EVENT_LEN - e.length).fill(new Fr(0)))).concat(Array(MAX_PRIVATE_EVENTS_PER_TXE_QUERY - events.length).fill(Array(MAX_EVENT_LEN).fill(new Fr(0)))).flat();
189
+ const eventLengths = events.map((e)=>new Fr(e.length)).concat(Array(MAX_PRIVATE_EVENTS_PER_TXE_QUERY - events.length).fill(new Fr(0)));
190
+ const queryLength = new Fr(events.length);
191
+ return toForeignCallResult([
192
+ toArray(rawArrayStorage),
193
+ toArray(eventLengths),
194
+ toSingle(queryLength)
195
+ ]);
196
+ }
197
+ privateStoreInExecutionCache(foreignValues, foreignHash) {
198
+ const values = fromArray(foreignValues);
199
+ const hash = fromSingle(foreignHash);
200
+ this.handlerAsPrivate().privateStoreInExecutionCache(values, hash);
201
+ return toForeignCallResult([]);
202
+ }
203
+ async privateLoadFromExecutionCache(foreignHash) {
204
+ const hash = fromSingle(foreignHash);
205
+ const returns = await this.handlerAsPrivate().privateLoadFromExecutionCache(hash);
206
+ return toForeignCallResult([
207
+ toArray(returns)
208
+ ]);
209
+ }
210
+ // When the argument is a slice, noir automatically adds a length field to oracle call.
211
+ // When the argument is an array, we add the field length manually to the signature.
212
+ utilityDebugLog(foreignLevel, foreignMessage, _foreignLength, foreignFields) {
213
+ const level = fromSingle(foreignLevel).toNumber();
214
+ const message = fromArray(foreignMessage).map((field)=>String.fromCharCode(field.toNumber())).join('');
215
+ const fields = fromArray(foreignFields);
216
+ this.handlerAsMisc().utilityDebugLog(level, message, fields);
217
+ return toForeignCallResult([]);
218
+ }
219
+ async utilityStorageRead(foreignBlockHash, foreignContractAddress, foreignStartStorageSlot, foreignNumberOfElements) {
220
+ const blockHash = L2BlockHash.fromString(foreignBlockHash);
221
+ const contractAddress = addressFromSingle(foreignContractAddress);
222
+ const startStorageSlot = fromSingle(foreignStartStorageSlot);
223
+ const numberOfElements = fromSingle(foreignNumberOfElements).toNumber();
224
+ const values = await this.handlerAsUtility().utilityStorageRead(blockHash, contractAddress, startStorageSlot, numberOfElements);
225
+ return toForeignCallResult([
226
+ toArray(values)
227
+ ]);
228
+ }
229
+ async utilityGetPublicDataWitness(foreignBlockHash, foreignLeafSlot) {
230
+ const blockHash = L2BlockHash.fromString(foreignBlockHash);
231
+ const leafSlot = fromSingle(foreignLeafSlot);
232
+ const witness = await this.handlerAsUtility().utilityGetPublicDataWitness(blockHash, leafSlot);
233
+ if (!witness) {
234
+ throw new Error(`Public data witness not found for slot ${leafSlot} at block ${blockHash.toString()}.`);
235
+ }
236
+ return toForeignCallResult(witness.toNoirRepresentation());
237
+ }
238
+ async utilityGetNotes(foreignOwnerIsSome, foreignOwnerValue, foreignStorageSlot, foreignNumSelects, foreignSelectByIndexes, foreignSelectByOffsets, foreignSelectByLengths, foreignSelectValues, foreignSelectComparators, foreignSortByIndexes, foreignSortByOffsets, foreignSortByLengths, foreignSortOrder, foreignLimit, foreignOffset, foreignStatus, foreignMaxNotes, foreignPackedHintedNoteLength) {
239
+ // Parse Option<AztecAddress>: ownerIsSome is 0 for None, 1 for Some
240
+ const owner = fromSingle(foreignOwnerIsSome).toBool() ? AztecAddress.fromField(fromSingle(foreignOwnerValue)) : undefined;
241
+ const storageSlot = fromSingle(foreignStorageSlot);
242
+ const numSelects = fromSingle(foreignNumSelects).toNumber();
243
+ const selectByIndexes = fromArray(foreignSelectByIndexes).map((fr)=>fr.toNumber());
244
+ const selectByOffsets = fromArray(foreignSelectByOffsets).map((fr)=>fr.toNumber());
245
+ const selectByLengths = fromArray(foreignSelectByLengths).map((fr)=>fr.toNumber());
246
+ const selectValues = fromArray(foreignSelectValues);
247
+ const selectComparators = fromArray(foreignSelectComparators).map((fr)=>fr.toNumber());
248
+ const sortByIndexes = fromArray(foreignSortByIndexes).map((fr)=>fr.toNumber());
249
+ const sortByOffsets = fromArray(foreignSortByOffsets).map((fr)=>fr.toNumber());
250
+ const sortByLengths = fromArray(foreignSortByLengths).map((fr)=>fr.toNumber());
251
+ const sortOrder = fromArray(foreignSortOrder).map((fr)=>fr.toNumber());
252
+ const limit = fromSingle(foreignLimit).toNumber();
253
+ const offset = fromSingle(foreignOffset).toNumber();
254
+ const status = fromSingle(foreignStatus).toNumber();
255
+ const maxNotes = fromSingle(foreignMaxNotes).toNumber();
256
+ const packedHintedNoteLength = fromSingle(foreignPackedHintedNoteLength).toNumber();
257
+ const noteDatas = await this.handlerAsUtility().utilityGetNotes(owner, storageSlot, numSelects, selectByIndexes, selectByOffsets, selectByLengths, selectValues, selectComparators, sortByIndexes, sortByOffsets, sortByLengths, sortOrder, limit, offset, status);
258
+ const returnDataAsArrayOfArrays = noteDatas.map((noteData)=>packAsHintedNote({
259
+ contractAddress: noteData.contractAddress,
260
+ owner: noteData.owner,
261
+ randomness: noteData.randomness,
262
+ storageSlot: noteData.storageSlot,
263
+ noteNonce: noteData.noteNonce,
264
+ isPending: noteData.isPending,
265
+ note: noteData.note
266
+ }));
267
+ // Now we convert each sub-array to an array of ForeignCallSingles
268
+ const returnDataAsArrayOfForeignCallSingleArrays = returnDataAsArrayOfArrays.map((subArray)=>subArray.map(toSingle));
269
+ // At last we convert the array of arrays to a bounded vec of arrays
270
+ return toForeignCallResult(arrayOfArraysToBoundedVecOfArrays(returnDataAsArrayOfForeignCallSingleArrays, maxNotes, packedHintedNoteLength));
271
+ }
272
+ privateNotifyCreatedNote(foreignOwner, foreignStorageSlot, foreignRandomness, foreignNoteTypeId, foreignNote, foreignNoteHash, foreignCounter) {
273
+ const owner = addressFromSingle(foreignOwner);
274
+ const storageSlot = fromSingle(foreignStorageSlot);
275
+ const randomness = fromSingle(foreignRandomness);
276
+ const noteTypeId = NoteSelector.fromField(fromSingle(foreignNoteTypeId));
277
+ const note = fromArray(foreignNote);
278
+ const noteHash = fromSingle(foreignNoteHash);
279
+ const counter = fromSingle(foreignCounter).toNumber();
280
+ this.handlerAsPrivate().privateNotifyCreatedNote(owner, storageSlot, randomness, noteTypeId, note, noteHash, counter);
281
+ return toForeignCallResult([]);
282
+ }
283
+ async privateNotifyNullifiedNote(foreignInnerNullifier, foreignNoteHash, foreignCounter) {
284
+ const innerNullifier = fromSingle(foreignInnerNullifier);
285
+ const noteHash = fromSingle(foreignNoteHash);
286
+ const counter = fromSingle(foreignCounter).toNumber();
287
+ await this.handlerAsPrivate().privateNotifyNullifiedNote(innerNullifier, noteHash, counter);
288
+ return toForeignCallResult([]);
289
+ }
290
+ async privateNotifyCreatedNullifier(foreignInnerNullifier) {
291
+ const innerNullifier = fromSingle(foreignInnerNullifier);
292
+ await this.handlerAsPrivate().privateNotifyCreatedNullifier(innerNullifier);
293
+ return toForeignCallResult([]);
294
+ }
295
+ async privateIsNullifierPending(foreignInnerNullifier, foreignContractAddress) {
296
+ const innerNullifier = fromSingle(foreignInnerNullifier);
297
+ const contractAddress = addressFromSingle(foreignContractAddress);
298
+ const isPending = await this.handlerAsPrivate().privateIsNullifierPending(innerNullifier, contractAddress);
299
+ return toForeignCallResult([
300
+ toSingle(new Fr(isPending))
301
+ ]);
302
+ }
303
+ async utilityCheckNullifierExists(foreignInnerNullifier) {
304
+ const innerNullifier = fromSingle(foreignInnerNullifier);
305
+ const exists = await this.handlerAsUtility().utilityCheckNullifierExists(innerNullifier);
306
+ return toForeignCallResult([
307
+ toSingle(new Fr(exists))
308
+ ]);
309
+ }
310
+ async utilityGetContractInstance(foreignAddress) {
311
+ const address = addressFromSingle(foreignAddress);
312
+ const instance = await this.handlerAsUtility().utilityGetContractInstance(address);
313
+ return toForeignCallResult([
314
+ instance.salt,
315
+ instance.deployer.toField(),
316
+ instance.currentContractClassId,
317
+ instance.initializationHash,
318
+ ...instance.publicKeys.toFields()
319
+ ].map(toSingle));
320
+ }
321
+ async utilityGetPublicKeysAndPartialAddress(foreignAddress) {
322
+ const address = addressFromSingle(foreignAddress);
323
+ const { publicKeys, partialAddress } = await this.handlerAsUtility().utilityGetPublicKeysAndPartialAddress(address);
324
+ return toForeignCallResult([
325
+ toArray([
326
+ ...publicKeys.toFields(),
327
+ partialAddress
328
+ ])
329
+ ]);
330
+ }
331
+ async utilityGetKeyValidationRequest(foreignPkMHash) {
332
+ const pkMHash = fromSingle(foreignPkMHash);
333
+ const keyValidationRequest = await this.handlerAsUtility().utilityGetKeyValidationRequest(pkMHash);
334
+ return toForeignCallResult(keyValidationRequest.toFields().map(toSingle));
335
+ }
336
+ privateCallPrivateFunction(_foreignTargetContractAddress, _foreignFunctionSelector, _foreignArgsHash, _foreignSideEffectCounter, _foreignIsStaticCall) {
337
+ throw new Error('Contract calls are forbidden inside a `TestEnvironment::private_context`, use `private_call` instead');
338
+ }
339
+ async utilityGetNullifierMembershipWitness(foreignBlockHash, foreignNullifier) {
340
+ const blockHash = L2BlockHash.fromString(foreignBlockHash);
341
+ const nullifier = fromSingle(foreignNullifier);
342
+ const witness = await this.handlerAsUtility().utilityGetNullifierMembershipWitness(blockHash, nullifier);
343
+ if (!witness) {
344
+ throw new Error(`Nullifier membership witness not found at block ${blockHash}.`);
345
+ }
346
+ return toForeignCallResult(witness.toNoirRepresentation());
347
+ }
348
+ async utilityGetAuthWitness(foreignMessageHash) {
349
+ const messageHash = fromSingle(foreignMessageHash);
350
+ const authWitness = await this.handlerAsUtility().utilityGetAuthWitness(messageHash);
351
+ if (!authWitness) {
352
+ throw new Error(`Auth witness not found for message hash ${messageHash}.`);
353
+ }
354
+ return toForeignCallResult([
355
+ toArray(authWitness)
356
+ ]);
357
+ }
358
+ privateNotifyEnqueuedPublicFunctionCall(_foreignTargetContractAddress, _foreignCalldataHash, _foreignSideEffectCounter, _foreignIsStaticCall) {
359
+ throw new Error('Enqueueing public calls is not supported in TestEnvironment::private_context');
360
+ }
361
+ privateNotifySetPublicTeardownFunctionCall(_foreignTargetContractAddress, _foreignCalldataHash, _foreignSideEffectCounter, _foreignIsStaticCall) {
362
+ throw new Error('Enqueueing public calls is not supported in TestEnvironment::private_context');
363
+ }
364
+ privateNotifySetMinRevertibleSideEffectCounter(_foreignMinRevertibleSideEffectCounter) {
365
+ throw new Error('Enqueueing public calls is not supported in TestEnvironment::private_context');
366
+ }
367
+ async privateIsSideEffectCounterRevertible(foreignSideEffectCounter) {
368
+ const sideEffectCounter = fromSingle(foreignSideEffectCounter).toNumber();
369
+ const isRevertible = await this.handlerAsPrivate().privateIsSideEffectCounterRevertible(sideEffectCounter);
370
+ return toForeignCallResult([
371
+ toSingle(new Fr(isRevertible))
372
+ ]);
373
+ }
374
+ utilityGetUtilityContext() {
375
+ const context = this.handlerAsUtility().utilityGetUtilityContext();
376
+ return toForeignCallResult(context.toNoirRepresentation());
377
+ }
378
+ async utilityGetBlockHeader(foreignBlockNumber) {
379
+ const blockNumber = BlockNumber(fromSingle(foreignBlockNumber).toNumber());
380
+ const header = await this.handlerAsUtility().utilityGetBlockHeader(blockNumber);
381
+ if (!header) {
382
+ throw new Error(`Block header not found for block ${blockNumber}.`);
383
+ }
384
+ return toForeignCallResult(header.toFields().map(toSingle));
385
+ }
386
+ async utilityGetNoteHashMembershipWitness(foreignBlockHash, foreignLeafValue) {
387
+ const blockHash = L2BlockHash.fromString(foreignBlockHash);
388
+ const leafValue = fromSingle(foreignLeafValue);
389
+ const witness = await this.handlerAsUtility().utilityGetNoteHashMembershipWitness(blockHash, leafValue);
390
+ if (!witness) {
391
+ throw new Error(`Note hash ${leafValue} not found in the note hash tree at block ${blockHash.toString()}.`);
392
+ }
393
+ return toForeignCallResult(witness.toNoirRepresentation());
394
+ }
395
+ async utilityGetArchiveMembershipWitness(foreignBlockHash, foreignLeafValue) {
396
+ const blockHash = L2BlockHash.fromString(foreignBlockHash);
397
+ const leafValue = fromSingle(foreignLeafValue);
398
+ const witness = await this.handlerAsUtility().utilityGetArchiveMembershipWitness(blockHash, leafValue);
399
+ if (!witness) {
400
+ throw new Error(`Block hash ${leafValue} not found in the archive tree at block ${blockHash.toString()}.`);
401
+ }
402
+ return toForeignCallResult(witness.toNoirRepresentation());
403
+ }
404
+ async utilityGetLowNullifierMembershipWitness(foreignBlockHash, foreignNullifier) {
405
+ const blockHash = L2BlockHash.fromString(foreignBlockHash);
406
+ const nullifier = fromSingle(foreignNullifier);
407
+ const witness = await this.handlerAsUtility().utilityGetLowNullifierMembershipWitness(blockHash, nullifier);
408
+ if (!witness) {
409
+ throw new Error(`Low nullifier witness not found for nullifier ${nullifier} at block ${blockHash}.`);
410
+ }
411
+ return toForeignCallResult(witness.toNoirRepresentation());
412
+ }
413
+ async utilityFetchTaggedLogs(foreignPendingTaggedLogArrayBaseSlot) {
414
+ const pendingTaggedLogArrayBaseSlot = fromSingle(foreignPendingTaggedLogArrayBaseSlot);
415
+ await this.handlerAsUtility().utilityFetchTaggedLogs(pendingTaggedLogArrayBaseSlot);
416
+ return toForeignCallResult([]);
417
+ }
418
+ async utilityValidateAndStoreEnqueuedNotesAndEvents(foreignContractAddress, foreignNoteValidationRequestsArrayBaseSlot, foreignEventValidationRequestsArrayBaseSlot) {
419
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
420
+ const noteValidationRequestsArrayBaseSlot = fromSingle(foreignNoteValidationRequestsArrayBaseSlot);
421
+ const eventValidationRequestsArrayBaseSlot = fromSingle(foreignEventValidationRequestsArrayBaseSlot);
422
+ await this.handlerAsUtility().utilityValidateAndStoreEnqueuedNotesAndEvents(contractAddress, noteValidationRequestsArrayBaseSlot, eventValidationRequestsArrayBaseSlot);
423
+ return toForeignCallResult([]);
424
+ }
425
+ async utilityBulkRetrieveLogs(foreignContractAddress, foreignLogRetrievalRequestsArrayBaseSlot, foreignLogRetrievalResponsesArrayBaseSlot) {
426
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
427
+ const logRetrievalRequestsArrayBaseSlot = fromSingle(foreignLogRetrievalRequestsArrayBaseSlot);
428
+ const logRetrievalResponsesArrayBaseSlot = fromSingle(foreignLogRetrievalResponsesArrayBaseSlot);
429
+ await this.handlerAsUtility().utilityBulkRetrieveLogs(contractAddress, logRetrievalRequestsArrayBaseSlot, logRetrievalResponsesArrayBaseSlot);
430
+ return toForeignCallResult([]);
431
+ }
432
+ async utilityStoreCapsule(foreignContractAddress, foreignSlot, foreignCapsule) {
433
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
434
+ const slot = fromSingle(foreignSlot);
435
+ const capsule = fromArray(foreignCapsule);
436
+ await this.handlerAsUtility().utilityStoreCapsule(contractAddress, slot, capsule);
437
+ return toForeignCallResult([]);
438
+ }
439
+ async utilityLoadCapsule(foreignContractAddress, foreignSlot, foreignTSize) {
440
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
441
+ const slot = fromSingle(foreignSlot);
442
+ const tSize = fromSingle(foreignTSize).toNumber();
443
+ const values = await this.handlerAsUtility().utilityLoadCapsule(contractAddress, slot);
444
+ // We are going to return a Noir Option struct to represent the possibility of null values. Options are a struct
445
+ // with two fields: `some` (a boolean) and `value` (a field array in this case).
446
+ if (values === null) {
447
+ // No data was found so we set `some` to 0 and pad `value` with zeros get the correct return size.
448
+ return toForeignCallResult([
449
+ toSingle(new Fr(0)),
450
+ toArray(Array(tSize).fill(new Fr(0)))
451
+ ]);
452
+ } else {
453
+ // Data was found so we set `some` to 1 and return it along with `value`.
454
+ return toForeignCallResult([
455
+ toSingle(new Fr(1)),
456
+ toArray(values)
457
+ ]);
458
+ }
459
+ }
460
+ async utilityDeleteCapsule(foreignContractAddress, foreignSlot) {
461
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
462
+ const slot = fromSingle(foreignSlot);
463
+ await this.handlerAsUtility().utilityDeleteCapsule(contractAddress, slot);
464
+ return toForeignCallResult([]);
465
+ }
466
+ async utilityCopyCapsule(foreignContractAddress, foreignSrcSlot, foreignDstSlot, foreignNumEntries) {
467
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
468
+ const srcSlot = fromSingle(foreignSrcSlot);
469
+ const dstSlot = fromSingle(foreignDstSlot);
470
+ const numEntries = fromSingle(foreignNumEntries).toNumber();
471
+ await this.handlerAsUtility().utilityCopyCapsule(contractAddress, srcSlot, dstSlot, numEntries);
472
+ return toForeignCallResult([]);
473
+ }
474
+ // TODO: I forgot to add a corresponding function here, when I introduced an oracle method to txe_oracle.ts.
475
+ // The compiler didn't throw an error, so it took me a while to learn of the existence of this file, and that I need
476
+ // to implement this function here. Isn't there a way to programmatically identify that this is missing, given the
477
+ // existence of a txe_oracle method?
478
+ async utilityAes128Decrypt(foreignCiphertextBVecStorage, foreignCiphertextLength, foreignIv, foreignSymKey) {
479
+ const ciphertext = fromUintBoundedVec(foreignCiphertextBVecStorage, foreignCiphertextLength, 8);
480
+ const iv = fromUintArray(foreignIv, 8);
481
+ const symKey = fromUintArray(foreignSymKey, 8);
482
+ const plaintextBuffer = await this.handlerAsUtility().utilityAes128Decrypt(ciphertext, iv, symKey);
483
+ return toForeignCallResult(arrayToBoundedVec(bufferToU8Array(plaintextBuffer), foreignCiphertextBVecStorage.length));
484
+ }
485
+ async utilityGetSharedSecret(foreignAddress, foreignEphPKField0, foreignEphPKField1, foreignEphPKField2) {
486
+ const address = AztecAddress.fromField(fromSingle(foreignAddress));
487
+ const ephPK = Point.fromFields([
488
+ fromSingle(foreignEphPKField0),
489
+ fromSingle(foreignEphPKField1),
490
+ fromSingle(foreignEphPKField2)
491
+ ]);
492
+ const secret = await this.handlerAsUtility().utilityGetSharedSecret(address, ephPK);
493
+ return toForeignCallResult(secret.toFields().map(toSingle));
494
+ }
495
+ emitOffchainEffect(_foreignData) {
496
+ throw new Error('Offchain effects are not yet supported in the TestEnvironment');
497
+ }
498
+ // AVM opcodes
499
+ avmOpcodeEmitUnencryptedLog(_foreignMessage) {
500
+ // TODO(#8811): Implement
501
+ return toForeignCallResult([]);
502
+ }
503
+ async avmOpcodeStorageRead(foreignSlot, foreignContractAddress) {
504
+ const slot = fromSingle(foreignSlot);
505
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
506
+ const value = (await this.handlerAsAvm().avmOpcodeStorageRead(slot, contractAddress)).value;
507
+ return toForeignCallResult([
508
+ toSingle(new Fr(value))
509
+ ]);
510
+ }
511
+ async avmOpcodeStorageWrite(foreignSlot, foreignValue) {
512
+ const slot = fromSingle(foreignSlot);
513
+ const value = fromSingle(foreignValue);
514
+ await this.handlerAsAvm().avmOpcodeStorageWrite(slot, value);
515
+ return toForeignCallResult([]);
516
+ }
517
+ async avmOpcodeGetContractInstanceDeployer(foreignAddress) {
518
+ const address = addressFromSingle(foreignAddress);
519
+ const instance = await this.handlerAsUtility().utilityGetContractInstance(address);
520
+ return toForeignCallResult([
521
+ toSingle(instance.deployer),
522
+ // AVM requires an extra boolean indicating the instance was found
523
+ toSingle(new Fr(1))
524
+ ]);
525
+ }
526
+ async avmOpcodeGetContractInstanceClassId(foreignAddress) {
527
+ const address = addressFromSingle(foreignAddress);
528
+ const instance = await this.handlerAsUtility().utilityGetContractInstance(address);
529
+ return toForeignCallResult([
530
+ toSingle(instance.currentContractClassId),
531
+ // AVM requires an extra boolean indicating the instance was found
532
+ toSingle(new Fr(1))
533
+ ]);
534
+ }
535
+ async avmOpcodeGetContractInstanceInitializationHash(foreignAddress) {
536
+ const address = addressFromSingle(foreignAddress);
537
+ const instance = await this.handlerAsUtility().utilityGetContractInstance(address);
538
+ return toForeignCallResult([
539
+ toSingle(instance.initializationHash),
540
+ // AVM requires an extra boolean indicating the instance was found
541
+ toSingle(new Fr(1))
542
+ ]);
543
+ }
544
+ async avmOpcodeSender() {
545
+ const sender = await this.handlerAsAvm().avmOpcodeSender();
546
+ return toForeignCallResult([
547
+ toSingle(sender)
548
+ ]);
549
+ }
550
+ async avmOpcodeEmitNullifier(foreignNullifier) {
551
+ const nullifier = fromSingle(foreignNullifier);
552
+ await this.handlerAsAvm().avmOpcodeEmitNullifier(nullifier);
553
+ return toForeignCallResult([]);
554
+ }
555
+ async avmOpcodeEmitNoteHash(foreignNoteHash) {
556
+ const noteHash = fromSingle(foreignNoteHash);
557
+ await this.handlerAsAvm().avmOpcodeEmitNoteHash(noteHash);
558
+ return toForeignCallResult([]);
559
+ }
560
+ async avmOpcodeNullifierExists(foreignInnerNullifier, foreignTargetAddress) {
561
+ const innerNullifier = fromSingle(foreignInnerNullifier);
562
+ const targetAddress = AztecAddress.fromField(fromSingle(foreignTargetAddress));
563
+ const exists = await this.handlerAsAvm().avmOpcodeNullifierExists(innerNullifier, targetAddress);
564
+ return toForeignCallResult([
565
+ toSingle(new Fr(exists))
566
+ ]);
567
+ }
568
+ async avmOpcodeAddress() {
569
+ const contractAddress = await this.handlerAsAvm().avmOpcodeAddress();
570
+ return toForeignCallResult([
571
+ toSingle(contractAddress.toField())
572
+ ]);
573
+ }
574
+ async avmOpcodeBlockNumber() {
575
+ const blockNumber = await this.handlerAsAvm().avmOpcodeBlockNumber();
576
+ return toForeignCallResult([
577
+ toSingle(new Fr(blockNumber))
578
+ ]);
579
+ }
580
+ async avmOpcodeTimestamp() {
581
+ const timestamp = await this.handlerAsAvm().avmOpcodeTimestamp();
582
+ return toForeignCallResult([
583
+ toSingle(new Fr(timestamp))
584
+ ]);
585
+ }
586
+ async avmOpcodeIsStaticCall() {
587
+ const isStaticCall = await this.handlerAsAvm().avmOpcodeIsStaticCall();
588
+ return toForeignCallResult([
589
+ toSingle(new Fr(isStaticCall ? 1 : 0))
590
+ ]);
591
+ }
592
+ async avmOpcodeChainId() {
593
+ const chainId = await this.handlerAsAvm().avmOpcodeChainId();
594
+ return toForeignCallResult([
595
+ toSingle(chainId)
596
+ ]);
597
+ }
598
+ async avmOpcodeVersion() {
599
+ const version = await this.handlerAsAvm().avmOpcodeVersion();
600
+ return toForeignCallResult([
601
+ toSingle(version)
602
+ ]);
603
+ }
604
+ avmOpcodeReturndataSize() {
605
+ throw new Error('Contract calls are forbidden inside a `TestEnvironment::public_context`, use `public_call` instead');
606
+ }
607
+ avmOpcodeReturndataCopy(_foreignRdOffset, _foreignCopySize) {
608
+ throw new Error('Contract calls are forbidden inside a `TestEnvironment::public_context`, use `public_call` instead');
609
+ }
610
+ avmOpcodeCall(_foreignL2Gas, _foreignDaGas, _foreignAddress, _foreignLength, _foreignArgs) {
611
+ throw new Error('Contract calls are forbidden inside a `TestEnvironment::public_context`, use `public_call` instead');
612
+ }
613
+ avmOpcodeStaticCall(_foreignL2Gas, _foreignDaGas, _foreignAddress, _foreignLength, _foreignArgs) {
614
+ throw new Error('Contract calls are forbidden inside a `TestEnvironment::public_context`, use `public_call` instead');
615
+ }
616
+ avmOpcodeSuccessCopy() {
617
+ throw new Error('Contract calls are forbidden inside a `TestEnvironment::public_context`, use `public_call` instead');
618
+ }
619
+ async txePrivateCallNewFlow(foreignFrom, foreignTargetContractAddress, foreignFunctionSelector, foreignArgs, foreignArgsHash, foreignIsStaticCall) {
620
+ const from = addressFromSingle(foreignFrom);
621
+ const targetContractAddress = addressFromSingle(foreignTargetContractAddress);
622
+ const functionSelector = FunctionSelector.fromField(fromSingle(foreignFunctionSelector));
623
+ const args = fromArray(foreignArgs);
624
+ const argsHash = fromSingle(foreignArgsHash);
625
+ const isStaticCall = fromSingle(foreignIsStaticCall).toBool();
626
+ const returnValues = await this.handlerAsTxe().txePrivateCallNewFlow(from, targetContractAddress, functionSelector, args, argsHash, isStaticCall);
627
+ return toForeignCallResult([
628
+ toArray(returnValues)
629
+ ]);
630
+ }
631
+ async txeSimulateUtilityFunction(foreignTargetContractAddress, foreignFunctionSelector, foreignArgs) {
632
+ const targetContractAddress = addressFromSingle(foreignTargetContractAddress);
633
+ const functionSelector = FunctionSelector.fromField(fromSingle(foreignFunctionSelector));
634
+ const args = fromArray(foreignArgs);
635
+ const returnValues = await this.handlerAsTxe().txeSimulateUtilityFunction(targetContractAddress, functionSelector, args);
636
+ return toForeignCallResult([
637
+ toArray(returnValues)
638
+ ]);
639
+ }
640
+ async txePublicCallNewFlow(foreignFrom, foreignAddress, foreignCalldata, foreignIsStaticCall) {
641
+ const from = addressFromSingle(foreignFrom);
642
+ const address = addressFromSingle(foreignAddress);
643
+ const calldata = fromArray(foreignCalldata);
644
+ const isStaticCall = fromSingle(foreignIsStaticCall).toBool();
645
+ const returnValues = await this.handlerAsTxe().txePublicCallNewFlow(from, address, calldata, isStaticCall);
646
+ return toForeignCallResult([
647
+ toArray(returnValues)
648
+ ]);
649
+ }
650
+ async privateGetSenderForTags() {
651
+ const sender = await this.handlerAsPrivate().privateGetSenderForTags();
652
+ // Return a Noir Option struct with `some` and `value` fields
653
+ if (sender === undefined) {
654
+ // No sender found, return Option with some=0 and value=0
655
+ return toForeignCallResult([
656
+ toSingle(0),
657
+ toSingle(0)
658
+ ]);
659
+ } else {
660
+ // Sender found, return Option with some=1 and value=sender address
661
+ return toForeignCallResult([
662
+ toSingle(1),
663
+ toSingle(sender)
664
+ ]);
665
+ }
666
+ }
667
+ async privateSetSenderForTags(foreignSenderForTags) {
668
+ const senderForTags = AztecAddress.fromField(fromSingle(foreignSenderForTags));
669
+ await this.handlerAsPrivate().privateSetSenderForTags(senderForTags);
670
+ return toForeignCallResult([]);
671
+ }
672
+ async privateGetNextAppTagAsSender(foreignSender, foreignRecipient) {
673
+ const sender = AztecAddress.fromField(fromSingle(foreignSender));
674
+ const recipient = AztecAddress.fromField(fromSingle(foreignRecipient));
675
+ const nextAppTag = await this.handlerAsPrivate().privateGetNextAppTagAsSender(sender, recipient);
676
+ return toForeignCallResult([
677
+ toSingle(nextAppTag.value)
678
+ ]);
679
+ }
680
+ }