Merge rust-bitcoin/rust-bitcoin#1512: Make `Witness::tapscript()` return `Script` instead of raw bytes

e0bc50953a Make `Witness::tapscript()` return `Script` instead of raw bytes (Jiri Jakes)

Pull request description:

  Since there is unsized `Script` now, this method can return it.

ACKs for top commit:
  sanket1729:
    utACK e0bc50953a
  tcharding:
    ACK e0bc50953a

Tree-SHA512: 32d4ca14f1b0fc1029f7376b1a43db90332b869a806609c82f660cb2690a4f0e1b91e1799fdac0d43c8a630aed0331f251d4159a662e86e5942c6fb698c42cd2
This commit is contained in:
sanket1729 2022-12-29 23:07:41 -08:00
commit 90bb150422
No known key found for this signature in database
GPG Key ID: 648FFB183E0870A2
1 changed files with 5 additions and 4 deletions

View File

@ -15,7 +15,7 @@ use crate::consensus::{Decodable, Encodable, WriteExt};
use crate::sighash::EcdsaSighashType; use crate::sighash::EcdsaSighashType;
use crate::io::{self, Read, Write}; use crate::io::{self, Read, Write};
use crate::prelude::*; use crate::prelude::*;
use crate::VarInt; use crate::{Script, VarInt};
use crate::taproot::TAPROOT_ANNEX_PREFIX; use crate::taproot::TAPROOT_ANNEX_PREFIX;
/// The Witness is the data used to unlock bitcoin since the [segwit upgrade]. /// The Witness is the data used to unlock bitcoin since the [segwit upgrade].
@ -317,7 +317,7 @@ impl Witness {
/// the first byte of the last element being equal to 0x50. See /// the first byte of the last element being equal to 0x50. See
/// [Script::is_v1_p2tr](crate::blockdata::script::Script::is_v1_p2tr) to /// [Script::is_v1_p2tr](crate::blockdata::script::Script::is_v1_p2tr) to
/// check whether this is actually a Taproot witness. /// check whether this is actually a Taproot witness.
pub fn tapscript(&self) -> Option<&[u8]> { pub fn tapscript(&self) -> Option<&Script> {
let len = self.len(); let len = self.len();
self self
.last() .last()
@ -338,6 +338,7 @@ impl Witness {
.and_then(|script_pos_from_last| { .and_then(|script_pos_from_last| {
self.nth(len - script_pos_from_last) self.nth(len - script_pos_from_last)
}) })
.map(Script::from_bytes)
} }
} }
@ -641,8 +642,8 @@ mod test {
}; };
// With or without annex, the tapscript should be returned. // With or without annex, the tapscript should be returned.
assert_eq!(witness.tapscript(), Some(&tapscript[..])); assert_eq!(witness.tapscript(), Some(Script::from_bytes(&tapscript[..])));
assert_eq!(witness_annex.tapscript(), Some(&tapscript[..])); assert_eq!(witness_annex.tapscript(), Some(Script::from_bytes(&tapscript[..])));
} }
#[test] #[test]