@ -15,9 +15,9 @@ function constructSign (f, txb) {
const network = NETWORKS [ f . network ]
const network = NETWORKS [ f . network ]
const stages = f . stages && f . stages . concat ( )
const stages = f . stages && f . stages . concat ( )
f . inputs . forEach ( function ( input , index ) {
f . inputs . forEach ( ( input , index ) => {
if ( ! input . signs ) return
if ( ! input . signs ) return
input . signs . forEach ( function ( sign ) {
input . signs . forEach ( sign => {
const keyPair = ECPair . fromWIF ( sign . keyPair , network )
const keyPair = ECPair . fromWIF ( sign . keyPair , network )
let redeemScript
let redeemScript
let witnessScript
let witnessScript
@ -55,7 +55,7 @@ function construct (f, dontSign) {
if ( Number . isFinite ( f . version ) ) txb . setVersion ( f . version )
if ( Number . isFinite ( f . version ) ) txb . setVersion ( f . version )
if ( f . locktime !== undefined ) txb . setLockTime ( f . locktime )
if ( f . locktime !== undefined ) txb . setLockTime ( f . locktime )
f . inputs . forEach ( function ( input ) {
f . inputs . forEach ( input => {
let prevTx
let prevTx
if ( input . txRaw ) {
if ( input . txRaw ) {
const constructed = construct ( input . txRaw )
const constructed = construct ( input . txRaw )
@ -75,7 +75,7 @@ function construct (f, dontSign) {
txb . addInput ( prevTx , input . vout , input . sequence , prevTxScript )
txb . addInput ( prevTx , input . vout , input . sequence , prevTxScript )
} )
} )
f . outputs . forEach ( function ( output ) {
f . outputs . forEach ( output => {
if ( output . address ) {
if ( output . address ) {
txb . addOutput ( output . address , output . value )
txb . addOutput ( output . address , output . value )
} else {
} else {
@ -87,20 +87,20 @@ function construct (f, dontSign) {
return constructSign ( f , txb )
return constructSign ( f , txb )
}
}
describe ( 'TransactionBuilder' , function ( ) {
describe ( 'TransactionBuilder' , ( ) => {
// constants
// constants
const keyPair = ECPair . fromPrivateKey ( Buffer . from ( '0000000000000000000000000000000000000000000000000000000000000001' , 'hex' ) )
const keyPair = ECPair . fromPrivateKey ( Buffer . from ( '0000000000000000000000000000000000000000000000000000000000000001' , 'hex' ) )
const scripts = [
const scripts = [
'1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH' ,
'1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH' ,
'1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP'
'1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP'
] . map ( function ( x ) {
] . map ( x => {
return baddress . toOutputScript ( x )
return baddress . toOutputScript ( x )
} )
} )
const txHash = Buffer . from ( '0e7cea811c0be9f73c0aca591034396e7264473fc25c1ca45195d7417b36cbe2' , 'hex' )
const txHash = Buffer . from ( '0e7cea811c0be9f73c0aca591034396e7264473fc25c1ca45195d7417b36cbe2' , 'hex' )
describe ( 'fromTransaction' , function ( ) {
describe ( 'fromTransaction' , ( ) => {
fixtures . valid . build . forEach ( function ( f ) {
fixtures . valid . build . forEach ( f => {
it ( 'returns TransactionBuilder, with ' + f . description , function ( ) {
it ( 'returns TransactionBuilder, with ' + f . description , ( ) => {
const network = NETWORKS [ f . network || 'bitcoin' ]
const network = NETWORKS [ f . network || 'bitcoin' ]
const tx = Transaction . fromHex ( f . txHex )
const tx = Transaction . fromHex ( f . txHex )
@ -112,82 +112,82 @@ describe('TransactionBuilder', function () {
} )
} )
} )
} )
fixtures . valid . fromTransaction . forEach ( function ( f ) {
fixtures . valid . fromTransaction . forEach ( f => {
it ( 'returns TransactionBuilder, with ' + f . description , function ( ) {
it ( 'returns TransactionBuilder, with ' + f . description , ( ) => {
const tx = new Transaction ( )
const tx = new Transaction ( )
f . inputs . forEach ( function ( input ) {
f . inputs . forEach ( input => {
const txHash2 = Buffer . from ( input . txId , 'hex' ) . reverse ( )
const txHash2 = Buffer . from ( input . txId , 'hex' ) . reverse ( )
tx . addInput ( txHash2 , input . vout , undefined , bscript . fromASM ( input . scriptSig ) )
tx . addInput ( txHash2 , input . vout , undefined , bscript . fromASM ( input . scriptSig ) )
} )
} )
f . outputs . forEach ( function ( output ) {
f . outputs . forEach ( output => {
tx . addOutput ( bscript . fromASM ( output . script ) , output . value )
tx . addOutput ( bscript . fromASM ( output . script ) , output . value )
} )
} )
const txb = TransactionBuilder . fromTransaction ( tx )
const txb = TransactionBuilder . fromTransaction ( tx )
const txAfter = f . incomplete ? txb . buildIncomplete ( ) : txb . build ( )
const txAfter = f . incomplete ? txb . buildIncomplete ( ) : txb . build ( )
txAfter . ins . forEach ( function ( input , i ) {
txAfter . ins . forEach ( ( input , i ) => {
assert . e qual( bscript . toASM ( input . script ) , f . inputs [ i ] . scriptSigAfter )
assert . strictE qual( bscript . toASM ( input . script ) , f . inputs [ i ] . scriptSigAfter )
} )
} )
txAfter . outs . forEach ( function ( output , i ) {
txAfter . outs . forEach ( ( output , i ) => {
assert . e qual( bscript . toASM ( output . script ) , f . outputs [ i ] . script )
assert . strictE qual( bscript . toASM ( output . script ) , f . outputs [ i ] . script )
} )
} )
} )
} )
} )
} )
fixtures . valid . fromTransactionSequential . forEach ( function ( f ) {
fixtures . valid . fromTransactionSequential . forEach ( f => {
it ( 'with ' + f . description , function ( ) {
it ( 'with ' + f . description , ( ) => {
const network = NETWORKS [ f . network ]
const network = NETWORKS [ f . network ]
const tx = Transaction . fromHex ( f . txHex )
const tx = Transaction . fromHex ( f . txHex )
const txb = TransactionBuilder . fromTransaction ( tx , network )
const txb = TransactionBuilder . fromTransaction ( tx , network )
tx . ins . forEach ( function ( input , i ) {
tx . ins . forEach ( ( input , i ) => {
assert . e qual( bscript . toASM ( input . script ) , f . inputs [ i ] . scriptSig )
assert . strictE qual( bscript . toASM ( input . script ) , f . inputs [ i ] . scriptSig )
} )
} )
constructSign ( f , txb )
constructSign ( f , txb )
const txAfter = f . incomplete ? txb . buildIncomplete ( ) : txb . build ( )
const txAfter = f . incomplete ? txb . buildIncomplete ( ) : txb . build ( )
txAfter . ins . forEach ( function ( input , i ) {
txAfter . ins . forEach ( ( input , i ) => {
assert . e qual( bscript . toASM ( input . script ) , f . inputs [ i ] . scriptSigAfter )
assert . strictE qual( bscript . toASM ( input . script ) , f . inputs [ i ] . scriptSigAfter )
} )
} )
assert . e qual( txAfter . toHex ( ) , f . txHexAfter )
assert . strictE qual( txAfter . toHex ( ) , f . txHexAfter )
} )
} )
} )
} )
it ( 'classifies transaction inputs' , function ( ) {
it ( 'classifies transaction inputs' , ( ) => {
const tx = Transaction . fromHex ( fixtures . valid . classification . hex )
const tx = Transaction . fromHex ( fixtures . valid . classification . hex )
const txb = TransactionBuilder . fromTransaction ( tx )
const txb = TransactionBuilder . fromTransaction ( tx )
txb . __ INPUTS . forEach ( function ( i ) {
txb . __ INPUTS . forEach ( i => {
assert . strictEqual ( i . prevOutType , 'scripthash' )
assert . strictEqual ( i . prevOutType , 'scripthash' )
assert . strictEqual ( i . redeemScriptType , 'multisig' )
assert . strictEqual ( i . redeemScriptType , 'multisig' )
} )
} )
} )
} )
fixtures . invalid . fromTransaction . forEach ( function ( f ) {
fixtures . invalid . fromTransaction . forEach ( f => {
it ( 'throws ' + f . exception , function ( ) {
it ( 'throws ' + f . exception , ( ) => {
const tx = Transaction . fromHex ( f . txHex )
const tx = Transaction . fromHex ( f . txHex )
assert . throws ( function ( ) {
assert . throws ( ( ) => {
TransactionBuilder . fromTransaction ( tx )
TransactionBuilder . fromTransaction ( tx )
} , new RegExp ( f . exception ) )
} , new RegExp ( f . exception ) )
} )
} )
} )
} )
} )
} )
describe ( 'addInput' , function ( ) {
describe ( 'addInput' , ( ) => {
let txb
let txb
beforeEach ( function ( ) {
beforeEach ( ( ) => {
txb = new TransactionBuilder ( )
txb = new TransactionBuilder ( )
} )
} )
it ( 'accepts a txHash, index [and sequence number]' , function ( ) {
it ( 'accepts a txHash, index [and sequence number]' , ( ) => {
const vin = txb . addInput ( txHash , 1 , 54 )
const vin = txb . addInput ( txHash , 1 , 54 )
assert . strictEqual ( vin , 0 )
assert . strictEqual ( vin , 0 )
@ -198,7 +198,7 @@ describe('TransactionBuilder', function () {
assert . strictEqual ( txb . __ INPUTS [ 0 ] . prevOutScript , undefined )
assert . strictEqual ( txb . __ INPUTS [ 0 ] . prevOutScript , undefined )
} )
} )
it ( 'accepts a txHash, index [, sequence number and scriptPubKey]' , function ( ) {
it ( 'accepts a txHash, index [, sequence number and scriptPubKey]' , ( ) => {
const vin = txb . addInput ( txHash , 1 , 54 , scripts [ 1 ] )
const vin = txb . addInput ( txHash , 1 , 54 , scripts [ 1 ] )
assert . strictEqual ( vin , 0 )
assert . strictEqual ( vin , 0 )
@ -209,7 +209,7 @@ describe('TransactionBuilder', function () {
assert . strictEqual ( txb . __ INPUTS [ 0 ] . prevOutScript , scripts [ 1 ] )
assert . strictEqual ( txb . __ INPUTS [ 0 ] . prevOutScript , scripts [ 1 ] )
} )
} )
it ( 'accepts a prevTx, index [and sequence number]' , function ( ) {
it ( 'accepts a prevTx, index [and sequence number]' , ( ) => {
const prevTx = new Transaction ( )
const prevTx = new Transaction ( )
prevTx . addOutput ( scripts [ 0 ] , 0 )
prevTx . addOutput ( scripts [ 0 ] , 0 )
prevTx . addOutput ( scripts [ 1 ] , 1 )
prevTx . addOutput ( scripts [ 1 ] , 1 )
@ -218,116 +218,116 @@ describe('TransactionBuilder', function () {
assert . strictEqual ( vin , 0 )
assert . strictEqual ( vin , 0 )
const txIn = txb . __ TX . ins [ 0 ]
const txIn = txb . __ TX . ins [ 0 ]
assert . deepEqual ( txIn . hash , prevTx . getHash ( ) )
assert . deepStrict Equal ( txIn . hash , prevTx . getHash ( ) )
assert . strictEqual ( txIn . index , 1 )
assert . strictEqual ( txIn . index , 1 )
assert . strictEqual ( txIn . sequence , 54 )
assert . strictEqual ( txIn . sequence , 54 )
assert . strictEqual ( txb . __ INPUTS [ 0 ] . prevOutScript , scripts [ 1 ] )
assert . strictEqual ( txb . __ INPUTS [ 0 ] . prevOutScript , scripts [ 1 ] )
} )
} )
it ( 'returns the input index' , function ( ) {
it ( 'returns the input index' , ( ) => {
assert . strictEqual ( txb . addInput ( txHash , 0 ) , 0 )
assert . strictEqual ( txb . addInput ( txHash , 0 ) , 0 )
assert . strictEqual ( txb . addInput ( txHash , 1 ) , 1 )
assert . strictEqual ( txb . addInput ( txHash , 1 ) , 1 )
} )
} )
it ( 'throws if SIGHASH_ALL has been used to sign any existing scriptSigs' , function ( ) {
it ( 'throws if SIGHASH_ALL has been used to sign any existing scriptSigs' , ( ) => {
txb . addInput ( txHash , 0 )
txb . addInput ( txHash , 0 )
txb . addOutput ( scripts [ 0 ] , 1000 )
txb . addOutput ( scripts [ 0 ] , 1000 )
txb . sign ( 0 , keyPair )
txb . sign ( 0 , keyPair )
assert . throws ( function ( ) {
assert . throws ( ( ) => {
txb . addInput ( txHash , 0 )
txb . addInput ( txHash , 0 )
} , /No, this would invalidate signatures/ )
} , /No, this would invalidate signatures/ )
} )
} )
} )
} )
describe ( 'addOutput' , function ( ) {
describe ( 'addOutput' , ( ) => {
let txb
let txb
beforeEach ( function ( ) {
beforeEach ( ( ) => {
txb = new TransactionBuilder ( )
txb = new TransactionBuilder ( )
} )
} )
it ( 'accepts an address string and value' , function ( ) {
it ( 'accepts an address string and value' , ( ) => {
const { address } = payments . p2pkh ( { pubkey : keyPair . publicKey } )
const { address } = payments . p2pkh ( { pubkey : keyPair . publicKey } )
const vout = txb . addOutput ( address , 1000 )
const vout = txb . addOutput ( address , 1000 )
assert . strictEqual ( vout , 0 )
assert . strictEqual ( vout , 0 )
const txout = txb . __ TX . outs [ 0 ]
const txout = txb . __ TX . outs [ 0 ]
assert . deepEqual ( txout . script , scripts [ 0 ] )
assert . deepStrict Equal ( txout . script , scripts [ 0 ] )
assert . strictEqual ( txout . value , 1000 )
assert . strictEqual ( txout . value , 1000 )
} )
} )
it ( 'accepts a ScriptPubKey and value' , function ( ) {
it ( 'accepts a ScriptPubKey and value' , ( ) => {
const vout = txb . addOutput ( scripts [ 0 ] , 1000 )
const vout = txb . addOutput ( scripts [ 0 ] , 1000 )
assert . strictEqual ( vout , 0 )
assert . strictEqual ( vout , 0 )
const txout = txb . __ TX . outs [ 0 ]
const txout = txb . __ TX . outs [ 0 ]
assert . deepEqual ( txout . script , scripts [ 0 ] )
assert . deepStrict Equal ( txout . script , scripts [ 0 ] )
assert . strictEqual ( txout . value , 1000 )
assert . strictEqual ( txout . value , 1000 )
} )
} )
it ( 'throws if address is of the wrong network' , function ( ) {
it ( 'throws if address is of the wrong network' , ( ) => {
assert . throws ( function ( ) {
assert . throws ( ( ) => {
txb . addOutput ( '2NGHjvjw83pcVFgMcA7QvSMh2c246rxLVz9' , 1000 )
txb . addOutput ( '2NGHjvjw83pcVFgMcA7QvSMh2c246rxLVz9' , 1000 )
} , /2NGHjvjw83pcVFgMcA7QvSMh2c246rxLVz9 has no matching Script/ )
} , /2NGHjvjw83pcVFgMcA7QvSMh2c246rxLVz9 has no matching Script/ )
} )
} )
it ( 'add second output after signed first input with SIGHASH_NONE' , function ( ) {
it ( 'add second output after signed first input with SIGHASH_NONE' , ( ) => {
txb . addInput ( txHash , 0 )
txb . addInput ( txHash , 0 )
txb . addOutput ( scripts [ 0 ] , 2000 )
txb . addOutput ( scripts [ 0 ] , 2000 )
txb . sign ( 0 , keyPair , undefined , Transaction . SIGHASH_NONE )
txb . sign ( 0 , keyPair , undefined , Transaction . SIGHASH_NONE )
assert . e qual( txb . addOutput ( scripts [ 1 ] , 9000 ) , 1 )
assert . strictE qual( txb . addOutput ( scripts [ 1 ] , 9000 ) , 1 )
} )
} )
it ( 'add first output after signed first input with SIGHASH_NONE' , function ( ) {
it ( 'add first output after signed first input with SIGHASH_NONE' , ( ) => {
txb . addInput ( txHash , 0 )
txb . addInput ( txHash , 0 )
txb . sign ( 0 , keyPair , undefined , Transaction . SIGHASH_NONE )
txb . sign ( 0 , keyPair , undefined , Transaction . SIGHASH_NONE )
assert . e qual( txb . addOutput ( scripts [ 0 ] , 2000 ) , 0 )
assert . strictE qual( txb . addOutput ( scripts [ 0 ] , 2000 ) , 0 )
} )
} )
it ( 'add second output after signed first input with SIGHASH_SINGLE' , function ( ) {
it ( 'add second output after signed first input with SIGHASH_SINGLE' , ( ) => {
txb . addInput ( txHash , 0 )
txb . addInput ( txHash , 0 )
txb . addOutput ( scripts [ 0 ] , 2000 )
txb . addOutput ( scripts [ 0 ] , 2000 )
txb . sign ( 0 , keyPair , undefined , Transaction . SIGHASH_SINGLE )
txb . sign ( 0 , keyPair , undefined , Transaction . SIGHASH_SINGLE )
assert . e qual( txb . addOutput ( scripts [ 1 ] , 9000 ) , 1 )
assert . strictE qual( txb . addOutput ( scripts [ 1 ] , 9000 ) , 1 )
} )
} )
it ( 'add first output after signed first input with SIGHASH_SINGLE' , function ( ) {
it ( 'add first output after signed first input with SIGHASH_SINGLE' , ( ) => {
txb . addInput ( txHash , 0 )
txb . addInput ( txHash , 0 )
txb . sign ( 0 , keyPair , undefined , Transaction . SIGHASH_SINGLE )
txb . sign ( 0 , keyPair , undefined , Transaction . SIGHASH_SINGLE )
assert . throws ( function ( ) {
assert . throws ( ( ) => {
txb . addOutput ( scripts [ 0 ] , 2000 )
txb . addOutput ( scripts [ 0 ] , 2000 )
} , /No, this would invalidate signatures/ )
} , /No, this would invalidate signatures/ )
} )
} )
it ( 'throws if SIGHASH_ALL has been used to sign any existing scriptSigs' , function ( ) {
it ( 'throws if SIGHASH_ALL has been used to sign any existing scriptSigs' , ( ) => {
txb . addInput ( txHash , 0 )
txb . addInput ( txHash , 0 )
txb . addOutput ( scripts [ 0 ] , 2000 )
txb . addOutput ( scripts [ 0 ] , 2000 )
txb . sign ( 0 , keyPair )
txb . sign ( 0 , keyPair )
assert . throws ( function ( ) {
assert . throws ( ( ) => {
txb . addOutput ( scripts [ 1 ] , 9000 )
txb . addOutput ( scripts [ 1 ] , 9000 )
} , /No, this would invalidate signatures/ )
} , /No, this would invalidate signatures/ )
} )
} )
} )
} )
describe ( 'setLockTime' , function ( ) {
describe ( 'setLockTime' , ( ) => {
it ( 'throws if if there exist any scriptSigs' , function ( ) {
it ( 'throws if if there exist any scriptSigs' , ( ) => {
const txb = new TransactionBuilder ( )
const txb = new TransactionBuilder ( )
txb . addInput ( txHash , 0 )
txb . addInput ( txHash , 0 )
txb . addOutput ( scripts [ 0 ] , 100 )
txb . addOutput ( scripts [ 0 ] , 100 )
txb . sign ( 0 , keyPair )
txb . sign ( 0 , keyPair )
assert . throws ( function ( ) {
assert . throws ( ( ) => {
txb . setLockTime ( 65535 )
txb . setLockTime ( 65535 )
} , /No, this would invalidate signatures/ )
} , /No, this would invalidate signatures/ )
} )
} )
} )
} )
describe ( 'sign' , function ( ) {
describe ( 'sign' , ( ) => {
it ( 'supports the alternative abstract interface { publicKey, sign }' , function ( ) {
it ( 'supports the alternative abstract interface { publicKey, sign }' , ( ) => {
const keyPair = {
const keyPair = {
publicKey : ECPair . makeRandom ( { rng : function ( ) { return Buffer . alloc ( 32 , 1 ) } } ) . publicKey ,
publicKey : ECPair . makeRandom ( { rng : ( ) => { return Buffer . alloc ( 32 , 1 ) } } ) . publicKey ,
sign : function ( hash ) { return Buffer . alloc ( 64 , 0x5f ) }
sign : hash => { return Buffer . alloc ( 64 , 0x5f ) }
}
}
const txb = new TransactionBuilder ( )
const txb = new TransactionBuilder ( )
@ -335,16 +335,16 @@ describe('TransactionBuilder', function () {
txb . addInput ( 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' , 1 )
txb . addInput ( 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' , 1 )
txb . addOutput ( '1111111111111111111114oLvT2' , 100000 )
txb . addOutput ( '1111111111111111111114oLvT2' , 100000 )
txb . sign ( 0 , keyPair )
txb . sign ( 0 , keyPair )
assert . e qual( txb . build ( ) . toHex ( ) , '0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff010000006a47304402205f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f02205f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f0121031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078fffffffff01a0860100000000001976a914000000000000000000000000000000000000000088ac00000000' )
assert . strictE qual( txb . build ( ) . toHex ( ) , '0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff010000006a47304402205f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f02205f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f0121031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078fffffffff01a0860100000000001976a914000000000000000000000000000000000000000088ac00000000' )
} )
} )
fixtures . invalid . sign . forEach ( function ( f ) {
fixtures . invalid . sign . forEach ( f => {
it ( 'throws ' + f . exception + ( f . description ? ' (' + f . description + ')' : '' ) , function ( ) {
it ( 'throws ' + f . exception + ( f . description ? ' (' + f . description + ')' : '' ) , ( ) => {
const txb = construct ( f , true )
const txb = construct ( f , true )
let threw = false
let threw = false
f . inputs . forEach ( function ( input , index ) {
f . inputs . forEach ( ( input , index ) => {
input . signs . forEach ( function ( sign ) {
input . signs . forEach ( sign => {
const keyPairNetwork = NETWORKS [ sign . network || f . network ]
const keyPairNetwork = NETWORKS [ sign . network || f . network ]
const keyPair2 = ECPair . fromWIF ( sign . keyPair , keyPairNetwork )
const keyPair2 = ECPair . fromWIF ( sign . keyPair , keyPairNetwork )
let redeemScript
let redeemScript
@ -359,7 +359,7 @@ describe('TransactionBuilder', function () {
}
}
if ( sign . throws ) {
if ( sign . throws ) {
assert . throws ( function ( ) {
assert . throws ( ( ) => {
txb . sign ( index , keyPair2 , redeemScript , sign . hashType , sign . value , witnessScript )
txb . sign ( index , keyPair2 , redeemScript , sign . hashType , sign . value , witnessScript )
} , new RegExp ( f . exception ) )
} , new RegExp ( f . exception ) )
threw = true
threw = true
@ -369,14 +369,14 @@ describe('TransactionBuilder', function () {
} )
} )
} )
} )
assert . e qual( threw , true )
assert . strictE qual( threw , true )
} )
} )
} )
} )
} )
} )
describe ( 'build' , function ( ) {
describe ( 'build' , ( ) => {
fixtures . valid . build . forEach ( function ( f ) {
fixtures . valid . build . forEach ( f => {
it ( 'builds "' + f . description + '"' , function ( ) {
it ( 'builds "' + f . description + '"' , ( ) => {
const txb = construct ( f )
const txb = construct ( f )
const tx = f . incomplete ? txb . buildIncomplete ( ) : txb . build ( )
const tx = f . incomplete ? txb . buildIncomplete ( ) : txb . build ( )
@ -385,10 +385,10 @@ describe('TransactionBuilder', function () {
} )
} )
// TODO: remove duplicate test code
// TODO: remove duplicate test code
fixtures . invalid . build . forEach ( function ( f ) {
fixtures . invalid . build . forEach ( f => {
describe ( 'for ' + ( f . description || f . exception ) , function ( ) {
describe ( 'for ' + ( f . description || f . exception ) , ( ) => {
it ( 'throws ' + f . exception , function ( ) {
it ( 'throws ' + f . exception , ( ) => {
assert . throws ( function ( ) {
assert . throws ( ( ) => {
let txb
let txb
if ( f . txHex ) {
if ( f . txHex ) {
txb = TransactionBuilder . fromTransaction ( Transaction . fromHex ( f . txHex ) )
txb = TransactionBuilder . fromTransaction ( Transaction . fromHex ( f . txHex ) )
@ -402,8 +402,8 @@ describe('TransactionBuilder', function () {
// if throws on incomplete too, enforce that
// if throws on incomplete too, enforce that
if ( f . incomplete ) {
if ( f . incomplete ) {
it ( 'throws ' + f . exception , function ( ) {
it ( 'throws ' + f . exception , ( ) => {
assert . throws ( function ( ) {
assert . throws ( ( ) => {
let txb
let txb
if ( f . txHex ) {
if ( f . txHex ) {
txb = TransactionBuilder . fromTransaction ( Transaction . fromHex ( f . txHex ) )
txb = TransactionBuilder . fromTransaction ( Transaction . fromHex ( f . txHex ) )
@ -415,7 +415,7 @@ describe('TransactionBuilder', function () {
} , new RegExp ( f . exception ) )
} , new RegExp ( f . exception ) )
} )
} )
} else {
} else {
it ( 'does not throw if buildIncomplete' , function ( ) {
it ( 'does not throw if buildIncomplete' , ( ) => {
let txb
let txb
if ( f . txHex ) {
if ( f . txHex ) {
txb = TransactionBuilder . fromTransaction ( Transaction . fromHex ( f . txHex ) )
txb = TransactionBuilder . fromTransaction ( Transaction . fromHex ( f . txHex ) )
@ -429,7 +429,7 @@ describe('TransactionBuilder', function () {
} )
} )
} )
} )
it ( 'for incomplete with 0 signatures' , function ( ) {
it ( 'for incomplete with 0 signatures' , ( ) => {
const randomTxData = '0100000000010100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff01e8030000000000001976a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac02483045022100aa5d8aa40a90f23ce2c3d11bc845ca4a12acd99cbea37de6b9f6d86edebba8cb022022dedc2aa0a255f74d04c0b76ece2d7c691f9dd11a64a8ac49f62a99c3a05f9d01232103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc71ac00000000'
const randomTxData = '0100000000010100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff01e8030000000000001976a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac02483045022100aa5d8aa40a90f23ce2c3d11bc845ca4a12acd99cbea37de6b9f6d86edebba8cb022022dedc2aa0a255f74d04c0b76ece2d7c691f9dd11a64a8ac49f62a99c3a05f9d01232103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc71ac00000000'
const randomAddress = '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH'
const randomAddress = '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH'
@ -441,7 +441,7 @@ describe('TransactionBuilder', function () {
assert ( tx )
assert ( tx )
} )
} )
it ( 'for incomplete P2SH with 0 signatures' , function ( ) {
it ( 'for incomplete P2SH with 0 signatures' , ( ) => {
const inp = Buffer . from ( '010000000173120703f67318aef51f7251272a6816d3f7523bb25e34b136d80be959391c100000000000ffffffff0100c817a80400000017a91471a8ec07ff69c6c4fee489184c462a9b1b9237488700000000' , 'hex' ) // arbitrary P2SH input
const inp = Buffer . from ( '010000000173120703f67318aef51f7251272a6816d3f7523bb25e34b136d80be959391c100000000000ffffffff0100c817a80400000017a91471a8ec07ff69c6c4fee489184c462a9b1b9237488700000000' , 'hex' ) // arbitrary P2SH input
const inpTx = Transaction . fromBuffer ( inp )
const inpTx = Transaction . fromBuffer ( inp )
@ -452,7 +452,7 @@ describe('TransactionBuilder', function () {
txb . buildIncomplete ( )
txb . buildIncomplete ( )
} )
} )
it ( 'for incomplete P2WPKH with 0 signatures' , function ( ) {
it ( 'for incomplete P2WPKH with 0 signatures' , ( ) => {
const inp = Buffer . from ( '010000000173120703f67318aef51f7251272a6816d3f7523bb25e34b136d80be959391c100000000000ffffffff0100c817a8040000001600141a15805e1f4040c9f68ccc887fca2e63547d794b00000000' , 'hex' )
const inp = Buffer . from ( '010000000173120703f67318aef51f7251272a6816d3f7523bb25e34b136d80be959391c100000000000ffffffff0100c817a8040000001600141a15805e1f4040c9f68ccc887fca2e63547d794b00000000' , 'hex' )
const inpTx = Transaction . fromBuffer ( inp )
const inpTx = Transaction . fromBuffer ( inp )
@ -463,7 +463,7 @@ describe('TransactionBuilder', function () {
txb . buildIncomplete ( )
txb . buildIncomplete ( )
} )
} )
it ( 'for incomplete P2WSH with 0 signatures' , function ( ) {
it ( 'for incomplete P2WSH with 0 signatures' , ( ) => {
const inpTx = Transaction . fromBuffer ( Buffer . from ( '010000000173120703f67318aef51f7251272a6816d3f7523bb25e34b136d80be959391c100000000000ffffffff0100c817a80400000022002072df76fcc0b231b94bdf7d8c25d7eef4716597818d211e19ade7813bff7a250200000000' , 'hex' ) )
const inpTx = Transaction . fromBuffer ( Buffer . from ( '010000000173120703f67318aef51f7251272a6816d3f7523bb25e34b136d80be959391c100000000000ffffffff0100c817a80400000022002072df76fcc0b231b94bdf7d8c25d7eef4716597818d211e19ade7813bff7a250200000000' , 'hex' ) )
const txb = new TransactionBuilder ( NETWORKS . testnet )
const txb = new TransactionBuilder ( NETWORKS . testnet )
@ -474,17 +474,17 @@ describe('TransactionBuilder', function () {
} )
} )
} )
} )
describe ( 'multisig' , function ( ) {
describe ( 'multisig' , ( ) => {
fixtures . valid . multisig . forEach ( function ( f ) {
fixtures . valid . multisig . forEach ( f => {
it ( f . description , function ( ) {
it ( f . description , ( ) => {
const network = NETWORKS [ f . network ]
const network = NETWORKS [ f . network ]
let txb = construct ( f , true )
let txb = construct ( f , true )
let tx
let tx
f . inputs . forEach ( function ( input , i ) {
f . inputs . forEach ( ( input , i ) => {
const redeemScript = bscript . fromASM ( input . redeemScript )
const redeemScript = bscript . fromASM ( input . redeemScript )
input . signs . forEach ( function ( sign ) {
input . signs . forEach ( sign => {
// rebuild the transaction each-time after the first
// rebuild the transaction each-time after the first
if ( tx ) {
if ( tx ) {
// manually override the scriptSig?
// manually override the scriptSig?
@ -513,10 +513,10 @@ describe('TransactionBuilder', function () {
} )
} )
} )
} )
describe ( 'various edge case' , function ( ) {
describe ( 'various edge case' , ( ) => {
const network = NETWORKS . testnet
const network = NETWORKS . testnet
it ( 'should warn of high fee for segwit transaction based on VSize, not Size' , function ( ) {
it ( 'should warn of high fee for segwit transaction based on VSize, not Size' , ( ) => {
const rawtx = '01000000000104fdaac89627208b4733484ca56bc291f4cf4fa8d7c5f29893c52b46788a0a' +
const rawtx = '01000000000104fdaac89627208b4733484ca56bc291f4cf4fa8d7c5f29893c52b46788a0a' +
'1df90000000000fffffffffdaac89627208b4733484ca56bc291f4cf4fa8d7c5f29893c52b46788a0a1df9' +
'1df90000000000fffffffffdaac89627208b4733484ca56bc291f4cf4fa8d7c5f29893c52b46788a0a1df9' +
'0100000000ffffffffa2ef7aaab316a3e5b5b0a78d1d35c774b95a079f9f0c762277a49caf1f26bca40000' +
'0100000000ffffffffa2ef7aaab316a3e5b5b0a78d1d35c774b95a079f9f0c762277a49caf1f26bca40000' +
@ -538,12 +538,12 @@ describe('TransactionBuilder', function () {
txb . __ INPUTS [ 2 ] . value = 248920
txb . __ INPUTS [ 2 ] . value = 248920
txb . __ INPUTS [ 3 ] . value = 248920
txb . __ INPUTS [ 3 ] . value = 248920
assert . throws ( function ( ) {
assert . throws ( ( ) => {
txb . build ( )
txb . build ( )
} , new RegExp ( 'Transaction has absurd fees' ) )
} , new RegExp ( 'Transaction has absurd fees' ) )
} )
} )
it ( 'should classify witness inputs with witness = true during multisigning' , function ( ) {
it ( 'should classify witness inputs with witness = true during multisigning' , ( ) => {
const keyPair = ECPair . fromWIF ( 'cRAwuVuVSBZMPu7hdrYvMCZ8eevzmkExjFbaBLhqnDdrezxN3nTS' , network )
const keyPair = ECPair . fromWIF ( 'cRAwuVuVSBZMPu7hdrYvMCZ8eevzmkExjFbaBLhqnDdrezxN3nTS' , network )
const witnessScript = Buffer . from ( '522102bbbd6eb01efcbe4bd9664b886f26f69de5afcb2e479d72596c8bf21929e352e22102d9c3f7180ef13ec5267723c9c2ffab56a4215241f837502ea8977c8532b9ea1952ae' , 'hex' )
const witnessScript = Buffer . from ( '522102bbbd6eb01efcbe4bd9664b886f26f69de5afcb2e479d72596c8bf21929e352e22102d9c3f7180ef13ec5267723c9c2ffab56a4215241f837502ea8977c8532b9ea1952ae' , 'hex' )
const redeemScript = Buffer . from ( '002024376a0a9abab599d0e028248d48ebe817bc899efcffa1cd2984d67289daf5af' , 'hex' )
const redeemScript = Buffer . from ( '002024376a0a9abab599d0e028248d48ebe817bc899efcffa1cd2984d67289daf5af' , 'hex' )
@ -558,13 +558,13 @@ describe('TransactionBuilder', function () {
const tx = txb . buildIncomplete ( )
const tx = txb . buildIncomplete ( )
// Only input is segwit, so txid should be accurate with the final tx
// Only input is segwit, so txid should be accurate with the final tx
assert . e qual( tx . getId ( ) , 'f15d0a65b21b4471405b21a099f8b18e1ae4d46d55efbd0f4766cf11ad6cb821' )
assert . strictE qual( tx . getId ( ) , 'f15d0a65b21b4471405b21a099f8b18e1ae4d46d55efbd0f4766cf11ad6cb821' )
const txHex = tx . toHex ( )
const txHex = tx . toHex ( )
TransactionBuilder . fromTransaction ( Transaction . fromHex ( txHex ) )
TransactionBuilder . fromTransaction ( Transaction . fromHex ( txHex ) )
} )
} )
it ( 'should handle badly pre-filled OP_0s' , function ( ) {
it ( 'should handle badly pre-filled OP_0s' , ( ) => {
// OP_0 is used where a signature is missing
// OP_0 is used where a signature is missing
const redeemScripSig = bscript . fromASM ( 'OP_0 OP_0 3045022100daf0f4f3339d9fbab42b098045c1e4958ee3b308f4ae17be80b63808558d0adb02202f07e3d1f79dc8da285ae0d7f68083d769c11f5621ebd9691d6b48c0d4283d7d01 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a4104f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e67253ae' )
const redeemScripSig = bscript . fromASM ( 'OP_0 OP_0 3045022100daf0f4f3339d9fbab42b098045c1e4958ee3b308f4ae17be80b63808558d0adb02202f07e3d1f79dc8da285ae0d7f68083d769c11f5621ebd9691d6b48c0d4283d7d01 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a4104f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e67253ae' )
const redeemScript = bscript . fromASM ( 'OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG' )
const redeemScript = bscript . fromASM ( 'OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG' )
@ -580,11 +580,11 @@ describe('TransactionBuilder', function () {
txb . sign ( 0 , keyPair2 , redeemScript )
txb . sign ( 0 , keyPair2 , redeemScript )
const tx2 = txb . build ( )
const tx2 = txb . build ( )
assert . e qual( tx2 . getId ( ) , 'eab59618a564e361adef6d918bd792903c3d41bcf1220137364fb847880467f9' )
assert . strictE qual( tx2 . getId ( ) , 'eab59618a564e361adef6d918bd792903c3d41bcf1220137364fb847880467f9' )
assert . e qual( bscript . toASM ( tx2 . ins [ 0 ] . script ) , 'OP_0 3045022100daf0f4f3339d9fbab42b098045c1e4958ee3b308f4ae17be80b63808558d0adb02202f07e3d1f79dc8da285ae0d7f68083d769c11f5621ebd9691d6b48c0d4283d7d01 3045022100a346c61738304eac5e7702188764d19cdf68f4466196729db096d6c87ce18cdd022018c0e8ad03054b0e7e235cda6bedecf35881d7aa7d94ff425a8ace7220f38af001 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a4104f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e67253ae' )
assert . strictE qual( bscript . toASM ( tx2 . ins [ 0 ] . script ) , 'OP_0 3045022100daf0f4f3339d9fbab42b098045c1e4958ee3b308f4ae17be80b63808558d0adb02202f07e3d1f79dc8da285ae0d7f68083d769c11f5621ebd9691d6b48c0d4283d7d01 3045022100a346c61738304eac5e7702188764d19cdf68f4466196729db096d6c87ce18cdd022018c0e8ad03054b0e7e235cda6bedecf35881d7aa7d94ff425a8ace7220f38af001 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a4104f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e67253ae' )
} )
} )
it ( 'should not classify blank scripts as nonstandard' , function ( ) {
it ( 'should not classify blank scripts as nonstandard' , ( ) => {
let txb = new TransactionBuilder ( )
let txb = new TransactionBuilder ( )
txb . setVersion ( 1 )
txb . setVersion ( 1 )
txb . addInput ( 'aa94ab02c182214f090e99a0d57021caffd0f195a81c24602b1028b130b63e31' , 0 )
txb . addInput ( 'aa94ab02c182214f090e99a0d57021caffd0f195a81c24602b1028b130b63e31' , 0 )
@ -596,14 +596,14 @@ describe('TransactionBuilder', function () {
txb . addOutput ( '1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK' , 15000 )
txb . addOutput ( '1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK' , 15000 )
txb . sign ( 0 , keyPair )
txb . sign ( 0 , keyPair )
const txId = txb . build ( ) . getId ( )
const txId = txb . build ( ) . getId ( )
assert . e qual( txId , '54f097315acbaedb92a95455da3368eb45981cdae5ffbc387a9afc872c0f29b3' )
assert . strictE qual( txId , '54f097315acbaedb92a95455da3368eb45981cdae5ffbc387a9afc872c0f29b3' )
// and, repeat
// and, repeat
txb = TransactionBuilder . fromTransaction ( Transaction . fromHex ( incomplete ) )
txb = TransactionBuilder . fromTransaction ( Transaction . fromHex ( incomplete ) )
txb . addOutput ( '1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK' , 15000 )
txb . addOutput ( '1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK' , 15000 )
txb . sign ( 0 , keyPair )
txb . sign ( 0 , keyPair )
const txId2 = txb . build ( ) . getId ( )
const txId2 = txb . build ( ) . getId ( )
assert . e qual( txId , txId2 )
assert . strictE qual( txId , txId2 )
} )
} )
} )
} )
} )
} )