From 9f2bbff5f1a1a041d224aa39be7a872880f71579 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 20 Nov 2024 12:25:30 +0100 Subject: [PATCH] chore: add back to/from i32 fns --- src/ecdsa/recovery.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/ecdsa/recovery.rs b/src/ecdsa/recovery.rs index 04aaf3690..0d9a61b4d 100644 --- a/src/ecdsa/recovery.rs +++ b/src/ecdsa/recovery.rs @@ -25,10 +25,10 @@ pub enum RecoveryId { Three, } -impl TryFrom for RecoveryId { - type Error = Error; +impl RecoveryId { + /// Allows library users to create valid recovery IDs from i32. #[inline] - fn try_from(id: i32) -> Result { + pub fn from_i32(id: i32) -> Result { match id { 0 => Ok(RecoveryId::Zero), 1 => Ok(RecoveryId::One), @@ -37,12 +37,11 @@ impl TryFrom for RecoveryId { _ => Err(Error::InvalidRecoveryId), } } -} -impl From for i32 { + /// Allows library users to convert recovery IDs to i32. #[inline] - fn from(val: RecoveryId) -> Self { - match val { + pub fn to_i32(self) -> i32 { + match self { RecoveryId::Zero => 0, RecoveryId::One => 1, RecoveryId::Two => 2, @@ -51,6 +50,17 @@ impl From for i32 { } } +impl TryFrom for RecoveryId { + type Error = Error; + #[inline] + fn try_from(id: i32) -> Result { Self::from_i32(id) } +} + +impl From for i32 { + #[inline] + fn from(val: RecoveryId) -> Self { val.to_i32() } +} + /// An ECDSA signature with a recovery ID for pubkey recovery. #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, Ord, PartialOrd)] pub struct RecoverableSignature(ffi::RecoverableSignature);