-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (34 loc) · 1.51 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
bip39 = require('bip39') // npm i -S bip39
ethUtil = require('ethereumjs-util')
hdkey = require('ethereumjs-wallet/hdkey')
const getAccount = (mnemonic, accountNumber) =>
{
var seed = bip39.mnemonicToSeed(mnemonic)
const root = hdkey.fromMasterSeed(seed)
const masterPrivateKey = root._hdkey._privateKey.toString('hex')
const addrNode = root.derivePath("m/44'/60'/0'/0/" + accountNumber.toString())
const privKey = "0x" + addrNode._hdkey._privateKey.toString('hex')
const pubKey = ethUtil.privateToPublic(addrNode._hdkey._privateKey)
const addr = "0x" + ethUtil.publicToAddress(pubKey).toString('hex')
return {address: addr, privateKey: privKey.toString('hex')}
}
const getAccounts = (mnemonic, startIndex, endIndex) =>
{
var seed = bip39.mnemonicToSeed(mnemonic)
const root = hdkey.fromMasterSeed(seed)
const masterPrivateKey = root._hdkey._privateKey.toString('hex')
var addressPrivateKeyPairs = []
for(var i = startIndex; i <= endIndex; i++)
{
const addrNode = root.derivePath("m/44'/60'/0'/0/" + i.toString())
const privKey = "0x" + addrNode._hdkey._privateKey.toString('hex')
const pubKey = ethUtil.privateToPublic(addrNode._hdkey._privateKey)
const addr = "0x" + ethUtil.publicToAddress(pubKey).toString('hex')
addressPrivateKeyPairs.push([addr, privKey.toString('hex')])
}
return addressPrivateKeyPairs;
}
module.exports = {
getAccount: getAccount,
getAccounts: getAccounts
}