Make ScriptStatus Serializable
it's common to cache this value client side, so making it Serializable simplify things downstream
This commit is contained in:
parent
84d6860144
commit
80b2adeb4b
26
src/types.rs
26
src/types.rs
@ -9,7 +9,7 @@ use std::sync::Arc;
|
||||
|
||||
use bitcoin::blockdata::block;
|
||||
use bitcoin::consensus::encode::deserialize;
|
||||
use bitcoin::hashes::hex::FromHex;
|
||||
use bitcoin::hashes::hex::{FromHex, ToHex};
|
||||
use bitcoin::hashes::{sha256, Hash};
|
||||
use bitcoin::{Script, Txid};
|
||||
|
||||
@ -69,8 +69,8 @@ impl<'a> Request<'a> {
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize)]
|
||||
pub struct Hex32Bytes(#[serde(deserialize_with = "from_hex")] [u8; 32]);
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Hex32Bytes(#[serde(deserialize_with = "from_hex", serialize_with = "to_hex")] [u8; 32]);
|
||||
|
||||
impl Deref for Hex32Bytes {
|
||||
type Target = [u8; 32];
|
||||
@ -118,6 +118,13 @@ where
|
||||
T::from_hex(&s).map_err(de::Error::custom)
|
||||
}
|
||||
|
||||
fn to_hex<S>(bytes: &[u8], serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::ser::Serializer,
|
||||
{
|
||||
serializer.serialize_str(&bytes.to_hex())
|
||||
}
|
||||
|
||||
fn from_hex_array<'de, T, D>(deserializer: D) -> Result<Vec<T>, D::Error>
|
||||
where
|
||||
T: FromHex + std::fmt::Debug,
|
||||
@ -388,3 +395,16 @@ impl From<std::sync::mpsc::RecvError> for Error {
|
||||
Error::Mpsc
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::ScriptStatus;
|
||||
|
||||
#[test]
|
||||
fn script_status_roundtrip() {
|
||||
let script_status: ScriptStatus = [1u8; 32].into();
|
||||
let script_status_json = serde_json::to_string(&script_status).unwrap();
|
||||
let script_status_back = serde_json::from_str(&script_status_json).unwrap();
|
||||
assert_eq!(script_status, script_status_back);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user