Skip to content

Commit

Permalink
Merge rust-bitcoin#3408: Prepare Witness
Browse files Browse the repository at this point in the history
3f3f30d Use iter instead of accessing content field (Tobin C. Harding)
6389d1c Stop using push_slice (Tobin C. Harding)
be163ee Use Witness::len instead of accessing field (Tobin C. Harding)

Pull request description:

  Prepare `Witness` to be moved to `primitives`.

  This is the first three patches out of rust-bitcoin#3406. Patch 1 and 2 are internal changes, path 3 is also internal but introduces a slight perf hit by doing multiple writes.

ACKs for top commit:
  apoelstra:
    ACK 3f3f30d successfully ran local tests

Tree-SHA512: 25e2570a22797dbfa15d6e5af9b72938243192ca264bc5fe82c2f327486e52a73993b3b61ee1161e5d17b01a7f9843960cb4031e6550561de4ecafb9f935e375
  • Loading branch information
apoelstra committed Oct 1, 2024
2 parents 66d2c02 + 3f3f30d commit f3fbd0e
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions bitcoin/src/blockdata/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use arbitrary::{Arbitrary, Unstructured};
use internals::compact_size;
use io::{BufRead, Write};

use crate::consensus::encode::{Error, ReadExt, MAX_VEC_SIZE};
use crate::consensus::{Decodable, Encodable, WriteExt};
use crate::consensus::encode::{self, Error, MAX_VEC_SIZE, ReadExt, WriteExt};
use crate::consensus::{Decodable, Encodable};
use crate::crypto::ecdsa;
use crate::prelude::Vec;
#[cfg(doc)]
Expand Down Expand Up @@ -230,10 +230,13 @@ fn resize_if_needed(vec: &mut Vec<u8>, required_len: usize) {
impl Encodable for Witness {
// `self.content` includes the varints so encoding here includes them, as expected.
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
let content_with_indices_len = self.content.len();
let indices_size = self.witness_elements * 4;
let content_len = content_with_indices_len - indices_size;
Ok(w.emit_compact_size(self.witness_elements)? + w.emit_slice(&self.content[..content_len])?)
let mut written = w.emit_compact_size(self.len())?;

for element in self.iter() {
written += encode::consensus_encode_with_size(element, w)?
}

Ok(written)
}
}

Expand All @@ -252,15 +255,15 @@ impl Witness {
/// It is expected that `pubkey` is related to the secret key used to create `signature`.
pub fn p2wpkh(signature: ecdsa::Signature, pubkey: secp256k1::PublicKey) -> Witness {
let mut witness = Witness::new();
witness.push_slice(&signature.serialize());
witness.push_slice(&pubkey.serialize());
witness.push(signature.serialize());
witness.push(pubkey.serialize());
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.push(signature.serialize());
witness
}

Expand Down Expand Up @@ -363,7 +366,7 @@ impl Witness {
///
/// Pushes the DER encoded signature + sighash_type, requires an allocation.
pub fn push_ecdsa_signature(&mut self, signature: ecdsa::Signature) {
self.push_slice(&signature.serialize())
self.push(signature.serialize())
}

/// Note `index` is the index into the `content` vector and should be the result of calling
Expand Down

0 comments on commit f3fbd0e

Please sign in to comment.