Skip to content

Commit af49a29

Browse files
authored
feat: impl of missing functionality in new key store (#5750)
1 parent 48389d3 commit af49a29

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

noir/noir-repo/tooling/noir_js_backend_barretenberg/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
"ts-node": "^10.9.1",
5858
"typescript": "5.4.2"
5959
}
60-
}
60+
}

yarn-project/circuit-types/src/keys/new_key_store.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { type AztecAddress, type Fr, type PartialAddress, type PublicKey } from '@aztec/circuits.js';
1+
import {
2+
type AztecAddress,
3+
type Fr,
4+
type GrumpkinPrivateKey,
5+
type PartialAddress,
6+
type PublicKey,
7+
} from '@aztec/circuits.js';
28

39
/**
410
* Represents a secure storage for managing keys.
@@ -18,6 +24,12 @@ export interface NewKeyStore {
1824
*/
1925
addAccount(sk: Fr, partialAddress: PartialAddress): Promise<AztecAddress>;
2026

27+
/**
28+
* Retrieves addresses of accounts stored in the key store.
29+
* @returns A Promise that resolves to an array of account addresses.
30+
*/
31+
getAccounts(): Promise<AztecAddress[]>;
32+
2133
/**
2234
* Gets the master nullifier public key for a given account.
2335
* @throws If the account does not exist in the key store.
@@ -76,4 +88,14 @@ export interface NewKeyStore {
7688
* @returns A Promise that resolves to the application outgoing viewing secret key.
7789
*/
7890
getAppOutgoingViewingSecretKey(account: AztecAddress, app: AztecAddress): Promise<Fr>;
91+
92+
/**
93+
* Retrieves the master nullifier secret key (nsk_m) corresponding to the specified master nullifier public key
94+
* (Npk_m).
95+
* @throws If the provided public key is not associated with any of the registered accounts.
96+
* @param masterNullifierPublicKey - The master nullifier public key to get secret key for.
97+
* @returns A Promise that resolves to the master nullifier secret key.
98+
* @dev Used when feeding the master nullifier secret key to the kernel circuit for nullifier keys verification.
99+
*/
100+
getMasterNullifierSecretKeyForPublicKey(masterNullifierPublicKey: PublicKey): Promise<GrumpkinPrivateKey>;
79101
}

yarn-project/key-store/src/new_test_key_store.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,17 @@ describe('NewTestKeyStore', () => {
5555
expect(appOutgoingViewingSecretKey.toString()).toMatchInlineSnapshot(
5656
`"0x2639b26510f9d30b7e173d301b263b246b7a576186be1f44cd7c86bc06773f8a"`,
5757
);
58+
59+
// Returned accounts are as expected
60+
const accounts = await keyStore.getAccounts();
61+
expect(accounts.toString()).toMatchInlineSnapshot(
62+
`"0x0ba7834252d19c4f09d29303c269f303f40ae3d2043f921ed0bf8c0709926d4e"`,
63+
);
64+
65+
// Manages to find master nullifer secret key for pub key
66+
const masterNullifierSecretKey = await keyStore.getMasterNullifierSecretKeyForPublicKey(masterNullifierPublicKey);
67+
expect(masterNullifierSecretKey.toString()).toMatchInlineSnapshot(
68+
`"0x0fde74d5e504c73b58aad420dd72590fc6004571411e7f77c45378714195a52b"`,
69+
);
5870
});
5971
});

yarn-project/key-store/src/new_test_key_store.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { type NewKeyStore, type PublicKey } from '@aztec/circuit-types';
2-
import { AztecAddress, Fr, GeneratorIndex, GrumpkinScalar, type PartialAddress, Point } from '@aztec/circuits.js';
2+
import {
3+
AztecAddress,
4+
Fr,
5+
GeneratorIndex,
6+
type GrumpkinPrivateKey,
7+
GrumpkinScalar,
8+
type PartialAddress,
9+
Point,
10+
} from '@aztec/circuits.js';
311
import { type Grumpkin } from '@aztec/circuits.js/barretenberg';
412
import { poseidon2Hash, sha512ToGrumpkinScalar } from '@aztec/foundation/crypto';
513
import { type AztecKVStore, type AztecMap } from '@aztec/kv-store';
@@ -75,6 +83,17 @@ export class NewTestKeyStore implements NewKeyStore {
7583
return Promise.resolve(accountAddress);
7684
}
7785

86+
/**
87+
* Retrieves addresses of accounts stored in the key store.
88+
* @returns A Promise that resolves to an array of account addresses.
89+
*/
90+
public getAccounts(): Promise<AztecAddress[]> {
91+
const allMapKeys = Array.from(this.#keys.keys());
92+
// We return account addresses based on the map keys that end with '-nsk_m'
93+
const accounts = allMapKeys.filter(key => key.endsWith('-nsk_m')).map(key => key.split('-')[0]);
94+
return Promise.resolve(accounts.map(account => AztecAddress.fromString(account)));
95+
}
96+
7897
/**
7998
* Gets the master nullifier public key for a given account.
8099
* @throws If the account does not exist in the key store.
@@ -197,4 +216,30 @@ export class NewTestKeyStore implements NewKeyStore {
197216
]),
198217
);
199218
}
219+
220+
/**
221+
* Retrieves the master nullifier secret key (nsk_m) corresponding to the specified master nullifier public key
222+
* (Npk_m).
223+
* @throws If the provided public key is not associated with any of the registered accounts.
224+
* @param masterNullifierPublicKey - The master nullifier public key to get secret key for.
225+
* @returns A Promise that resolves to the master nullifier secret key.
226+
* @dev Used when feeding the master nullifier secret key to the kernel circuit for nullifier keys verification.
227+
*/
228+
public getMasterNullifierSecretKeyForPublicKey(masterNullifierPublicKey: PublicKey): Promise<GrumpkinPrivateKey> {
229+
// We iterate over the map keys to find the account address that corresponds to the provided public key
230+
for (const [key, value] of this.#keys.entries()) {
231+
if (value.equals(masterNullifierPublicKey.toBuffer())) {
232+
// We extract the account address from the map key
233+
const accountAddress = key.split('-')[0];
234+
// We fetch the secret key and return it
235+
const masterNullifierSecretKeyBuffer = this.#keys.get(`${accountAddress.toString()}-nsk_m`);
236+
if (!masterNullifierSecretKeyBuffer) {
237+
throw new Error(`Could not find master nullifier secret key for account ${accountAddress.toString()}`);
238+
}
239+
return Promise.resolve(GrumpkinScalar.fromBuffer(masterNullifierSecretKeyBuffer));
240+
}
241+
}
242+
243+
throw new Error(`Could not find master nullifier secret key for public key ${masterNullifierPublicKey.toString()}`);
244+
}
200245
}

0 commit comments

Comments
 (0)