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
1 changed files with 9 additions and 8 deletions

View File

@ -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())
}
}
@ -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])
}
}
@ -66,4 +68,3 @@ impl Deref for Secret {
&self.inner
}
}