Skip to content

Commit

Permalink
replacing FromStr with TryFrom impl trait in ecdsa.rs crypto module
Browse files Browse the repository at this point in the history
  • Loading branch information
rockcoolsaint committed Dec 19, 2024
1 parent 617d74d commit e6389a3
Showing 1 changed file with 19 additions and 31 deletions.
50 changes: 19 additions & 31 deletions bitcoin/src/crypto/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
use core::str::FromStr;
use core::{fmt, iter};
use std::convert::TryFrom;
use core::convert::TryFrom;
use alloc::boxed::Box;

#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
use hex::FromHex;
use internals::{impl_to_hex_from_lower_hex, write_err};
use io::Write;
use std::rc::Rc;
use std::sync::Arc;
use alloc::rc::Rc;
use alloc::sync::Arc;

use crate::prelude::{DisplayHex, Vec};
use crate::script::PushBytes;
Expand Down Expand Up @@ -99,54 +99,42 @@ impl FromStr for Signature {
}

impl TryFrom<&str> for Signature {
type Error = Error;
type Error = ParseSignatureError;

fn try_from(s: &str) -> Result<Self, Self::Error> {
let bytes = Vec::from_hex(s)?;
let (sighash_byte, signature) = bytes.split_last().ok_or(Error::EmptySignature)?;
Ok(Signature {
signature: secp256k1::ecdsa::Signature::from_der(signature)?,
sighash_type: EcdsaSighashType::from_standard(*sighash_byte as u32)?,
})
Self::from_str(s)
}
}

impl TryFrom<String> for Signature {
type Error = ParseSignatureError;

fn try_from(s: String) -> Result<Self, Self::Error> {
Self::from_str(&s)
}
}

impl TryFrom<Box<str>> for Signature {
type Error = Error;
type Error = ParseSignatureError;

fn try_from(s: Box<str>) -> Result<Self, Self::Error> {
let bytes = Vec::from_hex(&s)?;
let (sighash_byte, signature) = bytes.split_last().ok_or(Error::EmptySignature)?;
Ok(Signature {
signature: secp256k1::ecdsa::Signature::from_der(signature)?,
sighash_type: EcdsaSighashType::from_standard(*sighash_byte as u32)?,
})
Self::from_str(&s)
}
}

impl TryFrom<Arc<str>> for Signature {
type Error = Error;
type Error = ParseSignatureError;

fn try_from(s: Arc<str>) -> Result<Self, Self::Error> {
let bytes = Vec::from_hex(&s)?;
let (sighash_byte, signature) = bytes.split_last().ok_or(Error::EmptySignature)?;
Ok(Signature {
signature: secp256k1::ecdsa::Signature::from_der(signature)?,
sighash_type: EcdsaSighashType::from_standard(*sighash_byte as u32)?,
})
Self::from_str(&s)
}
}

impl TryFrom<Rc<str>> for Signature {
type Error = Error;
type Error = ParseSignatureError;

fn try_from(s: Rc<str>) -> Result<Self, Self::Error> {
let bytes = Vec::from_hex(&s)?;
let (sighash_byte, signature) = bytes.split_last().ok_or(Error::EmptySignature)?;
Ok(Signature {
signature: secp256k1::ecdsa::Signature::from_der(signature)?,
sighash_type: EcdsaSighashType::from_standard(*sighash_byte as u32)?,
})
Self::from_str(&s)
}
}

Expand Down

0 comments on commit e6389a3

Please sign in to comment.