Skip to content

Commit a04a842

Browse files
committed
Merge #101: Make ScriptStatus Serializable
80b2ade Make ScriptStatus Serializable (Riccardo Casatta) Pull request description: it's common to cache this value client side, so making it Serializable simplify things downstream ACKs for top commit: afilini: ACK 80b2ade Tree-SHA512: 9467b4f2ee24ee2a684931df31607135f44c624f65d7d3ee64b3991a06de73a07475e4d1a4b8062093500e6a7cb47c1b2ebf094596fca33bf4cc54b26851b910
2 parents d26dfd7 + 80b2ade commit a04a842

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/types.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::sync::Arc;
99

1010
use bitcoin::blockdata::block;
1111
use bitcoin::consensus::encode::deserialize;
12-
use bitcoin::hashes::hex::FromHex;
12+
use bitcoin::hashes::hex::{FromHex, ToHex};
1313
use bitcoin::hashes::{sha256, Hash};
1414
use 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

7575
impl 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+
121128
fn from_hex_array<'de, T, D>(deserializer: D) -> Result<Vec<T>, D::Error>
122129
where
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

Comments
 (0)