diff --git a/core/lib/basic_types/src/web3/mod.rs b/core/lib/basic_types/src/web3/mod.rs index be785040d2d..a2166d62710 100644 --- a/core/lib/basic_types/src/web3/mod.rs +++ b/core/lib/basic_types/src/web3/mod.rs @@ -61,9 +61,13 @@ impl Serialize for Bytes { where S: Serializer, { - let mut serialized = "0x".to_owned(); - serialized.push_str(&hex::encode(&self.0)); - serializer.serialize_str(serialized.as_ref()) + if serializer.is_human_readable() { + let mut serialized = "0x".to_owned(); + serialized.push_str(&hex::encode(&self.0)); + serializer.serialize_str(serialized.as_ref()) + } else { + serializer.serialize_bytes(&self.0) + } } } @@ -72,7 +76,11 @@ impl<'a> Deserialize<'a> for Bytes { where D: Deserializer<'a>, { - deserializer.deserialize_identifier(BytesVisitor) + if deserializer.is_human_readable() { + deserializer.deserialize_identifier(BytesVisitor) + } else { + Vec::::deserialize(deserializer).map(Bytes) + } } } diff --git a/core/lib/basic_types/src/web3/tests.rs b/core/lib/basic_types/src/web3/tests.rs index e7c0a0d048a..bbeaece0374 100644 --- a/core/lib/basic_types/src/web3/tests.rs +++ b/core/lib/basic_types/src/web3/tests.rs @@ -83,3 +83,12 @@ fn block_can_be_deserialized() { let block: Block = serde_json::from_str(pre_dencun).unwrap(); assert!(block.excess_blob_gas.is_none()); } + +#[test] +fn test_bytes_serde() { + use bincode::{deserialize, serialize}; + let original = Bytes(vec![0, 1, 2, 3, 4]); + let encoded: Vec = serialize(&original).unwrap(); + let decoded: Bytes = deserialize(&encoded).unwrap(); + assert_eq!(original, decoded); +}