Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/artifacts/solc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde.workspace = true
thiserror.workspace = true
tracing.workspace = true
yansi.workspace = true
regex.workspace = true

# async
tokio = { workspace = true, optional = true, features = ["fs"] }
Expand Down
22 changes: 22 additions & 0 deletions crates/artifacts/solc/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,21 @@ impl BytecodeObject {
pub fn contains_placeholder(&self, file: &str, library: &str) -> bool {
self.contains_fully_qualified_placeholder(&format!("{file}:{library}"))
}

/// Strips all __$xxx$__ placeholders from the bytecode if it's an unlinked bytecode.
/// by replacing them with 20 zero bytes.
/// This is useful for matching bytecodes to a contract source, and for the source map,
/// in which the actual address of the placeholder isn't important.
pub fn strip_bytecode_placeholders(&self) -> Option<Bytes> {
match &self {
Self::Bytecode(bytes) => Some(bytes.clone()),
Self::Unlinked(s) => {
// Replace all __$xxx$__ placeholders with 32 zero bytes
let bytes = replace_placeholders_and_decode(s).ok()?;
Some(bytes.into())
}
}
}
}

// Returns an empty bytecode object
Expand Down Expand Up @@ -388,6 +403,13 @@ where
}
}

// Replace all __$xxx$__ placeholders with 32 zero bytes
pub fn replace_placeholders_and_decode(s: &str) -> Result<Vec<u8>, hex::FromHexError> {
let re = regex::Regex::new(r"_\$.{34}\$_").expect("invalid regex");
let s = re.replace_all(s, "00".repeat(40));
hex::decode(s.as_bytes())
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeployedBytecode {
#[serde(flatten)]
Expand Down
Loading