Merge rust-bitcoin/rust-bitcoin#6986: docs: move OP_CODESEPARATOR docs to ScriptCode
What changed, and why it matters
This change is purely documentation. It moves existing explanations about a special Bitcoin script opcode, OP_CODESEPARATOR, from one place in the code comments to a more central location. No program logic, code behavior, or security checks were changed.
No action required; review is informational only.
Security signals we found
No code changes
Documentation-only refactor
References existing OP_CODESEPARATOR handling rules already present in the codebase
Evidence from the diff
The commit refactors doc comments in rust-bitcoin. It consolidates guidance on how callers must prepare scriptCode when OP_CODESEPARATOR is involved, moving it from the WitnessScript AsRef impl to the ScriptCode type definition and updating sighash function docs to reference it. The diff contains only comment additions, removals, and rewordings; no executable code was modified.
Changed components
bitcoin/src/crypto/sighash.rs (doc comments)primitives/src/script/mod.rs (doc comments)Inspect captured patch +23 / −7
### bitcoin/src/crypto/sighash.rs
@@ -567,7 +567,8 @@ impl<R: Borrow<Transaction>> SighashCache<R> {
/// [`std::io::Write`] trait.
///
/// `script_code` is dependent on the type of the spend transaction. For p2wpkh use
- /// [`ScriptPubKey::p2wpkh_script_code`], for p2wsh just pass in the witness script. (Also see
+ /// [`ScriptPubKey::p2wpkh_script_code`], for p2wsh use the witness script, prepared as
+ /// described in [`ScriptCode`] if it contains `OP_CODESEPARATOR` instructions. (Also see
/// [`Self::p2wpkh_signature_hash`] and [`SighashCache::p2wsh_signature_hash`].)
///
/// In order to sign, the data written by this function must be hashed using a double SHA256
@@ -658,6 +659,9 @@ impl<R: Borrow<Transaction>> SighashCache<R> {
///
/// `script_code` is the prepared P2WSH script code derived from the witness script,
/// the script that goes into the [`Witness`], not the `script_pubkey` of a [`TxOut`].
+ ///
+ /// The caller must prepare `script_code` as described in [`ScriptCode`].
+ /// This function keeps all remaining `OP_CODESEPARATOR` instructions.
pub fn p2wsh_signature_hash(
&mut self,
input_index: usize,
@@ -690,8 +694,7 @@ impl<R: Borrow<Transaction>> SighashCache<R> {
///
/// # Warning
///
- /// - The caller must remove everything up to and including the last
- /// **executed** `OP_CODESEPARATOR` before the signature check.
+ /// - The caller must prepare `script_code` as described in [`ScriptCode`].
/// This function removes all remaining `OP_CODESEPARATOR` instructions
/// when encoding `script_code`.
/// - Does NOT handle the sighash single bug (see "Return type" section)
### primitives/src/script/mod.rs
@@ -103,6 +103,23 @@ pub type WitnessScriptBuf = ScriptBuf<WitnessScriptTag>;
pub type WitnessScript = Script<WitnessScriptTag>;
/// A reference to a script code (scriptCode) used in sighash computation.
+///
+/// Used for legacy and Segwit v0 signature hashing. The `AsRef` implementations on
+/// [`WitnessScript`], [`RedeemScript`], [`ScriptPubKey`], and their owned `Buf` variants
+/// borrow the script bytes without modifying them.
+///
+/// # `OP_CODESEPARATOR`
+///
+/// The caller must remove everything up to and including the last **executed**
+/// `OP_CODESEPARATOR` before the signature checking opcode. If no
+/// `OP_CODESEPARATOR` has executed, no prefix is removed.
+///
+/// For Segwit v0, all `OP_CODESEPARATOR` instructions in the remaining script are
+/// kept, including those in unexecuted branches (see [BIP-0143]). For legacy
+/// signature hashing, the remaining `OP_CODESEPARATOR` instructions are removed
+/// when encoding the signing data.
+///
+/// [BIP-0143]: <https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki>
pub type ScriptCode = Script<ScriptCodeTag>;
/// The maximum allowed redeem script size for a P2SH output.
@@ -206,10 +223,6 @@ impl TryFrom<&WitnessScript> for WScriptHash {
}
/// Borrows `WitnessScript` as `ScriptCode` without modifying its bytes.
-///
-/// If the witness script contains `OP_CODESEPARATOR`s, it is up to the caller
-/// to remove everything up to and including the last **executed**
-/// `OP_CODESEPARATOR` before the signature check.
impl AsRef<ScriptCode> for WitnessScript {
fn as_ref(&self) -> &ScriptCode {
ScriptCode::from_bytes(self.as_bytes())Why this scored 19/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.