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 d7e4c8b
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions bitcoin/src/crypto/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 Down

0 comments on commit d7e4c8b

Please sign in to comment.