AI-generated analysisPublished automatically and not human-verified. Validated context appears in community notes below.
← Watch feed
Informational 19 Bitcoin

Merge rust-bitcoin/rust-bitcoin#6956: bitcoin: introduce ScriptCode for sighash computation

Public commit record

What the developer wrote

Authored by Andrew Poelstra

100/100 · Strong
Merge rust-bitcoin/rust-bitcoin#6956: bitcoin: introduce ScriptCode for sighash computation

dc64b80c64a062badef7f070c60be329862e13fe bitcoin: accept ScriptCode in P2WSH sighash (jrakibi)
b7dab15c0912524f19e9234f2923613c7dda3ec8 bitcoin: test legacy and Segwit script code (jrakibi)
00462c353ae26e8a6c39e2500a6e36724975ae45 bitcoin: accept ScriptCode in legacy sighash (jrakibi)
b0eb34c9a7a9c066883bceb03990232cbf513e04 bitcoin: return P2wpkhScriptCode from p2wpkh_script_code (jrakibi)
0ece781f091b113616756f79a4a1100db20b8ea7 bitcoin: change SegWit v0 encoding to accept ScriptCode (jrakibi)
ab63f7a73db5ccdeb5a7d4178c157542510815dd bitcoin: add P2wpkhScriptCode (jrakibi)
8940e6697c2c77ab938c5c206e8d02d0d73f8df0 primitives: add AsRef<ScriptCode> for RedeemScript and ScriptPubKey (jrakibi)
1cfb377923fb746ab4fe30f0a61e2d2e6b5db07c primitives: add AsRef<ScriptCode> for witness scripts (jrakibi)
ee4e9f0be5560c950c93ad057e28649339a71028 primitives: add new ScriptCode type (jrakibi)

Pull request description:

Introduce `ScriptCode` for sighash computation and `P2wpkhScriptCode` to hold the fixed 25 byte P2WPKH script code without allocating.


I tried to make the commits as atomic as possible and explain the reason behind each change in the commit message.

For witness scripts, I only added `AsRef<ScriptCode>` since the witness script is not always the script code. For the new `P2wpkhScriptCode`, it makes sense to implement `Deref` because it already holds the complete P2WPKH script code.

For OP_CODESEPARATOR handling in Segwit, we don’t need to introduce any removal logic as we did for legacy, since the remaining separators must be kept. tho it's still up to the caller to remove everything up to and including the last executed OP_CODESEPARATOR before the signature check, as clarified in the doc comment.


In #6079, Kix proposed `ScriptCodeBuf([u8; SCRIPT_CODE_LEN])`. I used `P2wpkhScriptCode` instead because ScriptCodeBuf might suggest a type that covers other output types too. I’m happy to rename it to ScriptCodeBuf if preferred.


Closes https://github.com/rust-bitcoin/rust-bitcoin/issues/6079, https://github.com/rust-bitcoin/rust-bitcoin/issues/6808 and #4135


ACKs for top commit:
apoelstra:
ACK dc64b80c64a062badef7f070c60be329862e13fe; successfully ran local tests


Tree-SHA512: 47d9ea3cf20d1231f18d1cb365124ea5326dc9430736ae86f442feaa21c5c4213503aa1e20ca63e8495e0a39c5da9821a224acfbc162987dd9a81d97b2eea743
✓ Specific, descriptive subject✓ Names a concrete action or component✓ Provides detailed explanatory context✓ Explains rationale or failure mode✓ Mentions testing or verification✓ Links an issue, advisory, or supporting reference✓ Names security-relevant behavior explicitly
The short version

What changed, and why it matters

This is a code-quality and type-safety refactor for the rust-bitcoin library. It introduces a dedicated ScriptCode type for computing Bitcoin transaction signatures (sighashes), replacing the previous practice of reusing the WitnessScriptBuf type. The change makes the API clearer and reduces the chance that developers accidentally pass the wrong kind of script when signing transactions. There is no direct security vulnerability being patched here; it is a preventive correctness improvement.

Recommended action

No urgent action required. Downstream users should review the API changes (new ScriptCode type, P2wpkhScriptCode, and AsRef<ScriptCode> conversions) when upgrading, and update sighash call sites accordingly. The change is a routine refactor with preventive security benefits.

Security signals we found

01

Type-system hardening: new ScriptCode type prevents misuse of witness/redeem/scriptPubKey types in sighash APIs

02

API change: legacy and segwit v0 sighash functions now accept ScriptCode instead of arbitrary script types

03

Correctness: OP_CODESEPARATOR encoding logic now operates on ScriptCode

04

New test coverage: BIP-143 P2WSH OP_CODESEPARATOR sighash vectors added

05

No direct vulnerability or exploit path identified in the diff

Risk score

Why this scored 19/100

Our methodology →
Potential impact 2/30
Exploitability 1/25
Stealth signal 1/15
Affected reach 3/15
Confidence 8/10
Evidence quality 4/5
Human-validated context

Community notes

Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.

No validated notes yet.

The AI analysis stands alone for now. Submit a note if you can add evidence or important context.