Skip to content

Commit

Permalink
refactor: account instead of singer
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Aug 15, 2023
1 parent e1effe8 commit 2f90170
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ describe('AztecRpcServer', function () {
const keyPair = ConstantKeyPair.random(await Grumpkin.new());
const completeAddress = await CompleteAddress.fromPrivateKey(await keyPair.getPrivateKey());

await rpcServer.registerSigner(await keyPair.getPrivateKey(), completeAddress);
await rpcServer.registerAccount(await keyPair.getPrivateKey(), completeAddress);
expect(await db.getAccount(completeAddress.address)).toEqual(completeAddress);
});

it('cannot add the same account twice', async () => {
const keyPair = ConstantKeyPair.random(await Grumpkin.new());
const completeAddress = await CompleteAddress.fromPrivateKey(await keyPair.getPrivateKey());

await rpcServer.registerSigner(await keyPair.getPrivateKey(), completeAddress);
await expect(async () => rpcServer.registerSigner(await keyPair.getPrivateKey(), completeAddress)).rejects.toThrow(
await rpcServer.registerAccount(await keyPair.getPrivateKey(), completeAddress);
await expect(async () => rpcServer.registerAccount(await keyPair.getPrivateKey(), completeAddress)).rejects.toThrow(
`Account ${completeAddress.address} already exists`,
);
});
Expand Down
42 changes: 27 additions & 15 deletions yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class AztecRPCServer implements AztecRPC {
this.log.info('Stopped');
}

public async registerSigner(privKey: PrivateKey, account: CompleteAddress) {
public async registerAccount(privKey: PrivateKey, account: CompleteAddress) {
const pubKey = this.keyStore.addAccount(privKey);
// TODO: Re-enable this check once https://github.com/AztecProtocol/aztec-packages/issues/1556 is solved
// if (!pubKey.equals(account.publicKey)) {
Expand All @@ -91,33 +91,45 @@ export class AztecRPCServer implements AztecRPC {
this.synchroniser.addAccount(pubKey, this.keyStore);
}

public async registerRecipient(account: CompleteAddress): Promise<void> {
await this.db.addAccount(account);
this.log.info(`Added account: ${account.toString()}`);
public async getAccounts(): Promise<CompleteAddress[]> {
return await this.db.getAccounts();
}

public async addContracts(contracts: DeployedContract[]) {
const contractDaos = contracts.map(c => toContractDao(c.abi, c.address, c.portalContract));
await Promise.all(contractDaos.map(c => this.db.addContract(c)));
for (const contract of contractDaos) {
const portalInfo =
contract.portalContract && !contract.portalContract.isZero() ? ` with portal ${contract.portalContract}` : '';
this.log.info(`Added contract ${contract.name} at ${contract.address}${portalInfo}`);
public async getAccount(address: AztecAddress): Promise<CompleteAddress> {
const result = await this.db.getAccount(address);
if (!result) {
throw new Error(`Unable to get public key for address ${address.toString()}`);
}
return Promise.resolve(result);
}

public async getAccounts(): Promise<CompleteAddress[]> {
return await this.db.getAccounts();
public async registerRecipient(recipient: CompleteAddress): Promise<void> {
await this.db.addRecipient(recipient);
this.log.info(`Added recipient: ${recipient.toString()}`);
}

public async getAccount(address: AztecAddress): Promise<CompleteAddress> {
const result = await this.db.getAccount(address);
public async getRecipients(): Promise<CompleteAddress[]> {
return await this.db.getRecipients();
}

public async getRecipient(address: AztecAddress): Promise<CompleteAddress> {
const result = await this.db.getRecipient(address);
if (!result) {
throw new Error(`Unable to get public key for address ${address.toString()}`);
}
return Promise.resolve(result);
}

public async addContracts(contracts: DeployedContract[]) {
const contractDaos = contracts.map(c => toContractDao(c.abi, c.address, c.portalContract));
await Promise.all(contractDaos.map(c => this.db.addContract(c)));
for (const contract of contractDaos) {
const portalInfo =
contract.portalContract && !contract.portalContract.isZero() ? ` with portal ${contract.portalContract}` : '';
this.log.info(`Added contract ${contract.name} at ${contract.address}${portalInfo}`);
}
}

public async getPublicStorageAt(contract: AztecAddress, storageSlot: Fr) {
if ((await this.getContractData(contract)) === undefined) {
throw new Error(`Contract ${contract.toString()} is not deployed`);
Expand Down
20 changes: 20 additions & 0 deletions yarn-project/aztec-rpc/src/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,24 @@ export interface Database extends ContractDatabase {
* @returns A promise that resolves to an array of AztecAddress instances.
*/
getAccounts(): Promise<CompleteAddress[]>;

/**
* Adds recipient to the database.
* @param recipient - The recipient to add.
* @returns Empty promise.
*/
addRecipient(recipient: CompleteAddress): Promise<void>;

/**
* Retrieves the recipient corresponding to the provided aztec address.
* @param address - The aztec address of the recipient contract.
* @returns A promise that resolves to a CompleteAddress instance if the address is found, or undefined if not found.
*/
getRecipient(address: AztecAddress): Promise<CompleteAddress | undefined>;

/**
* Retrieves the list of recipients added to this database
* @returns A promise that resolves to an array of AztecAddress instances.
*/
getRecipients(): Promise<CompleteAddress[]>;
}
19 changes: 19 additions & 0 deletions yarn-project/aztec-rpc/src/database/memory_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class MemoryDB extends MemoryContractDatabase implements Database {
private noteSpendingInfoTable: NoteSpendingInfoDao[] = [];
private treeRoots: Record<MerkleTreeId, Fr> | undefined;
private accounts: CompleteAddress[] = [];
private recipients: CompleteAddress[] = [];

constructor(logSuffix?: string) {
super(createDebugLogger(logSuffix ? 'aztec:memory_db_' + logSuffix : 'aztec:memory_db'));
Expand Down Expand Up @@ -110,4 +111,22 @@ export class MemoryDB extends MemoryContractDatabase implements Database {
getAccounts(): Promise<CompleteAddress[]> {
return Promise.resolve(this.accounts);
}

addRecipient(recipient: CompleteAddress): Promise<void> {
const recipientIndex = this.recipients.findIndex(r => r.address.equals(recipient.address));
if (recipientIndex !== -1) {
throw new Error(`Recipient ${recipient.address.toString()} already exists in memory database`);
}
this.recipients.push(recipient);
return Promise.resolve();
}

getRecipient(address: AztecAddress): Promise<CompleteAddress | undefined> {
const recipient = this.recipients.find(r => r.address.equals(address));
return Promise.resolve(recipient);
}

getRecipients(): Promise<CompleteAddress[]> {
return Promise.resolve(this.recipients);
}
}
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/account/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class Account {
*/
public async register(): Promise<AccountWallet> {
const completeAddress = await this.getCompleteAddress();
await this.rpc.registerSigner(this.encryptionPrivateKey, completeAddress);
await this.rpc.registerAccount(this.encryptionPrivateKey, completeAddress);
return this.getWallet();
}

Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec.js/src/aztec_rpc_client/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export abstract class BaseWallet implements Wallet {

abstract createTxExecutionRequest(execs: FunctionCall[], opts?: CreateTxRequestOpts): Promise<TxExecutionRequest>;

registerSigner(privKey: PrivateKey, completeAddress: CompleteAddress): Promise<void> {
return this.rpc.registerSigner(privKey, completeAddress);
registerAccount(privKey: PrivateKey, completeAddress: CompleteAddress): Promise<void> {
return this.rpc.registerAccount(privKey, completeAddress);
}
registerRecipient(account: CompleteAddress): Promise<void> {
return this.rpc.registerRecipient(account);
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/utils/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function createAccounts(
const privKey = i == 0 && privateKey ? privateKey : PrivateKey.random();
const publicKey = await generatePublicKey(privKey);
const deploymentInfo = await getContractDeploymentInfo(accountContractAbi, [], salt, publicKey);
await aztecRpcClient.registerSigner(privKey, deploymentInfo.completeAddress);
await aztecRpcClient.registerAccount(privKey, deploymentInfo.completeAddress);
const contractDeployer = new ContractDeployer(accountContractAbi, aztecRpcClient, publicKey);
const tx = contractDeployer.deploy().send({ contractAddressSalt: salt });
await tx.isMined({ interval: 0.5 });
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/src/e2e_escrow_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('e2e_escrow_contract', () => {
escrowPublicKey = await generatePublicKey(escrowPrivateKey);
const salt = Fr.random();
const deployInfo = await getContractDeploymentInfo(EscrowContractAbi, [owner], salt, escrowPublicKey);
await aztecRpcServer.registerSigner(escrowPrivateKey, deployInfo.completeAddress);
await aztecRpcServer.registerAccount(escrowPrivateKey, deployInfo.completeAddress);

escrowContract = await EscrowContract.deployWithPublicKey(wallet, escrowPublicKey, owner)
.send({ contractAddressSalt: salt })
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/src/e2e_p2p_network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('e2e_p2p_network', () => {

const keyPair = ConstantKeyPair.random(await Grumpkin.new());
const completeAddress = await CompleteAddress.fromPrivateKey(await keyPair.getPrivateKey());
await aztecRpcServer.registerSigner(await keyPair.getPrivateKey(), completeAddress);
await aztecRpcServer.registerAccount(await keyPair.getPrivateKey(), completeAddress);

const txs = await submitTxsTo(aztecRpcServer, completeAddress.address, numTxs, completeAddress.publicKey);
return {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/types/src/interfaces/aztec_rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export interface AztecRPC {
* @param completeAddress - Complete address of the account.
* @returns Empty promise.
*/
registerSigner(privKey: PrivateKey, completeAddress: CompleteAddress): Promise<void>;
registerAccount(privKey: PrivateKey, completeAddress: CompleteAddress): Promise<void>;

/**
* Registers recipient account in the Aztec RPC server.
Expand Down

0 comments on commit 2f90170

Please sign in to comment.