From fec1879b9562504758b4297000f0c2c14bd17988 Mon Sep 17 00:00:00 2001 From: Innocent Onyemaenu Date: Mon, 11 Mar 2024 03:11:07 +0100 Subject: [PATCH] Replace FromStr with TryFrom impl trait in ecdsa.rs crypto module Any type that has a FromStr should have TryFrom<{&str, String, Box, Rc, Arc}> This changes are for the crypto ecdsa.rs lib --- bitcoin/src/crypto/ecdsa.rs | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/bitcoin/src/crypto/ecdsa.rs b/bitcoin/src/crypto/ecdsa.rs index 2755f0b511..da68c660b7 100644 --- a/bitcoin/src/crypto/ecdsa.rs +++ b/bitcoin/src/crypto/ecdsa.rs @@ -6,12 +6,17 @@ use core::str::FromStr; use core::{fmt, iter}; +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 alloc::rc::Rc; +use alloc::sync::Arc; +use alloc::string::String; use crate::prelude::{DisplayHex, Vec}; use crate::script::PushBytes; @@ -94,6 +99,46 @@ impl FromStr for Signature { } } +impl TryFrom<&str> for Signature { + type Error = ParseSignatureError; + + fn try_from(s: &str) -> Result { + 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 = ParseSignatureError; + + fn try_from(s: Box) -> Result { + Self::from_str(&s) + } +} + +impl TryFrom> for Signature { + type Error = ParseSignatureError; + + fn try_from(s: Arc) -> Result { + Self::from_str(&s) + } +} + +impl TryFrom> for Signature { + type Error = ParseSignatureError; + + fn try_from(s: Rc) -> Result { + Self::from_str(&s) + } +} + /// Holds signature serialized in-line (not in `Vec`). /// /// This avoids allocation and allows proving maximum size of the signature (73 bytes).