@@ -6,6 +6,8 @@ const BN = require('bignumber.js');
6
6
const prompt = require ( 'prompt-sync' ) ( ) ;
7
7
const utils = require ( './utils' ) ;
8
8
const bip39 = require ( 'bip39' ) ;
9
+ const bitgojs = require ( 'bitgo' ) ;
10
+ let bitgo ;
9
11
10
12
const utxoNetworks = {
11
13
btc : utxoLib . networks . bitcoin ,
@@ -158,8 +160,8 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
158
160
return txBuilder . build ( ) . toHex ( ) ;
159
161
} ;
160
162
161
- const handleHalfSignEth = function ( recoveryRequest , key , skipConfirm ) {
162
- return utils . halfSignEthTransaction ( recoveryRequest . coin , recoveryRequest . txPrebuild , key ) ;
163
+ const handleHalfSignEth = function ( recoveryRequest , key , skipConfirm , basecoin ) {
164
+ return utils . halfSignEthTransaction ( basecoin , recoveryRequest , key ) ;
163
165
}
164
166
165
167
const handleSignEthereum = function ( recoveryRequest , key , skipConfirm ) {
@@ -244,13 +246,13 @@ const handleSignXlm = function(recoveryRequest, key, skipConfirm) {
244
246
throw new Error ( 'Recovery transaction is trying to perform multiple operations - aborting' ) ;
245
247
}
246
248
247
- if ( transaction . operations [ 0 ] . type !== 'payment' ) {
248
- throw new Error ( 'Recovery transaction is not a payment transaction - aborting' ) ;
249
+ if ( transaction . operations [ 0 ] . type !== 'payment' && transaction . operations [ 0 ] . type !== 'createAccount' ) {
250
+ throw new Error ( 'Recovery transaction is not a payment or createAccount transaction - aborting' ) ;
249
251
}
250
252
251
253
const outputs = [ {
252
254
address : transaction . operations [ 0 ] . destination ,
253
- amount : transaction . operations [ 0 ] . amount
255
+ amount : transaction . operations [ 0 ] . amount || transaction . operations [ 0 ] . startingBalance
254
256
} ] ;
255
257
256
258
confirmRecovery ( recoveryRequest . backupKey , outputs , customMessage , skipConfirm ) ;
@@ -363,7 +365,14 @@ const handleSign = function(args) {
363
365
const file = args . file ;
364
366
365
367
const recoveryRequest = JSON . parse ( fs . readFileSync ( file , { encoding : 'utf8' } ) ) ;
366
- const coin = recoveryRequest . coin ;
368
+ let coin = recoveryRequest . coin ;
369
+
370
+ if ( coin . startsWith ( 't' ) ) {
371
+ bitgo = new bitgojs . BitGo ( { env : 'test' } ) ;
372
+ } else {
373
+ console . log ( 'prod' ) ;
374
+ bitgo = new bitgojs . BitGo ( { env : 'prod' } ) ;
375
+ }
367
376
368
377
if ( ! args . key ) {
369
378
console . log ( "\nEnter your private key for signing.\nEnter an xprv or 24 words.\nIf entering 24 words, separate each word with only a comma and no spaces.\n" ) ;
@@ -374,27 +383,27 @@ const handleSign = function(args) {
374
383
375
384
let txHex , halfSignedInfo ;
376
385
377
- switch ( coin ) {
378
- case 'eth' : case 'teth' :
386
+ // If a tokenContractAddress was provided, use that. Otherwise use the named coin
387
+ const basecoin = recoveryRequest . tokenContractAddress ? bitgo . coin ( recoveryRequest . tokenContractAddress ) : bitgo . coin ( coin ) ;
388
+
389
+ switch ( basecoin . getFamily ( ) ) {
390
+ case 'eth' :
379
391
if ( recoveryRequest . txPrebuild ) {
380
- halfSignedInfo = handleHalfSignEth ( recoveryRequest , key , args . confirm ) ;
392
+ halfSignedInfo = handleHalfSignEth ( recoveryRequest , key , args . confirm , basecoin ) ;
381
393
} else {
382
- txHex = handleSignEthereum ( recoveryRequest , key , args . confirm ) ;
394
+ if ( coin . getChain ( ) === 'eth' || coin . getChain ( ) === 'teth' ) {
395
+ txHex = handleSignEthereum ( recoveryRequest , key , args . confirm ) ;
396
+ } else {
397
+ txHex = handleSignErc20 ( recoveryRequest , key , args . confirm , basecoin ) ;
398
+ }
383
399
}
384
400
break ;
385
- case 'xrp' : case 'txrp' :
401
+ case 'xrp' :
386
402
txHex = handleSignXrp ( recoveryRequest , key , args . confirm ) ;
387
403
break ;
388
- case 'xlm' : case 'txlm' :
404
+ case 'xlm' :
389
405
txHex = handleSignXlm ( recoveryRequest , key , args . confirm ) ;
390
406
break ;
391
- case 'erc20' : case 'terc20' : case 'terc' : case 'erc' :
392
- if ( recoveryRequest . txPrebuild ) {
393
- halfSignedInfo = handleHalfSignEth ( recoveryRequest , key , args . confirm ) ;
394
- } else {
395
- txHex = handleSignErc20 ( recoveryRequest , key , args . confirm ) ;
396
- }
397
- break ;
398
407
default :
399
408
txHex = handleSignUtxo ( recoveryRequest , key , args . confirm ) ;
400
409
break ;
@@ -408,7 +417,7 @@ const handleSign = function(args) {
408
417
console . log ( `Writing signed transaction to file: ${ filename } ` ) ;
409
418
410
419
let finalRecovery ;
411
-
420
+
412
421
if ( txHex ) {
413
422
finalRecovery = _ . pick ( recoveryRequest , [ 'backupKey' , 'coin' , 'recoveryAmount' ] ) ;
414
423
finalRecovery . txHex = txHex ;
0 commit comments