Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Secret - from hash function, also validate data (#4159)
Browse files Browse the repository at this point in the history
* from hash for secret

* checked from_slice

* move assert

* remove fromhash
  • Loading branch information
NikVolf authored and gavofyork committed Jan 16, 2017
1 parent bac6293 commit f807aa6
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions ethkey/src/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ impl fmt::Debug for Secret {
}

impl Secret {
pub fn from_slice(key: &[u8]) -> Result<Self, Error> {
if key.len() != 32 {
return Err(Error::InvalidSecret);
}
fn from_slice_unchecked(key: &[u8]) -> Self {
assert_eq!(32, key.len(), "Caller should provide 32-byte length slice");

let mut h = H256::default();
h.copy_from_slice(&key[0..32]);
Ok(Secret { inner: h })
Secret { inner: h }
}

pub fn from_slice(key: &[u8]) -> Result<Self, Error> {
let secret = key::SecretKey::from_slice(&super::SECP256K1, key)?;
Ok(secret.into())
}
}

Expand All @@ -54,8 +57,7 @@ impl FromStr for Secret {

impl From<key::SecretKey> for Secret {
fn from(key: key::SecretKey) -> Self {
Self::from_slice(&key[0..32])
.expect("`key::SecretKey` is valid (no way to construct invalid one); qed")
Self::from_slice_unchecked(&key[0..32])
}
}

Expand All @@ -66,4 +68,3 @@ impl Deref for Secret {
&self.inner
}
}

0 comments on commit f807aa6

Please sign in to comment.