-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtron.js
59 lines (48 loc) · 1.56 KB
/
tron.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
const bip39 = require('bip39')
const HDKey = require('hdkey')
const TronWeb = require('tronweb')
const EC = require('elliptic').ec
const getPubKeyFromPriKey = (priKeyBytes) => {
const ec = new EC('secp256k1');
const key = ec.keyFromPrivate(priKeyBytes, 'bytes');
const pubkey = key.getPublic();
const x = pubkey.x;
const y = pubkey.y;
let xHex = x.toString('hex');
while (xHex.length < 64) {
xHex = `0${xHex}`;
}
let yHex = y.toString('hex');
while (yHex.length < 64) {
yHex = `0${yHex}`;
}
return `04${xHex}${yHex}`;
}
async function revealAddress (words, offset, hdPath) {
try {
const seed = bip39.mnemonicToSeedSync(words)
const hdkey = HDKey.fromMasterSeed(Buffer.from(seed, 'hex'))
const result = []
for (let i = 0; i < offset; i++) {
const childkey = hdkey.derive(`${hdPath}/${i}`)
const pri = childkey.privateKey.toString('hex')
const tronweb = new TronWeb(
'http://127.0.0.1:9090',
'http://127.0.0.1:9090',
'http://127.0.0.1:9090',
pri,
)
const pub = getPubKeyFromPriKey(tronweb.utils.code.hexStr2byteArray(pri))
result.push({
index: i,
address: tronweb.address.fromPrivateKey(pri),
publicKey: pub.toUpperCase(),
privateKey: pri.toUpperCase()
})
}
return result
} catch (error) {
throw error
}
}
module.exports = revealAddress