Merge rust-bitcoin/rust-bitcoin#2457: Inline private ScriptBuf::p2wpkh function

10cf51c4c5 Inline private ScriptBuf::p2wpkh function (Tobin C. Harding)

Pull request description:

  This function is a bit unclear and is only called once, just inline it.

  Refactor only, no logic changes.

ACKs for top commit:
  apoelstra:
    ACK 10cf51c4c5
  Kixunil:
    ACK 10cf51c4c5

Tree-SHA512: 3907923f2258089a5fc1cc1e1d0b34e99457d69a5822cefa7bf90405d7ac05d570fb2855f62e2b5b4b871485e349e8dc09eb8f14c0676a8bdd70593e345b9b41
This commit is contained in:
Andrew Poelstra 2024-02-08 15:50:00 +00:00
commit 53461f71c9
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 9 additions and 13 deletions

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: CC0-1.0 // SPDX-License-Identifier: CC0-1.0
use core::convert::{TryFrom, TryInto}; use core::convert::TryFrom;
use core::fmt; use core::fmt;
use core::ops::{ use core::ops::{
Bound, Index, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, Bound, Index, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
@ -328,14 +328,6 @@ impl Script {
&& self.0[1] == OP_PUSHBYTES_20.to_u8() && self.0[1] == OP_PUSHBYTES_20.to_u8()
} }
pub(crate) fn p2wpkh(&self) -> Option<&[u8; 20]> {
if self.is_p2wpkh() {
Some(self.0[2..].try_into().expect("is_v0_p2wpkh checks the length"))
} else {
None
}
}
/// Checks whether a script pubkey is a P2TR output. /// Checks whether a script pubkey is a P2TR output.
#[inline] #[inline]
pub fn is_p2tr(&self) -> bool { pub fn is_p2tr(&self) -> bool {
@ -384,10 +376,14 @@ impl Script {
/// ///
/// [BIP143]: <https://github.com/bitcoin/bips/blob/99701f68a88ce33b2d0838eb84e115cef505b4c2/bip-0143.mediawiki> /// [BIP143]: <https://github.com/bitcoin/bips/blob/99701f68a88ce33b2d0838eb84e115cef505b4c2/bip-0143.mediawiki>
pub fn p2wpkh_script_code(&self) -> Option<ScriptBuf> { pub fn p2wpkh_script_code(&self) -> Option<ScriptBuf> {
self.p2wpkh().map(|wpkh| { if self.is_p2wpkh() {
let wpkh = WPubkeyHash::from_slice(wpkh).expect("checked in p2wpkh()"); // The `self` script is 0x00, 0x14, <pubkey_hash>
ScriptBuf::p2wpkh_script_code(wpkh) let bytes = &self.0[2..];
}) let wpkh = WPubkeyHash::from_slice(bytes).expect("length checked in is_p2wpkh()");
Some(ScriptBuf::p2wpkh_script_code(wpkh))
} else {
None
}
} }
/// Returns the minimum value an output with this script should have in order to be /// Returns the minimum value an output with this script should have in order to be