@aztec/wallets 0.0.1-commit.f224bb98b → 0.0.1-commit.f5a9928
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/embedded/account-contract-providers/bundle.d.ts +6 -8
- package/dest/embedded/account-contract-providers/bundle.d.ts.map +1 -1
- package/dest/embedded/account-contract-providers/bundle.js +12 -10
- package/dest/embedded/account-contract-providers/lazy.d.ts +6 -8
- package/dest/embedded/account-contract-providers/lazy.d.ts.map +1 -1
- package/dest/embedded/account-contract-providers/lazy.js +20 -10
- package/dest/embedded/account-contract-providers/types.d.ts +6 -8
- package/dest/embedded/account-contract-providers/types.d.ts.map +1 -1
- package/dest/embedded/embedded_wallet.d.ts +74 -11
- package/dest/embedded/embedded_wallet.d.ts.map +1 -1
- package/dest/embedded/embedded_wallet.js +260 -72
- package/dest/embedded/entrypoints/browser.d.ts +3 -3
- package/dest/embedded/entrypoints/browser.d.ts.map +1 -1
- package/dest/embedded/entrypoints/browser.js +43 -19
- package/dest/embedded/entrypoints/node.d.ts +2 -2
- package/dest/embedded/entrypoints/node.d.ts.map +1 -1
- package/dest/embedded/entrypoints/node.js +46 -19
- package/dest/embedded/entrypoints/resolve_aztec_node.d.ts +5 -0
- package/dest/embedded/entrypoints/resolve_aztec_node.d.ts.map +1 -0
- package/dest/embedded/entrypoints/resolve_aztec_node.js +4 -0
- package/dest/embedded/store_encryption.d.ts +74 -0
- package/dest/embedded/store_encryption.d.ts.map +1 -0
- package/dest/embedded/store_encryption.js +93 -0
- package/dest/embedded/wallet_db.d.ts +9 -6
- package/dest/embedded/wallet_db.d.ts.map +1 -1
- package/dest/embedded/wallet_db.js +13 -11
- package/dest/testing.d.ts +8 -4
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +8 -14
- package/package.json +13 -10
- package/src/embedded/account-contract-providers/bundle.ts +15 -12
- package/src/embedded/account-contract-providers/lazy.ts +23 -12
- package/src/embedded/account-contract-providers/types.ts +6 -4
- package/src/embedded/embedded_wallet.ts +343 -82
- package/src/embedded/entrypoints/browser.ts +39 -24
- package/src/embedded/entrypoints/node.ts +43 -28
- package/src/embedded/entrypoints/resolve_aztec_node.ts +20 -0
- package/src/embedded/store_encryption.ts +146 -0
- package/src/embedded/wallet_db.ts +18 -12
- package/src/testing.ts +11 -16
|
@@ -1,33 +1,117 @@
|
|
|
1
|
-
import { type Account,
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
1
|
+
import { type Account, NO_FROM } from '@aztec/aztec.js/account';
|
|
2
|
+
import { CallAuthorizationRequest } from '@aztec/aztec.js/authorization';
|
|
3
|
+
import {
|
|
4
|
+
ContractFunctionInteraction,
|
|
5
|
+
type InteractionWaitOptions,
|
|
6
|
+
NO_WAIT,
|
|
7
|
+
type SendReturn,
|
|
8
|
+
type WaitOpts,
|
|
9
|
+
} from '@aztec/aztec.js/contracts';
|
|
10
|
+
import type {
|
|
11
|
+
Aliased,
|
|
12
|
+
ExecuteUtilityOptions,
|
|
13
|
+
PrivateEvent,
|
|
14
|
+
PrivateEventFilter,
|
|
15
|
+
ProfileOptions,
|
|
16
|
+
SendOptions,
|
|
17
|
+
SimulateOptions,
|
|
18
|
+
} from '@aztec/aztec.js/wallet';
|
|
19
|
+
import { AccountManager, TxSimulationResultWithAppOffset } from '@aztec/aztec.js/wallet';
|
|
4
20
|
import type { DefaultAccountEntrypointOptions } from '@aztec/entrypoints/account';
|
|
21
|
+
import { DefaultEntrypoint } from '@aztec/entrypoints/default';
|
|
22
|
+
import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
|
|
23
|
+
import { Schnorr } from '@aztec/foundation/crypto/schnorr';
|
|
5
24
|
import { Fq, Fr } from '@aztec/foundation/curves/bn254';
|
|
25
|
+
import type { JsonRpcFetch, JsonRpcFetchConfig } from '@aztec/foundation/json-rpc/client';
|
|
6
26
|
import type { Logger } from '@aztec/foundation/log';
|
|
7
|
-
import type {
|
|
27
|
+
import type { AztecAsyncKVStore } from '@aztec/kv-store';
|
|
28
|
+
import type { PXEConfig, PXECreationOptions } from '@aztec/pxe/client/lazy';
|
|
8
29
|
import type { PXE } from '@aztec/pxe/server';
|
|
30
|
+
import type { EventMetadataDefinition, FunctionCall } from '@aztec/stdlib/abi';
|
|
9
31
|
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
10
|
-
import {
|
|
32
|
+
import { getContractClassFromArtifact } from '@aztec/stdlib/contract';
|
|
33
|
+
import { GasSettings } from '@aztec/stdlib/gas';
|
|
11
34
|
import type { AztecNode } from '@aztec/stdlib/interfaces/client';
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
35
|
+
import {
|
|
36
|
+
type ContractOverrides,
|
|
37
|
+
ExecutionPayload,
|
|
38
|
+
SimulationOverrides,
|
|
39
|
+
type TxExecutionRequest,
|
|
40
|
+
type TxProfileResult,
|
|
41
|
+
TxStatus,
|
|
42
|
+
type UtilityExecutionResult,
|
|
43
|
+
collectOffchainEffects,
|
|
44
|
+
mergeExecutionPayloads,
|
|
45
|
+
} from '@aztec/stdlib/tx';
|
|
46
|
+
import { BaseWallet, type SimulateViaEntrypointOptions, getGasLimits } from '@aztec/wallet-sdk/base-wallet';
|
|
15
47
|
|
|
16
48
|
import type { AccountContractsProvider } from './account-contract-providers/types.js';
|
|
17
49
|
import { type AccountType, WalletDB } from './wallet_db.js';
|
|
18
50
|
|
|
51
|
+
/** Options for the PXE instance created by the EmbeddedWallet. */
|
|
52
|
+
export type EmbeddedWalletPXEOptions = Partial<PXEConfig> & PXECreationOptions;
|
|
53
|
+
|
|
54
|
+
/** Splits a unified EmbeddedWalletPXEOptions into PXEConfig overrides and PXECreationOptions. */
|
|
55
|
+
export function splitPxeOptions(pxe?: EmbeddedWalletPXEOptions): {
|
|
56
|
+
config: Partial<PXEConfig>;
|
|
57
|
+
creation: PXECreationOptions;
|
|
58
|
+
} {
|
|
59
|
+
if (!pxe) {
|
|
60
|
+
return { config: {}, creation: {} };
|
|
61
|
+
}
|
|
62
|
+
const { loggers, loggerActorLabel, proverOrOptions, store, simulator, hooks, preloadedContractsProvider, ...config } =
|
|
63
|
+
pxe;
|
|
64
|
+
return {
|
|
65
|
+
config,
|
|
66
|
+
creation: { loggers, loggerActorLabel, proverOrOptions, store, simulator, hooks, preloadedContractsProvider },
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Options for the EmbeddedWallet's own DB (accounts, senders — distinct from PXE state). */
|
|
71
|
+
export type EmbeddedWalletDBOptions = {
|
|
72
|
+
/** Override the wallet DB backend. If omitted, an IndexedDB (browser) / LMDB (node) store is created. */
|
|
73
|
+
store?: AztecAsyncKVStore;
|
|
74
|
+
};
|
|
75
|
+
|
|
19
76
|
export type EmbeddedWalletOptions = {
|
|
20
77
|
/** Parent logger. Child loggers are derived via createChild() for each subsystem. */
|
|
21
78
|
logger?: Logger;
|
|
22
79
|
/** Use ephemeral (in-memory) stores. Data will not persist across sessions. */
|
|
23
80
|
ephemeral?: boolean;
|
|
24
|
-
/**
|
|
81
|
+
/** PXE configuration and dependency overrides (custom store, prover, simulator). */
|
|
82
|
+
pxe?: EmbeddedWalletPXEOptions;
|
|
83
|
+
/** Wallet DB dependency overrides (custom store). */
|
|
84
|
+
walletDb?: EmbeddedWalletDBOptions;
|
|
85
|
+
/** JSON-RPC client options used when the wallet is created from a node URL. */
|
|
86
|
+
nodeClientOptions?: {
|
|
87
|
+
/** Custom JSON-RPC transport. Takes precedence over fetchOptions. */
|
|
88
|
+
fetch?: JsonRpcFetch;
|
|
89
|
+
/** Configuration for the default retrying fetch transport. */
|
|
90
|
+
fetchOptions?: JsonRpcFetchConfig;
|
|
91
|
+
/** Maximum number of calls sent in one JSON-RPC batch. */
|
|
92
|
+
maxBatchSize?: number;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Override PXE configuration.
|
|
96
|
+
* @deprecated Use `pxe` instead.
|
|
97
|
+
*/
|
|
25
98
|
pxeConfig?: Partial<PXEConfig>;
|
|
26
|
-
/**
|
|
99
|
+
/**
|
|
100
|
+
* Advanced PXE creation options (custom store, prover, simulator).
|
|
101
|
+
* @deprecated Use `pxe` instead.
|
|
102
|
+
*/
|
|
27
103
|
pxeOptions?: PXECreationOptions;
|
|
28
104
|
};
|
|
29
105
|
|
|
106
|
+
const DEFAULT_ESTIMATED_GAS_PADDING = 0.1;
|
|
107
|
+
|
|
30
108
|
export class EmbeddedWallet extends BaseWallet {
|
|
109
|
+
protected estimatedGasPadding = DEFAULT_ESTIMATED_GAS_PADDING;
|
|
110
|
+
|
|
111
|
+
// Stub class ids, populated on wallet startup
|
|
112
|
+
// to avoid redundant work per simulation
|
|
113
|
+
protected stubClassIds = new Map<AccountType, Fr>();
|
|
114
|
+
|
|
31
115
|
constructor(
|
|
32
116
|
pxe: PXE,
|
|
33
117
|
aztecNode: AztecNode,
|
|
@@ -39,10 +123,6 @@ export class EmbeddedWallet extends BaseWallet {
|
|
|
39
123
|
}
|
|
40
124
|
|
|
41
125
|
protected async getAccountFromAddress(address: AztecAddress): Promise<Account> {
|
|
42
|
-
if (address.equals(AztecAddress.ZERO)) {
|
|
43
|
-
return new SignerlessAccount();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
126
|
const { secretKey, salt, signingKey, type } = await this.walletDB.retrieveAccount(address);
|
|
47
127
|
const accountManager = await this.createAccountInternal(type, secretKey, salt, signingKey);
|
|
48
128
|
const account = await accountManager.getAccount();
|
|
@@ -60,20 +140,195 @@ export class EmbeddedWallet extends BaseWallet {
|
|
|
60
140
|
|
|
61
141
|
override async registerSender(address: AztecAddress, alias: string) {
|
|
62
142
|
await this.walletDB.storeSender(address, alias);
|
|
63
|
-
|
|
143
|
+
await this.pxe.registerTaggingSecretSource({ kind: 'address-derived', sender: address });
|
|
144
|
+
return address;
|
|
64
145
|
}
|
|
65
146
|
|
|
66
147
|
override async getAddressBook(): Promise<Aliased<AztecAddress>[]> {
|
|
67
|
-
const
|
|
148
|
+
const sources = await this.pxe.getTaggingSecretSources({ kind: 'address-derived' });
|
|
149
|
+
const senders = sources.map(source => source.sender);
|
|
68
150
|
const storedSenders = await this.walletDB.listSenders();
|
|
69
151
|
for (const storedSender of storedSenders) {
|
|
70
152
|
if (senders.findIndex(sender => sender.equals(storedSender.item)) === -1) {
|
|
71
|
-
await this.pxe.
|
|
153
|
+
await this.pxe.registerTaggingSecretSource({ kind: 'address-derived', sender: storedSender.item });
|
|
72
154
|
}
|
|
73
155
|
}
|
|
74
156
|
return storedSenders;
|
|
75
157
|
}
|
|
76
158
|
|
|
159
|
+
/**
|
|
160
|
+
* Overrides the base sendTx to add a pre-simulation step before the actual send. The simulation
|
|
161
|
+
* estimates actual gas usage and captures call authorization requests to generate
|
|
162
|
+
* the necessary authwitnesses.
|
|
163
|
+
*/
|
|
164
|
+
public override async sendTx<W extends InteractionWaitOptions = undefined>(
|
|
165
|
+
executionPayload: ExecutionPayload,
|
|
166
|
+
opts: SendOptions<W>,
|
|
167
|
+
): Promise<SendReturn<W>> {
|
|
168
|
+
// PXE has autoSync disabled by the embedded wallet entrypoints, so we sync once here to cover
|
|
169
|
+
// both the inner simulateTx (via simulateViaEntrypoint) and the proveTx that super.sendTx
|
|
170
|
+
await this.pxe.sync();
|
|
171
|
+
const feeOptions = await this.completeFeeOptions({
|
|
172
|
+
from: opts.from,
|
|
173
|
+
feePayer: executionPayload.feePayer,
|
|
174
|
+
gasSettings: opts.fee?.gasSettings,
|
|
175
|
+
forEstimation: true,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// Simulate the transaction first to estimate gas and capture required
|
|
179
|
+
// private authwitnesses based on offchain effects.
|
|
180
|
+
const simulationResult = await this.simulateViaEntrypoint(executionPayload, {
|
|
181
|
+
from: opts.from,
|
|
182
|
+
feeOptions,
|
|
183
|
+
additionalScopes: opts.additionalScopes,
|
|
184
|
+
skipTxValidation: true,
|
|
185
|
+
sendMessagesAs: opts.sendMessagesAs,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
const offchainEffects = collectOffchainEffects(simulationResult.privateExecutionResult);
|
|
189
|
+
const authWitnesses = await Promise.all(
|
|
190
|
+
offchainEffects.map(async effect => {
|
|
191
|
+
try {
|
|
192
|
+
const authRequest = await CallAuthorizationRequest.fromFields(effect.data);
|
|
193
|
+
return this.createAuthWit(authRequest.onBehalfOf, {
|
|
194
|
+
consumer: effect.contractAddress,
|
|
195
|
+
innerHash: authRequest.innerHash,
|
|
196
|
+
});
|
|
197
|
+
} catch {
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
}),
|
|
201
|
+
);
|
|
202
|
+
for (const authwit of authWitnesses) {
|
|
203
|
+
if (authwit) {
|
|
204
|
+
executionPayload.authWitnesses.push(authwit);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
const maxTxGasLimits = await this.getMaxTxGasLimits();
|
|
208
|
+
const estimated = getGasLimits(simulationResult.gasUsed, maxTxGasLimits, this.estimatedGasPadding);
|
|
209
|
+
this.log.verbose(
|
|
210
|
+
`Estimated gas limits for tx: DA=${estimated.gasLimits.daGas} L2=${estimated.gasLimits.l2Gas} teardownDA=${estimated.teardownGasLimits.daGas} teardownL2=${estimated.teardownGasLimits.l2Gas}`,
|
|
211
|
+
);
|
|
212
|
+
const gasSettings = GasSettings.from({
|
|
213
|
+
...opts.fee?.gasSettings,
|
|
214
|
+
maxFeesPerGas: feeOptions.gasSettings.maxFeesPerGas,
|
|
215
|
+
maxPriorityFeesPerGas: feeOptions.gasSettings.maxPriorityFeesPerGas,
|
|
216
|
+
gasLimits: opts.fee?.gasSettings?.gasLimits ?? estimated.gasLimits,
|
|
217
|
+
teardownGasLimits: opts.fee?.gasSettings?.teardownGasLimits ?? estimated.teardownGasLimits,
|
|
218
|
+
});
|
|
219
|
+
let wait: InteractionWaitOptions = opts.wait;
|
|
220
|
+
if (wait !== NO_WAIT) {
|
|
221
|
+
const callerWaitOpts: WaitOpts = typeof wait === 'object' ? wait : {};
|
|
222
|
+
wait = {
|
|
223
|
+
...callerWaitOpts,
|
|
224
|
+
// Default to PROPOSED so the wait returns as soon as the tx lands in a proposed L2 block,
|
|
225
|
+
// rather than waiting until the end of the slot for the checkpoint to be published to L1.
|
|
226
|
+
// This is what makes MBPS (Multiple Blocks Per Slot) actually improve UX: with CHECKPOINTED
|
|
227
|
+
// we'd block until L1 inclusion regardless of how early in the slot the tx was sequenced.
|
|
228
|
+
// The tradeoff is a weaker guarantee — a proposed block only becomes canonical once it (or
|
|
229
|
+
// a later block in the same slot) is checkpointed, so a tx could be re-orged out if the
|
|
230
|
+
// proposer fails to publish to L1 (which should be rare, since they'd get slashed for it).
|
|
231
|
+
waitForStatus: callerWaitOpts.waitForStatus ?? TxStatus.PROPOSED,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
return super.sendTx(executionPayload, {
|
|
235
|
+
...opts,
|
|
236
|
+
wait: wait as W,
|
|
237
|
+
fee: { ...opts.fee, gasSettings },
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Overrides the base simulateTx to drive PXE syncing explicitly. The PXE created by the embedded
|
|
243
|
+
* wallet has autoSync disabled (so we can share one sync across simulate+send in sendTx); for
|
|
244
|
+
* standalone simulations we still need a fresh anchor block, which we provide here.
|
|
245
|
+
*/
|
|
246
|
+
public override async simulateTx(
|
|
247
|
+
executionPayload: ExecutionPayload,
|
|
248
|
+
opts: SimulateOptions,
|
|
249
|
+
): Promise<TxSimulationResultWithAppOffset> {
|
|
250
|
+
await this.pxe.sync();
|
|
251
|
+
return super.simulateTx(executionPayload, opts);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
public override async profileTx(executionPayload: ExecutionPayload, opts: ProfileOptions): Promise<TxProfileResult> {
|
|
255
|
+
await this.pxe.sync();
|
|
256
|
+
return super.profileTx(executionPayload, opts);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
public override async executeUtility(
|
|
260
|
+
call: FunctionCall,
|
|
261
|
+
opts: ExecuteUtilityOptions,
|
|
262
|
+
): Promise<UtilityExecutionResult> {
|
|
263
|
+
await this.pxe.sync();
|
|
264
|
+
return super.executeUtility(call, opts);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
public override async getPrivateEvents<T>(
|
|
268
|
+
eventDef: EventMetadataDefinition,
|
|
269
|
+
eventFilter: PrivateEventFilter,
|
|
270
|
+
): Promise<PrivateEvent<T>[]> {
|
|
271
|
+
await this.pxe.sync();
|
|
272
|
+
return super.getPrivateEvents<T>(eventDef, eventFilter);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Hashes and registers the stub class for every supported account type with PXE, populating
|
|
277
|
+
* stubClassIds. Called on wallet initialization.
|
|
278
|
+
*/
|
|
279
|
+
async initStubClasses(): Promise<void> {
|
|
280
|
+
const schnorrArtifact = await this.accountContracts.getStubAccountContractArtifact('schnorr');
|
|
281
|
+
const { id: schnorrClassId } = await getContractClassFromArtifact(schnorrArtifact);
|
|
282
|
+
await this.pxe.registerContractClass(schnorrArtifact);
|
|
283
|
+
|
|
284
|
+
// ecdsa stubs share the same class id
|
|
285
|
+
const ecdsaArtifact = await this.accountContracts.getStubAccountContractArtifact('ecdsasecp256r1');
|
|
286
|
+
const { id: ecdsaClassId } = await getContractClassFromArtifact(ecdsaArtifact);
|
|
287
|
+
await this.pxe.registerContractClass(ecdsaArtifact);
|
|
288
|
+
|
|
289
|
+
this.stubClassIds.set('schnorr', schnorrClassId);
|
|
290
|
+
this.stubClassIds.set('schnorr_initializerless', schnorrClassId);
|
|
291
|
+
this.stubClassIds.set('ecdsasecp256k1', ecdsaClassId);
|
|
292
|
+
this.stubClassIds.set('ecdsasecp256r1', ecdsaClassId);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Builds contract overrides for all provided addresses by replacing their account contracts with stub implementations.
|
|
297
|
+
* Uses a type-specific stub artifact so that the stub's constructor selector matches the real account's constructor.
|
|
298
|
+
*/
|
|
299
|
+
protected async buildAccountOverrides(addresses: AztecAddress[]): Promise<ContractOverrides> {
|
|
300
|
+
const accounts = await this.getAccounts();
|
|
301
|
+
const contracts: ContractOverrides = {};
|
|
302
|
+
|
|
303
|
+
const filtered = accounts.filter(acc => addresses.some(addr => addr.equals(acc.item)));
|
|
304
|
+
|
|
305
|
+
for (const account of filtered) {
|
|
306
|
+
const address = account.item;
|
|
307
|
+
const { type } = await this.walletDB.retrieveAccount(address);
|
|
308
|
+
const stubClassId = this.stubClassIds.get(type);
|
|
309
|
+
if (!stubClassId) {
|
|
310
|
+
throw new Error(
|
|
311
|
+
`Stub class for account type '${type}' was not registered at wallet init. This is a bug — initStubClasses should cover every supported AccountType.`,
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const originalAccount = await this.getAccountFromAddress(address);
|
|
316
|
+
const completeAddress = originalAccount.getCompleteAddress();
|
|
317
|
+
const contractInstance = await this.pxe.getContractInstance(completeAddress.address);
|
|
318
|
+
if (!contractInstance) {
|
|
319
|
+
throw new Error(
|
|
320
|
+
`No contract instance found for address: ${completeAddress.address} during account override building. This is a bug!`,
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
contracts[address.toString()] = {
|
|
325
|
+
instance: { ...contractInstance, currentContractClassId: stubClassId },
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return contracts;
|
|
330
|
+
}
|
|
331
|
+
|
|
77
332
|
/**
|
|
78
333
|
* Simulates calls via a stub account entrypoint, bypassing real account authorization.
|
|
79
334
|
* This allows kernelless simulation with contract overrides, skipping expensive
|
|
@@ -81,75 +336,53 @@ export class EmbeddedWallet extends BaseWallet {
|
|
|
81
336
|
*/
|
|
82
337
|
protected override async simulateViaEntrypoint(
|
|
83
338
|
executionPayload: ExecutionPayload,
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
_skipFeeEnforcement?: boolean,
|
|
89
|
-
): Promise<TxSimulationResult> {
|
|
90
|
-
const { account: fromAccount, instance, artifact } = await this.getFakeAccountDataFor(from);
|
|
339
|
+
opts: SimulateViaEntrypointOptions,
|
|
340
|
+
): Promise<TxSimulationResultWithAppOffset> {
|
|
341
|
+
const { from, feeOptions, additionalScopes, skipTxValidation, skipFeeEnforcement, sendMessagesAs } = opts;
|
|
342
|
+
const scopes = this.scopesFrom(from, additionalScopes ?? [], sendMessagesAs);
|
|
91
343
|
|
|
92
344
|
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
93
|
-
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
94
|
-
txNonce: Fr.random(),
|
|
95
|
-
cancellable: this.cancellableTransactions,
|
|
96
|
-
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions,
|
|
97
|
-
};
|
|
98
345
|
const finalExecutionPayload = feeExecutionPayload
|
|
99
346
|
? mergeExecutionPayloads([feeExecutionPayload, executionPayload])
|
|
100
347
|
: executionPayload;
|
|
101
348
|
const chainInfo = await this.getChainInfo();
|
|
102
|
-
const txRequest = await fromAccount.createTxExecutionRequest(
|
|
103
|
-
finalExecutionPayload,
|
|
104
|
-
feeOptions.gasSettings,
|
|
105
|
-
chainInfo,
|
|
106
|
-
executionOptions,
|
|
107
|
-
);
|
|
108
|
-
return this.pxe.simulateTx(txRequest, {
|
|
109
|
-
simulatePublic: true,
|
|
110
|
-
skipFeeEnforcement: true,
|
|
111
|
-
skipTxValidation: true,
|
|
112
|
-
overrides: {
|
|
113
|
-
contracts: { [from.toString()]: { instance, artifact } },
|
|
114
|
-
},
|
|
115
|
-
scopes,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
349
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if (originalAccount instanceof SignerlessAccount) {
|
|
127
|
-
throw new Error(`Cannot create fake account data for SignerlessAccount at address: ${address}`);
|
|
128
|
-
}
|
|
129
|
-
const originalAddress = (originalAccount as Account).getCompleteAddress();
|
|
130
|
-
const contractInstance = await this.pxe.getContractInstance(originalAddress.address);
|
|
131
|
-
if (!contractInstance) {
|
|
132
|
-
throw new Error(`No contract instance found for address: ${originalAddress.address}`);
|
|
133
|
-
}
|
|
134
|
-
const stubAccount = await this.accountContracts.createStubAccount(originalAddress);
|
|
135
|
-
const stubArtifact = await this.accountContracts.getStubAccountContractArtifact();
|
|
136
|
-
const instance = await getContractInstanceFromInstantiationParams(stubArtifact, {
|
|
137
|
-
salt: Fr.random(),
|
|
138
|
-
});
|
|
139
|
-
return {
|
|
140
|
-
account: stubAccount,
|
|
141
|
-
instance,
|
|
142
|
-
artifact: stubArtifact,
|
|
143
|
-
};
|
|
350
|
+
const accountOverrides = await this.buildAccountOverrides(scopes);
|
|
351
|
+
const overrides = new SimulationOverrides({ contracts: accountOverrides });
|
|
352
|
+
|
|
353
|
+
let txRequest: TxExecutionRequest;
|
|
354
|
+
if (from === NO_FROM) {
|
|
355
|
+
const entrypoint = new DefaultEntrypoint();
|
|
356
|
+
txRequest = await entrypoint.createTxExecutionRequest(finalExecutionPayload, feeOptions.gasSettings, chainInfo);
|
|
144
357
|
} else {
|
|
145
|
-
const {
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
358
|
+
const { type } = await this.walletDB.retrieveAccount(from);
|
|
359
|
+
const originalAccount = await this.getAccountFromAddress(from);
|
|
360
|
+
const completeAddress = originalAccount.getCompleteAddress();
|
|
361
|
+
const account = await this.accountContracts.createStubAccount(completeAddress, type);
|
|
362
|
+
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
363
|
+
txNonce: Fr.random(),
|
|
364
|
+
cancellable: this.cancellableTransactions,
|
|
365
|
+
// If from is an address, feeOptions include the way the account contract should handle the fee payment
|
|
366
|
+
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions!,
|
|
151
367
|
};
|
|
368
|
+
txRequest = await account.createTxExecutionRequest(
|
|
369
|
+
finalExecutionPayload,
|
|
370
|
+
feeOptions.gasSettings,
|
|
371
|
+
chainInfo,
|
|
372
|
+
executionOptions,
|
|
373
|
+
);
|
|
152
374
|
}
|
|
375
|
+
|
|
376
|
+
const result = await this.pxe.simulateTx(txRequest, {
|
|
377
|
+
simulatePublic: true,
|
|
378
|
+
skipFeeEnforcement,
|
|
379
|
+
skipTxValidation,
|
|
380
|
+
overrides,
|
|
381
|
+
scopes,
|
|
382
|
+
senderForTags: this.senderForTagsFrom(from, sendMessagesAs),
|
|
383
|
+
});
|
|
384
|
+
const appCallOffset = await this.computeAppCallOffset(from, feeOptions);
|
|
385
|
+
return TxSimulationResultWithAppOffset.fromResultAndOffset(result, appCallOffset);
|
|
153
386
|
}
|
|
154
387
|
|
|
155
388
|
protected async createAccountInternal(
|
|
@@ -159,11 +392,19 @@ export class EmbeddedWallet extends BaseWallet {
|
|
|
159
392
|
signingKey: Buffer,
|
|
160
393
|
): Promise<AccountManager> {
|
|
161
394
|
let contract;
|
|
395
|
+
let immutablesHash;
|
|
396
|
+
let publicKey;
|
|
162
397
|
switch (type) {
|
|
163
398
|
case 'schnorr': {
|
|
164
399
|
contract = await this.accountContracts.getSchnorrAccountContract(Fq.fromBuffer(signingKey));
|
|
165
400
|
break;
|
|
166
401
|
}
|
|
402
|
+
case 'schnorr_initializerless': {
|
|
403
|
+
contract = await this.accountContracts.getSchnorrInitializerlessAccountContract(Fq.fromBuffer(signingKey));
|
|
404
|
+
publicKey = await new Schnorr().computePublicKey(Fq.fromBuffer(signingKey));
|
|
405
|
+
immutablesHash = await poseidon2Hash([publicKey.x, publicKey.y]);
|
|
406
|
+
break;
|
|
407
|
+
}
|
|
167
408
|
case 'ecdsasecp256k1': {
|
|
168
409
|
contract = await this.accountContracts.getEcdsaKAccountContract(signingKey);
|
|
169
410
|
break;
|
|
@@ -177,17 +418,29 @@ export class EmbeddedWallet extends BaseWallet {
|
|
|
177
418
|
}
|
|
178
419
|
}
|
|
179
420
|
|
|
180
|
-
const accountManager = await AccountManager.create(this, secret, contract, salt);
|
|
421
|
+
const accountManager = await AccountManager.create(this, secret, contract, { salt, immutablesHash });
|
|
181
422
|
|
|
182
423
|
const instance = accountManager.getInstance();
|
|
183
424
|
const existingInstance = await this.pxe.getContractInstance(instance.address);
|
|
184
425
|
if (!existingInstance) {
|
|
185
426
|
const existingArtifact = await this.pxe.getContractArtifact(instance.currentContractClassId);
|
|
427
|
+
const artifact = existingArtifact ?? (await accountManager.getAccountContract().getContractArtifact());
|
|
186
428
|
await this.registerContract(
|
|
187
429
|
instance,
|
|
188
430
|
!existingArtifact ? await accountManager.getAccountContract().getContractArtifact() : undefined,
|
|
189
431
|
accountManager.getSecretKey(),
|
|
190
432
|
);
|
|
433
|
+
if (type === 'schnorr_initializerless') {
|
|
434
|
+
const constructor = artifact.functions.find(f => f.name === 'constructor');
|
|
435
|
+
if (!constructor) {
|
|
436
|
+
throw new Error('Could not create SchnorrInitializerlessAccountContract: constructor ABI not found');
|
|
437
|
+
}
|
|
438
|
+
const storeCall = new ContractFunctionInteraction(this, instance.address, constructor, [
|
|
439
|
+
publicKey!.x,
|
|
440
|
+
publicKey!.y,
|
|
441
|
+
]);
|
|
442
|
+
await storeCall.simulate({ from: instance.address });
|
|
443
|
+
}
|
|
191
444
|
}
|
|
192
445
|
return accountManager;
|
|
193
446
|
}
|
|
@@ -204,9 +457,12 @@ export class EmbeddedWallet extends BaseWallet {
|
|
|
204
457
|
return accountManager;
|
|
205
458
|
}
|
|
206
459
|
|
|
207
|
-
createSchnorrAccount(secret: Fr, salt: Fr, signingKey
|
|
208
|
-
|
|
209
|
-
|
|
460
|
+
createSchnorrAccount(secret: Fr, salt: Fr, signingKey: Fq, alias?: string): Promise<AccountManager> {
|
|
461
|
+
return this.createAndStoreAccount(alias ?? '', 'schnorr', secret, salt, signingKey.toBuffer());
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
createSchnorrInitializerlessAccount(secret: Fr, salt: Fr, signingKey: Fq, alias?: string): Promise<AccountManager> {
|
|
465
|
+
return this.createAndStoreAccount(alias ?? '', 'schnorr_initializerless', secret, salt, signingKey.toBuffer());
|
|
210
466
|
}
|
|
211
467
|
|
|
212
468
|
createECDSARAccount(secret: Fr, salt: Fr, signingKey: Buffer, alias?: string): Promise<AccountManager> {
|
|
@@ -221,7 +477,12 @@ export class EmbeddedWallet extends BaseWallet {
|
|
|
221
477
|
this.minFeePadding = value ?? 0.5;
|
|
222
478
|
}
|
|
223
479
|
|
|
224
|
-
|
|
225
|
-
|
|
480
|
+
setEstimatedGasPadding(value?: number) {
|
|
481
|
+
this.estimatedGasPadding = value ?? DEFAULT_ESTIMATED_GAS_PADDING;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
async stop(): Promise<void> {
|
|
485
|
+
await this.pxe.stop();
|
|
486
|
+
await this.walletDB.close();
|
|
226
487
|
}
|
|
227
488
|
}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { AztecNode } from '@aztec/aztec.js/node';
|
|
2
2
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
3
|
-
import {
|
|
4
|
-
import { type PXE, type PXECreationOptions, createPXE } from '@aztec/pxe/client/lazy';
|
|
3
|
+
import { openTmpStore } from '@aztec/kv-store/sqlite-opfs';
|
|
4
|
+
import { type PXE, type PXECreationOptions, createPXE, openBrowserStore } from '@aztec/pxe/client/lazy';
|
|
5
5
|
import { type PXEConfig, getPXEConfig } from '@aztec/pxe/config';
|
|
6
|
+
import { getDefaultStandardPreloadedContracts } from '@aztec/standard-contracts/preloaded/lazy';
|
|
6
7
|
|
|
7
8
|
import { LazyAccountContractsProvider } from '../account-contract-providers/lazy.js';
|
|
8
9
|
import type { AccountContractsProvider } from '../account-contract-providers/types.js';
|
|
9
|
-
import { EmbeddedWallet, type EmbeddedWalletOptions } from '../embedded_wallet.js';
|
|
10
|
-
import { WalletDB } from '../wallet_db.js';
|
|
10
|
+
import { EmbeddedWallet, type EmbeddedWalletOptions, splitPxeOptions } from '../embedded_wallet.js';
|
|
11
|
+
import { WALLET_DATA_SCHEMA_VERSION, WalletDB } from '../wallet_db.js';
|
|
12
|
+
import { resolveAztecNode } from './resolve_aztec_node.js';
|
|
11
13
|
|
|
12
14
|
export class BrowserEmbeddedWallet extends EmbeddedWallet {
|
|
13
15
|
static async create<T extends BrowserEmbeddedWallet = BrowserEmbeddedWallet>(
|
|
@@ -23,13 +25,19 @@ export class BrowserEmbeddedWallet extends EmbeddedWallet {
|
|
|
23
25
|
): Promise<T> {
|
|
24
26
|
const rootLogger = options.logger ?? createLogger('embedded-wallet');
|
|
25
27
|
|
|
26
|
-
const aztecNode =
|
|
27
|
-
|
|
28
|
+
const aztecNode = resolveAztecNode(nodeOrUrl, options.nodeClientOptions);
|
|
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
35
|
const pxeConfig: PXEConfig = Object.assign(getPXEConfig(), {
|
|
30
|
-
proverEnabled:
|
|
31
|
-
|
|
32
|
-
|
|
36
|
+
proverEnabled: mergedConfigOverrides.proverEnabled,
|
|
37
|
+
// Unused in the browser: sqlite-opfs keys stores by name, not directory.
|
|
38
|
+
dataDirectory: 'pxe_data',
|
|
39
|
+
autoSync: false,
|
|
40
|
+
...mergedConfigOverrides,
|
|
33
41
|
});
|
|
34
42
|
|
|
35
43
|
if (options.ephemeral) {
|
|
@@ -37,36 +45,43 @@ export class BrowserEmbeddedWallet extends EmbeddedWallet {
|
|
|
37
45
|
}
|
|
38
46
|
|
|
39
47
|
const pxeOptions: PXECreationOptions = {
|
|
40
|
-
...
|
|
48
|
+
...mergedCreationOverrides,
|
|
49
|
+
preloadedContractsProvider: mergedCreationOverrides.preloadedContractsProvider ?? {
|
|
50
|
+
getPreloadedContracts: getDefaultStandardPreloadedContracts,
|
|
51
|
+
},
|
|
41
52
|
loggers: {
|
|
42
53
|
store: rootLogger.createChild('pxe:data'),
|
|
43
54
|
pxe: rootLogger.createChild('pxe:service'),
|
|
44
55
|
prover: rootLogger.createChild('pxe:prover'),
|
|
45
|
-
...
|
|
56
|
+
...mergedCreationOverrides.loggers,
|
|
46
57
|
},
|
|
47
58
|
};
|
|
48
59
|
|
|
49
60
|
const pxe = await createPXE(aztecNode, pxeConfig, pxeOptions);
|
|
50
61
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
62
|
+
let walletDBStore = options.walletDb?.store;
|
|
63
|
+
if (!walletDBStore) {
|
|
64
|
+
if (options.ephemeral) {
|
|
65
|
+
walletDBStore = await openTmpStore(true);
|
|
66
|
+
} else {
|
|
67
|
+
const { l1ChainId, l1ContractAddresses } = await aztecNode.getNodeInfo();
|
|
68
|
+
walletDBStore = await openBrowserStore(
|
|
54
69
|
'wallet_data',
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
dataStoreMapSizeKb: pxeConfig.dataStoreMapSizeKb,
|
|
58
|
-
l1Contracts,
|
|
59
|
-
},
|
|
60
|
-
1,
|
|
70
|
+
WALLET_DATA_SCHEMA_VERSION,
|
|
71
|
+
{ l1ChainId, rollupAddress: l1ContractAddresses.rollupAddress },
|
|
61
72
|
rootLogger.createChild('wallet:data'),
|
|
62
73
|
);
|
|
63
|
-
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const walletDB = new WalletDB(walletDBStore, rootLogger.createChild('wallet:db').info);
|
|
64
77
|
|
|
65
|
-
|
|
78
|
+
const wallet = new this(pxe, aztecNode, walletDB, new LazyAccountContractsProvider(), rootLogger) as T;
|
|
79
|
+
await wallet.initStubClasses();
|
|
80
|
+
return wallet;
|
|
66
81
|
}
|
|
67
82
|
}
|
|
68
83
|
|
|
69
84
|
export { BrowserEmbeddedWallet as EmbeddedWallet };
|
|
70
|
-
export type { EmbeddedWalletOptions } from '../embedded_wallet.js';
|
|
85
|
+
export type { EmbeddedWalletOptions, EmbeddedWalletPXEOptions } from '../embedded_wallet.js';
|
|
71
86
|
export { WalletDB } from '../wallet_db.js';
|
|
72
87
|
export type { AccountType } from '../wallet_db.js';
|