P2WPKH Transaction
  • The pay-to-witness-public-key-hash output can be spent without an endorsement in the input script.

  • Pay-to-Witness script begins with empty data push
    • OP_0 pushes empty array to stack.
    • Signals Version 0 witness script.

  • Spending TX input script is empty, endorsement is placed in witness
    • Output is spendable by endorsement from private key of 20-Byte public key hash, equivalent to pay-to-public-key-hash output.
    • Witness is an off-script/off-stack transaction element which is assessed during transaction verification.

P2WPKH Script Run
  • 1) Input & Output Scripts are run
    • P2WPKH Witness script elements are pushed onto stack.

  • 2) P2WPKH pattern is recognised on stack
    • Pay-to-witness pattern with a 20-byte data-push is recognised as a P2WPKH script.

  • 3) P2PKH script run
    • The stack is cleared and witness elements are pushed on.
    • A P2PKH script with the public key hash from the witness script is initiated and run.

  • 4) Final stack evaluation

  • Note: The pay-to-public-key-hash script is not explicitly expressed in the p2wpkh script, but rather, is implied by the pay-to-witness script pattern.
Pay-to-Witness Endorsement
  • BIP143 Signature Hash Algorithm for Pay-to-Witness Transactions
    • Modified signature hash preimage compared to non-witness txid serialisation.
    • O(n) computation time, n = length of transaction.
    • For offline signers: Previous output(s) is now included.

  • Script Code for P2WPKH(public key) is the P2PKH(public key) Script
    • Script code is part of signature hash preimage.

  • Commitment to Inputs and Outputs signaled by Sighash Marker
    • Non-committed input, sequence and output fields are initialised to 0.

P2WSH Transaction
  • Pay-to-Witness script begins with empty data push
    • OP_0 pushes empty array to stack.
    • Signals Version 0 witness script.

  • Output is spendable by Embedded Script together and its Unlocking Script
    • Embedded script is sha256 hashed
    • The spending TX witness includes the embedded script and its unlocking script.

  • Witness Elements:
    • Unlocking script operations are expressed as individual witness elements.
    • Embedded script is a single witness element.

P2WSH Script Run
  • 1) Input & Output Scripts are run
    • P2WSH witness script elements are pushed onto stack.

  • 2) P2WSH pattern is recognised on stack
    • Pay-to-witness pattern with a 32-byte data-push is recognised as a P2WSH script.
    • Embedded script witness element is evaluated against the on-stack embedded script hash value.

  • 3) Embedded script run
    • If the embedded script hashes correctly, the stack is cleared.
    • Unlocking script elements are pushed onto stack.
    • Embedded script is run.

  • 4) Final stack evaluation

  • Note: Evaluating the embedded script in the witness against its hash digest in the p2sh script is not explicitly expressed in the p2wsh script, but rather, is implied by the pay-to-witness script pattern.
P2SH(P2WSH) Transaction
  • Pay-to-Witness outputs can be wrapped in a P2SH output script
    • Non witness-enabled wallets can send to P2SH(P2WPKH) addresses.

  • Output is spendable by P2WPKH script and witness
    • Input script must include P2WPKH(public key) script.
    • Witness must include endorsement and public key for P2WPKH script runs.

P2SH(P2WPKH) Script Run
  • 1) Input & Output Scripts are run
    • Embedded P2WPKH in input script must hash correctly to hash digest in P2SH.

  • 2) P2SH pattern is recognised on stack
    • Embedded P2WPKH script is loaded.

  • 3) P2WPKH & P2PKH script runs
    • After a successful P2WPKH run, the witness elements are pushed onto the stack.
    • Then, the P2PKH script is run.

  • 4) Final stack evaluation

  • Note: See previous P2WPKH section for details on indivdiual P2WPKH and P2PKH script runs.
P2SH(P2WSH) Transaction
  • Pay-to-Witness outputs can be wrapped in a P2SH output script
    • Non witness-enabled wallets can send to P2SH(P2WSH) addresses.

  • Output is spendable by P2WSH script and witness
    • Input script must include P2WSH(Embedded Script).
    • Witness must include *unlocking script operators of the embedded script as individual witness elements, as well the embedded script as a single witness element.

P2SH(P2WSH) Script Run
  • 1) Input & Output Scripts are run
    • Embedded P2WPKH in input script must hash correctly to hash digest in P2SH.

  • 2) P2SH pattern is recognised on stack
    • Embedded P2WSH script is loaded.

  • 3) P2WPKH & P2PKH script runs
    • After the successful P2WSH run, the unlocking script operations in the witness are pushed onto the stack.
    • Finally, the P2WSH embedded script is run.

  • 4) Final stack evaluation

  • Note: See previous P2WSH section for details on indivdiual P2WSH and P2WSH embedded script runs.