diff --git a/crates/primitives/src/serde_helper/mod.rs b/crates/primitives/src/serde_helper/mod.rs index badbae09d6..f2018531fa 100644 --- a/crates/primitives/src/serde_helper/mod.rs +++ b/crates/primitives/src/serde_helper/mod.rs @@ -1,9 +1,12 @@ //! Various serde utilities mod storage_key; + +use serde::Serializer; pub use storage_key::*; mod jsonu256; +use crate::H256; pub use jsonu256::*; pub mod num; @@ -58,6 +61,27 @@ pub mod hex_bytes { } } +/// Serialize a byte vec as a hex string _without_ 0x prefix. +/// +/// This behaves exactly as [hex::encode] +pub fn serialize_hex_string_no_prefix(x: T, s: S) -> Result +where + S: Serializer, + T: AsRef<[u8]>, +{ + s.serialize_str(&hex::encode(x.as_ref())) +} + +/// Serialize a byte vec as a hex string _without_ 0x prefix +pub fn serialize_h256_hex_string_no_prefix(x: &H256, s: S) -> Result +where + S: Serializer, +{ + let val = format!("{:?}", x); + // skip the 0x prefix + s.serialize_str(&val.as_str()[2..]) +} + #[cfg(test)] mod tests { use super::*;