This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
/
keystore.ts
178 lines (161 loc) · 4.24 KB
/
keystore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import aes from 'aes-js';
import hashjs from 'hash.js';
import { pbkdf2Sync } from 'pbkdf2';
import scrypt from 'scrypt.js';
import uuid from 'uuid';
import { bytes } from '@zilliqa-js/util';
import { randomBytes } from './random';
import {
KeystoreV3,
KDF,
KDFParams,
PBKDF2Params,
ScryptParams,
} from './types';
import { getAddressFromPrivateKey } from './util';
const ALGO_IDENTIFIER = 'aes-128-ctr';
/**
* getDerivedKey
*
* NOTE: only scrypt and pbkdf2 are supported.
*
* @param {Buffer} key - the passphrase
* @param {KDF} kdf - the key derivation function to be used
* @param {KDFParams} params - params for the kdf
*
* @returns {Promise<Buffer>}
*/
async function getDerivedKey(
key: Buffer,
kdf: KDF,
params: KDFParams,
): Promise<Buffer> {
const salt = Buffer.from(params.salt, 'hex');
if (kdf === 'pbkdf2') {
const { c, dklen } = params as PBKDF2Params;
return pbkdf2Sync(key, salt, c, dklen, 'sha256');
}
if (kdf === 'scrypt') {
const { n, r, p, dklen } = params as ScryptParams;
return scrypt(key, salt, n, r, p, dklen);
}
throw new Error('Only pbkdf2 and scrypt are supported');
}
/**
* encryptPrivateKey
*
* Encodes and encrypts an account in the format specified by
* https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition.
* However, note that, in keeping with the hash function used by Zilliqa's
* core protocol, the MAC is generated using sha256 instead of keccak.
*
* NOTE: only scrypt and pbkdf2 are supported.
*
* @param {KDF} kdf - the key derivation function to be used
* @param {string} privateKey - hex-encoded private key
* @param {string} passphrase - a passphrase used for encryption
*
* @returns {Promise<string>}
*/
export const encryptPrivateKey = async (
kdf: KDF,
privateKey: string,
passphrase: string,
): Promise<string> => {
const address = getAddressFromPrivateKey(privateKey);
const salt = randomBytes(32);
const iv = Buffer.from(randomBytes(16), 'hex');
const kdfparams = {
salt,
n: 8192,
c: 262144,
r: 8,
p: 1,
dklen: 32,
};
const derivedKey = await getDerivedKey(
Buffer.from(passphrase),
kdf,
kdfparams,
);
const cipher = new aes.ModeOfOperation.ctr(
derivedKey.slice(0, 16),
new aes.Counter(iv),
);
const ciphertext = Buffer.from(
cipher.encrypt(Buffer.from(privateKey, 'hex')),
);
return JSON.stringify({
address,
crypto: {
cipher: ALGO_IDENTIFIER,
cipherparams: {
iv: iv.toString('hex'),
},
ciphertext: ciphertext.toString('hex'),
kdf,
kdfparams,
mac: hashjs
// @ts-ignore
.hmac(hashjs.sha256, derivedKey, 'hex')
.update(
Buffer.concat([
derivedKey.slice(16, 32),
ciphertext,
iv,
Buffer.from(ALGO_IDENTIFIER),
]),
'hex',
)
.digest('hex'),
},
id: uuid.v4({ random: bytes.hexToIntArray(randomBytes(16)) }),
version: 3,
});
};
/**
* decryptPrivateKey
*
* Recovers the private key from a keystore file using the given passphrase.
*
* @param {string} passphrase
* @param {KeystoreV3} keystore
* @returns {Promise<string>}
*/
export const decryptPrivateKey = async (
passphrase: string,
keystore: KeystoreV3,
): Promise<string> => {
const ciphertext = Buffer.from(keystore.crypto.ciphertext, 'hex');
const iv = Buffer.from(keystore.crypto.cipherparams.iv, 'hex');
const kdfparams = keystore.crypto.kdfparams;
const derivedKey = await getDerivedKey(
Buffer.from(passphrase),
keystore.crypto.kdf,
kdfparams,
);
const mac = hashjs
// @ts-ignore
.hmac(hashjs.sha256, derivedKey, 'hex')
.update(
Buffer.concat([
derivedKey.slice(16, 32),
ciphertext,
iv,
Buffer.from(ALGO_IDENTIFIER),
]),
'hex',
)
.digest('hex');
// we need to do a byte-by-byte comparison to avoid non-constant time side
// channel attacks.
if (!bytes.isEqual(mac.toUpperCase(), keystore.crypto.mac.toUpperCase())) {
return Promise.reject('Failed to decrypt.');
}
const cipher = new aes.ModeOfOperation.ctr(
derivedKey.slice(0, 16),
new aes.Counter(iv),
);
return Buffer.from(cipher.decrypt(ciphertext)).toString('hex');
};
export { uuid };