@@ -36,6 +36,7 @@ use util::endian;
36
36
use blockdata:: constants:: WITNESS_SCALE_FACTOR ;
37
37
#[ cfg( feature="bitcoinconsensus" ) ] use blockdata:: script;
38
38
use blockdata:: script:: Script ;
39
+ use blockdata:: witness:: Witness ;
39
40
use consensus:: { encode, Decodable , Encodable } ;
40
41
use consensus:: encode:: MAX_VEC_SIZE ;
41
42
use hash_types:: { SigHash , Txid , Wtxid } ;
@@ -197,7 +198,7 @@ pub struct TxIn {
197
198
/// Encodable/Decodable, as it is (de)serialized at the end of the full
198
199
/// Transaction. It *is* (de)serialized with the rest of the TxIn in other
199
200
/// (de)serialization routines.
200
- pub witness : Vec < Vec < u8 > >
201
+ pub witness : Witness
201
202
}
202
203
203
204
impl Default for TxIn {
@@ -206,7 +207,7 @@ impl Default for TxIn {
206
207
previous_output : OutPoint :: default ( ) ,
207
208
script_sig : Script :: new ( ) ,
208
209
sequence : u32:: max_value ( ) ,
209
- witness : Vec :: new ( ) ,
210
+ witness : Witness :: default ( ) ,
210
211
}
211
212
}
212
213
}
@@ -280,7 +281,7 @@ impl Transaction {
280
281
let cloned_tx = Transaction {
281
282
version : self . version ,
282
283
lock_time : self . lock_time ,
283
- input : self . input . iter ( ) . map ( |txin| TxIn { script_sig : Script :: new ( ) , witness : vec ! [ ] , .. * txin } ) . collect ( ) ,
284
+ input : self . input . iter ( ) . map ( |txin| TxIn { script_sig : Script :: new ( ) , witness : Witness :: default ( ) , .. * txin } ) . collect ( ) ,
284
285
output : self . output . clone ( ) ,
285
286
} ;
286
287
cloned_tx. txid ( ) . into ( )
@@ -357,7 +358,7 @@ impl Transaction {
357
358
previous_output: self . input[ input_index] . previous_output,
358
359
script_sig: script_pubkey. clone( ) ,
359
360
sequence: self . input[ input_index] . sequence,
360
- witness: vec! [ ] ,
361
+ witness: Witness :: default ( ) ,
361
362
} ] ;
362
363
} else {
363
364
tx. input = Vec :: with_capacity ( self . input . len ( ) ) ;
@@ -366,7 +367,7 @@ impl Transaction {
366
367
previous_output : input. previous_output ,
367
368
script_sig : if n == input_index { script_pubkey. clone ( ) } else { Script :: new ( ) } ,
368
369
sequence : if n != input_index && ( sighash == EcdsaSigHashType :: Single || sighash == EcdsaSigHashType :: None ) { 0 } else { input. sequence } ,
369
- witness : vec ! [ ] ,
370
+ witness : Witness :: default ( ) ,
370
371
} ) ;
371
372
}
372
373
}
@@ -473,10 +474,7 @@ impl Transaction {
473
474
input. script_sig . len ( ) ) ;
474
475
if !input. witness . is_empty ( ) {
475
476
inputs_with_witnesses += 1 ;
476
- input_weight += VarInt ( input. witness . len ( ) as u64 ) . len ( ) ;
477
- for elem in & input. witness {
478
- input_weight += VarInt ( elem. len ( ) as u64 ) . len ( ) + elem. len ( ) ;
479
- }
477
+ input_weight += input. witness . serialized_len ( ) ;
480
478
}
481
479
}
482
480
let mut output_size = 0 ;
@@ -578,7 +576,7 @@ impl Decodable for TxIn {
578
576
previous_output : Decodable :: consensus_decode ( & mut d) ?,
579
577
script_sig : Decodable :: consensus_decode ( & mut d) ?,
580
578
sequence : Decodable :: consensus_decode ( d) ?,
581
- witness : vec ! [ ] ,
579
+ witness : Witness :: default ( ) ,
582
580
} )
583
581
}
584
582
}
@@ -1433,6 +1431,7 @@ mod tests {
1433
1431
use hashes:: hex:: FromHex ;
1434
1432
use std:: collections:: HashMap ;
1435
1433
use blockdata:: script;
1434
+ use blockdata:: witness:: Witness ;
1436
1435
1437
1436
// a random recent segwit transaction from blockchain using both old and segwit inputs
1438
1437
let mut spending: Transaction = deserialize ( Vec :: from_hex ( "020000000001031cfbc8f54fbfa4a33a30068841371f80dbfe166211242213188428f437445c91000000006a47304402206fbcec8d2d2e740d824d3d36cc345b37d9f65d665a99f5bd5c9e8d42270a03a8022013959632492332200c2908459547bf8dbf97c65ab1a28dec377d6f1d41d3d63e012103d7279dfb90ce17fe139ba60a7c41ddf605b25e1c07a4ddcb9dfef4e7d6710f48feffffff476222484f5e35b3f0e43f65fc76e21d8be7818dd6a989c160b1e5039b7835fc00000000171600140914414d3c94af70ac7e25407b0689e0baa10c77feffffffa83d954a62568bbc99cc644c62eb7383d7c2a2563041a0aeb891a6a4055895570000000017160014795d04cc2d4f31480d9a3710993fbd80d04301dffeffffff06fef72f000000000017a91476fd7035cd26f1a32a5ab979e056713aac25796887a5000f00000000001976a914b8332d502a529571c6af4be66399cd33379071c588ac3fda0500000000001976a914fc1d692f8de10ae33295f090bea5fe49527d975c88ac522e1b00000000001976a914808406b54d1044c429ac54c0e189b0d8061667e088ac6eb68501000000001976a914dfab6085f3a8fb3e6710206a5a959313c5618f4d88acbba20000000000001976a914eb3026552d7e3f3073457d0bee5d4757de48160d88ac0002483045022100bee24b63212939d33d513e767bc79300051f7a0d433c3fcf1e0e3bf03b9eb1d70220588dc45a9ce3a939103b4459ce47500b64e23ab118dfc03c9caa7d6bfc32b9c601210354fd80328da0f9ae6eef2b3a81f74f9a6f66761fadf96f1d1d22b1fd6845876402483045022100e29c7e3a5efc10da6269e5fc20b6a1cb8beb92130cc52c67e46ef40aaa5cac5f0220644dd1b049727d991aece98a105563416e10a5ac4221abac7d16931842d5c322012103960b87412d6e169f30e12106bdf70122aabb9eb61f455518322a18b920a4dfa887d30700" )
@@ -1471,7 +1470,9 @@ mod tests {
1471
1470
} ) . is_err( ) ) ;
1472
1471
1473
1472
// test that we get a failure if we corrupt a signature
1474
- spending. input [ 1 ] . witness [ 0 ] [ 10 ] = 42 ;
1473
+ let mut witness: Vec < _ > = spending. input [ 1 ] . witness . to_vec ( ) ;
1474
+ witness[ 0 ] [ 10 ] = 42 ;
1475
+ spending. input [ 1 ] . witness = Witness :: from_vec ( witness) ;
1475
1476
match spending. verify ( |point : & OutPoint | {
1476
1477
if let Some ( tx) = spent3. remove ( & point. txid ) {
1477
1478
return tx. output . get ( point. vout as usize ) . cloned ( ) ;
0 commit comments