-
Notifications
You must be signed in to change notification settings - Fork 4
/
handlers.js
230 lines (206 loc) · 7.15 KB
/
handlers.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
require("babel-polyfill"); // side-effects...
const TransportNodeHid = require("@ledgerhq/hw-transport-node-hid").default;
const Z = require("./zil-ledger-js-interface").default;
const readline = require('readline');
const chalk = require('chalk');
const fs = require('fs');
const curl = require('curlrequest')
const ELF_URL = 'https://github.com/Zilliqa/ledger-app-zilliqa/releases/download/v0.4.2/app.hex';
function getReadlineInterface() {
return readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
}
async function open() {
// This might throw if device not connected via USB
const t = await TransportNodeHid.open("");
t.setDebugMode(true);
return t;
}
async function downloadInstallApp() {
const opts = {url: ELF_URL, timeout: 5, encoding: null};
return await new Promise( (resolve, reject) => {
curl.request(opts, function (err, hexFile) {
if (err) {
return reject(err);
}
fs.writeFile("./app.hex", hexFile, function (err) {
if (err) {
return reject(err);
}
console.info(chalk.blue("Downloaded Zilliqa hex file."));
console.info(chalk.blue("Ledger device will ask you for confirmations..."));
const proc = require("./installApp");
proc.on('exit', (exitCode) => {
if (exitCode !== 0) {
console.error(`Installation failed: ${exitCode}`);
reject(exitCode);
}
else {
console.info(chalk.blue(`Installation successful!`));
}
return resolve({exitCode});
});
});
});
});
}
async function installApp (){
return await new Promise( (resolve, reject) => {
require("./installApp");
const proc = require("./installApp");
proc.on('exit', (exitCode) => {
if (exitCode !== 0) {
console.error(`Installation failed: ${exitCode}`);
reject(exitCode);
}
else {
console.info(chalk.blue(`Installation successful!`));
}
return resolve({exitCode});
});
});
}
async function getAppVersion() {
const transport = await open();
if (transport instanceof Error) {
return transport.message
}
const zil = new Z(transport);
return await zil.getVersion().then(r => {
transport.close().catch(e => {
console.error(e.message)
}).then(() => {
return r
});
return r;
}).catch(e => { reject(e); });;
}
async function getPubKey() {
const transport = await open();
if (transport instanceof Error) {
return transport.message
}
const q = "> Enter the key index: ";
return await new Promise((resolve, reject) => {
getReadlineInterface().question(chalk.yellow(q), async (index) => {
if (isNaN(index)) {
console.error("Index should be an integer.");
return resolve("Bad input.");
}
const zil = new Z(transport);
return zil.getPublicKey(index).then(r => {
transport.close().catch(e => {
console.error(e.message);
}).then(() => {
return resolve(r)
});
return resolve(r);
}).catch(e => { reject(e); });
});
});
}
async function getPublicAddress() {
const transport = await open();
if (transport instanceof Error) {
return transport.message
}
const q = "> Enter the key index: ";
return await new Promise((resolve, reject) => {
getReadlineInterface().question(chalk.yellow(q), async (index) => {
if (isNaN(index)) {
console.error("Index should be an integer.");
return resolve("Bad input.");
}
const zil = new Z(transport);
return zil.getPublicAddress(index).then(r => {
transport.close().catch(e => {
console.error(e.message);
}).then(() => {
return resolve(r)
});
return resolve(r);
}).catch(e => { reject(e); });
});
});
}
async function signHash() {
const transport = await open();
const q = "> Enter the hash bytes: ";
return await new Promise((resolve, reject) => {
getReadlineInterface().question(chalk.yellow(q), async (hashStr) => {
if (typeof hashStr !== "string") {
console.error("hash should be a string.");
return reject("Bad input.");
}
const q2 = "> Enter the key index: ";
getReadlineInterface().question(chalk.yellow(q2), async (index) => {
if (isNaN(index)) {
console.error("Index should be an integer.");
return reject("Bad input.");
}
const zil = new Z(transport);
return zil.signHash(index, hashStr).then(r => {
transport.close().catch(e => {
console.error(e.message);
}).then(() => {
return resolve(r)
});
return resolve(r);
}).catch(e => { reject(e); });;
});
});
});
}
async function signTxn() {
const transport = await open();
if (transport instanceof Error) {
return transport.message
}
const q = "> Enter the path to the transaction JSON file: ";
return await new Promise((resolve, reject) => {
getReadlineInterface().question(chalk.yellow(q), async (txnJsonFile) => {
if (typeof txnJsonFile !== "string") {
console.error("Need to specify path to a JSON file.");
return resolve("Bad input.");
}
let txnParams;
try {
txnParams = JSON.parse(fs.readFileSync(txnJsonFile, 'utf8'));
}
catch (e) {
reject(e);
return;
}
const q2 = "> Enter the key index: ";
getReadlineInterface().question(chalk.yellow(q2), async (keyIndex) => {
if (isNaN(keyIndex)) {
console.error("Index should be an integer.");
return reject("Bad input.");
}
const zil = new Z(transport);
return zil.signTxn(keyIndex, txnParams).then(r => {
transport.close().catch(e => {
console.error(e.message);
}).then(() => {
return resolve(r)
});
return resolve(r);
}).catch(e => {
reject(e);
});
});
});
});
}
module.exports = [
downloadInstallApp,
installApp,
getAppVersion,
getPubKey,
getPublicAddress,
signTxn,
signHash
];