-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
89 lines (68 loc) · 2.08 KB
/
index.js
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
const bip39 = require('bip39');
const hdkey = require('ethereumjs-wallet/hdkey');
const keythereum = require('keythereum');
exports.debug = false;
function getPrivateKeyFromMnemonic(mnemonic) {
const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(mnemonic));
const wallet_hdpath = "m/44'/60'/0'/0/";
const wallet = hdwallet.derivePath(wallet_hdpath + '0').getWallet();
const privateKey = wallet._privKey.toString('hex');
const account = '0x' + wallet.getAddress().toString('hex').toUpperCase();
if (exports.debug)
console.log({
mnemonic: mnemonic,
privateKey: privateKey,
account: account
});
return privateKey;
}
exports.getPrivateKeyFromMnemonic = getPrivateKeyFromMnemonic;
function getPrivateKeyFromRandom() {
const params = { keyBytes: 32, ivBytes: 16 };
const dk = keythereum.create(params);
const privateKey = dk.privateKey.toString('hex');
if (exports.debug)
console.log({
privateKey: privateKey,
salt: dk.salt.toString('hex'),
iv: dk.iv.toString('hex')
});
return privateKey;
}
exports.getPrivateKeyFromRandom = getPrivateKeyFromRandom;
function getPrivateKeyFromKeystore(keystore, password) {
const privateKeyBuf = keythereum.recover(password, keystore);
const privateKey = privateKeyBuf.toString('hex');
if (exports.debug)
console.log({
privateKey: privateKey,
password: password,
keystore: keystore
});
return privateKey;
}
exports.getPrivateKeyFromKeystore = getPrivateKeyFromKeystore;
function getKeystoreFromPrivateKey(privateKey, password) {
const params = { keyBytes: 32, ivBytes: 16 };
const dk = keythereum.create(params);
const options = {
kdf: 'pbkdf2',
cipher: 'aes-128-ctr',
kdfparams: {
c: 262144,
dklen: 32,
prf: 'hmac-sha256'
}
};
// const keystore = keythereum.dump(password, dk.privateKey, dk.salt, dk.iv, options);
const keystore = keythereum.dump(password, privateKey, dk.salt, dk.iv, options);
if (exports.debug) {
console.log({
privateKey: privateKey,
password: password,
keystore: keystore
});
}
return keystore;
}
exports.getKeystoreFromPrivateKey = getKeystoreFromPrivateKey;