33 */
44import * as Bluebird from 'bluebird' ;
55import * as crypto from 'crypto' ;
6- import { CoinFamily } from '@bitgo/statics' ;
6+ import { CoinFamily , BaseCoin as StaticsBaseCoin } from '@bitgo/statics' ;
77const co = Bluebird . coroutine ;
88import * as bitgoAccountLib from '@bitgo/account-lib' ;
9- import { HDNode } from 'bitgo-utxo-lib' ;
9+ import { HDNode , networks } from 'bitgo-utxo-lib' ;
1010import * as request from 'superagent' ;
1111import * as common from '../../common' ;
1212
13- import { BaseCoin as StaticsBaseCoin } from '@bitgo/statics' ;
14-
1513import {
1614 BaseCoin ,
1715 KeyPair ,
@@ -26,14 +24,11 @@ import {
2624 TransactionPrebuild as BaseTransactionPrebuild ,
2725 TransactionExplanation ,
2826} from '../baseCoin' ;
29- import * as utxoLib from 'bitgo-utxo-lib' ;
27+
3028import { BitGo } from '../../bitgo' ;
3129import { NodeCallback } from '../types' ;
3230import { TransactionBuilder } from '@bitgo/account-lib' ;
3331
34- import debug = require( 'debug' ) ;
35- import { utxo } from "@bitgo/statics/dist/src/utxo" ;
36-
3732export interface TronSignTransactionOptions extends SignTransactionOptions {
3833 txPrebuild : TransactionPrebuild ;
3934 prv : string ;
@@ -44,6 +39,7 @@ export interface TxInfo {
4439 from : string ;
4540 txid : string ;
4641}
42+
4743export interface TronTransactionExplanation extends TransactionExplanation {
4844 expiration : number ;
4945 timestamp : number ;
@@ -83,7 +79,6 @@ export enum NodeTypes {
8379 Solidity ,
8480}
8581
86-
8782export class Trx extends BaseCoin {
8883 protected readonly _staticsCoin : Readonly < StaticsBaseCoin > ;
8984
@@ -157,7 +152,7 @@ export class Trx extends BaseCoin {
157152 // random. 512 bits is therefore the maximum entropy and gives us maximum security against cracking.
158153 seed = crypto . randomBytes ( 512 / 8 ) ;
159154 }
160- const hd = utxoLib . HDNode . fromSeedBuffer ( seed ) ;
155+ const hd = HDNode . fromSeedBuffer ( seed ) ;
161156 return {
162157 pub : hd . neutered ( ) . toBase58 ( ) ,
163158 prv : hd . toBase58 ( ) ,
@@ -166,7 +161,7 @@ export class Trx extends BaseCoin {
166161
167162 isValidXpub ( xpub : string ) : boolean {
168163 try {
169- return utxoLib . HDNode . fromBase58 ( xpub ) . isNeutered ( ) ;
164+ return HDNode . fromBase58 ( xpub ) . isNeutered ( ) ;
170165 } catch ( e ) {
171166 return false ;
172167 }
@@ -284,38 +279,23 @@ export class Trx extends BaseCoin {
284279 throw new Error ( 'invalid xpub' ) ;
285280 }
286281
287- const hdNode = utxoLib . HDNode . fromBase58 ( xpub , this . bitcoinEncoding ( ) ) ;
282+ const hdNode = HDNode . fromBase58 ( xpub , networks . bitcoin ) ;
288283 return hdNode . keyPair . __Q . getEncoded ( false ) . toString ( 'hex' ) ;
289284 }
290285
291286 compressedPubToHexAddress ( pub : string ) : string {
292287 const byteArrayAddr = bitgoAccountLib . Trx . Utils . getByteArrayFromHexAddress ( pub ) ;
293288 const rawAddress = bitgoAccountLib . Trx . Utils . getRawAddressFromPubKey ( byteArrayAddr ) ;
294- return Buffer . from ( rawAddress ) . toString ( 'hex' ) . toUpperCase ( ) ;
289+ return bitgoAccountLib . Trx . Utils . getHexAddressFromByteArray ( rawAddress ) ;
295290 }
296291
297292 xprvToCompressedPrv ( xprv : string ) : string {
298293 if ( ! this . isValidXprv ( xprv ) ) {
299294 throw new Error ( 'invalid xprv' ) ;
300295 }
301296
302- const hdNode = utxoLib . HDNode . fromBase58 ( xprv , this . bitcoinEncoding ( ) ) ;
297+ const hdNode = HDNode . fromBase58 ( xprv , networks . bitcoin ) ;
303298 return hdNode . keyPair . d . toBuffer ( 32 ) . toString ( 'hex' ) ;
304- } ;
305-
306- bitcoinEncoding ( ) : any {
307- return {
308- messagePrefix : '\x18Bitcoin Signed Message:\n' ,
309- bech32 : 'bc' ,
310- bip32 : {
311- public : 0x0488b21e ,
312- private : 0x0488ade4
313- } ,
314- pubKeyHash : 0x00 ,
315- scriptHash : 0x05 ,
316- wif : 0x80 ,
317- coin : 'btc' ,
318- } ;
319299 }
320300
321301 /**
@@ -324,7 +304,7 @@ export class Trx extends BaseCoin {
324304 * @param callback
325305 * @returns {Object } response from Trongrid
326306 */
327- recoveryPost ( query : { path : string , jsonObj : any , node : NodeTypes } , callback ?: NodeCallback < any > ) : Bluebird < any > {
307+ recoveryPost ( query : { path : string ; jsonObj : any ; node : NodeTypes } , callback ?: NodeCallback < any > ) : Bluebird < any > {
328308 const self = this ;
329309 return co ( function * ( ) {
330310 let nodeUri = '' ;
@@ -340,16 +320,19 @@ export class Trx extends BaseCoin {
340320 }
341321
342322 const response = yield request
343- . post ( nodeUri + query . path )
344- . send ( query . jsonObj ) ;
323+ . post ( nodeUri + query . path )
324+ . type ( 'json' )
325+ . send ( query . jsonObj ) ;
345326
346327 if ( ! response . ok ) {
347328 throw new Error ( 'could not reach Tron node' ) ;
348329 }
349- return response . body ;
330+
331+ // unfortunately, it doesn't look like most TRON nodes return valid json as body
332+ return JSON . parse ( response . text ) ;
350333 } )
351- . call ( this )
352- . asCallback ( callback ) ;
334+ . call ( this )
335+ . asCallback ( callback ) ;
353336 }
354337
355338 /**
@@ -387,15 +370,15 @@ export class Trx extends BaseCoin {
387370 path : '/wallet/createtransaction' ,
388371 jsonObj : {
389372 to_address : toAddr ,
390- from_address : fromAddr ,
391- amount : amount . toFixed ( 0 ) ,
373+ owner_address : fromAddr ,
374+ amount,
392375 } ,
393376 node : NodeTypes . Full ,
394377 } ) ;
395- return result . balance ;
378+ return result ;
396379 } )
397- . call ( this )
398- . asCallback ( callback ) ;
380+ . call ( this )
381+ . asCallback ( callback ) ;
399382 }
400383
401384 /**
@@ -420,9 +403,9 @@ export class Trx extends BaseCoin {
420403 const bitgoAddress = self . compressedPubToHexAddress ( self . xpubToCompressedPub ( params . bitgoKey ) ) ;
421404 const recoveryAddressHex = bitgoAccountLib . Trx . Utils . getHexAddressFromBase58Address ( params . recoveryDestination ) ;
422405
406+ // call the node to get our account balance
423407 const recoveryAmount = yield self . getAccountBalanceFromNode ( bitgoAddress ) ;
424408
425- //const userPrv = HDKey.fromExtendedKey(keys[0]).privateKey;
426409 const userXPub = keys [ 0 ] . neutered ( ) . toBase58 ( ) ;
427410 const userXPrv = keys [ 0 ] . toBase58 ( ) ;
428411 const backupXPub = keys [ 1 ] . neutered ( ) . toBase58 ( ) ;
@@ -431,18 +414,16 @@ export class Trx extends BaseCoin {
431414 const userHexAddr = self . compressedPubToHexAddress ( self . xpubToCompressedPub ( userXPub ) ) ;
432415 const backupHexAddr = self . compressedPubToHexAddress ( self . xpubToCompressedPub ( backupXPub ) ) ;
433416
434- // TODO: some checks here about pubs being valid, for this wallet, etc.
435-
436- // construct the tx
437- // TODO: recovery amount might not include fees, we'll have to figure out the max we can spend
438- // sensitive to energy and tx cost amounts
417+ // construct the tx -
439418 // there's an assumption here being made about fees: for a wallet that hasn't been used in awhile, the implication is
440- // it has maximum bandwidth. thus, a recovery should cost the minimum amount (1e6 sun)
419+ // it has maximum bandwidth. thus, a recovery should cost the minimum amount (1e6 sun or 1 Tron )
441420 if ( 1e6 > recoveryAmount ) {
442421 throw new Error ( 'Amount of funds to recover wouldnt be able to fund a send' ) ;
443422 }
444423 const recoveryAmountMinusFees = recoveryAmount - 1e6 ;
445- const buildTx = self . getBuildTransaction ( recoveryAddressHex , bitgoAddress , recoveryAmountMinusFees ) ;
424+ const buildTx = yield self . getBuildTransaction ( recoveryAddressHex , bitgoAddress , recoveryAmountMinusFees ) ;
425+
426+ // TODO: some checks here about pubs being valid, for this wallet, etc. from build transaction
446427
447428 // construct our tx
448429 const txBuilder = new bitgoAccountLib . TransactionBuilder ( { coinName : this . getChain ( ) } ) ;
@@ -453,11 +434,11 @@ export class Trx extends BaseCoin {
453434 return txBuilder . build ( ) . toJson ( ) ;
454435 }
455436
456- // sign our tx
457437 txBuilder . sign ( { key : userPrv } ) ;
458438
439+ // krs recoveries don't get signed
459440 if ( ! isKrsRecovery ) {
460- const backupXPrv = keys [ 0 ] . toBase58 ( ) ;
441+ const backupXPrv = keys [ 1 ] . toBase58 ( ) ;
461442 const backupPrv = self . xprvToCompressedPrv ( backupXPrv ) ;
462443
463444 txBuilder . sign ( { key : backupPrv } ) ;
0 commit comments