Duplicate new_witness_program_unchecked in primitives
What changed, and why it matters
This commit is a routine internal code reorganization. It copies a small helper function that builds SegWit scriptPubkeys into a lower-level crate so both crates can use it. The function remains private to the library, performs no user-facing behavior changes, and adds no new public API or security-sensitive logic.
No security action required. Review as normal refactoring if desired.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change duplicates new_witness_program_unchecked from the bitcoin crate into primitives::script. The duplicated function is marked pub(crate) and #[allow(dead_code)], keeps the same debug_assert! length checks, and is intended only for internal use by existing new_p2a, new_p2wpkh, new_p2wsh, new_p2tr, and new_p2tr_tweaked constructors. No logic was altered; only visibility/location changed to satisfy crate layering.
Changed components
primitives/src/script/mod.rsInspect captured patch +17 / −0
diff --git a/primitives/src/script/mod.rs b/primitives/src/script/mod.rs
index 89912c2a..b71692d7 100644
--- a/primitives/src/script/mod.rs
+++ b/primitives/src/script/mod.rs
@@ -25,6 +25,7 @@ use crate::prelude::rc::Rc;
#[cfg(target_has_atomic = "ptr")]
use crate::prelude::sync::Arc;
use crate::prelude::{Borrow, BorrowMut, Box, Cow, ToOwned, Vec};
+use crate::witness_version::WitnessVersion;
#[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)]
@@ -89,6 +90,22 @@ pub const MAX_REDEEM_SCRIPT_SIZE: usize = 520;
/// The maximum allowed redeem script size of the witness script.
pub const MAX_WITNESS_SCRIPT_SIZE: usize = 10_000;
+/// Generates P2WSH-type of scriptPubkey with a given [`WitnessVersion`] and the program bytes.
+/// Does not do any checks on version or program length.
+///
+/// Convenience method used by `new_p2a`, `new_p2wpkh`, `new_p2wsh`, `new_p2tr`, and `new_p2tr_tweaked`.
+#[allow(dead_code)]
+pub(crate) fn new_witness_program_unchecked<T: AsRef<PushBytes>, Tg>(
+ version: WitnessVersion,
+ program: T,
+) -> ScriptBuf<Tg> {
+ let program = program.as_ref();
+ debug_assert!(program.len() >= 2 && program.len() <= 40);
+ // In SegWit v0, the program must be either 20 bytes (P2WPKH) or 32 bytes (P2WSH) long.
+ debug_assert!(version != WitnessVersion::V0 || program.len() == 20 || program.len() == 32);
+ Builder::new().push_opcode(version.into()).push_slice(program).into_script()
+}
+
/// Either a redeem script or a Segwit version 0 scriptpubkey.
///
/// In the case of P2SH-wrapped Segwit version outputs, we take a Segwit scriptPubKey
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.