@aztec/wallet-sdk 0.0.1-commit.9372f48 → 0.0.1-commit.949a33fd8
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 +79 -34
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +238 -65
- package/dest/base-wallet/index.d.ts +3 -2
- 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 +50 -0
- package/dest/base-wallet/utils.d.ts.map +1 -0
- package/dest/base-wallet/utils.js +133 -0
- 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 +19 -9
- package/src/base-wallet/base_wallet.ts +330 -116
- package/src/base-wallet/index.ts +7 -1
- package/src/base-wallet/utils.ts +240 -0
- 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,26 +40,33 @@ 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,
|
|
46
|
+
type NodeInfo,
|
|
41
47
|
computePartialAddress,
|
|
42
48
|
getContractClassFromArtifact,
|
|
43
49
|
} from '@aztec/stdlib/contract';
|
|
44
50
|
import { SimulationError } from '@aztec/stdlib/errors';
|
|
45
|
-
import { Gas, GasSettings } from '@aztec/stdlib/gas';
|
|
46
|
-
import {
|
|
51
|
+
import { Gas, GasFees, GasSettings, ManaUsageEstimate } from '@aztec/stdlib/gas';
|
|
52
|
+
import {
|
|
53
|
+
computeSiloedPrivateInitializationNullifier,
|
|
54
|
+
computeSiloedPublicInitializationNullifier,
|
|
55
|
+
} from '@aztec/stdlib/hash';
|
|
47
56
|
import type { AztecNode } from '@aztec/stdlib/interfaces/client';
|
|
48
|
-
import
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
import {
|
|
58
|
+
BlockHeader,
|
|
59
|
+
ExecutionPayload,
|
|
60
|
+
type TxExecutionRequest,
|
|
61
|
+
type TxProfileResult,
|
|
62
|
+
type UtilityExecutionResult,
|
|
63
|
+
mergeExecutionPayloads,
|
|
53
64
|
} from '@aztec/stdlib/tx';
|
|
54
|
-
import { ExecutionPayload, mergeExecutionPayloads } from '@aztec/stdlib/tx';
|
|
55
65
|
|
|
56
66
|
import { inspect } from 'util';
|
|
57
67
|
|
|
68
|
+
import { buildMergedSimulationResult, extractOptimizablePublicStaticCalls, simulateViaNode } from './utils.js';
|
|
69
|
+
|
|
58
70
|
/**
|
|
59
71
|
* Options to configure fee payment for a transaction
|
|
60
72
|
*/
|
|
@@ -65,26 +77,71 @@ export type FeeOptions = {
|
|
|
65
77
|
*/
|
|
66
78
|
walletFeePaymentMethod?: FeePaymentMethod;
|
|
67
79
|
/** Configuration options for the account to properly handle the selected fee payment method */
|
|
68
|
-
accountFeePaymentMethodOptions
|
|
80
|
+
accountFeePaymentMethodOptions?: AccountFeePaymentMethodOptions;
|
|
69
81
|
/** The gas settings to use for the transaction */
|
|
70
82
|
gasSettings: GasSettings;
|
|
71
83
|
};
|
|
72
84
|
|
|
85
|
+
/** Options for `simulateViaEntrypoint`. */
|
|
86
|
+
export type SimulateViaEntrypointOptions = Pick<
|
|
87
|
+
SimulateOptions,
|
|
88
|
+
'from' | 'additionalScopes' | 'skipTxValidation' | 'skipFeeEnforcement' | 'sendMessagesAs'
|
|
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
|
+
|
|
73
111
|
/**
|
|
74
112
|
* A base class for Wallet implementations
|
|
75
113
|
*/
|
|
76
114
|
export abstract class BaseWallet implements Wallet {
|
|
77
|
-
protected log = createLogger('wallet-sdk:base_wallet');
|
|
78
|
-
|
|
79
115
|
protected minFeePadding = 0.5;
|
|
80
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;
|
|
81
120
|
|
|
82
121
|
// Protected because we want to force wallets to instantiate their own PXE.
|
|
83
122
|
protected constructor(
|
|
84
123
|
protected readonly pxe: PXE,
|
|
85
124
|
protected readonly aztecNode: AztecNode,
|
|
125
|
+
protected log = createLogger('wallet-sdk:base_wallet'),
|
|
86
126
|
) {}
|
|
87
127
|
|
|
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);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Picks the sender address PXE should tag private messages with. Returns `undefined` when there is no signing
|
|
136
|
+
* account (`from === NO_FROM`) and no explicit override; in that case any private log emitted by the tx will fail
|
|
137
|
+
* the contract-side `Sender for tags is not set` assertion unless `set_sender_for_tags` is called first.
|
|
138
|
+
* @param from - Tx sender, or `NO_FROM`.
|
|
139
|
+
* @param sendMessagesAs - Explicit override.
|
|
140
|
+
*/
|
|
141
|
+
protected senderForTagsFrom(from: AztecAddress | NoFrom, sendMessagesAs?: AztecAddress): AztecAddress | undefined {
|
|
142
|
+
return sendMessagesAs ?? (from === NO_FROM ? undefined : from);
|
|
143
|
+
}
|
|
144
|
+
|
|
88
145
|
protected abstract getAccountFromAddress(address: AztecAddress): Promise<Account>;
|
|
89
146
|
|
|
90
147
|
abstract getAccounts(): Promise<Aliased<AztecAddress>[]>;
|
|
@@ -102,32 +159,42 @@ export abstract class BaseWallet implements Wallet {
|
|
|
102
159
|
}
|
|
103
160
|
|
|
104
161
|
async getChainInfo(): Promise<ChainInfo> {
|
|
105
|
-
|
|
162
|
+
if (!this.nodeInfoPromise) {
|
|
163
|
+
this.nodeInfoPromise = this.aztecNode.getNodeInfo();
|
|
164
|
+
}
|
|
165
|
+
const { l1ChainId, rollupVersion } = await this.nodeInfoPromise;
|
|
106
166
|
return { chainId: new Fr(l1ChainId), version: new Fr(rollupVersion) };
|
|
107
167
|
}
|
|
108
168
|
|
|
109
169
|
protected async createTxExecutionRequestFromPayloadAndFee(
|
|
110
170
|
executionPayload: ExecutionPayload,
|
|
111
|
-
from: AztecAddress,
|
|
171
|
+
from: AztecAddress | NoFrom,
|
|
112
172
|
feeOptions: FeeOptions,
|
|
113
173
|
): Promise<TxExecutionRequest> {
|
|
114
174
|
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
115
|
-
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
116
|
-
txNonce: Fr.random(),
|
|
117
|
-
cancellable: this.cancellableTransactions,
|
|
118
|
-
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions,
|
|
119
|
-
};
|
|
120
175
|
const finalExecutionPayload = feeExecutionPayload
|
|
121
176
|
? mergeExecutionPayloads([feeExecutionPayload, executionPayload])
|
|
122
177
|
: executionPayload;
|
|
123
|
-
const fromAccount = await this.getAccountFromAddress(from);
|
|
124
178
|
const chainInfo = await this.getChainInfo();
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
chainInfo
|
|
129
|
-
|
|
130
|
-
|
|
179
|
+
|
|
180
|
+
if (from === NO_FROM) {
|
|
181
|
+
const entrypoint = new DefaultEntrypoint();
|
|
182
|
+
return entrypoint.createTxExecutionRequest(finalExecutionPayload, feeOptions.gasSettings, chainInfo);
|
|
183
|
+
} else {
|
|
184
|
+
const fromAccount = await this.getAccountFromAddress(from);
|
|
185
|
+
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
186
|
+
txNonce: Fr.random(),
|
|
187
|
+
cancellable: this.cancellableTransactions,
|
|
188
|
+
// If from is an address, feeOptions include the way the account contract should handle the fee payment
|
|
189
|
+
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions!,
|
|
190
|
+
};
|
|
191
|
+
return fromAccount.createTxExecutionRequest(
|
|
192
|
+
finalExecutionPayload,
|
|
193
|
+
feeOptions.gasSettings,
|
|
194
|
+
chainInfo,
|
|
195
|
+
executionOptions,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
131
198
|
}
|
|
132
199
|
|
|
133
200
|
public async createAuthWit(
|
|
@@ -176,31 +243,39 @@ export abstract class BaseWallet implements Wallet {
|
|
|
176
243
|
|
|
177
244
|
/**
|
|
178
245
|
* Completes partial user-provided fee options with wallet defaults.
|
|
179
|
-
* @param
|
|
180
|
-
* @param feePayer - The address paying for fees (if any fee payment method is embedded in the execution payload)
|
|
181
|
-
* @param gasSettings - User-provided partial gas settings
|
|
182
|
-
* @returns - Complete fee options that can be used to create a transaction execution request
|
|
246
|
+
* @param config - Fee completion config.
|
|
183
247
|
*/
|
|
184
|
-
protected async completeFeeOptions(
|
|
185
|
-
from
|
|
186
|
-
feePayer?: AztecAddress,
|
|
187
|
-
gasSettings?: Partial<FieldsOf<GasSettings>>,
|
|
188
|
-
): Promise<FeeOptions> {
|
|
248
|
+
protected async completeFeeOptions(config: CompleteFeeOptionsConfig): Promise<FeeOptions> {
|
|
249
|
+
const { from, feePayer, gasSettings, forEstimation, congestionEstimate } = config;
|
|
189
250
|
const maxFeesPerGas =
|
|
190
|
-
gasSettings?.maxFeesPerGas ?? (await this.
|
|
251
|
+
gasSettings?.maxFeesPerGas ?? (await this.getMinFees(congestionEstimate)).mul(1 + this.minFeePadding);
|
|
191
252
|
let accountFeePaymentMethodOptions;
|
|
192
|
-
//
|
|
193
|
-
//
|
|
194
|
-
if (
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
253
|
+
// If from is an address, we need to determine the appropriate fee payment method options for the
|
|
254
|
+
// account contract entrypoint to use
|
|
255
|
+
if (from !== NO_FROM) {
|
|
256
|
+
if (!feePayer) {
|
|
257
|
+
// The transaction does not include a fee payment method, so we set the flag
|
|
258
|
+
// for the account to use its fee juice balance
|
|
259
|
+
accountFeePaymentMethodOptions = AccountFeePaymentMethodOptions.PREEXISTING_FEE_JUICE;
|
|
260
|
+
} else {
|
|
261
|
+
// The transaction includes fee payment method, so we check if we are the fee payer for it
|
|
262
|
+
// (this can only happen if the embedded payment method is FeeJuiceWithClaim)
|
|
263
|
+
accountFeePaymentMethodOptions = from.equals(feePayer)
|
|
264
|
+
? AccountFeePaymentMethodOptions.FEE_JUICE_WITH_CLAIM
|
|
265
|
+
: AccountFeePaymentMethodOptions.EXTERNAL;
|
|
266
|
+
}
|
|
202
267
|
}
|
|
203
|
-
const
|
|
268
|
+
const gasSettingsOverrides = {
|
|
269
|
+
gasLimits: gasSettings?.gasLimits ? Gas.from(gasSettings.gasLimits) : undefined,
|
|
270
|
+
teardownGasLimits: gasSettings?.teardownGasLimits ? Gas.from(gasSettings.teardownGasLimits) : undefined,
|
|
271
|
+
maxFeesPerGas,
|
|
272
|
+
maxPriorityFeesPerGas: gasSettings?.maxPriorityFeesPerGas ?? GasFees.empty(),
|
|
273
|
+
};
|
|
274
|
+
// When estimating gas (simulation), use high limits so the simulation doesn't run out of gas.
|
|
275
|
+
// When sending for real, use protocol max limits that the network will actually accept.
|
|
276
|
+
const fullGasSettings = forEstimation
|
|
277
|
+
? GasSettings.forEstimation(gasSettingsOverrides)
|
|
278
|
+
: GasSettings.fallback(gasSettingsOverrides);
|
|
204
279
|
this.log.debug(`Using L2 gas settings`, fullGasSettings);
|
|
205
280
|
return {
|
|
206
281
|
gasSettings: fullGasSettings,
|
|
@@ -210,34 +285,25 @@ export abstract class BaseWallet implements Wallet {
|
|
|
210
285
|
}
|
|
211
286
|
|
|
212
287
|
/**
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
* to
|
|
216
|
-
* @param from - The address where the transaction is being sent from
|
|
217
|
-
* @param feePayer - The address paying for fees (if any fee payment method is embedded in the execution payload)
|
|
218
|
-
* @param gasSettings - User-provided partial gas settings
|
|
288
|
+
* Returns the worst-case min fee across predicted future slots.
|
|
289
|
+
* Falls back to getCurrentMinFees if the node doesn't support getPredictedMinFees.
|
|
290
|
+
* @param estimate - The mana usage estimate to use for fee prediction. Defaults to Limit for conservative estimation.
|
|
219
291
|
*/
|
|
220
|
-
protected async
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
maxPriorityFeesPerGas,
|
|
236
|
-
);
|
|
237
|
-
return {
|
|
238
|
-
...defaultFeeOptions,
|
|
239
|
-
gasSettings: gasSettingsForEstimation,
|
|
240
|
-
};
|
|
292
|
+
protected async getMinFees(estimate: ManaUsageEstimate = ManaUsageEstimate.Limit): Promise<GasFees> {
|
|
293
|
+
try {
|
|
294
|
+
const predicted = await this.aztecNode.getPredictedMinFees(estimate);
|
|
295
|
+
if (predicted.length === 0) {
|
|
296
|
+
return this.aztecNode.getCurrentMinFees();
|
|
297
|
+
}
|
|
298
|
+
return predicted.reduce((worst, fees) => (fees.feePerL2Gas > worst.feePerL2Gas ? fees : worst));
|
|
299
|
+
} catch (err: any) {
|
|
300
|
+
// Fallback for old nodes that don't support getPredictedMinFees.
|
|
301
|
+
// Only fall back on method-not-found errors (JSON-RPC code -32601); rethrow others.
|
|
302
|
+
if (err?.cause?.code === -32601 || err?.message?.includes('Method not found')) {
|
|
303
|
+
return this.aztecNode.getCurrentMinFees();
|
|
304
|
+
}
|
|
305
|
+
throw err;
|
|
306
|
+
}
|
|
241
307
|
}
|
|
242
308
|
|
|
243
309
|
registerSender(address: AztecAddress, _alias: string = ''): Promise<AztecAddress> {
|
|
@@ -282,32 +348,138 @@ export abstract class BaseWallet implements Wallet {
|
|
|
282
348
|
return instance;
|
|
283
349
|
}
|
|
284
350
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
opts
|
|
294
|
-
opts
|
|
351
|
+
/**
|
|
352
|
+
* Simulates calls through the standard PXE path (account entrypoint).
|
|
353
|
+
* @param executionPayload - The execution payload to simulate.
|
|
354
|
+
* @param opts - Simulation options.
|
|
355
|
+
*/
|
|
356
|
+
protected async simulateViaEntrypoint(executionPayload: ExecutionPayload, opts: SimulateViaEntrypointOptions) {
|
|
357
|
+
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(
|
|
358
|
+
executionPayload,
|
|
359
|
+
opts.from,
|
|
360
|
+
opts.feeOptions,
|
|
295
361
|
);
|
|
362
|
+
const result = await this.pxe.simulateTx(txRequest, {
|
|
363
|
+
simulatePublic: true,
|
|
364
|
+
skipTxValidation: opts.skipTxValidation,
|
|
365
|
+
skipFeeEnforcement: opts.skipFeeEnforcement,
|
|
366
|
+
scopes: this.scopesFrom(opts.from, opts.additionalScopes),
|
|
367
|
+
senderForTags: this.senderForTagsFrom(opts.from, opts.sendMessagesAs),
|
|
368
|
+
});
|
|
369
|
+
const appCallOffset = await this.computeAppCallOffset(opts.from, opts.feeOptions);
|
|
370
|
+
return TxSimulationResultWithAppOffset.fromResultAndOffset(result, appCallOffset);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Computes the index where the app's calls begin in the flattened array of calls (0 = entrypoint/root, 1..N = fee
|
|
375
|
+
* calls, N+1 = app).
|
|
376
|
+
* @param from - The sender address, or NO_FROM for the default entrypoint.
|
|
377
|
+
* @param feeOptions - Fee options containing the wallet fee payment method.
|
|
378
|
+
*/
|
|
379
|
+
protected async computeAppCallOffset(from: AztecAddress | NoFrom, feeOptions: FeeOptions): Promise<number> {
|
|
380
|
+
if (from === NO_FROM) {
|
|
381
|
+
return 0;
|
|
382
|
+
}
|
|
383
|
+
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
384
|
+
return (feeExecutionPayload?.calls.length ?? 0) + 1; // +1 for entrypoint
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Simulates a transaction, optimizing leading public static calls by running them directly
|
|
389
|
+
* on the node while sending the remaining calls through the standard PXE path.
|
|
390
|
+
* Return values from both paths are merged back in original call order.
|
|
391
|
+
* @param executionPayload - The execution payload to simulate.
|
|
392
|
+
* @param opts - Simulation options (from address, fee settings, etc.).
|
|
393
|
+
* @returns The merged simulation result.
|
|
394
|
+
*/
|
|
395
|
+
async simulateTx(
|
|
396
|
+
executionPayload: ExecutionPayload,
|
|
397
|
+
opts: SimulateOptions,
|
|
398
|
+
): Promise<TxSimulationResultWithAppOffset> {
|
|
399
|
+
const feeOptions = await this.completeFeeOptions({
|
|
400
|
+
from: opts.from,
|
|
401
|
+
feePayer: executionPayload.feePayer,
|
|
402
|
+
gasSettings: opts.fee?.gasSettings,
|
|
403
|
+
forEstimation: true,
|
|
404
|
+
congestionEstimate: opts.fee?.congestionEstimate,
|
|
405
|
+
});
|
|
406
|
+
const { optimizableCalls, remainingCalls } = extractOptimizablePublicStaticCalls(executionPayload);
|
|
407
|
+
const remainingPayload = { ...executionPayload, calls: remainingCalls };
|
|
408
|
+
|
|
409
|
+
const chainInfo = await this.getChainInfo();
|
|
410
|
+
let blockHeader: BlockHeader;
|
|
411
|
+
// PXE might not be synced yet, so we pull the latest header from the node
|
|
412
|
+
// To keep things consistent, we'll always try with PXE first
|
|
413
|
+
try {
|
|
414
|
+
blockHeader = await this.pxe.getSyncedBlockHeader();
|
|
415
|
+
} catch {
|
|
416
|
+
blockHeader = (await this.aztecNode.getBlockHeader('latest'))!;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const simulationOrigin = opts.from === NO_FROM ? AztecAddress.ZERO : opts.from;
|
|
420
|
+
const [optimizedResults, normalResult] = await Promise.all([
|
|
421
|
+
optimizableCalls.length > 0
|
|
422
|
+
? simulateViaNode(
|
|
423
|
+
this.aztecNode,
|
|
424
|
+
optimizableCalls,
|
|
425
|
+
simulationOrigin,
|
|
426
|
+
chainInfo,
|
|
427
|
+
feeOptions.gasSettings,
|
|
428
|
+
blockHeader,
|
|
429
|
+
opts.skipFeeEnforcement ?? true,
|
|
430
|
+
this.getContractName.bind(this),
|
|
431
|
+
)
|
|
432
|
+
: Promise.resolve([]),
|
|
433
|
+
remainingCalls.length > 0
|
|
434
|
+
? this.simulateViaEntrypoint(remainingPayload, {
|
|
435
|
+
from: opts.from,
|
|
436
|
+
feeOptions,
|
|
437
|
+
additionalScopes: opts.additionalScopes,
|
|
438
|
+
skipTxValidation: opts.skipTxValidation,
|
|
439
|
+
skipFeeEnforcement: opts.skipFeeEnforcement ?? true,
|
|
440
|
+
sendMessagesAs: opts.sendMessagesAs,
|
|
441
|
+
})
|
|
442
|
+
: Promise.resolve(null),
|
|
443
|
+
]);
|
|
444
|
+
|
|
445
|
+
return buildMergedSimulationResult(optimizedResults, normalResult);
|
|
296
446
|
}
|
|
297
447
|
|
|
298
448
|
async profileTx(executionPayload: ExecutionPayload, opts: ProfileOptions): Promise<TxProfileResult> {
|
|
299
|
-
const feeOptions = await this.completeFeeOptions(
|
|
449
|
+
const feeOptions = await this.completeFeeOptions({
|
|
450
|
+
from: opts.from,
|
|
451
|
+
feePayer: executionPayload.feePayer,
|
|
452
|
+
gasSettings: opts.fee?.gasSettings,
|
|
453
|
+
congestionEstimate: opts.fee?.congestionEstimate,
|
|
454
|
+
});
|
|
300
455
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
301
|
-
return this.pxe.profileTx(txRequest,
|
|
456
|
+
return this.pxe.profileTx(txRequest, {
|
|
457
|
+
profileMode: opts.profileMode,
|
|
458
|
+
skipProofGeneration: opts.skipProofGeneration ?? true,
|
|
459
|
+
scopes: this.scopesFrom(opts.from, opts.additionalScopes),
|
|
460
|
+
senderForTags: this.senderForTagsFrom(opts.from, opts.sendMessagesAs),
|
|
461
|
+
});
|
|
302
462
|
}
|
|
303
463
|
|
|
304
464
|
public async sendTx<W extends InteractionWaitOptions = undefined>(
|
|
305
465
|
executionPayload: ExecutionPayload,
|
|
306
466
|
opts: SendOptions<W>,
|
|
307
467
|
): Promise<SendReturn<W>> {
|
|
308
|
-
const feeOptions = await this.completeFeeOptions(
|
|
468
|
+
const feeOptions = await this.completeFeeOptions({
|
|
469
|
+
from: opts.from,
|
|
470
|
+
feePayer: executionPayload.feePayer,
|
|
471
|
+
gasSettings: opts.fee?.gasSettings,
|
|
472
|
+
congestionEstimate: opts.fee?.congestionEstimate,
|
|
473
|
+
});
|
|
309
474
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
310
|
-
const provenTx = await this.pxe.proveTx(txRequest
|
|
475
|
+
const provenTx = await this.pxe.proveTx(txRequest, {
|
|
476
|
+
scopes: this.scopesFrom(opts.from, opts.additionalScopes),
|
|
477
|
+
senderForTags: this.senderForTagsFrom(opts.from, opts.sendMessagesAs),
|
|
478
|
+
});
|
|
479
|
+
const offchainOutput = extractOffchainOutput(
|
|
480
|
+
provenTx.getOffchainEffects(),
|
|
481
|
+
provenTx.publicInputs.constants.anchorBlockHeader.globalVariables.timestamp,
|
|
482
|
+
);
|
|
311
483
|
const tx = await provenTx.toTx();
|
|
312
484
|
const txHash = tx.getTxHash();
|
|
313
485
|
if (await this.aztecNode.getTxEffect(txHash)) {
|
|
@@ -321,12 +493,32 @@ export abstract class BaseWallet implements Wallet {
|
|
|
321
493
|
|
|
322
494
|
// If wait is NO_WAIT, return txHash immediately
|
|
323
495
|
if (opts.wait === NO_WAIT) {
|
|
324
|
-
return txHash as SendReturn<W>;
|
|
496
|
+
return { txHash, ...offchainOutput } as SendReturn<W>;
|
|
325
497
|
}
|
|
326
498
|
|
|
327
499
|
// Otherwise, wait for the full receipt (default behavior on wait: undefined)
|
|
328
500
|
const waitOpts = typeof opts.wait === 'object' ? opts.wait : undefined;
|
|
329
|
-
|
|
501
|
+
const receipt = await waitForTx(this.aztecNode, txHash, waitOpts);
|
|
502
|
+
|
|
503
|
+
// Display debug logs from public execution if present (served in test mode only)
|
|
504
|
+
if (receipt.debugLogs?.length) {
|
|
505
|
+
await displayDebugLogs(receipt.debugLogs, this.getContractName.bind(this));
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
return { receipt, ...offchainOutput } as SendReturn<W>;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Resolves a contract address to a human-readable name via PXE, if available.
|
|
513
|
+
* @param address - The contract address to resolve.
|
|
514
|
+
*/
|
|
515
|
+
protected async getContractName(address: AztecAddress): Promise<string | undefined> {
|
|
516
|
+
const instance = await this.pxe.getContractInstance(address);
|
|
517
|
+
if (!instance) {
|
|
518
|
+
return undefined;
|
|
519
|
+
}
|
|
520
|
+
const artifact = await this.pxe.getContractArtifact(instance.currentContractClassId);
|
|
521
|
+
return artifact?.name;
|
|
330
522
|
}
|
|
331
523
|
|
|
332
524
|
protected contextualizeError(err: Error, ...context: string[]): Error {
|
|
@@ -343,8 +535,8 @@ export abstract class BaseWallet implements Wallet {
|
|
|
343
535
|
return err;
|
|
344
536
|
}
|
|
345
537
|
|
|
346
|
-
|
|
347
|
-
return this.pxe.
|
|
538
|
+
executeUtility(call: FunctionCall, opts: ExecuteUtilityOptions): Promise<UtilityExecutionResult> {
|
|
539
|
+
return this.pxe.executeUtility(call, { authwits: opts.authWitnesses, scopes: opts.scopes });
|
|
348
540
|
}
|
|
349
541
|
|
|
350
542
|
async getPrivateEvents<T>(
|
|
@@ -367,17 +559,39 @@ export abstract class BaseWallet implements Wallet {
|
|
|
367
559
|
return decodedEvents;
|
|
368
560
|
}
|
|
369
561
|
|
|
562
|
+
/**
|
|
563
|
+
* Returns metadata about a contract, including whether it has been initialized, published, and updated.
|
|
564
|
+
* @param address - The contract address to query.
|
|
565
|
+
*/
|
|
370
566
|
async getContractMetadata(address: AztecAddress) {
|
|
371
567
|
const instance = await this.pxe.getContractInstance(address);
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
|
|
568
|
+
const publiclyRegisteredContractPromise = this.aztecNode.getContract(address);
|
|
569
|
+
|
|
570
|
+
let initializationStatus: ContractInitializationStatus;
|
|
571
|
+
if (instance) {
|
|
572
|
+
// We have the instance, so we can compute the private initialization nullifier (which includes init_hash and is
|
|
573
|
+
// emitted by both private and public initializers) and get a definitive INITIALIZED/UNINITIALIZED answer.
|
|
574
|
+
const initNullifier = await computeSiloedPrivateInitializationNullifier(address, instance.initializationHash);
|
|
575
|
+
const witness = await this.aztecNode.getNullifierMembershipWitness('latest', initNullifier);
|
|
576
|
+
initializationStatus = witness
|
|
577
|
+
? ContractInitializationStatus.INITIALIZED
|
|
578
|
+
: ContractInitializationStatus.UNINITIALIZED;
|
|
579
|
+
} else {
|
|
580
|
+
// Without the instance we lack the init_hash needed for the private nullifier. We fall back to checking the
|
|
581
|
+
// public initialization nullifier (computed from address alone). Not all contracts emit it (only those with
|
|
582
|
+
// public functions that require initialization checks), so its absence doesn't mean the contract is
|
|
583
|
+
// uninitialized.
|
|
584
|
+
const publicNullifier = await computeSiloedPublicInitializationNullifier(address);
|
|
585
|
+
const witness = await this.aztecNode.getNullifierMembershipWitness('latest', publicNullifier);
|
|
586
|
+
initializationStatus = witness ? ContractInitializationStatus.INITIALIZED : ContractInitializationStatus.UNKNOWN;
|
|
587
|
+
}
|
|
588
|
+
const publiclyRegisteredContract = await publiclyRegisteredContractPromise;
|
|
375
589
|
const isContractUpdated =
|
|
376
590
|
publiclyRegisteredContract &&
|
|
377
591
|
!publiclyRegisteredContract.currentContractClassId.equals(publiclyRegisteredContract.originalContractClassId);
|
|
378
592
|
return {
|
|
379
593
|
instance: instance ?? undefined,
|
|
380
|
-
|
|
594
|
+
initializationStatus,
|
|
381
595
|
isContractPublished: !!publiclyRegisteredContract,
|
|
382
596
|
isContractUpdated: !!isContractUpdated,
|
|
383
597
|
updatedContractClassId: isContractUpdated ? publiclyRegisteredContract.currentContractClassId : undefined,
|
package/src/base-wallet/index.ts
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {
|
|
2
|
+
BaseWallet,
|
|
3
|
+
type CompleteFeeOptionsConfig,
|
|
4
|
+
type FeeOptions,
|
|
5
|
+
type SimulateViaEntrypointOptions,
|
|
6
|
+
} from './base_wallet.js';
|
|
7
|
+
export { simulateViaNode, buildMergedSimulationResult, extractOptimizablePublicStaticCalls } from './utils.js';
|