@aztec/key-store 0.0.1-commit.d431d1c → 0.0.1-commit.e310a4c8
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/dest/key_store.d.ts +1 -1
- package/dest/key_store.d.ts.map +1 -1
- package/dest/key_store.js +31 -21
- package/package.json +5 -5
- package/src/key_store.ts +36 -23
package/dest/key_store.d.ts
CHANGED
|
@@ -97,4 +97,4 @@ export declare class KeyStore {
|
|
|
97
97
|
*/
|
|
98
98
|
getKeyPrefixAndAccount(value: Bufferable): Promise<[KeyPrefix, AztecAddress]>;
|
|
99
99
|
}
|
|
100
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
100
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoia2V5X3N0b3JlLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMva2V5X3N0b3JlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVBLE9BQU8sRUFBRSxFQUFFLEVBQUUsTUFBTSxnQ0FBZ0MsQ0FBQztBQUNwRCxPQUFPLEVBQUUsY0FBYyxFQUFTLE1BQU0sbUNBQW1DLENBQUM7QUFFMUUsT0FBTyxFQUFFLEtBQUssVUFBVSxFQUFxQixNQUFNLDZCQUE2QixDQUFDO0FBQ2pGLE9BQU8sS0FBSyxFQUFFLGlCQUFpQixFQUFpQixNQUFNLGlCQUFpQixDQUFDO0FBQ3hFLE9BQU8sRUFBRSxZQUFZLEVBQUUsTUFBTSw2QkFBNkIsQ0FBQztBQUMzRCxPQUFPLEVBQUUsZUFBZSxFQUFFLEtBQUssY0FBYyxFQUFFLE1BQU0sd0JBQXdCLENBQUM7QUFDOUUsT0FBTyxFQUFFLG9CQUFvQixFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFDNUQsT0FBTyxFQUVMLEtBQUssU0FBUyxFQUNkLEtBQUssU0FBUyxFQUlmLE1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TyxDQUFDLGNBQWMsQ0FBQyxDQVE3RjtJQUVEOzs7Ozs7T0FNRztJQUNVLDhCQUE4QixDQUFDLE9BQU8sRUFBRSxZQUFZLEVBQUUsR0FBRyxFQUFFLFlBQVksR0FBRyxPQUFPLENBQUMsRUFBRSxDQUFDLENBYWpHO0lBRUQ7Ozs7OztPQU1HO0lBQ1Usa0JBQWtCLENBQUMsR0FBRyxFQUFFLFNBQVMsR0FBRyxPQUFPLENBQUMsY0FBYyxDQUFDLENBa0J2RTtJQUVEOzs7OztPQUtHO0lBQ1Usc0JBQXNCLENBQUMsS0FBSyxFQUFFLFVBQVUsR0FBRyxPQUFPLENBQUMsQ0FBQyxTQUFTLEVBQUUsWUFBWSxDQUFDLENBQUMsQ0FjekY7Q0FDRiJ9
|
package/dest/key_store.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key_store.d.ts","sourceRoot":"","sources":["../src/key_store.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,EAAE,EAAE,MAAM,gCAAgC,CAAC;AACpD,OAAO,EAAE,cAAc,EAAS,MAAM,mCAAmC,CAAC;AAE1E,OAAO,EAAE,KAAK,UAAU,EAAqB,MAAM,6BAA6B,CAAC;AACjF,OAAO,KAAK,EAAE,iBAAiB,EAAiB,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,KAAK,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC9E,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAEL,KAAK,SAAS,EACd,KAAK,SAAS,EAIf,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"key_store.d.ts","sourceRoot":"","sources":["../src/key_store.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,EAAE,EAAE,MAAM,gCAAgC,CAAC;AACpD,OAAO,EAAE,cAAc,EA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;IACU,iCAAiC,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,cAAc,CAAC,CAQ7F;IAED;;;;;;OAMG;IACU,8BAA8B,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,EAAE,YAAY,GAAG,OAAO,CAAC,EAAE,CAAC,CAajG;IAED;;;;;;OAMG;IACU,kBAAkB,CAAC,GAAG,EAAE,SAAS,GAAG,OAAO,CAAC,cAAc,CAAC,CAkBvE;IAED;;;;;OAKG;IACU,sBAAsB,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC,CAczF;CACF"}
|
package/dest/key_store.js
CHANGED
|
@@ -8,12 +8,17 @@ import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
|
8
8
|
import { CompleteAddress } from '@aztec/stdlib/contract';
|
|
9
9
|
import { KeyValidationRequest } from '@aztec/stdlib/kernel';
|
|
10
10
|
import { KEY_PREFIXES, computeAppSecretKey, deriveKeys, derivePublicKeyFromSecretKey } from '@aztec/stdlib/keys';
|
|
11
|
+
/** Maps a key prefix to the storage suffix for the corresponding master secret key. */ function secretKeyStorageSuffix(prefix) {
|
|
12
|
+
return prefix === 'n' ? 'nhk_m' : `${prefix}sk_m`;
|
|
13
|
+
}
|
|
11
14
|
/**
|
|
12
15
|
* Used for managing keys. Can hold keys of multiple accounts.
|
|
13
16
|
*/ export class KeyStore {
|
|
14
17
|
static SCHEMA_VERSION = 1;
|
|
18
|
+
#db;
|
|
15
19
|
#keys;
|
|
16
20
|
constructor(database){
|
|
21
|
+
this.#db = database;
|
|
17
22
|
this.#keys = database.openMap('key_store');
|
|
18
23
|
}
|
|
19
24
|
/**
|
|
@@ -30,28 +35,31 @@ import { KEY_PREFIXES, computeAppSecretKey, deriveKeys, derivePublicKeyFromSecre
|
|
|
30
35
|
* @param partialAddress - The partial address of the account.
|
|
31
36
|
* @returns The account's complete address.
|
|
32
37
|
*/ async addAccount(sk, partialAddress) {
|
|
33
|
-
const {
|
|
38
|
+
const { masterNullifierHidingKey, masterIncomingViewingSecretKey, masterOutgoingViewingSecretKey, masterTaggingSecretKey, publicKeys } = await deriveKeys(sk);
|
|
34
39
|
const completeAddress = await CompleteAddress.fromSecretKeyAndPartialAddress(sk, partialAddress);
|
|
35
40
|
const { address: account } = completeAddress;
|
|
36
|
-
//
|
|
37
|
-
await this.#keys.set(`${account.toString()}-ivsk_m`, masterIncomingViewingSecretKey.toBuffer());
|
|
38
|
-
await this.#keys.set(`${account.toString()}-ovsk_m`, masterOutgoingViewingSecretKey.toBuffer());
|
|
39
|
-
await this.#keys.set(`${account.toString()}-tsk_m`, masterTaggingSecretKey.toBuffer());
|
|
40
|
-
await this.#keys.set(`${account.toString()}-nsk_m`, masterNullifierSecretKey.toBuffer());
|
|
41
|
-
await this.#keys.set(`${account.toString()}-npk_m`, publicKeys.masterNullifierPublicKey.toBuffer());
|
|
42
|
-
await this.#keys.set(`${account.toString()}-ivpk_m`, publicKeys.masterIncomingViewingPublicKey.toBuffer());
|
|
43
|
-
await this.#keys.set(`${account.toString()}-ovpk_m`, publicKeys.masterOutgoingViewingPublicKey.toBuffer());
|
|
44
|
-
await this.#keys.set(`${account.toString()}-tpk_m`, publicKeys.masterTaggingPublicKey.toBuffer());
|
|
45
|
-
// We store pk_m_hash under `account-{n/iv/ov/t}pk_m_hash` key to be able to obtain address and key prefix
|
|
46
|
-
// using the #getKeyPrefixAndAccount function later on
|
|
41
|
+
// Compute hashes before transaction
|
|
47
42
|
const masterNullifierPublicKeyHash = await publicKeys.masterNullifierPublicKey.hash();
|
|
48
|
-
await this.#keys.set(`${account.toString()}-npk_m_hash`, masterNullifierPublicKeyHash.toBuffer());
|
|
49
43
|
const masterIncomingViewingPublicKeyHash = await publicKeys.masterIncomingViewingPublicKey.hash();
|
|
50
|
-
await this.#keys.set(`${account.toString()}-ivpk_m_hash`, masterIncomingViewingPublicKeyHash.toBuffer());
|
|
51
44
|
const masterOutgoingViewingPublicKeyHash = await publicKeys.masterOutgoingViewingPublicKey.hash();
|
|
52
|
-
await this.#keys.set(`${account.toString()}-ovpk_m_hash`, masterOutgoingViewingPublicKeyHash.toBuffer());
|
|
53
45
|
const masterTaggingPublicKeyHash = await publicKeys.masterTaggingPublicKey.hash();
|
|
54
|
-
await this.#
|
|
46
|
+
await this.#db.transactionAsync(async ()=>{
|
|
47
|
+
// Naming of keys is as follows ${account}-${n/iv/ov/t}${sk/pk}_m
|
|
48
|
+
await this.#keys.set(`${account.toString()}-ivsk_m`, masterIncomingViewingSecretKey.toBuffer());
|
|
49
|
+
await this.#keys.set(`${account.toString()}-ovsk_m`, masterOutgoingViewingSecretKey.toBuffer());
|
|
50
|
+
await this.#keys.set(`${account.toString()}-tsk_m`, masterTaggingSecretKey.toBuffer());
|
|
51
|
+
await this.#keys.set(`${account.toString()}-nhk_m`, masterNullifierHidingKey.toBuffer());
|
|
52
|
+
await this.#keys.set(`${account.toString()}-npk_m`, publicKeys.masterNullifierPublicKey.toBuffer());
|
|
53
|
+
await this.#keys.set(`${account.toString()}-ivpk_m`, publicKeys.masterIncomingViewingPublicKey.toBuffer());
|
|
54
|
+
await this.#keys.set(`${account.toString()}-ovpk_m`, publicKeys.masterOutgoingViewingPublicKey.toBuffer());
|
|
55
|
+
await this.#keys.set(`${account.toString()}-tpk_m`, publicKeys.masterTaggingPublicKey.toBuffer());
|
|
56
|
+
// We store pk_m_hash under `account-{n/iv/ov/t}pk_m_hash` key to be able to obtain address and key prefix
|
|
57
|
+
// using the #getKeyPrefixAndAccount function later on
|
|
58
|
+
await this.#keys.set(`${account.toString()}-npk_m_hash`, masterNullifierPublicKeyHash.toBuffer());
|
|
59
|
+
await this.#keys.set(`${account.toString()}-ivpk_m_hash`, masterIncomingViewingPublicKeyHash.toBuffer());
|
|
60
|
+
await this.#keys.set(`${account.toString()}-ovpk_m_hash`, masterOutgoingViewingPublicKeyHash.toBuffer());
|
|
61
|
+
await this.#keys.set(`${account.toString()}-tpk_m_hash`, masterTaggingPublicKeyHash.toBuffer());
|
|
62
|
+
});
|
|
55
63
|
// At last, we return the newly derived account address
|
|
56
64
|
return completeAddress;
|
|
57
65
|
}
|
|
@@ -83,9 +91,10 @@ import { KEY_PREFIXES, computeAppSecretKey, deriveKeys, derivePublicKeyFromSecre
|
|
|
83
91
|
throw new Error(`Could not find ${keyPrefix}pkM for ${keyPrefix}pk_m_hash ${pkMHash.toString()}.`);
|
|
84
92
|
}
|
|
85
93
|
// Now we find the secret key for the public key
|
|
86
|
-
const
|
|
94
|
+
const skStorageSuffix = secretKeyStorageSuffix(keyPrefix);
|
|
95
|
+
const skMBuffer = await this.#keys.getAsync(`${account.toString()}-${skStorageSuffix}`);
|
|
87
96
|
if (!skMBuffer) {
|
|
88
|
-
throw new Error(`Could not find ${
|
|
97
|
+
throw new Error(`Could not find ${skStorageSuffix} for account ${account.toString()} whose address was successfully obtained with ${keyPrefix}pk_m_hash ${pkMHash.toString()}.`);
|
|
89
98
|
}
|
|
90
99
|
const skM = GrumpkinScalar.fromBuffer(skMBuffer);
|
|
91
100
|
// We sanity check that it's possible to derive the public key from the secret key
|
|
@@ -183,14 +192,15 @@ import { KEY_PREFIXES, computeAppSecretKey, deriveKeys, derivePublicKeyFromSecre
|
|
|
183
192
|
* @dev Used when feeding the sk_m to the kernel circuit for keys verification.
|
|
184
193
|
*/ async getMasterSecretKey(pkM) {
|
|
185
194
|
const [keyPrefix, account] = await this.getKeyPrefixAndAccount(pkM);
|
|
186
|
-
const
|
|
195
|
+
const skStorageSuffix = secretKeyStorageSuffix(keyPrefix);
|
|
196
|
+
const secretKeyBuffer = await this.#keys.getAsync(`${account.toString()}-${skStorageSuffix}`);
|
|
187
197
|
if (!secretKeyBuffer) {
|
|
188
|
-
throw new Error(`Could not find ${
|
|
198
|
+
throw new Error(`Could not find ${skStorageSuffix} for ${keyPrefix}pk_m ${pkM.toString()}. This should not happen.`);
|
|
189
199
|
}
|
|
190
200
|
const skM = GrumpkinScalar.fromBuffer(secretKeyBuffer);
|
|
191
201
|
const derivedpkM = await derivePublicKeyFromSecretKey(skM);
|
|
192
202
|
if (!derivedpkM.equals(pkM)) {
|
|
193
|
-
throw new Error(`Could not find ${
|
|
203
|
+
throw new Error(`Could not find ${skStorageSuffix} for ${keyPrefix}pkM ${pkM.toString()} in secret keys buffer.`);
|
|
194
204
|
}
|
|
195
205
|
return Promise.resolve(skM);
|
|
196
206
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/key-store",
|
|
3
|
-
"version": "0.0.1-commit.
|
|
3
|
+
"version": "0.0.1-commit.e310a4c8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": "./dest/index.js",
|
|
6
6
|
"typedocOptions": {
|
|
@@ -57,10 +57,10 @@
|
|
|
57
57
|
]
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@aztec/constants": "0.0.1-commit.
|
|
61
|
-
"@aztec/foundation": "0.0.1-commit.
|
|
62
|
-
"@aztec/kv-store": "0.0.1-commit.
|
|
63
|
-
"@aztec/stdlib": "0.0.1-commit.
|
|
60
|
+
"@aztec/constants": "0.0.1-commit.e310a4c8",
|
|
61
|
+
"@aztec/foundation": "0.0.1-commit.e310a4c8",
|
|
62
|
+
"@aztec/kv-store": "0.0.1-commit.e310a4c8",
|
|
63
|
+
"@aztec/stdlib": "0.0.1-commit.e310a4c8",
|
|
64
64
|
"tslib": "^2.4.0"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
package/src/key_store.ts
CHANGED
|
@@ -17,14 +17,21 @@ import {
|
|
|
17
17
|
derivePublicKeyFromSecretKey,
|
|
18
18
|
} from '@aztec/stdlib/keys';
|
|
19
19
|
|
|
20
|
+
/** Maps a key prefix to the storage suffix for the corresponding master secret key. */
|
|
21
|
+
function secretKeyStorageSuffix(prefix: KeyPrefix): string {
|
|
22
|
+
return prefix === 'n' ? 'nhk_m' : `${prefix}sk_m`;
|
|
23
|
+
}
|
|
24
|
+
|
|
20
25
|
/**
|
|
21
26
|
* Used for managing keys. Can hold keys of multiple accounts.
|
|
22
27
|
*/
|
|
23
28
|
export class KeyStore {
|
|
24
29
|
public static readonly SCHEMA_VERSION = 1;
|
|
30
|
+
#db: AztecAsyncKVStore;
|
|
25
31
|
#keys: AztecAsyncMap<string, Buffer>;
|
|
26
32
|
|
|
27
33
|
constructor(database: AztecAsyncKVStore) {
|
|
34
|
+
this.#db = database;
|
|
28
35
|
this.#keys = database.openMap('key_store');
|
|
29
36
|
}
|
|
30
37
|
|
|
@@ -46,7 +53,7 @@ export class KeyStore {
|
|
|
46
53
|
*/
|
|
47
54
|
public async addAccount(sk: Fr, partialAddress: PartialAddress): Promise<CompleteAddress> {
|
|
48
55
|
const {
|
|
49
|
-
|
|
56
|
+
masterNullifierHidingKey,
|
|
50
57
|
masterIncomingViewingSecretKey,
|
|
51
58
|
masterOutgoingViewingSecretKey,
|
|
52
59
|
masterTaggingSecretKey,
|
|
@@ -56,27 +63,31 @@ export class KeyStore {
|
|
|
56
63
|
const completeAddress = await CompleteAddress.fromSecretKeyAndPartialAddress(sk, partialAddress);
|
|
57
64
|
const { address: account } = completeAddress;
|
|
58
65
|
|
|
59
|
-
//
|
|
60
|
-
await this.#keys.set(`${account.toString()}-ivsk_m`, masterIncomingViewingSecretKey.toBuffer());
|
|
61
|
-
await this.#keys.set(`${account.toString()}-ovsk_m`, masterOutgoingViewingSecretKey.toBuffer());
|
|
62
|
-
await this.#keys.set(`${account.toString()}-tsk_m`, masterTaggingSecretKey.toBuffer());
|
|
63
|
-
await this.#keys.set(`${account.toString()}-nsk_m`, masterNullifierSecretKey.toBuffer());
|
|
64
|
-
|
|
65
|
-
await this.#keys.set(`${account.toString()}-npk_m`, publicKeys.masterNullifierPublicKey.toBuffer());
|
|
66
|
-
await this.#keys.set(`${account.toString()}-ivpk_m`, publicKeys.masterIncomingViewingPublicKey.toBuffer());
|
|
67
|
-
await this.#keys.set(`${account.toString()}-ovpk_m`, publicKeys.masterOutgoingViewingPublicKey.toBuffer());
|
|
68
|
-
await this.#keys.set(`${account.toString()}-tpk_m`, publicKeys.masterTaggingPublicKey.toBuffer());
|
|
69
|
-
|
|
70
|
-
// We store pk_m_hash under `account-{n/iv/ov/t}pk_m_hash` key to be able to obtain address and key prefix
|
|
71
|
-
// using the #getKeyPrefixAndAccount function later on
|
|
66
|
+
// Compute hashes before transaction
|
|
72
67
|
const masterNullifierPublicKeyHash = await publicKeys.masterNullifierPublicKey.hash();
|
|
73
|
-
await this.#keys.set(`${account.toString()}-npk_m_hash`, masterNullifierPublicKeyHash.toBuffer());
|
|
74
68
|
const masterIncomingViewingPublicKeyHash = await publicKeys.masterIncomingViewingPublicKey.hash();
|
|
75
|
-
await this.#keys.set(`${account.toString()}-ivpk_m_hash`, masterIncomingViewingPublicKeyHash.toBuffer());
|
|
76
69
|
const masterOutgoingViewingPublicKeyHash = await publicKeys.masterOutgoingViewingPublicKey.hash();
|
|
77
|
-
await this.#keys.set(`${account.toString()}-ovpk_m_hash`, masterOutgoingViewingPublicKeyHash.toBuffer());
|
|
78
70
|
const masterTaggingPublicKeyHash = await publicKeys.masterTaggingPublicKey.hash();
|
|
79
|
-
|
|
71
|
+
|
|
72
|
+
await this.#db.transactionAsync(async () => {
|
|
73
|
+
// Naming of keys is as follows ${account}-${n/iv/ov/t}${sk/pk}_m
|
|
74
|
+
await this.#keys.set(`${account.toString()}-ivsk_m`, masterIncomingViewingSecretKey.toBuffer());
|
|
75
|
+
await this.#keys.set(`${account.toString()}-ovsk_m`, masterOutgoingViewingSecretKey.toBuffer());
|
|
76
|
+
await this.#keys.set(`${account.toString()}-tsk_m`, masterTaggingSecretKey.toBuffer());
|
|
77
|
+
await this.#keys.set(`${account.toString()}-nhk_m`, masterNullifierHidingKey.toBuffer());
|
|
78
|
+
|
|
79
|
+
await this.#keys.set(`${account.toString()}-npk_m`, publicKeys.masterNullifierPublicKey.toBuffer());
|
|
80
|
+
await this.#keys.set(`${account.toString()}-ivpk_m`, publicKeys.masterIncomingViewingPublicKey.toBuffer());
|
|
81
|
+
await this.#keys.set(`${account.toString()}-ovpk_m`, publicKeys.masterOutgoingViewingPublicKey.toBuffer());
|
|
82
|
+
await this.#keys.set(`${account.toString()}-tpk_m`, publicKeys.masterTaggingPublicKey.toBuffer());
|
|
83
|
+
|
|
84
|
+
// We store pk_m_hash under `account-{n/iv/ov/t}pk_m_hash` key to be able to obtain address and key prefix
|
|
85
|
+
// using the #getKeyPrefixAndAccount function later on
|
|
86
|
+
await this.#keys.set(`${account.toString()}-npk_m_hash`, masterNullifierPublicKeyHash.toBuffer());
|
|
87
|
+
await this.#keys.set(`${account.toString()}-ivpk_m_hash`, masterIncomingViewingPublicKeyHash.toBuffer());
|
|
88
|
+
await this.#keys.set(`${account.toString()}-ovpk_m_hash`, masterOutgoingViewingPublicKeyHash.toBuffer());
|
|
89
|
+
await this.#keys.set(`${account.toString()}-tpk_m_hash`, masterTaggingPublicKeyHash.toBuffer());
|
|
90
|
+
});
|
|
80
91
|
|
|
81
92
|
// At last, we return the newly derived account address
|
|
82
93
|
return completeAddress;
|
|
@@ -118,10 +129,11 @@ export class KeyStore {
|
|
|
118
129
|
}
|
|
119
130
|
|
|
120
131
|
// Now we find the secret key for the public key
|
|
121
|
-
const
|
|
132
|
+
const skStorageSuffix = secretKeyStorageSuffix(keyPrefix);
|
|
133
|
+
const skMBuffer = await this.#keys.getAsync(`${account.toString()}-${skStorageSuffix}`);
|
|
122
134
|
if (!skMBuffer) {
|
|
123
135
|
throw new Error(
|
|
124
|
-
`Could not find ${
|
|
136
|
+
`Could not find ${skStorageSuffix} for account ${account.toString()} whose address was successfully obtained with ${keyPrefix}pk_m_hash ${pkMHash.toString()}.`,
|
|
125
137
|
);
|
|
126
138
|
}
|
|
127
139
|
|
|
@@ -251,17 +263,18 @@ export class KeyStore {
|
|
|
251
263
|
public async getMasterSecretKey(pkM: PublicKey): Promise<GrumpkinScalar> {
|
|
252
264
|
const [keyPrefix, account] = await this.getKeyPrefixAndAccount(pkM);
|
|
253
265
|
|
|
254
|
-
const
|
|
266
|
+
const skStorageSuffix = secretKeyStorageSuffix(keyPrefix);
|
|
267
|
+
const secretKeyBuffer = await this.#keys.getAsync(`${account.toString()}-${skStorageSuffix}`);
|
|
255
268
|
if (!secretKeyBuffer) {
|
|
256
269
|
throw new Error(
|
|
257
|
-
`Could not find ${
|
|
270
|
+
`Could not find ${skStorageSuffix} for ${keyPrefix}pk_m ${pkM.toString()}. This should not happen.`,
|
|
258
271
|
);
|
|
259
272
|
}
|
|
260
273
|
|
|
261
274
|
const skM = GrumpkinScalar.fromBuffer(secretKeyBuffer);
|
|
262
275
|
const derivedpkM = await derivePublicKeyFromSecretKey(skM);
|
|
263
276
|
if (!derivedpkM.equals(pkM)) {
|
|
264
|
-
throw new Error(`Could not find ${
|
|
277
|
+
throw new Error(`Could not find ${skStorageSuffix} for ${keyPrefix}pkM ${pkM.toString()} in secret keys buffer.`);
|
|
265
278
|
}
|
|
266
279
|
|
|
267
280
|
return Promise.resolve(skM);
|