Secret - from hash function, also validate data (#4159)

* from hash for secret

* checked from_slice

* move assert

* remove fromhash
This commit is contained in:
Nikolay Volf 2017-01-16 18:50:20 +03:00 committed by Gav Wood
parent bac6293309
commit f807aa65a1

View File

@ -33,14 +33,17 @@ impl fmt::Debug for Secret {
} }
impl Secret { impl Secret {
pub fn from_slice(key: &[u8]) -> Result<Self, Error> { fn from_slice_unchecked(key: &[u8]) -> Self {
if key.len() != 32 { assert_eq!(32, key.len(), "Caller should provide 32-byte length slice");
return Err(Error::InvalidSecret);
}
let mut h = H256::default(); let mut h = H256::default();
h.copy_from_slice(&key[0..32]); 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())
} }
} }
@ -54,8 +57,7 @@ impl FromStr for Secret {
impl From<key::SecretKey> for Secret { impl From<key::SecretKey> for Secret {
fn from(key: key::SecretKey) -> Self { 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")
} }
} }
@ -66,4 +68,3 @@ impl Deref for Secret {
&self.inner &self.inner
} }
} }