-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Secret - from hash function, also validate data #4159
Conversation
I think we should validate the hash before turning it into secret: https://github.com/apoelstra/rust-secp256k1/blob/master/src/key.rs#L86 More: https://en.bitcoin.it/wiki/Private_key#Range_of_valid_ECDSA_private_keys |
Hmm I thought of this as a shortcut of |
Darn, you're absolutely right. I wanted to use |
Changes Unknown when pulling 20be2de on ethkey-from into ** on master**. |
@tomusdrw |
Changes Unknown when pulling 2837bcd on ethkey-from into ** on master**. |
@@ -54,17 +63,11 @@ impl FromStr for Secret { | |||
|
|||
impl From<key::SecretKey> for Secret { | |||
fn from(key: key::SecretKey) -> Self { | |||
Self::from_slice(&key[0..32]) | |||
Self::from_slice_unchecked(&key[0..32]) | |||
.expect("`key::SecretKey` is valid (no way to construct invalid one); qed") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could get rid of this expect by moving .len() != 32
check to from_slice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced it with just assert
since SecretKey
have got this check already
Changes Unknown when pulling 82df539 on ethkey-from into ** on master**. |
} | ||
|
||
pub fn from_hash(key: &H256) -> Result<Self, Error> { | ||
Self::from_slice(&**key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this function necessary? From an abstract standpoint getting a secret from a hash doesn't make a whole lot of sense and from a code standpoint the H256
already has a Deref
impl.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
though it was from what pr started, now indeed there is no need in it
was hoping to use into()
in few cases, now not :)
|
||
pub fn from_slice(key: &[u8]) -> Result<Self, Error> { | ||
let secret = key::SecretKey::from_slice(&super::SECP256K1, key)?; | ||
Ok(secret.into()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could also .map(Into::into)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with .map_err(Into::into)
:/ which doesn't look all that nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, yeah. probably cleaner this way then :)
No description provided.