|
1 | 1 | 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'; |
3 | 11 | import { type Grumpkin } from '@aztec/circuits.js/barretenberg';
|
4 | 12 | import { poseidon2Hash, sha512ToGrumpkinScalar } from '@aztec/foundation/crypto';
|
5 | 13 | import { type AztecKVStore, type AztecMap } from '@aztec/kv-store';
|
@@ -75,6 +83,17 @@ export class NewTestKeyStore implements NewKeyStore {
|
75 | 83 | return Promise.resolve(accountAddress);
|
76 | 84 | }
|
77 | 85 |
|
| 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 | + |
78 | 97 | /**
|
79 | 98 | * Gets the master nullifier public key for a given account.
|
80 | 99 | * @throws If the account does not exist in the key store.
|
@@ -197,4 +216,30 @@ export class NewTestKeyStore implements NewKeyStore {
|
197 | 216 | ]),
|
198 | 217 | );
|
199 | 218 | }
|
| 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 | + } |
200 | 245 | }
|
0 commit comments