From 6265b2558ab26417af8aa0c82288920d1d21c065 Mon Sep 17 00:00:00 2001 From: sanket1729 Date: Sun, 25 Apr 2021 22:43:54 -0700 Subject: [PATCH] Fix SecretKey FromStr bug Secret::from_str did not check if the secret key was a valid one or not. --- src/key.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/key.rs b/src/key.rs index ca4ea53..4adeb56 100644 --- a/src/key.rs +++ b/src/key.rs @@ -51,7 +51,7 @@ impl str::FromStr for SecretKey { fn from_str(s: &str) -> Result { let mut res = [0; constants::SECRET_KEY_SIZE]; match from_hex(s, &mut res) { - Ok(constants::SECRET_KEY_SIZE) => Ok(SecretKey(res)), + Ok(constants::SECRET_KEY_SIZE) => SecretKey::from_slice(&res), _ => Err(Error::InvalidSecretKey) } } @@ -525,6 +525,10 @@ mod test { fn invalid_secret_key() { // Zero assert_eq!(SecretKey::from_slice(&[0; 32]), Err(InvalidSecretKey)); + assert_eq!( + SecretKey::from_str(&format!("0000000000000000000000000000000000000000000000000000000000000000")), + Err(InvalidSecretKey) + ); // -1 assert_eq!(SecretKey::from_slice(&[0xff; 32]), Err(InvalidSecretKey)); // Top of range