import { Transaction , TxOptions } from '../src/transaction' ; import * as NETWORKS from '../src/networks' ; import * as bscript from '../src/script' ; const DEFAULT_SEQUENCE = 0xffffffff ; const EMPTY_SCRIPT = Buffer . allocUnsafe ( 0 ) ; export const getNetwork = ( name ? ) => ( name ? NETWORKS [ name ] : undefined ) ; export const getVinVoutScript = ( vinvout ) => { if ( vinvout . data ) { return Buffer . from ( vinvout . data , 'hex' ) ; } if ( vinvout . script ) { return bscript . fromASM ( vinvout . script ) ; } return EMPTY_SCRIPT ; } ; type RawOptions = { noWitness ? : boolean ; network ? : TxOptions [ 'network' ] ; txSpecific ? : TxOptions [ 'txSpecific' ] ; } ; export type Fixture = { description : string ; network ? : string ; id : string ; hash : string ; hex : string ; whex ? : string ; weight : number ; virtualSize : number ; coinbase : boolean ; raw : { version : number ; } ; } ; export const fromRaw = ( raw , options = { } ) => { const tx = new Transaction ( options ) ; tx . version = raw . version ; tx . locktime = raw . locktime ; tx . ins = raw . ins . map ( ( input ) => ( { index : input . index , hash : Buffer . from ( input . hash , 'hex' ) , script : getVinVoutScript ( input ) , sequence : input . sequence || DEFAULT_SEQUENCE , witness : ! options . noWitness && input . witness ? input . witness . map ( ( x ) => Buffer . from ( x , 'hex' ) ) : [ ] , } ) ) ; tx . outs = raw . outs . map ( ( output ) => ( { script : getVinVoutScript ( output ) , value : output . value , } ) ) ; return tx ; } ; export const checkTx = ( tx , raw ) => { expect ( tx . version ) . toEqual ( raw . version ) ; expect ( tx . locktime ) . toEqual ( raw . locktime ) ; expect ( tx . timestamp ) . toEqual ( raw . timestamp ) ; tx . ins . forEach ( ( input , i ) => { const expected = raw . ins [ i ] ; expect ( input . hash . toString ( 'hex' ) ) . toEqual ( expected . hash ) ; expect ( input . index ) . toEqual ( expected . index ) ; expect ( input . script . toString ( 'hex' ) ) . toEqual ( getVinVoutScript ( expected ) . toString ( 'hex' ) ) ; expect ( input . sequence ) . toEqual ( expected . sequence || DEFAULT_SEQUENCE ) ; } ) ; tx . outs . forEach ( ( output , i ) => { const expected = raw . outs [ i ] ; expect ( output . value ) . toEqual ( expected . value . toString ( ) ) ; expect ( output . script . toString ( 'hex' ) ) . toEqual ( getVinVoutScript ( expected ) . toString ( 'hex' ) ) ; } ) ; } ;