@@ -25,7 +25,8 @@ use std::{error, fmt, io};
25
25
use crate :: address;
26
26
use crate :: deps_common:: bitcoin:: network:: encodable:: { ConsensusDecodable , ConsensusEncodable } ;
27
27
use crate :: deps_common:: bitcoin:: util:: hash:: Sha256dHash ;
28
- use crate :: util:: hash:: to_hex as hex_encode;
28
+ use crate :: util:: hash:: { hex_bytes, to_hex as hex_encode} ;
29
+ use crate :: util:: HexError ;
29
30
30
31
/// Serialization error
31
32
#[ derive( Debug ) ]
@@ -67,6 +68,8 @@ pub enum Error {
67
68
UnrecognizedNetworkCommand ( String ) ,
68
69
/// Unexpected hex digit
69
70
UnexpectedHexDigit ( char ) ,
71
+ /// Invalid hex input
72
+ InvalidHex ( HexError ) ,
70
73
}
71
74
72
75
impl fmt:: Display for Error {
@@ -106,6 +109,7 @@ impl fmt::Display for Error {
106
109
write ! ( f, "unrecognized network command: {nwcmd}" )
107
110
}
108
111
Error :: UnexpectedHexDigit ( ref d) => write ! ( f, "unexpected hex digit: {d}" ) ,
112
+ Error :: InvalidHex ( ref e) => fmt:: Display :: fmt ( e, f) ,
109
113
}
110
114
}
111
115
}
@@ -123,7 +127,8 @@ impl error::Error for Error {
123
127
| Error :: UnsupportedWitnessVersion ( ..)
124
128
| Error :: UnsupportedSegwitFlag ( ..)
125
129
| Error :: UnrecognizedNetworkCommand ( ..)
126
- | Error :: UnexpectedHexDigit ( ..) => None ,
130
+ | Error :: UnexpectedHexDigit ( ..)
131
+ | Error :: InvalidHex ( ..) => None ,
127
132
}
128
133
}
129
134
}
@@ -142,6 +147,13 @@ impl From<io::Error> for Error {
142
147
}
143
148
}
144
149
150
+ #[ doc( hidden) ]
151
+ impl From < HexError > for Error {
152
+ fn from ( error : HexError ) -> Self {
153
+ Error :: InvalidHex ( error)
154
+ }
155
+ }
156
+
145
157
/// Objects which are referred to by hash
146
158
pub trait BitcoinHash {
147
159
/// Produces a Sha256dHash which can be used to refer to the object
@@ -193,6 +205,15 @@ where
193
205
}
194
206
}
195
207
208
+ /// Deserialize an object from a hex-encoded string
209
+ pub fn deserialize_hex < T > ( data : & str ) -> Result < T , Error >
210
+ where
211
+ for < ' a > T : ConsensusDecodable < RawDecoder < Cursor < & ' a [ u8 ] > > > ,
212
+ {
213
+ let bytes = hex_bytes ( data) ?;
214
+ deserialize ( & bytes)
215
+ }
216
+
196
217
/// An encoder for raw binary data
197
218
pub struct RawEncoder < W > {
198
219
writer : W ,
0 commit comments