@aztec/test-wallet 3.0.0-devnet.2
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/bundle.d.ts +4 -0
- package/dest/bundle.d.ts.map +1 -0
- package/dest/bundle.js +2 -0
- package/dest/lazy.d.ts +4 -0
- package/dest/lazy.d.ts.map +1 -0
- package/dest/lazy.js +2 -0
- package/dest/server.d.ts +4 -0
- package/dest/server.d.ts.map +1 -0
- package/dest/server.js +2 -0
- package/dest/utils.d.ts +39 -0
- package/dest/utils.d.ts.map +1 -0
- package/dest/utils.js +65 -0
- package/dest/wallet/bundle.d.ts +23 -0
- package/dest/wallet/bundle.d.ts.map +1 -0
- package/dest/wallet/bundle.js +67 -0
- package/dest/wallet/lazy.d.ts +23 -0
- package/dest/wallet/lazy.d.ts.map +1 -0
- package/dest/wallet/lazy.js +68 -0
- package/dest/wallet/server.d.ts +23 -0
- package/dest/wallet/server.d.ts.map +1 -0
- package/dest/wallet/server.js +67 -0
- package/dest/wallet/test_wallet.d.ts +143 -0
- package/dest/wallet/test_wallet.d.ts.map +1 -0
- package/dest/wallet/test_wallet.js +174 -0
- package/package.json +94 -0
- package/src/bundle.ts +8 -0
- package/src/lazy.ts +8 -0
- package/src/server.ts +8 -0
- package/src/utils.ts +101 -0
- package/src/wallet/bundle.ts +79 -0
- package/src/wallet/lazy.ts +80 -0
- package/src/wallet/server.ts +79 -0
- package/src/wallet/test_wallet.ts +265 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { SchnorrAccountContract } from '@aztec/accounts/schnorr';
|
|
2
|
+
import type { ContractArtifact } from '@aztec/aztec.js/abi';
|
|
3
|
+
import { type Account, type AccountContract, SignerlessAccount } from '@aztec/aztec.js/account';
|
|
4
|
+
import {
|
|
5
|
+
type CallIntent,
|
|
6
|
+
type ContractFunctionInteractionCallIntent,
|
|
7
|
+
type IntentInnerHash,
|
|
8
|
+
SetPublicAuthwitContractInteraction,
|
|
9
|
+
getMessageHashFromIntent,
|
|
10
|
+
lookupValidity,
|
|
11
|
+
} from '@aztec/aztec.js/authorization';
|
|
12
|
+
import { BaseWallet, type SendOptions, type SimulateOptions } from '@aztec/aztec.js/wallet';
|
|
13
|
+
import { AccountManager } from '@aztec/aztec.js/wallet';
|
|
14
|
+
import type { DefaultAccountEntrypointOptions } from '@aztec/entrypoints/account';
|
|
15
|
+
import { ExecutionPayload, mergeExecutionPayloads } from '@aztec/entrypoints/payload';
|
|
16
|
+
import { Fq, Fr, GrumpkinScalar } from '@aztec/foundation/fields';
|
|
17
|
+
import { AuthWitness } from '@aztec/stdlib/auth-witness';
|
|
18
|
+
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
19
|
+
import type { ContractInstanceWithAddress } from '@aztec/stdlib/contract';
|
|
20
|
+
import type { NotesFilter, UniqueNote } from '@aztec/stdlib/note';
|
|
21
|
+
import type { TxSimulationResult } from '@aztec/stdlib/tx';
|
|
22
|
+
|
|
23
|
+
import { ProvenTx } from '../utils.js';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Data for generating an account.
|
|
27
|
+
*/
|
|
28
|
+
export interface AccountData {
|
|
29
|
+
/**
|
|
30
|
+
* Secret to derive the keys for the account.
|
|
31
|
+
*/
|
|
32
|
+
secret: Fr;
|
|
33
|
+
/**
|
|
34
|
+
* Contract address salt.
|
|
35
|
+
*/
|
|
36
|
+
salt: Fr;
|
|
37
|
+
/**
|
|
38
|
+
* Contract that backs the account.
|
|
39
|
+
*/
|
|
40
|
+
contract: AccountContract;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Wallet implementation that stores accounts in memory and allows allows their creation
|
|
45
|
+
* from the outside (which is something actual wallets shouldn't allow!)
|
|
46
|
+
* It is intended to be used in e2e tests.
|
|
47
|
+
*/
|
|
48
|
+
export abstract class BaseTestWallet extends BaseWallet {
|
|
49
|
+
protected accounts: Map<string, Account> = new Map();
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Toggle for running "simulated simulations" when calling simulateTx.
|
|
53
|
+
*
|
|
54
|
+
* Terminology:
|
|
55
|
+
* - "simulation": run private circuits normally and then run the kernel in a simulated (brillig) mode on ACVM.
|
|
56
|
+
* No kernel witnesses are generated, but protocol rules are checked.
|
|
57
|
+
* - "simulated simulation": skip running kernels in ACVM altogether and emulate their behavior in TypeScript
|
|
58
|
+
* (akin to generateSimulatedProvingResult). We mutate public inputs like the kernels would and can swap in
|
|
59
|
+
* fake/private bytecode or accounts for tests. This is much faster but is not usable in situations where we
|
|
60
|
+
* need kernel witnesses.
|
|
61
|
+
*
|
|
62
|
+
* When this flag is true, simulateTx constructs a request using a fake account (and accepts contract overrides
|
|
63
|
+
* on the input) and the PXE emulates kernel effects without generating kernel witnesses. When false, simulateTx
|
|
64
|
+
* defers to the standard simulation path.
|
|
65
|
+
*/
|
|
66
|
+
private simulatedSimulations = false;
|
|
67
|
+
|
|
68
|
+
/** Enable the "simulated simulation" path for simulateTx. */
|
|
69
|
+
enableSimulatedSimulations() {
|
|
70
|
+
this.simulatedSimulations = true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Disable the "simulated simulation" path for simulateTx. */
|
|
74
|
+
disableSimulatedSimulations() {
|
|
75
|
+
this.simulatedSimulations = false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
setBaseFeePadding(value?: number) {
|
|
79
|
+
this.baseFeePadding = value ?? 0.5;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
protected async getAccountFromAddress(address: AztecAddress): Promise<Account> {
|
|
83
|
+
let account: Account | undefined;
|
|
84
|
+
if (address.equals(AztecAddress.ZERO)) {
|
|
85
|
+
const chainInfo = await this.getChainInfo();
|
|
86
|
+
account = new SignerlessAccount(chainInfo);
|
|
87
|
+
} else {
|
|
88
|
+
account = this.accounts.get(address?.toString() ?? '');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!account) {
|
|
92
|
+
throw new Error(`Account not found in wallet for address: ${address}`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return account;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
getAccounts() {
|
|
99
|
+
return Promise.resolve(Array.from(this.accounts.values()).map(acc => ({ alias: '', item: acc.getAddress() })));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Creates a new account with the provided account data or generates random values and uses SchnorrAccountContract
|
|
104
|
+
* if not provided.
|
|
105
|
+
*
|
|
106
|
+
* @param accountData - Optional account configuration containing secret, salt and account contract.
|
|
107
|
+
* @returns A new AccountManager instance for the created account
|
|
108
|
+
*/
|
|
109
|
+
async createAccount(accountData?: AccountData): Promise<AccountManager> {
|
|
110
|
+
// Generate random values if not provided
|
|
111
|
+
const secret = accountData?.secret ?? Fr.random();
|
|
112
|
+
const salt = accountData?.salt ?? Fr.random();
|
|
113
|
+
// Use SchnorrAccountContract if not provided
|
|
114
|
+
const contract = accountData?.contract ?? new SchnorrAccountContract(GrumpkinScalar.random());
|
|
115
|
+
|
|
116
|
+
const accountManager = await AccountManager.create(this, secret, contract, salt);
|
|
117
|
+
|
|
118
|
+
const instance = accountManager.getInstance();
|
|
119
|
+
const artifact = await contract.getContractArtifact();
|
|
120
|
+
|
|
121
|
+
await this.registerContract(instance, artifact, secret);
|
|
122
|
+
|
|
123
|
+
this.accounts.set(accountManager.address.toString(), await accountManager.getAccount());
|
|
124
|
+
|
|
125
|
+
return accountManager;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
abstract createSchnorrAccount(secret: Fr, salt: Fr, signingKey?: Fq): Promise<AccountManager>;
|
|
129
|
+
abstract createECDSARAccount(secret: Fr, salt: Fr, signingKey: Buffer): Promise<AccountManager>;
|
|
130
|
+
abstract createECDSAKAccount(secret: Fr, salt: Fr, signingKey: Buffer): Promise<AccountManager>;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Lookup the validity of an authwit in private and public contexts.
|
|
134
|
+
*
|
|
135
|
+
* Uses the chain id and version of the wallet.
|
|
136
|
+
*
|
|
137
|
+
* @param wallet - The wallet use to simulate and read the public data
|
|
138
|
+
* @param onBehalfOf - The address of the "approver"
|
|
139
|
+
* @param intent - The consumer and inner hash or the caller and action to lookup
|
|
140
|
+
* @param witness - The computed authentication witness to check
|
|
141
|
+
* @returns - A struct containing the validity of the authwit in private and public contexts.
|
|
142
|
+
*/
|
|
143
|
+
lookupValidity(
|
|
144
|
+
onBehalfOf: AztecAddress,
|
|
145
|
+
intent: IntentInnerHash | CallIntent | ContractFunctionInteractionCallIntent,
|
|
146
|
+
witness: AuthWitness,
|
|
147
|
+
): Promise<{
|
|
148
|
+
/** boolean flag indicating if the authwit is valid in private context */
|
|
149
|
+
isValidInPrivate: boolean;
|
|
150
|
+
/** boolean flag indicating if the authwit is valid in public context */
|
|
151
|
+
isValidInPublic: boolean;
|
|
152
|
+
}> {
|
|
153
|
+
return lookupValidity(this, onBehalfOf, intent, witness);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Returns an interaction that can be used to set the authorization status
|
|
158
|
+
* of an intent
|
|
159
|
+
* @param from - The address authorizing/revoking the action
|
|
160
|
+
* @param messageHashOrIntent - The action to authorize/revoke
|
|
161
|
+
* @param authorized - Whether the action can be performed or not
|
|
162
|
+
*/
|
|
163
|
+
public setPublicAuthWit(
|
|
164
|
+
from: AztecAddress,
|
|
165
|
+
messageHashOrIntent: Fr | Buffer | IntentInnerHash | CallIntent | ContractFunctionInteractionCallIntent,
|
|
166
|
+
authorized: boolean,
|
|
167
|
+
): Promise<SetPublicAuthwitContractInteraction> {
|
|
168
|
+
return SetPublicAuthwitContractInteraction.create(this, from, messageHashOrIntent, authorized);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Creates and returns an authwit according the the rules
|
|
173
|
+
* of the provided account. This authwit can be verified
|
|
174
|
+
* by the account contract
|
|
175
|
+
* @param from - The address authorizing the action
|
|
176
|
+
* @param messageHashOrIntent - The action to authorize
|
|
177
|
+
*/
|
|
178
|
+
public override async createAuthWit(
|
|
179
|
+
from: AztecAddress,
|
|
180
|
+
messageHashOrIntent: Fr | Buffer | IntentInnerHash | CallIntent | ContractFunctionInteractionCallIntent,
|
|
181
|
+
): Promise<AuthWitness> {
|
|
182
|
+
const account = await this.getAccountFromAddress(from);
|
|
183
|
+
const chainInfo = await this.getChainInfo();
|
|
184
|
+
const messageHash = await getMessageHashFromIntent(messageHashOrIntent, chainInfo);
|
|
185
|
+
return account.createAuthWit(messageHash);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
abstract getFakeAccountDataFor(
|
|
189
|
+
address: AztecAddress, // eslint-disable-next-line jsdoc/require-jsdoc
|
|
190
|
+
): Promise<{ account: Account; instance: ContractInstanceWithAddress; artifact: ContractArtifact }>;
|
|
191
|
+
|
|
192
|
+
override async simulateTx(executionPayload: ExecutionPayload, opts: SimulateOptions): Promise<TxSimulationResult> {
|
|
193
|
+
if (!this.simulatedSimulations) {
|
|
194
|
+
return super.simulateTx(executionPayload, opts);
|
|
195
|
+
} else {
|
|
196
|
+
const feeOptions = opts.fee?.estimateGas
|
|
197
|
+
? await this.getFeeOptionsForGasEstimation(opts.from, opts.fee)
|
|
198
|
+
: await this.getDefaultFeeOptions(opts.from, opts.fee);
|
|
199
|
+
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
200
|
+
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
201
|
+
txNonce: Fr.random(),
|
|
202
|
+
cancellable: this.cancellableTransactions,
|
|
203
|
+
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions,
|
|
204
|
+
};
|
|
205
|
+
const finalExecutionPayload = feeExecutionPayload
|
|
206
|
+
? mergeExecutionPayloads([feeExecutionPayload, executionPayload])
|
|
207
|
+
: executionPayload;
|
|
208
|
+
const { account: fromAccount, instance, artifact } = await this.getFakeAccountDataFor(opts.from);
|
|
209
|
+
const txRequest = await fromAccount.createTxExecutionRequest(
|
|
210
|
+
finalExecutionPayload,
|
|
211
|
+
feeOptions.gasSettings,
|
|
212
|
+
executionOptions,
|
|
213
|
+
);
|
|
214
|
+
const contractOverrides = {
|
|
215
|
+
[opts.from.toString()]: { instance, artifact },
|
|
216
|
+
};
|
|
217
|
+
return this.pxe.simulateTx(txRequest, true /* simulatePublic */, true, true, { contracts: contractOverrides });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* A utility to prove a transaction using this wallet and return it to be sent by a different entity on their own accord
|
|
223
|
+
*
|
|
224
|
+
* Note that this should not be used in production code since a proven transaction could be sent to a malicious
|
|
225
|
+
* node to index and track. It also makes it very difficult for the wallet to keep track of the interaction.
|
|
226
|
+
* @param exec - The execution payload to prove.
|
|
227
|
+
* @param opts - The options to configure the interaction
|
|
228
|
+
* @returns - A proven tx ready to be sent to the network
|
|
229
|
+
*/
|
|
230
|
+
async proveTx(exec: ExecutionPayload, opts: SendOptions): Promise<ProvenTx> {
|
|
231
|
+
const fee = await this.getDefaultFeeOptions(opts.from, opts.fee);
|
|
232
|
+
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(exec, opts.from, fee);
|
|
233
|
+
const txProvingResult = await this.pxe.proveTx(txRequest);
|
|
234
|
+
return new ProvenTx(
|
|
235
|
+
this.aztecNode,
|
|
236
|
+
await txProvingResult.toTx(),
|
|
237
|
+
txProvingResult.getOffchainEffects(),
|
|
238
|
+
txProvingResult.stats,
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* A debugging utility to get notes based on the provided filter.
|
|
244
|
+
*
|
|
245
|
+
* Note that this should not be used in production code because the structure of notes is considered to be
|
|
246
|
+
* an implementation detail of contracts. This is only meant to be used for debugging purposes. If you need to obtain
|
|
247
|
+
* note-related information in production code, please implement a custom utility function on your contract and call
|
|
248
|
+
* that function instead (e.g. `get_balance(owner: AztecAddress) -> u128` utility function on a Token contract).
|
|
249
|
+
*
|
|
250
|
+
* @param filter - The filter to apply to the notes.
|
|
251
|
+
* @returns The requested notes.
|
|
252
|
+
*/
|
|
253
|
+
getNotes(filter: NotesFilter): Promise<UniqueNote[]> {
|
|
254
|
+
return this.pxe.getNotes(filter);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Stops the internal job queue.
|
|
259
|
+
*
|
|
260
|
+
* This function is typically used when tearing down tests.
|
|
261
|
+
*/
|
|
262
|
+
stop(): Promise<void> {
|
|
263
|
+
return this.pxe.stop();
|
|
264
|
+
}
|
|
265
|
+
}
|