diff --git a/yarn-project/circuit-types/src/keys/index.ts b/yarn-project/circuit-types/src/keys/index.ts index c770f6798143..be16aca18e40 100644 --- a/yarn-project/circuit-types/src/keys/index.ts +++ b/yarn-project/circuit-types/src/keys/index.ts @@ -1,3 +1,3 @@ export * from './key_pair.js'; export * from './key_store.js'; -export * from './new_key_store.js'; \ No newline at end of file +export * from './new_key_store.js'; diff --git a/yarn-project/circuit-types/src/keys/new_key_store.ts b/yarn-project/circuit-types/src/keys/new_key_store.ts index faaf018b833a..6ae9337cc18d 100644 --- a/yarn-project/circuit-types/src/keys/new_key_store.ts +++ b/yarn-project/circuit-types/src/keys/new_key_store.ts @@ -1,9 +1,23 @@ -import { type AztecAddress, Fr, PartialAddress, type PublicKey } from '@aztec/circuits.js'; +import { type AztecAddress, type Fr, type PartialAddress, type PublicKey } from '@aztec/circuits.js'; /** * Represents a secure storage for managing keys. */ export interface NewKeyStore { + /** + * Creates a new account from a randomly generated secret key. + * @returns A promise that resolves to the newly created account's AztecAddress. + */ + createAccount(): Promise; + + /** + * Adds an account to the key store from the provided secret key. + * @param sk - The secret key of the account. + * @param partialAddress - The partial address of the account. + * @returns The account's address. + */ + addAccount(sk: Fr, partialAddress: PartialAddress): Promise; + /** * Gets the master nullifier public key for a given account. * @throws If the account does not exist in the key store. diff --git a/yarn-project/key-store/src/new_test_key_store.test.ts b/yarn-project/key-store/src/new_test_key_store.test.ts new file mode 100644 index 000000000000..6bc488e27642 --- /dev/null +++ b/yarn-project/key-store/src/new_test_key_store.test.ts @@ -0,0 +1,18 @@ +import { Fr } from "@aztec/circuits.js"; +import { NewTestKeyStore } from "./new_test_key_store.js"; +import { openTmpStore } from "@aztec/kv-store/utils"; +import { Grumpkin } from "@aztec/circuits.js/barretenberg"; + +describe('NewTestKeyStore', () => { + it('Adds account and returns keys', () => { + const db = openTmpStore(); + const keyStore = new NewTestKeyStore(new Grumpkin(), db); + + // Arbitrary fixed values + const sk = new Fr(8923n); + const partialAddress = new Fr(243523n); + + const accountAddress = keyStore.addAccount(sk, partialAddress); + expect(accountAddress).toMatchInlineSnapshot(`"0071f7630d28ce02cc1ca8b15c44953f84a39e1478445395247ae04dfa213c0e"`); + }); +}); diff --git a/yarn-project/key-store/src/new_test_key_store.ts b/yarn-project/key-store/src/new_test_key_store.ts index 784410316f98..63a87773541a 100644 --- a/yarn-project/key-store/src/new_test_key_store.ts +++ b/yarn-project/key-store/src/new_test_key_store.ts @@ -1,5 +1,5 @@ import { type NewKeyStore, type PublicKey } from '@aztec/circuit-types'; -import { AztecAddress, Fq, type Fr, GeneratorIndex, type PartialAddress, Point } from '@aztec/circuits.js'; +import { AztecAddress, Fq, Fr, GeneratorIndex, type PartialAddress, Point } from '@aztec/circuits.js'; import { type Grumpkin } from '@aztec/circuits.js/barretenberg'; import { poseidonHash } from '@aztec/foundation/crypto'; import { type AztecKVStore, type AztecMap } from '@aztec/kv-store'; @@ -16,14 +16,30 @@ export class NewTestKeyStore implements NewKeyStore { this.#keys = database.openMap('key_store'); } + /** + * Creates a new account from a randomly generated secret key. + * @returns A promise that resolves to the newly created account's AztecAddress. + */ + public createAccount(): Promise { + const sk = Fr.random(); + const partialAddress = Fr.random(); + return this.addAccount(sk, partialAddress); + } + + /** + * Adds an account to the key store from the provided secret key. + * @param sk - The secret key of the account. + * @param partialAddress - The partial address of the account. + * @returns The account's address. + */ public async addAccount(sk: Fr, partialAddress: PartialAddress): Promise { - // First we derive the master secret keys + // First we derive master secret keys const masterNullifierSecretKey = poseidonHash([sk], GeneratorIndex.NSK_M); const masterIncomingViewingSecretKey = poseidonHash([sk], GeneratorIndex.IVSK_M); const masterOutgoingViewingSecretKey = poseidonHash([sk], GeneratorIndex.OVSK_M); const masterTaggingSecretKey = poseidonHash([sk], GeneratorIndex.TSK_M); - // Then we derive the master public keys + // Then we derive master public keys // TODO: Is converting from Fr to Fq bellow an issue? Fr.MODULUS is < Fq.MODULUS so it shouldn't but should we refactor this anyway? const masterNullifierPublicKey = this.curve.mul( this.curve.generator(),