22
33var assert = require ( 'assert' )
44var ops = require ( '../src/opcodes' )
5- var scripts = require ( '../src/scripts' )
5+ var script = require ( '../src/scripts' )
66
77var Address = require ( '../src/address' )
88var BigInteger = require ( 'bigi' )
@@ -21,16 +21,14 @@ function construct (f, sign) {
2121 var prevTxScript
2222
2323 if ( input . prevTxScript ) {
24- prevTxScript = scripts . fromASM ( input . prevTxScript )
24+ prevTxScript = script . fromASM ( input . prevTxScript )
2525 }
2626
2727 txb . addInput ( input . txId , input . vout , input . sequence , prevTxScript )
2828 } )
2929
3030 f . outputs . forEach ( function ( output ) {
31- var script = scripts . fromASM ( output . script )
32-
33- txb . addOutput ( script , output . value )
31+ txb . addOutput ( script . fromASM ( output . script ) , output . value )
3432 } )
3533
3634 if ( sign === undefined || sign ) {
@@ -40,7 +38,7 @@ function construct (f, sign) {
4038 var redeemScript
4139
4240 if ( sign . redeemScript ) {
43- redeemScript = scripts . fromASM ( sign . redeemScript )
41+ redeemScript = script . fromASM ( sign . redeemScript )
4442 }
4543
4644 txb . sign ( index , keyPair , redeemScript , sign . hashType )
@@ -60,23 +58,20 @@ function construct (f, sign) {
6058 return txb
6159}
6260
63- describe ( 'TransactionBuilder' , function ( ) {
64- var privAddress , privScript
65- var prevTx , prevTxHash
66- var keyPair
61+ describe . only ( 'TransactionBuilder' , function ( ) {
62+ // constants
63+ var keyPair = new ECPair ( BigInteger . ONE )
64+ var scripts = [
65+ '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH' ,
66+ '1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP'
67+ ] . map ( function ( x ) {
68+ return Address . toOutputScript ( x )
69+ } )
70+ var txHash = new Buffer ( '0e7cea811c0be9f73c0aca591034396e7264473fc25c1ca45195d7417b36cbe2' , 'hex' )
6771 var txb
6872
6973 beforeEach ( function ( ) {
7074 txb = new TransactionBuilder ( )
71-
72- prevTx = new Transaction ( )
73- prevTx . addOutput ( Address . toOutputScript ( '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH' ) , 0 )
74- prevTx . addOutput ( Address . toOutputScript ( '1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP' ) , 1 )
75- prevTxHash = prevTx . getHash ( )
76-
77- keyPair = new ECPair ( BigInteger . ONE )
78- privAddress = keyPair . getAddress ( )
79- privScript = Address . toOutputScript ( privAddress )
8075 } )
8176
8277 describe ( 'fromTransaction' , function ( ) {
@@ -104,69 +99,73 @@ describe('TransactionBuilder', function () {
10499
105100 describe ( 'addInput' , function ( ) {
106101 it ( 'accepts a txHash, index [and sequence number]' , function ( ) {
107- var vin = txb . addInput ( prevTxHash , 1 , 54 )
102+ var vin = txb . addInput ( txHash , 1 , 54 )
108103 assert . strictEqual ( vin , 0 )
109104
110105 var txIn = txb . tx . ins [ 0 ]
111- assert . strictEqual ( txIn . hash , prevTxHash )
106+ assert . strictEqual ( txIn . hash , txHash )
112107 assert . strictEqual ( txIn . index , 1 )
113108 assert . strictEqual ( txIn . sequence , 54 )
114109 assert . strictEqual ( txb . inputs [ 0 ] . prevOutScript , undefined )
115110 } )
116111
117112 it ( 'accepts a txHash, index [, sequence number and scriptPubKey]' , function ( ) {
118- var vin = txb . addInput ( prevTxHash , 1 , 54 , prevTx . outs [ 1 ] . script )
113+ var vin = txb . addInput ( txHash , 1 , 54 , scripts [ 1 ] )
119114 assert . strictEqual ( vin , 0 )
120115
121116 var txIn = txb . tx . ins [ 0 ]
122- assert . strictEqual ( txIn . hash , prevTxHash )
117+ assert . strictEqual ( txIn . hash , txHash )
123118 assert . strictEqual ( txIn . index , 1 )
124119 assert . strictEqual ( txIn . sequence , 54 )
125- assert . strictEqual ( txb . inputs [ 0 ] . prevOutScript , prevTx . outs [ 1 ] . script )
120+ assert . strictEqual ( txb . inputs [ 0 ] . prevOutScript , scripts [ 1 ] )
126121 } )
127122
128123 it ( 'accepts a prevTx, index [and sequence number]' , function ( ) {
124+ var prevTx = new Transaction ( )
125+ prevTx . addOutput ( scripts [ 0 ] , 0 )
126+ prevTx . addOutput ( scripts [ 1 ] , 1 )
127+
129128 var vin = txb . addInput ( prevTx , 1 , 54 )
130129 assert . strictEqual ( vin , 0 )
131130
132131 var txIn = txb . tx . ins [ 0 ]
133- assert . deepEqual ( txIn . hash , prevTxHash )
132+ assert . deepEqual ( txIn . hash , prevTx . getHash ( ) )
134133 assert . strictEqual ( txIn . index , 1 )
135134 assert . strictEqual ( txIn . sequence , 54 )
136- assert . strictEqual ( txb . inputs [ 0 ] . prevOutScript , prevTx . outs [ 1 ] . script )
135+ assert . strictEqual ( txb . inputs [ 0 ] . prevOutScript , scripts [ 1 ] )
137136 } )
138137
139138 it ( 'returns the input index' , function ( ) {
140- assert . strictEqual ( txb . addInput ( prevTxHash , 0 ) , 0 )
141- assert . strictEqual ( txb . addInput ( prevTxHash , 1 ) , 1 )
139+ assert . strictEqual ( txb . addInput ( txHash , 0 ) , 0 )
140+ assert . strictEqual ( txb . addInput ( txHash , 1 ) , 1 )
142141 } )
143142
144143 it ( 'throws if SIGHASH_ALL has been used to sign any existing scriptSigs' , function ( ) {
145- txb . addInput ( prevTxHash , 0 )
144+ txb . addInput ( txHash , 0 )
146145 txb . sign ( 0 , keyPair )
147146
148147 assert . throws ( function ( ) {
149- txb . addInput ( prevTxHash , 0 )
148+ txb . addInput ( txHash , 0 )
150149 } , / N o , t h i s w o u l d i n v a l i d a t e s i g n a t u r e s / )
151150 } )
152151 } )
153152
154153 describe ( 'addOutput' , function ( ) {
155154 it ( 'accepts an address string and value' , function ( ) {
156- var vout = txb . addOutput ( privAddress , 1000 )
155+ var vout = txb . addOutput ( keyPair . getAddress ( ) , 1000 )
157156 assert . strictEqual ( vout , 0 )
158157
159158 var txout = txb . tx . outs [ 0 ]
160- assert . deepEqual ( txout . script , privScript )
159+ assert . deepEqual ( txout . script , scripts [ 0 ] )
161160 assert . strictEqual ( txout . value , 1000 )
162161 } )
163162
164163 it ( 'accepts a ScriptPubKey and value' , function ( ) {
165- var vout = txb . addOutput ( privScript , 1000 )
164+ var vout = txb . addOutput ( scripts [ 0 ] , 1000 )
166165 assert . strictEqual ( vout , 0 )
167166
168167 var txout = txb . tx . outs [ 0 ]
169- assert . deepEqual ( txout . script , privScript )
168+ assert . deepEqual ( txout . script , scripts [ 0 ] )
170169 assert . strictEqual ( txout . value , 1000 )
171170 } )
172171
@@ -177,12 +176,12 @@ describe('TransactionBuilder', function () {
177176 } )
178177
179178 it ( 'throws if SIGHASH_ALL has been used to sign any existing scriptSigs' , function ( ) {
180- txb . addInput ( prevTxHash , 0 )
181- txb . addOutput ( privScript , 2000 )
179+ txb . addInput ( txHash , 0 )
180+ txb . addOutput ( scripts [ 0 ] , 2000 )
182181 txb . sign ( 0 , keyPair )
183182
184183 assert . throws ( function ( ) {
185- txb . addOutput ( privScript , 9000 )
184+ txb . addOutput ( scripts [ 1 ] , 9000 )
186185 } , / N o , t h i s w o u l d i n v a l i d a t e s i g n a t u r e s / )
187186 } )
188187 } )
@@ -199,7 +198,7 @@ describe('TransactionBuilder', function () {
199198 var redeemScript
200199
201200 if ( sign . redeemScript ) {
202- redeemScript = scripts . fromASM ( sign . redeemScript )
201+ redeemScript = script . fromASM ( sign . redeemScript )
203202 }
204203
205204 if ( ! sign . throws ) {
@@ -261,7 +260,7 @@ describe('TransactionBuilder', function () {
261260 var network = NETWORKS [ f . network ]
262261
263262 f . inputs . forEach ( function ( input , i ) {
264- var redeemScript = scripts . fromASM ( input . redeemScript )
263+ var redeemScript = script . fromASM ( input . redeemScript )
265264
266265 input . signs . forEach ( function ( sign ) {
267266 // rebuild the transaction each-time after the first
@@ -271,11 +270,11 @@ describe('TransactionBuilder', function () {
271270 var scriptSig = tx . ins [ i ] . script
272271
273272 // ignore OP_0 on the front, ignore redeemScript
274- var signatures = scripts . decompile ( scriptSig ) . slice ( 1 , - 1 ) . filter ( function ( x ) { return x !== ops . OP_0 } )
273+ var signatures = script . decompile ( scriptSig ) . slice ( 1 , - 1 ) . filter ( function ( x ) { return x !== ops . OP_0 } )
275274
276275 // rebuild/replace the scriptSig without them
277- var replacement = scripts . scriptHashInput ( scripts . multisigInput ( signatures ) , redeemScript )
278- assert . strictEqual ( scripts . toASM ( replacement ) , sign . scriptSigFiltered )
276+ var replacement = script . scriptHashInput ( script . multisigInput ( signatures ) , redeemScript )
277+ assert . strictEqual ( script . toASM ( replacement ) , sign . scriptSigFiltered )
279278
280279 tx . ins [ i ] . script = replacement
281280 }
@@ -291,7 +290,7 @@ describe('TransactionBuilder', function () {
291290 tx = txb . buildIncomplete ( )
292291
293292 // now verify the serialized scriptSig is as expected
294- assert . strictEqual ( scripts . toASM ( tx . ins [ i ] . script ) , sign . scriptSig )
293+ assert . strictEqual ( script . toASM ( tx . ins [ i ] . script ) , sign . scriptSig )
295294 } )
296295 } )
297296
@@ -308,7 +307,7 @@ describe('TransactionBuilder', function () {
308307
309308 txb = TransactionBuilder . fromTransaction ( lameTx , network )
310309
311- var redeemScript = scripts . fromASM ( 'OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG' )
310+ var redeemScript = script . fromASM ( 'OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG' )
312311
313312 var keyPair = ECPair . fromWIF ( '91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe' , network )
314313 txb . sign ( 0 , keyPair , redeemScript )
0 commit comments