@@ -9,7 +9,7 @@ use std::sync::Arc;
99
1010use bitcoin:: blockdata:: block;
1111use bitcoin:: consensus:: encode:: deserialize;
12- use bitcoin:: hashes:: hex:: FromHex ;
12+ use bitcoin:: hashes:: hex:: { FromHex , ToHex } ;
1313use bitcoin:: hashes:: { sha256, Hash } ;
1414use bitcoin:: { Script , Txid } ;
1515
@@ -69,8 +69,8 @@ impl<'a> Request<'a> {
6969}
7070
7171#[ doc( hidden) ]
72- #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Deserialize ) ]
73- pub struct Hex32Bytes ( #[ serde( deserialize_with = "from_hex" ) ] [ u8 ; 32 ] ) ;
72+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Deserialize , Serialize ) ]
73+ pub struct Hex32Bytes ( #[ serde( deserialize_with = "from_hex" , serialize_with = "to_hex" ) ] [ u8 ; 32 ] ) ;
7474
7575impl Deref for Hex32Bytes {
7676 type Target = [ u8 ; 32 ] ;
@@ -118,6 +118,13 @@ where
118118 T :: from_hex ( & s) . map_err ( de:: Error :: custom)
119119}
120120
121+ fn to_hex < S > ( bytes : & [ u8 ] , serializer : S ) -> std:: result:: Result < S :: Ok , S :: Error >
122+ where
123+ S : serde:: ser:: Serializer ,
124+ {
125+ serializer. serialize_str ( & bytes. to_hex ( ) )
126+ }
127+
121128fn from_hex_array < ' de , T , D > ( deserializer : D ) -> Result < Vec < T > , D :: Error >
122129where
123130 T : FromHex + std:: fmt:: Debug ,
@@ -388,3 +395,16 @@ impl From<std::sync::mpsc::RecvError> for Error {
388395 Error :: Mpsc
389396 }
390397}
398+
399+ #[ cfg( test) ]
400+ mod tests {
401+ use crate :: ScriptStatus ;
402+
403+ #[ test]
404+ fn script_status_roundtrip ( ) {
405+ let script_status: ScriptStatus = [ 1u8 ; 32 ] . into ( ) ;
406+ let script_status_json = serde_json:: to_string ( & script_status) . unwrap ( ) ;
407+ let script_status_back = serde_json:: from_str ( & script_status_json) . unwrap ( ) ;
408+ assert_eq ! ( script_status, script_status_back) ;
409+ }
410+ }
0 commit comments