1use alloy_primitives::{B256, Bytes, U256, hex, keccak256};
4use foundry_compilers::artifacts::BytecodeObject;
5use regex::Regex;
6use std::sync::LazyLock;
7
8static BYTECODE_PLACEHOLDER_RE: LazyLock<Regex> =
9 LazyLock::new(|| Regex::new(r"__\$.{34}\$__").expect("invalid regex"));
10
11pub fn block_on<F: std::future::Future>(future: F) -> F::Output {
13 block_on_handle(&tokio::runtime::Handle::current(), future)
14}
15
16pub fn block_on_handle<F: std::future::Future>(
18 handle: &tokio::runtime::Handle,
19 future: F,
20) -> F::Output {
21 tokio::task::block_in_place(|| handle.block_on(future))
22}
23
24pub fn erc7201(id: &str) -> B256 {
44 let x = U256::from_be_bytes(keccak256(id).0) - U256::from(1);
45 keccak256(x.to_be_bytes::<32>()) & B256::from(!U256::from(0xff))
46}
47
48pub fn ignore_metadata_hash(bytecode: &[u8]) -> &[u8] {
51 let Some((rest, metadata_len_bytes)) = bytecode.split_last_chunk() else { return bytecode };
53 let metadata_len = u16::from_be_bytes(*metadata_len_bytes) as usize;
54 if metadata_len > rest.len() {
55 return bytecode;
56 }
57 let (rest, metadata) = rest.split_at(rest.len() - metadata_len);
58 if ciborium::from_reader::<ciborium::Value, _>(metadata).is_ok() { rest } else { bytecode }
59}
60
61pub fn strip_bytecode_placeholders(bytecode: &BytecodeObject) -> Option<Bytes> {
66 match &bytecode {
67 BytecodeObject::Bytecode(bytes) => Some(bytes.clone()),
68 BytecodeObject::Unlinked(s) => {
69 let s = (*BYTECODE_PLACEHOLDER_RE).replace_all(s, "00".repeat(40));
71 let bytes = hex::decode(s.as_bytes());
72 Some(bytes.ok()?.into())
73 }
74 }
75}