diff --git a/cli/src/main.rs b/cli/src/main.rs index d710d85f..8f612dc8 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -345,8 +345,8 @@ async fn main() -> Result<(), Box> { } // if the user has not specified a transpose api key, use the default - if cmd.transpose_api_key.is_none() { - cmd.transpose_api_key = Some(configuration.transpose_api_key); + if cmd.transpose_api_key.is_empty() { + cmd.transpose_api_key = configuration.transpose_api_key; } // if the user has passed an output filename, override the default filename diff --git a/common/src/utils/env.rs b/common/src/utils/env.rs new file mode 100644 index 00000000..3ab61cd1 --- /dev/null +++ b/common/src/utils/env.rs @@ -0,0 +1,9 @@ +pub fn set_env(key: &str, value: &str) { + if std::env::var(key).is_err() { + std::env::set_var(key, value); + } +} + +pub fn get_env(key: &str) -> Option { + std::env::var(key).ok() +} diff --git a/common/src/utils/mod.rs b/common/src/utils/mod.rs index c817f031..d1de2976 100644 --- a/common/src/utils/mod.rs +++ b/common/src/utils/mod.rs @@ -1,3 +1,4 @@ +pub mod env; pub mod hex; pub mod http; pub mod integers; diff --git a/core/src/inspect/core/contracts.rs b/core/src/inspect/core/contracts.rs index 58cd8b84..bf251b2a 100644 --- a/core/src/inspect/core/contracts.rs +++ b/core/src/inspect/core/contracts.rs @@ -9,25 +9,36 @@ use heimdall_common::{resources::transpose::get_label, utils::hex::ToLowerHex}; #[derive(Debug, Clone)] pub struct Contracts { pub contracts: HashMap, - transpose_api_key: Option, + transpose_api_key: String, + skip_resolving: bool, } #[allow(dead_code)] impl Contracts { pub fn new(args: &InspectArgs) -> Self { - Self { contracts: HashMap::new(), transpose_api_key: args.transpose_api_key.clone() } + Self { + contracts: HashMap::new(), + transpose_api_key: args.transpose_api_key.clone(), + skip_resolving: args.skip_resolving, + } } pub async fn add(&mut self, address: Address) -> Result<(), Error> { + // if skip resolving, just add the address + if self.skip_resolving { + self.contracts.insert(address, address.to_lower_hex()); + return Ok(()); + } + // if alias already exists, don't overwrite if self.contracts.contains_key(&address) { return Ok(()); } - if let Some(transpose_api_key) = &self.transpose_api_key { + if !self.transpose_api_key.is_empty() { self.contracts.insert( address, - get_label(&address.to_lower_hex(), transpose_api_key) + get_label(&address.to_lower_hex(), &self.transpose_api_key) .await .unwrap_or(address.to_lower_hex()), ); @@ -39,15 +50,23 @@ impl Contracts { } pub async fn extend(&mut self, addresses: HashSet
) -> Result<(), Error> { + // if skip resolving, just add the address + if self.skip_resolving { + self.contracts + .extend(addresses.into_iter().map(|address| (address, address.to_lower_hex()))); + return Ok(()); + } + // for each address, get the label - if let Some(transpose_api_key) = &self.transpose_api_key { + if !self.transpose_api_key.is_empty() { + let transpose_api_key_clone = self.transpose_api_key.clone(); let handles: Vec<_> = addresses .clone() .into_iter() .map(move |address| { - let transpose_api_key = transpose_api_key.clone(); + let transpose_api_key_clone = transpose_api_key_clone.clone(); tokio::spawn(async move { - get_label(&address.to_lower_hex(), &transpose_api_key).await + get_label(&address.to_lower_hex(), &transpose_api_key_clone).await }) }) .collect(); diff --git a/core/src/inspect/core/logs.rs b/core/src/inspect/core/logs.rs index 52f1ebf7..93a52808 100644 --- a/core/src/inspect/core/logs.rs +++ b/core/src/inspect/core/logs.rs @@ -3,7 +3,7 @@ use ethers::types::{Address, Bytes, Log, H256, U256, U64}; use heimdall_common::{ debug_max, ether::signatures::{ResolveSelector, ResolvedLog}, - utils::hex::ToLowerHex, + utils::{env::get_env, hex::ToLowerHex}, }; use serde::{Deserialize, Serialize}; @@ -74,21 +74,29 @@ impl TryFrom for DecodedLog { type Error = crate::error::Error; async fn try_from(value: Log) -> Result { - let signature = match value.topics.first() { - Some(topic) => { - let topic = topic.to_lower_hex(); - Some(topic) - } - None => None, - }; - - let resolved_logs = match signature { - Some(signature) => { - debug_max!("resolving signature: {}", signature.to_string().to_lowercase()); - ResolvedLog::resolve(&signature).await.unwrap_or(Vec::new()) - } - None => Vec::new(), - }; + let mut resolved_logs = Vec::new(); + let skip_resolving = get_env("SKIP_RESOLVING") + .unwrap_or("false".to_string()) + .parse::() + .unwrap_or(false); + + if !skip_resolving { + let signature = match value.topics.first() { + Some(topic) => { + let topic = topic.to_lower_hex(); + Some(topic) + } + None => None, + }; + + resolved_logs = match signature { + Some(signature) => { + debug_max!("resolving signature: {}", signature.to_string().to_lowercase()); + ResolvedLog::resolve(&signature).await.unwrap_or(Vec::new()) + } + None => Vec::new(), + }; + } Ok(Self { address: value.address, diff --git a/core/src/inspect/core/tracing.rs b/core/src/inspect/core/tracing.rs index 5ef0c680..3ea3364e 100644 --- a/core/src/inspect/core/tracing.rs +++ b/core/src/inspect/core/tracing.rs @@ -14,6 +14,7 @@ use ethers::{ use heimdall_common::{ ether::signatures::ResolvedFunction, utils::{ + env::get_env, hex::ToLowerHex, io::{logging::TraceFactory, types::Parameterize}, }, @@ -196,6 +197,12 @@ impl TryFrom for DecodedCall { let result = crate::decode::decode( DecodeArgsBuilder::new() .target(calldata) + .skip_resolving( + get_env("SKIP_RESOLVING") + .unwrap_or("false".to_string()) + .parse::() + .unwrap_or(false), + ) .build() .map_err(|_e| Error::DecodeError)?, ) diff --git a/core/src/inspect/mod.rs b/core/src/inspect/mod.rs index a9c2f1b6..2bc37821 100644 --- a/core/src/inspect/mod.rs +++ b/core/src/inspect/mod.rs @@ -12,6 +12,7 @@ use heimdall_common::{ debug_max, ether::rpc::{get_block_logs, get_trace, get_transaction}, utils::{ + env::set_env, hex::ToLowerHex, io::logging::{Logger, TraceFactory}, }, @@ -45,9 +46,9 @@ pub struct InspectArgs { #[clap(long, short)] pub default: bool, - /// Your OPTIONAL Transpose.io API Key, used for labeling contract addresses. - #[clap(long = "transpose-api-key", short, hide_default_value = true)] - pub transpose_api_key: Option, + /// Your OPTIONAL Transpose.io API Key. Used to resolve contract labels. + #[clap(long = "transpose-api-key", short, default_value = "", hide_default_value = true)] + pub transpose_api_key: String, /// Name for the output files. #[clap(long, short, default_value = "", hide_default_value = true)] @@ -56,6 +57,10 @@ pub struct InspectArgs { /// The output directory to write the output to, or 'print' to print to the console. #[clap(long = "output", short = 'o', default_value = "output", hide_default_value = true)] pub output: String, + + /// Whether to skip resolving function selectors and contract labels. + #[clap(long = "skip-resolving")] + pub skip_resolving: bool, } impl InspectArgsBuilder { @@ -65,9 +70,10 @@ impl InspectArgsBuilder { verbose: Some(clap_verbosity_flag::Verbosity::new(0, 1)), rpc_url: Some(String::new()), default: Some(true), - transpose_api_key: None, + transpose_api_key: Some(String::new()), name: Some(String::new()), output: Some(String::from("output")), + skip_resolving: Some(false), } } } @@ -82,7 +88,6 @@ pub struct InspectResult { #[allow(deprecated)] pub async fn inspect(args: InspectArgs) -> Result { // set logger environment variable if not already set - // TODO: abstract this to a heimdall_common util if std::env::var("RUST_LOG").is_err() { std::env::set_var( "RUST_LOG", @@ -93,6 +98,10 @@ pub async fn inspect(args: InspectArgs) -> Result { ); } + // set skip_resolving env variable + // TODO: create a trait that can be added to a struct to set env variables + set_env("SKIP_RESOLVING", &args.skip_resolving.to_string()); + // get a new logger and trace let (logger, _trace) = Logger::new(match args.verbose.log_level() { Some(level) => level.as_str(), diff --git a/core/tests/test_inspect.rs b/core/tests/test_inspect.rs index 28689534..c3bca568 100644 --- a/core/tests/test_inspect.rs +++ b/core/tests/test_inspect.rs @@ -14,9 +14,10 @@ mod integration_tests { verbose: Verbosity::new(0, 0), rpc_url: String::from("https://eth.llamarpc.com"), default: true, - transpose_api_key: None, + transpose_api_key: String::from(""), name: String::from(""), output: String::from("output"), + skip_resolving: true, }; let _ = heimdall_core::inspect::inspect(args).await.unwrap(); @@ -31,9 +32,10 @@ mod integration_tests { verbose: Verbosity::new(0, 0), rpc_url: String::from("https://eth.llamarpc.com"), default: true, - transpose_api_key: None, + transpose_api_key: String::from(""), name: String::from(""), output: String::from("output"), + skip_resolving: true, }; let _ = heimdall_core::inspect::inspect(args).await.unwrap(); @@ -63,6 +65,7 @@ mod integration_tests { .target(txid.to_string()) .verbose(Verbosity::new(-1, 0)) .rpc_url("https://eth.llamarpc.com".to_string()) + .skip_resolving(true) .build() .unwrap();