@aztec/txe 0.0.0-test.0 → 0.0.1-commit.001888fc

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 (96) 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 +146 -82
  9. package/dest/oracle/interfaces.d.ts +58 -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 +122 -0
  15. package/dest/oracle/txe_oracle_top_level_context.d.ts +65 -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 +542 -0
  18. package/dest/rpc_translator.d.ts +254 -0
  19. package/dest/rpc_translator.d.ts.map +1 -0
  20. package/dest/rpc_translator.js +791 -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 +59 -0
  25. package/dest/state_machine/dummy_p2p_client.d.ts.map +1 -0
  26. package/dest/state_machine/dummy_p2p_client.js +153 -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 +18 -0
  31. package/dest/state_machine/index.d.ts.map +1 -0
  32. package/dest/state_machine/index.js +72 -0
  33. package/dest/state_machine/mock_epoch_cache.d.ts +29 -0
  34. package/dest/state_machine/mock_epoch_cache.d.ts.map +1 -0
  35. package/dest/state_machine/mock_epoch_cache.js +58 -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 +78 -0
  40. package/dest/txe_session.d.ts.map +1 -0
  41. package/dest/txe_session.js +364 -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_public_contract_data_source.d.ts +11 -11
  51. package/dest/util/txe_public_contract_data_source.d.ts.map +1 -1
  52. package/dest/util/txe_public_contract_data_source.js +21 -59
  53. package/dest/utils/block_creation.d.ts +28 -0
  54. package/dest/utils/block_creation.d.ts.map +1 -0
  55. package/dest/utils/block_creation.js +58 -0
  56. package/dest/utils/tx_effect_creation.d.ts +5 -0
  57. package/dest/utils/tx_effect_creation.d.ts.map +1 -0
  58. package/dest/utils/tx_effect_creation.js +13 -0
  59. package/package.json +30 -24
  60. package/src/bin/index.ts +3 -2
  61. package/src/constants.ts +3 -0
  62. package/src/index.ts +171 -103
  63. package/src/oracle/interfaces.ts +87 -0
  64. package/src/oracle/txe_oracle_public_context.ts +158 -0
  65. package/src/oracle/txe_oracle_top_level_context.ts +782 -0
  66. package/src/rpc_translator.ts +1218 -0
  67. package/src/state_machine/archiver.ts +97 -0
  68. package/src/state_machine/dummy_p2p_client.ts +227 -0
  69. package/src/state_machine/global_variable_builder.ts +38 -0
  70. package/src/state_machine/index.ts +120 -0
  71. package/src/state_machine/mock_epoch_cache.ts +72 -0
  72. package/src/state_machine/synchronizer.ts +88 -0
  73. package/src/txe_session.ts +556 -0
  74. package/src/util/encoding.ts +85 -11
  75. package/src/util/{txe_account_data_provider.ts → txe_account_store.ts} +1 -1
  76. package/src/util/txe_public_contract_data_source.ts +28 -69
  77. package/src/utils/block_creation.ts +97 -0
  78. package/src/utils/tx_effect_creation.ts +30 -0
  79. package/dest/node/txe_node.d.ts +0 -358
  80. package/dest/node/txe_node.d.ts.map +0 -1
  81. package/dest/node/txe_node.js +0 -504
  82. package/dest/oracle/txe_oracle.d.ts +0 -152
  83. package/dest/oracle/txe_oracle.d.ts.map +0 -1
  84. package/dest/oracle/txe_oracle.js +0 -833
  85. package/dest/txe_service/txe_service.d.ts +0 -212
  86. package/dest/txe_service/txe_service.d.ts.map +0 -1
  87. package/dest/txe_service/txe_service.js +0 -572
  88. package/dest/util/txe_account_data_provider.d.ts +0 -10
  89. package/dest/util/txe_account_data_provider.d.ts.map +0 -1
  90. package/dest/util/txe_world_state_db.d.ts +0 -14
  91. package/dest/util/txe_world_state_db.d.ts.map +0 -1
  92. package/dest/util/txe_world_state_db.js +0 -27
  93. package/src/node/txe_node.ts +0 -725
  94. package/src/oracle/txe_oracle.ts +0 -1241
  95. package/src/txe_service/txe_service.ts +0 -749
  96. package/src/util/txe_world_state_db.ts +0 -38
@@ -0,0 +1,791 @@
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 { BlockHash } 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 = 10; // This is MAX_MESSAGE_CONTENT_LEN - PRIVATE_EVENT_MSG_PLAINTEXT_RESERVED_FIELDS_LEN
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
+ // eslint-disable-next-line camelcase
65
+ async aztec_txe_setTopLevelTXEContext() {
66
+ await this.stateHandler.enterTopLevelState();
67
+ return toForeignCallResult([]);
68
+ }
69
+ // eslint-disable-next-line camelcase
70
+ async aztec_txe_setPrivateTXEContext(foreignContractAddressIsSome, foreignContractAddressValue, foreignAnchorBlockNumberIsSome, foreignAnchorBlockNumberValue) {
71
+ const contractAddress = fromSingle(foreignContractAddressIsSome).toBool() ? AztecAddress.fromField(fromSingle(foreignContractAddressValue)) : undefined;
72
+ const anchorBlockNumber = fromSingle(foreignAnchorBlockNumberIsSome).toBool() ? BlockNumber(fromSingle(foreignAnchorBlockNumberValue).toNumber()) : undefined;
73
+ const privateContextInputs = await this.stateHandler.enterPrivateState(contractAddress, anchorBlockNumber);
74
+ return toForeignCallResult(privateContextInputs.toFields().map(toSingle));
75
+ }
76
+ // eslint-disable-next-line camelcase
77
+ async aztec_txe_setPublicTXEContext(foreignContractAddressIsSome, foreignContractAddressValue) {
78
+ const contractAddress = fromSingle(foreignContractAddressIsSome).toBool() ? AztecAddress.fromField(fromSingle(foreignContractAddressValue)) : undefined;
79
+ await this.stateHandler.enterPublicState(contractAddress);
80
+ return toForeignCallResult([]);
81
+ }
82
+ // eslint-disable-next-line camelcase
83
+ async aztec_txe_setUtilityTXEContext(foreignContractAddressIsSome, foreignContractAddressValue) {
84
+ const contractAddress = fromSingle(foreignContractAddressIsSome).toBool() ? AztecAddress.fromField(fromSingle(foreignContractAddressValue)) : undefined;
85
+ await this.stateHandler.enterUtilityState(contractAddress);
86
+ return toForeignCallResult([]);
87
+ }
88
+ // Other oracles - these get handled by the oracle handler
89
+ // TXE-specific oracles
90
+ // eslint-disable-next-line camelcase
91
+ aztec_txe_getDefaultAddress() {
92
+ const defaultAddress = this.handlerAsTxe().getDefaultAddress();
93
+ return toForeignCallResult([
94
+ toSingle(defaultAddress)
95
+ ]);
96
+ }
97
+ // eslint-disable-next-line camelcase
98
+ async aztec_txe_getNextBlockNumber() {
99
+ const nextBlockNumber = await this.handlerAsTxe().getNextBlockNumber();
100
+ return toForeignCallResult([
101
+ toSingle(nextBlockNumber)
102
+ ]);
103
+ }
104
+ // eslint-disable-next-line camelcase
105
+ async aztec_txe_getNextBlockTimestamp() {
106
+ const nextBlockTimestamp = await this.handlerAsTxe().getNextBlockTimestamp();
107
+ return toForeignCallResult([
108
+ toSingle(nextBlockTimestamp)
109
+ ]);
110
+ }
111
+ // eslint-disable-next-line camelcase
112
+ async aztec_txe_advanceBlocksBy(foreignBlocks) {
113
+ const blocks = fromSingle(foreignBlocks).toNumber();
114
+ await this.handlerAsTxe().advanceBlocksBy(blocks);
115
+ return toForeignCallResult([]);
116
+ }
117
+ // eslint-disable-next-line camelcase
118
+ aztec_txe_advanceTimestampBy(foreignDuration) {
119
+ const duration = fromSingle(foreignDuration).toBigInt();
120
+ this.handlerAsTxe().advanceTimestampBy(duration);
121
+ return toForeignCallResult([]);
122
+ }
123
+ // eslint-disable-next-line camelcase
124
+ async aztec_txe_deploy(artifact, instance, foreignSecret) {
125
+ const secret = fromSingle(foreignSecret);
126
+ await this.handlerAsTxe().deploy(artifact, instance, secret);
127
+ return toForeignCallResult([
128
+ toArray([
129
+ instance.salt,
130
+ instance.deployer.toField(),
131
+ instance.currentContractClassId,
132
+ instance.initializationHash,
133
+ ...instance.publicKeys.toFields()
134
+ ])
135
+ ]);
136
+ }
137
+ // eslint-disable-next-line camelcase
138
+ async aztec_txe_createAccount(foreignSecret) {
139
+ const secret = fromSingle(foreignSecret);
140
+ const completeAddress = await this.handlerAsTxe().createAccount(secret);
141
+ return toForeignCallResult([
142
+ toSingle(completeAddress.address),
143
+ ...completeAddress.publicKeys.toFields().map(toSingle)
144
+ ]);
145
+ }
146
+ // eslint-disable-next-line camelcase
147
+ async aztec_txe_addAccount(artifact, instance, foreignSecret) {
148
+ const secret = fromSingle(foreignSecret);
149
+ const completeAddress = await this.handlerAsTxe().addAccount(artifact, instance, secret);
150
+ return toForeignCallResult([
151
+ toSingle(completeAddress.address),
152
+ ...completeAddress.publicKeys.toFields().map(toSingle)
153
+ ]);
154
+ }
155
+ // eslint-disable-next-line camelcase
156
+ async aztec_txe_addAuthWitness(foreignAddress, foreignMessageHash) {
157
+ const address = addressFromSingle(foreignAddress);
158
+ const messageHash = fromSingle(foreignMessageHash);
159
+ await this.handlerAsTxe().addAuthWitness(address, messageHash);
160
+ return toForeignCallResult([]);
161
+ }
162
+ // PXE oracles
163
+ // eslint-disable-next-line camelcase
164
+ aztec_utl_assertCompatibleOracleVersion(foreignVersion) {
165
+ const version = fromSingle(foreignVersion).toNumber();
166
+ this.handlerAsMisc().assertCompatibleOracleVersion(version);
167
+ return toForeignCallResult([]);
168
+ }
169
+ // eslint-disable-next-line camelcase
170
+ aztec_utl_getRandomField() {
171
+ const randomField = this.handlerAsMisc().getRandomField();
172
+ return toForeignCallResult([
173
+ toSingle(randomField)
174
+ ]);
175
+ }
176
+ // eslint-disable-next-line camelcase
177
+ async aztec_txe_getLastBlockTimestamp() {
178
+ const timestamp = await this.handlerAsTxe().getLastBlockTimestamp();
179
+ return toForeignCallResult([
180
+ toSingle(new Fr(timestamp))
181
+ ]);
182
+ }
183
+ // eslint-disable-next-line camelcase
184
+ async aztec_txe_getLastTxEffects() {
185
+ const { txHash, noteHashes, nullifiers } = await this.handlerAsTxe().getLastTxEffects();
186
+ return toForeignCallResult([
187
+ toSingle(txHash.hash),
188
+ ...arrayToBoundedVec(toArray(noteHashes), MAX_NOTE_HASHES_PER_TX),
189
+ ...arrayToBoundedVec(toArray(nullifiers), MAX_NULLIFIERS_PER_TX)
190
+ ]);
191
+ }
192
+ // eslint-disable-next-line camelcase
193
+ async aztec_txe_getPrivateEvents(foreignSelector, foreignContractAddress, foreignScope) {
194
+ const selector = EventSelector.fromField(fromSingle(foreignSelector));
195
+ const contractAddress = addressFromSingle(foreignContractAddress);
196
+ const scope = addressFromSingle(foreignScope);
197
+ // TODO(F-335): Avoid doing the following 2 calls here.
198
+ {
199
+ await this.handlerAsTxe().syncContractNonOracleMethod(contractAddress, scope, this.stateHandler.getCurrentJob());
200
+ // We cycle job to commit the stores after the contract sync.
201
+ await this.stateHandler.cycleJob();
202
+ }
203
+ const events = await this.handlerAsTxe().getPrivateEvents(selector, contractAddress, scope);
204
+ if (events.length > MAX_PRIVATE_EVENTS_PER_TXE_QUERY) {
205
+ throw new Error(`Array of length ${events.length} larger than maxLen ${MAX_PRIVATE_EVENTS_PER_TXE_QUERY}`);
206
+ }
207
+ if (events.some((e)=>e.length > MAX_EVENT_LEN)) {
208
+ throw new Error(`Some private event has length larger than maxLen ${MAX_EVENT_LEN}`);
209
+ }
210
+ // This is a workaround as Noir does not currently let us return nested structs with arrays. We instead return a raw
211
+ // multidimensional array in get_private_events_oracle and create the BoundedVecs here.
212
+ 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();
213
+ const eventLengths = events.map((e)=>new Fr(e.length)).concat(Array(MAX_PRIVATE_EVENTS_PER_TXE_QUERY - events.length).fill(new Fr(0)));
214
+ const queryLength = new Fr(events.length);
215
+ return toForeignCallResult([
216
+ toArray(rawArrayStorage),
217
+ toArray(eventLengths),
218
+ toSingle(queryLength)
219
+ ]);
220
+ }
221
+ // eslint-disable-next-line camelcase
222
+ aztec_prv_storeInExecutionCache(foreignValues, foreignHash) {
223
+ const values = fromArray(foreignValues);
224
+ const hash = fromSingle(foreignHash);
225
+ this.handlerAsPrivate().storeInExecutionCache(values, hash);
226
+ return toForeignCallResult([]);
227
+ }
228
+ // eslint-disable-next-line camelcase
229
+ async aztec_prv_loadFromExecutionCache(foreignHash) {
230
+ const hash = fromSingle(foreignHash);
231
+ const returns = await this.handlerAsPrivate().loadFromExecutionCache(hash);
232
+ return toForeignCallResult([
233
+ toArray(returns)
234
+ ]);
235
+ }
236
+ // When the argument is a slice, noir automatically adds a length field to oracle call.
237
+ // When the argument is an array, we add the field length manually to the signature.
238
+ // eslint-disable-next-line camelcase
239
+ async aztec_utl_log(foreignLevel, foreignMessage, _foreignLength, foreignFields) {
240
+ const level = fromSingle(foreignLevel).toNumber();
241
+ const message = fromArray(foreignMessage).map((field)=>String.fromCharCode(field.toNumber())).join('');
242
+ const fields = fromArray(foreignFields);
243
+ await this.handlerAsMisc().log(level, message, fields);
244
+ return toForeignCallResult([]);
245
+ }
246
+ // eslint-disable-next-line camelcase
247
+ async aztec_utl_storageRead(foreignBlockHash, foreignContractAddress, foreignStartStorageSlot, foreignNumberOfElements) {
248
+ const blockHash = new BlockHash(fromSingle(foreignBlockHash));
249
+ const contractAddress = addressFromSingle(foreignContractAddress);
250
+ const startStorageSlot = fromSingle(foreignStartStorageSlot);
251
+ const numberOfElements = fromSingle(foreignNumberOfElements).toNumber();
252
+ const values = await this.handlerAsUtility().storageRead(blockHash, contractAddress, startStorageSlot, numberOfElements);
253
+ return toForeignCallResult([
254
+ toArray(values)
255
+ ]);
256
+ }
257
+ // eslint-disable-next-line camelcase
258
+ async aztec_utl_getPublicDataWitness(foreignBlockHash, foreignLeafSlot) {
259
+ const blockHash = new BlockHash(fromSingle(foreignBlockHash));
260
+ const leafSlot = fromSingle(foreignLeafSlot);
261
+ const witness = await this.handlerAsUtility().getPublicDataWitness(blockHash, leafSlot);
262
+ if (!witness) {
263
+ throw new Error(`Public data witness not found for slot ${leafSlot} at block ${blockHash.toString()}.`);
264
+ }
265
+ return toForeignCallResult(witness.toNoirRepresentation());
266
+ }
267
+ // eslint-disable-next-line camelcase
268
+ async aztec_utl_getNotes(foreignOwnerIsSome, foreignOwnerValue, foreignStorageSlot, foreignNumSelects, foreignSelectByIndexes, foreignSelectByOffsets, foreignSelectByLengths, foreignSelectValues, foreignSelectComparators, foreignSortByIndexes, foreignSortByOffsets, foreignSortByLengths, foreignSortOrder, foreignLimit, foreignOffset, foreignStatus, foreignMaxNotes, foreignPackedHintedNoteLength) {
269
+ // Parse Option<AztecAddress>: ownerIsSome is 0 for None, 1 for Some
270
+ const owner = fromSingle(foreignOwnerIsSome).toBool() ? AztecAddress.fromField(fromSingle(foreignOwnerValue)) : undefined;
271
+ const storageSlot = fromSingle(foreignStorageSlot);
272
+ const numSelects = fromSingle(foreignNumSelects).toNumber();
273
+ const selectByIndexes = fromArray(foreignSelectByIndexes).map((fr)=>fr.toNumber());
274
+ const selectByOffsets = fromArray(foreignSelectByOffsets).map((fr)=>fr.toNumber());
275
+ const selectByLengths = fromArray(foreignSelectByLengths).map((fr)=>fr.toNumber());
276
+ const selectValues = fromArray(foreignSelectValues);
277
+ const selectComparators = fromArray(foreignSelectComparators).map((fr)=>fr.toNumber());
278
+ const sortByIndexes = fromArray(foreignSortByIndexes).map((fr)=>fr.toNumber());
279
+ const sortByOffsets = fromArray(foreignSortByOffsets).map((fr)=>fr.toNumber());
280
+ const sortByLengths = fromArray(foreignSortByLengths).map((fr)=>fr.toNumber());
281
+ const sortOrder = fromArray(foreignSortOrder).map((fr)=>fr.toNumber());
282
+ const limit = fromSingle(foreignLimit).toNumber();
283
+ const offset = fromSingle(foreignOffset).toNumber();
284
+ const status = fromSingle(foreignStatus).toNumber();
285
+ const maxNotes = fromSingle(foreignMaxNotes).toNumber();
286
+ const packedHintedNoteLength = fromSingle(foreignPackedHintedNoteLength).toNumber();
287
+ const noteDatas = await this.handlerAsUtility().getNotes(owner, storageSlot, numSelects, selectByIndexes, selectByOffsets, selectByLengths, selectValues, selectComparators, sortByIndexes, sortByOffsets, sortByLengths, sortOrder, limit, offset, status);
288
+ const returnDataAsArrayOfArrays = noteDatas.map((noteData)=>packAsHintedNote({
289
+ contractAddress: noteData.contractAddress,
290
+ owner: noteData.owner,
291
+ randomness: noteData.randomness,
292
+ storageSlot: noteData.storageSlot,
293
+ noteNonce: noteData.noteNonce,
294
+ isPending: noteData.isPending,
295
+ note: noteData.note
296
+ }));
297
+ // Now we convert each sub-array to an array of ForeignCallSingles
298
+ const returnDataAsArrayOfForeignCallSingleArrays = returnDataAsArrayOfArrays.map((subArray)=>subArray.map(toSingle));
299
+ // At last we convert the array of arrays to a bounded vec of arrays
300
+ return toForeignCallResult(arrayOfArraysToBoundedVecOfArrays(returnDataAsArrayOfForeignCallSingleArrays, maxNotes, packedHintedNoteLength));
301
+ }
302
+ // eslint-disable-next-line camelcase
303
+ aztec_prv_notifyCreatedNote(foreignOwner, foreignStorageSlot, foreignRandomness, foreignNoteTypeId, foreignNote, foreignNoteHash, foreignCounter) {
304
+ const owner = addressFromSingle(foreignOwner);
305
+ const storageSlot = fromSingle(foreignStorageSlot);
306
+ const randomness = fromSingle(foreignRandomness);
307
+ const noteTypeId = NoteSelector.fromField(fromSingle(foreignNoteTypeId));
308
+ const note = fromArray(foreignNote);
309
+ const noteHash = fromSingle(foreignNoteHash);
310
+ const counter = fromSingle(foreignCounter).toNumber();
311
+ this.handlerAsPrivate().notifyCreatedNote(owner, storageSlot, randomness, noteTypeId, note, noteHash, counter);
312
+ return toForeignCallResult([]);
313
+ }
314
+ // eslint-disable-next-line camelcase
315
+ async aztec_prv_notifyNullifiedNote(foreignInnerNullifier, foreignNoteHash, foreignCounter) {
316
+ const innerNullifier = fromSingle(foreignInnerNullifier);
317
+ const noteHash = fromSingle(foreignNoteHash);
318
+ const counter = fromSingle(foreignCounter).toNumber();
319
+ await this.handlerAsPrivate().notifyNullifiedNote(innerNullifier, noteHash, counter);
320
+ return toForeignCallResult([]);
321
+ }
322
+ // eslint-disable-next-line camelcase
323
+ async aztec_prv_notifyCreatedNullifier(foreignInnerNullifier) {
324
+ const innerNullifier = fromSingle(foreignInnerNullifier);
325
+ await this.handlerAsPrivate().notifyCreatedNullifier(innerNullifier);
326
+ return toForeignCallResult([]);
327
+ }
328
+ // eslint-disable-next-line camelcase
329
+ async aztec_prv_isNullifierPending(foreignInnerNullifier, foreignContractAddress) {
330
+ const innerNullifier = fromSingle(foreignInnerNullifier);
331
+ const contractAddress = addressFromSingle(foreignContractAddress);
332
+ const isPending = await this.handlerAsPrivate().isNullifierPending(innerNullifier, contractAddress);
333
+ return toForeignCallResult([
334
+ toSingle(new Fr(isPending))
335
+ ]);
336
+ }
337
+ // eslint-disable-next-line camelcase
338
+ async aztec_utl_checkNullifierExists(foreignInnerNullifier) {
339
+ const innerNullifier = fromSingle(foreignInnerNullifier);
340
+ const exists = await this.handlerAsUtility().checkNullifierExists(innerNullifier);
341
+ return toForeignCallResult([
342
+ toSingle(new Fr(exists))
343
+ ]);
344
+ }
345
+ // eslint-disable-next-line camelcase
346
+ async aztec_utl_getContractInstance(foreignAddress) {
347
+ const address = addressFromSingle(foreignAddress);
348
+ const instance = await this.handlerAsUtility().getContractInstance(address);
349
+ return toForeignCallResult([
350
+ instance.salt,
351
+ instance.deployer.toField(),
352
+ instance.currentContractClassId,
353
+ instance.initializationHash,
354
+ ...instance.publicKeys.toFields()
355
+ ].map(toSingle));
356
+ }
357
+ // eslint-disable-next-line camelcase
358
+ async aztec_utl_tryGetPublicKeysAndPartialAddress(foreignAddress) {
359
+ const address = addressFromSingle(foreignAddress);
360
+ const result = await this.handlerAsUtility().tryGetPublicKeysAndPartialAddress(address);
361
+ // We are going to return a Noir Option struct to represent the possibility of null values. Options are a struct
362
+ // with two fields: `some` (a boolean) and `value` (a field array in this case).
363
+ if (result === undefined) {
364
+ // No data was found so we set `some` to 0 and pad `value` with zeros get the correct return size.
365
+ return toForeignCallResult([
366
+ toSingle(new Fr(0)),
367
+ toArray(Array(13).fill(new Fr(0)))
368
+ ]);
369
+ } else {
370
+ // Data was found so we set `some` to 1 and return it along with `value`.
371
+ return toForeignCallResult([
372
+ toSingle(new Fr(1)),
373
+ toArray([
374
+ ...result.publicKeys.toFields(),
375
+ result.partialAddress
376
+ ])
377
+ ]);
378
+ }
379
+ }
380
+ // eslint-disable-next-line camelcase
381
+ async aztec_utl_getKeyValidationRequest(foreignPkMHash) {
382
+ const pkMHash = fromSingle(foreignPkMHash);
383
+ const keyValidationRequest = await this.handlerAsUtility().getKeyValidationRequest(pkMHash);
384
+ return toForeignCallResult(keyValidationRequest.toFields().map(toSingle));
385
+ }
386
+ // eslint-disable-next-line camelcase
387
+ aztec_prv_callPrivateFunction(_foreignTargetContractAddress, _foreignFunctionSelector, _foreignArgsHash, _foreignSideEffectCounter, _foreignIsStaticCall) {
388
+ throw new Error('Contract calls are forbidden inside a `TestEnvironment::private_context`, use `private_call` instead');
389
+ }
390
+ // eslint-disable-next-line camelcase
391
+ async aztec_utl_getNullifierMembershipWitness(foreignBlockHash, foreignNullifier) {
392
+ const blockHash = new BlockHash(fromSingle(foreignBlockHash));
393
+ const nullifier = fromSingle(foreignNullifier);
394
+ const witness = await this.handlerAsUtility().getNullifierMembershipWitness(blockHash, nullifier);
395
+ if (!witness) {
396
+ throw new Error(`Nullifier membership witness not found at block ${blockHash}.`);
397
+ }
398
+ return toForeignCallResult(witness.toNoirRepresentation());
399
+ }
400
+ // eslint-disable-next-line camelcase
401
+ async aztec_utl_getAuthWitness(foreignMessageHash) {
402
+ const messageHash = fromSingle(foreignMessageHash);
403
+ const authWitness = await this.handlerAsUtility().getAuthWitness(messageHash);
404
+ if (!authWitness) {
405
+ throw new Error(`Auth witness not found for message hash ${messageHash}.`);
406
+ }
407
+ return toForeignCallResult([
408
+ toArray(authWitness)
409
+ ]);
410
+ }
411
+ // eslint-disable-next-line camelcase
412
+ aztec_prv_validatePublicCalldata(_foreignCalldataHash) {
413
+ throw new Error('Enqueueing public calls is not supported in TestEnvironment::private_context');
414
+ }
415
+ // eslint-disable-next-line camelcase
416
+ aztec_prv_notifyRevertiblePhaseStart(_foreignMinRevertibleSideEffectCounter) {
417
+ throw new Error('Enqueueing public calls is not supported in TestEnvironment::private_context');
418
+ }
419
+ // eslint-disable-next-line camelcase
420
+ async aztec_prv_inRevertiblePhase(foreignSideEffectCounter) {
421
+ const sideEffectCounter = fromSingle(foreignSideEffectCounter).toNumber();
422
+ const isRevertible = await this.handlerAsPrivate().inRevertiblePhase(sideEffectCounter);
423
+ return toForeignCallResult([
424
+ toSingle(new Fr(isRevertible))
425
+ ]);
426
+ }
427
+ // eslint-disable-next-line camelcase
428
+ aztec_utl_getUtilityContext() {
429
+ const context = this.handlerAsUtility().getUtilityContext();
430
+ return toForeignCallResult(context.toNoirRepresentation());
431
+ }
432
+ // eslint-disable-next-line camelcase
433
+ async aztec_utl_getBlockHeader(foreignBlockNumber) {
434
+ const blockNumber = BlockNumber(fromSingle(foreignBlockNumber).toNumber());
435
+ const header = await this.handlerAsUtility().getBlockHeader(blockNumber);
436
+ if (!header) {
437
+ throw new Error(`Block header not found for block ${blockNumber}.`);
438
+ }
439
+ return toForeignCallResult(header.toFields().map(toSingle));
440
+ }
441
+ // eslint-disable-next-line camelcase
442
+ async aztec_utl_getNoteHashMembershipWitness(foreignAnchorBlockHash, foreignNoteHash) {
443
+ const blockHash = new BlockHash(fromSingle(foreignAnchorBlockHash));
444
+ const noteHash = fromSingle(foreignNoteHash);
445
+ const witness = await this.handlerAsUtility().getNoteHashMembershipWitness(blockHash, noteHash);
446
+ if (!witness) {
447
+ throw new Error(`Note hash ${noteHash} not found in the note hash tree at block ${blockHash.toString()}.`);
448
+ }
449
+ return toForeignCallResult(witness.toNoirRepresentation());
450
+ }
451
+ // eslint-disable-next-line camelcase
452
+ async aztec_utl_getBlockHashMembershipWitness(foreignAnchorBlockHash, foreignBlockHash) {
453
+ const anchorBlockHash = new BlockHash(fromSingle(foreignAnchorBlockHash));
454
+ const blockHash = new BlockHash(fromSingle(foreignBlockHash));
455
+ const witness = await this.handlerAsUtility().getBlockHashMembershipWitness(anchorBlockHash, blockHash);
456
+ if (!witness) {
457
+ throw new Error(`Block hash ${blockHash.toString()} not found in the archive tree at anchor block ${anchorBlockHash.toString()}.`);
458
+ }
459
+ return toForeignCallResult(witness.toNoirRepresentation());
460
+ }
461
+ // eslint-disable-next-line camelcase
462
+ async aztec_utl_getLowNullifierMembershipWitness(foreignBlockHash, foreignNullifier) {
463
+ const blockHash = new BlockHash(fromSingle(foreignBlockHash));
464
+ const nullifier = fromSingle(foreignNullifier);
465
+ const witness = await this.handlerAsUtility().getLowNullifierMembershipWitness(blockHash, nullifier);
466
+ if (!witness) {
467
+ throw new Error(`Low nullifier witness not found for nullifier ${nullifier} at block ${blockHash}.`);
468
+ }
469
+ return toForeignCallResult(witness.toNoirRepresentation());
470
+ }
471
+ // eslint-disable-next-line camelcase
472
+ async aztec_utl_fetchTaggedLogs(foreignPendingTaggedLogArrayBaseSlot) {
473
+ const pendingTaggedLogArrayBaseSlot = fromSingle(foreignPendingTaggedLogArrayBaseSlot);
474
+ await this.handlerAsUtility().fetchTaggedLogs(pendingTaggedLogArrayBaseSlot);
475
+ return toForeignCallResult([]);
476
+ }
477
+ // eslint-disable-next-line camelcase
478
+ async aztec_utl_validateAndStoreEnqueuedNotesAndEvents(foreignContractAddress, foreignNoteValidationRequestsArrayBaseSlot, foreignEventValidationRequestsArrayBaseSlot, foreignMaxNotePackedLen, foreignMaxEventSerializedLen) {
479
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
480
+ const noteValidationRequestsArrayBaseSlot = fromSingle(foreignNoteValidationRequestsArrayBaseSlot);
481
+ const eventValidationRequestsArrayBaseSlot = fromSingle(foreignEventValidationRequestsArrayBaseSlot);
482
+ const maxNotePackedLen = fromSingle(foreignMaxNotePackedLen).toNumber();
483
+ const maxEventSerializedLen = fromSingle(foreignMaxEventSerializedLen).toNumber();
484
+ await this.handlerAsUtility().validateAndStoreEnqueuedNotesAndEvents(contractAddress, noteValidationRequestsArrayBaseSlot, eventValidationRequestsArrayBaseSlot, maxNotePackedLen, maxEventSerializedLen);
485
+ return toForeignCallResult([]);
486
+ }
487
+ // eslint-disable-next-line camelcase
488
+ async aztec_utl_bulkRetrieveLogs(foreignContractAddress, foreignLogRetrievalRequestsArrayBaseSlot, foreignLogRetrievalResponsesArrayBaseSlot) {
489
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
490
+ const logRetrievalRequestsArrayBaseSlot = fromSingle(foreignLogRetrievalRequestsArrayBaseSlot);
491
+ const logRetrievalResponsesArrayBaseSlot = fromSingle(foreignLogRetrievalResponsesArrayBaseSlot);
492
+ await this.handlerAsUtility().bulkRetrieveLogs(contractAddress, logRetrievalRequestsArrayBaseSlot, logRetrievalResponsesArrayBaseSlot);
493
+ return toForeignCallResult([]);
494
+ }
495
+ // eslint-disable-next-line camelcase
496
+ async aztec_utl_utilityResolveMessageContexts(foreignContractAddress, foreignMessageContextRequestsArrayBaseSlot, foreignMessageContextResponsesArrayBaseSlot) {
497
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
498
+ const messageContextRequestsArrayBaseSlot = fromSingle(foreignMessageContextRequestsArrayBaseSlot);
499
+ const messageContextResponsesArrayBaseSlot = fromSingle(foreignMessageContextResponsesArrayBaseSlot);
500
+ await this.handlerAsUtility().utilityResolveMessageContexts(contractAddress, messageContextRequestsArrayBaseSlot, messageContextResponsesArrayBaseSlot);
501
+ return toForeignCallResult([]);
502
+ }
503
+ // eslint-disable-next-line camelcase
504
+ async aztec_utl_storeCapsule(foreignContractAddress, foreignSlot, foreignCapsule) {
505
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
506
+ const slot = fromSingle(foreignSlot);
507
+ const capsule = fromArray(foreignCapsule);
508
+ await this.handlerAsUtility().storeCapsule(contractAddress, slot, capsule);
509
+ return toForeignCallResult([]);
510
+ }
511
+ // eslint-disable-next-line camelcase
512
+ async aztec_utl_loadCapsule(foreignContractAddress, foreignSlot, foreignTSize) {
513
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
514
+ const slot = fromSingle(foreignSlot);
515
+ const tSize = fromSingle(foreignTSize).toNumber();
516
+ const values = await this.handlerAsUtility().loadCapsule(contractAddress, slot);
517
+ // We are going to return a Noir Option struct to represent the possibility of null values. Options are a struct
518
+ // with two fields: `some` (a boolean) and `value` (a field array in this case).
519
+ if (values === null) {
520
+ // No data was found so we set `some` to 0 and pad `value` with zeros get the correct return size.
521
+ return toForeignCallResult([
522
+ toSingle(new Fr(0)),
523
+ toArray(Array(tSize).fill(new Fr(0)))
524
+ ]);
525
+ } else {
526
+ // Data was found so we set `some` to 1 and return it along with `value`.
527
+ return toForeignCallResult([
528
+ toSingle(new Fr(1)),
529
+ toArray(values)
530
+ ]);
531
+ }
532
+ }
533
+ // eslint-disable-next-line camelcase
534
+ async aztec_utl_deleteCapsule(foreignContractAddress, foreignSlot) {
535
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
536
+ const slot = fromSingle(foreignSlot);
537
+ await this.handlerAsUtility().deleteCapsule(contractAddress, slot);
538
+ return toForeignCallResult([]);
539
+ }
540
+ // eslint-disable-next-line camelcase
541
+ async aztec_utl_copyCapsule(foreignContractAddress, foreignSrcSlot, foreignDstSlot, foreignNumEntries) {
542
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
543
+ const srcSlot = fromSingle(foreignSrcSlot);
544
+ const dstSlot = fromSingle(foreignDstSlot);
545
+ const numEntries = fromSingle(foreignNumEntries).toNumber();
546
+ await this.handlerAsUtility().copyCapsule(contractAddress, srcSlot, dstSlot, numEntries);
547
+ return toForeignCallResult([]);
548
+ }
549
+ // TODO: I forgot to add a corresponding function here, when I introduced an oracle method to txe_oracle.ts.
550
+ // 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
551
+ // to implement this function here. Isn't there a way to programmatically identify that this is missing, given the
552
+ // existence of a txe_oracle method?
553
+ // TODO(F-452): Return Option and wrap in try/catch so BB exceptions don't crash TXE.
554
+ // eslint-disable-next-line camelcase
555
+ async aztec_utl_aes128Decrypt(foreignCiphertextBVecStorage, foreignCiphertextLength, foreignIv, foreignSymKey) {
556
+ const ciphertext = fromUintBoundedVec(foreignCiphertextBVecStorage, foreignCiphertextLength, 8);
557
+ const iv = fromUintArray(foreignIv, 8);
558
+ const symKey = fromUintArray(foreignSymKey, 8);
559
+ const plaintextBuffer = await this.handlerAsUtility().aes128Decrypt(ciphertext, iv, symKey);
560
+ return toForeignCallResult(arrayToBoundedVec(bufferToU8Array(plaintextBuffer), foreignCiphertextBVecStorage.length));
561
+ }
562
+ // eslint-disable-next-line camelcase
563
+ async aztec_utl_getSharedSecret(foreignAddress, foreignEphPKField0, foreignEphPKField1, foreignEphPKField2) {
564
+ const address = AztecAddress.fromField(fromSingle(foreignAddress));
565
+ const ephPK = Point.fromFields([
566
+ fromSingle(foreignEphPKField0),
567
+ fromSingle(foreignEphPKField1),
568
+ fromSingle(foreignEphPKField2)
569
+ ]);
570
+ const secret = await this.handlerAsUtility().getSharedSecret(address, ephPK);
571
+ return toForeignCallResult(secret.toFields().map(toSingle));
572
+ }
573
+ // eslint-disable-next-line camelcase
574
+ aztec_utl_emitOffchainEffect(_foreignData) {
575
+ throw new Error('Offchain effects are not yet supported in the TestEnvironment');
576
+ }
577
+ // AVM opcodes
578
+ // eslint-disable-next-line camelcase
579
+ aztec_avm_emitPublicLog(_foreignMessage) {
580
+ // TODO(#8811): Implement
581
+ return toForeignCallResult([]);
582
+ }
583
+ // eslint-disable-next-line camelcase
584
+ async aztec_avm_storageRead(foreignSlot, foreignContractAddress) {
585
+ const slot = fromSingle(foreignSlot);
586
+ const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
587
+ const value = (await this.handlerAsAvm().storageRead(slot, contractAddress)).value;
588
+ return toForeignCallResult([
589
+ toSingle(new Fr(value))
590
+ ]);
591
+ }
592
+ // eslint-disable-next-line camelcase
593
+ async aztec_avm_storageWrite(foreignSlot, foreignValue) {
594
+ const slot = fromSingle(foreignSlot);
595
+ const value = fromSingle(foreignValue);
596
+ await this.handlerAsAvm().storageWrite(slot, value);
597
+ return toForeignCallResult([]);
598
+ }
599
+ // eslint-disable-next-line camelcase
600
+ async aztec_avm_getContractInstanceDeployer(foreignAddress) {
601
+ const address = addressFromSingle(foreignAddress);
602
+ const instance = await this.handlerAsUtility().getContractInstance(address);
603
+ return toForeignCallResult([
604
+ toSingle(instance.deployer),
605
+ // AVM requires an extra boolean indicating the instance was found
606
+ toSingle(new Fr(1))
607
+ ]);
608
+ }
609
+ // eslint-disable-next-line camelcase
610
+ async aztec_avm_getContractInstanceClassId(foreignAddress) {
611
+ const address = addressFromSingle(foreignAddress);
612
+ const instance = await this.handlerAsUtility().getContractInstance(address);
613
+ return toForeignCallResult([
614
+ toSingle(instance.currentContractClassId),
615
+ // AVM requires an extra boolean indicating the instance was found
616
+ toSingle(new Fr(1))
617
+ ]);
618
+ }
619
+ // eslint-disable-next-line camelcase
620
+ async aztec_avm_getContractInstanceInitializationHash(foreignAddress) {
621
+ const address = addressFromSingle(foreignAddress);
622
+ const instance = await this.handlerAsUtility().getContractInstance(address);
623
+ return toForeignCallResult([
624
+ toSingle(instance.initializationHash),
625
+ // AVM requires an extra boolean indicating the instance was found
626
+ toSingle(new Fr(1))
627
+ ]);
628
+ }
629
+ // eslint-disable-next-line camelcase
630
+ async aztec_avm_sender() {
631
+ const sender = await this.handlerAsAvm().sender();
632
+ return toForeignCallResult([
633
+ toSingle(sender)
634
+ ]);
635
+ }
636
+ // eslint-disable-next-line camelcase
637
+ async aztec_avm_emitNullifier(foreignNullifier) {
638
+ const nullifier = fromSingle(foreignNullifier);
639
+ await this.handlerAsAvm().emitNullifier(nullifier);
640
+ return toForeignCallResult([]);
641
+ }
642
+ // eslint-disable-next-line camelcase
643
+ async aztec_avm_emitNoteHash(foreignNoteHash) {
644
+ const noteHash = fromSingle(foreignNoteHash);
645
+ await this.handlerAsAvm().emitNoteHash(noteHash);
646
+ return toForeignCallResult([]);
647
+ }
648
+ // eslint-disable-next-line camelcase
649
+ async aztec_avm_nullifierExists(foreignSiloedNullifier) {
650
+ const siloedNullifier = fromSingle(foreignSiloedNullifier);
651
+ const exists = await this.handlerAsAvm().nullifierExists(siloedNullifier);
652
+ return toForeignCallResult([
653
+ toSingle(new Fr(exists))
654
+ ]);
655
+ }
656
+ // eslint-disable-next-line camelcase
657
+ async aztec_avm_address() {
658
+ const contractAddress = await this.handlerAsAvm().address();
659
+ return toForeignCallResult([
660
+ toSingle(contractAddress.toField())
661
+ ]);
662
+ }
663
+ // eslint-disable-next-line camelcase
664
+ async aztec_avm_blockNumber() {
665
+ const blockNumber = await this.handlerAsAvm().blockNumber();
666
+ return toForeignCallResult([
667
+ toSingle(new Fr(blockNumber))
668
+ ]);
669
+ }
670
+ // eslint-disable-next-line camelcase
671
+ async aztec_avm_timestamp() {
672
+ const timestamp = await this.handlerAsAvm().timestamp();
673
+ return toForeignCallResult([
674
+ toSingle(new Fr(timestamp))
675
+ ]);
676
+ }
677
+ // eslint-disable-next-line camelcase
678
+ async aztec_avm_isStaticCall() {
679
+ const isStaticCall = await this.handlerAsAvm().isStaticCall();
680
+ return toForeignCallResult([
681
+ toSingle(new Fr(isStaticCall ? 1 : 0))
682
+ ]);
683
+ }
684
+ // eslint-disable-next-line camelcase
685
+ async aztec_avm_chainId() {
686
+ const chainId = await this.handlerAsAvm().chainId();
687
+ return toForeignCallResult([
688
+ toSingle(chainId)
689
+ ]);
690
+ }
691
+ // eslint-disable-next-line camelcase
692
+ async aztec_avm_version() {
693
+ const version = await this.handlerAsAvm().version();
694
+ return toForeignCallResult([
695
+ toSingle(version)
696
+ ]);
697
+ }
698
+ // eslint-disable-next-line camelcase
699
+ aztec_avm_returndataSize() {
700
+ throw new Error('Contract calls are forbidden inside a `TestEnvironment::public_context`, use `public_call` instead');
701
+ }
702
+ // eslint-disable-next-line camelcase
703
+ aztec_avm_returndataCopy(_foreignRdOffset, _foreignCopySize) {
704
+ throw new Error('Contract calls are forbidden inside a `TestEnvironment::public_context`, use `public_call` instead');
705
+ }
706
+ // eslint-disable-next-line camelcase
707
+ aztec_avm_call(_foreignL2Gas, _foreignDaGas, _foreignAddress, _foreignLength, _foreignArgs) {
708
+ throw new Error('Contract calls are forbidden inside a `TestEnvironment::public_context`, use `public_call` instead');
709
+ }
710
+ // eslint-disable-next-line camelcase
711
+ aztec_avm_staticCall(_foreignL2Gas, _foreignDaGas, _foreignAddress, _foreignLength, _foreignArgs) {
712
+ throw new Error('Contract calls are forbidden inside a `TestEnvironment::public_context`, use `public_call` instead');
713
+ }
714
+ // eslint-disable-next-line camelcase
715
+ aztec_avm_successCopy() {
716
+ throw new Error('Contract calls are forbidden inside a `TestEnvironment::public_context`, use `public_call` instead');
717
+ }
718
+ // eslint-disable-next-line camelcase
719
+ async aztec_txe_privateCallNewFlow(foreignFrom, foreignTargetContractAddress, foreignFunctionSelector, foreignArgs, foreignArgsHash, foreignIsStaticCall) {
720
+ const from = addressFromSingle(foreignFrom);
721
+ const targetContractAddress = addressFromSingle(foreignTargetContractAddress);
722
+ const functionSelector = FunctionSelector.fromField(fromSingle(foreignFunctionSelector));
723
+ const args = fromArray(foreignArgs);
724
+ const argsHash = fromSingle(foreignArgsHash);
725
+ const isStaticCall = fromSingle(foreignIsStaticCall).toBool();
726
+ const returnValues = await this.handlerAsTxe().privateCallNewFlow(from, targetContractAddress, functionSelector, args, argsHash, isStaticCall, this.stateHandler.getCurrentJob());
727
+ // TODO(F-335): Avoid doing the following call here.
728
+ await this.stateHandler.cycleJob();
729
+ return toForeignCallResult([
730
+ toArray(returnValues)
731
+ ]);
732
+ }
733
+ // eslint-disable-next-line camelcase
734
+ async aztec_txe_executeUtilityFunction(foreignTargetContractAddress, foreignFunctionSelector, foreignArgs) {
735
+ const targetContractAddress = addressFromSingle(foreignTargetContractAddress);
736
+ const functionSelector = FunctionSelector.fromField(fromSingle(foreignFunctionSelector));
737
+ const args = fromArray(foreignArgs);
738
+ const returnValues = await this.handlerAsTxe().executeUtilityFunction(targetContractAddress, functionSelector, args, this.stateHandler.getCurrentJob());
739
+ // TODO(F-335): Avoid doing the following call here.
740
+ await this.stateHandler.cycleJob();
741
+ return toForeignCallResult([
742
+ toArray(returnValues)
743
+ ]);
744
+ }
745
+ // eslint-disable-next-line camelcase
746
+ async aztec_txe_publicCallNewFlow(foreignFrom, foreignAddress, foreignCalldata, foreignIsStaticCall) {
747
+ const from = addressFromSingle(foreignFrom);
748
+ const address = addressFromSingle(foreignAddress);
749
+ const calldata = fromArray(foreignCalldata);
750
+ const isStaticCall = fromSingle(foreignIsStaticCall).toBool();
751
+ const returnValues = await this.handlerAsTxe().publicCallNewFlow(from, address, calldata, isStaticCall);
752
+ // TODO(F-335): Avoid doing the following call here.
753
+ await this.stateHandler.cycleJob();
754
+ return toForeignCallResult([
755
+ toArray(returnValues)
756
+ ]);
757
+ }
758
+ // eslint-disable-next-line camelcase
759
+ async aztec_prv_getSenderForTags() {
760
+ const sender = await this.handlerAsPrivate().getSenderForTags();
761
+ // Return a Noir Option struct with `some` and `value` fields
762
+ if (sender === undefined) {
763
+ // No sender found, return Option with some=0 and value=0
764
+ return toForeignCallResult([
765
+ toSingle(0),
766
+ toSingle(0)
767
+ ]);
768
+ } else {
769
+ // Sender found, return Option with some=1 and value=sender address
770
+ return toForeignCallResult([
771
+ toSingle(1),
772
+ toSingle(sender)
773
+ ]);
774
+ }
775
+ }
776
+ // eslint-disable-next-line camelcase
777
+ async aztec_prv_setSenderForTags(foreignSenderForTags) {
778
+ const senderForTags = AztecAddress.fromField(fromSingle(foreignSenderForTags));
779
+ await this.handlerAsPrivate().setSenderForTags(senderForTags);
780
+ return toForeignCallResult([]);
781
+ }
782
+ // eslint-disable-next-line camelcase
783
+ async aztec_prv_getNextAppTagAsSender(foreignSender, foreignRecipient) {
784
+ const sender = AztecAddress.fromField(fromSingle(foreignSender));
785
+ const recipient = AztecAddress.fromField(fromSingle(foreignRecipient));
786
+ const nextAppTag = await this.handlerAsPrivate().getNextAppTagAsSender(sender, recipient);
787
+ return toForeignCallResult([
788
+ toSingle(nextAppTag.value)
789
+ ]);
790
+ }
791
+ }