Rename schnorr module to taproot

"schnorr" is a dirty word; the current `schnorr` module defines a
`Signature` that includes a sighash type, this sighash type is a bitcoin
specific construct related to taproot. Therefore the `Signature` is
better named `taproot::Signature`. Note also that the usage of `schnorr`
in `secp256k1` is probably justified because the
`secp256::schnorr::Signature` is just doing the crypto.

While we are at it, update docs and error messages to use "taproot"
instead of "schnorr". Also change function names and identifiers that
use "schnorr".
This commit is contained in:
Tobin C. Harding 2023-02-07 14:35:03 +11:00
parent 9f39e872bc
commit be7b3754a9
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
12 changed files with 68 additions and 67 deletions

View File

@ -81,6 +81,7 @@ use std::str::FromStr;
use bitcoin::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey, Fingerprint}; use bitcoin::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey, Fingerprint};
use bitcoin::consensus::encode; use bitcoin::consensus::encode;
use bitcoin::constants::COIN_VALUE; use bitcoin::constants::COIN_VALUE;
use bitcoin::crypto::taproot;
use bitcoin::hashes::Hash; use bitcoin::hashes::Hash;
use bitcoin::key::{TapTweak, XOnlyPublicKey}; use bitcoin::key::{TapTweak, XOnlyPublicKey};
use bitcoin::opcodes::all::{OP_CHECKSIG, OP_CLTV, OP_DROP}; use bitcoin::opcodes::all::{OP_CHECKSIG, OP_CLTV, OP_DROP};
@ -91,7 +92,7 @@ use bitcoin::taproot::{
LeafVersion, TapLeafHash, TapSighash, TaprootBuilder, TaprootSpendInfo, LeafVersion, TapLeafHash, TapSighash, TaprootBuilder, TaprootSpendInfo,
}; };
use bitcoin::{ use bitcoin::{
absolute, script, schnorr, Address, Amount, Network, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Witness, absolute, script, Address, Amount, Network, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Witness,
}; };
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
@ -282,7 +283,7 @@ fn generate_bip86_key_spend_tx(
|(vout, input)| { |(vout, input)| {
let hash_ty = input let hash_ty = input
.sighash_type .sighash_type
.and_then(|psbt_sighash_type| psbt_sighash_type.schnorr_hash_ty().ok()) .and_then(|psbt_sighash_type| psbt_sighash_type.taproot_hash_ty().ok())
.unwrap_or(TapSighashType::All); .unwrap_or(TapSighashType::All);
let hash = SighashCache::new(&unsigned_tx).taproot_key_spend_signature_hash( let hash = SighashCache::new(&unsigned_tx).taproot_key_spend_signature_hash(
vout, vout,
@ -299,7 +300,7 @@ fn generate_bip86_key_spend_tx(
.ok_or("Missing taproot key origin")?; .ok_or("Missing taproot key origin")?;
let secret_key = master_xpriv.derive_priv(secp, &derivation_path)?.to_priv().inner; let secret_key = master_xpriv.derive_priv(secp, &derivation_path)?.to_priv().inner;
sign_psbt_schnorr( sign_psbt_taproot(
&secret_key, &secret_key,
input.tap_internal_key.unwrap(), input.tap_internal_key.unwrap(),
None, None,
@ -517,7 +518,7 @@ impl BenefactorWallet {
let hash_ty = input let hash_ty = input
.sighash_type .sighash_type
.and_then(|psbt_sighash_type| psbt_sighash_type.schnorr_hash_ty().ok()) .and_then(|psbt_sighash_type| psbt_sighash_type.taproot_hash_ty().ok())
.unwrap_or(TapSighashType::All); .unwrap_or(TapSighashType::All);
let hash = SighashCache::new(&psbt.unsigned_tx).taproot_key_spend_signature_hash( let hash = SighashCache::new(&psbt.unsigned_tx).taproot_key_spend_signature_hash(
0, 0,
@ -535,7 +536,7 @@ impl BenefactorWallet {
.ok_or("Missing taproot key origin")?; .ok_or("Missing taproot key origin")?;
let secret_key = let secret_key =
self.master_xpriv.derive_priv(&self.secp, &derivation_path)?.to_priv().inner; self.master_xpriv.derive_priv(&self.secp, &derivation_path)?.to_priv().inner;
sign_psbt_schnorr( sign_psbt_taproot(
&secret_key, &secret_key,
spend_info.internal_key(), spend_info.internal_key(),
None, None,
@ -669,7 +670,7 @@ impl BeneficiaryWallet {
*lh, *lh,
hash_ty, hash_ty,
)?; )?;
sign_psbt_schnorr( sign_psbt_taproot(
&secret_key, &secret_key,
*x_only_pubkey, *x_only_pubkey,
Some(*lh), Some(*lh),
@ -729,7 +730,7 @@ impl BeneficiaryWallet {
// licenses. // licenses.
// Calling this with `leaf_hash` = `None` will sign for key-spend // Calling this with `leaf_hash` = `None` will sign for key-spend
fn sign_psbt_schnorr( fn sign_psbt_taproot(
secret_key: &secp256k1::SecretKey, secret_key: &secp256k1::SecretKey,
pubkey: XOnlyPublicKey, pubkey: XOnlyPublicKey,
leaf_hash: Option<TapLeafHash>, leaf_hash: Option<TapLeafHash>,
@ -746,7 +747,7 @@ fn sign_psbt_schnorr(
let sig = secp.sign_schnorr(&Message::from_slice(&hash.into_inner()[..]).unwrap(), &keypair); let sig = secp.sign_schnorr(&Message::from_slice(&hash.into_inner()[..]).unwrap(), &keypair);
let final_signature = schnorr::Signature { sig, hash_ty }; let final_signature = taproot::Signature { sig, hash_ty };
if let Some(lh) = leaf_hash { if let Some(lh) = leaf_hash {
psbt_input.tap_script_sigs.insert((pubkey, lh), final_signature); psbt_input.tap_script_sigs.insert((pubkey, lh), final_signature);

View File

@ -8,5 +8,5 @@
pub mod ecdsa; pub mod ecdsa;
pub mod key; pub mod key;
pub mod schnorr;
pub mod sighash; pub mod sighash;
pub mod taproot;

View File

@ -253,7 +253,7 @@ impl fmt::Display for Error {
PrevoutIndex => write!(f, "The index requested is greater than available prevouts or different from the provided [Provided::Anyone] index"), PrevoutIndex => write!(f, "The index requested is greater than available prevouts or different from the provided [Provided::Anyone] index"),
PrevoutKind => write!(f, "A single prevout has been provided but all prevouts are needed without `ANYONECANPAY`"), PrevoutKind => write!(f, "A single prevout has been provided but all prevouts are needed without `ANYONECANPAY`"),
WrongAnnex => write!(f, "Annex must be at least one byte long and the first bytes must be `0x50`"), WrongAnnex => write!(f, "Annex must be at least one byte long and the first bytes must be `0x50`"),
InvalidSighashType(hash_ty) => write!(f, "Invalid schnorr Signature hash type : {} ", hash_ty), InvalidSighashType(hash_ty) => write!(f, "Invalid taproot signature hash type : {} ", hash_ty),
} }
} }
} }

View File

@ -1,10 +1,9 @@
// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net> // Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
// SPDX-License-Identifier: CC0-1.0 // SPDX-License-Identifier: CC0-1.0
//! Schnorr Bitcoin keys. //! Bitcoin taproot keys.
//! //!
//! This module provides Schnorr keys used in Bitcoin, reexporting Secp256k1 //! This module provides taproot keys used in Bitcoin (including reexporting secp256k1 keys).
//! Schnorr key types.
//! //!
use core::fmt; use core::fmt;
@ -17,7 +16,7 @@ use crate::prelude::*;
use crate::sighash::TapSighashType; use crate::sighash::TapSighashType;
/// A BIP340-341 serialized schnorr signature with the corresponding hash type. /// A BIP340-341 serialized taproot signature with the corresponding hash type.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))] #[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
@ -66,7 +65,7 @@ impl Signature {
} }
/// A schnorr sig related error. /// A taproot sig related error.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive] #[non_exhaustive]
pub enum Error { pub enum Error {
@ -74,7 +73,7 @@ pub enum Error {
InvalidSighashType(u8), InvalidSighashType(u8),
/// Signature has valid size but does not parse correctly /// Signature has valid size but does not parse correctly
Secp256k1(secp256k1::Error), Secp256k1(secp256k1::Error),
/// Invalid schnorr signature size /// Invalid taproot signature size
InvalidSignatureSize(usize), InvalidSignatureSize(usize),
} }
@ -83,11 +82,11 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
Error::InvalidSighashType(hash_ty) => Error::InvalidSighashType(hash_ty) =>
write!(f, "Invalid signature hash type {}", hash_ty), write!(f, "invalid signature hash type {}", hash_ty),
Error::Secp256k1(ref e) => Error::Secp256k1(ref e) =>
write_err!(f, "Schnorr signature has correct len but is malformed"; e), write_err!(f, "taproot signature has correct len but is malformed"; e),
Error::InvalidSignatureSize(sz) => Error::InvalidSignatureSize(sz) =>
write!(f, "Invalid Schnorr signature size: {}", sz), write!(f, "invalid taproot signature size: {}", sz),
} }
} }
} }

View File

@ -127,7 +127,7 @@ pub use crate::blockdata::witness::{self, Witness};
pub use crate::blockdata::{constants, opcodes}; pub use crate::blockdata::{constants, opcodes};
pub use crate::consensus::encode::VarInt; pub use crate::consensus::encode::VarInt;
pub use crate::crypto::key::{self, PrivateKey, PublicKey}; pub use crate::crypto::key::{self, PrivateKey, PublicKey};
pub use crate::crypto::{ecdsa, schnorr, sighash}; pub use crate::crypto::sighash;
pub use crate::error::Error; pub use crate::error::Error;
pub use crate::hash_types::{Txid, Wtxid, BlockHash, PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash}; pub use crate::hash_types::{Txid, Wtxid, BlockHash, PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash};
pub use crate::merkle_tree::MerkleBlock; pub use crate::merkle_tree::MerkleBlock;

View File

@ -87,13 +87,13 @@ pub enum Error {
InvalidXOnlyPublicKey, InvalidXOnlyPublicKey,
/// Parsing error indicating invalid ECDSA signatures /// Parsing error indicating invalid ECDSA signatures
InvalidEcdsaSignature(crate::crypto::ecdsa::Error), InvalidEcdsaSignature(crate::crypto::ecdsa::Error),
/// Parsing error indicating invalid Schnorr signatures /// Parsing error indicating invalid taproot signatures
InvalidSchnorrSignature(crate::crypto::schnorr::Error), InvalidTaprootSignature(crate::crypto::taproot::Error),
/// Parsing error indicating invalid control block /// Parsing error indicating invalid control block
InvalidControlBlock, InvalidControlBlock,
/// Parsing error indicating invalid leaf version /// Parsing error indicating invalid leaf version
InvalidLeafVersion, InvalidLeafVersion,
/// Parsing error indicating a Taproot error /// Parsing error indicating a taproot error
Taproot(&'static str), Taproot(&'static str),
/// Error related to an xpub key /// Error related to an xpub key
XPubKey(&'static str), XPubKey(&'static str),
@ -136,7 +136,7 @@ impl fmt::Display for Error {
Error::InvalidSecp256k1PublicKey(ref e) => write_err!(f, "invalid secp256k1 public key"; e), Error::InvalidSecp256k1PublicKey(ref e) => write_err!(f, "invalid secp256k1 public key"; e),
Error::InvalidXOnlyPublicKey => f.write_str("invalid xonly public key"), Error::InvalidXOnlyPublicKey => f.write_str("invalid xonly public key"),
Error::InvalidEcdsaSignature(ref e) => write_err!(f, "invalid ECDSA signature"; e), Error::InvalidEcdsaSignature(ref e) => write_err!(f, "invalid ECDSA signature"; e),
Error::InvalidSchnorrSignature(ref e) => write_err!(f, "invalid Schnorr signature"; e), Error::InvalidTaprootSignature(ref e) => write_err!(f, "invalid taproot signature"; e),
Error::InvalidControlBlock => f.write_str("invalid control block"), Error::InvalidControlBlock => f.write_str("invalid control block"),
Error::InvalidLeafVersion => f.write_str("invalid leaf version"), Error::InvalidLeafVersion => f.write_str("invalid leaf version"),
Error::Taproot(s) => write!(f, "taproot error - {}", s), Error::Taproot(s) => write!(f, "taproot error - {}", s),
@ -179,7 +179,7 @@ impl std::error::Error for Error {
| InvalidSecp256k1PublicKey(_) | InvalidSecp256k1PublicKey(_)
| InvalidXOnlyPublicKey | InvalidXOnlyPublicKey
| InvalidEcdsaSignature(_) | InvalidEcdsaSignature(_)
| InvalidSchnorrSignature(_) | InvalidTaprootSignature(_)
| InvalidControlBlock | InvalidControlBlock
| InvalidLeafVersion | InvalidLeafVersion
| Taproot(_) | Taproot(_)

View File

@ -11,7 +11,7 @@ use secp256k1::XOnlyPublicKey;
use crate::blockdata::script::ScriptBuf; use crate::blockdata::script::ScriptBuf;
use crate::blockdata::witness::Witness; use crate::blockdata::witness::Witness;
use crate::blockdata::transaction::{Transaction, TxOut}; use crate::blockdata::transaction::{Transaction, TxOut};
use crate::crypto::{ecdsa, schnorr}; use crate::crypto::{ecdsa, taproot};
use crate::crypto::key::PublicKey; use crate::crypto::key::PublicKey;
use crate::hashes::{self, hash160, ripemd160, sha256, sha256d}; use crate::hashes::{self, hash160, ripemd160, sha256, sha256d};
use crate::bip32::KeySource; use crate::bip32::KeySource;
@ -47,9 +47,9 @@ const PSBT_IN_SHA256: u8 = 0x0b;
const PSBT_IN_HASH160: u8 = 0x0c; const PSBT_IN_HASH160: u8 = 0x0c;
/// Type: HASH256 preimage PSBT_IN_HASH256 = 0x0d /// Type: HASH256 preimage PSBT_IN_HASH256 = 0x0d
const PSBT_IN_HASH256: u8 = 0x0d; const PSBT_IN_HASH256: u8 = 0x0d;
/// Type: Schnorr Signature in Key Spend PSBT_IN_TAP_KEY_SIG = 0x13 /// Type: Taproot Signature in Key Spend PSBT_IN_TAP_KEY_SIG = 0x13
const PSBT_IN_TAP_KEY_SIG: u8 = 0x13; const PSBT_IN_TAP_KEY_SIG: u8 = 0x13;
/// Type: Schnorr Signature in Script Spend PSBT_IN_TAP_SCRIPT_SIG = 0x14 /// Type: Taproot Signature in Script Spend PSBT_IN_TAP_SCRIPT_SIG = 0x14
const PSBT_IN_TAP_SCRIPT_SIG: u8 = 0x14; const PSBT_IN_TAP_SCRIPT_SIG: u8 = 0x14;
/// Type: Taproot Leaf Script PSBT_IN_TAP_LEAF_SCRIPT = 0x14 /// Type: Taproot Leaf Script PSBT_IN_TAP_LEAF_SCRIPT = 0x14
const PSBT_IN_TAP_LEAF_SCRIPT: u8 = 0x15; const PSBT_IN_TAP_LEAF_SCRIPT: u8 = 0x15;
@ -109,11 +109,11 @@ pub struct Input {
/// HAS256 hash to preimage map. /// HAS256 hash to preimage map.
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_byte_values"))] #[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_byte_values"))]
pub hash256_preimages: BTreeMap<sha256d::Hash, Vec<u8>>, pub hash256_preimages: BTreeMap<sha256d::Hash, Vec<u8>>,
/// Serialized schnorr signature with sighash type for key spend. /// Serialized taproot signature with sighash type for key spend.
pub tap_key_sig: Option<schnorr::Signature>, pub tap_key_sig: Option<taproot::Signature>,
/// Map of `<xonlypubkey>|<leafhash>` with signature. /// Map of `<xonlypubkey>|<leafhash>` with signature.
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))] #[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))]
pub tap_script_sigs: BTreeMap<(XOnlyPublicKey, TapLeafHash), schnorr::Signature>, pub tap_script_sigs: BTreeMap<(XOnlyPublicKey, TapLeafHash), taproot::Signature>,
/// Map of Control blocks to Script version pair. /// Map of Control blocks to Script version pair.
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))] #[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))]
pub tap_scripts: BTreeMap<ControlBlock, (ScriptBuf, LeafVersion)>, pub tap_scripts: BTreeMap<ControlBlock, (ScriptBuf, LeafVersion)>,
@ -146,9 +146,9 @@ pub struct PsbtSighashType {
impl fmt::Display for PsbtSighashType { impl fmt::Display for PsbtSighashType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.schnorr_hash_ty() { match self.taproot_hash_ty() {
Err(_) => write!(f, "{:#x}", self.inner), Err(_) => write!(f, "{:#x}", self.inner),
Ok(schnorr_hash_ty) => fmt::Display::fmt(&schnorr_hash_ty, f), Ok(taproot_hash_ty) => fmt::Display::fmt(&taproot_hash_ty, f),
} }
} }
} }
@ -160,7 +160,7 @@ impl FromStr for PsbtSighashType {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
// We accept strings of form: "SIGHASH_ALL" etc. // We accept strings of form: "SIGHASH_ALL" etc.
// //
// NB: some of Schnorr sighash types are non-standard for pre-taproot // NB: some of Taproot sighash types are non-standard for pre-taproot
// inputs. We also do not support SIGHASH_RESERVED in verbatim form // inputs. We also do not support SIGHASH_RESERVED in verbatim form
// ("0xFF" string should be used instead). // ("0xFF" string should be used instead).
if let Ok(ty) = TapSighashType::from_str(s) { if let Ok(ty) = TapSighashType::from_str(s) {
@ -182,8 +182,8 @@ impl From<EcdsaSighashType> for PsbtSighashType {
} }
impl From<TapSighashType> for PsbtSighashType { impl From<TapSighashType> for PsbtSighashType {
fn from(schnorr_hash_ty: TapSighashType) -> Self { fn from(taproot_hash_ty: TapSighashType) -> Self {
PsbtSighashType { inner: schnorr_hash_ty as u32 } PsbtSighashType { inner: taproot_hash_ty as u32 }
} }
} }
@ -196,7 +196,7 @@ impl PsbtSighashType {
/// Returns the [`TapSighashType`] if the [`PsbtSighashType`] can be /// Returns the [`TapSighashType`] if the [`PsbtSighashType`] can be
/// converted to one. /// converted to one.
pub fn schnorr_hash_ty(self) -> Result<TapSighashType, sighash::Error> { pub fn taproot_hash_ty(self) -> Result<TapSighashType, sighash::Error> {
if self.inner > 0xffu32 { if self.inner > 0xffu32 {
Err(sighash::Error::InvalidSighashType(self.inner)) Err(sighash::Error::InvalidSighashType(self.inner))
} else { } else {
@ -239,10 +239,10 @@ impl Input {
/// ///
/// # Errors /// # Errors
/// ///
/// If the `sighash_type` field is set to a invalid Schnorr sighash value. /// If the `sighash_type` field is set to a invalid Taproot sighash value.
pub fn schnorr_hash_ty(&self) -> Result<TapSighashType, sighash::Error> { pub fn taproot_hash_ty(&self) -> Result<TapSighashType, sighash::Error> {
self.sighash_type self.sighash_type
.map(|sighash_type| sighash_type.schnorr_hash_ty()) .map(|sighash_type| sighash_type.taproot_hash_ty())
.unwrap_or(Ok(TapSighashType::Default)) .unwrap_or(Ok(TapSighashType::Default))
} }
@ -312,12 +312,12 @@ impl Input {
} }
PSBT_IN_TAP_KEY_SIG => { PSBT_IN_TAP_KEY_SIG => {
impl_psbt_insert_pair! { impl_psbt_insert_pair! {
self.tap_key_sig <= <raw_key: _>|<raw_value: schnorr::Signature> self.tap_key_sig <= <raw_key: _>|<raw_value: taproot::Signature>
} }
} }
PSBT_IN_TAP_SCRIPT_SIG => { PSBT_IN_TAP_SCRIPT_SIG => {
impl_psbt_insert_pair! { impl_psbt_insert_pair! {
self.tap_script_sigs <= <raw_key: (XOnlyPublicKey, TapLeafHash)>|<raw_value: schnorr::Signature> self.tap_script_sigs <= <raw_key: (XOnlyPublicKey, TapLeafHash)>|<raw_value: taproot::Signature>
} }
} }
PSBT_IN_TAP_LEAF_SCRIPT => { PSBT_IN_TAP_LEAF_SCRIPT => {
@ -543,8 +543,8 @@ mod test {
} }
#[test] #[test]
fn psbt_sighash_type_schnorr() { fn psbt_sighash_type_taproot() {
for schnorr in &[ for tap in &[
TapSighashType::Default, TapSighashType::Default,
TapSighashType::All, TapSighashType::All,
TapSighashType::None, TapSighashType::None,
@ -553,11 +553,11 @@ mod test {
TapSighashType::NonePlusAnyoneCanPay, TapSighashType::NonePlusAnyoneCanPay,
TapSighashType::SinglePlusAnyoneCanPay, TapSighashType::SinglePlusAnyoneCanPay,
] { ] {
let sighash = PsbtSighashType::from(*schnorr); let sighash = PsbtSighashType::from(*tap);
let s = format!("{}", sighash); let s = format!("{}", sighash);
let back = PsbtSighashType::from_str(&s).unwrap(); let back = PsbtSighashType::from_str(&s).unwrap();
assert_eq!(back, sighash); assert_eq!(back, sighash);
assert_eq!(back.schnorr_hash_ty().unwrap(), *schnorr); assert_eq!(back.taproot_hash_ty().unwrap(), *tap);
} }
} }
@ -570,6 +570,6 @@ mod test {
assert_eq!(back, sighash); assert_eq!(back, sighash);
assert_eq!(back.ecdsa_hash_ty(), Err(NonStandardSighashType(nonstd))); assert_eq!(back.ecdsa_hash_ty(), Err(NonStandardSighashType(nonstd)));
assert_eq!(back.schnorr_hash_ty(), Err(sighash::Error::InvalidSighashType(nonstd))); assert_eq!(back.taproot_hash_ty(), Err(sighash::Error::InvalidSighashType(nonstd)));
} }
} }

View File

@ -1376,9 +1376,9 @@ mod tests {
assert_eq!(err.to_string(), "invalid xonly public key"); assert_eq!(err.to_string(), "invalid xonly public key");
let err = hex_psbt!("70736274ff010071020000000127744ababf3027fe0d6cf23a96eee2efb188ef52301954585883e69b6624b2420000000000ffffffff02787c01000000000016001483a7e34bd99ff03a4962ef8a1a101bb295461ece606b042a010000001600147ac369df1b20e033d6116623957b0ac49f3c52e8000000000001012b00f2052a010000002251205a2c2cf5b52cf31f83ad2e8da63ff03183ecd8f609c7510ae8a48e03910a0757011342173bb3d36c074afb716fec6307a069a2e450b995f3c82785945ab8df0e24260dcd703b0cbf34de399184a9481ac2b3586db6601f026a77f7e4938481bc34751701aa000000").unwrap_err(); let err = hex_psbt!("70736274ff010071020000000127744ababf3027fe0d6cf23a96eee2efb188ef52301954585883e69b6624b2420000000000ffffffff02787c01000000000016001483a7e34bd99ff03a4962ef8a1a101bb295461ece606b042a010000001600147ac369df1b20e033d6116623957b0ac49f3c52e8000000000001012b00f2052a010000002251205a2c2cf5b52cf31f83ad2e8da63ff03183ecd8f609c7510ae8a48e03910a0757011342173bb3d36c074afb716fec6307a069a2e450b995f3c82785945ab8df0e24260dcd703b0cbf34de399184a9481ac2b3586db6601f026a77f7e4938481bc34751701aa000000").unwrap_err();
#[cfg(feature = "std")] #[cfg(feature = "std")]
assert_eq!(err.to_string(), "invalid Schnorr signature"); assert_eq!(err.to_string(), "invalid taproot signature");
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
assert_eq!(err.to_string(), "invalid Schnorr signature: Invalid Schnorr signature size: 66"); assert_eq!(err.to_string(), "invalid taproot signature: invalid taproot signature size: 66");
let err = hex_psbt!("70736274ff010071020000000127744ababf3027fe0d6cf23a96eee2efb188ef52301954585883e69b6624b2420000000000ffffffff02787c01000000000016001483a7e34bd99ff03a4962ef8a1a101bb295461ece606b042a010000001600147ac369df1b20e033d6116623957b0ac49f3c52e8000000000001012b00f2052a010000002251205a2c2cf5b52cf31f83ad2e8da63ff03183ecd8f609c7510ae8a48e03910a0757221602fe349064c98d6e2a853fa3c9b12bd8b304a19c195c60efa7ee2393046d3fa2321900772b2da75600008001000080000000800100000000000000000000").unwrap_err(); let err = hex_psbt!("70736274ff010071020000000127744ababf3027fe0d6cf23a96eee2efb188ef52301954585883e69b6624b2420000000000ffffffff02787c01000000000016001483a7e34bd99ff03a4962ef8a1a101bb295461ece606b042a010000001600147ac369df1b20e033d6116623957b0ac49f3c52e8000000000001012b00f2052a010000002251205a2c2cf5b52cf31f83ad2e8da63ff03183ecd8f609c7510ae8a48e03910a0757221602fe349064c98d6e2a853fa3c9b12bd8b304a19c195c60efa7ee2393046d3fa2321900772b2da75600008001000080000000800100000000000000000000").unwrap_err();
assert_eq!(err.to_string(), "invalid xonly public key"); assert_eq!(err.to_string(), "invalid xonly public key");
let err = hex_psbt!("70736274ff01007d020000000127744ababf3027fe0d6cf23a96eee2efb188ef52301954585883e69b6624b2420000000000ffffffff02887b0100000000001600142382871c7e8421a00093f754d91281e675874b9f606b042a010000002251205a2c2cf5b52cf31f83ad2e8da63ff03183ecd8f609c7510ae8a48e03910a0757000000000001012b00f2052a010000002251205a2c2cf5b52cf31f83ad2e8da63ff03183ecd8f609c7510ae8a48e03910a0757000001052102fe349064c98d6e2a853fa3c9b12bd8b304a19c195c60efa7ee2393046d3fa23200").unwrap_err(); let err = hex_psbt!("70736274ff01007d020000000127744ababf3027fe0d6cf23a96eee2efb188ef52301954585883e69b6624b2420000000000ffffffff02887b0100000000001600142382871c7e8421a00093f754d91281e675874b9f606b042a010000002251205a2c2cf5b52cf31f83ad2e8da63ff03183ecd8f609c7510ae8a48e03910a0757000000000001012b00f2052a010000002251205a2c2cf5b52cf31f83ad2e8da63ff03183ecd8f609c7510ae8a48e03910a0757000001052102fe349064c98d6e2a853fa3c9b12bd8b304a19c195c60efa7ee2393046d3fa23200").unwrap_err();
@ -1392,14 +1392,14 @@ mod tests {
assert_eq!(err.to_string(), "hash parse error: invalid slice length 33 (expected 32)"); assert_eq!(err.to_string(), "hash parse error: invalid slice length 33 (expected 32)");
let err = hex_psbt!("70736274ff01005e02000000019bd48765230bf9a72e662001f972556e54f0c6f97feb56bcb5600d817f6995260100000000ffffffff0148e6052a01000000225120030da4fce4f7db28c2cb2951631e003713856597fe963882cb500e68112cca63000000000001012b00f2052a01000000225120c2247efbfd92ac47f6f40b8d42d169175a19fa9fa10e4a25d7f35eb4dd85b69241142cb13ac68248de806aa6a3659cf3c03eb6821d09c8114a4e868febde865bb6d2cd970e15f53fc0c82f950fd560ffa919b76172be017368a89913af074f400b094289756aa3739ccc689ec0fcf3a360be32cc0b59b16e93a1e8bb4605726b2ca7a3ff706c4176649632b2cc68e1f912b8a578e3719ce7710885c7a966f49bcd43cb01010000").unwrap_err(); let err = hex_psbt!("70736274ff01005e02000000019bd48765230bf9a72e662001f972556e54f0c6f97feb56bcb5600d817f6995260100000000ffffffff0148e6052a01000000225120030da4fce4f7db28c2cb2951631e003713856597fe963882cb500e68112cca63000000000001012b00f2052a01000000225120c2247efbfd92ac47f6f40b8d42d169175a19fa9fa10e4a25d7f35eb4dd85b69241142cb13ac68248de806aa6a3659cf3c03eb6821d09c8114a4e868febde865bb6d2cd970e15f53fc0c82f950fd560ffa919b76172be017368a89913af074f400b094289756aa3739ccc689ec0fcf3a360be32cc0b59b16e93a1e8bb4605726b2ca7a3ff706c4176649632b2cc68e1f912b8a578e3719ce7710885c7a966f49bcd43cb01010000").unwrap_err();
#[cfg(feature = "std")] #[cfg(feature = "std")]
assert_eq!(err.to_string(), "invalid Schnorr signature"); assert_eq!(err.to_string(), "invalid taproot signature");
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
assert_eq!(err.to_string(), "invalid Schnorr signature: Invalid Schnorr signature size: 66"); assert_eq!(err.to_string(), "invalid taproot signature: invalid taproot signature size: 66");
let err = hex_psbt!("70736274ff01005e02000000019bd48765230bf9a72e662001f972556e54f0c6f97feb56bcb5600d817f6995260100000000ffffffff0148e6052a01000000225120030da4fce4f7db28c2cb2951631e003713856597fe963882cb500e68112cca63000000000001012b00f2052a01000000225120c2247efbfd92ac47f6f40b8d42d169175a19fa9fa10e4a25d7f35eb4dd85b69241142cb13ac68248de806aa6a3659cf3c03eb6821d09c8114a4e868febde865bb6d2cd970e15f53fc0c82f950fd560ffa919b76172be017368a89913af074f400b093989756aa3739ccc689ec0fcf3a360be32cc0b59b16e93a1e8bb4605726b2ca7a3ff706c4176649632b2cc68e1f912b8a578e3719ce7710885c7a966f49bcd43cb0000").unwrap_err(); let err = hex_psbt!("70736274ff01005e02000000019bd48765230bf9a72e662001f972556e54f0c6f97feb56bcb5600d817f6995260100000000ffffffff0148e6052a01000000225120030da4fce4f7db28c2cb2951631e003713856597fe963882cb500e68112cca63000000000001012b00f2052a01000000225120c2247efbfd92ac47f6f40b8d42d169175a19fa9fa10e4a25d7f35eb4dd85b69241142cb13ac68248de806aa6a3659cf3c03eb6821d09c8114a4e868febde865bb6d2cd970e15f53fc0c82f950fd560ffa919b76172be017368a89913af074f400b093989756aa3739ccc689ec0fcf3a360be32cc0b59b16e93a1e8bb4605726b2ca7a3ff706c4176649632b2cc68e1f912b8a578e3719ce7710885c7a966f49bcd43cb0000").unwrap_err();
#[cfg(feature = "std")] #[cfg(feature = "std")]
assert_eq!(err.to_string(), "invalid Schnorr signature"); assert_eq!(err.to_string(), "invalid taproot signature");
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
assert_eq!(err.to_string(), "invalid Schnorr signature: Invalid Schnorr signature size: 57"); assert_eq!(err.to_string(), "invalid taproot signature: invalid taproot signature size: 57");
let err = hex_psbt!("70736274ff01005e02000000019bd48765230bf9a72e662001f972556e54f0c6f97feb56bcb5600d817f6995260100000000ffffffff0148e6052a01000000225120030da4fce4f7db28c2cb2951631e003713856597fe963882cb500e68112cca63000000000001012b00f2052a01000000225120c2247efbfd92ac47f6f40b8d42d169175a19fa9fa10e4a25d7f35eb4dd85b6926315c150929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac06f7d62059e9497a1a4a267569d9876da60101aff38e3529b9b939ce7f91ae970115f2e490af7cc45c4f78511f36057ce5c5a5c56325a29fb44dfc203f356e1f80023202cb13ac68248de806aa6a3659cf3c03eb6821d09c8114a4e868febde865bb6d2acc00000").unwrap_err(); let err = hex_psbt!("70736274ff01005e02000000019bd48765230bf9a72e662001f972556e54f0c6f97feb56bcb5600d817f6995260100000000ffffffff0148e6052a01000000225120030da4fce4f7db28c2cb2951631e003713856597fe963882cb500e68112cca63000000000001012b00f2052a01000000225120c2247efbfd92ac47f6f40b8d42d169175a19fa9fa10e4a25d7f35eb4dd85b6926315c150929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac06f7d62059e9497a1a4a267569d9876da60101aff38e3529b9b939ce7f91ae970115f2e490af7cc45c4f78511f36057ce5c5a5c56325a29fb44dfc203f356e1f80023202cb13ac68248de806aa6a3659cf3c03eb6821d09c8114a4e868febde865bb6d2acc00000").unwrap_err();
assert_eq!(err.to_string(), "invalid control block"); assert_eq!(err.to_string(), "invalid control block");
let err = hex_psbt!("70736274ff01005e02000000019bd48765230bf9a72e662001f972556e54f0c6f97feb56bcb5600d817f6995260100000000ffffffff0148e6052a01000000225120030da4fce4f7db28c2cb2951631e003713856597fe963882cb500e68112cca63000000000001012b00f2052a01000000225120c2247efbfd92ac47f6f40b8d42d169175a19fa9fa10e4a25d7f35eb4dd85b6926115c150929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac06f7d62059e9497a1a4a267569d9876da60101aff38e3529b9b939ce7f91ae970115f2e490af7cc45c4f78511f36057ce5c5a5c56325a29fb44dfc203f356e123202cb13ac68248de806aa6a3659cf3c03eb6821d09c8114a4e868febde865bb6d2acc00000").unwrap_err(); let err = hex_psbt!("70736274ff01005e02000000019bd48765230bf9a72e662001f972556e54f0c6f97feb56bcb5600d817f6995260100000000ffffffff0148e6052a01000000225120030da4fce4f7db28c2cb2951631e003713856597fe963882cb500e68112cca63000000000001012b00f2052a01000000225120c2247efbfd92ac47f6f40b8d42d169175a19fa9fa10e4a25d7f35eb4dd85b6926115c150929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac06f7d62059e9497a1a4a267569d9876da60101aff38e3529b9b939ce7f91ae970115f2e490af7cc45c4f78511f36057ce5c5a5c56325a29fb44dfc203f356e123202cb13ac68248de806aa6a3659cf3c03eb6821d09c8114a4e868febde865bb6d2acc00000").unwrap_err();

View File

@ -19,7 +19,7 @@ use crate::consensus::encode::{self, serialize, Decodable, Encodable, deserializ
use secp256k1::{self, XOnlyPublicKey}; use secp256k1::{self, XOnlyPublicKey};
use crate::bip32::{ChildNumber, Fingerprint, KeySource}; use crate::bip32::{ChildNumber, Fingerprint, KeySource};
use crate::hashes::{hash160, ripemd160, sha256, sha256d, Hash}; use crate::hashes::{hash160, ripemd160, sha256, sha256d, Hash};
use crate::crypto::{ecdsa, schnorr}; use crate::crypto::{ecdsa, taproot};
use crate::psbt::{Error, PartiallySignedTransaction}; use crate::psbt::{Error, PartiallySignedTransaction};
use crate::taproot::{TapNodeHash, TapLeafHash, ControlBlock, LeafVersion}; use crate::taproot::{TapNodeHash, TapLeafHash, ControlBlock, LeafVersion};
use crate::crypto::key::PublicKey; use crate::crypto::key::PublicKey;
@ -283,24 +283,24 @@ impl Deserialize for XOnlyPublicKey {
} }
} }
impl Serialize for schnorr::Signature { impl Serialize for taproot::Signature {
fn serialize(&self) -> Vec<u8> { fn serialize(&self) -> Vec<u8> {
self.to_vec() self.to_vec()
} }
} }
impl Deserialize for schnorr::Signature { impl Deserialize for taproot::Signature {
fn deserialize(bytes: &[u8]) -> Result<Self, Error> { fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
schnorr::Signature::from_slice(bytes) taproot::Signature::from_slice(bytes)
.map_err(|e| match e { .map_err(|e| match e {
schnorr::Error::InvalidSighashType(flag) => { taproot::Error::InvalidSighashType(flag) => {
Error::NonStandardSighashType(flag as u32) Error::NonStandardSighashType(flag as u32)
} }
schnorr::Error::InvalidSignatureSize(_) => { taproot::Error::InvalidSignatureSize(_) => {
Error::InvalidSchnorrSignature(e) Error::InvalidTaprootSignature(e)
} }
schnorr::Error::Secp256k1(..) => { taproot::Error::Secp256k1(..) => {
Error::InvalidSchnorrSignature(e) Error::InvalidTaprootSignature(e)
} }
}) })
} }

View File

@ -31,6 +31,7 @@ use bitcoin::blockdata::locktime::{absolute, relative};
use bitcoin::blockdata::witness::Witness; use bitcoin::blockdata::witness::Witness;
use bitcoin::consensus::encode::deserialize; use bitcoin::consensus::encode::deserialize;
use bitcoin::crypto::key::UntweakedPublicKey; use bitcoin::crypto::key::UntweakedPublicKey;
use bitcoin::crypto::{ecdsa, taproot};
use bitcoin::hashes::hex::FromHex; use bitcoin::hashes::hex::FromHex;
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash}; use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash};
use bitcoin::psbt::raw::{self, Key, Pair, ProprietaryKey}; use bitcoin::psbt::raw::{self, Key, Pair, ProprietaryKey};
@ -38,7 +39,7 @@ use bitcoin::psbt::{Input, Output, Psbt, PsbtSighashType};
use bitcoin::sighash::{EcdsaSighashType, TapSighashType}; use bitcoin::sighash::{EcdsaSighashType, TapSighashType};
use bitcoin::taproot::{ControlBlock, LeafVersion, TaprootBuilder, TaprootSpendInfo}; use bitcoin::taproot::{ControlBlock, LeafVersion, TaprootBuilder, TaprootSpendInfo};
use bitcoin::{ use bitcoin::{
ecdsa, schnorr, Address, Block, Network, OutPoint, PrivateKey, PublicKey, ScriptBuf, Sequence, Target, Address, Block, Network, OutPoint, PrivateKey, PublicKey, ScriptBuf, Sequence, Target,
Transaction, TxIn, TxOut, Txid, Work, Transaction, TxIn, TxOut, Txid, Work,
}; };
use secp256k1::Secp256k1; use secp256k1::Secp256k1;
@ -339,15 +340,15 @@ fn serde_regression_proprietary_key() {
} }
#[test] #[test]
fn serde_regression_schnorr_sig() { fn serde_regression_taproot_sig() {
let s = include_str!("data/serde/schnorr_sig_hex"); let s = include_str!("data/serde/taproot_sig_hex");
let sig = schnorr::Signature { let sig = taproot::Signature {
sig: secp256k1::schnorr::Signature::from_str(s.trim()).unwrap(), sig: secp256k1::schnorr::Signature::from_str(s.trim()).unwrap(),
hash_ty: TapSighashType::All, hash_ty: TapSighashType::All,
}; };
let got = serialize(&sig).unwrap(); let got = serialize(&sig).unwrap();
let want = include_bytes!("data/serde/schnorr_sig_bincode") as &[_]; let want = include_bytes!("data/serde/taproot_sig_bincode") as &[_];
assert_eq!(got, want) assert_eq!(got, want)
} }