From a69036f07b183f2a3050359f690f508a9131b175 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 14 Feb 2023 22:15:58 +0100 Subject: [PATCH] fix(core): block FromStr implementation (#2155) --- ethers-core/src/types/block.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/ethers-core/src/types/block.rs b/ethers-core/src/types/block.rs index 0359c4b8..e43813ef 100644 --- a/ethers-core/src/types/block.rs +++ b/ethers-core/src/types/block.rs @@ -535,8 +535,7 @@ impl FromStr for BlockId { type Err = String; fn from_str(s: &str) -> Result { - let s = s.strip_prefix("0x").unwrap_or(s); - if s.len() == 64 { + if s.starts_with("0x") && s.len() == 66 { let hash = s.parse::().map_err(|e| e.to_string()); hash.map(Self::Hash) } else { @@ -645,15 +644,10 @@ impl FromStr for BlockNumber { "safe" => Ok(Self::Safe), "earliest" => Ok(Self::Earliest), "pending" => Ok(Self::Pending), - n => { - if let Ok(n) = n.parse::() { - Ok(Self::Number(n)) - } else if let Ok(n) = n.parse::() { - Ok(Self::Number(n.into())) - } else { - Err("Invalid block number".into()) - } - } + // hex + n if n.starts_with("0x") => n.parse().map(Self::Number).map_err(|e| e.to_string()), + // decimal + n => n.parse::().map(|n| Self::Number(n.into())).map_err(|e| e.to_string()), } } }