From d7e4c8b3624d683569afe336dea34839bf30195b 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 | 44 ++++++++++++++----------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/bitcoin/src/crypto/ecdsa.rs b/bitcoin/src/crypto/ecdsa.rs index 646c2168c0..6d497bee4a 100644 --- a/bitcoin/src/crypto/ecdsa.rs +++ b/bitcoin/src/crypto/ecdsa.rs @@ -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 { - 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) } }