Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Becker committed Nov 4, 2024
1 parent aa7fe83 commit 153f067
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 206 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 34 additions & 8 deletions crates/common/src/ether/calldata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ use alloy::primitives::TxHash;
use eyre::{bail, eyre, Result};

/// Given a target, return calldata of the target.
pub async fn get_calldata_from_target(target: &str, rpc_url: &str) -> Result<Vec<u8>> {
pub async fn get_calldata_from_target(target: &str, raw: bool, rpc_url: &str) -> Result<Vec<u8>> {
// If the target is a transaction hash, fetch the calldata from the RPC provider.
if let Ok(address) = target.parse::<TxHash>() {
return get_transaction(address, rpc_url)
.await
.map(|tx| tx.input.to_vec())
.map_err(|_| eyre!("failed to fetch transaction from RPC provider"));
// if raw is true, the user specified that the target is raw calldata. skip fetching the transaction.
if !raw {
return get_transaction(address, rpc_url)
.await
.map(|tx| tx.input.to_vec())
.map_err(|_| eyre!("failed to fetch transaction from RPC provider"));
}
}

// If the target is not a transaction hash, it could be calldata.
Expand All @@ -34,6 +37,7 @@ mod tests {

let calldata = get_calldata_from_target(
"0x317907eeece00619fd4418c18a4ec4ebe5c87cdbff808f4b01cc2c6384799837",
false,
&rpc_url,
)
.await
Expand All @@ -51,6 +55,7 @@ mod tests {

let calldata = get_calldata_from_target(
"0xf14fcbc8bf9eac48d61719f80efb268ef1099a248fa332ed639041337954647ec6583f2e",
false,
&rpc_url,
)
.await
Expand All @@ -66,10 +71,31 @@ mod tests {
std::process::exit(0);
});

let calldata =
get_calldata_from_target("asfnsdalkfasdlfnlasdkfnalkdsfndaskljfnasldkjfnasf", &rpc_url)
.await;
let calldata = get_calldata_from_target(
"asfnsdalkfasdlfnlasdkfnalkdsfndaskljfnasldkjfnasf",
false,
&rpc_url,
)
.await;

assert!(calldata.is_err());
}

#[tokio::test]
async fn test_get_calldata_when_target_is_calldata_that_is_exactly_32_bytes() {
let rpc_url = std::env::var("RPC_URL").unwrap_or_else(|_| {
println!("RPC_URL not set, skipping test");
std::process::exit(0);
});

let calldata = get_calldata_from_target(
"0x317907eeece00619fd4418c18a4ec4ebe5c87cdbff808f4b01cc2c6384799837",
true,
&rpc_url,
)
.await
.expect("failed to get calldata from target");

assert!(calldata.len() == 32);
}
}
2 changes: 2 additions & 0 deletions crates/core/tests/test_decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod integration_tests {
constructor: false,
truncate_calldata: false,
skip_resolving: false,
raw: false,
};
let _ = heimdall_decoder::decode(args).await;
}
Expand All @@ -29,6 +30,7 @@ mod integration_tests {
constructor: false,
truncate_calldata: false,
skip_resolving: false,
raw: false,
};
let _ = heimdall_decoder::decode(args).await;
}
Expand Down
7 changes: 6 additions & 1 deletion crates/decode/src/interfaces/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ pub struct DecodeArgs {
/// Whether to skip resolving selectors. Heimdall will attempt to guess types.
#[clap(long = "skip-resolving")]
pub skip_resolving: bool,

/// Whether to treat the target as a raw calldata string. Useful if the target is exactly 32 bytes.
#[clap(long, short)]
pub raw: bool,
}

impl DecodeArgs {
pub async fn get_calldata(&self) -> Result<Vec<u8>> {
get_calldata_from_target(&self.target, &self.rpc_url).await
get_calldata_from_target(&self.target, self.raw, &self.rpc_url).await
}
}

Expand All @@ -62,6 +66,7 @@ impl DecodeArgsBuilder {
constructor: Some(false),
truncate_calldata: Some(false),
skip_resolving: Some(false),
raw: Some(false),
}
}
}
7 changes: 6 additions & 1 deletion crates/decompile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ fancy-regex = "0.11.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
alloy-dyn-abi = "0.8.3"
alloy = { version = "0.3.3", features = ["full", "rpc-types-debug", "rpc-types-trace"] }
alloy = { version = "0.3.3", features = [
"full",
"rpc-types-debug",
"rpc-types-trace",
] }
hashbrown = "0.14.5"
tokio = { version = "1", features = ["full"] }

heimdall-disassembler.workspace = true
heimdall-vm.workspace = true
8 changes: 4 additions & 4 deletions crates/decompile/src/core/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ impl Analyzer {
}

// check if the ending brackets are needed
if analyzer_state.jumped_conditional.is_some() &&
analyzer_state.conditional_stack.contains(
if analyzer_state.jumped_conditional.is_some()
&& analyzer_state.conditional_stack.contains(
analyzer_state
.jumped_conditional
.as_ref()
Expand All @@ -167,8 +167,8 @@ impl Analyzer {
{
// remove the conditional
for (i, conditional) in analyzer_state.conditional_stack.iter().enumerate() {
if conditional ==
analyzer_state.jumped_conditional.as_ref().expect(
if conditional
== analyzer_state.jumped_conditional.as_ref().expect(
"impossible case: should have short-circuited in previous conditional",
)
{
Expand Down
Loading

0 comments on commit 153f067

Please sign in to comment.