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