Bitcompare Community

Vicky Sharp
Vicky Sharp

Posted on

What are Bitcoin transaction scripts?

Top comments (1)

Collapse
 
mariahover profile image
Maria Hover

Bitcoin transaction scripts are small pieces of code written in a Forth-like, stack-based scripting language called Bitcoin Script. These scripts are used to lock and unlock Bitcoin (BTC) funds in a secure, programmable way. The script defines the conditions under which a Bitcoin can be spent, and it plays a critical role in the execution of Bitcoin transactions.

Unlike traditional programming languages, Bitcoin Script is not Turing complete. This means it does not allow for loops or complex computation to prevent infinite loops and keep the system lightweight and efficient. Instead, the script performs simple logical and arithmetic operations, ensuring that transactions remain predictable, secure, and verifiable.

How Do Bitcoin Transaction Scripts Work?

A Bitcoin transaction involves two main components of the script:

1. Locking Script (scriptPubKey)
2. Unlocking Script (scriptSig)

When someone sends Bitcoin, they attach a locking script (scriptPubKey) to the output of the transaction. When the recipient wants to spend that Bitcoin, they must provide an unlocking script (scriptSig) that satisfies the conditions defined by the locking script. If the unlocking script successfully satisfies the locking script, the transaction is considered valid, and the Bitcoin can be spent.

1. Locking Script (scriptPubKey)
The locking script defines the rules that must be met to unlock the Bitcoin. It is attached to the Bitcoin being sent. The most common example is a Pay-to-Public-Key-Hash (P2PKH) transaction, where the script locks the funds to a specific Bitcoin address.

Example of a locking script (P2PKH)

OP_DUP OP_HASH160 <recipient's public key hash> OP_EQUALVERIFY OP_CHECKSIG
Enter fullscreen mode Exit fullscreen mode

Explanation:

OP_DUP: Duplicates the top item on the stack (the public key in this case).
OP_HASH160: Hashes the duplicated public key using SHA-256 and RIPEMD-160.
<recipient's public key hash>: This is the hashed version of the recipient's public key.
OP_EQUALVERIFY: Verifies that the two items on the stack (hashed public key and the public key hash) are equal.
OP_CHECKSIG: Checks that the transaction is properly signed by the private key corresponding to the public key.

2. Unlocking Script (scriptSig)
The unlocking script is used to satisfy the conditions of the locking script. It typically includes the signature and the public key. For a standard P2PKH transaction, the unlocking script contains the digital signature and the public key corresponding to the Bitcoin address.

Example of an unlocking script (P2PKH)

<signature> <public key>
Enter fullscreen mode Exit fullscreen mode

Explanation:

The signature is created using the sender's private key.
The public key is used to verify the signature and unlock the funds.

How Are Scripts Processed?

When a transaction is made, the unlocking script and the locking script are concatenated and evaluated together. The process looks like this:

  1. The unlocking script is executed first, placing the signature and public key onto the stack.
  2. The locking script is then executed, using the values from the stack to check if the locking conditions are met.
  3. If all conditions are met (i.e., the signature is verified, and the hash of the public key matches), the transaction is valid.

Types of Bitcoin Scripts

Here are some key types of Bitcoin transaction scripts:

Script Type Description Usage Example
Pay-to-Public-Key (P2PK) Locks funds directly to a specific public key. Early Bitcoin transactions.
Pay-to-Public-Key-Hash (P2PKH) Locks funds to a Bitcoin address (hashed public key). Most common transaction type.
Pay-to-Script-Hash (P2SH) Locks funds to a hash of a script. Multi-signature transactions.
Multi-Signature (Multisig) Requires multiple signatures to unlock funds. Used in escrow or joint wallets.
Time-Locked Scripts Locks funds until a specific time or block height. Used in payment channels.

Example Walkthrough of a Bitcoin Transaction

Let's walk through a simple Pay-to-Public-Key-Hash (P2PKH) transaction.

1. Alice Sends BTC to Bob: Alice creates a transaction with a locking script attached to the BTC output that contains Bob’s public key hash.

2. Bob Wants to Spend the BTC: Bob creates a new transaction with an unlocking script that includes his public key and a signature.

3. Verification Process: The Bitcoin network runs both scripts together.

  • Bob's unlocking script places his signature and public key on the stack.
  • The locking script verifies the signature and public key hash using OP_DUP, OP_HASH160, OP_EQUALVERIFY, and OP_CHECKSIG.
  • If all operations pass, the transaction is considered valid, and the funds are moved to the next output.

Security Features of Bitcoin Scripts

Bitcoin's scripting system includes several built-in security features:

  • Non-Turing Completeness: The script avoids loops, preventing infinite loops or resource exhaustion.
  • Stack-Based Execution: Ensures simplicity and transparency of logic execution.
  • Reduced Attack Surface: Limited instruction set means fewer potential vulnerabilities.
  • Script Verification: Each transaction is verified by the Bitcoin network before being included in a block.

Use Cases for Bitcoin Scripts

Bitcoin scripts go beyond simple payments. Here are some notable use cases:

  • Multi-Signature Wallets: Used to enable escrow, business accounts, or joint accounts.
  • Payment Channels: Bitcoin Lightning Network uses time-locked scripts to secure off-chain payments.
  • Smart Contracts: While limited, basic forms of smart contracts can be created using scripts.
  • Atomic Swaps: Trustless exchange of one cryptocurrency for another using hashed time-locked contracts (HTLC).

Conclusion

Bitcoin transaction scripts play a vital role in securing and validating Bitcoin transactions. By using simple, stack-based logic, these scripts enable various types of payments, from basic single-key payments to more advanced contracts like multi-signature wallets and time-locked transactions. Their limited functionality enhances security, predictability, and efficiency, ensuring the Bitcoin network remains robust and decentralized.