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

Commit 4f6f62e

Browse files
author
Marc Schot
authored
Merge branch 'master' into fix-USB-init-error-onserver-and-vm
2 parents 01d336b + e8ca065 commit 4f6f62e

File tree

1 file changed

+115
-53
lines changed

1 file changed

+115
-53
lines changed

index.js

Lines changed: 115 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ var networks = {
4848
"167.114.29.53:4002",
4949
"167.114.29.54:4002",
5050
"167.114.29.55:4002"
51-
]
51+
],
52+
ledgerpath: "44'/1'/"
5253
},
5354
mainnet: {
5455
nethash: "6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988",
@@ -99,7 +100,8 @@ var networks = {
99100
"193.70.72.88:4001",
100101
"193.70.72.89:4001",
101102
"193.70.72.90:4001"
102-
]
103+
],
104+
ledgerpath: "44'/111'/"
103105
}
104106
};
105107

@@ -157,35 +159,39 @@ function postTransaction(container, transaction, cb){
157159

158160
let senderAddress = arkjs.crypto.getAddress(transaction.senderPublicKey);
159161
getFromNode('http://' + server + '/api/accounts?address=' + senderAddress, function(err, response, body){
160-
if(!body) {
161-
performPost();
162-
} else {
163-
body = JSON.parse(body);
164-
if (body.account.secondSignature) {
165-
container.prompt({
166-
type: 'password',
167-
name: 'passphrase',
168-
message: 'Second passphrase: ',
169-
}, function(result) {
170-
if (result.passphrase) {
171-
var secondKeys = arkjs.crypto.getKeys(result.passphrase);
172-
arkjs.crypto.secondSign(transaction, secondKeys);
173-
transaction.id = arkjs.crypto.getId(transaction);
174-
performPost();
175-
} else {
176-
vorpal.log('No second passphrase given. Trying without.');
177-
performPost();
178-
}
179-
});
180-
} else {
181-
performPost();
182-
}
183-
}
162+
163+
if(!err && body) {
164+
try {
165+
body = JSON.parse(body);
166+
if ( !body.hasOwnProperty('success') || body.success === false) {
167+
// The account does not yet exist on the connected node.
168+
throw "Failed: " + body.error;
169+
}
170+
if (body.hasOwnProperty('account') && body.account.secondSignature) {
171+
container.prompt({
172+
type: 'password',
173+
name: 'passphrase',
174+
message: 'Second passphrase: ',
175+
}, function(result) {
176+
if (result.passphrase) {
177+
var secondKeys = arkjs.crypto.getKeys(result.passphrase);
178+
arkjs.crypto.secondSign(transaction, secondKeys);
179+
transaction.id = arkjs.crypto.getId(transaction);
180+
} else {
181+
vorpal.log('No second passphrase given. Trying without.');
182+
}
183+
});
184+
}
185+
} catch (error) {
186+
vorpal.log(colors.red(error));
187+
}
188+
} // if(body)
189+
performPost();
184190
});
185191
}
186192

187193
function getFromNode(url, cb){
188-
nethash=network?network.nethash:"";
194+
let nethash=network?network.nethash:"";
189195
request(
190196
{
191197
url: url,
@@ -253,14 +259,23 @@ function getAccount(container, seriesCb) {
253259
}
254260
}
255261

262+
function resetLedger() {
263+
ledgerAccounts = [];
264+
ledgerBridge = null;
265+
if (ledgerComm !== null) {
266+
ledgerComm.close_async();
267+
ledgerComm = null;
268+
}
269+
}
270+
256271
async function populateLedgerAccounts() {
257272
if (!ledgerSupported || !ledgerBridge) {
258273
return;
259274
}
260275
ledgerAccounts = [];
261276
var accounts = [];
262277
var account_index = 0;
263-
var path = "44'/111'/";
278+
var path = network.hasOwnProperty('ledgerpath') ? network.ledgerpath : "44'/111'/";
264279
var empty = false;
265280

266281
while (!empty) {
@@ -288,11 +303,22 @@ async function populateLedgerAccounts() {
288303
(body) => { accountData = body }
289304
);
290305
if (!accountData || accountData.success === false) {
306+
// Add an empty available account when 0 transactions have been made.
291307
empty = true;
292-
result = null;
308+
result.accountData = {
309+
address: result.address,
310+
unconfirmedBalance: "0",
311+
balance: "0",
312+
publicKey: result.publicKey,
313+
unconfirmedSignature: 0,
314+
secondSignature: 0,
315+
secondPublicKey: null,
316+
multisignatures: [],
317+
u_multisignatures: []
318+
};
293319
} else {
294320
result.accountData = accountData.account;
295-
}
321+
}
296322
}
297323
} catch (e) {
298324
console.log('no request:', e);
@@ -345,28 +371,32 @@ async function ledgerSignTransaction(seriesCb, transaction, account, callback) {
345371
}
346372

347373
if (ledgerSupported) {
348-
ledgerWorker.on('message', function (message) {
349-
if (message.connected && network && (!ledgerComm || !ledgerAccounts.length)) {
350-
ledger.comm_node.create_async().then((comm) => {
351-
ledgerComm = comm;
352-
ledgerBridge = new LedgerArk(ledgerComm);
353-
populateLedgerAccounts();
354-
}).fail((error) => {
355-
//console.log('ledger error: ', error);
356-
});
357-
} else if (!message.connected && ledgerComm) {
358-
vorpal.log('Ledger App Disconnected');
359-
ledgerComm.close_async();
360-
ledgerComm = null;
361-
ledgerBridge = null;
362-
ledgerAccounts = [];
363-
};
364-
});
374+
ledgerWorker.on('message', function (message) {
375+
if (message.connected && network && (!ledgerComm || !ledgerAccounts.length)) {
376+
ledger.comm_node.create_async().then((comm) => {
377+
ledgerComm = comm;
378+
ledgerBridge = new LedgerArk(ledgerComm);
379+
populateLedgerAccounts();
380+
}).fail((error) => {
381+
//vorpal.log(colors.red('ledger error: ' +error));
382+
});
383+
} else if (!message.connected && ledgerComm) {
384+
vorpal.log('Ledger App Disconnected');
385+
resetLedger();
386+
};
387+
});
365388
}
366389

367390
vorpal
368391
.command('connect <network>', 'Connect to network. Network is devnet or mainnet')
369392
.action(function(args, callback) {
393+
// reset an existing connection first
394+
if(server) {
395+
server=null;
396+
network=null;
397+
resetLedger();
398+
}
399+
370400
var self = this;
371401
network = networks[args.network];
372402

@@ -408,6 +438,13 @@ function connect2network(n, callback){
408438
vorpal
409439
.command('connect node <url>', 'Connect to a server. For example "connect node 5.39.9.251:4000"')
410440
.action(function(args, callback) {
441+
// reset an existing connection first
442+
if(server) {
443+
server=null;
444+
network=null;
445+
resetLedger();
446+
}
447+
411448
var self = this;
412449
server=args.url;
413450
getFromNode('http://'+server+'/api/blocks/getNethash', function(err, response, body){
@@ -458,6 +495,7 @@ vorpal
458495
self.delimiter('ark>');
459496
server=null;
460497
network=null;
498+
resetLedger();
461499
callback();
462500
});
463501

@@ -653,7 +691,11 @@ vorpal
653691
}, function(result){
654692
if (result.continue) {
655693
if (currentVote) {
656-
var unvoteTransaction = arkjs.vote.createVote(passphrase, ['-'+currentVote.publicKey]);
694+
try {
695+
var unvoteTransaction = arkjs.vote.createVote(passphrase, ['-'+currentVote.publicKey]);
696+
} catch (error) {
697+
return seriesCb('Failed: ' + error);
698+
}
657699
ledgerSignTransaction(seriesCb, unvoteTransaction, account, function(unvoteTransaction) {
658700
if (!unvoteTransaction) {
659701
return seriesCb('Failed to sign unvote transaction with ledger');
@@ -674,7 +716,11 @@ vorpal
674716
return seriesCb('Failed to fetch unconfirmed transaction: ' + body.error);
675717
} else if (body.transaction) {
676718
clearInterval(checkTransactionTimerId);
677-
var transaction = arkjs.vote.createVote(passphrase, ['+'+newDelegate.publicKey]);
719+
try {
720+
var transaction = arkjs.vote.createVote(passphrase, ['+'+newDelegate.publicKey]);
721+
} catch (error) {
722+
return seriesCb('Failed: ' + error);
723+
}
678724
ledgerSignTransaction(seriesCb, transaction, account, function(transaction) {
679725
if (!transaction) {
680726
return seriesCb('Failed to sign vote transaction with ledger');
@@ -687,7 +733,11 @@ vorpal
687733
});
688734
});
689735
} else {
690-
var transaction = arkjs.vote.createVote(passphrase, ['+'+newDelegate.publicKey]);
736+
try {
737+
var transaction = arkjs.vote.createVote(passphrase, ['+'+newDelegate.publicKey]);
738+
} catch (error) {
739+
return seriesCb('Failed: ' + error);
740+
}
691741
ledgerSignTransaction(seriesCb, transaction, account, function(transaction) {
692742
if (!transaction) {
693743
return seriesCb('Failed to sign transaction with ledger');
@@ -770,7 +820,11 @@ vorpal
770820
message: 'Removing last vote for ' + lastDelegate.username,
771821
}, function(result){
772822
if (result.continue) {
773-
var transaction = arkjs.vote.createVote(passphrase, delegates);
823+
try {
824+
var transaction = arkjs.vote.createVote(passphrase, delegates);
825+
} catch (error) {
826+
return seriesCb('Failed: ' + error);
827+
}
774828
ledgerSignTransaction(seriesCb, transaction, account, function(transaction) {
775829
if (!transaction) {
776830
return seriesCb('Failed to sign transaction with ledger');
@@ -880,7 +934,11 @@ vorpal
880934
message: 'Sending ' + arkAmountString + ' ' + network.config.token+' '+(currency?'('+currency+args.amount+') ':'')+'to '+args.address+' now',
881935
}, function(result){
882936
if (result.continue) {
883-
var transaction = arkjs.transaction.createTransaction(args.address, arkamount, null, passphrase);
937+
try {
938+
var transaction = arkjs.transaction.createTransaction(args.address, arkamount, null, passphrase);
939+
} catch (error) {
940+
return seriesCb('Failed: ' + error);
941+
}
884942
ledgerSignTransaction(seriesCb, transaction, account, function(transaction) {
885943
if (!transaction) {
886944
return seriesCb('Failed to sign transaction with ledger');
@@ -944,7 +1002,11 @@ vorpal
9441002
} else {
9451003
return seriesCb('No public key for account');
9461004
}
947-
var transaction = arkjs.delegate.createDelegate(passphrase, args.username);
1005+
try {
1006+
var transaction = arkjs.delegate.createDelegate(passphrase, args.username);
1007+
} catch (error) {
1008+
return seriesCb('Failed: ' + error);
1009+
}
9481010
ledgerSignTransaction(seriesCb, transaction, account, function(transaction) {
9491011
if (!transaction) {
9501012
return seriesCb('Failed to sign transaction with ledger');

0 commit comments

Comments
 (0)