@aztec/wallet-sdk 0.0.1-commit.d6f2b3f94 → 0.0.1-commit.d939eb5aa
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/dest/base-wallet/base_wallet.d.ts +58 -39
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +194 -71
- package/dest/base-wallet/index.d.ts +2 -2
- package/dest/base-wallet/index.d.ts.map +1 -1
- package/dest/base-wallet/utils.d.ts +5 -3
- package/dest/base-wallet/utils.d.ts.map +1 -1
- package/dest/base-wallet/utils.js +10 -5
- package/dest/crypto.d.ts +39 -1
- package/dest/crypto.d.ts.map +1 -1
- package/dest/crypto.js +88 -0
- package/dest/extension/provider/extension_wallet.d.ts +4 -6
- package/dest/extension/provider/extension_wallet.d.ts.map +1 -1
- package/dest/extension/provider/extension_wallet.js +9 -2
- package/dest/extension/provider/index.d.ts +2 -2
- package/dest/extension/provider/index.d.ts.map +1 -1
- package/dest/iframe/handlers/iframe_connection_handler.d.ts +118 -0
- package/dest/iframe/handlers/iframe_connection_handler.d.ts.map +1 -0
- package/dest/iframe/handlers/iframe_connection_handler.js +228 -0
- package/dest/iframe/handlers/index.d.ts +2 -0
- package/dest/iframe/handlers/index.d.ts.map +1 -0
- package/dest/iframe/handlers/index.js +1 -0
- package/dest/iframe/provider/iframe_discovery.d.ts +25 -0
- package/dest/iframe/provider/iframe_discovery.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_discovery.js +167 -0
- package/dest/iframe/provider/iframe_provider.d.ts +65 -0
- package/dest/iframe/provider/iframe_provider.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_provider.js +257 -0
- package/dest/iframe/provider/iframe_wallet.d.ts +68 -0
- package/dest/iframe/provider/iframe_wallet.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_wallet.js +200 -0
- package/dest/iframe/provider/index.d.ts +4 -0
- package/dest/iframe/provider/index.d.ts.map +1 -0
- package/dest/iframe/provider/index.js +3 -0
- package/dest/manager/types.d.ts +6 -5
- package/dest/manager/types.d.ts.map +1 -1
- package/dest/manager/wallet_manager.d.ts +1 -1
- package/dest/manager/wallet_manager.d.ts.map +1 -1
- package/dest/manager/wallet_manager.js +48 -18
- package/dest/types.d.ts +14 -2
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +4 -0
- package/package.json +12 -8
- package/src/base-wallet/base_wallet.ts +265 -127
- package/src/base-wallet/index.ts +6 -1
- package/src/base-wallet/utils.ts +15 -4
- package/src/crypto.ts +104 -0
- package/src/extension/provider/extension_wallet.ts +13 -10
- package/src/extension/provider/index.ts +1 -1
- package/src/iframe/handlers/iframe_connection_handler.ts +328 -0
- package/src/iframe/handlers/index.ts +7 -0
- package/src/iframe/provider/iframe_discovery.ts +185 -0
- package/src/iframe/provider/iframe_provider.ts +331 -0
- package/src/iframe/provider/iframe_wallet.ts +229 -0
- package/src/iframe/provider/index.ts +3 -0
- package/src/manager/types.ts +5 -4
- package/src/manager/wallet_manager.ts +55 -23
- package/src/types.ts +13 -0
|
@@ -1,32 +1,37 @@
|
|
|
1
|
-
import type { Account } from '@aztec/aztec.js/account';
|
|
1
|
+
import type { Account, NoFrom } from '@aztec/aztec.js/account';
|
|
2
|
+
import { NO_FROM } from '@aztec/aztec.js/account';
|
|
2
3
|
import type { CallIntent, IntentInnerHash } from '@aztec/aztec.js/authorization';
|
|
3
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
type InteractionWaitOptions,
|
|
6
|
+
NO_WAIT,
|
|
7
|
+
type SendReturn,
|
|
8
|
+
extractOffchainOutput,
|
|
9
|
+
} from '@aztec/aztec.js/contracts';
|
|
4
10
|
import type { FeePaymentMethod } from '@aztec/aztec.js/fee';
|
|
5
11
|
import { waitForTx } from '@aztec/aztec.js/node';
|
|
6
|
-
import type {
|
|
7
|
-
Aliased,
|
|
8
|
-
AppCapabilities,
|
|
9
|
-
BatchResults,
|
|
10
|
-
BatchedMethod,
|
|
11
|
-
PrivateEvent,
|
|
12
|
-
PrivateEventFilter,
|
|
13
|
-
ProfileOptions,
|
|
14
|
-
SendOptions,
|
|
15
|
-
SimulateOptions,
|
|
16
|
-
Wallet,
|
|
17
|
-
WalletCapabilities,
|
|
18
|
-
} from '@aztec/aztec.js/wallet';
|
|
19
12
|
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
13
|
+
type Aliased,
|
|
14
|
+
type AppCapabilities,
|
|
15
|
+
type BatchResults,
|
|
16
|
+
type BatchedMethod,
|
|
17
|
+
ContractInitializationStatus,
|
|
18
|
+
type ExecuteUtilityOptions,
|
|
19
|
+
type PrivateEvent,
|
|
20
|
+
type PrivateEventFilter,
|
|
21
|
+
type ProfileOptions,
|
|
22
|
+
type SendOptions,
|
|
23
|
+
type SimulateOptions,
|
|
24
|
+
TxSimulationResultWithAppOffset,
|
|
25
|
+
type Wallet,
|
|
26
|
+
type WalletCapabilities,
|
|
27
|
+
} from '@aztec/aztec.js/wallet';
|
|
25
28
|
import { AccountFeePaymentMethodOptions, type DefaultAccountEntrypointOptions } from '@aztec/entrypoints/account';
|
|
29
|
+
import { DefaultEntrypoint } from '@aztec/entrypoints/default';
|
|
26
30
|
import type { ChainInfo } from '@aztec/entrypoints/interfaces';
|
|
27
31
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
28
32
|
import { createLogger } from '@aztec/foundation/log';
|
|
29
33
|
import type { FieldsOf } from '@aztec/foundation/types';
|
|
34
|
+
import { displayDebugLogs } from '@aztec/pxe/client/lazy';
|
|
30
35
|
import type { PXE, PackedPrivateEvent } from '@aztec/pxe/server';
|
|
31
36
|
import {
|
|
32
37
|
type ContractArtifact,
|
|
@@ -35,23 +40,27 @@ import {
|
|
|
35
40
|
decodeFromAbi,
|
|
36
41
|
} from '@aztec/stdlib/abi';
|
|
37
42
|
import type { AuthWitness } from '@aztec/stdlib/auth-witness';
|
|
38
|
-
import
|
|
43
|
+
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
39
44
|
import {
|
|
40
45
|
type ContractInstanceWithAddress,
|
|
41
46
|
computePartialAddress,
|
|
42
47
|
getContractClassFromArtifact,
|
|
43
48
|
} from '@aztec/stdlib/contract';
|
|
44
49
|
import { SimulationError } from '@aztec/stdlib/errors';
|
|
45
|
-
import { Gas, GasSettings } from '@aztec/stdlib/gas';
|
|
46
|
-
import {
|
|
50
|
+
import { Gas, GasFees, GasSettings, ManaUsageEstimate } from '@aztec/stdlib/gas';
|
|
51
|
+
import {
|
|
52
|
+
computeSiloedPrivateInitializationNullifier,
|
|
53
|
+
computeSiloedPublicInitializationNullifier,
|
|
54
|
+
} from '@aztec/stdlib/hash';
|
|
47
55
|
import type { AztecNode } from '@aztec/stdlib/interfaces/client';
|
|
48
56
|
import {
|
|
57
|
+
BlockHeader,
|
|
58
|
+
ExecutionPayload,
|
|
49
59
|
type TxExecutionRequest,
|
|
50
60
|
type TxProfileResult,
|
|
51
|
-
|
|
52
|
-
|
|
61
|
+
type UtilityExecutionResult,
|
|
62
|
+
mergeExecutionPayloads,
|
|
53
63
|
} from '@aztec/stdlib/tx';
|
|
54
|
-
import { ExecutionPayload, mergeExecutionPayloads } from '@aztec/stdlib/tx';
|
|
55
64
|
|
|
56
65
|
import { inspect } from 'util';
|
|
57
66
|
|
|
@@ -67,17 +76,41 @@ export type FeeOptions = {
|
|
|
67
76
|
*/
|
|
68
77
|
walletFeePaymentMethod?: FeePaymentMethod;
|
|
69
78
|
/** Configuration options for the account to properly handle the selected fee payment method */
|
|
70
|
-
accountFeePaymentMethodOptions
|
|
79
|
+
accountFeePaymentMethodOptions?: AccountFeePaymentMethodOptions;
|
|
71
80
|
/** The gas settings to use for the transaction */
|
|
72
81
|
gasSettings: GasSettings;
|
|
73
82
|
};
|
|
74
83
|
|
|
84
|
+
/** Options for `simulateViaEntrypoint`. */
|
|
85
|
+
export type SimulateViaEntrypointOptions = Pick<
|
|
86
|
+
SimulateOptions,
|
|
87
|
+
'from' | 'additionalScopes' | 'skipTxValidation' | 'skipFeeEnforcement'
|
|
88
|
+
> & {
|
|
89
|
+
/** Fee options for the entrypoint */
|
|
90
|
+
feeOptions: FeeOptions;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/** Options for `completeFeeOptions`. */
|
|
94
|
+
export type CompleteFeeOptionsConfig = {
|
|
95
|
+
/** The address where the transaction is being sent from. */
|
|
96
|
+
from: AztecAddress | NoFrom;
|
|
97
|
+
/** The address paying for fees (if any fee payment method is embedded in the execution payload). */
|
|
98
|
+
feePayer?: AztecAddress;
|
|
99
|
+
/** User-provided partial gas settings. */
|
|
100
|
+
gasSettings?: Partial<FieldsOf<GasSettings>>;
|
|
101
|
+
/** If true, returns gas settings with high gas limits for estimation. If false, uses fallback limits. */
|
|
102
|
+
forEstimation?: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Assumed network congestion level for fee prediction. Controls how aggressively the wallet
|
|
105
|
+
* estimates future fees. Defaults to Limit (worst case) when not specified.
|
|
106
|
+
*/
|
|
107
|
+
congestionEstimate?: ManaUsageEstimate;
|
|
108
|
+
};
|
|
109
|
+
|
|
75
110
|
/**
|
|
76
111
|
* A base class for Wallet implementations
|
|
77
112
|
*/
|
|
78
113
|
export abstract class BaseWallet implements Wallet {
|
|
79
|
-
protected log = createLogger('wallet-sdk:base_wallet');
|
|
80
|
-
|
|
81
114
|
protected minFeePadding = 0.5;
|
|
82
115
|
protected cancellableTransactions = false;
|
|
83
116
|
|
|
@@ -85,8 +118,15 @@ export abstract class BaseWallet implements Wallet {
|
|
|
85
118
|
protected constructor(
|
|
86
119
|
protected readonly pxe: PXE,
|
|
87
120
|
protected readonly aztecNode: AztecNode,
|
|
121
|
+
protected log = createLogger('wallet-sdk:base_wallet'),
|
|
88
122
|
) {}
|
|
89
123
|
|
|
124
|
+
protected scopesFrom(from: AztecAddress | NoFrom, additionalScopes: AztecAddress[] = []): AztecAddress[] {
|
|
125
|
+
const allScopes = from === NO_FROM ? additionalScopes : [from, ...additionalScopes];
|
|
126
|
+
const scopeSet = new Set(allScopes.map(address => address.toString()));
|
|
127
|
+
return [...scopeSet].map(AztecAddress.fromString);
|
|
128
|
+
}
|
|
129
|
+
|
|
90
130
|
protected abstract getAccountFromAddress(address: AztecAddress): Promise<Account>;
|
|
91
131
|
|
|
92
132
|
abstract getAccounts(): Promise<Aliased<AztecAddress>[]>;
|
|
@@ -110,26 +150,33 @@ export abstract class BaseWallet implements Wallet {
|
|
|
110
150
|
|
|
111
151
|
protected async createTxExecutionRequestFromPayloadAndFee(
|
|
112
152
|
executionPayload: ExecutionPayload,
|
|
113
|
-
from: AztecAddress,
|
|
153
|
+
from: AztecAddress | NoFrom,
|
|
114
154
|
feeOptions: FeeOptions,
|
|
115
155
|
): Promise<TxExecutionRequest> {
|
|
116
156
|
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
117
|
-
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
118
|
-
txNonce: Fr.random(),
|
|
119
|
-
cancellable: this.cancellableTransactions,
|
|
120
|
-
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions,
|
|
121
|
-
};
|
|
122
157
|
const finalExecutionPayload = feeExecutionPayload
|
|
123
158
|
? mergeExecutionPayloads([feeExecutionPayload, executionPayload])
|
|
124
159
|
: executionPayload;
|
|
125
|
-
const fromAccount = await this.getAccountFromAddress(from);
|
|
126
160
|
const chainInfo = await this.getChainInfo();
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
chainInfo
|
|
131
|
-
|
|
132
|
-
|
|
161
|
+
|
|
162
|
+
if (from === NO_FROM) {
|
|
163
|
+
const entrypoint = new DefaultEntrypoint();
|
|
164
|
+
return entrypoint.createTxExecutionRequest(finalExecutionPayload, feeOptions.gasSettings, chainInfo);
|
|
165
|
+
} else {
|
|
166
|
+
const fromAccount = await this.getAccountFromAddress(from);
|
|
167
|
+
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
168
|
+
txNonce: Fr.random(),
|
|
169
|
+
cancellable: this.cancellableTransactions,
|
|
170
|
+
// If from is an address, feeOptions include the way the account contract should handle the fee payment
|
|
171
|
+
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions!,
|
|
172
|
+
};
|
|
173
|
+
return fromAccount.createTxExecutionRequest(
|
|
174
|
+
finalExecutionPayload,
|
|
175
|
+
feeOptions.gasSettings,
|
|
176
|
+
chainInfo,
|
|
177
|
+
executionOptions,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
133
180
|
}
|
|
134
181
|
|
|
135
182
|
public async createAuthWit(
|
|
@@ -178,31 +225,39 @@ export abstract class BaseWallet implements Wallet {
|
|
|
178
225
|
|
|
179
226
|
/**
|
|
180
227
|
* Completes partial user-provided fee options with wallet defaults.
|
|
181
|
-
* @param
|
|
182
|
-
* @param feePayer - The address paying for fees (if any fee payment method is embedded in the execution payload)
|
|
183
|
-
* @param gasSettings - User-provided partial gas settings
|
|
184
|
-
* @returns - Complete fee options that can be used to create a transaction execution request
|
|
228
|
+
* @param config - Fee completion config.
|
|
185
229
|
*/
|
|
186
|
-
protected async completeFeeOptions(
|
|
187
|
-
from
|
|
188
|
-
feePayer?: AztecAddress,
|
|
189
|
-
gasSettings?: Partial<FieldsOf<GasSettings>>,
|
|
190
|
-
): Promise<FeeOptions> {
|
|
230
|
+
protected async completeFeeOptions(config: CompleteFeeOptionsConfig): Promise<FeeOptions> {
|
|
231
|
+
const { from, feePayer, gasSettings, forEstimation, congestionEstimate } = config;
|
|
191
232
|
const maxFeesPerGas =
|
|
192
|
-
gasSettings?.maxFeesPerGas ?? (await this.
|
|
233
|
+
gasSettings?.maxFeesPerGas ?? (await this.getMinFees(congestionEstimate)).mul(1 + this.minFeePadding);
|
|
193
234
|
let accountFeePaymentMethodOptions;
|
|
194
|
-
//
|
|
195
|
-
//
|
|
196
|
-
if (
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
235
|
+
// If from is an address, we need to determine the appropriate fee payment method options for the
|
|
236
|
+
// account contract entrypoint to use
|
|
237
|
+
if (from !== NO_FROM) {
|
|
238
|
+
if (!feePayer) {
|
|
239
|
+
// The transaction does not include a fee payment method, so we set the flag
|
|
240
|
+
// for the account to use its fee juice balance
|
|
241
|
+
accountFeePaymentMethodOptions = AccountFeePaymentMethodOptions.PREEXISTING_FEE_JUICE;
|
|
242
|
+
} else {
|
|
243
|
+
// The transaction includes fee payment method, so we check if we are the fee payer for it
|
|
244
|
+
// (this can only happen if the embedded payment method is FeeJuiceWithClaim)
|
|
245
|
+
accountFeePaymentMethodOptions = from.equals(feePayer)
|
|
246
|
+
? AccountFeePaymentMethodOptions.FEE_JUICE_WITH_CLAIM
|
|
247
|
+
: AccountFeePaymentMethodOptions.EXTERNAL;
|
|
248
|
+
}
|
|
204
249
|
}
|
|
205
|
-
const
|
|
250
|
+
const gasSettingsOverrides = {
|
|
251
|
+
gasLimits: gasSettings?.gasLimits ? Gas.from(gasSettings.gasLimits) : undefined,
|
|
252
|
+
teardownGasLimits: gasSettings?.teardownGasLimits ? Gas.from(gasSettings.teardownGasLimits) : undefined,
|
|
253
|
+
maxFeesPerGas,
|
|
254
|
+
maxPriorityFeesPerGas: gasSettings?.maxPriorityFeesPerGas ?? GasFees.empty(),
|
|
255
|
+
};
|
|
256
|
+
// When estimating gas (simulation), use high limits so the simulation doesn't run out of gas.
|
|
257
|
+
// When sending for real, use protocol max limits that the network will actually accept.
|
|
258
|
+
const fullGasSettings = forEstimation
|
|
259
|
+
? GasSettings.forEstimation(gasSettingsOverrides)
|
|
260
|
+
: GasSettings.fallback(gasSettingsOverrides);
|
|
206
261
|
this.log.debug(`Using L2 gas settings`, fullGasSettings);
|
|
207
262
|
return {
|
|
208
263
|
gasSettings: fullGasSettings,
|
|
@@ -212,34 +267,25 @@ export abstract class BaseWallet implements Wallet {
|
|
|
212
267
|
}
|
|
213
268
|
|
|
214
269
|
/**
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
* to
|
|
218
|
-
* @param from - The address where the transaction is being sent from
|
|
219
|
-
* @param feePayer - The address paying for fees (if any fee payment method is embedded in the execution payload)
|
|
220
|
-
* @param gasSettings - User-provided partial gas settings
|
|
270
|
+
* Returns the worst-case min fee across predicted future slots.
|
|
271
|
+
* Falls back to getCurrentMinFees if the node doesn't support getPredictedMinFees.
|
|
272
|
+
* @param estimate - The mana usage estimate to use for fee prediction. Defaults to Limit for conservative estimation.
|
|
221
273
|
*/
|
|
222
|
-
protected async
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
maxPriorityFeesPerGas,
|
|
238
|
-
);
|
|
239
|
-
return {
|
|
240
|
-
...defaultFeeOptions,
|
|
241
|
-
gasSettings: gasSettingsForEstimation,
|
|
242
|
-
};
|
|
274
|
+
protected async getMinFees(estimate: ManaUsageEstimate = ManaUsageEstimate.Limit): Promise<GasFees> {
|
|
275
|
+
try {
|
|
276
|
+
const predicted = await this.aztecNode.getPredictedMinFees(estimate);
|
|
277
|
+
if (predicted.length === 0) {
|
|
278
|
+
return this.aztecNode.getCurrentMinFees();
|
|
279
|
+
}
|
|
280
|
+
return predicted.reduce((worst, fees) => (fees.feePerL2Gas > worst.feePerL2Gas ? fees : worst));
|
|
281
|
+
} catch (err: any) {
|
|
282
|
+
// Fallback for old nodes that don't support getPredictedMinFees.
|
|
283
|
+
// Only fall back on method-not-found errors (JSON-RPC code -32601); rethrow others.
|
|
284
|
+
if (err?.cause?.code === -32601 || err?.message?.includes('Method not found')) {
|
|
285
|
+
return this.aztecNode.getCurrentMinFees();
|
|
286
|
+
}
|
|
287
|
+
throw err;
|
|
288
|
+
}
|
|
243
289
|
}
|
|
244
290
|
|
|
245
291
|
registerSender(address: AztecAddress, _alias: string = ''): Promise<AztecAddress> {
|
|
@@ -287,20 +333,36 @@ export abstract class BaseWallet implements Wallet {
|
|
|
287
333
|
/**
|
|
288
334
|
* Simulates calls through the standard PXE path (account entrypoint).
|
|
289
335
|
* @param executionPayload - The execution payload to simulate.
|
|
290
|
-
* @param
|
|
291
|
-
* @param feeOptions - Fee options for the transaction.
|
|
292
|
-
* @param skipTxValidation - Whether to skip tx validation.
|
|
293
|
-
* @param skipFeeEnforcement - Whether to skip fee enforcement.
|
|
336
|
+
* @param opts - Simulation options.
|
|
294
337
|
*/
|
|
295
|
-
protected async simulateViaEntrypoint(
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
338
|
+
protected async simulateViaEntrypoint(executionPayload: ExecutionPayload, opts: SimulateViaEntrypointOptions) {
|
|
339
|
+
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(
|
|
340
|
+
executionPayload,
|
|
341
|
+
opts.from,
|
|
342
|
+
opts.feeOptions,
|
|
343
|
+
);
|
|
344
|
+
const result = await this.pxe.simulateTx(txRequest, {
|
|
345
|
+
simulatePublic: true,
|
|
346
|
+
skipTxValidation: opts.skipTxValidation,
|
|
347
|
+
skipFeeEnforcement: opts.skipFeeEnforcement,
|
|
348
|
+
scopes: this.scopesFrom(opts.from, opts.additionalScopes),
|
|
349
|
+
});
|
|
350
|
+
const appCallOffset = await this.computeAppCallOffset(opts.from, opts.feeOptions);
|
|
351
|
+
return TxSimulationResultWithAppOffset.fromResultAndOffset(result, appCallOffset);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Computes the index where the app's calls begin in the flattened array of calls (0 = entrypoint/root, 1..N = fee
|
|
356
|
+
* calls, N+1 = app).
|
|
357
|
+
* @param from - The sender address, or NO_FROM for the default entrypoint.
|
|
358
|
+
* @param feeOptions - Fee options containing the wallet fee payment method.
|
|
359
|
+
*/
|
|
360
|
+
protected async computeAppCallOffset(from: AztecAddress | NoFrom, feeOptions: FeeOptions): Promise<number> {
|
|
361
|
+
if (from === NO_FROM) {
|
|
362
|
+
return 0;
|
|
363
|
+
}
|
|
364
|
+
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
365
|
+
return (feeExecutionPayload?.calls.length ?? 0) + 1; // +1 for entrypoint
|
|
304
366
|
}
|
|
305
367
|
|
|
306
368
|
/**
|
|
@@ -311,36 +373,52 @@ export abstract class BaseWallet implements Wallet {
|
|
|
311
373
|
* @param opts - Simulation options (from address, fee settings, etc.).
|
|
312
374
|
* @returns The merged simulation result.
|
|
313
375
|
*/
|
|
314
|
-
async simulateTx(
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
376
|
+
async simulateTx(
|
|
377
|
+
executionPayload: ExecutionPayload,
|
|
378
|
+
opts: SimulateOptions,
|
|
379
|
+
): Promise<TxSimulationResultWithAppOffset> {
|
|
380
|
+
const feeOptions = await this.completeFeeOptions({
|
|
381
|
+
from: opts.from,
|
|
382
|
+
feePayer: executionPayload.feePayer,
|
|
383
|
+
gasSettings: opts.fee?.gasSettings,
|
|
384
|
+
forEstimation: true,
|
|
385
|
+
congestionEstimate: opts.fee?.congestionEstimate,
|
|
386
|
+
});
|
|
318
387
|
const { optimizableCalls, remainingCalls } = extractOptimizablePublicStaticCalls(executionPayload);
|
|
319
388
|
const remainingPayload = { ...executionPayload, calls: remainingCalls };
|
|
320
389
|
|
|
321
390
|
const chainInfo = await this.getChainInfo();
|
|
322
|
-
|
|
391
|
+
let blockHeader: BlockHeader;
|
|
392
|
+
// PXE might not be synced yet, so we pull the latest header from the node
|
|
393
|
+
// To keep things consistent, we'll always try with PXE first
|
|
394
|
+
try {
|
|
395
|
+
blockHeader = await this.pxe.getSyncedBlockHeader();
|
|
396
|
+
} catch {
|
|
397
|
+
blockHeader = (await this.aztecNode.getBlockHeader())!;
|
|
398
|
+
}
|
|
323
399
|
|
|
400
|
+
const simulationOrigin = opts.from === NO_FROM ? AztecAddress.ZERO : opts.from;
|
|
324
401
|
const [optimizedResults, normalResult] = await Promise.all([
|
|
325
402
|
optimizableCalls.length > 0
|
|
326
403
|
? simulateViaNode(
|
|
327
404
|
this.aztecNode,
|
|
328
405
|
optimizableCalls,
|
|
329
|
-
|
|
406
|
+
simulationOrigin,
|
|
330
407
|
chainInfo,
|
|
331
408
|
feeOptions.gasSettings,
|
|
332
409
|
blockHeader,
|
|
333
410
|
opts.skipFeeEnforcement ?? true,
|
|
411
|
+
this.getContractName.bind(this),
|
|
334
412
|
)
|
|
335
413
|
: Promise.resolve([]),
|
|
336
414
|
remainingCalls.length > 0
|
|
337
|
-
? this.simulateViaEntrypoint(
|
|
338
|
-
|
|
339
|
-
opts.from,
|
|
415
|
+
? this.simulateViaEntrypoint(remainingPayload, {
|
|
416
|
+
from: opts.from,
|
|
340
417
|
feeOptions,
|
|
341
|
-
opts.
|
|
342
|
-
opts.
|
|
343
|
-
|
|
418
|
+
additionalScopes: opts.additionalScopes,
|
|
419
|
+
skipTxValidation: opts.skipTxValidation,
|
|
420
|
+
skipFeeEnforcement: opts.skipFeeEnforcement ?? true,
|
|
421
|
+
})
|
|
344
422
|
: Promise.resolve(null),
|
|
345
423
|
]);
|
|
346
424
|
|
|
@@ -348,18 +426,36 @@ export abstract class BaseWallet implements Wallet {
|
|
|
348
426
|
}
|
|
349
427
|
|
|
350
428
|
async profileTx(executionPayload: ExecutionPayload, opts: ProfileOptions): Promise<TxProfileResult> {
|
|
351
|
-
const feeOptions = await this.completeFeeOptions(
|
|
429
|
+
const feeOptions = await this.completeFeeOptions({
|
|
430
|
+
from: opts.from,
|
|
431
|
+
feePayer: executionPayload.feePayer,
|
|
432
|
+
gasSettings: opts.fee?.gasSettings,
|
|
433
|
+
congestionEstimate: opts.fee?.congestionEstimate,
|
|
434
|
+
});
|
|
352
435
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
353
|
-
return this.pxe.profileTx(txRequest,
|
|
436
|
+
return this.pxe.profileTx(txRequest, {
|
|
437
|
+
profileMode: opts.profileMode,
|
|
438
|
+
skipProofGeneration: opts.skipProofGeneration ?? true,
|
|
439
|
+
scopes: this.scopesFrom(opts.from, opts.additionalScopes),
|
|
440
|
+
});
|
|
354
441
|
}
|
|
355
442
|
|
|
356
443
|
public async sendTx<W extends InteractionWaitOptions = undefined>(
|
|
357
444
|
executionPayload: ExecutionPayload,
|
|
358
445
|
opts: SendOptions<W>,
|
|
359
446
|
): Promise<SendReturn<W>> {
|
|
360
|
-
const feeOptions = await this.completeFeeOptions(
|
|
447
|
+
const feeOptions = await this.completeFeeOptions({
|
|
448
|
+
from: opts.from,
|
|
449
|
+
feePayer: executionPayload.feePayer,
|
|
450
|
+
gasSettings: opts.fee?.gasSettings,
|
|
451
|
+
congestionEstimate: opts.fee?.congestionEstimate,
|
|
452
|
+
});
|
|
361
453
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
362
|
-
const provenTx = await this.pxe.proveTx(txRequest);
|
|
454
|
+
const provenTx = await this.pxe.proveTx(txRequest, this.scopesFrom(opts.from, opts.additionalScopes));
|
|
455
|
+
const offchainOutput = extractOffchainOutput(
|
|
456
|
+
provenTx.getOffchainEffects(),
|
|
457
|
+
provenTx.publicInputs.constants.anchorBlockHeader.globalVariables.timestamp,
|
|
458
|
+
);
|
|
363
459
|
const tx = await provenTx.toTx();
|
|
364
460
|
const txHash = tx.getTxHash();
|
|
365
461
|
if (await this.aztecNode.getTxEffect(txHash)) {
|
|
@@ -373,12 +469,32 @@ export abstract class BaseWallet implements Wallet {
|
|
|
373
469
|
|
|
374
470
|
// If wait is NO_WAIT, return txHash immediately
|
|
375
471
|
if (opts.wait === NO_WAIT) {
|
|
376
|
-
return txHash as SendReturn<W>;
|
|
472
|
+
return { txHash, ...offchainOutput } as SendReturn<W>;
|
|
377
473
|
}
|
|
378
474
|
|
|
379
475
|
// Otherwise, wait for the full receipt (default behavior on wait: undefined)
|
|
380
476
|
const waitOpts = typeof opts.wait === 'object' ? opts.wait : undefined;
|
|
381
|
-
|
|
477
|
+
const receipt = await waitForTx(this.aztecNode, txHash, waitOpts);
|
|
478
|
+
|
|
479
|
+
// Display debug logs from public execution if present (served in test mode only)
|
|
480
|
+
if (receipt.debugLogs?.length) {
|
|
481
|
+
await displayDebugLogs(receipt.debugLogs, this.getContractName.bind(this));
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return { receipt, ...offchainOutput } as SendReturn<W>;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Resolves a contract address to a human-readable name via PXE, if available.
|
|
489
|
+
* @param address - The contract address to resolve.
|
|
490
|
+
*/
|
|
491
|
+
protected async getContractName(address: AztecAddress): Promise<string | undefined> {
|
|
492
|
+
const instance = await this.pxe.getContractInstance(address);
|
|
493
|
+
if (!instance) {
|
|
494
|
+
return undefined;
|
|
495
|
+
}
|
|
496
|
+
const artifact = await this.pxe.getContractArtifact(instance.currentContractClassId);
|
|
497
|
+
return artifact?.name;
|
|
382
498
|
}
|
|
383
499
|
|
|
384
500
|
protected contextualizeError(err: Error, ...context: string[]): Error {
|
|
@@ -395,8 +511,8 @@ export abstract class BaseWallet implements Wallet {
|
|
|
395
511
|
return err;
|
|
396
512
|
}
|
|
397
513
|
|
|
398
|
-
|
|
399
|
-
return this.pxe.
|
|
514
|
+
executeUtility(call: FunctionCall, opts: ExecuteUtilityOptions): Promise<UtilityExecutionResult> {
|
|
515
|
+
return this.pxe.executeUtility(call, { authwits: opts.authWitnesses, scopes: opts.scopes });
|
|
400
516
|
}
|
|
401
517
|
|
|
402
518
|
async getPrivateEvents<T>(
|
|
@@ -419,17 +535,39 @@ export abstract class BaseWallet implements Wallet {
|
|
|
419
535
|
return decodedEvents;
|
|
420
536
|
}
|
|
421
537
|
|
|
538
|
+
/**
|
|
539
|
+
* Returns metadata about a contract, including whether it has been initialized, published, and updated.
|
|
540
|
+
* @param address - The contract address to query.
|
|
541
|
+
*/
|
|
422
542
|
async getContractMetadata(address: AztecAddress) {
|
|
423
543
|
const instance = await this.pxe.getContractInstance(address);
|
|
424
|
-
const
|
|
425
|
-
|
|
426
|
-
|
|
544
|
+
const publiclyRegisteredContractPromise = this.aztecNode.getContract(address);
|
|
545
|
+
|
|
546
|
+
let initializationStatus: ContractInitializationStatus;
|
|
547
|
+
if (instance) {
|
|
548
|
+
// We have the instance, so we can compute the private initialization nullifier (which includes init_hash and is
|
|
549
|
+
// emitted by both private and public initializers) and get a definitive INITIALIZED/UNINITIALIZED answer.
|
|
550
|
+
const initNullifier = await computeSiloedPrivateInitializationNullifier(address, instance.initializationHash);
|
|
551
|
+
const witness = await this.aztecNode.getNullifierMembershipWitness('latest', initNullifier);
|
|
552
|
+
initializationStatus = witness
|
|
553
|
+
? ContractInitializationStatus.INITIALIZED
|
|
554
|
+
: ContractInitializationStatus.UNINITIALIZED;
|
|
555
|
+
} else {
|
|
556
|
+
// Without the instance we lack the init_hash needed for the private nullifier. We fall back to checking the
|
|
557
|
+
// public initialization nullifier (computed from address alone). Not all contracts emit it (only those with
|
|
558
|
+
// public functions that require initialization checks), so its absence doesn't mean the contract is
|
|
559
|
+
// uninitialized.
|
|
560
|
+
const publicNullifier = await computeSiloedPublicInitializationNullifier(address);
|
|
561
|
+
const witness = await this.aztecNode.getNullifierMembershipWitness('latest', publicNullifier);
|
|
562
|
+
initializationStatus = witness ? ContractInitializationStatus.INITIALIZED : ContractInitializationStatus.UNKNOWN;
|
|
563
|
+
}
|
|
564
|
+
const publiclyRegisteredContract = await publiclyRegisteredContractPromise;
|
|
427
565
|
const isContractUpdated =
|
|
428
566
|
publiclyRegisteredContract &&
|
|
429
567
|
!publiclyRegisteredContract.currentContractClassId.equals(publiclyRegisteredContract.originalContractClassId);
|
|
430
568
|
return {
|
|
431
569
|
instance: instance ?? undefined,
|
|
432
|
-
|
|
570
|
+
initializationStatus,
|
|
433
571
|
isContractPublished: !!publiclyRegisteredContract,
|
|
434
572
|
isContractUpdated: !!isContractUpdated,
|
|
435
573
|
updatedContractClassId: isContractUpdated ? publiclyRegisteredContract.currentContractClassId : undefined,
|
package/src/base-wallet/index.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {
|
|
2
|
+
BaseWallet,
|
|
3
|
+
type CompleteFeeOptionsConfig,
|
|
4
|
+
type FeeOptions,
|
|
5
|
+
type SimulateViaEntrypointOptions,
|
|
6
|
+
} from './base_wallet.js';
|
|
2
7
|
export { simulateViaNode, buildMergedSimulationResult, extractOptimizablePublicStaticCalls } from './utils.js';
|
package/src/base-wallet/utils.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { AztecNode } from '@aztec/aztec.js/node';
|
|
2
|
+
import { TxSimulationResultWithAppOffset } from '@aztec/aztec.js/wallet';
|
|
2
3
|
import { MAX_ENQUEUED_CALLS_PER_CALL } from '@aztec/constants';
|
|
3
4
|
import type { ChainInfo } from '@aztec/entrypoints/interfaces';
|
|
4
5
|
import { makeTuple } from '@aztec/foundation/array';
|
|
5
6
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
6
7
|
import type { Tuple } from '@aztec/foundation/serialize';
|
|
8
|
+
import type { ContractNameResolver } from '@aztec/pxe/client/lazy';
|
|
9
|
+
import { displayDebugLogs } from '@aztec/pxe/client/lazy';
|
|
7
10
|
import { generateSimulatedProvingResult } from '@aztec/pxe/simulator';
|
|
8
11
|
import { type FunctionCall, FunctionSelector } from '@aztec/stdlib/abi';
|
|
9
12
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
@@ -72,6 +75,7 @@ async function simulateBatchViaNode(
|
|
|
72
75
|
gasSettings: GasSettings,
|
|
73
76
|
blockHeader: BlockHeader,
|
|
74
77
|
skipFeeEnforcement: boolean,
|
|
78
|
+
getContractName: ContractNameResolver,
|
|
75
79
|
): Promise<TxSimulationResult> {
|
|
76
80
|
const txContext = new TxContext(chainInfo.chainId, chainInfo.version, gasSettings);
|
|
77
81
|
|
|
@@ -126,6 +130,7 @@ async function simulateBatchViaNode(
|
|
|
126
130
|
const provingResult = await generateSimulatedProvingResult(
|
|
127
131
|
privateResult,
|
|
128
132
|
(_contractAddress: AztecAddress, _functionSelector: FunctionSelector) => Promise.resolve(''),
|
|
133
|
+
node,
|
|
129
134
|
1, // minRevertibleSideEffectCounter
|
|
130
135
|
);
|
|
131
136
|
|
|
@@ -144,6 +149,9 @@ async function simulateBatchViaNode(
|
|
|
144
149
|
throw publicOutput.revertReason;
|
|
145
150
|
}
|
|
146
151
|
|
|
152
|
+
// Display debug logs from the public simulation.
|
|
153
|
+
await displayDebugLogs(publicOutput.debugLogs, getContractName);
|
|
154
|
+
|
|
147
155
|
return new TxSimulationResult(privateResult, provingResult.publicInputs, publicOutput, undefined);
|
|
148
156
|
}
|
|
149
157
|
|
|
@@ -168,6 +176,7 @@ export async function simulateViaNode(
|
|
|
168
176
|
gasSettings: GasSettings,
|
|
169
177
|
blockHeader: BlockHeader,
|
|
170
178
|
skipFeeEnforcement: boolean = true,
|
|
179
|
+
getContractName: ContractNameResolver,
|
|
171
180
|
): Promise<TxSimulationResult[]> {
|
|
172
181
|
const batches: FunctionCall[][] = [];
|
|
173
182
|
|
|
@@ -186,6 +195,7 @@ export async function simulateViaNode(
|
|
|
186
195
|
gasSettings,
|
|
187
196
|
blockHeader,
|
|
188
197
|
skipFeeEnforcement,
|
|
198
|
+
getContractName,
|
|
189
199
|
);
|
|
190
200
|
results.push(result);
|
|
191
201
|
}
|
|
@@ -205,13 +215,13 @@ export async function simulateViaNode(
|
|
|
205
215
|
*/
|
|
206
216
|
export function buildMergedSimulationResult(
|
|
207
217
|
optimizedResults: TxSimulationResult[],
|
|
208
|
-
normalResult:
|
|
209
|
-
):
|
|
218
|
+
normalResult: TxSimulationResultWithAppOffset | null,
|
|
219
|
+
): TxSimulationResultWithAppOffset {
|
|
210
220
|
const optimizedReturnValues = optimizedResults.flatMap(r => r.publicOutput?.publicReturnValues ?? []);
|
|
211
221
|
const normalReturnValues = normalResult?.publicOutput?.publicReturnValues ?? [];
|
|
212
222
|
const allReturnValues = [...optimizedReturnValues, ...normalReturnValues];
|
|
213
223
|
|
|
214
|
-
const baseResult = normalResult ?? optimizedResults[0];
|
|
224
|
+
const baseResult: TxSimulationResult = normalResult ?? optimizedResults[0];
|
|
215
225
|
|
|
216
226
|
const mergedPublicOutput: PublicSimulationOutput | undefined = baseResult.publicOutput
|
|
217
227
|
? {
|
|
@@ -220,10 +230,11 @@ export function buildMergedSimulationResult(
|
|
|
220
230
|
}
|
|
221
231
|
: undefined;
|
|
222
232
|
|
|
223
|
-
|
|
233
|
+
const merged = new TxSimulationResult(
|
|
224
234
|
baseResult.privateExecutionResult,
|
|
225
235
|
baseResult.publicInputs,
|
|
226
236
|
mergedPublicOutput,
|
|
227
237
|
normalResult?.stats,
|
|
228
238
|
);
|
|
239
|
+
return TxSimulationResultWithAppOffset.fromResultAndOffset(merged, normalResult?.appCallOffset ?? 0);
|
|
229
240
|
}
|