Skip to content

Commit

Permalink
fix: make from_str parse 0x-prefixed strings
Browse files Browse the repository at this point in the history
  • Loading branch information
gakonst committed Dec 27, 2020
1 parent da4262d commit 405e9e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ethereum-types/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ mod tests {
}
}

#[test]
fn test_parse_0x() {
assert!("0x0000000000000000000000000000000000000000000000000000000000000000".parse::<H256>().is_ok())
}

#[test]
fn test_serialize_invalid() {
assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000\"")
Expand Down
5 changes: 5 additions & 0 deletions fixed-hash/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@ macro_rules! impl_rustc_hex_for_fixed_hash {
/// - When encountering invalid non hex-digits
/// - Upon empty string input or invalid input length in general
fn from_str(input: &str) -> $crate::core_::result::Result<$name, $crate::rustc_hex::FromHexError> {
let input = if input.starts_with("0x") {
&input[2..]
} else {
input
};
let mut iter = $crate::rustc_hex::FromHexIter::new(input);
let mut result = Self::zero();
for byte in result.as_mut() {
Expand Down

0 comments on commit 405e9e8

Please sign in to comment.