script: make new_p2wsh available on hashable scripts
What changed, and why it matters
This is a small API change in a Bitcoin library written in Rust. It moves a helper function that creates P2WSH (pay-to-witness-script-hash) output scripts so it can be used on more script types. There is no indication this fixes a security bug or introduces a vulnerability; it appears to be a routine feature/cleanup change.
No security action required. Review as normal API refactoring if this code is part of your dependency tree.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors ScriptPubKeyBuf::new_p2wsh into a generic impl<T: ScriptHashableTag> ScriptBuf<T>::new_p2wsh. The implementation body is unchanged: it still calls new_witness_program_unchecked(WitnessVersion::V0, script_hash) with a 32-byte WScriptHash. A test is added showing RedeemScriptBuf::new_p2wsh now works and produces the same bytes as ScriptPubKeyBuf::new_p2wsh. No cryptographic, parsing, or resource-handling logic changes.
Changed components
primitives/src/script/owned.rsprimitives/src/script/tests.rsInspect captured patch +13 / −7
diff --git a/primitives/src/script/owned.rs b/primitives/src/script/owned.rs
index 1388dbc7..e0ff74a2 100644
--- a/primitives/src/script/owned.rs
+++ b/primitives/src/script/owned.rs
@@ -11,7 +11,7 @@ use super::{Script, ScriptBufDecoderError, P2A_PROGRAM};
use crate::opcodes::all::{OP_1, OP_1NEGATE, OP_EQUAL, OP_HASH160, OP_RETURN};
use crate::opcodes::{self, Opcode};
use crate::prelude::{Box, Vec};
-use crate::script::{Builder, PushBytes, ScriptHash, WScriptHash};
+use crate::script::{Builder, PushBytes, ScriptHash, ScriptHashableTag, WScriptHash};
use crate::witness_version::WitnessVersion;
use crate::ScriptPubKeyBuf;
@@ -275,18 +275,20 @@ impl ScriptPubKeyBuf {
.into_script()
}
- /// Generates P2WSH-type of scriptPubkey with a given hash of the redeem script.
- pub fn new_p2wsh(script_hash: WScriptHash) -> Self {
- // script hash is 32 bytes long, so it's safe to use `new_witness_program_unchecked` (Segwitv0)
- super::new_witness_program_unchecked(WitnessVersion::V0, script_hash)
- }
-
/// Generates pay to anchor output.
pub fn new_p2a() -> Self {
super::new_witness_program_unchecked(WitnessVersion::V1, P2A_PROGRAM)
}
}
+impl<T: ScriptHashableTag> ScriptBuf<T> {
+ /// Generates a P2WSH witness program script with a given hash of the witness script.
+ pub fn new_p2wsh(script_hash: WScriptHash) -> Self {
+ // script hash is 32 bytes long, so it's safe to use `new_witness_program_unchecked` (Segwitv0)
+ super::new_witness_program_unchecked(WitnessVersion::V0, script_hash)
+ }
+}
+
// Cannot derive due to generics.
impl<T> Default for ScriptBuf<T> {
fn default() -> Self { Self(PhantomData, Vec::new()) }
diff --git a/primitives/src/script/tests.rs b/primitives/src/script/tests.rs
index bf102e59..b57595f5 100644
--- a/primitives/src/script/tests.rs
+++ b/primitives/src/script/tests.rs
@@ -858,6 +858,10 @@ fn new_p2wsh() {
let mut want = vec![0x00, 0x20];
want.extend([0x34; 32]);
assert_eq!(p2wsh.as_bytes(), &want[..]);
+
+ let redeem_script = RedeemScriptBuf::new_p2wsh(wscript_hash);
+ assert!(redeem_script.is_p2wsh());
+ assert_eq!(redeem_script.as_bytes(), &want[..]);
}
#[test]
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.