From c47ce482d3eff28c9982caeb898d4ec380ce4ef3 Mon Sep 17 00:00:00 2001 From: Innocent Onyemaenu Date: Thu, 19 Dec 2024 11:05:18 +0100 Subject: [PATCH] replacing FromStr with TryFrom impl trait in ecdsa.rs crypto module --- bitcoin/src/crypto/ecdsa.rs | 103 ++++++++++++------------------------ 1 file changed, 33 insertions(+), 70 deletions(-) diff --git a/bitcoin/src/crypto/ecdsa.rs b/bitcoin/src/crypto/ecdsa.rs index 646c2168c0..da68c660b7 100644 --- a/bitcoin/src/crypto/ecdsa.rs +++ b/bitcoin/src/crypto/ecdsa.rs @@ -6,7 +6,7 @@ use core::str::FromStr; use core::{fmt, iter}; -use std::convert::TryFrom; +use core::convert::TryFrom; use alloc::boxed::Box; #[cfg(feature = "arbitrary")] @@ -14,8 +14,9 @@ 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; @@ -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 { - 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 for Signature { + type Error = ParseSignatureError; + + fn try_from(s: String) -> Result { + Self::from_str(&s) } } impl TryFrom> for Signature { - type Error = Error; + type Error = ParseSignatureError; fn try_from(s: Box) -> Result { - 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> for Signature { - type Error = Error; + type Error = ParseSignatureError; fn try_from(s: Arc) -> Result { - 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> for Signature { - type Error = Error; + type Error = ParseSignatureError; fn try_from(s: Rc) -> Result { - 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) } } @@ -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] @@ -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 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 { @@ -260,17 +229,13 @@ 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(&self, state: &mut H) { - core::hash::Hash::hash(&**self, state) - } + fn hash(&self, state: &mut H) { core::hash::Hash::hash(&**self, state) } } impl<'a> IntoIterator for &'a SerializedSignature { @@ -278,9 +243,7 @@ impl<'a> IntoIterator for &'a SerializedSignature { 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.