Skip to content

feat : convert --function flag data to selector #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ rand = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
ethers-core = { workspace = true }
24 changes: 24 additions & 0 deletions crates/cli/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use clap_cryo::Parser;
use color_print::cstr;
use colored::Colorize;
use cryo_freeze::ParseError;
use ethers_core::utils::keccak256;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{default::Default, path::PathBuf};
Expand Down Expand Up @@ -263,6 +265,28 @@ pub struct Args {
}

impl Args {
/// pass if it's hex, convert to hex if it's not
pub fn convert_to_selector_strings(
function_signatures: Vec<String>,
) -> Result<Vec<String>, ParseError> {
let mut selectors = Vec::new();
for signature in function_signatures {
if Args::is_hex_signature(&signature) {
// If it's already a hex string, use it directly
selectors.push(signature);
} else {
// Otherwise, hash and convert to hex
let hash = keccak256(&signature);
let selector_string = hex::encode(&hash[0..4]); // Convert to hex string
selectors.push(selector_string);
}
}
Ok(selectors)
}
// helper to check hex
fn is_hex_signature(signature: &str) -> bool {
signature.starts_with("0x") && signature.len() == 10 // 0x + 8 hex chars
}
pub(crate) fn merge_with_precedence(self, other: Args) -> Self {
let default_struct = Args::default();

Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use eyre::Result;
#[allow(clippy::needless_return)]
async fn main() -> Result<()> {
let args = Args::parse();

match run::run(args).await {
Ok(Some(freeze_summary)) if freeze_summary.errored.is_empty() => Ok(()),
Ok(Some(_freeze_summary)) => std::process::exit(1),
Expand Down
10 changes: 8 additions & 2 deletions crates/cli/src/parse/partitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,16 @@ fn parse_call_datas(
function: &Option<Vec<String>>,
inputs: &Option<Vec<String>>,
) -> Result<Option<Vec<CallDataChunk>>, ParseError> {
let call_datas = match (call_datas, function, inputs) {
let func = if let Some(function_signatures) = function {
let vec = function_signatures.clone();
Some(Args::convert_to_selector_strings(vec)?)
} else {
None
};
let call_datas = match (call_datas, &func, inputs) {
(None, None, None) => return Ok(None),
(Some(call_data), None, None) => hex_strings_to_binary(call_data)?,
(None, Some(function), None) => hex_strings_to_binary(function)?,
(None, Some(func), None) => hex_strings_to_binary(func)?,
(None, Some(function), Some(inputs)) => {
let mut call_datas = Vec::new();
for f in function.iter() {
Expand Down