P2WSH scripts serve a role similar to P2SH in the realm of SegWit, catering to more intricate transaction types. The logic for a P2WSH witness script & output pubKeyScript is exactly the same as a P2SH input ScriptSig & output pubKeyScript. However, there is a large difference in the op_codes explicitly expressed in the raw transaction. Wallets/clients know that when a P2WSH output is detected they'll need to insert the common P2SH op_codes; therefore, a P2WSH pubKeyScript requires only one specific item: the hashed lock script.Here's a step-by-step breakdown:
1. Validate Input Hashed Lock Script Matches Previous Output Hashed Lock Script.
First, to spend Bitcoin sent to a hashed script, it must be proven that the script we’re unlocking matches the original hashed script. This means that the stack first consumes the entire lock/redeem script as a single array & hashes it with HASH160. This is then compared to the original hashed script using OP_EQUAL.
2. Execution Of Unlock Script & Lock Script.
If the last op_code (OP_EQUAL) of the previous validation step returns 1/true, then we can move on to actually unlocking the lock script by pushing both the lock script (now separated into the appropriate data & op_code bytes). This is a more normal execution in which all elements are pushed to the stack & then processed in typical LIFO behavior.
OP_Code(s) Review
P2WSH requires two (2) pieces of data, the scripts, & four (4) op_codes.
# WitnessScript
[witness]
[witness-script]
[signature]
# PubKeyScript
<sha256>
[witness-script-hash]
<decode>
<equal>
Step 1
Decode the witness to extract <sig>, <witness script>