Skip to content

Commit

Permalink
final interface refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Aug 15, 2023
1 parent e1effe8 commit d15bfcf
Show file tree
Hide file tree
Showing 19 changed files with 153 additions and 60 deletions.
2 changes: 1 addition & 1 deletion yarn-project/acir-simulator/src/client/db_oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export interface DBOracle extends CommitmentsDB {
* @param address - Address to fetch the pubkey for.
* @returns A complete address associated with the input address.
*/
getAccount(address: AztecAddress): Promise<CompleteAddress>;
getCompleteAddress(address: AztecAddress): Promise<CompleteAddress>;

/**
* Retrieve the secret key associated with a specific public key.
Expand Down
10 changes: 5 additions & 5 deletions yarn-project/acir-simulator/src/client/private_execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('Private Execution test suite', () => {
owner = ownerCompleteAddress.address;
recipient = recipientCompleteAddress.address;

oracle.getAccount.mockImplementation((address: AztecAddress) => {
oracle.getCompleteAddress.mockImplementation((address: AztecAddress) => {
if (address.equals(owner)) return Promise.resolve(ownerCompleteAddress);
if (address.equals(recipient)) return Promise.resolve(recipientCompleteAddress);
throw new Error(`Unknown address ${address}`);
Expand Down Expand Up @@ -429,7 +429,7 @@ describe('Private Execution test suite', () => {
owner = ownerCompleteAddress.address;
recipient = recipientCompleteAddress.address;

oracle.getAccount.mockImplementation((address: AztecAddress) => {
oracle.getCompleteAddress.mockImplementation((address: AztecAddress) => {
if (address.equals(owner)) return Promise.resolve(ownerCompleteAddress);
if (address.equals(recipient)) return Promise.resolve(recipientCompleteAddress);
throw new Error(`Unknown address ${address}`);
Expand Down Expand Up @@ -632,7 +632,7 @@ describe('Private Execution test suite', () => {
beforeEach(async () => {
const recipientCompleteAddress = await CompleteAddress.fromPrivateKey(recipientPk);
recipient = recipientCompleteAddress.address;
oracle.getAccount.mockImplementation((address: AztecAddress) => {
oracle.getCompleteAddress.mockImplementation((address: AztecAddress) => {
if (address.equals(recipient)) return Promise.resolve(recipientCompleteAddress);
throw new Error(`Unknown address ${address}`);
});
Expand Down Expand Up @@ -773,7 +773,7 @@ describe('Private Execution test suite', () => {

owner = ownerCompleteAddress.address;

oracle.getAccount.mockImplementation((address: AztecAddress) => {
oracle.getCompleteAddress.mockImplementation((address: AztecAddress) => {
if (address.equals(owner)) return Promise.resolve(ownerCompleteAddress);
throw new Error(`Unknown address ${address}`);
});
Expand Down Expand Up @@ -959,7 +959,7 @@ describe('Private Execution test suite', () => {
const args = [completeAddress.address];
const pubKey = completeAddress.publicKey;

oracle.getAccount.mockResolvedValue(completeAddress);
oracle.getCompleteAddress.mockResolvedValue(completeAddress);
const result = await runSimulator({ origin: AztecAddress.random(), abi, args });
expect(result.returnValues).toEqual([pubKey.x.value, pubKey.y.value]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class PrivateFunctionExecution {
getSecretKey: ([ownerX], [ownerY]) => this.context.getSecretKey(this.contractAddress, ownerX, ownerY),
getPublicKey: async ([acvmAddress]) => {
const address = frToAztecAddress(fromACVMField(acvmAddress));
const { publicKey, partialAddress } = await this.context.db.getAccount(address);
const { publicKey, partialAddress } = await this.context.db.getCompleteAddress(address);
return [publicKey.x, publicKey.y, partialAddress].map(toACVMField);
},
getNotes: ([slot], sortBy, sortOrder, [limit], [offset], [returnSize]) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Unconstrained Execution test suite', () => {
const ownerCompleteAddress = await CompleteAddress.fromPrivateKey(ownerPk);
owner = ownerCompleteAddress.address;

oracle.getAccount.mockImplementation((address: AztecAddress) => {
oracle.getCompleteAddress.mockImplementation((address: AztecAddress) => {
if (address.equals(owner)) return Promise.resolve(ownerCompleteAddress);
throw new Error(`Unknown address ${address}`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class UnconstrainedFunctionExecution {
getSecretKey: ([ownerX], [ownerY]) => this.context.getSecretKey(this.contractAddress, ownerX, ownerY),
getPublicKey: async ([acvmAddress]) => {
const address = frToAztecAddress(fromACVMField(acvmAddress));
const { publicKey, partialAddress } = await this.context.db.getAccount(address);
const { publicKey, partialAddress } = await this.context.db.getCompleteAddress(address);
return [publicKey.x, publicKey.y, partialAddress].map(toACVMField);
},
getNotes: ([slot], sortBy, sortOrder, [limit], [offset], [returnSize]) =>
Expand Down
34 changes: 34 additions & 0 deletions yarn-project/aztec-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,40 @@ async function main() {
}
});

program
.command('get-recipients')
.description('Gets all the recipients stored in the Aztec RPC.')
.option('-u, --rpc-url <string>', 'URL of the Aztec RPC', AZTEC_RPC_HOST || 'http://localhost:8080')
.action(async (options: any) => {
const client = createAztecRpcClient(options.rpcUrl);
const recipients = await client.getRecipients();
if (!recipients.length) {
log('No recipients found.');
} else {
log(`Recipients found: \n`);
for (const recipient of recipients) {
log(recipient.toReadableString());
}
}
});

program
.command('get-recipient')
.description('Gets an recipient given its Aztec address.')
.argument('<address>', 'The Aztec address to get recipient for')
.option('-u, --rpc-url <string>', 'URL of the Aztec RPC', AZTEC_RPC_HOST || 'http://localhost:8080')
.action(async (_address, options) => {
const client = createAztecRpcClient(options.rpcUrl);
const address = AztecAddress.fromString(_address);
const recipient = await client.getRecipient(address);

if (!recipient) {
log(`Unknown recipient ${_address}`);
} else {
log(recipient.toReadableString());
}
});

program
.command('send')
.description('Calls a function on an Aztec contract.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,33 @@ describe('AztecRpcServer', function () {
rpcServer = new AztecRPCServer(keyStore, node, db, config);
});

it('registers a public key in the db when adding a new account', async () => {
it('registers an account and returns it as an account only and not as a recipient', async () => {
const keyPair = ConstantKeyPair.random(await Grumpkin.new());
const completeAddress = await CompleteAddress.fromPrivateKey(await keyPair.getPrivateKey());

await rpcServer.registerSigner(await keyPair.getPrivateKey(), completeAddress);
expect(await db.getAccount(completeAddress.address)).toEqual(completeAddress);
await rpcServer.registerAccount(await keyPair.getPrivateKey(), completeAddress);
const accounts = await rpcServer.getAccounts();
const recipients = await rpcServer.getRecipients();
expect(accounts).toEqual([completeAddress]);
expect(recipients).toEqual([]);
});

it('registers a recipient and returns it as a recipient only and not as an account', async () => {
const completeAddress = await CompleteAddress.random();

await rpcServer.registerRecipient(completeAddress);
const accounts = await rpcServer.getAccounts();
const recipients = await rpcServer.getRecipients();
expect(accounts).toEqual([]);
expect(recipients).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
58 changes: 41 additions & 17 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,19 +81,55 @@ 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)) {
// throw new Error(`Public key mismatch: ${pubKey.toString()} != ${account.publicKey.toString()}`);
// }
await this.db.addAccount(account);
await this.db.addCompleteAddress(account);
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[]> {
// Get complete addresses of both the recipients and the accounts
const addresses = await this.db.getCompleteAddresses();
// Filter out the addresses not corresponding to accounts
const accountPubKeys = await this.keyStore.getAccounts();
const accounts = addresses.filter(address => accountPubKeys.find(pubKey => pubKey.equals(address.publicKey)));
return accounts;
}

public async getAccount(address: AztecAddress): Promise<CompleteAddress> {
const result = await this.getAccounts();
const account = result.find(r => r.address.equals(address));
if (!account) {
throw new Error(`Unable to get complete address for address ${address.toString()}`);
}
return Promise.resolve(account);
}

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

public async getRecipients(): Promise<CompleteAddress[]> {
// Get complete addresses of both the recipients and the accounts
const addresses = await this.db.getCompleteAddresses();
// Filter out the addresses corresponding to accounts
const accountPubKeys = await this.keyStore.getAccounts();
const recipients = addresses.filter(address => !accountPubKeys.find(pubKey => pubKey.equals(address.publicKey)));
return recipients;
}

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

public async addContracts(contracts: DeployedContract[]) {
Expand All @@ -106,18 +142,6 @@ export class AztecRPCServer implements AztecRPC {
}
}

public async getAccounts(): Promise<CompleteAddress[]> {
return await this.db.getAccounts();
}

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 getPublicStorageAt(contract: AztecAddress, storageSlot: Fr) {
if ((await this.getContractData(contract)) === undefined) {
throw new Error(`Contract ${contract.toString()} is not deployed`);
Expand Down
16 changes: 8 additions & 8 deletions yarn-project/aztec-rpc/src/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,22 @@ export interface Database extends ContractDatabase {
setTreeRoots(roots: Record<MerkleTreeId, Fr>): Promise<void>;

/**
* Adds account to the database.
* @param account - The account to add.
* Adds complete address to the database.
* @param address - The complete address to add.
* @returns Empty promise.
*/
addAccount(account: CompleteAddress): Promise<void>;
addCompleteAddress(address: CompleteAddress): Promise<void>;

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

/**
* Retrieves the list of accounts added to this database
* Retrieves the list of complete address added to this database
* @returns A promise that resolves to an array of AztecAddress instances.
*/
getAccounts(): Promise<CompleteAddress[]>;
getCompleteAddresses(): Promise<CompleteAddress[]>;
}
6 changes: 3 additions & 3 deletions yarn-project/aztec-rpc/src/database/memory_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class MemoryDB extends MemoryContractDatabase implements Database {
return Promise.resolve();
}

addAccount(account: CompleteAddress): Promise<void> {
addCompleteAddress(account: CompleteAddress): Promise<void> {
const accountIndex = this.accounts.findIndex(r => r.address.equals(account.address));
if (accountIndex !== -1) {
throw new Error(`Account ${account.address.toString()} already exists in memory database`);
Expand All @@ -102,12 +102,12 @@ export class MemoryDB extends MemoryContractDatabase implements Database {
return Promise.resolve();
}

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

getAccounts(): Promise<CompleteAddress[]> {
getCompleteAddresses(): Promise<CompleteAddress[]> {
return Promise.resolve(this.accounts);
}
}
6 changes: 3 additions & 3 deletions yarn-project/aztec-rpc/src/simulator_oracle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export class SimulatorOracle implements DBOracle {
return this.keyStore.getAccountPrivateKey(pubKey);
}

async getAccount(address: AztecAddress): Promise<CompleteAddress> {
const completeAddress = await this.db.getAccount(address);
async getCompleteAddress(address: AztecAddress): Promise<CompleteAddress> {
const completeAddress = await this.db.getCompleteAddress(address);
if (!completeAddress)
throw new Error(
`Unknown public key for address ${address.toString()}. Add public key to Aztec RPC server by calling server.addAccount(...)`,
`Unknown complete address for address ${address.toString()}. Add the information to Aztec RPC server by calling server.registerRecipient(...) or server.registerAccount(...)`,
);
return completeAddress;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('Synchroniser', () => {
const privateKey = PrivateKey.random();
keyStore.addAccount(privateKey);
const completeAddress = await CompleteAddress.fromPrivateKey(privateKey);
await database.addAccount(completeAddress);
await database.addCompleteAddress(completeAddress);

// Add the account which will add the note processor to the synchroniser
synchroniser.addAccount(completeAddress.publicKey, keyStore);
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export class Synchroniser {
* retrieved information from contracts might be old/stale (e.g. old token balance).
*/
public async isAccountStateSynchronised(account: AztecAddress) {
const completeAddress = await this.db.getAccount(account);
const completeAddress = await this.db.getCompleteAddress(account);
if (!completeAddress) {
return false;
}
Expand Down
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
10 changes: 8 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 All @@ -43,6 +43,12 @@ export abstract class BaseWallet implements Wallet {
getAccount(address: AztecAddress): Promise<CompleteAddress | undefined> {
return this.rpc.getAccount(address);
}
getRecipients(): Promise<CompleteAddress[]> {
return this.rpc.getRecipients();
}
getRecipient(address: AztecAddress): Promise<CompleteAddress | undefined> {
return this.rpc.getRecipient(address);
}
addContracts(contracts: DeployedContract[]): Promise<void> {
return this.rpc.addContracts(contracts);
}
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
Loading

0 comments on commit d15bfcf

Please sign in to comment.