Skip to content

Commit

Permalink
feat(cli): Publicly deploy a pre-initialized account (#6960)
Browse files Browse the repository at this point in the history
After running a `create-account`, the owner can later run
`create-acccount --skip-initialization --public-deploy` to publicly
deploy it, and `get-contract-data` to check its status.
  • Loading branch information
spalladino authored Jun 6, 2024
1 parent bf38cc2 commit e671935
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 50 deletions.
4 changes: 2 additions & 2 deletions yarn-project/aztec.js/src/account_manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { DeployAccountSentTx } from './deploy_account_sent_tx.js';
*/
export type DeployAccountOptions = Pick<
DeployOptions,
'fee' | 'skipClassRegistration' | 'skipPublicDeployment' | 'estimateGas'
'fee' | 'skipClassRegistration' | 'skipPublicDeployment' | 'estimateGas' | 'skipInitialization'
>;

/**
Expand Down Expand Up @@ -166,7 +166,7 @@ export class AccountManager {
contractAddressSalt: this.salt,
skipClassRegistration: opts?.skipClassRegistration ?? true,
skipPublicDeployment: opts?.skipPublicDeployment ?? true,
skipInitialization: false,
skipInitialization: opts?.skipInitialization ?? false,
universalDeploy: true,
fee: opts?.fee,
estimateGas: opts?.estimateGas,
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export { createPXEClient } from './rpc_clients/index.js';
export { AuthWitnessProvider } from './account/index.js';

export { AccountContract } from './account/index.js';
export { AccountManager } from './account_manager/index.js';
export { AccountManager, DeployAccountOptions } from './account_manager/index.js';

export { AccountWallet, AccountWalletWithSecretKey, SignerlessWallet, Wallet } from './wallet/index.js';

Expand Down
19 changes: 0 additions & 19 deletions yarn-project/cli/src/cmds/check_deploy.ts

This file was deleted.

10 changes: 9 additions & 1 deletion yarn-project/cli/src/cmds/create_account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getSchnorrAccount } from '@aztec/accounts/schnorr';
import { type DeployAccountOptions } from '@aztec/aztec.js';
import { deriveSigningKey } from '@aztec/circuits.js';
import { Fr } from '@aztec/foundation/fields';
import { type DebugLogger, type LogFn } from '@aztec/foundation/log';
Expand All @@ -10,6 +11,8 @@ export async function createAccount(
rpcUrl: string,
privateKey: Fr | undefined,
registerOnly: boolean,
publicDeploy: boolean,
skipInitialization: boolean,
wait: boolean,
feeOpts: IFeeOpts,
debugLogger: DebugLogger,
Expand Down Expand Up @@ -40,7 +43,12 @@ export async function createAccount(
await account.register();
} else {
const wallet = await account.getWallet();
const sendOpts = feeOpts.toSendOpts(wallet);
const sendOpts: DeployAccountOptions = {
...feeOpts.toSendOpts(wallet),
skipClassRegistration: !publicDeploy,
skipPublicDeployment: !publicDeploy,
skipInitialization: skipInitialization,
};
if (feeOpts.estimateOnly) {
const gas = await (await account.getDeployMethod()).estimateGas({ ...sendOpts });
printGasEstimates(feeOpts, gas, log);
Expand Down
31 changes: 20 additions & 11 deletions yarn-project/cli/src/cmds/get_contract_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@ export async function getContractData(
const instance = await client.getContractInstance(contractAddress);
const contractClass = includeBytecode && instance && (await client.getContractClass(instance?.contractClassId));

if (!instance) {
log(`No contract found at ${contractAddress}`);
return;
const isPrivatelyDeployed = !!instance;
const isPubliclyDeployed = await client.isContractPubliclyDeployed(contractAddress);
if (isPubliclyDeployed && isPrivatelyDeployed) {
log(`Contract is publicly deployed at ${contractAddress.toString()}`);
} else if (isPrivatelyDeployed) {
log(`Contract is registered in the local pxe at ${contractAddress.toString()} but not publicly deployed`);
} else if (isPubliclyDeployed) {
log(`Contract is publicly deployed at ${contractAddress.toString()} but not registered in the local pxe`);
} else {
log(`No contract found at ${contractAddress.toString()}`);
}

log(`\nContract Data:`);
Object.entries(instance).forEach(([key, value]) => {
const capitalized = key.charAt(0).toUpperCase() + key.slice(1);
log(`${capitalized}: ${value.toString()}`);
});
if (instance) {
log(``);
Object.entries(instance).forEach(([key, value]) => {
const capitalized = key.charAt(0).toUpperCase() + key.slice(1);
log(`${capitalized}: ${value.toString()}`);
});

if (contractClass) {
log(`Bytecode: ${contractClass.packedBytecode.toString('base64')}`);
if (contractClass) {
log(`\nBytecode: ${contractClass.packedBytecode.toString('base64')}`);
}
log('');
}
log('\n');
}
33 changes: 17 additions & 16 deletions yarn-project/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
'Creates an aztec account that can be used for sending transactions. Registers the account on the PXE and deploys an account contract. Uses a Schnorr single-key account which uses the same key for encryption and authentication (not secure for production usage).',
)
.summary('Creates an aztec account that can be used for sending transactions.')
.option(
'--skip-initialization',
'Skip initializing the account contract. Useful for publicly deploying an existing account.',
)
.option('--public-deploy', 'Publicly deploys the account and registers the class if needed.')
.addOption(createPrivateKeyOption('Private key for account. Uses random by default.', false))
.addOption(pxeOption)
.addOptions(FeeOpts.getOptions())
Expand All @@ -232,8 +237,18 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
.option('--no-wait', 'Skip waiting for the contract to be deployed. Print the hash of deployment transaction')
.action(async args => {
const { createAccount } = await import('./cmds/create_account.js');
const { rpcUrl, privateKey, wait, registerOnly } = args;
await createAccount(rpcUrl, privateKey, registerOnly, wait, FeeOpts.fromCli(args, log), debugLogger, log);
const { rpcUrl, privateKey, wait, registerOnly, skipInitialization, publicDeploy } = args;
await createAccount(
rpcUrl,
privateKey,
registerOnly,
skipInitialization,
publicDeploy,
wait,
FeeOpts.fromCli(args, log),
debugLogger,
log,
);
});

program
Expand Down Expand Up @@ -313,20 +328,6 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
);
});

program
.command('check-deploy')
.description('Checks if a contract is deployed to the specified Aztec address.')
.requiredOption(
'-ca, --contract-address <address>',
'An Aztec address to check if contract has been deployed to.',
parseAztecAddress,
)
.addOption(pxeOption)
.action(async options => {
const { checkDeploy } = await import('./cmds/check_deploy.js');
await checkDeploy(options.rpcUrl, options.contractAddress, debugLogger, log);
});

program
.command('add-contract')
.description(
Expand Down

0 comments on commit e671935

Please sign in to comment.