Skip to content

Commit

Permalink
ParsePublicKeyError using hex::InvalidCharError
Browse files Browse the repository at this point in the history
Replaced the InvalidChar variant u8 with hex::InvalidCharError
  • Loading branch information
rockcoolsaint committed Jan 1, 2025
1 parent 16b5220 commit c27b5e7
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions bitcoin/src/crypto/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,14 @@ impl FromStr for PublicKey {
match s.len() {
66 => {
let bytes = <[u8; 33]>::from_hex(s).map_err(|e| match e {
InvalidChar(e) => ParsePublicKeyError::InvalidChar(e.invalid_char()),
InvalidChar(e) => ParsePublicKeyError::InvalidChar(e),
InvalidLength(_) => unreachable!("length checked already"),
})?;
Ok(PublicKey::from_slice(&bytes)?)
}
130 => {
let bytes = <[u8; 65]>::from_hex(s).map_err(|e| match e {
InvalidChar(e) => ParsePublicKeyError::InvalidChar(e.invalid_char()),
InvalidChar(e) => ParsePublicKeyError::InvalidChar(e),
InvalidLength(_) => unreachable!("length checked already"),
})?;
Ok(PublicKey::from_slice(&bytes)?)
Expand Down Expand Up @@ -1017,7 +1017,7 @@ pub enum ParsePublicKeyError {
/// Error originated while parsing string.
Encoding(FromSliceError),
/// Hex decoding error.
InvalidChar(u8),
InvalidChar(hex::InvalidCharError),
/// `PublicKey` hex should be 66 or 130 digits long.
InvalidHexLength(usize),
}
Expand Down Expand Up @@ -1520,12 +1520,22 @@ mod tests {
assert_eq!(s.len(), 130);
let res = s.parse::<PublicKey>();
assert!(res.is_err());
assert_eq!(res.unwrap_err(), ParsePublicKeyError::InvalidChar(103));
if let Err(ParsePublicKeyError::InvalidChar(err)) = res {
assert_eq!(err.invalid_char(), b'g');
assert_eq!(err.pos(), 129);
} else {
panic!("Expected Invalid char error");
}

let s = "032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1ag";
assert_eq!(s.len(), 66);
let res = s.parse::<PublicKey>();
assert!(res.is_err());
assert_eq!(res.unwrap_err(), ParsePublicKeyError::InvalidChar(103));
if let Err(ParsePublicKeyError::InvalidChar(err)) = res {
assert_eq!(err.invalid_char(), b'g');
assert_eq!(err.pos(), 65);
} else {
panic!("Expected Invalid char error");
}
}
}

0 comments on commit c27b5e7

Please sign in to comment.