From 46d7092c5b5e9e7d8285c40c53e16f989d78002f Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Mon, 25 Nov 2024 12:12:21 +0100 Subject: [PATCH 1/2] fix: serialize zksolc bytecode without 0x prefix --- .../src/zksync/artifact_output/zk/bytecode.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/compilers/src/zksync/artifact_output/zk/bytecode.rs b/crates/compilers/src/zksync/artifact_output/zk/bytecode.rs index 076a107c..8a190f58 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, From 1d51c636b3a12f560add3bae336c5d53b4388767 Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Mon, 25 Nov 2024 20:38:33 +0100 Subject: [PATCH 2/2] test(artifact:zk): unprefixed bytecode test --- .../src/zksync/artifact_output/zk/bytecode.rs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/compilers/src/zksync/artifact_output/zk/bytecode.rs b/crates/compilers/src/zksync/artifact_output/zk/bytecode.rs index 8a190f58..a26b94ab 100644 --- a/crates/compilers/src/zksync/artifact_output/zk/bytecode.rs +++ b/crates/compilers/src/zksync/artifact_output/zk/bytecode.rs @@ -72,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")); + } +}