Skip to content

Commit

Permalink
chore: quick keystore refactor (#9355)
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 authored Oct 24, 2024
1 parent 1711fac commit 31b9999
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions yarn-project/key-store/src/key_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
KeyValidationRequest,
type PartialAddress,
Point,
computeAddress,
computeAppSecretKey,
deriveKeys,
derivePublicKeyFromSecretKey,
Expand Down Expand Up @@ -54,8 +53,8 @@ export class KeyStore {
publicKeys,
} = deriveKeys(sk);

const publicKeysHash = publicKeys.hash();
const account = computeAddress(publicKeysHash, partialAddress);
const completeAddress = CompleteAddress.fromSecretKeyAndPartialAddress(sk, partialAddress);
const { address: account } = completeAddress;

// Naming of keys is as follows ${account}-${n/iv/ov/t}${sk/pk}_m
await this.#keys.set(`${account.toString()}-ivsk_m`, masterIncomingViewingSecretKey.toBuffer());
Expand All @@ -82,7 +81,7 @@ export class KeyStore {
await this.#keys.set(`${account.toString()}-tpk_m_hash`, publicKeys.masterTaggingPublicKey.hash().toBuffer());

// At last, we return the newly derived account address
return Promise.resolve(new CompleteAddress(account, publicKeys, partialAddress));
return Promise.resolve(completeAddress);
}

/**
Expand All @@ -104,7 +103,7 @@ export class KeyStore {
* @returns The key validation request.
*/
public getKeyValidationRequest(pkMHash: Fr, contractAddress: AztecAddress): Promise<KeyValidationRequest> {
const [keyPrefix, account] = this.#getKeyPrefixAndAccount(pkMHash);
const [keyPrefix, account] = this.getKeyPrefixAndAccount(pkMHash);

// Now we find the master public key for the account
const pkMBuffer = this.#keys.get(`${account.toString()}-${keyPrefix}pk_m`);
Expand Down Expand Up @@ -141,6 +140,22 @@ export class KeyStore {
return Promise.resolve(new KeyValidationRequest(pkM, skApp));
}

/**
* Gets the master nullifier public key for a given account.
* @throws If the account does not exist in the key store.
* @param account - The account address for which to retrieve the master nullifier public key.
* @returns The master nullifier public key for the account.
*/
public async getMasterNullifierPublicKey(account: AztecAddress): Promise<PublicKey> {
const masterNullifierPublicKeyBuffer = this.#keys.get(`${account.toString()}-npk_m`);
if (!masterNullifierPublicKeyBuffer) {
throw new Error(
`Account ${account.toString()} does not exist. Registered accounts: ${await this.getAccounts()}.`,
);
}
return Promise.resolve(Point.fromBuffer(masterNullifierPublicKeyBuffer));
}

/**
* Gets the master incoming viewing public key for a given account.
* @throws If the account does not exist in the key store.
Expand Down Expand Up @@ -245,7 +260,7 @@ export class KeyStore {
* @dev Used when feeding the sk_m to the kernel circuit for keys verification.
*/
public getMasterSecretKey(pkM: PublicKey): Promise<GrumpkinScalar> {
const [keyPrefix, account] = this.#getKeyPrefixAndAccount(pkM);
const [keyPrefix, account] = this.getKeyPrefixAndAccount(pkM);

const secretKeyBuffer = this.#keys.get(`${account.toString()}-${keyPrefix}sk_m`);
if (!secretKeyBuffer) {
Expand All @@ -268,7 +283,7 @@ export class KeyStore {
* @dev Note that this is quite inefficient but it should not matter because there should never be too many keys
* in the key store.
*/
#getKeyPrefixAndAccount(value: Bufferable): [KeyPrefix, AztecAddress] {
public getKeyPrefixAndAccount(value: Bufferable): [KeyPrefix, AztecAddress] {
const valueBuffer = serializeToBuffer(value);
for (const [key, val] of this.#keys.entries()) {
if (val.equals(valueBuffer)) {
Expand Down

0 comments on commit 31b9999

Please sign in to comment.