2022-06-29 04:05:31 +00:00
|
|
|
// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
|
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2022-01-09 06:50:02 +00:00
|
|
|
|
|
|
|
//! ECDSA Bitcoin signatures.
|
|
|
|
//!
|
|
|
|
//! This module provides ECDSA signatures used Bitcoin that can be roundtrip (de)serialized.
|
|
|
|
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::prelude::*;
|
2022-01-09 06:50:02 +00:00
|
|
|
use core::str::FromStr;
|
2022-01-13 04:27:20 +00:00
|
|
|
use core::{fmt, iter};
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::hashes::hex::{self, FromHex};
|
|
|
|
use crate::blockdata::transaction::NonStandardSighashType;
|
2022-01-09 06:50:02 +00:00
|
|
|
use secp256k1;
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::EcdsaSighashType;
|
2022-06-07 03:25:27 +00:00
|
|
|
use crate::internal_macros::write_err;
|
2022-01-09 06:50:02 +00:00
|
|
|
|
|
|
|
/// An ECDSA signature with the corresponding hash type.
|
2022-05-06 03:37:24 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2022-01-09 06:50:02 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
2022-05-25 06:41:59 +00:00
|
|
|
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
|
2022-01-09 06:50:02 +00:00
|
|
|
pub struct EcdsaSig {
|
|
|
|
/// The underlying ECDSA Signature
|
|
|
|
pub sig: secp256k1::ecdsa::Signature,
|
|
|
|
/// The corresponding hash type
|
2022-03-28 21:48:53 +00:00
|
|
|
pub hash_ty: EcdsaSighashType,
|
2022-01-09 06:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EcdsaSig {
|
2022-01-07 02:58:47 +00:00
|
|
|
/// Constructs an ECDSA bitcoin signature for [`EcdsaSighashType::All`].
|
2022-01-09 06:50:02 +00:00
|
|
|
pub fn sighash_all(sig: secp256k1::ecdsa::Signature) -> EcdsaSig {
|
|
|
|
EcdsaSig {
|
|
|
|
sig,
|
2022-03-28 21:48:53 +00:00
|
|
|
hash_ty: EcdsaSighashType::All
|
2022-01-09 06:50:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 02:58:47 +00:00
|
|
|
/// Deserializes from slice following the standardness rules for [`EcdsaSighashType`].
|
2022-01-09 06:50:02 +00:00
|
|
|
pub fn from_slice(sl: &[u8]) -> Result<Self, EcdsaSigError> {
|
|
|
|
let (hash_ty, sig) = sl.split_last()
|
|
|
|
.ok_or(EcdsaSigError::EmptySignature)?;
|
2022-03-28 21:48:53 +00:00
|
|
|
let hash_ty = EcdsaSighashType::from_standard(*hash_ty as u32)
|
2022-03-28 21:56:36 +00:00
|
|
|
.map_err(|_| EcdsaSigError::NonStandardSighashType(*hash_ty as u32))?;
|
2022-01-09 06:50:02 +00:00
|
|
|
let sig = secp256k1::ecdsa::Signature::from_der(sig)
|
|
|
|
.map_err(EcdsaSigError::Secp256k1)?;
|
|
|
|
Ok(EcdsaSig { sig, hash_ty })
|
|
|
|
}
|
|
|
|
|
2022-01-07 02:58:47 +00:00
|
|
|
/// Serializes an ECDSA signature (inner secp256k1 signature in DER format).
|
2022-01-09 06:50:02 +00:00
|
|
|
pub fn to_vec(&self) -> Vec<u8> {
|
|
|
|
// TODO: add support to serialize to a writer to SerializedSig
|
2022-01-13 04:27:20 +00:00
|
|
|
self.sig.serialize_der()
|
2022-05-25 03:40:13 +00:00
|
|
|
.iter().copied()
|
2022-01-13 04:27:20 +00:00
|
|
|
.chain(iter::once(self.hash_ty as u8))
|
|
|
|
.collect()
|
2022-01-09 06:50:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for EcdsaSig {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
hex::format_hex(&self.sig.serialize_der(), f)?;
|
|
|
|
hex::format_hex(&[self.hash_ty as u8], f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for EcdsaSig {
|
|
|
|
type Err = EcdsaSigError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let bytes = Vec::from_hex(s)?;
|
|
|
|
let (sighash_byte, signature) = bytes.split_last()
|
|
|
|
.ok_or(EcdsaSigError::EmptySignature)?;
|
|
|
|
Ok(EcdsaSig {
|
|
|
|
sig: secp256k1::ecdsa::Signature::from_der(signature)?,
|
2022-03-28 21:48:53 +00:00
|
|
|
hash_ty: EcdsaSighashType::from_standard(*sighash_byte as u32)?
|
2022-01-09 06:50:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A key-related error.
|
|
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
2022-05-31 04:29:50 +00:00
|
|
|
#[non_exhaustive]
|
2022-01-09 06:50:02 +00:00
|
|
|
pub enum EcdsaSigError {
|
|
|
|
/// Hex encoding error
|
|
|
|
HexEncoding(hex::Error),
|
|
|
|
/// Base58 encoding error
|
2022-03-28 21:56:36 +00:00
|
|
|
NonStandardSighashType(u32),
|
2022-01-09 06:50:02 +00:00
|
|
|
/// Empty Signature
|
|
|
|
EmptySignature,
|
|
|
|
/// secp256k1-related error
|
|
|
|
Secp256k1(secp256k1::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for EcdsaSigError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2022-05-25 02:56:51 +00:00
|
|
|
EcdsaSigError::HexEncoding(ref e) =>
|
|
|
|
write_err!(f, "EcdsaSig hex encoding error"; e),
|
2022-03-28 21:56:36 +00:00
|
|
|
EcdsaSigError::NonStandardSighashType(hash_ty) =>
|
2022-01-09 06:50:02 +00:00
|
|
|
write!(f, "Non standard signature hash type {}", hash_ty),
|
|
|
|
EcdsaSigError::EmptySignature =>
|
|
|
|
write!(f, "Empty ECDSA signature"),
|
2022-05-04 05:02:18 +00:00
|
|
|
EcdsaSigError::Secp256k1(ref e) =>
|
2022-05-25 02:56:51 +00:00
|
|
|
write_err!(f, "invalid ECDSA signature"; e),
|
2022-01-09 06:50:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
2022-05-04 05:56:24 +00:00
|
|
|
impl std::error::Error for EcdsaSigError {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
|
|
use self::EcdsaSigError::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
HexEncoding(e) => Some(e),
|
|
|
|
Secp256k1(e) => Some(e),
|
|
|
|
NonStandardSighashType(_) | EmptySignature => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-09 06:50:02 +00:00
|
|
|
|
|
|
|
impl From<secp256k1::Error> for EcdsaSigError {
|
|
|
|
fn from(e: secp256k1::Error) -> EcdsaSigError {
|
|
|
|
EcdsaSigError::Secp256k1(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 21:56:36 +00:00
|
|
|
impl From<NonStandardSighashType> for EcdsaSigError {
|
|
|
|
fn from(err: NonStandardSighashType) -> Self {
|
|
|
|
EcdsaSigError::NonStandardSighashType(err.0)
|
2022-01-09 06:50:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<hex::Error> for EcdsaSigError {
|
|
|
|
fn from(err: hex::Error) -> Self {
|
|
|
|
EcdsaSigError::HexEncoding(err)
|
|
|
|
}
|
|
|
|
}
|