1
1
const utxoLib = require ( 'bitgo-utxo-lib' ) ;
2
+ const accountLib = require ( '@bitgo/account-lib' ) ;
3
+ const statics = require ( '@bitgo/statics' ) ;
2
4
const prova = require ( 'prova-lib' ) ;
3
5
const fs = require ( 'fs' ) ;
4
6
const _ = require ( 'lodash' ) ;
@@ -28,6 +30,7 @@ const coinDecimals = {
28
30
btc : 8 ,
29
31
eth : 18 ,
30
32
eos : 4 ,
33
+ trx : 6 ,
31
34
xrp : 6 ,
32
35
bch : 8 ,
33
36
bsv : 8 ,
@@ -38,6 +41,7 @@ const coinDecimals = {
38
41
tbtc : 8 ,
39
42
teth : 18 ,
40
43
teos : 4 ,
44
+ ttrx : 6 ,
41
45
txrp : 6 ,
42
46
tltc : 8 ,
43
47
txlm : 7 ,
@@ -204,6 +208,34 @@ const handleSignEthereum = function(recoveryRequest, key, skipConfirm) {
204
208
return transaction . serialize ( ) . toString ( 'hex' ) ;
205
209
} ;
206
210
211
+ const handleSignTrx = function ( recoveryRequest , key , skipConfirm ) {
212
+ const coin = recoveryRequest . coin ;
213
+
214
+ const txHex = getTransactionHexFromRequest ( recoveryRequest ) ;
215
+ const builder = new accountLib . TransactionBuilder ( { coinName : coin } ) ;
216
+ builder . from ( txHex ) ;
217
+
218
+ const customMessage = recoveryRequest . custom ? recoveryRequest . custom . message : 'None' ;
219
+
220
+ const outputs = builder . build ( ) . destinations . map ( d => {
221
+ return {
222
+ address : d . address ,
223
+ amount : d . value . toString ( 10 )
224
+ } ;
225
+ } ) ;
226
+
227
+ confirmRecovery ( recoveryRequest . backupKey , outputs , customMessage , skipConfirm ) ;
228
+
229
+ if ( ! key ) {
230
+ console . log ( 'Please enter the xprv of the wallet for signing: ' ) ;
231
+ key = prompt ( ) ;
232
+ }
233
+
234
+ const backupKeyNode = getHDNodeAndVerify ( key , recoveryRequest . backupKey ) ;
235
+ builder . sign ( { key : backupKeyNode . keyPair . getPrivateKeyBuffer ( ) } ) ;
236
+ return JSON . stringify ( builder . build ( ) . toJson ( ) ) ;
237
+ } ;
238
+
207
239
const handleSignEos = function ( recoveryRequest , key , skipConfirm ) {
208
240
const EosJs = require ( 'eosjs' ) ;
209
241
const ecc = require ( 'eosjs-ecc' ) ;
@@ -418,12 +450,11 @@ const handleSign = function(args) {
418
450
const file = args . file ;
419
451
420
452
const recoveryRequest = JSON . parse ( fs . readFileSync ( file , { encoding : 'utf8' } ) ) ;
421
- let coin = recoveryRequest . coin ;
453
+ const coin = statics . coins . get ( recoveryRequest . coin ) ;
422
454
423
- if ( coin . startsWith ( 't' ) ) {
455
+ if ( coin . network . type === 'testnet' ) {
424
456
bitgo = new bitgojs . BitGo ( { env : 'test' } ) ;
425
457
} else {
426
- console . log ( 'prod' ) ;
427
458
bitgo = new bitgojs . BitGo ( { env : 'prod' } ) ;
428
459
}
429
460
@@ -432,19 +463,19 @@ const handleSign = function(args) {
432
463
args . key = prompt ( "Key: " ) ;
433
464
}
434
465
435
- const key = parseKey ( args . key , coin , args . path ) ;
466
+ const key = parseKey ( args . key , coin . name , args . path ) ;
436
467
437
468
let txHex , halfSignedInfo ;
438
469
439
470
// If a tokenContractAddress was provided, use that. Otherwise use the named coin
440
- const basecoin = recoveryRequest . tokenContractAddress ? bitgo . coin ( recoveryRequest . tokenContractAddress ) : bitgo . coin ( coin ) ;
471
+ const basecoin = recoveryRequest . tokenContractAddress ? bitgo . coin ( recoveryRequest . tokenContractAddress ) : bitgo . coin ( coin . name ) ;
441
472
442
473
switch ( basecoin . getFamily ( ) ) {
443
474
case 'eth' :
444
475
if ( recoveryRequest . txPrebuild ) {
445
476
halfSignedInfo = handleHalfSignEth ( recoveryRequest , key , args . confirm , basecoin ) ;
446
477
} else {
447
- if ( coin . getChain ( ) === 'eth' || coin . getChain ( ) === 'teth' ) {
478
+ if ( coin . family === 'eth' && ! coin . isToken ) {
448
479
txHex = handleSignEthereum ( recoveryRequest , key , args . confirm ) ;
449
480
} else {
450
481
txHex = handleSignErc20 ( recoveryRequest , key , args . confirm , basecoin ) ;
@@ -454,6 +485,9 @@ const handleSign = function(args) {
454
485
case 'eos' :
455
486
txHex = handleSignEos ( recoveryRequest , key , args . confirm ) ;
456
487
break ;
488
+ case 'trx' :
489
+ txHex = handleSignTrx ( recoveryRequest , key , args . confirm ) ;
490
+ break ;
457
491
case 'xrp' :
458
492
txHex = handleSignXrp ( recoveryRequest , key , args . confirm ) ;
459
493
break ;
@@ -489,4 +523,4 @@ const handleSign = function(args) {
489
523
return finalRecovery ;
490
524
} ;
491
525
492
- module . exports = { handleSign, handleSignUtxo, handleSignEthereum, handleSignXrp, handleSignXlm, handleSignErc20, handleSignEos, parseKey } ;
526
+ module . exports = { handleSign, handleSignUtxo, handleSignEthereum, handleSignXrp, handleSignXlm, handleSignErc20, handleSignEos, handleSignTrx , parseKey } ;
0 commit comments