Skip to main content

Basics

A Script serves as a set of instructions that accompany each transaction, known as the "locking script." This Script dictates how the recipient can access the transferred Bitcoins. It's a stack-based language and is processed from left to right.

To access the funds protected by the "locking script," an "unlocking script" is required. The transaction outputs become unlocked and spendable only when the combined scripts (unlocking and locking) produce a valid result.

Locking script is more technically known as scriptPubKey and unlocking script is refered to as witness in SegWit transaction and scriptSig in non-SegWit transactions.

note

Our focus will be primarily on SegWit transactions (P2WSH and P2WPKH) as non-SegWit transactions (P2SH and P2PKH) are becoming less common and have a significant drawback – malleability. This issue is particularly problematic in P2WSH transactions.

Script language encompasses two primary components:

  • Data: This could be a public key, a signature, a text blob, or any other form of information.
  • OPCODES: These are basic constants, commands and functions used within the language.

You can find an excellent explanations and examples of the following on learnmeabitcoin.com/technical/script:

  • How Scripts work?
  • What makes a Script valid?
  • Where can you find Scripts in Bitcoin?
  • Why do we use Script?
  • Examples of some standard Scripts

They nicely explain all of the above and we find it difficult to explain it better.

Script Validation

Non-SegWit

Non-SegWit transactions get validated by executing a Bitcoin script which is constructed by joining the scriptPubKey and scriptSig fields, eg: script = scriptSig + scriptPubKey.

SegWit

In SegWit transactions, unlocking funds involves a two-step validation process. First, the transaction checks whether the scriptPubKey matches the hash of the witness_script, confirming the use of the correct witness_script. If the hash matches, the process then proceeds to execute and validate the Bitcoin script code within the witness. The transaction successfully unlocks funds only if this script execution is validated successfully.

It's less crucial to focus on scriptPubKey validation if you can correctly construct and execute the Bitcoin script.

info

In non-SegWit transactions, the locking script is openly visible in the scriptPubKey field on the blockchain. Conversely, in SegWit transactions, the locking script is not directly visible, as it is represented by a hash. Therefore, when spending from a SegWit address, you must remember the witness_script that was used to lock the funds.

How is Bitcoin script executed?

Execution of the Script happens on the stack, but before we execute it we need to construct the script.

Script is constructed by joining the witness_data and witness_script, with the witness_data coming first.

witness_script = PubKey OP_CHECKSIG
witness_data = Sig
script = witness_data + witness_script
script = Sig PubKey OP_CHECKSIG

StackScriptDescription
SigPubKeyOP_CHECKSIG
SigPubKeyOP_CHECKSIGSig is added to the stack
PubKey
Sig
OP_CHECKSIGPubKey is added to the stack
1OP_CHECKSIG checks the signature for the top two stack items