Skip to content

Commit

Permalink
feat: add mesc --rpc-url support (#320)
Browse files Browse the repository at this point in the history
* add mesc

* fix(config): handle mesc error

* fix(config): handle mesc error

* fix(config): handle mesc error

* fix(config): handle mesc error

---------

Co-authored-by: Jonathan Becker <jonathan@jbecker.dev>
  • Loading branch information
sslivkoff and Jon-Becker authored Feb 1, 2024
1 parent 65c1c3a commit 3fc0cd6
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 9 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ keywords = ["ethereum", "web3", "decompiler", "evm", "crypto"]
heimdall-common = { path = "./../common" }
clap-verbosity-flag = "1.0.0"
clap = { version = "3.1.18", features = ["derive"] }
mesc = { version = "0.2.0" }
serde = { version = "1.0", features = ["derive"] }
toml = { version = "0.7.6" }
thiserror = "1.0.50"
Expand Down
45 changes: 43 additions & 2 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod error;
use crate::error::Error;
use clap::{AppSettings, Parser};
use heimdall_common::{
error, info, success,
debug, error, info, success,
utils::io::file::{delete_path, read_file, write_file},
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -74,8 +74,39 @@ impl Configuration {
.map_err(|e| Error::Generic(format!("failed to read config file: {}", e)))?;

// parse the config file
let config: Configuration = toml::from_str(&contents)
let mut config: Configuration = toml::from_str(&contents)
.map_err(|e| Error::ParseError(format!("failed to parse config file: {}", e)))?;

// load mesc config if enabled
if !mesc::is_mesc_enabled() {
return Ok(config);
}

if let Some(endpoint) = mesc::get_default_endpoint(Some("heimdall"))
.map_err(|e| Error::Generic(format!("MESC error: {}", e)))?
{
debug!("overriding rpc_url with mesc endpoint");
config.rpc_url = endpoint.url;
}
if let Some(key) = mesc::metadata::get_api_key("etherscan", Some("heimdall"))
.map_err(|e| Error::Generic(format!("MESC error: {}", e)))?
{
debug!("overriding etherscan_api_key with mesc key");
config.etherscan_api_key = key;
}
if let Some(key) = mesc::metadata::get_api_key("transpose", Some("heimdall"))
.map_err(|e| Error::Generic(format!("MESC error: {}", e)))?
{
debug!("overriding transpose_api_key with mesc key");
config.transpose_api_key = key;
}
if let Some(key) = mesc::metadata::get_api_key("openai", Some("heimdall"))
.map_err(|e| Error::Generic(format!("MESC error: {}", e)))?
{
debug!("overriding openai_api_key with mesc key");
config.openai_api_key = key;
}

Ok(config)
}

Expand Down Expand Up @@ -169,6 +200,16 @@ pub fn config(args: ConfigArgs) -> Result<(), Error> {
Ok(())
}

/// Parse user input --rpc-url into a full url
pub fn parse_url_arg(url: &str) -> Result<String, String> {
if mesc::is_mesc_enabled() {
if let Ok(Some(endpoint)) = mesc::get_endpoint_by_query(url, Some("heimdall")) {
return Ok(endpoint.url)
}
}
Ok(url.to_string())
}

#[allow(deprecated)]
#[cfg(test)]
mod tests {
Expand Down
4 changes: 3 additions & 1 deletion core/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use heimdall_common::{
},
warn,
};
use heimdall_config::parse_url_arg;
use indicatif::ProgressBar;
use std::time::Duration;

Expand Down Expand Up @@ -46,7 +47,8 @@ pub struct CFGArgs {
pub verbose: clap_verbosity_flag::Verbosity,

/// The RPC provider to use for fetching target bytecode.
#[clap(long = "rpc-url", short, default_value = "", hide_default_value = true)]
/// This can be an explicit URL or a reference to a MESC endpoint.
#[clap(long, short, parse(try_from_str = parse_url_arg), default_value = "", hide_default_value = true)]
pub rpc_url: String,

/// When prompted, always select the default value.
Expand Down
4 changes: 3 additions & 1 deletion core/src/decode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use heimdall_common::{
},
warn,
};
use heimdall_config::parse_url_arg;

use indicatif::ProgressBar;

Expand All @@ -55,7 +56,8 @@ pub struct DecodeArgs {
pub verbose: clap_verbosity_flag::Verbosity,

/// The RPC provider to use for fetching target calldata.
#[clap(long = "rpc-url", short, default_value = "", hide_default_value = true)]
/// This can be an explicit URL or a reference to a MESC endpoint.
#[clap(long, short, parse(try_from_str = parse_url_arg), default_value = "", hide_default_value = true)]
pub rpc_url: String,

/// Your OpenAI API key, used for explaining calldata.
Expand Down
4 changes: 3 additions & 1 deletion core/src/decompile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use heimdall_common::{
},
warn,
};
use heimdall_config::parse_url_arg;

use crate::{
decompile::{
Expand Down Expand Up @@ -63,7 +64,8 @@ pub struct DecompilerArgs {
pub verbose: clap_verbosity_flag::Verbosity,

/// The RPC provider to use for fetching target bytecode.
#[clap(long = "rpc-url", short, default_value = "", hide_default_value = true)]
/// This can be an explicit URL or a reference to a MESC endpoint.
#[clap(long, short, parse(try_from_str = parse_url_arg), default_value = "", hide_default_value = true)]
pub rpc_url: String,

/// When prompted, always select the default value.
Expand Down
4 changes: 3 additions & 1 deletion core/src/disassemble/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use heimdall_common::{
info,
utils::{io::logging::set_logger_env, strings::encode_hex},
};
use heimdall_config::parse_url_arg;

use crate::error::Error;

Expand All @@ -24,7 +25,8 @@ pub struct DisassemblerArgs {
pub verbose: clap_verbosity_flag::Verbosity,

/// The RPC provider to use for fetching target bytecode.
#[clap(long = "rpc-url", short, default_value = "", hide_default_value = true)]
/// This can be an explicit URL or a reference to a MESC endpoint.
#[clap(long, short, parse(try_from_str = parse_url_arg), default_value = "", hide_default_value = true)]
pub rpc_url: String,

/// Whether to use base-10 for the program counter.
Expand Down
4 changes: 3 additions & 1 deletion core/src/dump/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use heimdall_common::{
resources::transpose::{get_contract_creation, get_transaction_list},
utils::io::logging::*,
};
use heimdall_config::parse_url_arg;
use std::{collections::HashMap, env, str::FromStr, time::Instant};

use crate::error::Error;
Expand Down Expand Up @@ -44,7 +45,8 @@ pub struct DumpArgs {
pub output: String,

/// The RPC URL to use for fetching data.
#[clap(long = "rpc-url", short, default_value = "", hide_default_value = true)]
/// This can be an explicit URL or a reference to a MESC endpoint.
#[clap(long, short, parse(try_from_str = parse_url_arg), default_value = "", hide_default_value = true)]
pub rpc_url: String,

/// Your Transpose.io API Key
Expand Down
4 changes: 3 additions & 1 deletion core/src/inspect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use heimdall_common::{
},
warn,
};
use heimdall_config::parse_url_arg;

use crate::error::Error;

Expand All @@ -40,7 +41,8 @@ pub struct InspectArgs {
pub verbose: clap_verbosity_flag::Verbosity,

/// The RPC provider to use for fetching target calldata.
#[clap(long = "rpc-url", short, default_value = "", hide_default_value = true)]
/// This can be an explicit URL or a reference to a MESC endpoint.
#[clap(long, short, parse(try_from_str = parse_url_arg), default_value = "", hide_default_value = true)]
pub rpc_url: String,

/// When prompted, always select the default value.
Expand Down
4 changes: 3 additions & 1 deletion core/src/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use heimdall_common::{
},
warn,
};
use heimdall_config::parse_url_arg;

use std::{
collections::{HashMap, HashSet},
Expand Down Expand Up @@ -62,7 +63,8 @@ pub struct SnapshotArgs {
pub verbose: clap_verbosity_flag::Verbosity,

/// The RPC provider to use for fetching target bytecode.
#[clap(long = "rpc-url", short, default_value = "", hide_default_value = true)]
/// This can be an explicit URL or a reference to a MESC endpoint.
#[clap(long, short, parse(try_from_str = parse_url_arg), default_value = "", hide_default_value = true)]
pub rpc_url: String,

/// When prompted, always select the default value.
Expand Down

0 comments on commit 3fc0cd6

Please sign in to comment.