From 46d7dcba89210a41425f25fc0d4f6943b9212f1d Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 14 Apr 2023 17:33:15 +0200 Subject: [PATCH] chore: extract serde bytes helper (#2247) --- crates/primitives/src/hex_bytes.rs | 30 +++------------------- crates/primitives/src/serde_helper/mod.rs | 31 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/crates/primitives/src/hex_bytes.rs b/crates/primitives/src/hex_bytes.rs index b9db906775..63aac34baf 100644 --- a/crates/primitives/src/hex_bytes.rs +++ b/crates/primitives/src/hex_bytes.rs @@ -1,7 +1,8 @@ +use crate::serde_helper::hex_bytes; use bytes::Buf; use reth_codecs::Compact; use reth_rlp::{Decodable, DecodeError, Encodable}; -use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; use std::{ borrow::Borrow, clone::Clone, @@ -13,10 +14,7 @@ use thiserror::Error; /// Wrapper type around Bytes to deserialize/serialize "0x" prefixed ethereum hex strings #[derive(Clone, Default, PartialEq, Eq, Hash, Ord, PartialOrd, Serialize, Deserialize)] -pub struct Bytes( - #[serde(serialize_with = "serialize_bytes", deserialize_with = "deserialize_bytes")] - pub bytes::Bytes, -); +pub struct Bytes(#[serde(with = "hex_bytes")] pub bytes::Bytes); fn bytes_to_hex(b: &Bytes) -> String { hex::encode(b.0.as_ref()) @@ -185,28 +183,6 @@ impl FromStr for Bytes { } } -fn serialize_bytes(x: T, s: S) -> Result -where - S: Serializer, - T: AsRef<[u8]>, -{ - s.serialize_str(&format!("0x{}", hex::encode(x.as_ref()))) -} - -fn deserialize_bytes<'de, D>(d: D) -> Result -where - D: Deserializer<'de>, -{ - let value = String::deserialize(d)?; - if let Some(value) = value.strip_prefix("0x") { - hex::decode(value) - } else { - hex::decode(&value) - } - .map(Into::into) - .map_err(|e| serde::de::Error::custom(e.to_string())) -} - impl Compact for Bytes { fn to_compact(self, buf: &mut B) -> usize where diff --git a/crates/primitives/src/serde_helper/mod.rs b/crates/primitives/src/serde_helper/mod.rs index d0bc14671c..badbae09d6 100644 --- a/crates/primitives/src/serde_helper/mod.rs +++ b/crates/primitives/src/serde_helper/mod.rs @@ -27,6 +27,37 @@ pub mod u64_hex { } } +/// serde functions for handling bytes as hex strings, such as [bytes::Bytes] +pub mod hex_bytes { + use serde::{Deserialize, Deserializer, Serializer}; + + /// Serialize a byte vec as a hex string with 0x prefix + pub fn serialize(x: T, s: S) -> Result + where + S: Serializer, + T: AsRef<[u8]>, + { + s.serialize_str(&format!("0x{}", hex::encode(x.as_ref()))) + } + + /// Deserialize a hex string into a byte vec + /// Accepts a hex string with optional 0x prefix + pub fn deserialize<'de, T, D>(d: D) -> Result + where + D: Deserializer<'de>, + T: From>, + { + let value = String::deserialize(d)?; + if let Some(value) = value.strip_prefix("0x") { + hex::decode(value) + } else { + hex::decode(&value) + } + .map(Into::into) + .map_err(|e| serde::de::Error::custom(e.to_string())) + } +} + #[cfg(test)] mod tests { use super::*;