@@ -48,7 +48,8 @@ var networks = {
48
48
"167.114.29.53:4002" ,
49
49
"167.114.29.54:4002" ,
50
50
"167.114.29.55:4002"
51
- ]
51
+ ] ,
52
+ ledgerpath : "44'/1'/"
52
53
} ,
53
54
mainnet : {
54
55
nethash : "6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988" ,
@@ -99,7 +100,8 @@ var networks = {
99
100
"193.70.72.88:4001" ,
100
101
"193.70.72.89:4001" ,
101
102
"193.70.72.90:4001"
102
- ]
103
+ ] ,
104
+ ledgerpath : "44'/111'/"
103
105
}
104
106
} ;
105
107
@@ -157,35 +159,39 @@ function postTransaction(container, transaction, cb){
157
159
158
160
let senderAddress = arkjs . crypto . getAddress ( transaction . senderPublicKey ) ;
159
161
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 ( ) ;
184
190
} ) ;
185
191
}
186
192
187
193
function getFromNode ( url , cb ) {
188
- nethash = network ?network . nethash :"" ;
194
+ let nethash = network ?network . nethash :"" ;
189
195
request (
190
196
{
191
197
url : url ,
@@ -253,14 +259,23 @@ function getAccount(container, seriesCb) {
253
259
}
254
260
}
255
261
262
+ function resetLedger ( ) {
263
+ ledgerAccounts = [ ] ;
264
+ ledgerBridge = null ;
265
+ if ( ledgerComm !== null ) {
266
+ ledgerComm . close_async ( ) ;
267
+ ledgerComm = null ;
268
+ }
269
+ }
270
+
256
271
async function populateLedgerAccounts ( ) {
257
272
if ( ! ledgerSupported || ! ledgerBridge ) {
258
273
return ;
259
274
}
260
275
ledgerAccounts = [ ] ;
261
276
var accounts = [ ] ;
262
277
var account_index = 0 ;
263
- var path = "44'/111'/" ;
278
+ var path = network . hasOwnProperty ( 'ledgerpath' ) ? network . ledgerpath : "44'/111'/" ;
264
279
var empty = false ;
265
280
266
281
while ( ! empty ) {
@@ -288,11 +303,22 @@ async function populateLedgerAccounts() {
288
303
( body ) => { accountData = body }
289
304
) ;
290
305
if ( ! accountData || accountData . success === false ) {
306
+ // Add an empty available account when 0 transactions have been made.
291
307
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
+ } ;
293
319
} else {
294
320
result . accountData = accountData . account ;
295
- }
321
+ }
296
322
}
297
323
} catch ( e ) {
298
324
console . log ( 'no request:' , e ) ;
@@ -345,28 +371,32 @@ async function ledgerSignTransaction(seriesCb, transaction, account, callback) {
345
371
}
346
372
347
373
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
+ } ) ;
365
388
}
366
389
367
390
vorpal
368
391
. command ( 'connect <network>' , 'Connect to network. Network is devnet or mainnet' )
369
392
. action ( function ( args , callback ) {
393
+ // reset an existing connection first
394
+ if ( server ) {
395
+ server = null ;
396
+ network = null ;
397
+ resetLedger ( ) ;
398
+ }
399
+
370
400
var self = this ;
371
401
network = networks [ args . network ] ;
372
402
@@ -408,6 +438,13 @@ function connect2network(n, callback){
408
438
vorpal
409
439
. command ( 'connect node <url>' , 'Connect to a server. For example "connect node 5.39.9.251:4000"' )
410
440
. action ( function ( args , callback ) {
441
+ // reset an existing connection first
442
+ if ( server ) {
443
+ server = null ;
444
+ network = null ;
445
+ resetLedger ( ) ;
446
+ }
447
+
411
448
var self = this ;
412
449
server = args . url ;
413
450
getFromNode ( 'http://' + server + '/api/blocks/getNethash' , function ( err , response , body ) {
@@ -458,6 +495,7 @@ vorpal
458
495
self . delimiter ( 'ark>' ) ;
459
496
server = null ;
460
497
network = null ;
498
+ resetLedger ( ) ;
461
499
callback ( ) ;
462
500
} ) ;
463
501
@@ -653,7 +691,11 @@ vorpal
653
691
} , function ( result ) {
654
692
if ( result . continue ) {
655
693
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
+ }
657
699
ledgerSignTransaction ( seriesCb , unvoteTransaction , account , function ( unvoteTransaction ) {
658
700
if ( ! unvoteTransaction ) {
659
701
return seriesCb ( 'Failed to sign unvote transaction with ledger' ) ;
@@ -674,7 +716,11 @@ vorpal
674
716
return seriesCb ( 'Failed to fetch unconfirmed transaction: ' + body . error ) ;
675
717
} else if ( body . transaction ) {
676
718
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
+ }
678
724
ledgerSignTransaction ( seriesCb , transaction , account , function ( transaction ) {
679
725
if ( ! transaction ) {
680
726
return seriesCb ( 'Failed to sign vote transaction with ledger' ) ;
@@ -687,7 +733,11 @@ vorpal
687
733
} ) ;
688
734
} ) ;
689
735
} 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
+ }
691
741
ledgerSignTransaction ( seriesCb , transaction , account , function ( transaction ) {
692
742
if ( ! transaction ) {
693
743
return seriesCb ( 'Failed to sign transaction with ledger' ) ;
@@ -770,7 +820,11 @@ vorpal
770
820
message : 'Removing last vote for ' + lastDelegate . username ,
771
821
} , function ( result ) {
772
822
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
+ }
774
828
ledgerSignTransaction ( seriesCb , transaction , account , function ( transaction ) {
775
829
if ( ! transaction ) {
776
830
return seriesCb ( 'Failed to sign transaction with ledger' ) ;
@@ -880,7 +934,11 @@ vorpal
880
934
message : 'Sending ' + arkAmountString + ' ' + network . config . token + ' ' + ( currency ?'(' + currency + args . amount + ') ' :'' ) + 'to ' + args . address + ' now' ,
881
935
} , function ( result ) {
882
936
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
+ }
884
942
ledgerSignTransaction ( seriesCb , transaction , account , function ( transaction ) {
885
943
if ( ! transaction ) {
886
944
return seriesCb ( 'Failed to sign transaction with ledger' ) ;
@@ -944,7 +1002,11 @@ vorpal
944
1002
} else {
945
1003
return seriesCb ( 'No public key for account' ) ;
946
1004
}
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
+ }
948
1010
ledgerSignTransaction ( seriesCb , transaction , account , function ( transaction ) {
949
1011
if ( ! transaction ) {
950
1012
return seriesCb ( 'Failed to sign transaction with ledger' ) ;
0 commit comments