2022-06-29 04:05:31 +00:00
|
|
|
// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
|
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2021-04-12 11:24:25 +00:00
|
|
|
|
2023-02-07 03:35:03 +00:00
|
|
|
//! Bitcoin taproot keys.
|
2021-04-12 11:24:25 +00:00
|
|
|
//!
|
2023-02-07 03:35:03 +00:00
|
|
|
//! This module provides taproot keys used in Bitcoin (including reexporting secp256k1 keys).
|
2021-04-12 11:24:25 +00:00
|
|
|
//!
|
|
|
|
|
2021-11-16 00:00:12 +00:00
|
|
|
use core::fmt;
|
2022-06-24 02:01:53 +00:00
|
|
|
|
2022-09-05 02:19:28 +00:00
|
|
|
use bitcoin_internals::write_err;
|
2022-12-05 23:39:56 +00:00
|
|
|
pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey};
|
2022-09-15 02:14:52 +00:00
|
|
|
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::prelude::*;
|
2023-02-07 03:01:51 +00:00
|
|
|
use crate::sighash::TapSighashType;
|
2021-11-07 06:11:43 +00:00
|
|
|
|
2023-02-07 03:35:03 +00:00
|
|
|
/// A BIP340-341 serialized taproot signature with the corresponding hash type.
|
2022-05-06 03:37:24 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2021-10-28 07:43: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-11-08 00:36:52 +00:00
|
|
|
pub struct Signature {
|
2021-11-16 00:00:12 +00:00
|
|
|
/// The underlying schnorr signature
|
2022-01-03 02:52:44 +00:00
|
|
|
pub sig: secp256k1::schnorr::Signature,
|
2021-11-16 00:00:12 +00:00
|
|
|
/// The corresponding hash type
|
2023-02-07 03:01:51 +00:00
|
|
|
pub hash_ty: TapSighashType,
|
2021-11-16 00:00:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 00:36:52 +00:00
|
|
|
impl Signature {
|
2021-11-16 00:00:12 +00:00
|
|
|
/// Deserialize from slice
|
2022-11-08 00:36:52 +00:00
|
|
|
pub fn from_slice(sl: &[u8]) -> Result<Self, Error> {
|
2021-11-16 00:00:12 +00:00
|
|
|
match sl.len() {
|
|
|
|
64 => {
|
|
|
|
// default type
|
2022-12-05 23:39:56 +00:00
|
|
|
let sig =
|
|
|
|
secp256k1::schnorr::Signature::from_slice(sl).map_err(Error::Secp256k1)?;
|
2023-02-07 03:01:51 +00:00
|
|
|
Ok(Signature { sig, hash_ty: TapSighashType::Default })
|
2022-12-05 23:39:56 +00:00
|
|
|
}
|
2021-11-16 00:00:12 +00:00
|
|
|
65 => {
|
|
|
|
let (hash_ty, sig) = sl.split_last().expect("Slice len checked == 65");
|
2023-02-07 03:01:51 +00:00
|
|
|
let hash_ty = TapSighashType::from_consensus_u8(*hash_ty)
|
2022-11-08 00:36:52 +00:00
|
|
|
.map_err(|_| Error::InvalidSighashType(*hash_ty))?;
|
2022-12-05 23:39:56 +00:00
|
|
|
let sig =
|
|
|
|
secp256k1::schnorr::Signature::from_slice(sig).map_err(Error::Secp256k1)?;
|
2022-11-08 00:36:52 +00:00
|
|
|
Ok(Signature { sig, hash_ty })
|
2021-11-16 00:00:12 +00:00
|
|
|
}
|
2022-12-05 23:39:56 +00:00
|
|
|
len => Err(Error::InvalidSignatureSize(len)),
|
2021-11-16 00:00:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 00:36:52 +00:00
|
|
|
/// Serialize Signature
|
2022-08-01 22:33:22 +00:00
|
|
|
pub fn to_vec(self) -> Vec<u8> {
|
2021-11-16 00:00:12 +00:00
|
|
|
// TODO: add support to serialize to a writer to SerializedSig
|
|
|
|
let mut ser_sig = self.sig.as_ref().to_vec();
|
2023-02-07 03:01:51 +00:00
|
|
|
if self.hash_ty == TapSighashType::Default {
|
2021-11-16 00:00:12 +00:00
|
|
|
// default sighash type, don't add extra sighash byte
|
|
|
|
} else {
|
|
|
|
ser_sig.push(self.hash_ty as u8);
|
|
|
|
}
|
|
|
|
ser_sig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 03:35:03 +00:00
|
|
|
/// A taproot sig related error.
|
2021-11-16 00:00:12 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
2022-05-31 04:29:50 +00:00
|
|
|
#[non_exhaustive]
|
2022-11-08 00:36:52 +00:00
|
|
|
pub enum Error {
|
2021-11-16 00:00:12 +00:00
|
|
|
/// Base58 encoding error
|
|
|
|
InvalidSighashType(u8),
|
|
|
|
/// Signature has valid size but does not parse correctly
|
|
|
|
Secp256k1(secp256k1::Error),
|
2023-02-07 03:35:03 +00:00
|
|
|
/// Invalid taproot signature size
|
2022-11-08 00:36:52 +00:00
|
|
|
InvalidSignatureSize(usize),
|
2021-11-16 00:00:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 00:36:52 +00:00
|
|
|
impl fmt::Display for Error {
|
2021-11-16 00:00:12 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2022-11-08 00:36:52 +00:00
|
|
|
Error::InvalidSighashType(hash_ty) =>
|
2023-02-07 03:35:03 +00:00
|
|
|
write!(f, "invalid signature hash type {}", hash_ty),
|
2022-11-08 00:36:52 +00:00
|
|
|
Error::Secp256k1(ref e) =>
|
2023-02-07 03:35:03 +00:00
|
|
|
write_err!(f, "taproot signature has correct len but is malformed"; e),
|
2022-12-05 23:39:56 +00:00
|
|
|
Error::InvalidSignatureSize(sz) => write!(f, "invalid taproot signature size: {}", sz),
|
2021-11-16 00:00:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
2022-11-08 00:36:52 +00:00
|
|
|
impl std::error::Error for Error {
|
2022-05-04 05:56:24 +00:00
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
2022-11-08 00:36:52 +00:00
|
|
|
use self::Error::*;
|
2022-05-04 05:56:24 +00:00
|
|
|
|
|
|
|
match self {
|
|
|
|
Secp256k1(e) => Some(e),
|
2022-11-08 00:36:52 +00:00
|
|
|
InvalidSighashType(_) | InvalidSignatureSize(_) => None,
|
2022-05-04 05:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 00:00:12 +00:00
|
|
|
|
2022-11-08 00:36:52 +00:00
|
|
|
impl From<secp256k1::Error> for Error {
|
2022-12-05 23:39:56 +00:00
|
|
|
fn from(e: secp256k1::Error) -> Error { Error::Secp256k1(e) }
|
2021-11-16 00:00:12 +00:00
|
|
|
}
|