diff --git a/crates/compilers/src/zksync/artifact_output/zk/bytecode.rs b/crates/compilers/src/zksync/artifact_output/zk/bytecode.rs index 076a107c..a26b94ab 100644 --- a/crates/compilers/src/zksync/artifact_output/zk/bytecode.rs +++ b/crates/compilers/src/zksync/artifact_output/zk/bytecode.rs @@ -6,8 +6,20 @@ use foundry_compilers_artifacts::{ }; use serde::{Deserialize, Serialize}; +/// This will serialize the bytecode data without a `0x` prefix +/// +/// Equivalent of solc artifact bytecode's +/// [`serialize_bytecode_without_prefix`](foundry_compilers_artifacts::solc::bytecode::serialize_bytecode_without_prefix) +pub fn serialize_bytes_without_prefix(code: &Bytes, s: S) -> Result +where + S: serde::Serializer, +{ + s.serialize_str(&alloy_primitives::hex::encode(code)) +} + #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct ZkArtifactBytecode { + #[serde(serialize_with = "serialize_bytes_without_prefix")] object: Bytes, is_unlinked: bool, @@ -60,3 +72,25 @@ impl From for CompactDeployedBytecode { Self { bytecode: Some(bcode.into()), immutable_references: BTreeMap::default() } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + /// https://github.com/Moonsong-Labs/compilers/pull/44 + fn serialized_bytecode_is_not_prefixed() { + let object = Bytes::from(vec![0xDEu8, 0xAD, 0xBE, 0xEF]); + let sample = ZkArtifactBytecode { object, is_unlinked: false, missing_libraries: vec![] }; + + let json_str = + serde_json::to_string(&sample).expect("able to serialize artifact bytecode as json"); + + let deserialized: serde_json::Value = + serde_json::from_str(&json_str).expect("able to deserialize json"); + + let bytecode_str = deserialized["object"].as_str().expect(".object to be a string"); + + assert!(!bytecode_str.starts_with("0x")); + } +}