12 - hashOutputs
Last updated
Last updated
hashOutputs is a SHA256 hash of the concatenation of all outputs being signed in Transaction output format.
Transaction Output Format is a concatenation of the following 3 items:
The value of the output in Satoshis (8 bytes)
The length of the locking script (varInt format)
The locking script itself (string)
Whether the signature is applied to all, one or none of the outputs depends on the SIGHASH flags used in the signature.
If SIGHASH_ALL is used, all of the transaction outputs are concatenated and hashed together.
If SIGHASH_SINGLE is used, only the output with the same index as the input being signed is hashed. If there is no output at that index value, hashOuts is a 32 byte null string.
If SIGHASH_NONE is used, hashOuts is a 32 byte null string.
Hash_outputs is particularly important for staged processing as it allows the script to forward-enforce the conditions within the next output, which may include use of some or all of the script used in the input, or may be chosen from a variety of pre-determined script outputs.
<input> <r_tx_preimg>
...
Version, hash_prevouts, hash_nSequence, hash_outpoints, script, value and nSequence have been removed
<input> <r_tx_preimg>
0x20
Hash_outputs is 32 bytes (0x20)
<input> <r_tx_preimg> 0x20
OP_SPLIT
Split nSequence
<input> <hash_outputs> <rr_tx_preimg>
OP_SWAP
Swap hash_outputs to top of stack
<input> <rr_tx_preimg> <hash_outputs>
OP_ROT
Move input to top of stack
<rr_tx_preimg> <hash_outputs> <input>
OP_IF
True or false input
<rr_tx_preimg><hash_outputs>
<true_case_output>
Load the hash of the true case output onto the stack
<rr_tx_preimg><hash_outputs>
OP_ELSE
If input is false
<rr_tx_preimg><hash_outputs>
<false_case_output>
Load the hash of the false case output onto the stack
<rr_tx_preimg> <hash_outputs> <next_state_output>
OP_ENDIF
Correct next state output is loaded onto stack, exit IF loop
<rr_tx_preimg> <hash_outputs> <next_state_output>
OP_EQUALVERIFY
Check that desired next state matches transaction pre-image
<rr_tx_preimg>
...
rest of script
In this example, we are splitting the output hash from the pre-image and checking it against one of two pre-defined output hashes that represent our next state based on a true/false input. These pre-defined hashes can include any number of outputs with any script needed, and can attach any valid quantity of satoshis to each output.
Much more sophisticated checks can be done, including keeping <script> on the stack and checking that the next state includes the same script elements. This can be useful if you don't know exactly what the next state will be when the output is created. For instance, this might be for a digital object with transferrable ownership. Each new owner will want to add their own keys and checks, so this must be validated with the script.