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 c47ce48
Showing 1 changed file with 33 additions and 70 deletions.
103 changes: 33 additions & 70 deletions bitcoin/src/crypto/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
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 alloc::string::String;

use crate::prelude::{DisplayHex, Vec};
use crate::script::PushBytes;
Expand Down Expand Up @@ -99,54 +100,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 All @@ -166,9 +155,7 @@ pub struct SerializedSignature {
impl SerializedSignature {
/// Returns an iterator over bytes of the signature.
#[inline]
pub fn iter(&self) -> core::slice::Iter<'_, u8> {
self.into_iter()
}
pub fn iter(&self) -> core::slice::Iter<'_, u8> { self.into_iter() }

/// Writes this serialized signature to a `writer`.
#[inline]
Expand All @@ -181,65 +168,47 @@ impl core::ops::Deref for SerializedSignature {
type Target = [u8];

#[inline]
fn deref(&self) -> &Self::Target {
&self.data[..self.len]
}
fn deref(&self) -> &Self::Target { &self.data[..self.len] }
}

impl core::ops::DerefMut for SerializedSignature {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data[..self.len]
}
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.data[..self.len] }
}

impl AsRef<[u8]> for SerializedSignature {
#[inline]
fn as_ref(&self) -> &[u8] {
self
}
fn as_ref(&self) -> &[u8] { self }
}

impl AsMut<[u8]> for SerializedSignature {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
self
}
fn as_mut(&mut self) -> &mut [u8] { self }
}

impl AsRef<PushBytes> for SerializedSignature {
#[inline]
fn as_ref(&self) -> &PushBytes {
&<&PushBytes>::from(&self.data)[..self.len()]
}
fn as_ref(&self) -> &PushBytes { &<&PushBytes>::from(&self.data)[..self.len()] }
}

impl core::borrow::Borrow<[u8]> for SerializedSignature {
#[inline]
fn borrow(&self) -> &[u8] {
self
}
fn borrow(&self) -> &[u8] { self }
}

impl core::borrow::BorrowMut<[u8]> for SerializedSignature {
#[inline]
fn borrow_mut(&mut self) -> &mut [u8] {
self
}
fn borrow_mut(&mut self) -> &mut [u8] { self }
}

impl fmt::Debug for SerializedSignature {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(self, f) }
}

impl fmt::Display for SerializedSignature {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::LowerHex::fmt(self, f)
}
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self, f) }
}

impl fmt::LowerHex for SerializedSignature {
Expand All @@ -260,27 +229,21 @@ impl fmt::UpperHex for SerializedSignature {

impl PartialEq for SerializedSignature {
#[inline]
fn eq(&self, other: &SerializedSignature) -> bool {
**self == **other
}
fn eq(&self, other: &SerializedSignature) -> bool { **self == **other }
}

impl Eq for SerializedSignature {}

impl core::hash::Hash for SerializedSignature {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
core::hash::Hash::hash(&**self, state)
}
fn hash<H: core::hash::Hasher>(&self, state: &mut H) { core::hash::Hash::hash(&**self, state) }
}

impl<'a> IntoIterator for &'a SerializedSignature {
type IntoIter = core::slice::Iter<'a, u8>;
type Item = &'a u8;

#[inline]
fn into_iter(self) -> Self::IntoIter {
(*self).iter()
}
fn into_iter(self) -> Self::IntoIter { (*self).iter() }
}

/// Error encountered while parsing an ECDSA signature from a byte slice.
Expand Down

0 comments on commit c47ce48

Please sign in to comment.