@aztec/wallets 0.0.1-commit.0c875d939 → 0.0.1-commit.0dc957cde

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.
Files changed (32) hide show
  1. package/dest/embedded/account-contract-providers/bundle.d.ts +9 -4
  2. package/dest/embedded/account-contract-providers/bundle.d.ts.map +1 -1
  3. package/dest/embedded/account-contract-providers/bundle.js +10 -5
  4. package/dest/embedded/account-contract-providers/lazy.d.ts +9 -4
  5. package/dest/embedded/account-contract-providers/lazy.d.ts.map +1 -1
  6. package/dest/embedded/account-contract-providers/lazy.js +20 -6
  7. package/dest/embedded/account-contract-providers/types.d.ts +9 -4
  8. package/dest/embedded/account-contract-providers/types.d.ts.map +1 -1
  9. package/dest/embedded/embedded_wallet.d.ts +48 -8
  10. package/dest/embedded/embedded_wallet.d.ts.map +1 -1
  11. package/dest/embedded/embedded_wallet.js +164 -50
  12. package/dest/embedded/entrypoints/browser.d.ts +8 -3
  13. package/dest/embedded/entrypoints/browser.d.ts.map +1 -1
  14. package/dest/embedded/entrypoints/browser.js +32 -15
  15. package/dest/embedded/entrypoints/node.d.ts +7 -11
  16. package/dest/embedded/entrypoints/node.d.ts.map +1 -1
  17. package/dest/embedded/entrypoints/node.js +23 -15
  18. package/dest/testing.d.ts +1 -1
  19. package/dest/testing.d.ts.map +1 -1
  20. package/dest/testing.js +2 -2
  21. package/package.json +19 -9
  22. package/src/embedded/account-contract-providers/bundle.ts +13 -6
  23. package/src/embedded/account-contract-providers/lazy.ts +23 -7
  24. package/src/embedded/account-contract-providers/types.ts +6 -3
  25. package/src/embedded/embedded_wallet.ts +220 -60
  26. package/src/embedded/entrypoints/browser.ts +53 -24
  27. package/src/embedded/entrypoints/node.ts +50 -44
  28. package/src/testing.ts +2 -1
  29. package/dest/embedded/embedded_wallet_browser.d.ts +0 -5
  30. package/dest/embedded/embedded_wallet_browser.d.ts.map +0 -1
  31. package/dest/embedded/embedded_wallet_browser.js +0 -31
  32. package/src/embedded/embedded_wallet_browser.ts +0 -40
@@ -1,29 +1,81 @@
1
- import { type Account, SignerlessAccount } from '@aztec/aztec.js/account';
2
- import type { Aliased } from '@aztec/aztec.js/wallet';
3
- import { AccountManager } from '@aztec/aztec.js/wallet';
1
+ import { type Account, NO_FROM } from '@aztec/aztec.js/account';
2
+ import { CallAuthorizationRequest } from '@aztec/aztec.js/authorization';
3
+ import { type InteractionWaitOptions, type SendReturn, type WaitOpts, getGasLimits } from '@aztec/aztec.js/contracts';
4
+ import type { Aliased, SendOptions } from '@aztec/aztec.js/wallet';
5
+ import { AccountManager, TxSimulationResultWithAppOffset } from '@aztec/aztec.js/wallet';
4
6
  import type { DefaultAccountEntrypointOptions } from '@aztec/entrypoints/account';
7
+ import { DefaultEntrypoint } from '@aztec/entrypoints/default';
5
8
  import { Fq, Fr } from '@aztec/foundation/curves/bn254';
6
9
  import type { Logger } from '@aztec/foundation/log';
7
- import type { AccessScopes } from '@aztec/pxe/client/lazy';
10
+ import type { AztecAsyncKVStore } from '@aztec/kv-store';
11
+ import type { PXEConfig, PXECreationOptions } from '@aztec/pxe/client/lazy';
8
12
  import type { PXE } from '@aztec/pxe/server';
9
13
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
10
14
  import { getContractInstanceFromInstantiationParams } from '@aztec/stdlib/contract';
15
+ import { GasSettings } from '@aztec/stdlib/gas';
11
16
  import type { AztecNode } from '@aztec/stdlib/interfaces/client';
12
17
  import { deriveSigningKey } from '@aztec/stdlib/keys';
13
- import { ExecutionPayload, type TxSimulationResult, mergeExecutionPayloads } from '@aztec/stdlib/tx';
14
- import { BaseWallet, type FeeOptions } from '@aztec/wallet-sdk/base-wallet';
18
+ import {
19
+ type ContractOverrides,
20
+ ExecutionPayload,
21
+ SimulationOverrides,
22
+ type TxExecutionRequest,
23
+ TxStatus,
24
+ collectOffchainEffects,
25
+ mergeExecutionPayloads,
26
+ } from '@aztec/stdlib/tx';
27
+ import { BaseWallet, type SimulateViaEntrypointOptions } from '@aztec/wallet-sdk/base-wallet';
15
28
 
16
29
  import type { AccountContractsProvider } from './account-contract-providers/types.js';
17
30
  import { type AccountType, WalletDB } from './wallet_db.js';
18
31
 
32
+ /** Options for the PXE instance created by the EmbeddedWallet. */
33
+ export type EmbeddedWalletPXEOptions = Partial<PXEConfig> & PXECreationOptions;
34
+
35
+ /** Splits a unified EmbeddedWalletPXEOptions into PXEConfig overrides and PXECreationOptions. */
36
+ export function splitPxeOptions(pxe?: EmbeddedWalletPXEOptions): {
37
+ config: Partial<PXEConfig>;
38
+ creation: PXECreationOptions;
39
+ } {
40
+ if (!pxe) {
41
+ return { config: {}, creation: {} };
42
+ }
43
+ const { loggers, loggerActorLabel, proverOrOptions, store, simulator, ...config } = pxe;
44
+ return { config, creation: { loggers, loggerActorLabel, proverOrOptions, store, simulator } };
45
+ }
46
+
47
+ /** Options for the EmbeddedWallet's own DB (accounts, senders — distinct from PXE state). */
48
+ export type EmbeddedWalletDBOptions = {
49
+ /** Override the wallet DB backend. If omitted, an IndexedDB (browser) / LMDB (node) store is created. */
50
+ store?: AztecAsyncKVStore;
51
+ };
52
+
19
53
  export type EmbeddedWalletOptions = {
20
54
  /** Parent logger. Child loggers are derived via createChild() for each subsystem. */
21
55
  logger?: Logger;
22
56
  /** Use ephemeral (in-memory) stores. Data will not persist across sessions. */
23
57
  ephemeral?: boolean;
58
+ /** PXE configuration and dependency overrides (custom store, prover, simulator). */
59
+ pxe?: EmbeddedWalletPXEOptions;
60
+ /** Wallet DB dependency overrides (custom store). */
61
+ walletDb?: EmbeddedWalletDBOptions;
62
+ /**
63
+ * Override PXE configuration.
64
+ * @deprecated Use `pxe` instead.
65
+ */
66
+ pxeConfig?: Partial<PXEConfig>;
67
+ /**
68
+ * Advanced PXE creation options (custom store, prover, simulator).
69
+ * @deprecated Use `pxe` instead.
70
+ */
71
+ pxeOptions?: PXECreationOptions;
24
72
  };
25
73
 
74
+ const DEFAULT_ESTIMATED_GAS_PADDING = 0.1;
75
+
26
76
  export class EmbeddedWallet extends BaseWallet {
77
+ protected estimatedGasPadding = DEFAULT_ESTIMATED_GAS_PADDING;
78
+
27
79
  constructor(
28
80
  pxe: PXE,
29
81
  aztecNode: AztecNode,
@@ -35,10 +87,6 @@ export class EmbeddedWallet extends BaseWallet {
35
87
  }
36
88
 
37
89
  protected async getAccountFromAddress(address: AztecAddress): Promise<Account> {
38
- if (address.equals(AztecAddress.ZERO)) {
39
- return new SignerlessAccount();
40
- }
41
-
42
90
  const { secretKey, salt, signingKey, type } = await this.walletDB.retrieveAccount(address);
43
91
  const accountManager = await this.createAccountInternal(type, secretKey, salt, signingKey);
44
92
  const account = await accountManager.getAccount();
@@ -70,6 +118,118 @@ export class EmbeddedWallet extends BaseWallet {
70
118
  return storedSenders;
71
119
  }
72
120
 
121
+ /**
122
+ * Overrides the base sendTx to add a pre-simulation step before the actual send. The simulation
123
+ * estimates actual gas usage and captures call authorization requests to generate
124
+ * the necessary authwitnesses.
125
+ */
126
+ public override async sendTx<W extends InteractionWaitOptions = undefined>(
127
+ executionPayload: ExecutionPayload,
128
+ opts: SendOptions<W>,
129
+ ): Promise<SendReturn<W>> {
130
+ const feeOptions = await this.completeFeeOptions({
131
+ from: opts.from,
132
+ feePayer: executionPayload.feePayer,
133
+ gasSettings: opts.fee?.gasSettings,
134
+ forEstimation: true,
135
+ });
136
+
137
+ // Simulate the transaction first to estimate gas and capture required
138
+ // private authwitnesses based on offchain effects.
139
+ const simulationResult = await this.simulateViaEntrypoint(executionPayload, {
140
+ from: opts.from,
141
+ feeOptions,
142
+ additionalScopes: opts.additionalScopes,
143
+ skipTxValidation: true,
144
+ });
145
+
146
+ const offchainEffects = collectOffchainEffects(simulationResult.privateExecutionResult);
147
+ const authWitnesses = await Promise.all(
148
+ offchainEffects.map(async effect => {
149
+ try {
150
+ const authRequest = await CallAuthorizationRequest.fromFields(effect.data);
151
+ return this.createAuthWit(authRequest.onBehalfOf, {
152
+ consumer: effect.contractAddress,
153
+ innerHash: authRequest.innerHash,
154
+ });
155
+ } catch {
156
+ return undefined;
157
+ }
158
+ }),
159
+ );
160
+ for (const authwit of authWitnesses) {
161
+ if (authwit) {
162
+ executionPayload.authWitnesses.push(authwit);
163
+ }
164
+ }
165
+ const estimated = getGasLimits(simulationResult, this.estimatedGasPadding);
166
+ this.log.verbose(
167
+ `Estimated gas limits for tx: DA=${estimated.gasLimits.daGas} L2=${estimated.gasLimits.l2Gas} teardownDA=${estimated.teardownGasLimits.daGas} teardownL2=${estimated.teardownGasLimits.l2Gas}`,
168
+ );
169
+ const gasSettings = GasSettings.from({
170
+ ...opts.fee?.gasSettings,
171
+ maxFeesPerGas: feeOptions.gasSettings.maxFeesPerGas,
172
+ maxPriorityFeesPerGas: feeOptions.gasSettings.maxPriorityFeesPerGas,
173
+ gasLimits: opts.fee?.gasSettings?.gasLimits ?? estimated.gasLimits,
174
+ teardownGasLimits: opts.fee?.gasSettings?.teardownGasLimits ?? estimated.teardownGasLimits,
175
+ });
176
+ const waitOpts: WaitOpts = typeof opts.wait === 'object' ? opts.wait : {};
177
+
178
+ if (!waitOpts?.waitForStatus) {
179
+ // Default to PROPOSED so the wait returns as soon as the tx lands in a proposed L2 block,
180
+ // rather than waiting until the end of the slot for the checkpoint to be published to L1.
181
+ // This is what makes MBPS (Multiple Blocks Per Slot) actually improve UX: with CHECKPOINTED
182
+ // we'd block until L1 inclusion regardless of how early in the slot the tx was sequenced.
183
+ // The tradeoff is a weaker guarantee — a proposed block only becomes canonical once it (or
184
+ // a later block in the same slot) is checkpointed, so a tx could be re-orged out if the
185
+ // proposer fails to publish to L1 (which should be rare, since they'd get slashed for it).
186
+ waitOpts!.waitForStatus = TxStatus.PROPOSED;
187
+ }
188
+ return super.sendTx(executionPayload, {
189
+ ...opts,
190
+ fee: { ...opts.fee, gasSettings },
191
+ });
192
+ }
193
+
194
+ /**
195
+ * Builds contract overrides for all provided addresses by replacing their account contracts with stub implementations.
196
+ * Uses a type-specific stub artifact so that the stub's constructor selector matches the real account's constructor.
197
+ */
198
+ protected async buildAccountOverrides(addresses: AztecAddress[]): Promise<ContractOverrides> {
199
+ const accounts = await this.getAccounts();
200
+ const contracts: ContractOverrides = {};
201
+
202
+ const filtered = accounts.filter(acc => addresses.some(addr => addr.equals(acc.item)));
203
+
204
+ for (const account of filtered) {
205
+ const address = account.item;
206
+ const { type } = await this.walletDB.retrieveAccount(address);
207
+ const stubArtifact = await this.accountContracts.getStubAccountContractArtifact(type);
208
+
209
+ const originalAccount = await this.getAccountFromAddress(address);
210
+ const completeAddress = originalAccount.getCompleteAddress();
211
+ const contractInstance = await this.pxe.getContractInstance(completeAddress.address);
212
+ if (!contractInstance) {
213
+ throw new Error(
214
+ `No contract instance found for address: ${completeAddress.address} during account override building. This is a bug!`,
215
+ );
216
+ }
217
+
218
+ const stubConstructorArgs = type === 'schnorr' ? [Fr.ZERO, Fr.ZERO] : [Buffer.alloc(32), Buffer.alloc(32)];
219
+ const stubInstance = await getContractInstanceFromInstantiationParams(stubArtifact, {
220
+ salt: Fr.random(),
221
+ constructorArgs: stubConstructorArgs,
222
+ });
223
+
224
+ contracts[address.toString()] = {
225
+ instance: stubInstance,
226
+ artifact: stubArtifact,
227
+ };
228
+ }
229
+
230
+ return contracts;
231
+ }
232
+
73
233
  /**
74
234
  * Simulates calls via a stub account entrypoint, bypassing real account authorization.
75
235
  * This allows kernelless simulation with contract overrides, skipping expensive
@@ -77,61 +237,52 @@ export class EmbeddedWallet extends BaseWallet {
77
237
  */
78
238
  protected override async simulateViaEntrypoint(
79
239
  executionPayload: ExecutionPayload,
80
- from: AztecAddress,
81
- feeOptions: FeeOptions,
82
- scopes: AccessScopes,
83
- _skipTxValidation?: boolean,
84
- _skipFeeEnforcement?: boolean,
85
- ): Promise<TxSimulationResult> {
86
- const { account: fromAccount, instance, artifact } = await this.getFakeAccountDataFor(from);
240
+ opts: SimulateViaEntrypointOptions,
241
+ ): Promise<TxSimulationResultWithAppOffset> {
242
+ const { from, feeOptions, additionalScopes, skipTxValidation, skipFeeEnforcement } = opts;
243
+ const scopes = this.scopesFrom(from, additionalScopes);
87
244
 
88
245
  const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
89
- const executionOptions: DefaultAccountEntrypointOptions = {
90
- txNonce: Fr.random(),
91
- cancellable: this.cancellableTransactions,
92
- feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions,
93
- };
94
246
  const finalExecutionPayload = feeExecutionPayload
95
247
  ? mergeExecutionPayloads([feeExecutionPayload, executionPayload])
96
248
  : executionPayload;
97
249
  const chainInfo = await this.getChainInfo();
98
- const txRequest = await fromAccount.createTxExecutionRequest(
99
- finalExecutionPayload,
100
- feeOptions.gasSettings,
101
- chainInfo,
102
- executionOptions,
103
- );
104
- return this.pxe.simulateTx(txRequest, {
105
- simulatePublic: true,
106
- skipFeeEnforcement: true,
107
- skipTxValidation: true,
108
- overrides: {
109
- contracts: { [from.toString()]: { instance, artifact } },
110
- },
111
- scopes,
112
- });
113
- }
114
250
 
115
- private async getFakeAccountDataFor(address: AztecAddress) {
116
- const originalAccount = await this.getAccountFromAddress(address);
117
- if (originalAccount instanceof SignerlessAccount) {
118
- throw new Error(`Cannot create fake account data for SignerlessAccount at address: ${address}`);
119
- }
120
- const originalAddress = (originalAccount as Account).getCompleteAddress();
121
- const contractInstance = await this.pxe.getContractInstance(originalAddress.address);
122
- if (!contractInstance) {
123
- throw new Error(`No contract instance found for address: ${originalAddress.address}`);
251
+ const accountOverrides = await this.buildAccountOverrides(scopes);
252
+ const overrides = new SimulationOverrides(accountOverrides);
253
+
254
+ let txRequest: TxExecutionRequest;
255
+ if (from === NO_FROM) {
256
+ const entrypoint = new DefaultEntrypoint();
257
+ txRequest = await entrypoint.createTxExecutionRequest(finalExecutionPayload, feeOptions.gasSettings, chainInfo);
258
+ } else {
259
+ const { type } = await this.walletDB.retrieveAccount(from);
260
+ const originalAccount = await this.getAccountFromAddress(from);
261
+ const completeAddress = originalAccount.getCompleteAddress();
262
+ const account = await this.accountContracts.createStubAccount(completeAddress, type);
263
+ const executionOptions: DefaultAccountEntrypointOptions = {
264
+ txNonce: Fr.random(),
265
+ cancellable: this.cancellableTransactions,
266
+ // If from is an address, feeOptions include the way the account contract should handle the fee payment
267
+ feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions!,
268
+ };
269
+ txRequest = await account.createTxExecutionRequest(
270
+ finalExecutionPayload,
271
+ feeOptions.gasSettings,
272
+ chainInfo,
273
+ executionOptions,
274
+ );
124
275
  }
125
- const stubAccount = await this.accountContracts.createStubAccount(originalAddress);
126
- const stubArtifact = await this.accountContracts.getStubAccountContractArtifact();
127
- const instance = await getContractInstanceFromInstantiationParams(stubArtifact, {
128
- salt: Fr.random(),
276
+
277
+ const result = await this.pxe.simulateTx(txRequest, {
278
+ simulatePublic: true,
279
+ skipFeeEnforcement,
280
+ skipTxValidation,
281
+ overrides,
282
+ scopes,
129
283
  });
130
- return {
131
- account: stubAccount,
132
- instance,
133
- artifact: stubArtifact,
134
- };
284
+ const appCallOffset = await this.computeAppCallOffset(from, feeOptions);
285
+ return TxSimulationResultWithAppOffset.fromResultAndOffset(result, appCallOffset);
135
286
  }
136
287
 
137
288
  protected async createAccountInternal(
@@ -162,10 +313,15 @@ export class EmbeddedWallet extends BaseWallet {
162
313
  const accountManager = await AccountManager.create(this, secret, contract, salt);
163
314
 
164
315
  const instance = accountManager.getInstance();
165
- const artifact = await accountManager.getAccountContract().getContractArtifact();
166
-
167
- await this.registerContract(instance, artifact, accountManager.getSecretKey());
168
-
316
+ const existingInstance = await this.pxe.getContractInstance(instance.address);
317
+ if (!existingInstance) {
318
+ const existingArtifact = await this.pxe.getContractArtifact(instance.currentContractClassId);
319
+ await this.registerContract(
320
+ instance,
321
+ !existingArtifact ? await accountManager.getAccountContract().getContractArtifact() : undefined,
322
+ accountManager.getSecretKey(),
323
+ );
324
+ }
169
325
  return accountManager;
170
326
  }
171
327
 
@@ -198,6 +354,10 @@ export class EmbeddedWallet extends BaseWallet {
198
354
  this.minFeePadding = value ?? 0.5;
199
355
  }
200
356
 
357
+ setEstimatedGasPadding(value?: number) {
358
+ this.estimatedGasPadding = value ?? DEFAULT_ESTIMATED_GAS_PADDING;
359
+ }
360
+
201
361
  stop() {
202
362
  return this.pxe.stop();
203
363
  }
@@ -1,50 +1,79 @@
1
- import { createAztecNodeClient } from '@aztec/aztec.js/node';
2
- import { createLogger } from '@aztec/foundation/log';
1
+ import { type AztecNode, createAztecNodeClient } from '@aztec/aztec.js/node';
2
+ import { type Logger, createLogger } from '@aztec/foundation/log';
3
3
  import { createStore, openTmpStore } from '@aztec/kv-store/indexeddb';
4
- import { createPXE } from '@aztec/pxe/client/lazy';
5
- import { getPXEConfig } from '@aztec/pxe/config';
4
+ import { type PXE, type PXECreationOptions, createPXE } from '@aztec/pxe/client/lazy';
5
+ import { type PXEConfig, getPXEConfig } from '@aztec/pxe/config';
6
6
 
7
7
  import { LazyAccountContractsProvider } from '../account-contract-providers/lazy.js';
8
- import { EmbeddedWallet, type EmbeddedWalletOptions } from '../embedded_wallet.js';
8
+ import type { AccountContractsProvider } from '../account-contract-providers/types.js';
9
+ import { EmbeddedWallet, type EmbeddedWalletOptions, splitPxeOptions } from '../embedded_wallet.js';
9
10
  import { WalletDB } from '../wallet_db.js';
10
11
 
11
12
  export class BrowserEmbeddedWallet extends EmbeddedWallet {
12
- static async create(nodeUrl: string, options: EmbeddedWalletOptions = {}): Promise<BrowserEmbeddedWallet> {
13
+ static async create<T extends BrowserEmbeddedWallet = BrowserEmbeddedWallet>(
14
+ this: new (
15
+ pxe: PXE,
16
+ aztecNode: AztecNode,
17
+ walletDB: WalletDB,
18
+ accountContracts: AccountContractsProvider,
19
+ log?: Logger,
20
+ ) => T,
21
+ nodeOrUrl: string | AztecNode,
22
+ options: EmbeddedWalletOptions = {},
23
+ ): Promise<T> {
13
24
  const rootLogger = options.logger ?? createLogger('embedded-wallet');
14
25
 
15
- const aztecNode = createAztecNodeClient(nodeUrl);
16
-
26
+ const aztecNode = typeof nodeOrUrl === 'string' ? createAztecNodeClient(nodeOrUrl) : nodeOrUrl;
17
27
  const l1Contracts = await aztecNode.getL1ContractAddresses();
18
- const rollupAddress = l1Contracts.rollupAddress;
19
28
 
20
- const config = getPXEConfig();
21
- if (!options.ephemeral) {
22
- config.dataDirectory = `pxe-${rollupAddress}`;
29
+ // Support both the new unified `pxe` option and the deprecated `pxeConfig`/`pxeOptions`.
30
+ const { config: pxeConfigFromPxe, creation: pxeCreationFromPxe } = splitPxeOptions(options.pxe);
31
+ const mergedConfigOverrides = { ...options.pxeConfig, ...pxeConfigFromPxe };
32
+ const mergedCreationOverrides: PXECreationOptions = { ...options.pxeOptions, ...pxeCreationFromPxe };
33
+
34
+ const pxeConfig: PXEConfig = Object.assign(getPXEConfig(), {
35
+ proverEnabled: mergedConfigOverrides.proverEnabled ?? false,
36
+ dataDirectory: `pxe_data_${l1Contracts.rollupAddress}`,
37
+ ...mergedConfigOverrides,
38
+ });
39
+
40
+ if (options.ephemeral) {
41
+ delete pxeConfig.dataDirectory;
23
42
  }
24
43
 
25
- const pxe = await createPXE(aztecNode, config, {
44
+ const pxeOptions: PXECreationOptions = {
45
+ ...mergedCreationOverrides,
26
46
  loggers: {
27
47
  store: rootLogger.createChild('pxe:data'),
28
48
  pxe: rootLogger.createChild('pxe:service'),
29
49
  prover: rootLogger.createChild('pxe:prover'),
50
+ ...mergedCreationOverrides.loggers,
30
51
  },
31
- });
52
+ };
53
+
54
+ const pxe = await createPXE(aztecNode, pxeConfig, pxeOptions);
32
55
 
33
- const walletDBStore = options.ephemeral
34
- ? await openTmpStore(true)
35
- : await createStore(
36
- `wallet-${rollupAddress}`,
37
- { dataDirectory: 'wallet', dataStoreMapSizeKb: 2e10 },
38
- undefined,
39
- rootLogger.createChild('wallet:data'),
40
- );
56
+ const walletDBStore =
57
+ options.walletDb?.store ??
58
+ (options.ephemeral
59
+ ? await openTmpStore(true)
60
+ : await createStore(
61
+ 'wallet_data',
62
+ {
63
+ dataDirectory: `wallet_data_${l1Contracts.rollupAddress}`,
64
+ dataStoreMapSizeKb: pxeConfig.dataStoreMapSizeKb,
65
+ l1Contracts,
66
+ },
67
+ 1,
68
+ rootLogger.createChild('wallet:data'),
69
+ ));
41
70
  const walletDB = WalletDB.init(walletDBStore, rootLogger.createChild('wallet:db').info);
42
71
 
43
- return new BrowserEmbeddedWallet(pxe, aztecNode, walletDB, new LazyAccountContractsProvider(), rootLogger);
72
+ return new this(pxe, aztecNode, walletDB, new LazyAccountContractsProvider(), rootLogger) as T;
44
73
  }
45
74
  }
46
75
 
47
76
  export { BrowserEmbeddedWallet as EmbeddedWallet };
48
- export type { EmbeddedWalletOptions } from '../embedded_wallet.js';
77
+ export type { EmbeddedWalletOptions, EmbeddedWalletPXEOptions } from '../embedded_wallet.js';
49
78
  export { WalletDB } from '../wallet_db.js';
50
79
  export type { AccountType } from '../wallet_db.js';
@@ -1,34 +1,41 @@
1
1
  import { createAztecNodeClient } from '@aztec/aztec.js/node';
2
- import { createLogger } from '@aztec/foundation/log';
3
- import { createStore as createWalletStore, openTmpStore } from '@aztec/kv-store/lmdb-v2';
4
- import type { PXEConfig } from '@aztec/pxe/config';
5
- import { getPXEConfig } from '@aztec/pxe/config';
6
- import { type PXECreationOptions, createPXE } from '@aztec/pxe/server';
2
+ import { type Logger, createLogger } from '@aztec/foundation/log';
3
+ import { createStore, openTmpStore } from '@aztec/kv-store/lmdb-v2';
4
+ import { type PXEConfig, getPXEConfig } from '@aztec/pxe/config';
5
+ import { type PXE, type PXECreationOptions, createPXE } from '@aztec/pxe/server';
7
6
  import type { AztecNode } from '@aztec/stdlib/interfaces/client';
8
7
 
9
8
  import { BundleAccountContractsProvider } from '../account-contract-providers/bundle.js';
10
- import { EmbeddedWallet, type EmbeddedWalletOptions } from '../embedded_wallet.js';
9
+ import type { AccountContractsProvider } from '../account-contract-providers/types.js';
10
+ import { EmbeddedWallet, type EmbeddedWalletOptions, splitPxeOptions } from '../embedded_wallet.js';
11
11
  import { WalletDB } from '../wallet_db.js';
12
12
 
13
- export type NodeEmbeddedWalletOptions = EmbeddedWalletOptions & {
14
- /** Override PXE configuration. */
15
- pxeConfig?: Partial<PXEConfig>;
16
- /** Advanced PXE creation options (custom store, prover, simulator). */
17
- pxeOptions?: PXECreationOptions;
18
- };
19
-
20
13
  export class NodeEmbeddedWallet extends EmbeddedWallet {
21
- static async create(
14
+ static async create<T extends NodeEmbeddedWallet = NodeEmbeddedWallet>(
15
+ this: new (
16
+ pxe: PXE,
17
+ aztecNode: AztecNode,
18
+ walletDB: WalletDB,
19
+ accountContracts: AccountContractsProvider,
20
+ log?: Logger,
21
+ ) => T,
22
22
  nodeOrUrl: string | AztecNode,
23
- options: NodeEmbeddedWalletOptions = {},
24
- ): Promise<NodeEmbeddedWallet> {
23
+ options: EmbeddedWalletOptions = {},
24
+ ): Promise<T> {
25
25
  const rootLogger = options.logger ?? createLogger('embedded-wallet');
26
26
 
27
27
  const aztecNode = typeof nodeOrUrl === 'string' ? createAztecNodeClient(nodeOrUrl) : nodeOrUrl;
28
+ const l1Contracts = await aztecNode.getL1ContractAddresses();
29
+
30
+ // Support both the new unified `pxe` option and the deprecated `pxeConfig`/`pxeOptions`.
31
+ const { config: pxeConfigFromPxe, creation: pxeCreationFromPxe } = splitPxeOptions(options.pxe);
32
+ const mergedConfigOverrides = { ...options.pxeConfig, ...pxeConfigFromPxe };
33
+ const mergedCreationOverrides: PXECreationOptions = { ...options.pxeOptions, ...pxeCreationFromPxe };
28
34
 
29
- const pxeConfig = Object.assign(getPXEConfig(), {
30
- proverEnabled: options.pxeConfig?.proverEnabled ?? false,
31
- ...options.pxeConfig,
35
+ const pxeConfig: PXEConfig = Object.assign(getPXEConfig(), {
36
+ proverEnabled: mergedConfigOverrides.proverEnabled ?? false,
37
+ dataDirectory: `pxe_data_${l1Contracts.rollupAddress}`,
38
+ ...mergedConfigOverrides,
32
39
  });
33
40
 
34
41
  if (options.ephemeral) {
@@ -36,45 +43,44 @@ export class NodeEmbeddedWallet extends EmbeddedWallet {
36
43
  }
37
44
 
38
45
  const pxeOptions: PXECreationOptions = {
39
- ...options.pxeOptions,
46
+ ...mergedCreationOverrides,
40
47
  loggers: {
41
48
  store: rootLogger.createChild('pxe:data'),
42
49
  pxe: rootLogger.createChild('pxe:service'),
43
50
  prover: rootLogger.createChild('pxe:prover'),
44
- ...options.pxeOptions?.loggers,
51
+ ...mergedCreationOverrides.loggers,
45
52
  },
46
53
  };
47
54
 
48
55
  const pxe = await createPXE(aztecNode, pxeConfig, pxeOptions);
49
56
 
50
- const l1Contracts = await aztecNode.getL1ContractAddresses();
51
- const rollupAddress = l1Contracts.rollupAddress;
52
-
53
- const walletDBStore = options.ephemeral
54
- ? await openTmpStore(
55
- 'wallet_data',
56
- true,
57
- undefined,
58
- undefined,
59
- rootLogger.createChild('wallet:data').getBindings(),
60
- )
61
- : await createWalletStore(
62
- 'wallet_data',
63
- 1,
64
- {
65
- dataDirectory: pxeConfig.dataDirectory,
66
- dataStoreMapSizeKb: pxeConfig.dataStoreMapSizeKb,
67
- l1Contracts: { rollupAddress },
68
- },
69
- rootLogger.createChild('wallet:data').getBindings(),
70
- );
57
+ const walletDBStore =
58
+ options.walletDb?.store ??
59
+ (options.ephemeral
60
+ ? await openTmpStore(
61
+ `wallet_data_${l1Contracts.rollupAddress}`,
62
+ true,
63
+ undefined,
64
+ undefined,
65
+ rootLogger.createChild('wallet:data').getBindings(),
66
+ )
67
+ : await createStore(
68
+ 'wallet_data',
69
+ 1,
70
+ {
71
+ dataDirectory: `wallet_data_${l1Contracts.rollupAddress}`,
72
+ dataStoreMapSizeKb: pxeConfig.dataStoreMapSizeKb,
73
+ l1Contracts,
74
+ },
75
+ rootLogger.createChild('wallet:data').getBindings(),
76
+ ));
71
77
  const walletDB = WalletDB.init(walletDBStore, rootLogger.createChild('wallet:db').info);
72
78
 
73
- return new NodeEmbeddedWallet(pxe, aztecNode, walletDB, new BundleAccountContractsProvider(), rootLogger);
79
+ return new this(pxe, aztecNode, walletDB, new BundleAccountContractsProvider(), rootLogger) as T;
74
80
  }
75
81
  }
76
82
 
77
83
  export { NodeEmbeddedWallet as EmbeddedWallet };
78
- export type { EmbeddedWalletOptions } from '../embedded_wallet.js';
84
+ export type { EmbeddedWalletOptions, EmbeddedWalletPXEOptions } from '../embedded_wallet.js';
79
85
  export { WalletDB } from '../wallet_db.js';
80
86
  export type { AccountType } from '../wallet_db.js';
package/src/testing.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { InitialAccountData } from '@aztec/accounts/testing';
2
2
  import { getInitialTestAccountsData } from '@aztec/accounts/testing/lazy';
3
+ import { NO_FROM } from '@aztec/aztec.js/account';
3
4
  import type { WaitOpts } from '@aztec/aztec.js/contracts';
4
5
  import type { AccountManager } from '@aztec/aztec.js/wallet';
5
6
  import type { Fq, Fr } from '@aztec/foundation/curves/bn254';
@@ -21,7 +22,7 @@ export async function deployFundedSchnorrAccounts(
21
22
  const accountManager = await wallet.createSchnorrAccount(secret, salt, signingKey);
22
23
  const deployMethod = await accountManager.getDeployMethod();
23
24
  await deployMethod.send({
24
- from: AztecAddress.ZERO,
25
+ from: NO_FROM,
25
26
  skipClassPublication: i !== 0,
26
27
  wait: waitOptions,
27
28
  });
@@ -1,5 +0,0 @@
1
- import { EmbeddedWallet } from './embedded_wallet.js';
2
- export declare class BrowserEmbeddedWallet extends EmbeddedWallet {
3
- static create(nodeUrl: string): Promise<BrowserEmbeddedWallet>;
4
- }
5
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZW1iZWRkZWRfd2FsbGV0X2Jyb3dzZXIuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9lbWJlZGRlZC9lbWJlZGRlZF93YWxsZXRfYnJvd3Nlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFPQSxPQUFPLEVBQUUsY0FBYyxFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFHdEQscUJBQWEscUJBQXNCLFNBQVEsY0FBYztJQUN2RCxPQUFhLE1BQU0sQ0FBQyxPQUFPLEVBQUUsTUFBTSxHQUFHLE9BQU8sQ0FBQyxxQkFBcUIsQ0FBQyxDQTJCbkU7Q0FDRiJ9
@@ -1 +0,0 @@
1
- {"version":3,"file":"embedded_wallet_browser.d.ts","sourceRoot":"","sources":["../../src/embedded/embedded_wallet_browser.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAGtD,qBAAa,qBAAsB,SAAQ,cAAc;IACvD,OAAa,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,qBAAqB,CAAC,CA2BnE;CACF"}
@@ -1,31 +0,0 @@
1
- import { createAztecNodeClient } from '@aztec/aztec.js/node';
2
- import { createLogger } from '@aztec/foundation/log';
3
- import { createStore } from '@aztec/kv-store/indexeddb';
4
- import { createPXE } from '@aztec/pxe/client/lazy';
5
- import { getPXEConfig } from '@aztec/pxe/config';
6
- import { LazyAccountContractsProvider } from './account-contract-providers/lazy.js';
7
- import { EmbeddedWallet } from './embedded_wallet.js';
8
- import { WalletDB } from './wallet_db.js';
9
- export class BrowserEmbeddedWallet extends EmbeddedWallet {
10
- static async create(nodeUrl) {
11
- const aztecNode = createAztecNodeClient(nodeUrl);
12
- const l1Contracts = await aztecNode.getL1ContractAddresses();
13
- const rollupAddress = l1Contracts.rollupAddress;
14
- const config = getPXEConfig();
15
- config.dataDirectory = `pxe-${rollupAddress}`;
16
- const pxe = await createPXE(aztecNode, config, {
17
- loggers: {
18
- store: createLogger('pxe:data:idb'),
19
- pxe: createLogger('pxe:service'),
20
- prover: createLogger('bb:wasm:lazy')
21
- }
22
- });
23
- const logger = createLogger('embedded-wallet:browser');
24
- const walletDBStore = await createStore(`wallet-${rollupAddress}`, {
25
- dataDirectory: 'wallet',
26
- dataStoreMapSizeKb: 2e10
27
- }, undefined, createLogger('wallet:data:idb'));
28
- const walletDB = WalletDB.init(walletDBStore, logger.info);
29
- return new BrowserEmbeddedWallet(pxe, aztecNode, walletDB, new LazyAccountContractsProvider());
30
- }
31
- }