-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
78 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use alloy::{ | ||
network::{Network, TransactionBuilder as _}, | ||
primitives::{b256, Address, B256}, | ||
providers::Provider, | ||
}; | ||
|
||
use crate::{error::DetectProxyResult, utils::u256_to_address, ProxyType}; | ||
|
||
const COMPTROLLER_INTERFACE: [B256; 1] = [ | ||
// bytes4(keccak256("comptrollerImplementation()")) padded to 32 bytes | ||
b256!("0xbb82aa5e00000000000000000000000000000000000000000000000000000000"), | ||
]; | ||
|
||
pub(crate) async fn detect_comptroller_proxy<N, P: Provider<N>>( | ||
address: Address, | ||
provider: P, | ||
) -> DetectProxyResult<Option<ProxyType>> | ||
where | ||
N: Network, | ||
{ | ||
let call_0 = <N as Network>::TransactionRequest::default() | ||
.with_to(address) | ||
.with_input(COMPTROLLER_INTERFACE[0]); | ||
|
||
if let Ok(value) = provider.call(&call_0).await { | ||
let b256: B256 = B256::from_slice(&value); | ||
return Ok(Some(ProxyType::Comptroller(u256_to_address(b256.into())))); | ||
}; | ||
|
||
Ok(None) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,45 @@ | ||
use alloy::primitives::{bytes, Address, Bytes}; | ||
use alloy::{ | ||
network::Network, | ||
primitives::{bytes, Address, Bytes}, | ||
providers::Provider, | ||
}; | ||
|
||
use crate::{error::DetectProxyResult, ProxyType}; | ||
|
||
const EIP1167_PREFIX: Bytes = bytes!("363d3d373d3d3d363d"); | ||
const EIP1167_SUFFIX: Bytes = bytes!("57fd5bf3"); | ||
const EIP1167_SUFFIX_OFFSET_FROM_ADDRESS_END: usize = 11; | ||
|
||
pub(crate) fn detect_eip1167_minimal_proxy(code: &Bytes) -> Option<Address> { | ||
pub(crate) async fn detect_eip1167_minimal_proxy<N, P: Provider<N>>( | ||
address: Address, | ||
provider: P, | ||
) -> DetectProxyResult<Option<ProxyType>> | ||
where | ||
N: Network, | ||
{ | ||
let code = provider.get_code_at(address).await?; | ||
|
||
if !code.starts_with(&EIP1167_PREFIX) { | ||
return None; | ||
return Ok(None); | ||
} | ||
|
||
// detect length of address (20 bytes non-optimized, 0 < N < 20 bytes for vanity addresses) | ||
// push1 ... push20 use opcode 0x60 ... 0x73 | ||
let address_len = code[EIP1167_PREFIX.len()] as usize - 0x5f; | ||
|
||
if !(1..=20).contains(&address_len) { | ||
return None; | ||
return Ok(None); | ||
} | ||
|
||
let address_pos = EIP1167_PREFIX.len() + 1; | ||
let suffix = &code[address_pos + address_len + EIP1167_SUFFIX_OFFSET_FROM_ADDRESS_END..]; | ||
|
||
if !suffix.starts_with(&EIP1167_SUFFIX) { | ||
return None; | ||
return Ok(None); | ||
} | ||
|
||
let address_hex = &code[address_pos..address_pos + address_len]; | ||
let address = Address::left_padding_from(address_hex); | ||
|
||
Some(address) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use alloy::primitives::address; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn parse_eip1167_code() { | ||
let bytecode: Bytes = bytes!("363d3d373d3d3d363d73f62849f9a0b5bf2913b396098f7c7019b51a820a5af43d82803e903d91602b57fd5bf3000000000000000000000000000000000000000000000000000000000000007a6900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); | ||
let address: Address = address!("f62849f9a0b5bf2913b396098f7c7019b51a820a"); | ||
|
||
assert_eq!(detect_eip1167_minimal_proxy(&bytecode), Some(address)); | ||
} | ||
Ok(Some(ProxyType::Eip1167(address))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters