From 7d3e4f5e0d0fd6e13710cfe6b0b8124fc0e607c0 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 | 51 +++++++++++++++---------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/bitcoin/src/crypto/ecdsa.rs b/bitcoin/src/crypto/ecdsa.rs index 646c2168c0..5d1c28cd32 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) } }