@aztec/wallet-sdk 0.0.1-commit.7035c9bd6 → 0.0.1-commit.71324e566
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 +41 -39
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +121 -69
- 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 +3 -2
- package/dest/base-wallet/utils.d.ts.map +1 -1
- package/dest/base-wallet/utils.js +3 -1
- 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 +183 -116
- package/src/base-wallet/index.ts +6 -1
- package/src/base-wallet/utils.ts +6 -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,4 +1,5 @@
|
|
|
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
4
|
import {
|
|
4
5
|
type InteractionWaitOptions,
|
|
@@ -8,32 +9,29 @@ import {
|
|
|
8
9
|
} from '@aztec/aztec.js/contracts';
|
|
9
10
|
import type { FeePaymentMethod } from '@aztec/aztec.js/fee';
|
|
10
11
|
import { waitForTx } from '@aztec/aztec.js/node';
|
|
11
|
-
import type {
|
|
12
|
-
Aliased,
|
|
13
|
-
AppCapabilities,
|
|
14
|
-
BatchResults,
|
|
15
|
-
BatchedMethod,
|
|
16
|
-
ExecuteUtilityOptions,
|
|
17
|
-
PrivateEvent,
|
|
18
|
-
PrivateEventFilter,
|
|
19
|
-
ProfileOptions,
|
|
20
|
-
SendOptions,
|
|
21
|
-
SimulateOptions,
|
|
22
|
-
Wallet,
|
|
23
|
-
WalletCapabilities,
|
|
24
|
-
} from '@aztec/aztec.js/wallet';
|
|
25
12
|
import {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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';
|
|
31
28
|
import { AccountFeePaymentMethodOptions, type DefaultAccountEntrypointOptions } from '@aztec/entrypoints/account';
|
|
29
|
+
import { DefaultEntrypoint } from '@aztec/entrypoints/default';
|
|
32
30
|
import type { ChainInfo } from '@aztec/entrypoints/interfaces';
|
|
33
31
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
34
32
|
import { createLogger } from '@aztec/foundation/log';
|
|
35
33
|
import type { FieldsOf } from '@aztec/foundation/types';
|
|
36
|
-
import {
|
|
34
|
+
import { displayDebugLogs } from '@aztec/pxe/client/lazy';
|
|
37
35
|
import type { PXE, PackedPrivateEvent } from '@aztec/pxe/server';
|
|
38
36
|
import {
|
|
39
37
|
type ContractArtifact,
|
|
@@ -45,21 +43,25 @@ import type { AuthWitness } from '@aztec/stdlib/auth-witness';
|
|
|
45
43
|
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
46
44
|
import {
|
|
47
45
|
type ContractInstanceWithAddress,
|
|
46
|
+
type NodeInfo,
|
|
48
47
|
computePartialAddress,
|
|
49
48
|
getContractClassFromArtifact,
|
|
50
49
|
} from '@aztec/stdlib/contract';
|
|
51
50
|
import { SimulationError } from '@aztec/stdlib/errors';
|
|
52
|
-
import { Gas, GasSettings } from '@aztec/stdlib/gas';
|
|
53
|
-
import {
|
|
51
|
+
import { Gas, GasFees, GasSettings, ManaUsageEstimate } from '@aztec/stdlib/gas';
|
|
52
|
+
import {
|
|
53
|
+
computeSiloedPrivateInitializationNullifier,
|
|
54
|
+
computeSiloedPublicInitializationNullifier,
|
|
55
|
+
} from '@aztec/stdlib/hash';
|
|
54
56
|
import type { AztecNode } from '@aztec/stdlib/interfaces/client';
|
|
55
57
|
import {
|
|
56
58
|
BlockHeader,
|
|
59
|
+
ExecutionPayload,
|
|
57
60
|
type TxExecutionRequest,
|
|
58
61
|
type TxProfileResult,
|
|
59
|
-
TxSimulationResult,
|
|
60
62
|
type UtilityExecutionResult,
|
|
63
|
+
mergeExecutionPayloads,
|
|
61
64
|
} from '@aztec/stdlib/tx';
|
|
62
|
-
import { ExecutionPayload, mergeExecutionPayloads } from '@aztec/stdlib/tx';
|
|
63
65
|
|
|
64
66
|
import { inspect } from 'util';
|
|
65
67
|
|
|
@@ -75,7 +77,7 @@ export type FeeOptions = {
|
|
|
75
77
|
*/
|
|
76
78
|
walletFeePaymentMethod?: FeePaymentMethod;
|
|
77
79
|
/** Configuration options for the account to properly handle the selected fee payment method */
|
|
78
|
-
accountFeePaymentMethodOptions
|
|
80
|
+
accountFeePaymentMethodOptions?: AccountFeePaymentMethodOptions;
|
|
79
81
|
/** The gas settings to use for the transaction */
|
|
80
82
|
gasSettings: GasSettings;
|
|
81
83
|
};
|
|
@@ -87,15 +89,34 @@ export type SimulateViaEntrypointOptions = Pick<
|
|
|
87
89
|
> & {
|
|
88
90
|
/** Fee options for the entrypoint */
|
|
89
91
|
feeOptions: FeeOptions;
|
|
90
|
-
/** Scopes to use for the simulation */
|
|
91
|
-
scopes: AccessScopes;
|
|
92
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
|
+
|
|
93
111
|
/**
|
|
94
112
|
* A base class for Wallet implementations
|
|
95
113
|
*/
|
|
96
114
|
export abstract class BaseWallet implements Wallet {
|
|
97
115
|
protected minFeePadding = 0.5;
|
|
98
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;
|
|
99
120
|
|
|
100
121
|
// Protected because we want to force wallets to instantiate their own PXE.
|
|
101
122
|
protected constructor(
|
|
@@ -104,8 +125,8 @@ export abstract class BaseWallet implements Wallet {
|
|
|
104
125
|
protected log = createLogger('wallet-sdk:base_wallet'),
|
|
105
126
|
) {}
|
|
106
127
|
|
|
107
|
-
protected scopesFrom(from: AztecAddress, additionalScopes: AztecAddress[] = []): AztecAddress[] {
|
|
108
|
-
const allScopes = from
|
|
128
|
+
protected scopesFrom(from: AztecAddress | NoFrom, additionalScopes: AztecAddress[] = []): AztecAddress[] {
|
|
129
|
+
const allScopes = from === NO_FROM ? additionalScopes : [from, ...additionalScopes];
|
|
109
130
|
const scopeSet = new Set(allScopes.map(address => address.toString()));
|
|
110
131
|
return [...scopeSet].map(AztecAddress.fromString);
|
|
111
132
|
}
|
|
@@ -127,32 +148,42 @@ export abstract class BaseWallet implements Wallet {
|
|
|
127
148
|
}
|
|
128
149
|
|
|
129
150
|
async getChainInfo(): Promise<ChainInfo> {
|
|
130
|
-
|
|
151
|
+
if (!this.nodeInfoPromise) {
|
|
152
|
+
this.nodeInfoPromise = this.aztecNode.getNodeInfo();
|
|
153
|
+
}
|
|
154
|
+
const { l1ChainId, rollupVersion } = await this.nodeInfoPromise;
|
|
131
155
|
return { chainId: new Fr(l1ChainId), version: new Fr(rollupVersion) };
|
|
132
156
|
}
|
|
133
157
|
|
|
134
158
|
protected async createTxExecutionRequestFromPayloadAndFee(
|
|
135
159
|
executionPayload: ExecutionPayload,
|
|
136
|
-
from: AztecAddress,
|
|
160
|
+
from: AztecAddress | NoFrom,
|
|
137
161
|
feeOptions: FeeOptions,
|
|
138
162
|
): Promise<TxExecutionRequest> {
|
|
139
163
|
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
140
|
-
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
141
|
-
txNonce: Fr.random(),
|
|
142
|
-
cancellable: this.cancellableTransactions,
|
|
143
|
-
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions,
|
|
144
|
-
};
|
|
145
164
|
const finalExecutionPayload = feeExecutionPayload
|
|
146
165
|
? mergeExecutionPayloads([feeExecutionPayload, executionPayload])
|
|
147
166
|
: executionPayload;
|
|
148
|
-
const fromAccount = await this.getAccountFromAddress(from);
|
|
149
167
|
const chainInfo = await this.getChainInfo();
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
chainInfo
|
|
154
|
-
|
|
155
|
-
|
|
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
|
+
}
|
|
156
187
|
}
|
|
157
188
|
|
|
158
189
|
public async createAuthWit(
|
|
@@ -201,31 +232,39 @@ export abstract class BaseWallet implements Wallet {
|
|
|
201
232
|
|
|
202
233
|
/**
|
|
203
234
|
* Completes partial user-provided fee options with wallet defaults.
|
|
204
|
-
* @param
|
|
205
|
-
* @param feePayer - The address paying for fees (if any fee payment method is embedded in the execution payload)
|
|
206
|
-
* @param gasSettings - User-provided partial gas settings
|
|
207
|
-
* @returns - Complete fee options that can be used to create a transaction execution request
|
|
235
|
+
* @param config - Fee completion config.
|
|
208
236
|
*/
|
|
209
|
-
protected async completeFeeOptions(
|
|
210
|
-
from
|
|
211
|
-
feePayer?: AztecAddress,
|
|
212
|
-
gasSettings?: Partial<FieldsOf<GasSettings>>,
|
|
213
|
-
): Promise<FeeOptions> {
|
|
237
|
+
protected async completeFeeOptions(config: CompleteFeeOptionsConfig): Promise<FeeOptions> {
|
|
238
|
+
const { from, feePayer, gasSettings, forEstimation, congestionEstimate } = config;
|
|
214
239
|
const maxFeesPerGas =
|
|
215
|
-
gasSettings?.maxFeesPerGas ?? (await this.
|
|
240
|
+
gasSettings?.maxFeesPerGas ?? (await this.getMinFees(congestionEstimate)).mul(1 + this.minFeePadding);
|
|
216
241
|
let accountFeePaymentMethodOptions;
|
|
217
|
-
//
|
|
218
|
-
//
|
|
219
|
-
if (
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
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
|
+
}
|
|
227
256
|
}
|
|
228
|
-
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);
|
|
229
268
|
this.log.debug(`Using L2 gas settings`, fullGasSettings);
|
|
230
269
|
return {
|
|
231
270
|
gasSettings: fullGasSettings,
|
|
@@ -235,34 +274,25 @@ export abstract class BaseWallet implements Wallet {
|
|
|
235
274
|
}
|
|
236
275
|
|
|
237
276
|
/**
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
* to
|
|
241
|
-
* @param from - The address where the transaction is being sent from
|
|
242
|
-
* @param feePayer - The address paying for fees (if any fee payment method is embedded in the execution payload)
|
|
243
|
-
* @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.
|
|
244
280
|
*/
|
|
245
|
-
protected async
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
maxPriorityFeesPerGas,
|
|
261
|
-
);
|
|
262
|
-
return {
|
|
263
|
-
...defaultFeeOptions,
|
|
264
|
-
gasSettings: gasSettingsForEstimation,
|
|
265
|
-
};
|
|
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
|
+
}
|
|
266
296
|
}
|
|
267
297
|
|
|
268
298
|
registerSender(address: AztecAddress, _alias: string = ''): Promise<AztecAddress> {
|
|
@@ -318,12 +348,28 @@ export abstract class BaseWallet implements Wallet {
|
|
|
318
348
|
opts.from,
|
|
319
349
|
opts.feeOptions,
|
|
320
350
|
);
|
|
321
|
-
|
|
351
|
+
const result = await this.pxe.simulateTx(txRequest, {
|
|
322
352
|
simulatePublic: true,
|
|
323
353
|
skipTxValidation: opts.skipTxValidation,
|
|
324
354
|
skipFeeEnforcement: opts.skipFeeEnforcement,
|
|
325
|
-
scopes: opts.
|
|
355
|
+
scopes: this.scopesFrom(opts.from, opts.additionalScopes),
|
|
326
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
|
|
327
373
|
}
|
|
328
374
|
|
|
329
375
|
/**
|
|
@@ -334,10 +380,17 @@ export abstract class BaseWallet implements Wallet {
|
|
|
334
380
|
* @param opts - Simulation options (from address, fee settings, etc.).
|
|
335
381
|
* @returns The merged simulation result.
|
|
336
382
|
*/
|
|
337
|
-
async simulateTx(
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
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
|
+
});
|
|
341
394
|
const { optimizableCalls, remainingCalls } = extractOptimizablePublicStaticCalls(executionPayload);
|
|
342
395
|
const remainingPayload = { ...executionPayload, calls: remainingCalls };
|
|
343
396
|
|
|
@@ -351,12 +404,13 @@ export abstract class BaseWallet implements Wallet {
|
|
|
351
404
|
blockHeader = (await this.aztecNode.getBlockHeader())!;
|
|
352
405
|
}
|
|
353
406
|
|
|
407
|
+
const simulationOrigin = opts.from === NO_FROM ? AztecAddress.ZERO : opts.from;
|
|
354
408
|
const [optimizedResults, normalResult] = await Promise.all([
|
|
355
409
|
optimizableCalls.length > 0
|
|
356
410
|
? simulateViaNode(
|
|
357
411
|
this.aztecNode,
|
|
358
412
|
optimizableCalls,
|
|
359
|
-
|
|
413
|
+
simulationOrigin,
|
|
360
414
|
chainInfo,
|
|
361
415
|
feeOptions.gasSettings,
|
|
362
416
|
blockHeader,
|
|
@@ -368,7 +422,7 @@ export abstract class BaseWallet implements Wallet {
|
|
|
368
422
|
? this.simulateViaEntrypoint(remainingPayload, {
|
|
369
423
|
from: opts.from,
|
|
370
424
|
feeOptions,
|
|
371
|
-
|
|
425
|
+
additionalScopes: opts.additionalScopes,
|
|
372
426
|
skipTxValidation: opts.skipTxValidation,
|
|
373
427
|
skipFeeEnforcement: opts.skipFeeEnforcement ?? true,
|
|
374
428
|
})
|
|
@@ -379,7 +433,12 @@ export abstract class BaseWallet implements Wallet {
|
|
|
379
433
|
}
|
|
380
434
|
|
|
381
435
|
async profileTx(executionPayload: ExecutionPayload, opts: ProfileOptions): Promise<TxProfileResult> {
|
|
382
|
-
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
|
+
});
|
|
383
442
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
384
443
|
return this.pxe.profileTx(txRequest, {
|
|
385
444
|
profileMode: opts.profileMode,
|
|
@@ -392,7 +451,12 @@ export abstract class BaseWallet implements Wallet {
|
|
|
392
451
|
executionPayload: ExecutionPayload,
|
|
393
452
|
opts: SendOptions<W>,
|
|
394
453
|
): Promise<SendReturn<W>> {
|
|
395
|
-
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
|
+
});
|
|
396
460
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
397
461
|
const provenTx = await this.pxe.proveTx(txRequest, this.scopesFrom(opts.from, opts.additionalScopes));
|
|
398
462
|
const offchainOutput = extractOffchainOutput(
|
|
@@ -455,7 +519,7 @@ export abstract class BaseWallet implements Wallet {
|
|
|
455
519
|
}
|
|
456
520
|
|
|
457
521
|
executeUtility(call: FunctionCall, opts: ExecuteUtilityOptions): Promise<UtilityExecutionResult> {
|
|
458
|
-
return this.pxe.executeUtility(call, { authwits: opts.authWitnesses, scopes:
|
|
522
|
+
return this.pxe.executeUtility(call, { authwits: opts.authWitnesses, scopes: opts.scopes });
|
|
459
523
|
}
|
|
460
524
|
|
|
461
525
|
async getPrivateEvents<T>(
|
|
@@ -480,26 +544,29 @@ export abstract class BaseWallet implements Wallet {
|
|
|
480
544
|
|
|
481
545
|
/**
|
|
482
546
|
* Returns metadata about a contract, including whether it has been initialized, published, and updated.
|
|
483
|
-
*
|
|
484
|
-
* `isContractInitialized` requires the contract instance to be registered in the PXE (for `init_hash`). When the
|
|
485
|
-
* instance is not available, `isContractInitialized` is `undefined` since it cannot be determined.
|
|
486
547
|
* @param address - The contract address to query.
|
|
487
548
|
*/
|
|
488
549
|
async getContractMetadata(address: AztecAddress) {
|
|
489
550
|
const instance = await this.pxe.getContractInstance(address);
|
|
490
551
|
const publiclyRegisteredContractPromise = this.aztecNode.getContract(address);
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
// the instance (and thus init_hash), we can't compute it, so we return undefined.
|
|
494
|
-
//
|
|
495
|
-
// We skip the public initialization nullifier because it's not always emitted (contracts without public external
|
|
496
|
-
// functions that require initialization checks won't emit it). If the private one exists, the public one was
|
|
497
|
-
// created in the same tx and will also be present.
|
|
498
|
-
let isContractInitialized: boolean | undefined = undefined;
|
|
552
|
+
|
|
553
|
+
let initializationStatus: ContractInitializationStatus;
|
|
499
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.
|
|
500
557
|
const initNullifier = await computeSiloedPrivateInitializationNullifier(address, instance.initializationHash);
|
|
501
558
|
const witness = await this.aztecNode.getNullifierMembershipWitness('latest', initNullifier);
|
|
502
|
-
|
|
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;
|
|
503
570
|
}
|
|
504
571
|
const publiclyRegisteredContract = await publiclyRegisteredContractPromise;
|
|
505
572
|
const isContractUpdated =
|
|
@@ -507,7 +574,7 @@ export abstract class BaseWallet implements Wallet {
|
|
|
507
574
|
!publiclyRegisteredContract.currentContractClassId.equals(publiclyRegisteredContract.originalContractClassId);
|
|
508
575
|
return {
|
|
509
576
|
instance: instance ?? undefined,
|
|
510
|
-
|
|
577
|
+
initializationStatus,
|
|
511
578
|
isContractPublished: !!publiclyRegisteredContract,
|
|
512
579
|
isContractUpdated: !!isContractUpdated,
|
|
513
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,4 +1,5 @@
|
|
|
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';
|
|
@@ -214,13 +215,13 @@ export async function simulateViaNode(
|
|
|
214
215
|
*/
|
|
215
216
|
export function buildMergedSimulationResult(
|
|
216
217
|
optimizedResults: TxSimulationResult[],
|
|
217
|
-
normalResult:
|
|
218
|
-
):
|
|
218
|
+
normalResult: TxSimulationResultWithAppOffset | null,
|
|
219
|
+
): TxSimulationResultWithAppOffset {
|
|
219
220
|
const optimizedReturnValues = optimizedResults.flatMap(r => r.publicOutput?.publicReturnValues ?? []);
|
|
220
221
|
const normalReturnValues = normalResult?.publicOutput?.publicReturnValues ?? [];
|
|
221
222
|
const allReturnValues = [...optimizedReturnValues, ...normalReturnValues];
|
|
222
223
|
|
|
223
|
-
const baseResult = normalResult ?? optimizedResults[0];
|
|
224
|
+
const baseResult: TxSimulationResult = normalResult ?? optimizedResults[0];
|
|
224
225
|
|
|
225
226
|
const mergedPublicOutput: PublicSimulationOutput | undefined = baseResult.publicOutput
|
|
226
227
|
? {
|
|
@@ -229,10 +230,11 @@ export function buildMergedSimulationResult(
|
|
|
229
230
|
}
|
|
230
231
|
: undefined;
|
|
231
232
|
|
|
232
|
-
|
|
233
|
+
const merged = new TxSimulationResult(
|
|
233
234
|
baseResult.privateExecutionResult,
|
|
234
235
|
baseResult.publicInputs,
|
|
235
236
|
mergedPublicOutput,
|
|
236
237
|
normalResult?.stats,
|
|
237
238
|
);
|
|
239
|
+
return TxSimulationResultWithAppOffset.fromResultAndOffset(merged, normalResult?.appCallOffset ?? 0);
|
|
238
240
|
}
|
package/src/crypto.ts
CHANGED
|
@@ -497,3 +497,107 @@ export function hashToEmoji(hash: string, count: number = DEFAULT_EMOJI_GRID_SIZ
|
|
|
497
497
|
}
|
|
498
498
|
return emojis.join('');
|
|
499
499
|
}
|
|
500
|
+
|
|
501
|
+
// ─── Passphrase-based encryption (PBKDF2 + AES-256-GCM) ───────────────────
|
|
502
|
+
|
|
503
|
+
/** Default PBKDF2 iteration count. High to compensate for short PINs (~1-2s on modern hardware). */
|
|
504
|
+
const DEFAULT_PBKDF2_ITERATIONS = 2_000_000;
|
|
505
|
+
const PBKDF2_SALT_BYTES = 16;
|
|
506
|
+
const PBKDF2_IV_BYTES = 12;
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* Derives an AES-256-GCM key from a passphrase using PBKDF2-SHA256.
|
|
510
|
+
*
|
|
511
|
+
* @param passphrase - The user-provided passphrase or PIN
|
|
512
|
+
* @param salt - Random salt bytes
|
|
513
|
+
* @param iterations - PBKDF2 iteration count (default: 2,000,000)
|
|
514
|
+
* @returns An AES-256-GCM CryptoKey
|
|
515
|
+
*/
|
|
516
|
+
export async function deriveKeyFromPassphrase(
|
|
517
|
+
passphrase: string,
|
|
518
|
+
salt: Uint8Array,
|
|
519
|
+
iterations: number = DEFAULT_PBKDF2_ITERATIONS,
|
|
520
|
+
): Promise<CryptoKey> {
|
|
521
|
+
const keyMaterial = await crypto.subtle.importKey('raw', new TextEncoder().encode(passphrase), 'PBKDF2', false, [
|
|
522
|
+
'deriveKey',
|
|
523
|
+
]);
|
|
524
|
+
return crypto.subtle.deriveKey(
|
|
525
|
+
{ name: 'PBKDF2', salt: salt as BufferSource, iterations, hash: 'SHA-256' },
|
|
526
|
+
keyMaterial,
|
|
527
|
+
{ name: 'AES-GCM', length: 256 },
|
|
528
|
+
false,
|
|
529
|
+
['encrypt', 'decrypt'],
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Encrypts arbitrary bytes with a passphrase using PBKDF2 + AES-256-GCM.
|
|
535
|
+
*
|
|
536
|
+
* Output layout: `[salt (16)] [iv (12)] [ciphertext (...)]`
|
|
537
|
+
*
|
|
538
|
+
* @param plaintext - Data to encrypt
|
|
539
|
+
* @param passphrase - User passphrase or PIN
|
|
540
|
+
* @param iterations - PBKDF2 iteration count (default: 2,000,000)
|
|
541
|
+
* @returns A Uint8Array containing salt + iv + ciphertext
|
|
542
|
+
*/
|
|
543
|
+
export async function encryptWithPassphrase(
|
|
544
|
+
plaintext: Uint8Array,
|
|
545
|
+
passphrase: string,
|
|
546
|
+
iterations: number = DEFAULT_PBKDF2_ITERATIONS,
|
|
547
|
+
): Promise<Uint8Array> {
|
|
548
|
+
const salt = crypto.getRandomValues(new Uint8Array(PBKDF2_SALT_BYTES));
|
|
549
|
+
const iv = crypto.getRandomValues(new Uint8Array(PBKDF2_IV_BYTES));
|
|
550
|
+
const key = await deriveKeyFromPassphrase(passphrase, salt, iterations);
|
|
551
|
+
const ciphertext = new Uint8Array(
|
|
552
|
+
await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, plaintext as BufferSource),
|
|
553
|
+
);
|
|
554
|
+
const result = new Uint8Array(PBKDF2_SALT_BYTES + PBKDF2_IV_BYTES + ciphertext.length);
|
|
555
|
+
result.set(salt, 0);
|
|
556
|
+
result.set(iv, PBKDF2_SALT_BYTES);
|
|
557
|
+
result.set(ciphertext, PBKDF2_SALT_BYTES + PBKDF2_IV_BYTES);
|
|
558
|
+
return result;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Decrypts data produced by {@link encryptWithPassphrase}.
|
|
563
|
+
*
|
|
564
|
+
* @param data - The encrypted blob (salt + iv + ciphertext)
|
|
565
|
+
* @param passphrase - The passphrase used during encryption
|
|
566
|
+
* @param iterations - PBKDF2 iteration count (must match encryption)
|
|
567
|
+
* @returns The decrypted plaintext bytes
|
|
568
|
+
* @throws On wrong passphrase (AES-GCM auth tag mismatch)
|
|
569
|
+
*/
|
|
570
|
+
export async function decryptWithPassphrase(
|
|
571
|
+
data: Uint8Array,
|
|
572
|
+
passphrase: string,
|
|
573
|
+
iterations: number = DEFAULT_PBKDF2_ITERATIONS,
|
|
574
|
+
): Promise<Uint8Array> {
|
|
575
|
+
const salt = data.slice(0, PBKDF2_SALT_BYTES);
|
|
576
|
+
const iv = data.slice(PBKDF2_SALT_BYTES, PBKDF2_SALT_BYTES + PBKDF2_IV_BYTES);
|
|
577
|
+
const ciphertext = data.slice(PBKDF2_SALT_BYTES + PBKDF2_IV_BYTES);
|
|
578
|
+
const key = await deriveKeyFromPassphrase(passphrase, salt, iterations);
|
|
579
|
+
return new Uint8Array(await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, ciphertext as BufferSource));
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Converts a Uint8Array to a base64 string.
|
|
584
|
+
*/
|
|
585
|
+
export function uint8ToBase64(bytes: Uint8Array): string {
|
|
586
|
+
let binary = '';
|
|
587
|
+
for (const b of bytes) {
|
|
588
|
+
binary += String.fromCharCode(b);
|
|
589
|
+
}
|
|
590
|
+
return btoa(binary);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Converts a base64 string to a Uint8Array.
|
|
595
|
+
*/
|
|
596
|
+
export function base64ToUint8(b64: string): Uint8Array {
|
|
597
|
+
const binary = atob(b64);
|
|
598
|
+
const bytes = new Uint8Array(binary.length);
|
|
599
|
+
for (let i = 0; i < binary.length; i++) {
|
|
600
|
+
bytes[i] = binary.charCodeAt(i);
|
|
601
|
+
}
|
|
602
|
+
return bytes;
|
|
603
|
+
}
|
|
@@ -6,7 +6,7 @@ import { schemaHasMethod } from '@aztec/foundation/schemas';
|
|
|
6
6
|
import type { FunctionsOf } from '@aztec/foundation/types';
|
|
7
7
|
|
|
8
8
|
import { type EncryptedPayload, decrypt, encrypt } from '../../crypto.js';
|
|
9
|
-
import { type WalletMessage, WalletMessageType, type WalletResponse } from '../../types.js';
|
|
9
|
+
import { type DisconnectCallback, type WalletMessage, WalletMessageType, type WalletResponse } from '../../types.js';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Internal type representing a wallet method call before encryption.
|
|
@@ -19,11 +19,6 @@ type WalletMethodCall = {
|
|
|
19
19
|
args: unknown[];
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
/**
|
|
23
|
-
* Callback type for wallet disconnect events.
|
|
24
|
-
*/
|
|
25
|
-
export type DisconnectCallback = () => void;
|
|
26
|
-
|
|
27
22
|
/**
|
|
28
23
|
* A wallet implementation that communicates with browser extension wallets
|
|
29
24
|
* using an encrypted MessageChannel.
|