Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add back to/from i32 fns #765

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/ecdsa/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub enum RecoveryId {
Three,
}

impl TryFrom<i32> for RecoveryId {
type Error = Error;
impl RecoveryId {
/// Allows library users to create valid recovery IDs from i32.
#[inline]
fn try_from(id: i32) -> Result<RecoveryId, Error> {
pub fn from_i32(id: i32) -> Result<Self, Error> {
match id {
0 => Ok(RecoveryId::Zero),
1 => Ok(RecoveryId::One),
Expand All @@ -37,12 +37,11 @@ impl TryFrom<i32> for RecoveryId {
_ => Err(Error::InvalidRecoveryId),
}
}
}

impl From<RecoveryId> 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,
Expand All @@ -51,6 +50,17 @@ impl From<RecoveryId> for i32 {
}
}

impl TryFrom<i32> for RecoveryId {
type Error = Error;
#[inline]
fn try_from(id: i32) -> Result<RecoveryId, Error> { Self::from_i32(id) }
}

impl From<RecoveryId> 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);
Expand Down
Loading