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