From efc4406c14109ba02ed95ac6e9454b11e694e0c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ian=20K=2E=20Guimar=C3=A3es?= Date: Thu, 30 May 2024 20:40:58 -0300 Subject: [PATCH] refactor(evm core types): change find_cast return type from String to ParamType (#415) Co-authored-by: Jon-Becker --- Cargo.lock | 1 + crates/common/src/ether/evm/core/types.rs | 9 +++++---- crates/decompile/src/utils/postprocessors/bitwise.rs | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fc2ab792..13546f35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1938,6 +1938,7 @@ dependencies = [ name = "heimdall-core" version = "0.8.1" dependencies = [ + "alloy-json-abi", "async-convert", "async-recursion", "clap", diff --git a/crates/common/src/ether/evm/core/types.rs b/crates/common/src/ether/evm/core/types.rs index 8087c9d2..90bac483 100644 --- a/crates/common/src/ether/evm/core/types.rs +++ b/crates/common/src/ether/evm/core/types.rs @@ -284,17 +284,18 @@ pub fn byte_size_to_type(byte_size: usize) -> (usize, Vec) { } /// Given a string (typically a line of decompiled source code), extract a type cast if one exists. -// TODO: instead of returning a String, return a ParamType +/// /// ``` /// use heimdall_common::ether::evm::core::types::find_cast; +/// use ethers::abi::ParamType; /// /// let line = "uint256(0x000011)"; /// let (range, cast_type) = find_cast(line).expect("failed to find type cast"); /// assert_eq!(range, 8..16); /// assert_eq!(&line[range], "0x000011"); -/// assert_eq!(cast_type, "uint256"); +/// assert_eq!(cast_type, ParamType::Uint(256)); /// ``` -pub fn find_cast(line: &str) -> Result<(Range, String), Error> { +pub fn find_cast(line: &str) -> Result<(Range, ParamType), Error> { // find the start of the cast match TYPE_CAST_REGEX.find(line).expect("Failed to find type cast.") { Some(m) => { @@ -304,7 +305,7 @@ pub fn find_cast(line: &str) -> Result<(Range, String), Error> { // find where the cast ends let range = find_balanced_encapsulator(&line[end..], ('(', ')'))?; - Ok((end + range.start..end + range.end, cast_type)) + Ok((end + range.start..end + range.end, to_type(&cast_type))) } None => Err(Error::ParseError("failed to find type cast".to_string())), } diff --git a/crates/decompile/src/utils/postprocessors/bitwise.rs b/crates/decompile/src/utils/postprocessors/bitwise.rs index 6998f61a..f03fae1a 100644 --- a/crates/decompile/src/utils/postprocessors/bitwise.rs +++ b/crates/decompile/src/utils/postprocessors/bitwise.rs @@ -131,8 +131,9 @@ pub fn simplify_casts(line: &str) -> String { let cleaned_cast_pre = cleaned[0..cast_range.start - 1].to_string(); let cleaned_cast_post = cleaned[cast_range.end + 1..].to_string(); - let cleaned_cast = - cleaned[cast_range.start - 1..cast_range.end + 1].to_string().replace(&cast, ""); + let cleaned_cast = cleaned[cast_range.start - 1..cast_range.end + 1] + .to_string() + .replace(&cast.to_string(), ""); cleaned = format!("{cleaned_cast_pre}{cleaned_cast}{cleaned_cast_post}");