@@ -40,6 +40,7 @@ import {
4040import { KeyPair as XrpKeyPair } from './lib/keyPair' ;
4141import utils from './lib/utils' ;
4242import ripple from './ripple' ;
43+ import { TokenTransferBuilder , TransactionBuilderFactory , TransferBuilder } from './lib' ;
4344
4445export class Xrp extends BaseCoin {
4546 protected _staticsCoin : Readonly < StaticsBaseCoin > ;
@@ -541,29 +542,31 @@ export class Xrp extends BaseCoin {
541542 accountLines,
542543 keys,
543544 isKrsRecovery,
545+ isUnsignedSweep,
544546 userAddress,
545547 backupAddress,
546548 } ;
547549
548550 return this . recoverXrpToken ( params , tokenName , tokenParams ) ;
549551 }
550552
551- const transaction = {
552- TransactionType : 'Payment' ,
553- Account : params . rootAddress , // source address
554- Destination : destinationAddress ,
555- DestinationTag : destinationTag ,
556- Amount : recoverableBalance . toFixed ( 0 ) ,
557- Flags : 2147483648 ,
558- LastLedgerSequence : currentLedger + 1000000 , // give it 1 million ledgers' time (~1 month, suitable for KRS)
559- Fee : openLedgerFee . times ( 3 ) . toFixed ( 0 ) , // the factor three is for the multisigning
560- Sequence : sequenceId ,
561- } ;
562- const txJSON : string = JSON . stringify ( transaction ) ;
553+ const factory = new TransactionBuilderFactory ( coins . get ( this . getChain ( ) ) ) ;
554+ const txBuilder = factory . getTransferBuilder ( ) as TransferBuilder ;
555+ txBuilder
556+ . to ( destinationAddress as string )
557+ . amount ( recoverableBalance . toFixed ( 0 ) )
558+ . sender ( params . rootAddress )
559+ . flags ( 2147483648 )
560+ . lastLedgerSequence ( currentLedger + 1000000 ) // give it 1 million ledgers' time (~1 month, suitable for KRS)
561+ . fee ( openLedgerFee . times ( 3 ) . toFixed ( 0 ) ) // the factor three is for the multisigning
562+ . sequence ( sequenceId ) ;
563+
564+ const tx = await txBuilder . build ( ) ;
565+ const serializedTx = tx . toBroadcastFormat ( ) ;
563566
564567 if ( isUnsignedSweep ) {
565568 return {
566- txHex : txJSON ,
569+ txHex : serializedTx ,
567570 coin : this . getChain ( ) ,
568571 } ;
569572 }
@@ -572,7 +575,7 @@ export class Xrp extends BaseCoin {
572575 throw new Error ( `userKey is not a private key` ) ;
573576 }
574577 const userKey = keys [ 0 ] . privateKey . toString ( 'hex' ) ;
575- const userSignature = ripple . signWithPrivateKey ( txJSON , userKey , { signAs : userAddress } ) ;
578+ const userSignature = ripple . signWithPrivateKey ( serializedTx , userKey , { signAs : userAddress } ) ;
576579
577580 let signedTransaction : string ;
578581
@@ -583,7 +586,7 @@ export class Xrp extends BaseCoin {
583586 throw new Error ( `backupKey is not a private key` ) ;
584587 }
585588 const backupKey = keys [ 1 ] . privateKey . toString ( 'hex' ) ;
586- const backupSignature = ripple . signWithPrivateKey ( txJSON , backupKey , { signAs : backupAddress } ) ;
589+ const backupSignature = ripple . signWithPrivateKey ( serializedTx , backupKey , { signAs : backupAddress } ) ;
587590 signedTransaction = ripple . multisign ( [ userSignature . signedTransaction , backupSignature . signedTransaction ] ) ;
588591 }
589592
@@ -603,8 +606,6 @@ export class Xrp extends BaseCoin {
603606 public async recoverXrpToken ( params , tokenName , tokenParams ) {
604607 const { currency, issuer } = utils . getXrpCurrencyFromTokenName ( tokenName ) ;
605608
606- // const accountLines = JSON.parse(tokenParams.accountLines);
607- // const lines = accountLines.body.result.lines;
608609 const lines = tokenParams . accountLines . body . result . lines ;
609610
610611 let amount ;
@@ -622,33 +623,40 @@ export class Xrp extends BaseCoin {
622623 throw new Error ( `Does not have funds to recover` ) ;
623624 }
624625
626+ const decimalPlaces = coins . get ( tokenName ) . decimalPlaces ;
627+ amount = new BigNumber ( amount ) . shiftedBy ( decimalPlaces ) . toFixed ( ) ;
628+
625629 const FLAG_VALUE = 2147483648 ;
626630
627- const transaction = {
628- TransactionType : 'Payment' ,
629- Amount : {
630- value : amount ,
631- currency,
632- issuer,
633- } , // source address
634- Destination : tokenParams . destinationAddress ,
635- DestinationTag : tokenParams . destinationTag ,
636- Account : params . rootAddress ,
637- Flags : FLAG_VALUE ,
638- LastLedgerSequence : tokenParams . currentLedger + 1000000 , // give it 1 million ledgers' time (~1 month, suitable for KRS)
639- Fee : tokenParams . openLedgerFee . times ( 3 ) . toFixed ( 0 ) , // the factor three is for the multisigning
640- Sequence : tokenParams . sequenceId ,
641- } ;
642- const txJSON : string = JSON . stringify ( transaction ) ;
631+ const factory = new TransactionBuilderFactory ( coins . get ( tokenName ) ) ;
632+ const txBuilder = factory . getTokenTransferBuilder ( ) as TokenTransferBuilder ;
633+ txBuilder
634+ . to ( tokenParams . destinationAddress )
635+ . amount ( amount )
636+ . sender ( params . rootAddress )
637+ . flags ( FLAG_VALUE )
638+ . lastLedgerSequence ( tokenParams . currentLedger + 1000000 ) // give it 1 million ledgers' time (~1 month, suitable for KRS)
639+ . fee ( tokenParams . openLedgerFee . times ( 3 ) . toFixed ( 0 ) ) // the factor three is for the multisigning
640+ . sequence ( tokenParams . sequenceId ) ;
641+
642+ const tx = await txBuilder . build ( ) ;
643+ const serializedTx = tx . toBroadcastFormat ( ) ;
644+
645+ const { keys, isKrsRecovery, isUnsignedSweep, userAddress, backupAddress } = tokenParams ;
643646
644- const { keys, isKrsRecovery, userAddress, backupAddress } = tokenParams ;
647+ if ( isUnsignedSweep ) {
648+ return {
649+ txHex : serializedTx ,
650+ coin : this . getChain ( ) ,
651+ } ;
652+ }
645653
646654 if ( ! keys [ 0 ] . privateKey ) {
647655 throw new Error ( `userKey is not a private key` ) ;
648656 }
649657
650658 const userKey = keys [ 0 ] . privateKey . toString ( 'hex' ) ;
651- const userSignature = ripple . signWithPrivateKey ( txJSON , userKey , { signAs : userAddress } ) ;
659+ const userSignature = ripple . signWithPrivateKey ( serializedTx , userKey , { signAs : userAddress } ) ;
652660
653661 let signedTransaction : string ;
654662
@@ -659,7 +667,7 @@ export class Xrp extends BaseCoin {
659667 throw new Error ( `backupKey is not a private key` ) ;
660668 }
661669 const backupKey = keys [ 1 ] . privateKey . toString ( 'hex' ) ;
662- const backupSignature = ripple . signWithPrivateKey ( txJSON , backupKey , { signAs : backupAddress } ) ;
670+ const backupSignature = ripple . signWithPrivateKey ( serializedTx , backupKey , { signAs : backupAddress } ) ;
663671 signedTransaction = ripple . multisign ( [ userSignature . signedTransaction , backupSignature . signedTransaction ] ) ;
664672 }
665673
0 commit comments