Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: quick keystore refactor #9355

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading