Skip to content

Fix/cast abi strict args #11189

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 5 commits into
base: master
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
7 changes: 2 additions & 5 deletions crates/cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,10 +1809,7 @@ impl SimpleCast {
let func = get_func(sig)?;
match encode_function_args(&func, args) {
Ok(res) => Ok(hex::encode_prefixed(&res[4..])),
Err(e) => eyre::bail!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also handle the abi_encode_packed branch in CastSubcommand::AbiEncode

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invoked by cast abi-encode "<signature>" <value> --packed

"Could not ABI encode the function and arguments. Did you pass in the right types?\nError\n{}",
e
),
Err(e) => eyre::bail!("Could not ABI encode the function and arguments: {}", e),
}
}

Expand Down Expand Up @@ -1843,7 +1840,7 @@ impl SimpleCast {
let encoded = match encode_function_args_packed(&func, args) {
Ok(res) => hex::encode(res),
Err(e) => eyre::bail!(
"Could not ABI encode the function and arguments. Did you pass in the right types?\nError\n{}",
"Could not ABI encode the function and arguments: {}",
e
),
};
Expand Down
52 changes: 52 additions & 0 deletions crates/common/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let args: Vec<S> = args.into_iter().collect();

if inputs.len() != args.len() {
eyre::bail!("encode length mismatch: expected {} types, got {}", inputs.len(), args.len())
}

std::iter::zip(inputs, args)
.map(|(input, arg)| coerce_value(&input.selector_type(), arg.as_ref()))
.collect()
Expand Down Expand Up @@ -47,6 +53,13 @@ where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{

let args: Vec<S> = args.into_iter().collect();

if func.inputs.len() != args.len() {
eyre::bail!("encode length mismatch: expected {} types, got {}", func.inputs.len(), args.len())
}

let params: Vec<Vec<u8>> = std::iter::zip(&func.inputs, args)
.map(|(input, arg)| coerce_value(&input.selector_type(), arg.as_ref()))
.collect::<Result<Vec<_>>>()?
Expand Down Expand Up @@ -257,4 +270,43 @@ mod tests {
assert_eq!(parsed.indexed[1], DynSolValue::Uint(U256::from_be_bytes([3; 32]), 256));
assert_eq!(parsed.indexed[2], DynSolValue::Address(Address::from_word(param2)));
}

#[test]
fn test_encode_args_length_validation() {
use alloy_json_abi::Param;

let params = vec![
Param {
name: "a".to_string(),
ty: "uint256".to_string(),
internal_type: None,
components: vec![],
},
Param {
name: "b".to_string(),
ty: "address".to_string(),
internal_type: None,
components: vec![],
},
];

// Less arguments than parameters
let args = vec!["1"];
let res = encode_args(&params, &args);
assert!(res.is_err());
assert!(format!("{}", res.unwrap_err()).contains("encode length mismatch"));

// Exact number of arguments and parameters
let args = vec!["1", "0x0000000000000000000000000000000000000001"];
let res = encode_args(&params, &args);
assert!(res.is_ok());
let values = res.unwrap();
assert_eq!(values.len(), 2);

// More arguments than parameters
let args = vec!["1", "0x0000000000000000000000000000000000000001", "extra"];
let res = encode_args(&params, &args);
assert!(res.is_err());
assert!(format!("{}", res.unwrap_err()).contains("encode length mismatch"));
}
}