@aztec/cli 0.1.0-alpha20 → 0.1.0-alpha22

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/src/index.ts CHANGED
@@ -1,24 +1,29 @@
1
1
  #!/usr/bin/env -S node --no-warnings
2
2
  import { Command } from 'commander';
3
+ import { mnemonicToAccount } from 'viem/accounts';
3
4
  import { createLogger } from '@aztec/foundation/log';
4
5
  import { createDebugLogger } from '@aztec/foundation/log';
5
6
  import {
6
7
  AztecAddress,
7
8
  Contract,
8
9
  ContractDeployer,
10
+ Fr,
9
11
  Point,
10
12
  TxHash,
11
13
  createAccounts,
12
14
  createAztecRpcClient,
13
- pointToPublicKey,
15
+ getAccountWallet,
14
16
  } from '@aztec/aztec.js';
15
-
16
- import { encodeArgs, parseStructString } from './cli_encoder.js';
17
- import { deployAztecContracts, getContractAbi, prepTx } from './utils.js';
18
17
  import { JsonStringify } from '@aztec/foundation/json-rpc';
19
18
  import { StructType } from '@aztec/foundation/abi';
19
+ import { randomBytes } from '@aztec/foundation/crypto';
20
20
  import { ContractData, L2BlockL2Logs } from '@aztec/types';
21
21
 
22
+ import { encodeArgs, parseStructString } from './cli_encoder.js';
23
+ import { deployAztecContracts, getContractAbi, getTxSender, prepTx } from './utils.js';
24
+
25
+ const accountCreationSalt = Fr.ZERO;
26
+
22
27
  const debugLogger = createDebugLogger('aztec:cli');
23
28
  const log = createLogger();
24
29
 
@@ -26,65 +31,127 @@ const program = new Command();
26
31
 
27
32
  program.name('azti').description('CLI for interacting with Aztec.').version('0.1.0');
28
33
 
34
+ const { ETHEREUM_HOST, AZTEC_RPC_HOST, PRIVATE_KEY, PUBLIC_KEY, API_KEY } = process.env;
35
+
29
36
  /**
30
- * A placeholder for the Aztec-cli.
37
+ * Main function for the Aztec CLI.
31
38
  */
32
39
  async function main() {
33
40
  program
34
41
  .command('deploy-l1-contracts')
42
+ .description('Deploys all necessary Ethereum contracts for Aztec.')
35
43
  .argument(
36
44
  '[rpcUrl]',
37
45
  'Url of the ethereum host. Chain identifiers localhost and testnet can be used',
38
- 'http://localhost:8545',
46
+ ETHEREUM_HOST || 'http://localhost:8545',
39
47
  )
40
- .option('-a, --api-key <string>', 'Api key for the ethereum host', undefined)
41
- .option('-p, --private-key <string>', 'The private key to use for deployment')
48
+ .option('-a, --api-key <string>', 'Api key for the ethereum host', API_KEY)
49
+ .option('-p, --private-key <string>', 'The private key to use for deployment', PRIVATE_KEY)
42
50
  .option(
43
51
  '-m, --mnemonic <string>',
44
52
  'The mnemonic to use in deployment',
45
53
  'test test test test test test test test test test test junk',
46
54
  )
47
55
  .action(async (rpcUrl: string, options) => {
48
- await deployAztecContracts(rpcUrl, options.apiKey ?? '', options.privateKey, options.mnemonic, debugLogger);
56
+ const { rollupAddress, registryAddress, inboxAddress, outboxAddress, contractDeploymentEmitterAddress } =
57
+ await deployAztecContracts(rpcUrl, options.apiKey ?? '', options.privateKey, options.mnemonic, debugLogger);
58
+ log('\n');
59
+ log(`Rollup Address: ${rollupAddress.toString()}`);
60
+ log(`Registry Address: ${registryAddress.toString()}`);
61
+ log(`L1 -> L2 Inbox Address: ${inboxAddress.toString()}`);
62
+ log(`L2 -> L1 Outbox address: ${outboxAddress.toString()}`);
63
+ log(`Contract Deployment Emitter Address: ${contractDeploymentEmitterAddress.toString()}`);
64
+ log('\n');
65
+ });
66
+
67
+ program
68
+ .command('create-private-key')
69
+ .description('Generates a 32-byte private key.')
70
+ .option('-m, --mnemonic', 'A mnemonic string that can be used for the private key generation.')
71
+ .action(options => {
72
+ let privKey;
73
+ if (options.mnemonic) {
74
+ const acc = mnemonicToAccount(options.mnemonic);
75
+ privKey = Buffer.from(acc.getHdKey().privateKey!).toString('hex');
76
+ } else {
77
+ privKey = randomBytes(32).toString('hex');
78
+ }
79
+ log(`\n${privKey}\n`);
80
+ });
81
+
82
+ program
83
+ .command('create-account')
84
+ .description('Creates an aztec account that can be used for transactions.')
85
+ .option(
86
+ '-k, --private-key <string>',
87
+ 'Private Key to use for the 1st account generation. Uses random by default.',
88
+ PRIVATE_KEY,
89
+ )
90
+ .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', AZTEC_RPC_HOST || 'http://localhost:8080')
91
+ .action(async options => {
92
+ const client = createAztecRpcClient(options.rpcUrl);
93
+ const privateKey = options.privateKey && Buffer.from(options.privateKey.replace(/^0x/i, ''), 'hex');
94
+ const wallet = await createAccounts(client, privateKey, accountCreationSalt, 1);
95
+ const accounts = await wallet.getAccounts();
96
+ const pubKeys = await Promise.all(accounts.map(acc => wallet.getAccountPublicKey(acc)));
97
+ log(`\nCreated account(s).`);
98
+ accounts.map((acc, i) => log(`\nAddress: ${acc.toString()}\nPublic Key: ${pubKeys[i].toString()}\n`));
49
99
  });
50
100
 
51
101
  program
52
102
  .command('deploy')
103
+ .description('Deploys a compiled Noir contract to Aztec.')
53
104
  .argument('<contractAbi>', "A compiled Noir contract's ABI in JSON format", undefined)
54
- .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', 'http://localhost:8080')
55
- .option('-k, --public-key <string>')
56
- .option('-a, --constructor-args [args...]', 'Contract constructor arguments', [])
57
- .action(async (contractFile: string, options: any) => {
105
+ .argument('[constructorArgs...]', 'Contract constructor arguments', [])
106
+ .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', AZTEC_RPC_HOST || 'http://localhost:8080')
107
+ .option(
108
+ '-k, --public-key <string>',
109
+ 'Public key of the deployer. If not provided, it will check the RPC for existing ones.',
110
+ PUBLIC_KEY,
111
+ )
112
+ .action(async (contractFile: string, args: string[], options: any) => {
58
113
  const contractAbi = getContractAbi(contractFile, log);
59
114
  const constructorAbi = contractAbi.functions.find(({ name }) => name === 'constructor');
60
115
 
61
- const publicKey = Point.fromString(options.publicKey);
62
116
  const client = createAztecRpcClient(options.rpcUrl);
117
+ let publicKey;
118
+ if (options.publicKey) {
119
+ publicKey = Point.fromString(options.publicKey);
120
+ } else {
121
+ const accounts = await client.getAccounts();
122
+ if (!accounts) {
123
+ throw new Error('No public key provided or found in Aztec RPC.');
124
+ }
125
+ publicKey = await client.getAccountPublicKey(accounts[0]);
126
+ }
127
+
128
+ log(`Using Public Key: ${publicKey.toString()}`);
129
+
63
130
  const deployer = new ContractDeployer(contractAbi, client);
64
131
 
65
- const tx = deployer
66
- .deploy(...encodeArgs(options.constructorArgs, constructorAbi!.parameters), pointToPublicKey(publicKey))
67
- .send();
132
+ const tx = deployer.deploy(...encodeArgs(args, constructorAbi!.parameters), publicKey.toBigInts()).send();
68
133
  await tx.isMined();
69
134
  const receipt = await tx.getReceipt();
70
- log(`Contract deployed at ${receipt.contractAddress?.toString()}`);
135
+ log(`\nAztec Contract deployed at ${receipt.contractAddress?.toString()}\n`);
71
136
  });
72
137
 
73
138
  program
74
139
  .command('check-deploy')
140
+ .description('Checks if a contract is deployed to the specified Aztec address.')
75
141
  .argument('<contractAddress>', 'An Aztec address to check if contract has been deployed to.')
76
- .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', 'http://localhost:8080')
142
+ .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', AZTEC_RPC_HOST || 'http://localhost:8080')
77
143
  .action(async (_contractAddress, options) => {
78
144
  const client = createAztecRpcClient(options.rpcUrl);
79
145
  const address = AztecAddress.fromString(_contractAddress);
80
146
  const isDeployed = await client.isContractDeployed(address);
81
- log(isDeployed.toString());
147
+ log(`\n${isDeployed.toString()}\n`);
82
148
  });
83
149
 
84
150
  program
85
151
  .command('get-tx-receipt')
152
+ .description('Gets the receipt for the specified transaction hash.')
86
153
  .argument('<txHash>', 'A TX hash to get the receipt for.')
87
- .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', 'http://localhost:8080')
154
+ .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', AZTEC_RPC_HOST || 'http://localhost:8080')
88
155
  .action(async (_txHash, options) => {
89
156
  const client = createAztecRpcClient(options.rpcUrl);
90
157
  const txHash = TxHash.fromString(_txHash);
@@ -92,14 +159,15 @@ async function main() {
92
159
  if (!receipt) {
93
160
  log(`No receipt found for tx hash ${_txHash}`);
94
161
  } else {
95
- log(`TX Receipt: \n${JsonStringify(receipt, true)}`);
162
+ log(`\nTX Receipt: \n${JsonStringify(receipt, true)}\n`);
96
163
  }
97
164
  });
98
165
 
99
166
  program
100
167
  .command('get-contract-data')
168
+ .description('Gets information about the Aztec contract deployed at the specified address.')
101
169
  .argument('<contractAddress>', 'Aztec address of the contract.')
102
- .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', 'http://localhost:8080')
170
+ .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', AZTEC_RPC_HOST || 'http://localhost:8080')
103
171
  .option('-b, --include-bytecode', "Include the contract's public function bytecode, if any.")
104
172
  .action(async (_contractAddress, options) => {
105
173
  const client = createAztecRpcClient(options.rpcUrl);
@@ -119,18 +187,20 @@ async function main() {
119
187
  } else {
120
188
  contractData = contractDataOrInfo;
121
189
  }
122
- log(`Contract Data: \nAddress: ${contractData.contractAddress.toString()}`);
190
+ log(`\nContract Data: \nAddress: ${contractData.contractAddress.toString()}`);
123
191
  log(`Portal: ${contractData.portalContractAddress.toString()}`);
124
192
  if ('bytecode' in contractDataOrInfo) {
125
193
  log(`Bytecode: ${contractDataOrInfo.bytecode}`);
126
194
  }
195
+ log('\n');
127
196
  });
128
197
 
129
198
  program
130
199
  .command('get-logs')
200
+ .description('Gets all the unencrypted logs from L2 blocks in the range specified.')
131
201
  .argument('<from>', 'Block num start for getting logs.')
132
202
  .argument('<take>', 'How many block logs to fetch.')
133
- .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', 'http://localhost:8080')
203
+ .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', AZTEC_RPC_HOST || 'http://localhost:8080')
134
204
  .action(async (_from, _take, options) => {
135
205
  let from: number;
136
206
  let take: number;
@@ -151,26 +221,9 @@ async function main() {
151
221
  }
152
222
  });
153
223
 
154
- // NOTE: This implementation should change soon but keeping it here for quick account creation.
155
- program
156
- .command('create-account')
157
- .option('-k, --private-key', 'Private Key to use for the 1st account generation.')
158
- .option('-n, --num-addresses <number>', 'Number of addresses the account can control')
159
- .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', 'http://localhost:8080')
160
- .action(async options => {
161
- const client = createAztecRpcClient(options.rpcUrl);
162
- const privateKey = options.privateKey && Buffer.from(options.privateKeystr.replace(/^0x/i, ''), 'hex');
163
- const numAccounts = options.numAddresses ? parseInt(options.numAddresses) : 1;
164
- const wallet = await createAccounts(client, privateKey, numAccounts);
165
- const accounts = await wallet.getAccounts();
166
- const pubKeys = await Promise.all(accounts.map(acc => wallet.getAccountPublicKey(acc)));
167
- log(`Created account(s).`);
168
- accounts.map((acc, i) => log(`\nAddress: ${acc.toString()}\nPublic Key: ${pubKeys[i].toString()}\n`));
169
- });
170
-
171
224
  program
172
225
  .command('get-accounts')
173
- .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', 'http://localhost:8080')
226
+ .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', AZTEC_RPC_HOST || 'http://localhost:8080')
174
227
  .action(async (options: any) => {
175
228
  const client = createAztecRpcClient(options.rpcUrl);
176
229
  const accounts = await client.getAccounts();
@@ -178,14 +231,15 @@ async function main() {
178
231
  log('No accounts found.');
179
232
  } else {
180
233
  log(`Accounts found: \n`);
181
- accounts.forEach(acc => log(`${acc}\n`));
234
+ accounts.forEach(async acc => log(`Address: ${acc}\nPublic Key: ${await client.getAccountPublicKey(acc)}\n`));
182
235
  }
183
236
  });
184
237
 
185
238
  program
186
239
  .command('get-account-public-key')
240
+ .description("Gets an account's public key, given its Aztec address.")
187
241
  .argument('<address>', 'The Aztec address to get the public key for')
188
- .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', 'http://localhost:8080')
242
+ .option('-u, --rpc-url <string>', 'URL of the Aztec RPC', AZTEC_RPC_HOST || 'http://localhost:8080')
189
243
  .action(async (_address, options) => {
190
244
  const client = createAztecRpcClient(options.rpcUrl);
191
245
  const address = AztecAddress.fromString(_address);
@@ -199,59 +253,68 @@ async function main() {
199
253
 
200
254
  program
201
255
  .command('call-fn')
256
+ .description('Calls a function on an Aztec contract.')
202
257
  .argument('<contractAbi>', "The compiled contract's ABI in JSON format", undefined)
203
258
  .argument('<contractAddress>', 'Address of the contract')
204
259
  .argument('<functionName>', 'Name of Function to view')
205
- .argument('[from]', 'The caller of the transaction', undefined)
206
260
  .argument('[functionArgs...]', 'Function arguments', [])
207
- .option('-u, --rpcUrl <string>', 'URL of the Aztec RPC', 'http://localhost:8080')
208
- .action(async (contractFile, _contractAddress, functionName, _from, _functionArgs, options) => {
209
- const { contractAddress, functionArgs, from, contractAbi } = prepTx(
261
+ .option('-k, --private-key <string>', "The sender's private key.", PRIVATE_KEY)
262
+ .option('-u, --rpcUrl <string>', 'URL of the Aztec RPC', AZTEC_RPC_HOST || 'http://localhost:8080')
263
+
264
+ .action(async (contractFile, _contractAddress, functionName, _functionArgs, options) => {
265
+ const { contractAddress, functionArgs, contractAbi } = prepTx(
210
266
  contractFile,
211
267
  _contractAddress,
212
268
  functionName,
213
- _from,
214
269
  _functionArgs,
215
270
  log,
216
271
  );
272
+
217
273
  const client = createAztecRpcClient(options.rpcUrl);
218
- const wallet = await createAccounts(client);
274
+ const wallet = await getAccountWallet(client, Buffer.from(options.privateKey, 'hex'), accountCreationSalt);
219
275
  const contract = new Contract(contractAddress, contractAbi, wallet);
220
- const tx = contract.methods[functionName](...functionArgs).send({ from });
276
+ const from = (await wallet.getAccounts()).find(addr => addr.equals(wallet.getAddress()));
277
+ const tx = contract.methods[functionName](...functionArgs).send({
278
+ from,
279
+ });
221
280
  await tx.isMined();
222
- log('TX has been mined');
281
+ log('\nTX has been mined');
223
282
  const receipt = await tx.getReceipt();
224
283
  log(`TX Hash: ${(await tx.getTxHash()).toString()}`);
225
284
  log(`Block Num: ${receipt.blockNumber}`);
226
285
  log(`Block Hash: ${receipt.blockHash?.toString('hex')}`);
227
- log(`TX Status: ${receipt.status}`);
286
+ log(`TX Status: ${receipt.status}\n`);
228
287
  });
229
288
 
230
289
  program
231
- .command('view-tx')
290
+ .command('view-fn')
291
+ .description(
292
+ 'Simulates the execution of a view (read-only) function on a deployed contract, without modifying state.',
293
+ )
232
294
  .argument('<contractAbi>', "The compiled contract's ABI in JSON format", undefined)
233
295
  .argument('<contractAddress>', 'Address of the contract')
234
296
  .argument('<functionName>', 'Name of Function to view')
235
- .argument('[from]', 'The caller of the transaction', undefined)
236
297
  .argument('[functionArgs...]', 'Function arguments', [])
237
- .option('-u, --rpcUrl <string>', 'URL of the Aztec RPC', 'http://localhost:8080')
238
- .action(async (contractFile, _contractAddress, functionName, _from, _functionArgs, options) => {
239
- const { contractAddress, functionArgs, from } = prepTx(
298
+ .option('-f, --from <string>', 'Public key of the TX viewer. If empty, will try to find account in RPC.')
299
+ .option('-u, --rpcUrl <string>', 'URL of the Aztec RPC', AZTEC_RPC_HOST || 'http://localhost:8080')
300
+ .action(async (contractFile, _contractAddress, functionName, _functionArgs, options) => {
301
+ const { contractAddress, functionArgs } = prepTx(
240
302
  contractFile,
241
303
  _contractAddress,
242
304
  functionName,
243
- _from,
244
305
  _functionArgs,
245
306
  log,
246
307
  );
247
308
  const client = createAztecRpcClient(options.rpcUrl);
309
+ const from = await getTxSender(client, options.from);
248
310
  const result = await client.viewTx(functionName, functionArgs, contractAddress, from);
249
- log('View TX returned result: ', JsonStringify(result, true));
311
+ log('\nView TX result: ', JsonStringify(result, true), '\n');
250
312
  });
251
313
 
252
314
  // Helper for users to decode hex strings into structs if needed
253
315
  program
254
316
  .command('parse-parameter-struct')
317
+ .description("Helper for parsing an encoded string into a contract's parameter struct.")
255
318
  .argument('<encodedString>', 'The encoded hex string')
256
319
  .argument('<contractAbi>', "The compiled contract's ABI in JSON format")
257
320
  .argument('<parameterName>', 'The name of the struct parameter to decode into')
@@ -266,16 +329,17 @@ async function main() {
266
329
  return;
267
330
  }
268
331
  const data = parseStructString(encodedString, parameterAbitype.type as StructType);
269
- log(`Struct Data: \n${JsonStringify(data, true)}`);
332
+ log(`\nStruct Data: \n${JsonStringify(data, true)}\n`);
270
333
  });
271
334
 
272
335
  program
273
336
  .command('block-num')
274
- .option('-u, --rpcUrl <string>', 'URL of the Aztec RPC', 'http://localhost:8080')
337
+ .description('Gets the current Aztec L2 number.')
338
+ .option('-u, --rpcUrl <string>', 'URL of the Aztec RPC', AZTEC_RPC_HOST || 'http://localhost:8080')
275
339
  .action(async (options: any) => {
276
340
  const client = createAztecRpcClient(options.rpcUrl);
277
341
  const num = await client.getBlockNum();
278
- log(num);
342
+ log(`${num}\n`);
279
343
  });
280
344
 
281
345
  await program.parseAsync(process.argv);
package/src/utils.ts CHANGED
@@ -3,7 +3,7 @@ import fs from 'fs';
3
3
  import { createEthereumChain, deployL1Contracts } from '@aztec/ethereum';
4
4
  import { DebugLogger, Logger } from '@aztec/foundation/log';
5
5
  import { ContractAbi } from '@aztec/foundation/abi';
6
- import { AztecAddress } from '@aztec/aztec.js';
6
+ import { AztecAddress, AztecRPC } from '@aztec/aztec.js';
7
7
  import { encodeArgs } from './cli_encoder.js';
8
8
 
9
9
  /**
@@ -22,7 +22,7 @@ export async function deployAztecContracts(
22
22
  ) {
23
23
  const account = !privateKey ? mnemonicToAccount(mnemonic!) : privateKeyToAccount(`0x${privateKey}`);
24
24
  const chain = createEthereumChain(rpcUrl, apiKey);
25
- await deployL1Contracts(chain.rpcUrl, account, chain.chainInfo, debugLogger);
25
+ return await deployL1Contracts(chain.rpcUrl, account, chain.chainInfo, debugLogger);
26
26
  }
27
27
 
28
28
  /**
@@ -42,12 +42,36 @@ export function getContractAbi(fileDir: string, log: Logger) {
42
42
  return contractAbi;
43
43
  }
44
44
 
45
+ /**
46
+ * Utility to select a TX sender either from user input
47
+ * or from the first account that is found in an Aztec RPC instance.
48
+ * @param client - The Aztec RPC instance that will be checked for an account.
49
+ * @param _from - The user input.
50
+ * @returns An Aztec address. Will throw if one can't be found in either options.
51
+ */
52
+ export async function getTxSender(client: AztecRPC, _from?: string) {
53
+ let from: AztecAddress;
54
+ if (_from) {
55
+ try {
56
+ from = AztecAddress.fromString(_from);
57
+ } catch {
58
+ throw new Error(`Invalid option 'from' passed: ${_from}`);
59
+ }
60
+ } else {
61
+ const accounts = await client.getAccounts();
62
+ if (!accounts.length) {
63
+ throw new Error('No accounts found in Aztec RPC insance.');
64
+ }
65
+ from = accounts[0];
66
+ }
67
+ return from;
68
+ }
69
+
45
70
  /**
46
71
  * Performs necessary checks, conversions & operations to call a contract fn from the CLI.
47
72
  * @param contractFile - Directory of the compiled contract ABI.
48
73
  * @param _contractAddress - Aztec Address of the contract.
49
74
  * @param functionName - Name of the function to be called.
50
- * @param _from - The caller's address.
51
75
  * @param _functionArgs - Arguments to call the function with.
52
76
  * @param log - Logger instance that will output to the CLI
53
77
  * @returns Formatted contract address, function arguments and caller's aztec address.
@@ -56,7 +80,6 @@ export function prepTx(
56
80
  contractFile: string,
57
81
  _contractAddress: string,
58
82
  functionName: string,
59
- _from: string,
60
83
  _functionArgs: string[],
61
84
  log: Logger,
62
85
  ) {
@@ -73,14 +96,6 @@ export function prepTx(
73
96
  }
74
97
 
75
98
  const functionArgs = encodeArgs(_functionArgs, functionAbi.parameters);
76
- let from;
77
- if (_from) {
78
- try {
79
- from = AztecAddress.fromString(_from);
80
- } catch {
81
- throw new Error(`Unable to parse caller address ${_from}.`);
82
- }
83
- }
84
99
 
85
- return { contractAddress, functionArgs, from, contractAbi };
100
+ return { contractAddress, functionArgs, contractAbi };
86
101
  }
package/tsconfig.json CHANGED
@@ -18,4 +18,4 @@
18
18
  ],
19
19
  "include": ["src"],
20
20
  "exclude": ["contracts"]
21
- }
21
+ }