@@ -12,6 +12,9 @@ import {
1212import { ecc } from '@bitgo/secp256k1' ;
1313import { EXPORT_IN_C } from '../../resources/transactionData/exportInC' ;
1414import { IMPORT_IN_P } from '../../resources/transactionData/importInP' ;
15+ import { EXPORT_IN_P } from '../../resources/transactionData/exportInP' ;
16+ import { IMPORT_IN_C } from '../../resources/transactionData/importInC' ;
17+ import { TransactionBuilderFactory , Transaction } from '../../../src/lib' ;
1518
1619describe ( 'Utils' , function ( ) {
1720 let utils : Utils ;
@@ -539,4 +542,164 @@ describe('Utils', function () {
539542 assert . throws ( ( ) => utils . recoverySignature ( network , message , signature ) , / F a i l e d t o r e c o v e r s i g n a t u r e / ) ;
540543 } ) ;
541544 } ) ;
545+
546+ describe ( 'isTransactionOf' , function ( ) {
547+ const factory = new TransactionBuilderFactory ( coins . get ( 'tflrp' ) ) ;
548+ const utilsInstance = new Utils ( ) ;
549+ const testnetNetwork = coins . get ( 'tflrp' ) . network as FlareNetwork ;
550+ const pChainBlockchainIdHex = Buffer . from ( utilsInstance . cb58Decode ( testnetNetwork . blockchainID ) ) . toString ( 'hex' ) ;
551+ const cChainBlockchainIdHex = Buffer . from ( utilsInstance . cb58Decode ( testnetNetwork . cChainBlockchainID ) ) . toString (
552+ 'hex'
553+ ) ;
554+
555+ it ( 'should return true for Import in P transaction with matching P-chain blockchain ID' , async function ( ) {
556+ const txBuilder = factory
557+ . getImportInPBuilder ( )
558+ . threshold ( IMPORT_IN_P . threshold )
559+ . locktime ( IMPORT_IN_P . locktime )
560+ . fromPubKey ( IMPORT_IN_P . pAddresses )
561+ . externalChainId ( IMPORT_IN_P . sourceChainId )
562+ . fee ( IMPORT_IN_P . fee )
563+ . utxos ( IMPORT_IN_P . outputs ) ;
564+
565+ const tx = ( await txBuilder . build ( ) ) as Transaction ;
566+ const flareTransaction = tx . getFlareTransaction ( ) ;
567+
568+ assert . strictEqual ( utilsInstance . isTransactionOf ( flareTransaction , pChainBlockchainIdHex ) , true ) ;
569+ } ) ;
570+
571+ it ( 'should return false for Import in P transaction with non-matching C-chain blockchain ID' , async function ( ) {
572+ const txBuilder = factory
573+ . getImportInPBuilder ( )
574+ . threshold ( IMPORT_IN_P . threshold )
575+ . locktime ( IMPORT_IN_P . locktime )
576+ . fromPubKey ( IMPORT_IN_P . pAddresses )
577+ . externalChainId ( IMPORT_IN_P . sourceChainId )
578+ . fee ( IMPORT_IN_P . fee )
579+ . utxos ( IMPORT_IN_P . outputs ) ;
580+
581+ const tx = ( await txBuilder . build ( ) ) as Transaction ;
582+ const flareTransaction = tx . getFlareTransaction ( ) ;
583+
584+ assert . strictEqual ( utilsInstance . isTransactionOf ( flareTransaction , cChainBlockchainIdHex ) , false ) ;
585+ } ) ;
586+
587+ it ( 'should return true for Export in P transaction with matching P-chain blockchain ID' , async function ( ) {
588+ const txBuilder = factory
589+ . getExportInPBuilder ( )
590+ . threshold ( EXPORT_IN_P . threshold )
591+ . locktime ( EXPORT_IN_P . locktime )
592+ . fromPubKey ( EXPORT_IN_P . pAddresses )
593+ . externalChainId ( EXPORT_IN_P . sourceChainId )
594+ . fee ( EXPORT_IN_P . fee )
595+ . amount ( EXPORT_IN_P . amount )
596+ . utxos ( EXPORT_IN_P . outputs ) ;
597+
598+ const tx = ( await txBuilder . build ( ) ) as Transaction ;
599+ const flareTransaction = tx . getFlareTransaction ( ) ;
600+
601+ assert . strictEqual ( utilsInstance . isTransactionOf ( flareTransaction , pChainBlockchainIdHex ) , true ) ;
602+ } ) ;
603+
604+ it ( 'should return false for Export in P transaction with non-matching C-chain blockchain ID' , async function ( ) {
605+ const txBuilder = factory
606+ . getExportInPBuilder ( )
607+ . threshold ( EXPORT_IN_P . threshold )
608+ . locktime ( EXPORT_IN_P . locktime )
609+ . fromPubKey ( EXPORT_IN_P . pAddresses )
610+ . externalChainId ( EXPORT_IN_P . sourceChainId )
611+ . fee ( EXPORT_IN_P . fee )
612+ . amount ( EXPORT_IN_P . amount )
613+ . utxos ( EXPORT_IN_P . outputs ) ;
614+
615+ const tx = ( await txBuilder . build ( ) ) as Transaction ;
616+ const flareTransaction = tx . getFlareTransaction ( ) ;
617+
618+ assert . strictEqual ( utilsInstance . isTransactionOf ( flareTransaction , cChainBlockchainIdHex ) , false ) ;
619+ } ) ;
620+
621+ it ( 'should return true for Import in C transaction with matching C-chain blockchain ID' , async function ( ) {
622+ const txBuilder = factory
623+ . getImportInCBuilder ( )
624+ . threshold ( IMPORT_IN_C . threshold )
625+ . locktime ( IMPORT_IN_C . locktime )
626+ . fromPubKey ( IMPORT_IN_C . pAddresses )
627+ . externalChainId ( IMPORT_IN_C . sourceChainId )
628+ . feeRate ( IMPORT_IN_C . fee )
629+ . to ( IMPORT_IN_C . to )
630+ . utxos ( IMPORT_IN_C . outputs ) ;
631+
632+ const tx = ( await txBuilder . build ( ) ) as Transaction ;
633+ const flareTransaction = tx . getFlareTransaction ( ) ;
634+
635+ assert . strictEqual ( utilsInstance . isTransactionOf ( flareTransaction , cChainBlockchainIdHex ) , true ) ;
636+ } ) ;
637+
638+ it ( 'should return false for Import in C transaction with non-matching P-chain blockchain ID' , async function ( ) {
639+ const txBuilder = factory
640+ . getImportInCBuilder ( )
641+ . threshold ( IMPORT_IN_C . threshold )
642+ . locktime ( IMPORT_IN_C . locktime )
643+ . fromPubKey ( IMPORT_IN_C . pAddresses )
644+ . externalChainId ( IMPORT_IN_C . sourceChainId )
645+ . feeRate ( IMPORT_IN_C . fee )
646+ . to ( IMPORT_IN_C . to )
647+ . utxos ( IMPORT_IN_C . outputs ) ;
648+
649+ const tx = ( await txBuilder . build ( ) ) as Transaction ;
650+ const flareTransaction = tx . getFlareTransaction ( ) ;
651+
652+ assert . strictEqual ( utilsInstance . isTransactionOf ( flareTransaction , pChainBlockchainIdHex ) , false ) ;
653+ } ) ;
654+
655+ it ( 'should return true for Export in C transaction with matching C-chain blockchain ID' , async function ( ) {
656+ const txBuilder = factory
657+ . getExportInCBuilder ( )
658+ . fromPubKey ( EXPORT_IN_C . cHexAddress )
659+ . nonce ( EXPORT_IN_C . nonce )
660+ . amount ( EXPORT_IN_C . amount )
661+ . threshold ( EXPORT_IN_C . threshold )
662+ . locktime ( EXPORT_IN_C . locktime )
663+ . to ( EXPORT_IN_C . pAddresses )
664+ . feeRate ( EXPORT_IN_C . fee ) ;
665+
666+ const tx = ( await txBuilder . build ( ) ) as Transaction ;
667+ const flareTransaction = tx . getFlareTransaction ( ) ;
668+
669+ assert . strictEqual ( utilsInstance . isTransactionOf ( flareTransaction , cChainBlockchainIdHex ) , true ) ;
670+ } ) ;
671+
672+ it ( 'should return false for Export in C transaction with non-matching P-chain blockchain ID' , async function ( ) {
673+ const txBuilder = factory
674+ . getExportInCBuilder ( )
675+ . fromPubKey ( EXPORT_IN_C . cHexAddress )
676+ . nonce ( EXPORT_IN_C . nonce )
677+ . amount ( EXPORT_IN_C . amount )
678+ . threshold ( EXPORT_IN_C . threshold )
679+ . locktime ( EXPORT_IN_C . locktime )
680+ . to ( EXPORT_IN_C . pAddresses )
681+ . feeRate ( EXPORT_IN_C . fee ) ;
682+
683+ const tx = ( await txBuilder . build ( ) ) as Transaction ;
684+ const flareTransaction = tx . getFlareTransaction ( ) ;
685+
686+ assert . strictEqual ( utilsInstance . isTransactionOf ( flareTransaction , pChainBlockchainIdHex ) , false ) ;
687+ } ) ;
688+
689+ it ( 'should return false for invalid blockchain ID' , async function ( ) {
690+ const txBuilder = factory
691+ . getImportInPBuilder ( )
692+ . threshold ( IMPORT_IN_P . threshold )
693+ . locktime ( IMPORT_IN_P . locktime )
694+ . fromPubKey ( IMPORT_IN_P . pAddresses )
695+ . externalChainId ( IMPORT_IN_P . sourceChainId )
696+ . fee ( IMPORT_IN_P . fee )
697+ . utxos ( IMPORT_IN_P . outputs ) ;
698+
699+ const tx = ( await txBuilder . build ( ) ) as Transaction ;
700+ const flareTransaction = tx . getFlareTransaction ( ) ;
701+
702+ assert . strictEqual ( utilsInstance . isTransactionOf ( flareTransaction , 'invalidblockchainid' ) , false ) ;
703+ } ) ;
704+ } ) ;
542705} ) ;
0 commit comments