How to Create a New Account
This guide explains how to create a new account using Aztec.js.
An in-depth explainer about accounts on Aztec can be found here. Creating an account does two things:
- Deploys an account contract -- representing you -- allowing you to perform actions on the network (deploy contracts, call functions etc).
- Adds your encryption keys to the Private eXecution Environment (PXE) allowing it to decrypt and manage your private state.
Relevant imports
You will need to import these libraries:
create_account_imports
import { getSchnorrAccount } from '@aztec/accounts/schnorr';
import { getDeployedTestAccountsWallets } from '@aztec/accounts/testing';
import { Fr, GrumpkinScalar, createPXEClient } from '@aztec/aztec.js';
Source code: yarn-project/end-to-end/src/composed/docs_examples.test.ts#L2-L6
Define arguments needed
define_account_vars
const PXE_URL = process.env.PXE_URL || 'http://localhost:8080';
const pxe = createPXEClient(PXE_URL);
const secretKey = Fr.random();
const signingPrivateKey = GrumpkinScalar.random();
Source code: yarn-project/end-to-end/src/composed/docs_examples.test.ts#L18-L23
Create the wallet with these args
create_wallet
// Use a pre-funded wallet to pay for the fees for the deployments.
const wallet = (await getDeployedTestAccountsWallets(pxe))[0];
const newAccount = await getSchnorrAccount(pxe, secretKey, signingPrivateKey);
await newAccount.deploy({ deployWallet: wallet }).wait();
const newWallet = await newAccount.getWallet();
const newAccountAddress = newWallet.getAddress();
Source code: yarn-project/end-to-end/src/composed/docs_examples.test.ts#L25-L32
Now you have a new wallet in your PXE! To learn how to use this wallet to deploy a contract, read this guide.
What happens during account creation
When you create an account, several things happen under the hood:
- The account contract is deployed to the network
- The deployment transaction is simulated and executed through the private kernel circuit
- The transaction is submitted to the pool and picked up by the sequencer
- The sequencer includes it in an L2 block and executes the recursive rollup circuits
- The L2 block is published on L1
- The PXE retrieves the L2 block and decrypts any new account state
Creating specific account types
By default, Aztec.js provides several account implementations including Schnorr accounts. The example above uses the getDeployedTestAccountsWallets
helper which creates Schnorr accounts by default.
Next Steps
- Check out our section on Writing your own Account Contract leveraging our account abstraction
- Learn how to deploy contracts with your new account