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:
parent
2b1f87a1ba
commit
595f64bca6
33
src/lib.rs
33
src/lib.rs
|
@ -66,6 +66,23 @@ pub struct Signature(ffi::Signature);
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||||
pub struct RecoverableSignature(ffi::RecoverableSignature);
|
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 {
|
impl Signature {
|
||||||
#[inline]
|
#[inline]
|
||||||
/// Converts a DER-encoded byte slice to a signature
|
/// Converts a DER-encoded byte slice to a signature
|
||||||
|
@ -243,6 +260,8 @@ pub enum Error {
|
||||||
InvalidSignature,
|
InvalidSignature,
|
||||||
/// Bad secret key
|
/// Bad secret key
|
||||||
InvalidSecretKey,
|
InvalidSecretKey,
|
||||||
|
/// Bad recovery id
|
||||||
|
InvalidRecoveryId,
|
||||||
/// Boolean-returning function returned the wrong boolean
|
/// Boolean-returning function returned the wrong boolean
|
||||||
Unknown
|
Unknown
|
||||||
}
|
}
|
||||||
|
@ -734,6 +753,20 @@ mod tests {
|
||||||
assert_eq!(recid_in, recid_out);
|
assert_eq!(recid_in, recid_out);
|
||||||
assert_eq!(&bytes_in[..], &bytes_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"))]
|
#[cfg(all(test, feature = "unstable"))]
|
||||||
|
|
Loading…
Reference in New Issue