@@ -25,7 +25,8 @@ use std::{error, fmt, io};
2525use crate :: address;
2626use crate :: deps_common:: bitcoin:: network:: encodable:: { ConsensusDecodable , ConsensusEncodable } ;
2727use 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 ;
2930
3031/// Serialization error
3132#[ derive( Debug ) ]
@@ -67,6 +68,8 @@ pub enum Error {
6768 UnrecognizedNetworkCommand ( String ) ,
6869 /// Unexpected hex digit
6970 UnexpectedHexDigit ( char ) ,
71+ /// Invalid hex input
72+ InvalidHex ( HexError ) ,
7073}
7174
7275impl fmt:: Display for Error {
@@ -106,6 +109,7 @@ impl fmt::Display for Error {
106109 write ! ( f, "unrecognized network command: {nwcmd}" )
107110 }
108111 Error :: UnexpectedHexDigit ( ref d) => write ! ( f, "unexpected hex digit: {d}" ) ,
112+ Error :: InvalidHex ( ref e) => fmt:: Display :: fmt ( e, f) ,
109113 }
110114 }
111115}
@@ -123,7 +127,8 @@ impl error::Error for Error {
123127 | Error :: UnsupportedWitnessVersion ( ..)
124128 | Error :: UnsupportedSegwitFlag ( ..)
125129 | Error :: UnrecognizedNetworkCommand ( ..)
126- | Error :: UnexpectedHexDigit ( ..) => None ,
130+ | Error :: UnexpectedHexDigit ( ..)
131+ | Error :: InvalidHex ( ..) => None ,
127132 }
128133 }
129134}
@@ -142,6 +147,13 @@ impl From<io::Error> for Error {
142147 }
143148}
144149
150+ #[ doc( hidden) ]
151+ impl From < HexError > for Error {
152+ fn from ( error : HexError ) -> Self {
153+ Error :: InvalidHex ( error)
154+ }
155+ }
156+
145157/// Objects which are referred to by hash
146158pub trait BitcoinHash {
147159 /// Produces a Sha256dHash which can be used to refer to the object
@@ -193,6 +205,15 @@ where
193205 }
194206}
195207
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+
196217/// An encoder for raw binary data
197218pub struct RawEncoder < W > {
198219 writer : W ,
0 commit comments