Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Apr 12, 2024
1 parent e9da589 commit a2045e5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion yarn-project/circuit-types/src/keys/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './key_pair.js';
export * from './key_store.js';
export * from './new_key_store.js';
export * from './new_key_store.js';
16 changes: 15 additions & 1 deletion yarn-project/circuit-types/src/keys/new_key_store.ts
Original file line number Diff line number Diff line change
@@ -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<AztecAddress>;

/**
* 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<AztecAddress>;

/**
* Gets the master nullifier public key for a given account.
* @throws If the account does not exist in the key store.
Expand Down
18 changes: 18 additions & 0 deletions yarn-project/key-store/src/new_test_key_store.test.ts
Original file line number Diff line number Diff line change
@@ -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"`);
});
});
22 changes: 19 additions & 3 deletions yarn-project/key-store/src/new_test_key_store.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<AztecAddress> {
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<AztecAddress> {
// 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(),
Expand Down

0 comments on commit a2045e5

Please sign in to comment.