Skip to content
This repository was archived by the owner on Oct 8, 2019. It is now read-only.

Commit c9a75ca

Browse files
authored
Merge pull request #43 from wownmedia/fix-ledger-issues
Fix issue #24 Ledger first address and issue #42 reset Ledger when changing networks
2 parents 6ffdf0c + c720a1d commit c9a75ca

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

index.js

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ var networks = {
4242
"167.114.29.53:4002",
4343
"167.114.29.54:4002",
4444
"167.114.29.55:4002"
45-
]
45+
],
46+
ledgerpath: "44'/1'/"
4647
},
4748
mainnet: {
4849
nethash: "6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988",
@@ -93,7 +94,8 @@ var networks = {
9394
"193.70.72.88:4001",
9495
"193.70.72.89:4001",
9596
"193.70.72.90:4001"
96-
]
97+
],
98+
ledgerpath: "44'/111'/"
9799
}
98100
};
99101

@@ -179,7 +181,7 @@ function postTransaction(container, transaction, cb){
179181
}
180182

181183
function getFromNode(url, cb){
182-
nethash=network?network.nethash:"";
184+
let nethash=network?network.nethash:"";
183185
request(
184186
{
185187
url: url,
@@ -247,14 +249,23 @@ function getAccount(container, seriesCb) {
247249
}
248250
}
249251

252+
function resetLedger() {
253+
ledgerAccounts = [];
254+
ledgerBridge = null;
255+
if (ledgerComm !== null) {
256+
ledgerComm.close_async();
257+
ledgerComm = null;
258+
}
259+
}
260+
250261
async function populateLedgerAccounts() {
251262
if (!ledgerBridge) {
252263
return;
253264
}
254265
ledgerAccounts = [];
255266
var accounts = [];
256267
var account_index = 0;
257-
var path = "44'/111'/";
268+
var path = network.hasOwnProperty('ledgerpath') ? network.ledgerpath : "44'/111'/";
258269
var empty = false;
259270

260271
while (!empty) {
@@ -282,11 +293,22 @@ async function populateLedgerAccounts() {
282293
(body) => { accountData = body }
283294
);
284295
if (!accountData || accountData.success === false) {
296+
// Add an empty available account when 0 transactions have been made.
285297
empty = true;
286-
result = null;
298+
result.accountData = {
299+
address: result.address,
300+
unconfirmedBalance: "0",
301+
balance: "0",
302+
publicKey: result.publicKey,
303+
unconfirmedSignature: 0,
304+
secondSignature: 0,
305+
secondPublicKey: null,
306+
multisignatures: [],
307+
u_multisignatures: []
308+
};
287309
} else {
288310
result.accountData = accountData.account;
289-
}
311+
}
290312
}
291313
} catch (e) {
292314
console.log('no request:', e);
@@ -345,20 +367,24 @@ ledgerWorker.on('message', function (message) {
345367
ledgerBridge = new LedgerArk(ledgerComm);
346368
populateLedgerAccounts();
347369
}).fail((error) => {
348-
//console.log('ledger error: ', error);
370+
//vorpal.log(colors.red('ledger error: ' +error));
349371
});
350372
} else if (!message.connected && ledgerComm) {
351373
vorpal.log('Ledger App Disconnected');
352-
ledgerComm.close_async();
353-
ledgerComm = null;
354-
ledgerBridge = null;
355-
ledgerAccounts = [];
374+
resetLedger();
356375
};
357376
});
358377

359378
vorpal
360379
.command('connect <network>', 'Connect to network. Network is devnet or mainnet')
361380
.action(function(args, callback) {
381+
// reset an existing connection first
382+
if(server) {
383+
server=null;
384+
network=null;
385+
resetLedger();
386+
}
387+
362388
var self = this;
363389
network = networks[args.network];
364390

@@ -400,6 +426,13 @@ function connect2network(n, callback){
400426
vorpal
401427
.command('connect node <url>', 'Connect to a server. For example "connect node 5.39.9.251:4000"')
402428
.action(function(args, callback) {
429+
// reset an existing connection first
430+
if(server) {
431+
server=null;
432+
network=null;
433+
resetLedger();
434+
}
435+
403436
var self = this;
404437
server=args.url;
405438
getFromNode('http://'+server+'/api/blocks/getNethash', function(err, response, body){
@@ -450,6 +483,7 @@ vorpal
450483
self.delimiter('ark>');
451484
server=null;
452485
network=null;
486+
resetLedger();
453487
callback();
454488
});
455489

0 commit comments

Comments
 (0)