forked from CryptoAeon/zil-ledger-node-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
80 lines (67 loc) · 1.94 KB
/
main.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
const readline = require('readline');
const chalk = require('chalk');
const choices = [
"Download and install the Ledger app",
"Install app.hex in current directory",
"Get the app version",
"Generate public key",
"Get public address",
"Sign transaction",
"Sign 256b hash",
"Exit"
];
function generateMenu(choices) {
let menu = "";
choices.forEach((c, i) => { menu += `${(i + 1)}) ${c}\n` });
menu += "> ";
return menu;
}
const menu = generateMenu(choices);
const handlers = require("./handlers");
// Main
async function promptUser() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log(chalk.yellow(`\nPlease enter a number between 1 and ${choices.length}:`));
const exitN = choices.length - 1;
return await new Promise((resolve) => {
rl.question(chalk.yellow(menu), async (answer) => {
if (isNaN(answer)) {
console.log(`Invalid number: ${answer}`);
}
const choiceIdx = Number(answer) - 1;
if (choiceIdx === exitN) {
return resolve(true);
}
if (handlers[choiceIdx]) {
try {
const r = await handlers[choiceIdx]();
console.log(chalk.blue(JSON.stringify(r, null, 2)));
}
catch (e) {
console.error("Encountered error: " + e.message);
}
}
rl.close();
return resolve(false);
});
});
}
async function main() {
console.log(chalk.green("Welcome to the Zilliqa Ledger Node App!"));
while (true) {
try {
const done = await promptUser();
if (done) {
console.log("Goodbye!");
process.exit(0);
}
}
catch(e) {
console.error(e.message);
}
}
}
main();