Skip to content

Commit

Permalink
Fixing beacon proxy detection
Browse files Browse the repository at this point in the history
  • Loading branch information
naps62 committed Feb 3, 2025
1 parent 548ae53 commit 203ff46
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ version = "0.1.0"
edition = "2021"

[dependencies]
alloy = { version = "0.11.0", features = ["providers", "reqwest-rustls-tls"] }
alloy = { version = "0.11.0", features = [
"providers",
"reqwest-rustls-tls",
"rpc",
"rpc-types",
] }
rstest = "0.24.0"
thiserror = "2.0.11"

Expand Down
50 changes: 45 additions & 5 deletions src/eip1967.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloy::{
network::Network,
primitives::{b256, Address, Bytes, B256, U256},
network::{Network, TransactionBuilder},
primitives::{b256, bytes, Address, Bytes, B256, U256},
providers::Provider,
};

Expand All @@ -9,9 +9,19 @@ use crate::{error::DetectProxyResult, ProxyType};
// bytes32(uint256(keccak256('eip1967.proxy.implementation')) - 1)
const EIP1967_LOGIC_SLOT: B256 =
b256!("0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc");

// bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)
const EIP1967_BEACON_SLOT: B256 =
b256!("0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50");

const EIP1967_BEACON_METHODS: [Bytes; 2] = [
// bytes4(keccak256("implementation()")) padded to 32 bytes
bytes!("0x5c60da1b00000000000000000000000000000000000000000000000000000000"),
// bytes4(keccak256("childImplementation()")) padded to 32 bytes
// some implementations use this over the standard method name so that the beacon contract is not detected as an EIP-897 proxy itself
bytes!("0xda52571600000000000000000000000000000000000000000000000000000000"),
];

pub(crate) async fn detect_eip1967_direct_proxy<N, P: Provider<N>>(
address: Address,
provider: P,
Expand All @@ -23,9 +33,39 @@ where
return Ok(Some(ProxyType::Eip1967Direct(addr)));
}

if let Ok(Some(addr)) = storage_slot_as_address(&provider, address, EIP1967_BEACON_SLOT).await {
return Ok(Some(ProxyType::Eip1967Beacon(addr)));
}
Ok(None)
}

pub(crate) async fn detect_eip1967_beacon_proxy<N, P: Provider<N>>(
address: Address,
provider: P,
) -> DetectProxyResult<Option<ProxyType>>
where
N: Network,
{
let beacon = if let Ok(Some(addr)) =
storage_slot_as_address(&provider, address, EIP1967_BEACON_SLOT).await
{
addr
} else {
return Ok(None);
};

let beacon_call_0 = <N as Network>::TransactionRequest::default()
.with_to(beacon)
.with_input(EIP1967_BEACON_METHODS[0].clone());

let beacon_call_1 = <N as Network>::TransactionRequest::default()
.with_to(beacon)
.with_input(EIP1967_BEACON_METHODS[1].clone());

if let Ok(value) = provider.call(&beacon_call_0).await {
let b256: B256 = B256::from_slice(&value);
return Ok(Some(ProxyType::Eip1967Beacon(u256_to_address(b256.into()))));
} else if let Ok(value) = provider.call(&beacon_call_1).await {
let b256: B256 = B256::from_slice(&value);
return Ok(Some(ProxyType::Eip1967Beacon(u256_to_address(b256.into()))));
};

Ok(None)
}
Expand Down
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum ProxyType {
Eip1167(Address),
Eip1967Direct(Address),
Eip1967Beacon(Address),
OpenZeppelin(Address),
}

pub async fn detect_proxy<N, P: Provider<N>>(
Expand All @@ -29,6 +30,12 @@ where
return Ok(Some(proxy_type));
}

if let Some(proxy_type) = eip1967::detect_eip1967_beacon_proxy(address, provider).await? {
return Ok(Some(proxy_type));
}

//if let Some(proxy_type) = eip1967::detect_open(address, provider).await? {

Ok(None)
}

Expand All @@ -51,8 +58,8 @@ mod tests {
#[case::eip1167_vanity(address!("0xa81043fd06D57D140f6ad8C2913DbE87fdecDd5F"), ProxyType::Eip1167(address!("0x0000000010fd301be3200e67978e3cc67c962f48")))]
#[case::eip1967_direct(address!("0xA7AeFeaD2F25972D80516628417ac46b3F2604Af"), ProxyType::Eip1967Direct(address!("0x4bd844f72a8edd323056130a86fc624d0dbcf5b0")))]
#[case::eip1967_direct(address!("0x8260b9eC6d472a34AD081297794d7Cc00181360a"), ProxyType::Eip1967Direct(address!("0xe4e4003afe3765aca8149a82fc064c0b125b9e5a")))]
#[case::eip1967_beacon(address!("0xDd4e2eb37268B047f55fC5cAf22837F9EC08A881"), ProxyType::Eip1967Beacon(address!("0xb3e0edda8c2aeabfdece18ad7ac1ea86eb7d583b")))]
#[case::eip1967_beacon(address!("0x114f1388fAB456c4bA31B1850b244Eedcd024136"), ProxyType::Eip1967Beacon(address!("0xbe86f647b167567525ccaafcd6f881f1ee558216")))]
#[case::eip1967_beacon(address!("0xDd4e2eb37268B047f55fC5cAf22837F9EC08A881"), ProxyType::Eip1967Beacon(address!("0xe5c048792dcf2e4a56000c8b6a47f21df22752d1")))]
#[case::eip1967_beacon(address!("0x114f1388fAB456c4bA31B1850b244Eedcd024136"), ProxyType::Eip1967Beacon(address!("0x0fa0fd98727c443dd5275774c44d27cff9d279ed")))]
#[tokio::test]
async fn mainnet(#[case] proxy: Address, #[case] impl_: ProxyType) {
let provider = ProviderBuilder::new().on_http(MAINNET_RPC.clone());
Expand Down

0 comments on commit 203ff46

Please sign in to comment.