@aztec/key-store 0.23.0 → 0.26.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/src/index.ts +2 -0
- package/src/key_pair.ts +48 -0
- package/src/test_key_store.ts +95 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/key-store",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": "./dest/index.js",
|
|
6
6
|
"typedocOptions": {
|
|
@@ -30,10 +30,10 @@
|
|
|
30
30
|
"rootDir": "./src"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@aztec/circuit-types": "0.
|
|
34
|
-
"@aztec/circuits.js": "0.
|
|
35
|
-
"@aztec/foundation": "0.
|
|
36
|
-
"@aztec/kv-store": "0.
|
|
33
|
+
"@aztec/circuit-types": "0.26.1",
|
|
34
|
+
"@aztec/circuits.js": "0.26.1",
|
|
35
|
+
"@aztec/foundation": "0.26.1",
|
|
36
|
+
"@aztec/kv-store": "0.26.1",
|
|
37
37
|
"tslib": "^2.4.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
package/src/index.ts
ADDED
package/src/key_pair.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { KeyPair, PublicKey } from '@aztec/circuit-types';
|
|
2
|
+
import { GrumpkinPrivateKey, GrumpkinScalar } from '@aztec/circuits.js';
|
|
3
|
+
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The ConstantKeyPair class is an implementation of the KeyPair interface, which allows generation and management of
|
|
7
|
+
* a constant public and private key pair. It provides methods for creating a random instance of the key pair,
|
|
8
|
+
* retrieving the public key, getting the private key. This class ensures the persistence and consistency of
|
|
9
|
+
* the generated keys, making it suitable for cryptographic operations where constant key pairs are required.
|
|
10
|
+
*/
|
|
11
|
+
export class ConstantKeyPair implements KeyPair {
|
|
12
|
+
/**
|
|
13
|
+
* Generate a random ConstantKeyPair instance using the .
|
|
14
|
+
* The random private key is generated using 32 random bytes, and the corresponding public key is calculated
|
|
15
|
+
* by multiplying the Grumpkin generator point with the private key. This function provides an efficient
|
|
16
|
+
* way of generating unique key pairs for cryptographic purposes.
|
|
17
|
+
*
|
|
18
|
+
* @param curve - The curve used for elliptic curve cryptography operations.
|
|
19
|
+
* @returns A randomly generated ConstantKeyPair instance.
|
|
20
|
+
*/
|
|
21
|
+
public static random(curve: Grumpkin) {
|
|
22
|
+
const privateKey = GrumpkinScalar.random();
|
|
23
|
+
const publicKey = curve.mul(curve.generator(), privateKey);
|
|
24
|
+
return new ConstantKeyPair(publicKey, privateKey);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Creates a new instance from a private key.
|
|
29
|
+
* @param curve - The curve used for elliptic curve cryptography operations.
|
|
30
|
+
* @param signer - The signer to be used on the account.
|
|
31
|
+
* @param privateKey - The private key.
|
|
32
|
+
* @returns A new instance.
|
|
33
|
+
*/
|
|
34
|
+
public static fromPrivateKey(curve: Grumpkin, privateKey: GrumpkinPrivateKey) {
|
|
35
|
+
const publicKey = curve.mul(curve.generator(), privateKey);
|
|
36
|
+
return new ConstantKeyPair(publicKey, privateKey);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
constructor(private publicKey: PublicKey, private privateKey: GrumpkinPrivateKey) {}
|
|
40
|
+
|
|
41
|
+
public getPublicKey(): PublicKey {
|
|
42
|
+
return this.publicKey;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public getPrivateKey(): GrumpkinPrivateKey {
|
|
46
|
+
return this.privateKey;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { KeyPair, KeyStore, PublicKey } from '@aztec/circuit-types';
|
|
2
|
+
import {
|
|
3
|
+
AztecAddress,
|
|
4
|
+
GrumpkinPrivateKey,
|
|
5
|
+
GrumpkinScalar,
|
|
6
|
+
Point,
|
|
7
|
+
computeNullifierSecretKey,
|
|
8
|
+
computeSiloedNullifierSecretKey,
|
|
9
|
+
derivePublicKey,
|
|
10
|
+
} from '@aztec/circuits.js';
|
|
11
|
+
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
|
|
12
|
+
import { AztecKVStore, AztecMap } from '@aztec/kv-store';
|
|
13
|
+
|
|
14
|
+
import { ConstantKeyPair } from './key_pair.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* TestKeyStore is an implementation of the KeyStore interface, used for managing key pairs in a testing environment.
|
|
18
|
+
* It should be utilized in testing scenarios where secure key management is not required, and ease-of-use is prioritized.
|
|
19
|
+
*/
|
|
20
|
+
export class TestKeyStore implements KeyStore {
|
|
21
|
+
#keys: AztecMap<string, Buffer>;
|
|
22
|
+
|
|
23
|
+
constructor(private curve: Grumpkin, database: AztecKVStore) {
|
|
24
|
+
this.#keys = database.openMap('key_store');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public async addAccount(privKey: GrumpkinPrivateKey): Promise<PublicKey> {
|
|
28
|
+
const keyPair = ConstantKeyPair.fromPrivateKey(this.curve, privKey);
|
|
29
|
+
await this.#keys.setIfNotExists(keyPair.getPublicKey().toString(), keyPair.getPrivateKey().toBuffer());
|
|
30
|
+
return keyPair.getPublicKey();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public async createAccount(): Promise<PublicKey> {
|
|
34
|
+
const keyPair = ConstantKeyPair.random(this.curve);
|
|
35
|
+
await this.#keys.set(keyPair.getPublicKey().toString(), keyPair.getPrivateKey().toBuffer());
|
|
36
|
+
return keyPair.getPublicKey();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public getAccounts(): Promise<PublicKey[]> {
|
|
40
|
+
const range = Array.from(this.#keys.keys());
|
|
41
|
+
return Promise.resolve(range.map(key => Point.fromString(key)));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public getAccountPrivateKey(pubKey: PublicKey): Promise<GrumpkinPrivateKey> {
|
|
45
|
+
const account = this.getAccount(pubKey);
|
|
46
|
+
return Promise.resolve(account.getPrivateKey());
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public async getNullifierSecretKey(pubKey: PublicKey) {
|
|
50
|
+
const privateKey = await this.getAccountPrivateKey(pubKey);
|
|
51
|
+
return computeNullifierSecretKey(privateKey);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public async getNullifierSecretKeyFromPublicKey(nullifierPubKey: PublicKey) {
|
|
55
|
+
const accounts = await this.getAccounts();
|
|
56
|
+
for (let i = 0; i < accounts.length; ++i) {
|
|
57
|
+
const accountPublicKey = accounts[i];
|
|
58
|
+
const privateKey = await this.getAccountPrivateKey(accountPublicKey);
|
|
59
|
+
const secretKey = computeNullifierSecretKey(privateKey);
|
|
60
|
+
const publicKey = derivePublicKey(secretKey);
|
|
61
|
+
if (publicKey.equals(nullifierPubKey)) {
|
|
62
|
+
return secretKey;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
throw new Error('Unknown nullifier public key.');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public async getNullifierPublicKey(pubKey: PublicKey) {
|
|
69
|
+
const secretKey = await this.getNullifierSecretKey(pubKey);
|
|
70
|
+
return derivePublicKey(secretKey);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public async getSiloedNullifierSecretKey(pubKey: PublicKey, contractAddress: AztecAddress) {
|
|
74
|
+
const secretKey = await this.getNullifierSecretKey(pubKey);
|
|
75
|
+
return computeSiloedNullifierSecretKey(secretKey, contractAddress);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Retrieve the KeyPair object associated with a given pub key.
|
|
80
|
+
* Searches through the 'accounts' array for a matching public key and returns the corresponding account (KeyPair).
|
|
81
|
+
* Throws an error if no matching account is found in the 'accounts'.
|
|
82
|
+
*
|
|
83
|
+
* @param pubKey - The public key of the account to retrieve.
|
|
84
|
+
* @returns The KeyPair object associated with the provided key.
|
|
85
|
+
*/
|
|
86
|
+
private getAccount(pubKey: PublicKey): KeyPair {
|
|
87
|
+
const privKey = this.#keys.get(pubKey.toString());
|
|
88
|
+
if (!privKey) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
'Unknown account.\nSee docs for context: https://docs.aztec.network/developers/debugging/aztecnr-errors#could-not-process-note-because-of-error-unknown-account-skipping-note',
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
return ConstantKeyPair.fromPrivateKey(this.curve, GrumpkinScalar.fromBuffer(privKey));
|
|
94
|
+
}
|
|
95
|
+
}
|