Skip to content
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

fix: make from_str parse 0x-prefixed strings #487

Merged
merged 6 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
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
5 changes: 5 additions & 0 deletions uint/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,11 @@ macro_rules! construct_uint {
type Err = $crate::FromHexError;

fn from_str(value: &str) -> $crate::core_::result::Result<$name, Self::Err> {
let value = if value.starts_with("0x") {
&value[2..]
} else {
value
};
const BYTES_LEN: usize = $n_words * 8;
const MAX_ENCODED_LEN: usize = BYTES_LEN * 2;

Expand Down
2 changes: 2 additions & 0 deletions uint/tests/uint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ fn uint256_from() {

// test initializtion from string
let sa = U256::from_str("0a").unwrap();
let sa2 = U256::from_str("0x0a").unwrap();
assert_eq!(sa2, sa);
assert_eq!(e, sa);
assert_eq!(U256([0, 0, 0, 0]), U256::from_str("").unwrap());
assert_eq!(U256([0x1, 0, 0, 0]), U256::from_str("1").unwrap());
Expand Down