Adding to_i32 and from_i32 functions to RecoveryId in order to give

library users the ability to create RecoveryId objects and convert them to i32 equivalents, without allowing users to create invalid ones.
This commit is contained in:
Matt Quinn 2015-10-11 10:29:53 -07:00
parent 2b1f87a1ba
commit 595f64bca6
1 changed files with 33 additions and 0 deletions

View File

@ -66,6 +66,23 @@ pub struct Signature(ffi::Signature);
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct RecoverableSignature(ffi::RecoverableSignature);
impl RecoveryId {
#[inline]
/// Allows library users to create valid recovery IDs from i32.
pub fn from_i32(id: i32) -> Result<RecoveryId, Error> {
match id {
0 | 1 | 2 | 3 => Ok(RecoveryId(id)),
_ => Err(Error::InvalidRecoveryId)
}
}
#[inline]
/// Allows library users to convert recovery IDs to i32.
pub fn to_i32(&self) -> i32 {
self.0
}
}
impl Signature {
#[inline]
/// Converts a DER-encoded byte slice to a signature
@ -243,6 +260,8 @@ pub enum Error {
InvalidSignature,
/// Bad secret key
InvalidSecretKey,
/// Bad recovery id
InvalidRecoveryId,
/// Boolean-returning function returned the wrong boolean
Unknown
}
@ -734,6 +753,20 @@ mod tests {
assert_eq!(recid_in, recid_out);
assert_eq!(&bytes_in[..], &bytes_out[..]);
}
#[test]
fn test_recov_id_conversion_between_i32() {
assert!(RecoveryId::from_i32(-1).is_err());
assert!(RecoveryId::from_i32(0).is_ok());
assert!(RecoveryId::from_i32(1).is_ok());
assert!(RecoveryId::from_i32(2).is_ok());
assert!(RecoveryId::from_i32(3).is_ok());
assert!(RecoveryId::from_i32(4).is_err());
let id0 = RecoveryId::from_i32(0).unwrap();
assert_eq!(id0.to_i32(), 0);
let id1 = RecoveryId(1);
assert_eq!(id1.to_i32(), 1);
}
}
#[cfg(all(test, feature = "unstable"))]