Skip to content

Commit

Permalink
chore: merge main into jon-becker/common-hardening
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Becker committed Jan 30, 2024
2 parents 3ac10e2 + c4b94c3 commit 372215d
Show file tree
Hide file tree
Showing 18 changed files with 399 additions and 475 deletions.
358 changes: 147 additions & 211 deletions Cargo.lock

Large diffs are not rendered by default.

24 changes: 10 additions & 14 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ use crossterm::{
};

use heimdall_cache::{cache, CacheArgs};
use heimdall_common::utils::{
io::{
file::{write_file, write_lines_to_file},
logging::Logger,
use heimdall_common::{
fatal, info,
utils::{
io::file::{write_file, write_lines_to_file},
version::{current_version, remote_version},
},
version::{current_version, remote_version},
};
use heimdall_config::{config, ConfigArgs, Configuration};
use heimdall_core::{
Expand Down Expand Up @@ -101,12 +101,11 @@ async fn main() -> Result<(), Error> {

// print the panic message
let backtrace = Backtrace::new();
let (logger, _) = Logger::new("TRACE");
logger.fatal(&format!(
fatal!(
"thread 'main' encountered a fatal error: '{}'!",
panic_info.to_string().bright_white().on_bright_red().bold(),
));
logger.fatal(&format!("Stack Trace:\n\n{backtrace:#?}"));
);
fatal!("Stack Trace:\n\n{:?}", backtrace);
}));

let configuration = Configuration::load()
Expand Down Expand Up @@ -479,11 +478,8 @@ async fn main() -> Result<(), Error> {
let current_version = current_version();

if remote_version.gt(&current_version) {
let (logger, _) = Logger::new("TRACE");
println!();
logger.info("great news! An update is available!");
logger
.info(&format!("you can update now by running: `bifrost --version {remote_version}`"));
info!("great news! An update is available!");
info!("you can update now by running: `bifrost --version {}`", remote_version);
}

Ok(())
Expand Down
13 changes: 5 additions & 8 deletions common/src/ether/bytecode.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
use super::rpc::get_code;
use crate::{
constants::{ADDRESS_REGEX, BYTECODE_REGEX},
error::Error,
utils::{io::logging::Logger, strings::decode_hex},
error,
utils::strings::decode_hex,
Error,
};
use std::fs;

/// Get the bytecode from the target, which can be a contract address, a bytecode or a file path.
pub async fn get_bytecode_from_target(target: &str, rpc_url: &str) -> Result<Vec<u8>, Error> {
let logger = Logger::default();

if ADDRESS_REGEX.is_match(target).unwrap_or(false) {
// Target is a contract address, so we need to fetch the bytecode from the RPC provider.
get_code(target, rpc_url).await.map_err(|e| {
Error::Generic(format!("failed to fetch bytecode from RPC provider: {}", e))
})
} else if BYTECODE_REGEX.is_match(target).unwrap_or(false) {
// Target is already a bytecode, so we just need to remove 0x from the begining
Ok(decode_hex(target)?)
} else {
// Target is a file path, so we need to read the bytecode from the file.
let contents = fs::read_to_string(target).map_err(|e| {
logger.error(&format!("failed to open file '{}' .", &target));
error!("failed to open file '{}' .", &target);
Error::FilesystemError(e)
})?;

let contents = contents.replace('\n', "");
if BYTECODE_REGEX.is_match(&contents).unwrap_or(false) && contents.len() % 2 == 0 {
Ok(decode_hex(&contents)?)
} else {
logger.error(&format!("file '{}' doesn't contain valid bytecode.", &target));
error!("file '{}' doesn't contain valid bytecode.", &target);
return Err(Error::ParseError(format!(
"file '{}' doesn't contain valid bytecode.",
&target
Expand Down
Loading

0 comments on commit 372215d

Please sign in to comment.