Skip to content

Commit

Permalink
Add push_p2wpkh function on Witness
Browse files Browse the repository at this point in the history
In order to create the witness to spend p2wpkh output one must push the
signature and the pubkey, we should have a function for this.
  • Loading branch information
tcharding committed Sep 25, 2023
1 parent 8cd409d commit 764ac44
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions bitcoin/src/blockdata/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ impl Witness {
/// Creates a new empty [`Witness`].
pub fn new() -> Self { Witness::default() }

/// Creates a witness required to spend a P2WPKH output.
///
/// The witness will be made up of the DER encoded signature + sighash_type followed by the
/// serialized public key. Also useful for spending a P2SH-P2WPKH output.
///
/// It is expected that `pubkey` is related to the secret key used to create `signature`.
pub fn p2wpkh(&mut self, signature: &ecdsa::Signature, pubkey: &secp256k1::PublicKey) {
self.push_slice(&signature.serialize());
self.push_slice(&pubkey.serialize());
}

/// Creates a [`Witness`] object from a slice of bytes slices where each slice is a witness item.
pub fn from_slice<T: AsRef<[u8]>>(slice: &[T]) -> Self {
let witness_elements = slice.len();
Expand Down

0 comments on commit 764ac44

Please sign in to comment.