From bea3ddc3caf5da495e8e3ade78283ba24d53e04a Mon Sep 17 00:00:00 2001 From: "F.B. Jiang" Date: Sat, 5 Feb 2022 02:58:18 +0800 Subject: [PATCH] fix cast keccak bug (#671) * fix cast keccak bug * chore: add doc comment on 0x prefix Co-authored-by: Georgios Konstantopoulos --- cast/src/lib.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cast/src/lib.rs b/cast/src/lib.rs index 8617c61702bb..da5f3a715cbd 100644 --- a/cast/src/lib.rs +++ b/cast/src/lib.rs @@ -1033,12 +1033,20 @@ impl SimpleCast { /// fn main() -> eyre::Result<()> { /// assert_eq!(Cast::keccak("foo")?, "0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d"); /// assert_eq!(Cast::keccak("123abc")?, "0xb1f1c74a1ba56f07a892ea1110a39349d40f66ca01d245e704621033cb7046a4"); + /// assert_eq!(Cast::keccak("0x12")?, "0x5fa2358263196dbbf23d1ca7a509451f7a2f64c15837bfbb81298b1e3e24e4fa"); + /// assert_eq!(Cast::keccak("12")?, "0x7f8b6b088b6d74c2852fc86c796dca07b44eed6fb3daf5e6b59f7c364db14528"); /// /// Ok(()) /// } /// ``` pub fn keccak(data: &str) -> Result { - let hash: String = keccak256(data.as_bytes()).to_hex(); + let hash: String = match data.as_bytes() { + // If has a 0x prefix, read it as hexdata. + // If has no 0x prefix, read it as text + [b'0', b'x', rest @ ..] => keccak256(hex::decode(rest)?).to_hex(), + _ => keccak256(data).to_hex(), + }; + Ok(format!("0x{}", hash)) }