add `ThirtyTwoByteHash` hash trait which can be implemented for easier conversion of things to `Message`s

This commit is contained in:
Andrew Poelstra 2018-08-15 17:33:51 +00:00
parent 1f4a4c11a3
commit e5a02bd9a0
1 changed files with 15 additions and 0 deletions

View File

@ -205,6 +205,14 @@ fn from_str(s: &str) -> Result<Signature, Error> {
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct RecoverableSignature(ffi::RecoverableSignature);
/// Trait describing something that promises to be a 32-byte random number; in particular,
/// it has negligible probability of being zero or overflowing the group order. Such objects
/// may be converted to `Message`s without any error paths.
pub trait ThirtyTwoByteHash {
/// Converts the object into a 32-byte array
fn into_32(self) -> [u8; 32];
}
impl RecoveryId {
#[inline]
/// Allows library users to create valid recovery IDs from i32.
@ -519,6 +527,13 @@ impl Message {
}
}
impl<T: ThirtyTwoByteHash> From<T> for Message {
/// Converts a 32-byte hash directly to a message without error paths
fn from(t: T) -> Message {
Message(t.into_32())
}
}
/// An ECDSA error
#[derive(Copy, PartialEq, Eq, Clone, Debug)]
pub enum Error {