@@ -29,6 +29,27 @@ pub use bitcoin::{self, consensus::encode::MAX_VEC_SIZE};
2929
3030use crate :: taproot:: TapLeafHash ;
3131
32+ struct ByteCounter < W > {
33+ inner : W ,
34+ count : usize ,
35+ }
36+
37+ impl < W > io:: Write for ByteCounter < W >
38+ where W : io:: Write
39+ {
40+ fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
41+ let res = self . inner . write ( buf) ;
42+ if let Ok ( size) = res {
43+ self . count += size;
44+ }
45+ res
46+ }
47+
48+ fn flush ( & mut self ) -> io:: Result < ( ) > {
49+ self . inner . flush ( )
50+ }
51+ }
52+
3253/// Encoding error
3354#[ derive( Debug ) ]
3455pub enum Error {
@@ -63,6 +84,8 @@ pub enum Error {
6384 BadLockTime ( crate :: LockTime ) ,
6485 /// `VarInt` was encoded in a non-minimal way.
6586 NonMinimalVarInt ,
87+ /// Error decoding a transaction witness.
88+ Witness ( crate :: WitnessDecoderError ) ,
6689}
6790
6891impl fmt:: Display for Error {
@@ -90,6 +113,7 @@ impl fmt::Display for Error {
90113 Error :: HexVariableError ( ref e) => write ! ( f, "Hex variable error: {}" , e) ,
91114 Error :: BadLockTime ( ref lt) => write ! ( f, "Invalid locktime {}" , lt) ,
92115 Error :: NonMinimalVarInt => write ! ( f, "non-minimal varint" ) ,
116+ Self :: Witness ( ..) => f. write_str ( "error decoding witness" ) ,
93117 }
94118 }
95119}
@@ -98,6 +122,7 @@ impl error::Error for Error {
98122 fn cause ( & self ) -> Option < & dyn error:: Error > {
99123 match * self {
100124 Error :: Secp256k1zkp ( ref e) => Some ( e) ,
125+ Self :: Witness ( ref e) => Some ( e) ,
101126 _ => None ,
102127 }
103128 }
@@ -240,6 +265,24 @@ impl Decodable for crate::locktime::Time {
240265 }
241266}
242267
268+ impl Encodable for crate :: Witness {
269+ fn consensus_encode < W : io:: Write > ( & self , e : W ) -> Result < usize , Error > {
270+ let mut counter = ByteCounter { inner : e, count : 0 } ;
271+ crate :: encoding:: encode_to_writer ( self , & mut counter) ?;
272+ Ok ( counter. count )
273+ }
274+ }
275+
276+ impl Decodable for crate :: Witness {
277+ fn consensus_decode < D : io:: Read > ( d : D ) -> Result < Self , Error > {
278+ match crate :: encoding:: decode_from_read_unbuffered ( d) {
279+ Ok ( wit) => Ok ( wit) ,
280+ Err ( crate :: encoding:: ReadError :: Io ( e) ) => Err ( Error :: Io ( e) ) ,
281+ Err ( crate :: encoding:: ReadError :: Decode ( e) ) => Err ( Error :: Witness ( e) ) ,
282+ }
283+ }
284+ }
285+
243286/// A variable sized integer.
244287pub struct VarInt ( pub u64 ) ;
245288impl Encodable for VarInt {
0 commit comments