TeachMeBitcoin

P2SH-P2WSH Nested Multisig - Full Execution

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

8. P2SH-P2WSH Nested Multisig — Full Execution

Overview

P2SH-P2WSH wraps a P2WSH witness program inside P2SH, providing backward-compatible access to SegWit-discounted complex scripts (like multisig). The redeemScript is a 34-byte P2WSH script (OP_0 <32-byte SHA256 hash>), and the actual multisig logic lives in the witnessScript.

This three-layer nesting (P2SH → P2WSH → multisig) may seem convoluted, but it was essential for wallet compatibility during the SegWit transition and is still found in production systems.

Script Structure

witnessScript (the actual multisig logic):

OP_2 <pk1> <pk2> <pk3> OP_3 OP_CHECKMULTISIG

redeemScript (P2WSH wrapping of witnessScript):

OP_0 <32-byte SHA256(witnessScript)>

scriptPubKey (P2SH wrapping of redeemScript):

OP_HASH160 <HASH160(redeemScript)> OP_EQUAL

scriptSig:

PUSH(redeemScript)

Witness:

<dummy OP_0>
<sig1>
<sig2>
<witnessScript>

Execution Walkthrough

Phase 1 — P2SH check:
  scriptSig contains redeemScript push
  HASH160(redeemScript) == scriptPubKey hash → PASS

Phase 2 — SegWit detection:
  redeemScript = OP_0 <32 bytes> → recognized as v0 witness program
  scriptSig must only contain one push → validated

Phase 3 — P2WSH check:
  Last witness item = witnessScript
  SHA256(witnessScript) == 32-byte witness program in redeemScript → PASS

Phase 4 — witnessScript execution:
  Remaining witness stack: [<dummy>, <sig1>, <sig2>]
  Execute: OP_2 <pk1> <pk2> <pk3> OP_3 OP_CHECKMULTISIG

Result: [1] → VALID

Practical Use Case

Hardware wallets supporting multisig often use P2SH-P2WSH for corporate treasury setups, exchange cold storage, and custody solutions. The format provides:


- Backward-compatible '3...' addresses (for legacy senders)

- SegWit witness discount on large multisig scripts

- Strong 256-bit hash commitment to witnessScript

- Transaction malleability fix (critical for Lightning channels)
☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!