-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmneumonic.js
119 lines (97 loc) · 3.22 KB
/
mneumonic.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
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
// Compatible with TronLink
const bip39 = require('bip39');
const { BIP32Factory } = require('bip32');
const ecc = require('tiny-secp256k1');
const TronWeb = require('tronweb');
const fs = require('fs');
const readline = require('readline');
const bip32 = BIP32Factory(ecc);
function hideInput(prompt) {
return new Promise(resolve => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.stdoutMuted = true;
rl.question(prompt, (value) => {
rl.history = rl.history.slice(1);
rl.close();
resolve(value);
});
rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (rl.stdoutMuted)
rl.output.write("*");
else
rl.output.write(stringToWrite);
};
});
}
async function getValidWord(prompt, validWords) {
let word;
do {
word = await hideInput(prompt);
if (!validWords.includes(word)) {
console.log('\nInvalid word. Please try again.');
word = null;
}
} while (!word);
return word;
}
async function getMnemonic() {
const validWordCount = [12, 24];
const validWords = fs.readFileSync('bip39_english.txt', 'utf8').split('\n');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let wordCount;
do {
wordCount = await new Promise(resolve => {
rl.question('Enter number of words (12 or 24): ', answer => {
resolve(answer);
});
});
wordCount = parseInt(wordCount);
if (!validWordCount.includes(wordCount)) {
console.log('Invalid number of words. Please enter 12 or 24.');
}
} while (!validWordCount.includes(wordCount));
rl.close();
let mnemonic;
do {
mnemonic = '';
for (let i = 1; i <= wordCount; i++) {
const word = await getValidWord(`Enter word ${i}: `, validWords);
mnemonic += word + ' ';
}
mnemonic = mnemonic.trim();
if (!bip39.validateMnemonic(mnemonic)) {
console.log('Invalid mnemonic. Checksum failed. Please try again.');
}
} while (!bip39.validateMnemonic(mnemonic));
return mnemonic;
}
function getAccountAtIndex(mnemonic, index) {
const seed = bip39.mnemonicToSeedSync(mnemonic);
const node = bip32.fromSeed(seed);
const child = node.derivePath(`m/44'/195'/${index}'/0/0`);
const privateKey = child.privateKey.toString('hex');
const address = TronWeb.address.fromPrivateKey(privateKey);
return {
privateKey,
address
};
}
async function main() {
const args = process.argv.slice(2);
if (args.length > 1 || args.includes('--help') || (args.length === 1 && isNaN(parseInt(args[0])))) {
console.log('Usage: node mnemonic.js [accountIndex]');
console.log('accountIndex is optional and defaults to 0.');
process.exit(1);
}
const accountIndex = args.length === 1 ? parseInt(args[0]) : 0;
const mnemonic = await getMnemonic();
const account = getAccountAtIndex(mnemonic, accountIndex);
console.log(account);
}
main();