@aztec/aztec.js 0.23.0 → 0.24.0

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 (117) hide show
  1. package/dest/account/index.d.ts +1 -1
  2. package/dest/account/index.d.ts.map +1 -1
  3. package/dest/account/interface.d.ts +12 -2
  4. package/dest/account/interface.d.ts.map +1 -1
  5. package/dest/account/interface.js +1 -1
  6. package/dest/account_manager/index.js +2 -2
  7. package/dest/api/account.d.ts +1 -1
  8. package/dest/api/account.d.ts.map +1 -1
  9. package/dest/api/account.js +1 -1
  10. package/dest/api/deployment.d.ts +4 -0
  11. package/dest/api/deployment.d.ts.map +1 -0
  12. package/dest/api/deployment.js +4 -0
  13. package/dest/api/fee.d.ts +4 -0
  14. package/dest/api/fee.d.ts.map +1 -0
  15. package/dest/api/fee.js +3 -0
  16. package/dest/contract/unsafe_contract.d.ts +15 -0
  17. package/dest/contract/unsafe_contract.d.ts.map +1 -0
  18. package/dest/contract/unsafe_contract.js +14 -0
  19. package/dest/deployment/broadcast_function.d.ts +24 -0
  20. package/dest/deployment/broadcast_function.d.ts.map +1 -0
  21. package/dest/deployment/broadcast_function.js +67 -0
  22. package/dest/deployment/contract_deployer.d.ts.map +1 -0
  23. package/dest/{contract_deployer → deployment}/contract_deployer.js +1 -1
  24. package/dest/deployment/deploy_instance.d.ts +13 -0
  25. package/dest/deployment/deploy_instance.d.ts.map +1 -0
  26. package/dest/deployment/deploy_instance.js +13 -0
  27. package/dest/deployment/index.d.ts.map +1 -0
  28. package/dest/{contract_deployer → deployment}/index.js +1 -1
  29. package/dest/deployment/protocol_contracts.d.ts +7 -0
  30. package/dest/deployment/protocol_contracts.d.ts.map +1 -0
  31. package/dest/deployment/protocol_contracts.js +14 -0
  32. package/dest/deployment/register_class.d.ts +6 -0
  33. package/dest/deployment/register_class.d.ts.map +1 -0
  34. package/dest/deployment/register_class.js +11 -0
  35. package/dest/fee/fee_payment_method.d.ts +28 -0
  36. package/dest/fee/fee_payment_method.d.ts.map +1 -0
  37. package/dest/fee/fee_payment_method.js +2 -0
  38. package/dest/fee/generic_fee_payment_method.d.ts +58 -0
  39. package/dest/fee/generic_fee_payment_method.d.ts.map +1 -0
  40. package/dest/fee/generic_fee_payment_method.js +73 -0
  41. package/dest/fee/native_fee_payment_method.d.ts +32 -0
  42. package/dest/fee/native_fee_payment_method.d.ts.map +1 -0
  43. package/dest/fee/native_fee_payment_method.js +55 -0
  44. package/dest/index.d.ts +2 -1
  45. package/dest/index.d.ts.map +1 -1
  46. package/dest/index.js +3 -2
  47. package/dest/utils/cheat_codes.js +2 -2
  48. package/dest/wallet/account_wallet.d.ts +2 -2
  49. package/dest/wallet/account_wallet.d.ts.map +1 -1
  50. package/dest/wallet/account_wallet.js +3 -3
  51. package/dest/wallet/base_wallet.d.ts +2 -1
  52. package/dest/wallet/base_wallet.d.ts.map +1 -1
  53. package/dest/wallet/base_wallet.js +1 -1
  54. package/package.json +9 -6
  55. package/src/account/contract.ts +33 -0
  56. package/src/account/index.ts +16 -0
  57. package/src/account/interface.ts +47 -0
  58. package/src/account/wallet.ts +8 -0
  59. package/src/account_manager/deploy_account_sent_tx.ts +42 -0
  60. package/src/account_manager/index.ts +173 -0
  61. package/src/api/README.md +7 -0
  62. package/src/api/abi.ts +3 -0
  63. package/src/api/account.ts +13 -0
  64. package/src/api/aztec_address.ts +1 -0
  65. package/src/api/deployment.ts +3 -0
  66. package/src/api/eth_address.ts +1 -0
  67. package/src/api/ethereum.ts +6 -0
  68. package/src/api/fee.ts +3 -0
  69. package/src/api/fields.ts +1 -0
  70. package/src/api/init.ts +10 -0
  71. package/src/api/interfaces/pxe.ts +1 -0
  72. package/src/api/log_id.ts +1 -0
  73. package/src/api/tx_hash.ts +1 -0
  74. package/src/api/wallet.ts +1 -0
  75. package/src/contract/base_contract_interaction.ts +61 -0
  76. package/src/contract/batch_call.ts +23 -0
  77. package/src/contract/checker.ts +117 -0
  78. package/src/contract/contract.ts +56 -0
  79. package/src/contract/contract_base.ts +72 -0
  80. package/src/contract/contract_function_interaction.ts +80 -0
  81. package/src/contract/deploy_method.ts +151 -0
  82. package/src/contract/deploy_sent_tx.ts +69 -0
  83. package/src/contract/index.ts +44 -0
  84. package/src/contract/sent_tx.ts +135 -0
  85. package/src/contract/unsafe_contract.ts +19 -0
  86. package/src/deployment/broadcast_function.ts +118 -0
  87. package/src/deployment/contract_deployer.ts +30 -0
  88. package/src/deployment/deploy_instance.ts +28 -0
  89. package/src/deployment/index.ts +1 -0
  90. package/src/deployment/protocol_contracts.ts +17 -0
  91. package/src/deployment/register_class.ts +15 -0
  92. package/src/fee/fee_payment_method.ts +30 -0
  93. package/src/fee/generic_fee_payment_method.ts +81 -0
  94. package/src/fee/native_fee_payment_method.ts +61 -0
  95. package/src/index.ts +144 -0
  96. package/src/rpc_clients/index.ts +1 -0
  97. package/src/rpc_clients/pxe_client.ts +62 -0
  98. package/src/utils/abi_types.ts +16 -0
  99. package/src/utils/account.ts +32 -0
  100. package/src/utils/authwit.ts +24 -0
  101. package/src/utils/cheat_codes.ts +297 -0
  102. package/src/utils/index.ts +9 -0
  103. package/src/utils/l1_contracts.ts +21 -0
  104. package/src/utils/l2_contracts.ts +12 -0
  105. package/src/utils/pub_key.ts +12 -0
  106. package/src/utils/pxe.ts +16 -0
  107. package/src/utils/secrets.ts +11 -0
  108. package/src/wallet/account_wallet.ts +70 -0
  109. package/src/wallet/account_wallet_with_private_key.ts +28 -0
  110. package/src/wallet/base_wallet.ts +123 -0
  111. package/src/wallet/create_recipient.ts +13 -0
  112. package/src/wallet/index.ts +31 -0
  113. package/src/wallet/signerless_wallet.ts +37 -0
  114. package/dest/contract_deployer/contract_deployer.d.ts.map +0 -1
  115. package/dest/contract_deployer/index.d.ts.map +0 -1
  116. /package/dest/{contract_deployer → deployment}/contract_deployer.d.ts +0 -0
  117. /package/dest/{contract_deployer → deployment}/index.d.ts +0 -0
@@ -0,0 +1,297 @@
1
+ import { Note, PXE } from '@aztec/circuit-types';
2
+ import { AztecAddress, EthAddress, Fr } from '@aztec/circuits.js';
3
+ import { toBigIntBE, toHex } from '@aztec/foundation/bigint-buffer';
4
+ import { keccak, pedersenHash } from '@aztec/foundation/crypto';
5
+ import { createDebugLogger } from '@aztec/foundation/log';
6
+
7
+ import fs from 'fs';
8
+
9
+ /**
10
+ * A class that provides utility functions for interacting with the chain.
11
+ */
12
+ export class CheatCodes {
13
+ constructor(
14
+ /**
15
+ * The cheat codes for ethereum (L1).
16
+ */
17
+ public eth: EthCheatCodes,
18
+ /**
19
+ * The cheat codes for aztec.
20
+ */
21
+ public aztec: AztecCheatCodes,
22
+ ) {}
23
+
24
+ static create(rpcUrl: string, pxe: PXE): CheatCodes {
25
+ const ethCheatCodes = new EthCheatCodes(rpcUrl);
26
+ const aztecCheatCodes = new AztecCheatCodes(pxe, ethCheatCodes);
27
+ return new CheatCodes(ethCheatCodes, aztecCheatCodes);
28
+ }
29
+ }
30
+
31
+ /**
32
+ * A class that provides utility functions for interacting with ethereum (L1).
33
+ */
34
+ export class EthCheatCodes {
35
+ constructor(
36
+ /**
37
+ * The RPC URL to use for interacting with the chain
38
+ */
39
+ public rpcUrl: string,
40
+ /**
41
+ * The logger to use for the eth cheatcodes
42
+ */
43
+ public logger = createDebugLogger('aztec:cheat_codes:eth'),
44
+ ) {}
45
+
46
+ async rpcCall(method: string, params: any[]) {
47
+ const paramsString = JSON.stringify(params);
48
+ const content = {
49
+ body: `{"jsonrpc":"2.0", "method": "${method}", "params": ${paramsString}, "id": 1}`,
50
+ method: 'POST',
51
+ headers: { 'Content-Type': 'application/json' },
52
+ };
53
+ return await (await fetch(this.rpcUrl, content)).json();
54
+ }
55
+
56
+ /**
57
+ * Get the current blocknumber
58
+ * @returns The current block number
59
+ */
60
+ public async blockNumber(): Promise<number> {
61
+ const res = await this.rpcCall('eth_blockNumber', []);
62
+ return parseInt(res.result, 16);
63
+ }
64
+
65
+ /**
66
+ * Get the current chainId
67
+ * @returns The current chainId
68
+ */
69
+ public async chainId(): Promise<number> {
70
+ const res = await this.rpcCall('eth_chainId', []);
71
+ return parseInt(res.result, 16);
72
+ }
73
+
74
+ /**
75
+ * Get the current timestamp
76
+ * @returns The current timestamp
77
+ */
78
+ public async timestamp(): Promise<number> {
79
+ const res = await this.rpcCall('eth_getBlockByNumber', ['latest', true]);
80
+ return parseInt(res.result.timestamp, 16);
81
+ }
82
+
83
+ /**
84
+ * Advance the chain by a number of blocks
85
+ * @param numberOfBlocks - The number of blocks to mine
86
+ * @returns The current chainId
87
+ */
88
+ public async mine(numberOfBlocks = 1): Promise<void> {
89
+ const res = await this.rpcCall('hardhat_mine', [numberOfBlocks]);
90
+ if (res.error) {
91
+ throw new Error(`Error mining: ${res.error.message}`);
92
+ }
93
+ this.logger(`Mined ${numberOfBlocks} blocks`);
94
+ }
95
+
96
+ /**
97
+ * Set the next block timestamp
98
+ * @param timestamp - The timestamp to set the next block to
99
+ */
100
+ public async setNextBlockTimestamp(timestamp: number): Promise<void> {
101
+ const res = await this.rpcCall('evm_setNextBlockTimestamp', [timestamp]);
102
+ if (res.error) {
103
+ throw new Error(`Error setting next block timestamp: ${res.error.message}`);
104
+ }
105
+ this.logger(`Set next block timestamp to ${timestamp}`);
106
+ }
107
+
108
+ /**
109
+ * Dumps the current chain state to a file.
110
+ * @param fileName - The file name to dump state into
111
+ */
112
+ public async dumpChainState(fileName: string): Promise<void> {
113
+ const res = await this.rpcCall('hardhat_dumpState', []);
114
+ if (res.error) {
115
+ throw new Error(`Error dumping state: ${res.error.message}`);
116
+ }
117
+ const jsonContent = JSON.stringify(res.result);
118
+ fs.writeFileSync(`${fileName}.json`, jsonContent, 'utf8');
119
+ this.logger(`Dumped state to ${fileName}`);
120
+ }
121
+
122
+ /**
123
+ * Loads the chain state from a file.
124
+ * @param fileName - The file name to load state from
125
+ */
126
+ public async loadChainState(fileName: string): Promise<void> {
127
+ const data = JSON.parse(fs.readFileSync(`${fileName}.json`, 'utf8'));
128
+ const res = await this.rpcCall('hardhat_loadState', [data]);
129
+ if (res.error) {
130
+ throw new Error(`Error loading state: ${res.error.message}`);
131
+ }
132
+ this.logger(`Loaded state from ${fileName}`);
133
+ }
134
+
135
+ /**
136
+ * Load the value at a storage slot of a contract address on eth
137
+ * @param contract - The contract address
138
+ * @param slot - The storage slot
139
+ * @returns - The value at the storage slot
140
+ */
141
+ public async load(contract: EthAddress, slot: bigint): Promise<bigint> {
142
+ const res = await this.rpcCall('eth_getStorageAt', [contract.toString(), toHex(slot), 'latest']);
143
+ return BigInt(res.result);
144
+ }
145
+
146
+ /**
147
+ * Set the value at a storage slot of a contract address on eth
148
+ * @param contract - The contract address
149
+ * @param slot - The storage slot
150
+ * @param value - The value to set the storage slot to
151
+ */
152
+ public async store(contract: EthAddress, slot: bigint, value: bigint): Promise<void> {
153
+ // for the rpc call, we need to change value to be a 32 byte hex string.
154
+ const res = await this.rpcCall('hardhat_setStorageAt', [contract.toString(), toHex(slot), toHex(value, true)]);
155
+ if (res.error) {
156
+ throw new Error(`Error setting storage for contract ${contract} at ${slot}: ${res.error.message}`);
157
+ }
158
+ this.logger(`Set storage for contract ${contract} at ${slot} to ${value}`);
159
+ }
160
+
161
+ /**
162
+ * Computes the slot value for a given map and key.
163
+ * @param baseSlot - The base slot of the map (specified in Aztec.nr contract)
164
+ * @param key - The key to lookup in the map
165
+ * @returns The storage slot of the value in the map
166
+ */
167
+ public keccak256(baseSlot: bigint, key: bigint): bigint {
168
+ // abi encode (removing the 0x) - concat key and baseSlot (both padded to 32 bytes)
169
+ const abiEncoded = toHex(key, true).substring(2) + toHex(baseSlot, true).substring(2);
170
+ return toBigIntBE(keccak(Buffer.from(abiEncoded, 'hex')));
171
+ }
172
+
173
+ /**
174
+ * Send transactions impersonating an externally owned account or contract.
175
+ * @param who - The address to impersonate
176
+ */
177
+ public async startImpersonating(who: EthAddress): Promise<void> {
178
+ const res = await this.rpcCall('hardhat_impersonateAccount', [who.toString()]);
179
+ if (res.error) {
180
+ throw new Error(`Error impersonating ${who}: ${res.error.message}`);
181
+ }
182
+ this.logger(`Impersonating ${who}`);
183
+ }
184
+
185
+ /**
186
+ * Stop impersonating an account that you are currently impersonating.
187
+ * @param who - The address to stop impersonating
188
+ */
189
+ public async stopImpersonating(who: EthAddress): Promise<void> {
190
+ const res = await this.rpcCall('hardhat_stopImpersonatingAccount', [who.toString()]);
191
+ if (res.error) {
192
+ throw new Error(`Error when stopping the impersonation of ${who}: ${res.error.message}`);
193
+ }
194
+ this.logger(`Stopped impersonating ${who}`);
195
+ }
196
+
197
+ /**
198
+ * Set the bytecode for a contract
199
+ * @param contract - The contract address
200
+ * @param bytecode - The bytecode to set
201
+ */
202
+ public async etch(contract: EthAddress, bytecode: `0x${string}`): Promise<void> {
203
+ const res = await this.rpcCall('hardhat_setCode', [contract.toString(), bytecode]);
204
+ if (res.error) {
205
+ throw new Error(`Error setting bytecode for ${contract}: ${res.error.message}`);
206
+ }
207
+ this.logger(`Set bytecode for ${contract} to ${bytecode}`);
208
+ }
209
+
210
+ /**
211
+ * Get the bytecode for a contract
212
+ * @param contract - The contract address
213
+ * @returns The bytecode for the contract
214
+ */
215
+ public async getBytecode(contract: EthAddress): Promise<`0x${string}`> {
216
+ const res = await this.rpcCall('eth_getCode', [contract.toString(), 'latest']);
217
+ return res.result;
218
+ }
219
+ }
220
+
221
+ /**
222
+ * A class that provides utility functions for interacting with the aztec chain.
223
+ */
224
+ export class AztecCheatCodes {
225
+ constructor(
226
+ /**
227
+ * The PXE Service to use for interacting with the chain
228
+ */
229
+ public pxe: PXE,
230
+ /**
231
+ * The eth cheat codes.
232
+ */
233
+ public eth: EthCheatCodes,
234
+ /**
235
+ * The logger to use for the aztec cheatcodes
236
+ */
237
+ public logger = createDebugLogger('aztec:cheat_codes:aztec'),
238
+ ) {}
239
+
240
+ /**
241
+ * Computes the slot value for a given map and key.
242
+ * @param baseSlot - The base slot of the map (specified in Aztec.nr contract)
243
+ * @param key - The key to lookup in the map
244
+ * @returns The storage slot of the value in the map
245
+ */
246
+ public computeSlotInMap(baseSlot: Fr | bigint, key: Fr | bigint | AztecAddress): Fr {
247
+ // Based on `at` function in
248
+ // aztec3-packages/aztec-nr/aztec/src/state_vars/map.nr
249
+ return Fr.fromBuffer(pedersenHash([new Fr(baseSlot), new Fr(key)].map(f => f.toBuffer())));
250
+ }
251
+
252
+ /**
253
+ * Get the current blocknumber
254
+ * @returns The current block number
255
+ */
256
+ public async blockNumber(): Promise<number> {
257
+ return await this.pxe.getBlockNumber();
258
+ }
259
+
260
+ /**
261
+ * Set time of the next execution on aztec.
262
+ * It also modifies time on eth for next execution and stores this time as the last rollup block on the rollup contract.
263
+ * @param to - The timestamp to set the next block to (must be greater than current time)
264
+ */
265
+ public async warp(to: number): Promise<void> {
266
+ const rollupContract = (await this.pxe.getNodeInfo()).l1ContractAddresses.rollupAddress;
267
+ await this.eth.setNextBlockTimestamp(to);
268
+ // also store this time on the rollup contract (slot 1 tracks `lastBlockTs`).
269
+ // This is because when the sequencer executes public functions, it uses the timestamp stored in the rollup contract.
270
+ await this.eth.store(rollupContract, 1n, BigInt(to));
271
+ // also store this on slot 2 of the rollup contract (`lastWarpedBlockTs`) which tracks the last time warp was used.
272
+ await this.eth.store(rollupContract, 2n, BigInt(to));
273
+ }
274
+
275
+ /**
276
+ * Loads the value stored at the given slot in the public storage of the given contract.
277
+ * @param who - The address of the contract
278
+ * @param slot - The storage slot to lookup
279
+ * @returns The value stored at the given slot
280
+ */
281
+ public async loadPublic(who: AztecAddress, slot: Fr | bigint): Promise<Fr> {
282
+ const storageValue = await this.pxe.getPublicStorageAt(who, new Fr(slot));
283
+ return storageValue;
284
+ }
285
+
286
+ /**
287
+ * Loads the value stored at the given slot in the private storage of the given contract.
288
+ * @param contract - The address of the contract
289
+ * @param owner - The owner for whom the notes are encrypted
290
+ * @param slot - The storage slot to lookup
291
+ * @returns The notes stored at the given slot
292
+ */
293
+ public async loadPrivate(owner: AztecAddress, contract: AztecAddress, slot: Fr | bigint): Promise<Note[]> {
294
+ const extendedNotes = await this.pxe.getNotes({ owner, contractAddress: contract, storageSlot: new Fr(slot) });
295
+ return extendedNotes.map(extendedNote => extendedNote.note);
296
+ }
297
+ }
@@ -0,0 +1,9 @@
1
+ export * from './secrets.js';
2
+ export * from './pub_key.js';
3
+ export * from './l1_contracts.js';
4
+ export * from './l2_contracts.js';
5
+ export * from './abi_types.js';
6
+ export * from './cheat_codes.js';
7
+ export * from './authwit.js';
8
+ export * from './pxe.js';
9
+ export * from './account.js';
@@ -0,0 +1,21 @@
1
+ import { L1ContractAddresses } from '@aztec/ethereum';
2
+ import { retryUntil } from '@aztec/foundation/retry';
3
+
4
+ import { createPXEClient } from '../rpc_clients/index.js';
5
+
6
+ export const getL1ContractAddresses = async (url: string): Promise<L1ContractAddresses> => {
7
+ const pxeClient = createPXEClient(url);
8
+ const response = await retryUntil(
9
+ async () => {
10
+ try {
11
+ return (await pxeClient.getNodeInfo()).l1ContractAddresses;
12
+ } catch (err) {
13
+ // do nothing
14
+ }
15
+ },
16
+ 'isNodeReady',
17
+ 120,
18
+ 1,
19
+ );
20
+ return response;
21
+ };
@@ -0,0 +1,12 @@
1
+ import { PXE } from '@aztec/circuit-types';
2
+ import { AztecAddress } from '@aztec/foundation/aztec-address';
3
+
4
+ /**
5
+ * Checks whether a give contract is deployed on the network.
6
+ * @param pxe - The PXE to use to obtain the information.
7
+ * @param contractAddress - The address of the contract to check.
8
+ * @returns A flag indicating whether the contract is deployed.
9
+ */
10
+ export async function isContractDeployed(pxe: PXE, contractAddress: AztecAddress): Promise<boolean> {
11
+ return !!(await pxe.getContractData(contractAddress));
12
+ }
@@ -0,0 +1,12 @@
1
+ import { GrumpkinPrivateKey, PublicKey } from '@aztec/circuits.js';
2
+ import { Grumpkin } from '@aztec/circuits.js/barretenberg';
3
+
4
+ /**
5
+ * Method for generating a public grumpkin key from a private key.
6
+ * @param privateKey - The private key.
7
+ * @returns The generated public key.
8
+ */
9
+ export function generatePublicKey(privateKey: GrumpkinPrivateKey): PublicKey {
10
+ const grumpkin = new Grumpkin();
11
+ return grumpkin.mul(grumpkin.generator(), privateKey);
12
+ }
@@ -0,0 +1,16 @@
1
+ import { PXE } from '@aztec/circuit-types';
2
+ import { DebugLogger } from '@aztec/foundation/log';
3
+ import { retryUntil } from '@aztec/foundation/retry';
4
+
5
+ export const waitForPXE = async (pxe: PXE, logger?: DebugLogger) => {
6
+ await retryUntil(async () => {
7
+ try {
8
+ logger?.('Attempting to contact PXE...');
9
+ await pxe.getNodeInfo();
10
+ return true;
11
+ } catch (error) {
12
+ logger?.('Failed to contact PXE!');
13
+ }
14
+ return undefined;
15
+ }, 'RPC Get Node Info');
16
+ };
@@ -0,0 +1,11 @@
1
+ import { Fr } from '@aztec/circuits.js';
2
+ import { computeSecretMessageHash } from '@aztec/circuits.js/abis';
3
+
4
+ /**
5
+ * Given a secret, it computes its pedersen hash - used to send l1 to l2 messages
6
+ * @param secret - the secret to hash - secret could be generated however you want e.g. `Fr.random()`
7
+ * @returns the hash
8
+ */
9
+ export function computeMessageSecretHash(secret: Fr) {
10
+ return computeSecretMessageHash(secret);
11
+ }
@@ -0,0 +1,70 @@
1
+ import { AuthWitness, FunctionCall, PXE, TxExecutionRequest } from '@aztec/circuit-types';
2
+ import { Fr } from '@aztec/circuits.js';
3
+ import { ABIParameterVisibility, FunctionAbi, FunctionType } from '@aztec/foundation/abi';
4
+
5
+ import { AccountInterface, FeeOptions } from '../account/interface.js';
6
+ import { ContractFunctionInteraction } from '../contract/contract_function_interaction.js';
7
+ import { BaseWallet } from './base_wallet.js';
8
+
9
+ /**
10
+ * A wallet implementation that forwards authentication requests to a provided account.
11
+ */
12
+ export class AccountWallet extends BaseWallet {
13
+ constructor(pxe: PXE, protected account: AccountInterface) {
14
+ super(pxe);
15
+ }
16
+
17
+ createTxExecutionRequest(execs: FunctionCall[], fee?: FeeOptions): Promise<TxExecutionRequest> {
18
+ return this.account.createTxExecutionRequest(execs, fee);
19
+ }
20
+
21
+ async createAuthWitness(message: Fr | Buffer): Promise<AuthWitness> {
22
+ message = Buffer.isBuffer(message) ? Fr.fromBuffer(message) : message;
23
+ const witness = await this.account.createAuthWitness(message);
24
+ await this.pxe.addAuthWitness(witness);
25
+ return witness;
26
+ }
27
+
28
+ /**
29
+ * Returns a function interaction to set a message hash as authorized in this account.
30
+ * Public calls can then consume this authorization.
31
+ * @param message - Message hash to authorize.
32
+ * @param authorized - True to authorize, false to revoke authorization.
33
+ * @returns - A function interaction.
34
+ */
35
+ public setPublicAuth(message: Fr | Buffer, authorized: boolean): ContractFunctionInteraction {
36
+ const args = [message, authorized];
37
+ return new ContractFunctionInteraction(this, this.getAddress(), this.getSetIsValidStorageAbi(), args);
38
+ }
39
+
40
+ /** Returns the complete address of the account that implements this wallet. */
41
+ public getCompleteAddress() {
42
+ return this.account.getCompleteAddress();
43
+ }
44
+
45
+ /** Returns the address of the account that implements this wallet. */
46
+ public getAddress() {
47
+ return this.getCompleteAddress().address;
48
+ }
49
+
50
+ private getSetIsValidStorageAbi(): FunctionAbi {
51
+ return {
52
+ name: 'set_is_valid_storage',
53
+ functionType: 'open' as FunctionType,
54
+ isInternal: true,
55
+ parameters: [
56
+ {
57
+ name: 'message_hash',
58
+ type: { kind: 'field' },
59
+ visibility: 'private' as ABIParameterVisibility,
60
+ },
61
+ {
62
+ name: 'value',
63
+ type: { kind: 'boolean' },
64
+ visibility: 'private' as ABIParameterVisibility,
65
+ },
66
+ ],
67
+ returnTypes: [],
68
+ };
69
+ }
70
+ }
@@ -0,0 +1,28 @@
1
+ import { PXE } from '@aztec/circuit-types';
2
+ import { GrumpkinPrivateKey } from '@aztec/circuits.js';
3
+
4
+ import { Salt } from '../account/index.js';
5
+ import { AccountInterface } from '../account/interface.js';
6
+ import { AccountWallet } from './account_wallet.js';
7
+
8
+ /**
9
+ * Extends {@link AccountWallet} with the encryption private key. Not required for
10
+ * implementing the wallet interface but useful for testing purposes or exporting
11
+ * an account to another pxe.
12
+ */
13
+ export class AccountWalletWithPrivateKey extends AccountWallet {
14
+ constructor(
15
+ pxe: PXE,
16
+ account: AccountInterface,
17
+ private encryptionPrivateKey: GrumpkinPrivateKey,
18
+ /** Deployment salt for this account contract. */
19
+ public readonly salt: Salt,
20
+ ) {
21
+ super(pxe, account);
22
+ }
23
+
24
+ /** Returns the encryption private key associated with this account. */
25
+ public getEncryptionPrivateKey() {
26
+ return this.encryptionPrivateKey;
27
+ }
28
+ }
@@ -0,0 +1,123 @@
1
+ import {
2
+ AuthWitness,
3
+ ContractData,
4
+ DeployedContract,
5
+ ExtendedContractData,
6
+ ExtendedNote,
7
+ FunctionCall,
8
+ GetUnencryptedLogsResponse,
9
+ L2Block,
10
+ L2Tx,
11
+ LogFilter,
12
+ NoteFilter,
13
+ PXE,
14
+ SyncStatus,
15
+ Tx,
16
+ TxExecutionRequest,
17
+ TxHash,
18
+ TxReceipt,
19
+ } from '@aztec/circuit-types';
20
+ import { AztecAddress, CompleteAddress, Fr, GrumpkinPrivateKey, PartialAddress } from '@aztec/circuits.js';
21
+ import { ContractInstanceWithAddress } from '@aztec/types/contracts';
22
+ import { NodeInfo } from '@aztec/types/interfaces';
23
+
24
+ import { FeeOptions } from '../account/interface.js';
25
+ import { Wallet } from '../account/wallet.js';
26
+
27
+ /**
28
+ * A base class for Wallet implementations
29
+ */
30
+ export abstract class BaseWallet implements Wallet {
31
+ constructor(protected readonly pxe: PXE) {}
32
+
33
+ abstract getCompleteAddress(): CompleteAddress;
34
+
35
+ abstract createTxExecutionRequest(execs: FunctionCall[], fee?: FeeOptions): Promise<TxExecutionRequest>;
36
+
37
+ abstract createAuthWitness(message: Fr): Promise<AuthWitness>;
38
+
39
+ getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
40
+ return this.pxe.getContractInstance(address);
41
+ }
42
+ addCapsule(capsule: Fr[]): Promise<void> {
43
+ return this.pxe.addCapsule(capsule);
44
+ }
45
+ registerAccount(privKey: GrumpkinPrivateKey, partialAddress: PartialAddress): Promise<CompleteAddress> {
46
+ return this.pxe.registerAccount(privKey, partialAddress);
47
+ }
48
+ registerRecipient(account: CompleteAddress): Promise<void> {
49
+ return this.pxe.registerRecipient(account);
50
+ }
51
+ getRegisteredAccounts(): Promise<CompleteAddress[]> {
52
+ return this.pxe.getRegisteredAccounts();
53
+ }
54
+ getRegisteredAccount(address: AztecAddress): Promise<CompleteAddress | undefined> {
55
+ return this.pxe.getRegisteredAccount(address);
56
+ }
57
+ getRecipients(): Promise<CompleteAddress[]> {
58
+ return this.pxe.getRecipients();
59
+ }
60
+ getRecipient(address: AztecAddress): Promise<CompleteAddress | undefined> {
61
+ return this.pxe.getRecipient(address);
62
+ }
63
+ addContracts(contracts: DeployedContract[]): Promise<void> {
64
+ return this.pxe.addContracts(contracts);
65
+ }
66
+ getContracts(): Promise<AztecAddress[]> {
67
+ return this.pxe.getContracts();
68
+ }
69
+ simulateTx(txRequest: TxExecutionRequest, simulatePublic: boolean): Promise<Tx> {
70
+ return this.pxe.simulateTx(txRequest, simulatePublic);
71
+ }
72
+ sendTx(tx: Tx): Promise<TxHash> {
73
+ return this.pxe.sendTx(tx);
74
+ }
75
+ getTx(txHash: TxHash): Promise<L2Tx | undefined> {
76
+ return this.pxe.getTx(txHash);
77
+ }
78
+ getTxReceipt(txHash: TxHash): Promise<TxReceipt> {
79
+ return this.pxe.getTxReceipt(txHash);
80
+ }
81
+ getNotes(filter: NoteFilter): Promise<ExtendedNote[]> {
82
+ return this.pxe.getNotes(filter);
83
+ }
84
+ getPublicStorageAt(contract: AztecAddress, storageSlot: Fr): Promise<any> {
85
+ return this.pxe.getPublicStorageAt(contract, storageSlot);
86
+ }
87
+ addNote(note: ExtendedNote): Promise<void> {
88
+ return this.pxe.addNote(note);
89
+ }
90
+ getBlock(number: number): Promise<L2Block | undefined> {
91
+ return this.pxe.getBlock(number);
92
+ }
93
+ viewTx(functionName: string, args: any[], to: AztecAddress, from?: AztecAddress | undefined): Promise<any> {
94
+ return this.pxe.viewTx(functionName, args, to, from);
95
+ }
96
+ getExtendedContractData(contractAddress: AztecAddress): Promise<ExtendedContractData | undefined> {
97
+ return this.pxe.getExtendedContractData(contractAddress);
98
+ }
99
+ getContractData(contractAddress: AztecAddress): Promise<ContractData | undefined> {
100
+ return this.pxe.getContractData(contractAddress);
101
+ }
102
+ getUnencryptedLogs(filter: LogFilter): Promise<GetUnencryptedLogsResponse> {
103
+ return this.pxe.getUnencryptedLogs(filter);
104
+ }
105
+ getBlockNumber(): Promise<number> {
106
+ return this.pxe.getBlockNumber();
107
+ }
108
+ getNodeInfo(): Promise<NodeInfo> {
109
+ return this.pxe.getNodeInfo();
110
+ }
111
+ isGlobalStateSynchronized() {
112
+ return this.pxe.isGlobalStateSynchronized();
113
+ }
114
+ isAccountStateSynchronized(account: AztecAddress) {
115
+ return this.pxe.isAccountStateSynchronized(account);
116
+ }
117
+ getSyncStatus(): Promise<SyncStatus> {
118
+ return this.pxe.getSyncStatus();
119
+ }
120
+ addAuthWitness(authWitness: AuthWitness) {
121
+ return this.pxe.addAuthWitness(authWitness);
122
+ }
123
+ }
@@ -0,0 +1,13 @@
1
+ import { PXE } from '@aztec/circuit-types';
2
+ import { CompleteAddress } from '@aztec/circuits.js';
3
+
4
+ /**
5
+ * Creates a random address and registers it as a recipient on the pxe server. Useful for testing.
6
+ * @param pxe - PXE.
7
+ * @returns Complete address of the registered recipient.
8
+ */
9
+ export async function createRecipient(pxe: PXE): Promise<CompleteAddress> {
10
+ const completeAddress = CompleteAddress.random();
11
+ await pxe.registerRecipient(completeAddress);
12
+ return completeAddress;
13
+ }
@@ -0,0 +1,31 @@
1
+ import { PXE } from '@aztec/circuit-types';
2
+ import { AztecAddress } from '@aztec/foundation/aztec-address';
3
+
4
+ import { AccountContract } from '../account/contract.js';
5
+ import { AccountWallet } from './account_wallet.js';
6
+
7
+ export * from '../account/wallet.js';
8
+ export * from './account_wallet.js';
9
+ export * from './account_wallet_with_private_key.js';
10
+ export * from './signerless_wallet.js';
11
+
12
+ /**
13
+ * Gets a wallet for an already registered account.
14
+ * @param pxe - PXE Service instance.
15
+ * @param address - Address for the account.
16
+ * @param accountContract - Account contract implementation.
17
+ * @returns A wallet for this account that can be used to interact with a contract instance.
18
+ */
19
+ export async function getWallet(
20
+ pxe: PXE,
21
+ address: AztecAddress,
22
+ accountContract: AccountContract,
23
+ ): Promise<AccountWallet> {
24
+ const completeAddress = await pxe.getRegisteredAccount(address);
25
+ if (!completeAddress) {
26
+ throw new Error(`Account ${address} not found`);
27
+ }
28
+ const nodeInfo = await pxe.getNodeInfo();
29
+ const entrypoint = accountContract.getInterface(completeAddress, nodeInfo);
30
+ return new AccountWallet(pxe, entrypoint);
31
+ }