Transaction Scripts and Scripts Language

Youngchan Kim
2 min readFeb 7, 2018

--

This post is a study note of CH.06 Mastering Bitcoin and Youtube video of Andreas.

Bitcoin transaction script language, called Script, is a stack-based execution language.
Both the locking script which is placed on a UTXO and the unlocking script are written in this scripting language.

When a transaction is validated, the unlocking script in each input is executed alongside the corresponding locking script to see if it satisfies the spending condition.

Features of a transaction script.

  • Turing Incompleteness : There are no loops or complex flow control other than conditional flow control. So a script have limited complexity and predictable execution times.
  • Stateless Verification : Script language is stateless. No state prior or after execution.

Locking script

A locking script is a spending condition placed on an output.
It gonna be recored on blockchain. So it is important to minimize the size of the locking script. That is why unlocking script provides conditions which need on side of locking script.
Locking script was called a scriptPubKey, because it usually contained a public key or bitcoin address (public key hash). It is also called witnessScript(for segwit) or cryptographic puzzle.

Unlocking script

Unlocking script is a script which satisfy the condition of corresponding locking script. It is part of every transaction input. It contains a digital signature produced by private key.
The unlocking script was called scriptSig, because it usually contained a digital signature. It is also called witness(for segwit).

Every bitcoin validating node will validate transactions by executing the locking and unlocking scripts together. The input is valid if the unlocking script satisfies the locking script conditions.

Example

Let’s suppose that we have locking script 3 OP_ADD 5 OP_EQUAL and unlocking script 2 .
Then the validation software combines the locking and unlocking scripts and the resulting script is: 2 3 OP_ADD 5 OP_EQUAL .
Because script is a stack-based execution language, It is gonna true in the end. This means that anyone who can satisfy the condition, 2 in this case, can use the output.

Actually for security reasons, unlocking & locking script is executed separately.

  1. Unlocking script is executed, using the stack execution engine.
  2. If end without error, the main stack (not the alternate stack) is copied and the locking script is executed.
  3. If the result is “TRUE,” the input is a valid authorization to spend the UTXO.

A transaction is valid if nothing in the combined script triggers failure and the top stack item is True (non-zero) when the script exits. (link)

--

--