08 - Outpoint
Last updated
Was this helpful?
Last updated
Was this helpful?
The outpoint for the input being signed is a 36 byte long data item made of the concatenation of the input TXID and Vout (32 + 4 bytes).
It can be used for a variety of purposes including:
Source of unique token ID
Input randomness
Check input sources
We will now look at examples of each of these.
Tokens within the system require a unique 20 byte ID. This is achieved by hashing <outpoint>
using OP_HASH160
.
<r_tx_preimg>
...
nVersion, hashPrevouts and hashSequence have been removed
<r_tx_preimg>
0x24
add outpoint length to the stack
<r_tx_preimg> 0x24
OP_SPLIT
Split outpoint from pre-image
<outpoint> <rr_tx_preimg>
OP_SWAP
Swap outpoint to front
<rr_tx_preimg> <outpoint>
OP_HASH160
Calculate txid using OP_HASH160
<rr_tx_preimg> <token_id>
...
rest of script
The token_id can now be added to an output script and enforced forward. This will be covered later in this chapter.
The TXID cannot be known until the transaction is final, making it ideal as a source of input randomness. This example uses the MODULO function to implement a Rock-Paper-Scissors game.
<r_tx_preimg>
...
Version, hash_prevouts and hash_nSequence have been removed
<r_tx_preimg>
0x24
add outpoint length to the stack
<r_tx_preimg> 0x24
OP_SPLIT
Split outpoint from pre-image
<outpoint> <r_tx_preimg>
OP_DROP
Drop remaining pre-image
<outpoint>
OP_SHA256
Generate random output
<random_value>
OP_3
Put 3 on the stack
<random_value> 0x03
OP_MOD
Calculate modulo 2 - 1 or 0 outcome
<rem>
OP_IFDUP
Duplicates the stack if not zero
[0x00, 0x01 0x01, 0x02 0x02]
OP_NOTIF
3 possible stack states after OP_IFDUP. If 0, enter if statement path
<rock>
Zero is rock
[0x01 , 0x02]
OP_ELSE
If not zero, duplicated outcome remains.
[0x01 , 0x02]
OP_1
Test for 1
[0x01 , 0x02] 0x01
OP_EQUAL
Equality check
<result>
OP_IF
Enter if
<paper>
1 is paper
OP_ELSE
Else
<scissors>
2 is scissors
<rock/paper/scissors>
OP_ENDIF
Endif
<rock/paper/scissors>
...
rest of script
This UTXO requires that it be spent in a transaction with the two outputs below it in the previous transaction. It uses hash_prevouts which must be left on the stack in an earlier section of script.
<hash_prevouts> <r_tx_preimg>
...
Version and hash_nSequence have been removed and any needed conditions have been checked.
<hash_prevouts> <r_tx_preimg>
0x24
add outpoint length to the stack
<hash_prevouts> r_tx_preimg> 0x24
OP_SPLIT
Split outpoint from pre-image
<hash_prevouts> <outpoint> <r_tx_preimg>
OP_SWAP
Swap outpoint to front
<hash_prevouts> <r_tx_preimg> <outpoint>
OP_DUP
<hash_prevouts><r_tx_preimg> <outpoint> <outpoint>
0x20
add 32 to stack
<hash_prevouts><r_tx_preimg> <outpoint> <outpoint> 0x20
OP_SPLIT
Split the txid and vout from the outpoint
<hash_prevouts><r_tx_preimg> <outpoint> <txid><vout>
OP_1ADD
add 1 to the vout
<hash_prevouts><r_tx_preimg> <outpoint> <txid><vout+1>
OP_2DUP
Duplicate txid and vout+1
<hash_prevouts><r_tx_preimg> <outpoint> <txid><vout+1> <txid><vout+1>
OP_1ADD
add 1 to the vout
<hash_prevouts><r_tx_preimg> <outpoint> <txid><vout+1> <txid><vout+2>
OP_CAT
Create outpoint 3
<hash_prevouts><r_tx_preimg> <outpoint> <txid><vout+1> <outpoint3>
OP_CAT
Join to previous vout
<hash_prevouts><r_tx_preimg> <outpoint> <txid><vout+1&outpoint3>
OP_CAT
Join to txid
<hash_prevouts><r_tx_preimg> <outpoint> <outpoint2&3>
OP_CAT
Join all three outpoints together
<hash_prevouts><r_tx_preimg> <outpoint1&2&3>
OP_SHA256
hash outpoints
<hash_prevouts><r_tx_preimg> <hashoutpoints>
OP_ROT
Rotate hash_prevouts to top of stack
<r_tx_preimg> <hashoutpoints> <hash_prevouts>
OP_EQUALVERIFY
Check condition is met
<r_tx_preimg>
...
Rest of script