@aztec/wallet-sdk 3.0.3 → 4.0.0-devnet.1-patch.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +229 -332
- package/dest/base-wallet/base_wallet.d.ts +52 -12
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +108 -26
- package/dest/base-wallet/index.d.ts +2 -1
- package/dest/base-wallet/index.d.ts.map +1 -1
- package/dest/base-wallet/index.js +1 -0
- package/dest/base-wallet/utils.d.ts +48 -0
- package/dest/base-wallet/utils.d.ts.map +1 -0
- package/dest/base-wallet/utils.js +128 -0
- package/dest/crypto.d.ts +192 -0
- package/dest/crypto.d.ts.map +1 -0
- package/dest/crypto.js +394 -0
- package/dest/emoji_alphabet.d.ts +35 -0
- package/dest/emoji_alphabet.d.ts.map +1 -0
- package/dest/emoji_alphabet.js +299 -0
- package/dest/extension/handlers/background_connection_handler.d.ts +158 -0
- package/dest/extension/handlers/background_connection_handler.d.ts.map +1 -0
- package/dest/extension/handlers/background_connection_handler.js +258 -0
- package/dest/extension/handlers/content_script_connection_handler.d.ts +56 -0
- package/dest/extension/handlers/content_script_connection_handler.d.ts.map +1 -0
- package/dest/extension/handlers/content_script_connection_handler.js +174 -0
- package/dest/extension/handlers/index.d.ts +12 -0
- package/dest/extension/handlers/index.d.ts.map +1 -0
- package/dest/extension/handlers/index.js +10 -0
- package/dest/extension/handlers/internal_message_types.d.ts +63 -0
- package/dest/extension/handlers/internal_message_types.d.ts.map +1 -0
- package/dest/extension/handlers/internal_message_types.js +22 -0
- package/dest/extension/provider/extension_provider.d.ts +107 -0
- package/dest/extension/provider/extension_provider.d.ts.map +1 -0
- package/dest/extension/provider/extension_provider.js +160 -0
- package/dest/extension/provider/extension_wallet.d.ts +132 -0
- package/dest/extension/provider/extension_wallet.d.ts.map +1 -0
- package/dest/extension/provider/extension_wallet.js +278 -0
- package/dest/extension/provider/index.d.ts +3 -0
- package/dest/extension/provider/index.d.ts.map +1 -0
- package/dest/manager/index.d.ts +2 -7
- package/dest/manager/index.d.ts.map +1 -1
- package/dest/manager/index.js +0 -4
- package/dest/manager/types.d.ts +108 -5
- package/dest/manager/types.d.ts.map +1 -1
- package/dest/manager/types.js +17 -1
- package/dest/manager/wallet_manager.d.ts +50 -7
- package/dest/manager/wallet_manager.d.ts.map +1 -1
- package/dest/manager/wallet_manager.js +178 -29
- package/dest/types.d.ts +123 -0
- package/dest/types.d.ts.map +1 -0
- package/dest/types.js +11 -0
- package/package.json +20 -11
- package/src/base-wallet/base_wallet.ts +153 -45
- package/src/base-wallet/index.ts +1 -0
- package/src/base-wallet/utils.ts +230 -0
- package/src/crypto.ts +499 -0
- package/src/emoji_alphabet.ts +317 -0
- package/src/extension/handlers/background_connection_handler.ts +423 -0
- package/src/extension/handlers/content_script_connection_handler.ts +246 -0
- package/src/extension/handlers/index.ts +25 -0
- package/src/extension/handlers/internal_message_types.ts +69 -0
- package/src/extension/provider/extension_provider.ts +233 -0
- package/src/extension/provider/extension_wallet.ts +329 -0
- package/src/extension/provider/index.ts +7 -0
- package/src/manager/index.ts +3 -15
- package/src/manager/types.ts +112 -4
- package/src/manager/wallet_manager.ts +202 -31
- package/src/types.ts +132 -0
- package/dest/providers/extension/extension_provider.d.ts +0 -17
- package/dest/providers/extension/extension_provider.d.ts.map +0 -1
- package/dest/providers/extension/extension_provider.js +0 -56
- package/dest/providers/extension/extension_wallet.d.ts +0 -23
- package/dest/providers/extension/extension_wallet.d.ts.map +0 -1
- package/dest/providers/extension/extension_wallet.js +0 -96
- package/dest/providers/extension/index.d.ts +0 -4
- package/dest/providers/extension/index.d.ts.map +0 -1
- package/dest/providers/types.d.ts +0 -67
- package/dest/providers/types.d.ts.map +0 -1
- package/dest/providers/types.js +0 -3
- package/src/providers/extension/extension_provider.ts +0 -72
- package/src/providers/extension/extension_wallet.ts +0 -124
- package/src/providers/extension/index.ts +0 -3
- package/src/providers/types.ts +0 -71
- /package/dest/{providers/extension → extension/provider}/index.js +0 -0
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import type { Account } from '@aztec/aztec.js/account';
|
|
2
2
|
import type { CallIntent, IntentInnerHash } from '@aztec/aztec.js/authorization';
|
|
3
|
+
import { type InteractionWaitOptions, NO_WAIT, type SendReturn } from '@aztec/aztec.js/contracts';
|
|
3
4
|
import type { FeePaymentMethod } from '@aztec/aztec.js/fee';
|
|
5
|
+
import { waitForTx } from '@aztec/aztec.js/node';
|
|
4
6
|
import type {
|
|
5
7
|
Aliased,
|
|
8
|
+
AppCapabilities,
|
|
6
9
|
BatchResults,
|
|
7
|
-
BatchableMethods,
|
|
8
10
|
BatchedMethod,
|
|
9
11
|
PrivateEvent,
|
|
10
12
|
PrivateEventFilter,
|
|
@@ -12,6 +14,7 @@ import type {
|
|
|
12
14
|
SendOptions,
|
|
13
15
|
SimulateOptions,
|
|
14
16
|
Wallet,
|
|
17
|
+
WalletCapabilities,
|
|
15
18
|
} from '@aztec/aztec.js/wallet';
|
|
16
19
|
import {
|
|
17
20
|
GAS_ESTIMATION_DA_GAS_LIMIT,
|
|
@@ -34,27 +37,27 @@ import {
|
|
|
34
37
|
import type { AuthWitness } from '@aztec/stdlib/auth-witness';
|
|
35
38
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
36
39
|
import {
|
|
37
|
-
type ContractClassMetadata,
|
|
38
40
|
type ContractInstanceWithAddress,
|
|
39
|
-
type ContractMetadata,
|
|
40
41
|
computePartialAddress,
|
|
41
42
|
getContractClassFromArtifact,
|
|
42
43
|
} from '@aztec/stdlib/contract';
|
|
43
44
|
import { SimulationError } from '@aztec/stdlib/errors';
|
|
44
45
|
import { Gas, GasSettings } from '@aztec/stdlib/gas';
|
|
46
|
+
import { siloNullifier } from '@aztec/stdlib/hash';
|
|
45
47
|
import type { AztecNode } from '@aztec/stdlib/interfaces/client';
|
|
46
|
-
import
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
TxProfileResult,
|
|
50
|
-
TxReceipt,
|
|
48
|
+
import {
|
|
49
|
+
BlockHeader,
|
|
50
|
+
type TxExecutionRequest,
|
|
51
|
+
type TxProfileResult,
|
|
51
52
|
TxSimulationResult,
|
|
52
|
-
UtilitySimulationResult,
|
|
53
|
+
type UtilitySimulationResult,
|
|
53
54
|
} from '@aztec/stdlib/tx';
|
|
54
55
|
import { ExecutionPayload, mergeExecutionPayloads } from '@aztec/stdlib/tx';
|
|
55
56
|
|
|
56
57
|
import { inspect } from 'util';
|
|
57
58
|
|
|
59
|
+
import { buildMergedSimulationResult, extractOptimizablePublicStaticCalls, simulateViaNode } from './utils.js';
|
|
60
|
+
|
|
58
61
|
/**
|
|
59
62
|
* Options to configure fee payment for a transaction
|
|
60
63
|
*/
|
|
@@ -74,15 +77,14 @@ export type FeeOptions = {
|
|
|
74
77
|
* A base class for Wallet implementations
|
|
75
78
|
*/
|
|
76
79
|
export abstract class BaseWallet implements Wallet {
|
|
77
|
-
protected
|
|
78
|
-
|
|
79
|
-
protected baseFeePadding = 0.5;
|
|
80
|
+
protected minFeePadding = 0.5;
|
|
80
81
|
protected cancellableTransactions = false;
|
|
81
82
|
|
|
82
83
|
// Protected because we want to force wallets to instantiate their own PXE.
|
|
83
84
|
protected constructor(
|
|
84
85
|
protected readonly pxe: PXE,
|
|
85
86
|
protected readonly aztecNode: AztecNode,
|
|
87
|
+
protected log = createLogger('wallet-sdk:base_wallet'),
|
|
86
88
|
) {}
|
|
87
89
|
|
|
88
90
|
protected abstract getAccountFromAddress(address: AztecAddress): Promise<Account>;
|
|
@@ -121,20 +123,42 @@ export abstract class BaseWallet implements Wallet {
|
|
|
121
123
|
? mergeExecutionPayloads([feeExecutionPayload, executionPayload])
|
|
122
124
|
: executionPayload;
|
|
123
125
|
const fromAccount = await this.getAccountFromAddress(from);
|
|
124
|
-
|
|
126
|
+
const chainInfo = await this.getChainInfo();
|
|
127
|
+
return fromAccount.createTxExecutionRequest(
|
|
128
|
+
finalExecutionPayload,
|
|
129
|
+
feeOptions.gasSettings,
|
|
130
|
+
chainInfo,
|
|
131
|
+
executionOptions,
|
|
132
|
+
);
|
|
125
133
|
}
|
|
126
134
|
|
|
127
135
|
public async createAuthWit(
|
|
128
136
|
from: AztecAddress,
|
|
129
|
-
messageHashOrIntent:
|
|
137
|
+
messageHashOrIntent: IntentInnerHash | CallIntent,
|
|
130
138
|
): Promise<AuthWitness> {
|
|
131
139
|
const account = await this.getAccountFromAddress(from);
|
|
132
|
-
|
|
140
|
+
const chainInfo = await this.getChainInfo();
|
|
141
|
+
return account.createAuthWit(messageHashOrIntent, chainInfo);
|
|
133
142
|
}
|
|
134
143
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
144
|
+
/**
|
|
145
|
+
* Request capabilities from the wallet.
|
|
146
|
+
*
|
|
147
|
+
* This method is wallet-implementation-dependent and must be provided by classes extending BaseWallet.
|
|
148
|
+
* Embedded wallets typically don't support capability-based authorization (no user authorization flow),
|
|
149
|
+
* while external wallets (browser extensions, hardware wallets) implement this to reduce authorization
|
|
150
|
+
* friction by allowing apps to request permissions upfront.
|
|
151
|
+
*
|
|
152
|
+
* TODO: Consider making it abstract so implementing it is a conscious decision. Leaving it as-is
|
|
153
|
+
* while the feature stabilizes.
|
|
154
|
+
*
|
|
155
|
+
* @param _manifest - Application capability manifest declaring what operations the app needs
|
|
156
|
+
*/
|
|
157
|
+
public requestCapabilities(_manifest: AppCapabilities): Promise<WalletCapabilities> {
|
|
158
|
+
throw new Error('Not implemented');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
public async batch<const T extends readonly BatchedMethod[]>(methods: T): Promise<BatchResults<T>> {
|
|
138
162
|
const results: any[] = [];
|
|
139
163
|
for (const method of methods) {
|
|
140
164
|
const { name, args } = method;
|
|
@@ -165,7 +189,7 @@ export abstract class BaseWallet implements Wallet {
|
|
|
165
189
|
gasSettings?: Partial<FieldsOf<GasSettings>>,
|
|
166
190
|
): Promise<FeeOptions> {
|
|
167
191
|
const maxFeesPerGas =
|
|
168
|
-
gasSettings?.maxFeesPerGas ?? (await this.aztecNode.
|
|
192
|
+
gasSettings?.maxFeesPerGas ?? (await this.aztecNode.getCurrentMinFees()).mul(1 + this.minFeePadding);
|
|
169
193
|
let accountFeePaymentMethodOptions;
|
|
170
194
|
// The transaction does not include a fee payment method, so we set the flag
|
|
171
195
|
// for the account to use its fee juice balance
|
|
@@ -227,7 +251,7 @@ export abstract class BaseWallet implements Wallet {
|
|
|
227
251
|
artifact?: ContractArtifact,
|
|
228
252
|
secretKey?: Fr,
|
|
229
253
|
): Promise<ContractInstanceWithAddress> {
|
|
230
|
-
const
|
|
254
|
+
const existingInstance = await this.pxe.getContractInstance(instance.address);
|
|
231
255
|
|
|
232
256
|
if (existingInstance) {
|
|
233
257
|
// Instance already registered in the wallet
|
|
@@ -244,13 +268,12 @@ export abstract class BaseWallet implements Wallet {
|
|
|
244
268
|
// Instance not registered yet
|
|
245
269
|
if (!artifact) {
|
|
246
270
|
// Try to get the artifact from the wallet's contract class storage
|
|
247
|
-
|
|
248
|
-
if (!
|
|
271
|
+
artifact = await this.pxe.getContractArtifact(instance.currentContractClassId);
|
|
272
|
+
if (!artifact) {
|
|
249
273
|
throw new Error(
|
|
250
274
|
`Cannot register contract at ${instance.address.toString()}: artifact is required but not provided, and wallet does not have the artifact for contract class ${instance.currentContractClassId.toString()}`,
|
|
251
275
|
);
|
|
252
276
|
}
|
|
253
|
-
artifact = classMetadata.artifact;
|
|
254
277
|
}
|
|
255
278
|
await this.pxe.registerContract({ artifact, instance });
|
|
256
279
|
}
|
|
@@ -261,26 +284,89 @@ export abstract class BaseWallet implements Wallet {
|
|
|
261
284
|
return instance;
|
|
262
285
|
}
|
|
263
286
|
|
|
287
|
+
/**
|
|
288
|
+
* Simulates calls through the standard PXE path (account entrypoint).
|
|
289
|
+
* @param executionPayload - The execution payload to simulate.
|
|
290
|
+
* @param from - The sender address.
|
|
291
|
+
* @param feeOptions - Fee options for the transaction.
|
|
292
|
+
* @param skipTxValidation - Whether to skip tx validation.
|
|
293
|
+
* @param skipFeeEnforcement - Whether to skip fee enforcement.
|
|
294
|
+
*/
|
|
295
|
+
protected async simulateViaEntrypoint(
|
|
296
|
+
executionPayload: ExecutionPayload,
|
|
297
|
+
from: AztecAddress,
|
|
298
|
+
feeOptions: FeeOptions,
|
|
299
|
+
skipTxValidation?: boolean,
|
|
300
|
+
skipFeeEnforcement?: boolean,
|
|
301
|
+
) {
|
|
302
|
+
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, from, feeOptions);
|
|
303
|
+
return this.pxe.simulateTx(txRequest, { simulatePublic: true, skipTxValidation, skipFeeEnforcement });
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Simulates a transaction, optimizing leading public static calls by running them directly
|
|
308
|
+
* on the node while sending the remaining calls through the standard PXE path.
|
|
309
|
+
* Return values from both paths are merged back in original call order.
|
|
310
|
+
* @param executionPayload - The execution payload to simulate.
|
|
311
|
+
* @param opts - Simulation options (from address, fee settings, etc.).
|
|
312
|
+
* @returns The merged simulation result.
|
|
313
|
+
*/
|
|
264
314
|
async simulateTx(executionPayload: ExecutionPayload, opts: SimulateOptions): Promise<TxSimulationResult> {
|
|
265
315
|
const feeOptions = opts.fee?.estimateGas
|
|
266
316
|
? await this.completeFeeOptionsForEstimation(opts.from, executionPayload.feePayer, opts.fee?.gasSettings)
|
|
267
317
|
: await this.completeFeeOptions(opts.from, executionPayload.feePayer, opts.fee?.gasSettings);
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
318
|
+
const { optimizableCalls, remainingCalls } = extractOptimizablePublicStaticCalls(executionPayload);
|
|
319
|
+
const remainingPayload = { ...executionPayload, calls: remainingCalls };
|
|
320
|
+
|
|
321
|
+
const chainInfo = await this.getChainInfo();
|
|
322
|
+
let blockHeader: BlockHeader;
|
|
323
|
+
// PXE might not be synced yet, so we pull the latest header from the node
|
|
324
|
+
// To keep things consistent, we'll always try with PXE first
|
|
325
|
+
try {
|
|
326
|
+
blockHeader = await this.pxe.getSyncedBlockHeader();
|
|
327
|
+
} catch {
|
|
328
|
+
blockHeader = (await this.aztecNode.getBlockHeader())!;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const [optimizedResults, normalResult] = await Promise.all([
|
|
332
|
+
optimizableCalls.length > 0
|
|
333
|
+
? simulateViaNode(
|
|
334
|
+
this.aztecNode,
|
|
335
|
+
optimizableCalls,
|
|
336
|
+
opts.from,
|
|
337
|
+
chainInfo,
|
|
338
|
+
feeOptions.gasSettings,
|
|
339
|
+
blockHeader,
|
|
340
|
+
opts.skipFeeEnforcement ?? true,
|
|
341
|
+
)
|
|
342
|
+
: Promise.resolve([]),
|
|
343
|
+
remainingCalls.length > 0
|
|
344
|
+
? this.simulateViaEntrypoint(
|
|
345
|
+
remainingPayload,
|
|
346
|
+
opts.from,
|
|
347
|
+
feeOptions,
|
|
348
|
+
opts.skipTxValidation,
|
|
349
|
+
opts.skipFeeEnforcement ?? true,
|
|
350
|
+
)
|
|
351
|
+
: Promise.resolve(null),
|
|
352
|
+
]);
|
|
353
|
+
|
|
354
|
+
return buildMergedSimulationResult(optimizedResults, normalResult);
|
|
275
355
|
}
|
|
276
356
|
|
|
277
357
|
async profileTx(executionPayload: ExecutionPayload, opts: ProfileOptions): Promise<TxProfileResult> {
|
|
278
358
|
const feeOptions = await this.completeFeeOptions(opts.from, executionPayload.feePayer, opts.fee?.gasSettings);
|
|
279
359
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
280
|
-
return this.pxe.profileTx(txRequest,
|
|
360
|
+
return this.pxe.profileTx(txRequest, {
|
|
361
|
+
profileMode: opts.profileMode,
|
|
362
|
+
skipProofGeneration: opts.skipProofGeneration ?? true,
|
|
363
|
+
});
|
|
281
364
|
}
|
|
282
365
|
|
|
283
|
-
async sendTx
|
|
366
|
+
public async sendTx<W extends InteractionWaitOptions = undefined>(
|
|
367
|
+
executionPayload: ExecutionPayload,
|
|
368
|
+
opts: SendOptions<W>,
|
|
369
|
+
): Promise<SendReturn<W>> {
|
|
284
370
|
const feeOptions = await this.completeFeeOptions(opts.from, executionPayload.feePayer, opts.fee?.gasSettings);
|
|
285
371
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
286
372
|
const provenTx = await this.pxe.proveTx(txRequest);
|
|
@@ -294,7 +380,15 @@ export abstract class BaseWallet implements Wallet {
|
|
|
294
380
|
throw this.contextualizeError(err, inspect(tx));
|
|
295
381
|
});
|
|
296
382
|
this.log.info(`Sent transaction ${txHash}`);
|
|
297
|
-
|
|
383
|
+
|
|
384
|
+
// If wait is NO_WAIT, return txHash immediately
|
|
385
|
+
if (opts.wait === NO_WAIT) {
|
|
386
|
+
return txHash as SendReturn<W>;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Otherwise, wait for the full receipt (default behavior on wait: undefined)
|
|
390
|
+
const waitOpts = typeof opts.wait === 'object' ? opts.wait : undefined;
|
|
391
|
+
return (await waitForTx(this.aztecNode, txHash, waitOpts)) as SendReturn<W>;
|
|
298
392
|
}
|
|
299
393
|
|
|
300
394
|
protected contextualizeError(err: Error, ...context: string[]): Error {
|
|
@@ -312,18 +406,7 @@ export abstract class BaseWallet implements Wallet {
|
|
|
312
406
|
}
|
|
313
407
|
|
|
314
408
|
simulateUtility(call: FunctionCall, authwits?: AuthWitness[]): Promise<UtilitySimulationResult> {
|
|
315
|
-
return this.pxe.simulateUtility(call, authwits);
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
getContractClassMetadata(id: Fr, includeArtifact: boolean = false): Promise<ContractClassMetadata> {
|
|
319
|
-
return this.pxe.getContractClassMetadata(id, includeArtifact);
|
|
320
|
-
}
|
|
321
|
-
getContractMetadata(address: AztecAddress): Promise<ContractMetadata> {
|
|
322
|
-
return this.pxe.getContractMetadata(address);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
getTxReceipt(txHash: TxHash): Promise<TxReceipt> {
|
|
326
|
-
return this.aztecNode.getTxReceipt(txHash);
|
|
409
|
+
return this.pxe.simulateUtility(call, { authwits });
|
|
327
410
|
}
|
|
328
411
|
|
|
329
412
|
async getPrivateEvents<T>(
|
|
@@ -345,4 +428,29 @@ export abstract class BaseWallet implements Wallet {
|
|
|
345
428
|
|
|
346
429
|
return decodedEvents;
|
|
347
430
|
}
|
|
431
|
+
|
|
432
|
+
async getContractMetadata(address: AztecAddress) {
|
|
433
|
+
const instance = await this.pxe.getContractInstance(address);
|
|
434
|
+
const initNullifier = await siloNullifier(address, address.toField());
|
|
435
|
+
const publiclyRegisteredContract = await this.aztecNode.getContract(address);
|
|
436
|
+
const initNullifierMembershipWitness = await this.aztecNode.getNullifierMembershipWitness('latest', initNullifier);
|
|
437
|
+
const isContractUpdated =
|
|
438
|
+
publiclyRegisteredContract &&
|
|
439
|
+
!publiclyRegisteredContract.currentContractClassId.equals(publiclyRegisteredContract.originalContractClassId);
|
|
440
|
+
return {
|
|
441
|
+
instance: instance ?? undefined,
|
|
442
|
+
isContractInitialized: !!initNullifierMembershipWitness,
|
|
443
|
+
isContractPublished: !!publiclyRegisteredContract,
|
|
444
|
+
isContractUpdated: !!isContractUpdated,
|
|
445
|
+
updatedContractClassId: isContractUpdated ? publiclyRegisteredContract.currentContractClassId : undefined,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
async getContractClassMetadata(id: Fr) {
|
|
450
|
+
const publiclyRegisteredContractClass = await this.aztecNode.getContractClass(id);
|
|
451
|
+
return {
|
|
452
|
+
isArtifactRegistered: !!(await this.pxe.getContractArtifact(id)),
|
|
453
|
+
isContractClassPubliclyRegistered: !!publiclyRegisteredContractClass,
|
|
454
|
+
};
|
|
455
|
+
}
|
|
348
456
|
}
|
package/src/base-wallet/index.ts
CHANGED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import type { AztecNode } from '@aztec/aztec.js/node';
|
|
2
|
+
import { MAX_ENQUEUED_CALLS_PER_CALL } from '@aztec/constants';
|
|
3
|
+
import type { ChainInfo } from '@aztec/entrypoints/interfaces';
|
|
4
|
+
import { makeTuple } from '@aztec/foundation/array';
|
|
5
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
6
|
+
import type { Tuple } from '@aztec/foundation/serialize';
|
|
7
|
+
import { generateSimulatedProvingResult } from '@aztec/pxe/simulator';
|
|
8
|
+
import { type FunctionCall, FunctionSelector } from '@aztec/stdlib/abi';
|
|
9
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
10
|
+
import type { GasSettings } from '@aztec/stdlib/gas';
|
|
11
|
+
import {
|
|
12
|
+
ClaimedLengthArray,
|
|
13
|
+
CountedPublicCallRequest,
|
|
14
|
+
PrivateCircuitPublicInputs,
|
|
15
|
+
PublicCallRequest,
|
|
16
|
+
} from '@aztec/stdlib/kernel';
|
|
17
|
+
import { ChonkProof } from '@aztec/stdlib/proofs';
|
|
18
|
+
import {
|
|
19
|
+
type BlockHeader,
|
|
20
|
+
type ExecutionPayload,
|
|
21
|
+
HashedValues,
|
|
22
|
+
PrivateCallExecutionResult,
|
|
23
|
+
PrivateExecutionResult,
|
|
24
|
+
PublicSimulationOutput,
|
|
25
|
+
Tx,
|
|
26
|
+
TxContext,
|
|
27
|
+
TxSimulationResult,
|
|
28
|
+
} from '@aztec/stdlib/tx';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Splits an execution payload into a leading prefix of public static calls
|
|
32
|
+
* (eligible for direct node simulation) and the remaining calls.
|
|
33
|
+
*
|
|
34
|
+
* Only a leading run of public static calls is eligible for optimization.
|
|
35
|
+
* Any non-public-static call may enqueue public state mutations
|
|
36
|
+
* (e.g. private calls can enqueue public calls), so all calls that follow
|
|
37
|
+
* must go through the normal simulation path to see the correct state.
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
export function extractOptimizablePublicStaticCalls(payload: ExecutionPayload): {
|
|
41
|
+
/** Leading public static calls eligible for direct node simulation. */
|
|
42
|
+
optimizableCalls: FunctionCall[];
|
|
43
|
+
/** All remaining calls. */
|
|
44
|
+
remainingCalls: FunctionCall[];
|
|
45
|
+
} {
|
|
46
|
+
const splitIndex = payload.calls.findIndex(call => !call.isPublicStatic());
|
|
47
|
+
const boundary = splitIndex === -1 ? payload.calls.length : splitIndex;
|
|
48
|
+
return {
|
|
49
|
+
optimizableCalls: payload.calls.slice(0, boundary),
|
|
50
|
+
remainingCalls: payload.calls.slice(boundary),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Simulates a batch of public static calls by bypassing account entrypoint and private execution,
|
|
56
|
+
* directly constructing a minimal Tx and calling node.simulatePublicCalls.
|
|
57
|
+
*
|
|
58
|
+
* @param node - The Aztec node to simulate on.
|
|
59
|
+
* @param publicStaticCalls - Array of public static function calls (max MAX_ENQUEUED_CALLS_PER_CALL).
|
|
60
|
+
* @param from - The account address making the calls.
|
|
61
|
+
* @param chainInfo - Chain information (chainId and version).
|
|
62
|
+
* @param gasSettings - Gas settings for the transaction.
|
|
63
|
+
* @param blockHeader - Block header to use as anchor.
|
|
64
|
+
* @param skipFeeEnforcement - Whether to skip fee enforcement during simulation.
|
|
65
|
+
* @returns TxSimulationResult with public return values.
|
|
66
|
+
*/
|
|
67
|
+
async function simulateBatchViaNode(
|
|
68
|
+
node: AztecNode,
|
|
69
|
+
publicStaticCalls: FunctionCall[],
|
|
70
|
+
from: AztecAddress,
|
|
71
|
+
chainInfo: ChainInfo,
|
|
72
|
+
gasSettings: GasSettings,
|
|
73
|
+
blockHeader: BlockHeader,
|
|
74
|
+
skipFeeEnforcement: boolean,
|
|
75
|
+
): Promise<TxSimulationResult> {
|
|
76
|
+
const txContext = new TxContext(chainInfo.chainId, chainInfo.version, gasSettings);
|
|
77
|
+
|
|
78
|
+
const publicFunctionCalldata: HashedValues[] = [];
|
|
79
|
+
for (const call of publicStaticCalls) {
|
|
80
|
+
const calldata = await HashedValues.fromCalldata([call.selector.toField(), ...call.args]);
|
|
81
|
+
publicFunctionCalldata.push(calldata);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const publicCallRequests = makeTuple(MAX_ENQUEUED_CALLS_PER_CALL, i => {
|
|
85
|
+
const call = publicStaticCalls[i];
|
|
86
|
+
if (!call) {
|
|
87
|
+
return CountedPublicCallRequest.empty();
|
|
88
|
+
}
|
|
89
|
+
const publicCallRequest = new PublicCallRequest(from, call.to, call.isStatic, publicFunctionCalldata[i]!.hash);
|
|
90
|
+
// Counter starts at 1 (minRevertibleSideEffectCounter) so all calls are revertible
|
|
91
|
+
return new CountedPublicCallRequest(publicCallRequest, i + 1);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const publicCallRequestsArray: ClaimedLengthArray<CountedPublicCallRequest, typeof MAX_ENQUEUED_CALLS_PER_CALL> =
|
|
95
|
+
new ClaimedLengthArray(
|
|
96
|
+
publicCallRequests as Tuple<CountedPublicCallRequest, typeof MAX_ENQUEUED_CALLS_PER_CALL>,
|
|
97
|
+
publicStaticCalls.length,
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const publicInputs = PrivateCircuitPublicInputs.from({
|
|
101
|
+
...PrivateCircuitPublicInputs.empty(),
|
|
102
|
+
anchorBlockHeader: blockHeader,
|
|
103
|
+
txContext: txContext,
|
|
104
|
+
publicCallRequests: publicCallRequestsArray,
|
|
105
|
+
startSideEffectCounter: new Fr(0),
|
|
106
|
+
endSideEffectCounter: new Fr(publicStaticCalls.length + 1),
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// Minimal entrypoint structure — no real private execution, just public call requests
|
|
110
|
+
const emptyEntrypoint = new PrivateCallExecutionResult(
|
|
111
|
+
Buffer.alloc(0),
|
|
112
|
+
Buffer.alloc(0),
|
|
113
|
+
new Map(),
|
|
114
|
+
publicInputs,
|
|
115
|
+
[],
|
|
116
|
+
new Map(),
|
|
117
|
+
[],
|
|
118
|
+
[],
|
|
119
|
+
[],
|
|
120
|
+
[],
|
|
121
|
+
[],
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const privateResult = new PrivateExecutionResult(emptyEntrypoint, Fr.random(), publicFunctionCalldata);
|
|
125
|
+
|
|
126
|
+
const provingResult = await generateSimulatedProvingResult(
|
|
127
|
+
privateResult,
|
|
128
|
+
(_contractAddress: AztecAddress, _functionSelector: FunctionSelector) => Promise.resolve(''),
|
|
129
|
+
node,
|
|
130
|
+
1, // minRevertibleSideEffectCounter
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
provingResult.publicInputs.feePayer = from;
|
|
134
|
+
|
|
135
|
+
const tx = await Tx.create({
|
|
136
|
+
data: provingResult.publicInputs,
|
|
137
|
+
chonkProof: ChonkProof.empty(),
|
|
138
|
+
contractClassLogFields: [],
|
|
139
|
+
publicFunctionCalldata: publicFunctionCalldata,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const publicOutput = await node.simulatePublicCalls(tx, skipFeeEnforcement);
|
|
143
|
+
|
|
144
|
+
if (publicOutput.revertReason) {
|
|
145
|
+
throw publicOutput.revertReason;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return new TxSimulationResult(privateResult, provingResult.publicInputs, publicOutput, undefined);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Simulates public static calls by splitting them into batches of MAX_ENQUEUED_CALLS_PER_CALL
|
|
153
|
+
* and sending each batch directly to the node.
|
|
154
|
+
*
|
|
155
|
+
* @param node - The Aztec node to simulate on.
|
|
156
|
+
* @param publicStaticCalls - Array of public static function calls to optimize.
|
|
157
|
+
* @param from - The account address making the calls.
|
|
158
|
+
* @param chainInfo - Chain information (chainId and version).
|
|
159
|
+
* @param gasSettings - Gas settings for the transaction.
|
|
160
|
+
* @param blockHeader - Block header to use as anchor.
|
|
161
|
+
* @param skipFeeEnforcement - Whether to skip fee enforcement during simulation.
|
|
162
|
+
* @returns Array of TxSimulationResult, one per batch.
|
|
163
|
+
*/
|
|
164
|
+
export async function simulateViaNode(
|
|
165
|
+
node: AztecNode,
|
|
166
|
+
publicStaticCalls: FunctionCall[],
|
|
167
|
+
from: AztecAddress,
|
|
168
|
+
chainInfo: ChainInfo,
|
|
169
|
+
gasSettings: GasSettings,
|
|
170
|
+
blockHeader: BlockHeader,
|
|
171
|
+
skipFeeEnforcement: boolean = true,
|
|
172
|
+
): Promise<TxSimulationResult[]> {
|
|
173
|
+
const batches: FunctionCall[][] = [];
|
|
174
|
+
|
|
175
|
+
for (let i = 0; i < publicStaticCalls.length; i += MAX_ENQUEUED_CALLS_PER_CALL) {
|
|
176
|
+
batches.push(publicStaticCalls.slice(i, i + MAX_ENQUEUED_CALLS_PER_CALL));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const results: TxSimulationResult[] = [];
|
|
180
|
+
|
|
181
|
+
for (const batch of batches) {
|
|
182
|
+
const result = await simulateBatchViaNode(
|
|
183
|
+
node,
|
|
184
|
+
batch,
|
|
185
|
+
from,
|
|
186
|
+
chainInfo,
|
|
187
|
+
gasSettings,
|
|
188
|
+
blockHeader,
|
|
189
|
+
skipFeeEnforcement,
|
|
190
|
+
);
|
|
191
|
+
results.push(result);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return results;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Merges simulation results from the optimized (public static) and normal paths.
|
|
199
|
+
* Since optimized calls are always a leading prefix, return values are simply
|
|
200
|
+
* concatenated: optimized first, then normal.
|
|
201
|
+
* Stats are taken from the normal result only (the optimized path doesn't produce them).
|
|
202
|
+
*
|
|
203
|
+
* @param optimizedResults - Results from optimized public static call batches.
|
|
204
|
+
* @param normalResult - Result from normal simulation (null if all calls were optimized).
|
|
205
|
+
* @returns A single TxSimulationResult with return values in original call order.
|
|
206
|
+
*/
|
|
207
|
+
export function buildMergedSimulationResult(
|
|
208
|
+
optimizedResults: TxSimulationResult[],
|
|
209
|
+
normalResult: TxSimulationResult | null,
|
|
210
|
+
): TxSimulationResult {
|
|
211
|
+
const optimizedReturnValues = optimizedResults.flatMap(r => r.publicOutput?.publicReturnValues ?? []);
|
|
212
|
+
const normalReturnValues = normalResult?.publicOutput?.publicReturnValues ?? [];
|
|
213
|
+
const allReturnValues = [...optimizedReturnValues, ...normalReturnValues];
|
|
214
|
+
|
|
215
|
+
const baseResult = normalResult ?? optimizedResults[0];
|
|
216
|
+
|
|
217
|
+
const mergedPublicOutput: PublicSimulationOutput | undefined = baseResult.publicOutput
|
|
218
|
+
? {
|
|
219
|
+
...baseResult.publicOutput,
|
|
220
|
+
publicReturnValues: allReturnValues,
|
|
221
|
+
}
|
|
222
|
+
: undefined;
|
|
223
|
+
|
|
224
|
+
return new TxSimulationResult(
|
|
225
|
+
baseResult.privateExecutionResult,
|
|
226
|
+
baseResult.publicInputs,
|
|
227
|
+
mergedPublicOutput,
|
|
228
|
+
normalResult?.stats,
|
|
229
|
+
);
|
|
230
|
+
}
|