05 - OP_RETURN

OP_RETURN has a storied history which will not be covered in this content. More information can be found here.

OP_RETURN causes the script to terminate. If there is a single non-zero item on the stack the script will return TRUE, allowing it to be spent. If the topmost stack value is zero, or there are multiple items on the stack, the script fails and the transaction cannot be submitted to the blockchain.

This functionality can be applied to a multitude of use cases.

Example:

OP_DEPTH OP_1 OP_EQUAL OP_IF

<public_key> OP_CHECKSIG

OP_RETURN

OP_ENDIF

<rest_of_script>

In this example we first check the depth of the stack. This tells us how many items there are on the stack at this moment in the script processing. If there is just 1 item, the script expects that this will be a signature, that can be verified using the public key in the script. If the OP_CHECKSIG operation finds a valid signature, OP_RETURN will end the script successfully. If the signature check is invalid, the script will terminate in failure, and the output cannot be spent.

In this example, it is assumed that any further actions that take place in <rest_of_script> require two or more stack items in order to successfully validate.

The functionality of OP_CHECKSIG and other variations is covered in page 9 of this chapter.

FALSE RETURN scripts

One particular use-case for OP_RETURN is in the creation of FALSE RETURN scripts (commonly called 'OP_RETURN outputs').

A False Return script is created by generating a transaction output which uses OP_FALSE and OP_RETURN as the first two opcodes in its script. Because a script that begins with OP_FALSE OP_RETURN will always terminate in failure, these scripts are considered unspendable and are the only type of script that can be published with an output value of zero. As such they can be used as carriers for data items that do not form part of any locking conditions.

This technique is widely used on the BitcoinSV network for a variety of token solutions, and to allow platforms and applications to capture data onto the blockchain for indexing and analysis.

Example:

OP_FALSE OP_RETURN <data packet>

In this example, OP_FALSE is pushed onto the stack before the OP_RETURN opcode ends the script. Because OP_RETURN always returns with a false result this script can never be spent.

Last updated