diff --git a/bitcoin/examples/sign-tx-taproot.rs b/bitcoin/examples/sign-tx-taproot.rs index f8fe3f02..9076c009 100644 --- a/bitcoin/examples/sign-tx-taproot.rs +++ b/bitcoin/examples/sign-tx-taproot.rs @@ -76,7 +76,7 @@ fn main() { // Update the witness stack. let signature = bitcoin::taproot::Signature { signature, sighash_type }; - sighasher.witness_mut(input_index).unwrap().push(&signature.to_vec()); + *sighasher.witness_mut(input_index).unwrap() = Witness::p2tr_key_spend(&signature); // Get the signed transaction. let tx = sighasher.into_transaction(); diff --git a/bitcoin/src/blockdata/witness.rs b/bitcoin/src/blockdata/witness.rs index 3018e91f..69af2924 100644 --- a/bitcoin/src/blockdata/witness.rs +++ b/bitcoin/src/blockdata/witness.rs @@ -14,7 +14,7 @@ use crate::consensus::encode::{Error, MAX_VEC_SIZE}; use crate::consensus::{Decodable, Encodable, WriteExt}; use crate::crypto::ecdsa; use crate::prelude::*; -use crate::taproot::TAPROOT_ANNEX_PREFIX; +use crate::taproot::{self, TAPROOT_ANNEX_PREFIX}; use crate::{Script, VarInt}; /// The Witness is the data used to unlock bitcoin since the [segwit upgrade]. @@ -248,6 +248,13 @@ impl Witness { witness } + /// Creates a witness required to do a key path spend of a P2TR output. + pub fn p2tr_key_spend(signature: &taproot::Signature) -> Witness { + let mut witness = Witness::new(); + witness.push_slice(&signature.serialize()); + witness + } + /// Creates a [`Witness`] object from a slice of bytes slices where each slice is a witness item. pub fn from_slice>(slice: &[T]) -> Self { let witness_elements = slice.len();