Skip to content

Commit

Permalink
feat: throwing when adding an account multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jul 26, 2023
1 parent abfe183 commit b57310f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ describe('AztecRpcServer', function () {

await expect(rpcServer.addAccount(privateKey, address, partialAddress)).rejects.toThrowError(/cannot be derived/);
});

it('cannot add the same account twice', async () => {
const keyPair = ConstantKeyPair.random(await Grumpkin.new());
const pubKey = keyPair.getPublicKey();
const partialAddress = Fr.random();
const address = computeContractAddressFromPartial(wasm, pubKey, partialAddress);

await rpcServer.addAccount(await keyPair.getPrivateKey(), address, partialAddress);
await expect(async () =>
rpcServer.addAccount(await keyPair.getPrivateKey(), address, partialAddress),
).rejects.toThrow(`Account ${address} already exists`);
});
});
3 changes: 3 additions & 0 deletions yarn-project/aztec-rpc/src/database/memory_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ export class MemoryDB extends MemoryContractDatabase implements Database {
publicKey: PublicKey,
partialAddress: PartialContractAddress,
): Promise<void> {
if (this.publicKeys.has(address.toBigInt())) {
throw new Error(`Account ${address} already exists`);
}
this.publicKeys.set(address.toBigInt(), [publicKey, partialAddress]);
return Promise.resolve();
}
Expand Down

0 comments on commit b57310f

Please sign in to comment.