@aztec/wallet-sdk 0.0.1-commit.f2ce05ee → 0.0.1-commit.f5d02921e
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 +33 -18
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +129 -40
- 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 +7 -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 +4 -6
- package/dest/extension/provider/extension_wallet.d.ts.map +1 -1
- package/dest/extension/provider/extension_wallet.js +9 -2
- package/dest/extension/provider/index.d.ts +2 -2
- package/dest/extension/provider/index.d.ts.map +1 -1
- package/dest/iframe/handlers/iframe_connection_handler.d.ts +118 -0
- package/dest/iframe/handlers/iframe_connection_handler.d.ts.map +1 -0
- package/dest/iframe/handlers/iframe_connection_handler.js +228 -0
- package/dest/iframe/handlers/index.d.ts +2 -0
- package/dest/iframe/handlers/index.d.ts.map +1 -0
- package/dest/iframe/handlers/index.js +1 -0
- package/dest/iframe/provider/iframe_discovery.d.ts +25 -0
- package/dest/iframe/provider/iframe_discovery.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_discovery.js +167 -0
- package/dest/iframe/provider/iframe_provider.d.ts +65 -0
- package/dest/iframe/provider/iframe_provider.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_provider.js +257 -0
- package/dest/iframe/provider/iframe_wallet.d.ts +68 -0
- package/dest/iframe/provider/iframe_wallet.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_wallet.js +200 -0
- package/dest/iframe/provider/index.d.ts +4 -0
- package/dest/iframe/provider/index.d.ts.map +1 -0
- package/dest/iframe/provider/index.js +3 -0
- package/dest/manager/types.d.ts +6 -5
- package/dest/manager/types.d.ts.map +1 -1
- package/dest/manager/wallet_manager.d.ts +1 -1
- package/dest/manager/wallet_manager.d.ts.map +1 -1
- package/dest/manager/wallet_manager.js +48 -18
- package/dest/types.d.ts +14 -2
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +4 -0
- package/package.json +12 -8
- package/src/base-wallet/base_wallet.ts +175 -76
- package/src/base-wallet/index.ts +1 -1
- package/src/base-wallet/utils.ts +9 -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,20 +1,28 @@
|
|
|
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
|
|
7
|
-
Aliased,
|
|
8
|
-
AppCapabilities,
|
|
9
|
-
BatchResults,
|
|
10
|
-
BatchedMethod,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
import {
|
|
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
|
+
type Wallet,
|
|
25
|
+
type WalletCapabilities,
|
|
18
26
|
} from '@aztec/aztec.js/wallet';
|
|
19
27
|
import {
|
|
20
28
|
GAS_ESTIMATION_DA_GAS_LIMIT,
|
|
@@ -23,10 +31,12 @@ import {
|
|
|
23
31
|
GAS_ESTIMATION_TEARDOWN_L2_GAS_LIMIT,
|
|
24
32
|
} from '@aztec/constants';
|
|
25
33
|
import { AccountFeePaymentMethodOptions, type DefaultAccountEntrypointOptions } from '@aztec/entrypoints/account';
|
|
34
|
+
import { DefaultEntrypoint } from '@aztec/entrypoints/default';
|
|
26
35
|
import type { ChainInfo } from '@aztec/entrypoints/interfaces';
|
|
27
36
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
28
37
|
import { createLogger } from '@aztec/foundation/log';
|
|
29
38
|
import type { FieldsOf } from '@aztec/foundation/types';
|
|
39
|
+
import { type AccessScopes, displayDebugLogs } from '@aztec/pxe/client/lazy';
|
|
30
40
|
import type { PXE, PackedPrivateEvent } from '@aztec/pxe/server';
|
|
31
41
|
import {
|
|
32
42
|
type ContractArtifact,
|
|
@@ -35,7 +45,7 @@ import {
|
|
|
35
45
|
decodeFromAbi,
|
|
36
46
|
} from '@aztec/stdlib/abi';
|
|
37
47
|
import type { AuthWitness } from '@aztec/stdlib/auth-witness';
|
|
38
|
-
import
|
|
48
|
+
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
39
49
|
import {
|
|
40
50
|
type ContractInstanceWithAddress,
|
|
41
51
|
computePartialAddress,
|
|
@@ -43,13 +53,17 @@ import {
|
|
|
43
53
|
} from '@aztec/stdlib/contract';
|
|
44
54
|
import { SimulationError } from '@aztec/stdlib/errors';
|
|
45
55
|
import { Gas, GasSettings } from '@aztec/stdlib/gas';
|
|
46
|
-
import {
|
|
56
|
+
import {
|
|
57
|
+
computeSiloedPrivateInitializationNullifier,
|
|
58
|
+
computeSiloedPublicInitializationNullifier,
|
|
59
|
+
} from '@aztec/stdlib/hash';
|
|
47
60
|
import type { AztecNode } from '@aztec/stdlib/interfaces/client';
|
|
48
61
|
import {
|
|
62
|
+
BlockHeader,
|
|
49
63
|
type TxExecutionRequest,
|
|
50
64
|
type TxProfileResult,
|
|
51
65
|
TxSimulationResult,
|
|
52
|
-
type
|
|
66
|
+
type UtilityExecutionResult,
|
|
53
67
|
} from '@aztec/stdlib/tx';
|
|
54
68
|
import { ExecutionPayload, mergeExecutionPayloads } from '@aztec/stdlib/tx';
|
|
55
69
|
|
|
@@ -67,17 +81,25 @@ export type FeeOptions = {
|
|
|
67
81
|
*/
|
|
68
82
|
walletFeePaymentMethod?: FeePaymentMethod;
|
|
69
83
|
/** Configuration options for the account to properly handle the selected fee payment method */
|
|
70
|
-
accountFeePaymentMethodOptions
|
|
84
|
+
accountFeePaymentMethodOptions?: AccountFeePaymentMethodOptions;
|
|
71
85
|
/** The gas settings to use for the transaction */
|
|
72
86
|
gasSettings: GasSettings;
|
|
73
87
|
};
|
|
74
88
|
|
|
89
|
+
/** Options for `simulateViaEntrypoint`. */
|
|
90
|
+
export type SimulateViaEntrypointOptions = Pick<
|
|
91
|
+
SimulateOptions,
|
|
92
|
+
'from' | 'additionalScopes' | 'skipTxValidation' | 'skipFeeEnforcement'
|
|
93
|
+
> & {
|
|
94
|
+
/** Fee options for the entrypoint */
|
|
95
|
+
feeOptions: FeeOptions;
|
|
96
|
+
/** Scopes to use for the simulation */
|
|
97
|
+
scopes: AccessScopes;
|
|
98
|
+
};
|
|
75
99
|
/**
|
|
76
100
|
* A base class for Wallet implementations
|
|
77
101
|
*/
|
|
78
102
|
export abstract class BaseWallet implements Wallet {
|
|
79
|
-
protected log = createLogger('wallet-sdk:base_wallet');
|
|
80
|
-
|
|
81
103
|
protected minFeePadding = 0.5;
|
|
82
104
|
protected cancellableTransactions = false;
|
|
83
105
|
|
|
@@ -85,8 +107,15 @@ export abstract class BaseWallet implements Wallet {
|
|
|
85
107
|
protected constructor(
|
|
86
108
|
protected readonly pxe: PXE,
|
|
87
109
|
protected readonly aztecNode: AztecNode,
|
|
110
|
+
protected log = createLogger('wallet-sdk:base_wallet'),
|
|
88
111
|
) {}
|
|
89
112
|
|
|
113
|
+
protected scopesFrom(from: AztecAddress | NoFrom, additionalScopes: AztecAddress[] = []): AztecAddress[] {
|
|
114
|
+
const allScopes = from === NO_FROM ? additionalScopes : [from, ...additionalScopes];
|
|
115
|
+
const scopeSet = new Set(allScopes.map(address => address.toString()));
|
|
116
|
+
return [...scopeSet].map(AztecAddress.fromString);
|
|
117
|
+
}
|
|
118
|
+
|
|
90
119
|
protected abstract getAccountFromAddress(address: AztecAddress): Promise<Account>;
|
|
91
120
|
|
|
92
121
|
abstract getAccounts(): Promise<Aliased<AztecAddress>[]>;
|
|
@@ -110,26 +139,33 @@ export abstract class BaseWallet implements Wallet {
|
|
|
110
139
|
|
|
111
140
|
protected async createTxExecutionRequestFromPayloadAndFee(
|
|
112
141
|
executionPayload: ExecutionPayload,
|
|
113
|
-
from: AztecAddress,
|
|
142
|
+
from: AztecAddress | NoFrom,
|
|
114
143
|
feeOptions: FeeOptions,
|
|
115
144
|
): Promise<TxExecutionRequest> {
|
|
116
145
|
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
117
|
-
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
118
|
-
txNonce: Fr.random(),
|
|
119
|
-
cancellable: this.cancellableTransactions,
|
|
120
|
-
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions,
|
|
121
|
-
};
|
|
122
146
|
const finalExecutionPayload = feeExecutionPayload
|
|
123
147
|
? mergeExecutionPayloads([feeExecutionPayload, executionPayload])
|
|
124
148
|
: executionPayload;
|
|
125
|
-
const fromAccount = await this.getAccountFromAddress(from);
|
|
126
149
|
const chainInfo = await this.getChainInfo();
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
chainInfo
|
|
131
|
-
|
|
132
|
-
|
|
150
|
+
|
|
151
|
+
if (from === NO_FROM) {
|
|
152
|
+
const entrypoint = new DefaultEntrypoint();
|
|
153
|
+
return entrypoint.createTxExecutionRequest(finalExecutionPayload, feeOptions.gasSettings, chainInfo);
|
|
154
|
+
} else {
|
|
155
|
+
const fromAccount = await this.getAccountFromAddress(from);
|
|
156
|
+
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
157
|
+
txNonce: Fr.random(),
|
|
158
|
+
cancellable: this.cancellableTransactions,
|
|
159
|
+
// If from is an address, feeOptions include the way the account contract should handle the fee payment
|
|
160
|
+
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions!,
|
|
161
|
+
};
|
|
162
|
+
return fromAccount.createTxExecutionRequest(
|
|
163
|
+
finalExecutionPayload,
|
|
164
|
+
feeOptions.gasSettings,
|
|
165
|
+
chainInfo,
|
|
166
|
+
executionOptions,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
133
169
|
}
|
|
134
170
|
|
|
135
171
|
public async createAuthWit(
|
|
@@ -184,23 +220,27 @@ export abstract class BaseWallet implements Wallet {
|
|
|
184
220
|
* @returns - Complete fee options that can be used to create a transaction execution request
|
|
185
221
|
*/
|
|
186
222
|
protected async completeFeeOptions(
|
|
187
|
-
from: AztecAddress,
|
|
223
|
+
from: AztecAddress | NoFrom,
|
|
188
224
|
feePayer?: AztecAddress,
|
|
189
225
|
gasSettings?: Partial<FieldsOf<GasSettings>>,
|
|
190
226
|
): Promise<FeeOptions> {
|
|
191
227
|
const maxFeesPerGas =
|
|
192
228
|
gasSettings?.maxFeesPerGas ?? (await this.aztecNode.getCurrentMinFees()).mul(1 + this.minFeePadding);
|
|
193
229
|
let accountFeePaymentMethodOptions;
|
|
194
|
-
//
|
|
195
|
-
//
|
|
196
|
-
if (
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
230
|
+
// If from is an address, we need to determine the appropriate fee payment method options for the
|
|
231
|
+
// account contract entrypoint to use
|
|
232
|
+
if (from !== NO_FROM) {
|
|
233
|
+
if (!feePayer) {
|
|
234
|
+
// The transaction does not include a fee payment method, so we set the flag
|
|
235
|
+
// for the account to use its fee juice balance
|
|
236
|
+
accountFeePaymentMethodOptions = AccountFeePaymentMethodOptions.PREEXISTING_FEE_JUICE;
|
|
237
|
+
} else {
|
|
238
|
+
// The transaction includes fee payment method, so we check if we are the fee payer for it
|
|
239
|
+
// (this can only happen if the embedded payment method is FeeJuiceWithClaim)
|
|
240
|
+
accountFeePaymentMethodOptions = from.equals(feePayer)
|
|
241
|
+
? AccountFeePaymentMethodOptions.FEE_JUICE_WITH_CLAIM
|
|
242
|
+
: AccountFeePaymentMethodOptions.EXTERNAL;
|
|
243
|
+
}
|
|
204
244
|
}
|
|
205
245
|
const fullGasSettings: GasSettings = GasSettings.default({ ...gasSettings, maxFeesPerGas });
|
|
206
246
|
this.log.debug(`Using L2 gas settings`, fullGasSettings);
|
|
@@ -220,7 +260,7 @@ export abstract class BaseWallet implements Wallet {
|
|
|
220
260
|
* @param gasSettings - User-provided partial gas settings
|
|
221
261
|
*/
|
|
222
262
|
protected async completeFeeOptionsForEstimation(
|
|
223
|
-
from: AztecAddress,
|
|
263
|
+
from: AztecAddress | NoFrom,
|
|
224
264
|
feePayer?: AztecAddress,
|
|
225
265
|
gasSettings?: Partial<FieldsOf<GasSettings>>,
|
|
226
266
|
) {
|
|
@@ -287,20 +327,20 @@ export abstract class BaseWallet implements Wallet {
|
|
|
287
327
|
/**
|
|
288
328
|
* Simulates calls through the standard PXE path (account entrypoint).
|
|
289
329
|
* @param executionPayload - The execution payload to simulate.
|
|
290
|
-
* @param
|
|
291
|
-
* @param feeOptions - Fee options for the transaction.
|
|
292
|
-
* @param skipTxValidation - Whether to skip tx validation.
|
|
293
|
-
* @param skipFeeEnforcement - Whether to skip fee enforcement.
|
|
330
|
+
* @param opts - Simulation options.
|
|
294
331
|
*/
|
|
295
|
-
protected async simulateViaEntrypoint(
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
332
|
+
protected async simulateViaEntrypoint(executionPayload: ExecutionPayload, opts: SimulateViaEntrypointOptions) {
|
|
333
|
+
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(
|
|
334
|
+
executionPayload,
|
|
335
|
+
opts.from,
|
|
336
|
+
opts.feeOptions,
|
|
337
|
+
);
|
|
338
|
+
return this.pxe.simulateTx(txRequest, {
|
|
339
|
+
simulatePublic: true,
|
|
340
|
+
skipTxValidation: opts.skipTxValidation,
|
|
341
|
+
skipFeeEnforcement: opts.skipFeeEnforcement,
|
|
342
|
+
scopes: opts.scopes,
|
|
343
|
+
});
|
|
304
344
|
}
|
|
305
345
|
|
|
306
346
|
/**
|
|
@@ -319,28 +359,37 @@ export abstract class BaseWallet implements Wallet {
|
|
|
319
359
|
const remainingPayload = { ...executionPayload, calls: remainingCalls };
|
|
320
360
|
|
|
321
361
|
const chainInfo = await this.getChainInfo();
|
|
322
|
-
|
|
362
|
+
let blockHeader: BlockHeader;
|
|
363
|
+
// PXE might not be synced yet, so we pull the latest header from the node
|
|
364
|
+
// To keep things consistent, we'll always try with PXE first
|
|
365
|
+
try {
|
|
366
|
+
blockHeader = await this.pxe.getSyncedBlockHeader();
|
|
367
|
+
} catch {
|
|
368
|
+
blockHeader = (await this.aztecNode.getBlockHeader())!;
|
|
369
|
+
}
|
|
323
370
|
|
|
371
|
+
const simulationOrigin = opts.from === NO_FROM ? AztecAddress.ZERO : opts.from;
|
|
324
372
|
const [optimizedResults, normalResult] = await Promise.all([
|
|
325
373
|
optimizableCalls.length > 0
|
|
326
374
|
? simulateViaNode(
|
|
327
375
|
this.aztecNode,
|
|
328
376
|
optimizableCalls,
|
|
329
|
-
|
|
377
|
+
simulationOrigin,
|
|
330
378
|
chainInfo,
|
|
331
379
|
feeOptions.gasSettings,
|
|
332
380
|
blockHeader,
|
|
333
381
|
opts.skipFeeEnforcement ?? true,
|
|
382
|
+
this.getContractName.bind(this),
|
|
334
383
|
)
|
|
335
384
|
: Promise.resolve([]),
|
|
336
385
|
remainingCalls.length > 0
|
|
337
|
-
? this.simulateViaEntrypoint(
|
|
338
|
-
|
|
339
|
-
opts.from,
|
|
386
|
+
? this.simulateViaEntrypoint(remainingPayload, {
|
|
387
|
+
from: opts.from,
|
|
340
388
|
feeOptions,
|
|
341
|
-
opts.
|
|
342
|
-
opts.
|
|
343
|
-
|
|
389
|
+
scopes: this.scopesFrom(opts.from, opts.additionalScopes),
|
|
390
|
+
skipTxValidation: opts.skipTxValidation,
|
|
391
|
+
skipFeeEnforcement: opts.skipFeeEnforcement ?? true,
|
|
392
|
+
})
|
|
344
393
|
: Promise.resolve(null),
|
|
345
394
|
]);
|
|
346
395
|
|
|
@@ -350,7 +399,11 @@ export abstract class BaseWallet implements Wallet {
|
|
|
350
399
|
async profileTx(executionPayload: ExecutionPayload, opts: ProfileOptions): Promise<TxProfileResult> {
|
|
351
400
|
const feeOptions = await this.completeFeeOptions(opts.from, executionPayload.feePayer, opts.fee?.gasSettings);
|
|
352
401
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
353
|
-
return this.pxe.profileTx(txRequest,
|
|
402
|
+
return this.pxe.profileTx(txRequest, {
|
|
403
|
+
profileMode: opts.profileMode,
|
|
404
|
+
skipProofGeneration: opts.skipProofGeneration ?? true,
|
|
405
|
+
scopes: this.scopesFrom(opts.from, opts.additionalScopes),
|
|
406
|
+
});
|
|
354
407
|
}
|
|
355
408
|
|
|
356
409
|
public async sendTx<W extends InteractionWaitOptions = undefined>(
|
|
@@ -359,7 +412,11 @@ export abstract class BaseWallet implements Wallet {
|
|
|
359
412
|
): Promise<SendReturn<W>> {
|
|
360
413
|
const feeOptions = await this.completeFeeOptions(opts.from, executionPayload.feePayer, opts.fee?.gasSettings);
|
|
361
414
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
362
|
-
const provenTx = await this.pxe.proveTx(txRequest);
|
|
415
|
+
const provenTx = await this.pxe.proveTx(txRequest, this.scopesFrom(opts.from, opts.additionalScopes));
|
|
416
|
+
const offchainOutput = extractOffchainOutput(
|
|
417
|
+
provenTx.getOffchainEffects(),
|
|
418
|
+
provenTx.publicInputs.constants.anchorBlockHeader.globalVariables.timestamp,
|
|
419
|
+
);
|
|
363
420
|
const tx = await provenTx.toTx();
|
|
364
421
|
const txHash = tx.getTxHash();
|
|
365
422
|
if (await this.aztecNode.getTxEffect(txHash)) {
|
|
@@ -373,12 +430,32 @@ export abstract class BaseWallet implements Wallet {
|
|
|
373
430
|
|
|
374
431
|
// If wait is NO_WAIT, return txHash immediately
|
|
375
432
|
if (opts.wait === NO_WAIT) {
|
|
376
|
-
return txHash as SendReturn<W>;
|
|
433
|
+
return { txHash, ...offchainOutput } as SendReturn<W>;
|
|
377
434
|
}
|
|
378
435
|
|
|
379
436
|
// Otherwise, wait for the full receipt (default behavior on wait: undefined)
|
|
380
437
|
const waitOpts = typeof opts.wait === 'object' ? opts.wait : undefined;
|
|
381
|
-
|
|
438
|
+
const receipt = await waitForTx(this.aztecNode, txHash, waitOpts);
|
|
439
|
+
|
|
440
|
+
// Display debug logs from public execution if present (served in test mode only)
|
|
441
|
+
if (receipt.debugLogs?.length) {
|
|
442
|
+
await displayDebugLogs(receipt.debugLogs, this.getContractName.bind(this));
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
return { receipt, ...offchainOutput } as SendReturn<W>;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Resolves a contract address to a human-readable name via PXE, if available.
|
|
450
|
+
* @param address - The contract address to resolve.
|
|
451
|
+
*/
|
|
452
|
+
protected async getContractName(address: AztecAddress): Promise<string | undefined> {
|
|
453
|
+
const instance = await this.pxe.getContractInstance(address);
|
|
454
|
+
if (!instance) {
|
|
455
|
+
return undefined;
|
|
456
|
+
}
|
|
457
|
+
const artifact = await this.pxe.getContractArtifact(instance.currentContractClassId);
|
|
458
|
+
return artifact?.name;
|
|
382
459
|
}
|
|
383
460
|
|
|
384
461
|
protected contextualizeError(err: Error, ...context: string[]): Error {
|
|
@@ -395,8 +472,8 @@ export abstract class BaseWallet implements Wallet {
|
|
|
395
472
|
return err;
|
|
396
473
|
}
|
|
397
474
|
|
|
398
|
-
|
|
399
|
-
return this.pxe.
|
|
475
|
+
executeUtility(call: FunctionCall, opts: ExecuteUtilityOptions): Promise<UtilityExecutionResult> {
|
|
476
|
+
return this.pxe.executeUtility(call, { authwits: opts.authWitnesses, scopes: opts.scopes });
|
|
400
477
|
}
|
|
401
478
|
|
|
402
479
|
async getPrivateEvents<T>(
|
|
@@ -419,17 +496,39 @@ export abstract class BaseWallet implements Wallet {
|
|
|
419
496
|
return decodedEvents;
|
|
420
497
|
}
|
|
421
498
|
|
|
499
|
+
/**
|
|
500
|
+
* Returns metadata about a contract, including whether it has been initialized, published, and updated.
|
|
501
|
+
* @param address - The contract address to query.
|
|
502
|
+
*/
|
|
422
503
|
async getContractMetadata(address: AztecAddress) {
|
|
423
504
|
const instance = await this.pxe.getContractInstance(address);
|
|
424
|
-
const
|
|
425
|
-
|
|
426
|
-
|
|
505
|
+
const publiclyRegisteredContractPromise = this.aztecNode.getContract(address);
|
|
506
|
+
|
|
507
|
+
let initializationStatus: ContractInitializationStatus;
|
|
508
|
+
if (instance) {
|
|
509
|
+
// We have the instance, so we can compute the private initialization nullifier (which includes init_hash and is
|
|
510
|
+
// emitted by both private and public initializers) and get a definitive INITIALIZED/UNINITIALIZED answer.
|
|
511
|
+
const initNullifier = await computeSiloedPrivateInitializationNullifier(address, instance.initializationHash);
|
|
512
|
+
const witness = await this.aztecNode.getNullifierMembershipWitness('latest', initNullifier);
|
|
513
|
+
initializationStatus = witness
|
|
514
|
+
? ContractInitializationStatus.INITIALIZED
|
|
515
|
+
: ContractInitializationStatus.UNINITIALIZED;
|
|
516
|
+
} else {
|
|
517
|
+
// Without the instance we lack the init_hash needed for the private nullifier. We fall back to checking the
|
|
518
|
+
// public initialization nullifier (computed from address alone). Not all contracts emit it (only those with
|
|
519
|
+
// public functions that require initialization checks), so its absence doesn't mean the contract is
|
|
520
|
+
// uninitialized.
|
|
521
|
+
const publicNullifier = await computeSiloedPublicInitializationNullifier(address);
|
|
522
|
+
const witness = await this.aztecNode.getNullifierMembershipWitness('latest', publicNullifier);
|
|
523
|
+
initializationStatus = witness ? ContractInitializationStatus.INITIALIZED : ContractInitializationStatus.UNKNOWN;
|
|
524
|
+
}
|
|
525
|
+
const publiclyRegisteredContract = await publiclyRegisteredContractPromise;
|
|
427
526
|
const isContractUpdated =
|
|
428
527
|
publiclyRegisteredContract &&
|
|
429
528
|
!publiclyRegisteredContract.currentContractClassId.equals(publiclyRegisteredContract.originalContractClassId);
|
|
430
529
|
return {
|
|
431
530
|
instance: instance ?? undefined,
|
|
432
|
-
|
|
531
|
+
initializationStatus,
|
|
433
532
|
isContractPublished: !!publiclyRegisteredContract,
|
|
434
533
|
isContractUpdated: !!isContractUpdated,
|
|
435
534
|
updatedContractClassId: isContractUpdated ? publiclyRegisteredContract.currentContractClassId : undefined,
|
package/src/base-wallet/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { BaseWallet, type FeeOptions } from './base_wallet.js';
|
|
1
|
+
export { BaseWallet, type FeeOptions, type SimulateViaEntrypointOptions } from './base_wallet.js';
|
|
2
2
|
export { simulateViaNode, buildMergedSimulationResult, extractOptimizablePublicStaticCalls } from './utils.js';
|
package/src/base-wallet/utils.ts
CHANGED
|
@@ -4,6 +4,8 @@ import type { ChainInfo } from '@aztec/entrypoints/interfaces';
|
|
|
4
4
|
import { makeTuple } from '@aztec/foundation/array';
|
|
5
5
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
6
6
|
import type { Tuple } from '@aztec/foundation/serialize';
|
|
7
|
+
import type { ContractNameResolver } from '@aztec/pxe/client/lazy';
|
|
8
|
+
import { displayDebugLogs } from '@aztec/pxe/client/lazy';
|
|
7
9
|
import { generateSimulatedProvingResult } from '@aztec/pxe/simulator';
|
|
8
10
|
import { type FunctionCall, FunctionSelector } from '@aztec/stdlib/abi';
|
|
9
11
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
@@ -72,6 +74,7 @@ async function simulateBatchViaNode(
|
|
|
72
74
|
gasSettings: GasSettings,
|
|
73
75
|
blockHeader: BlockHeader,
|
|
74
76
|
skipFeeEnforcement: boolean,
|
|
77
|
+
getContractName: ContractNameResolver,
|
|
75
78
|
): Promise<TxSimulationResult> {
|
|
76
79
|
const txContext = new TxContext(chainInfo.chainId, chainInfo.version, gasSettings);
|
|
77
80
|
|
|
@@ -126,6 +129,7 @@ async function simulateBatchViaNode(
|
|
|
126
129
|
const provingResult = await generateSimulatedProvingResult(
|
|
127
130
|
privateResult,
|
|
128
131
|
(_contractAddress: AztecAddress, _functionSelector: FunctionSelector) => Promise.resolve(''),
|
|
132
|
+
node,
|
|
129
133
|
1, // minRevertibleSideEffectCounter
|
|
130
134
|
);
|
|
131
135
|
|
|
@@ -144,6 +148,9 @@ async function simulateBatchViaNode(
|
|
|
144
148
|
throw publicOutput.revertReason;
|
|
145
149
|
}
|
|
146
150
|
|
|
151
|
+
// Display debug logs from the public simulation.
|
|
152
|
+
await displayDebugLogs(publicOutput.debugLogs, getContractName);
|
|
153
|
+
|
|
147
154
|
return new TxSimulationResult(privateResult, provingResult.publicInputs, publicOutput, undefined);
|
|
148
155
|
}
|
|
149
156
|
|
|
@@ -168,6 +175,7 @@ export async function simulateViaNode(
|
|
|
168
175
|
gasSettings: GasSettings,
|
|
169
176
|
blockHeader: BlockHeader,
|
|
170
177
|
skipFeeEnforcement: boolean = true,
|
|
178
|
+
getContractName: ContractNameResolver,
|
|
171
179
|
): Promise<TxSimulationResult[]> {
|
|
172
180
|
const batches: FunctionCall[][] = [];
|
|
173
181
|
|
|
@@ -186,6 +194,7 @@ export async function simulateViaNode(
|
|
|
186
194
|
gasSettings,
|
|
187
195
|
blockHeader,
|
|
188
196
|
skipFeeEnforcement,
|
|
197
|
+
getContractName,
|
|
189
198
|
);
|
|
190
199
|
results.push(result);
|
|
191
200
|
}
|
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.
|
|
@@ -109,7 +104,7 @@ export class ExtensionWallet {
|
|
|
109
104
|
sharedKey: CryptoKey,
|
|
110
105
|
chainInfo: ChainInfo,
|
|
111
106
|
appId: string,
|
|
112
|
-
):
|
|
107
|
+
): ExtensionWallet {
|
|
113
108
|
const wallet = new ExtensionWallet(chainInfo, appId, extensionId, port, sharedKey);
|
|
114
109
|
|
|
115
110
|
// Set up message handler for encrypted responses and unencrypted control messages
|
|
@@ -127,8 +122,10 @@ export class ExtensionWallet {
|
|
|
127
122
|
wallet.port.start();
|
|
128
123
|
|
|
129
124
|
return new Proxy(wallet, {
|
|
130
|
-
get: (target, prop) => {
|
|
131
|
-
if (
|
|
125
|
+
get: (target, prop, receiver) => {
|
|
126
|
+
if (prop === 'asWallet') {
|
|
127
|
+
return () => receiver as unknown as Wallet;
|
|
128
|
+
} else if (schemaHasMethod(WalletSchema, prop.toString())) {
|
|
132
129
|
return async (...args: unknown[]) => {
|
|
133
130
|
const result = await target.postMessage({
|
|
134
131
|
type: prop.toString() as keyof FunctionsOf<Wallet>,
|
|
@@ -140,7 +137,13 @@ export class ExtensionWallet {
|
|
|
140
137
|
return target[prop as keyof ExtensionWallet];
|
|
141
138
|
}
|
|
142
139
|
},
|
|
143
|
-
})
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
asWallet(): Wallet {
|
|
144
|
+
// Overridden by the proxy in create() to return the proxy itself.
|
|
145
|
+
// This body is never reached when accessed through create().
|
|
146
|
+
return this as unknown as Wallet;
|
|
144
147
|
}
|
|
145
148
|
|
|
146
149
|
/**
|