Skip to content

Commit 272439c

Browse files
authored
Handle newlines for bytes data in cast keccak (#12339)
* Add cast keccak test * Trim end for bytes data in SimpleCast::keccak
1 parent de2adb9 commit 272439c

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

crates/cast/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,8 +2095,11 @@ impl SimpleCast {
20952095
/// ```
20962096
pub fn keccak(data: &str) -> Result<String> {
20972097
// Hex-decode if data starts with 0x.
2098-
let hash =
2099-
if data.starts_with("0x") { keccak256(hex::decode(data)?) } else { keccak256(data) };
2098+
let hash = if data.starts_with("0x") {
2099+
keccak256(hex::decode(data.trim_end())?)
2100+
} else {
2101+
keccak256(data)
2102+
};
21002103
Ok(hash.to_string())
21012104
}
21022105

crates/cast/tests/cli/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4232,6 +4232,20 @@ casttest!(erc20_allowance_success, |_prj, cmd| {
42324232
"#]]);
42334233
});
42344234

4235+
casttest!(keccak_stdin_bytes, |_prj, cmd| {
4236+
cmd.args(["keccak"]).stdin("0x12").assert_success().stdout_eq(str![[r#"
4237+
0x5fa2358263196dbbf23d1ca7a509451f7a2f64c15837bfbb81298b1e3e24e4fa
4238+
4239+
"#]]);
4240+
});
4241+
4242+
casttest!(keccak_stdin_bytes_with_newline, |_prj, cmd| {
4243+
cmd.args(["keccak"]).stdin("0x12\n").assert_success().stdout_eq(str![[r#"
4244+
0x5fa2358263196dbbf23d1ca7a509451f7a2f64c15837bfbb81298b1e3e24e4fa
4245+
4246+
"#]]);
4247+
});
4248+
42354249
// tests that `cast erc20 transfer` and `cast erc20 approve` commands works correctly
42364250
forgetest_async!(erc20_transfer_approve_success, |prj, cmd| {
42374251
let (_, handle) = anvil::spawn(NodeConfig::test()).await;

0 commit comments

Comments
 (0)