For further actions, you may consider blocking this person and/or reporting abuse
Read next
El Salvador Mined Nearly 474 Bitcoins in 3 Years
Maria Hover -
MicroStrategy's Stock Surge Linked to Bitcoin Price Predictions
Lisa Cantin -
Trump Proposes Using Bitcoin to Address $35 Trillion National Debt
Margaret Boucher -
Bitcoin Price Faces Potential Flash Crash to $40,000
Margaret Boucher -
Top comments (1)
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)
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)
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:
Types of Bitcoin Scripts
Here are some key types of Bitcoin transaction scripts:
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.
OP_DUP
,OP_HASH160
,OP_EQUALVERIFY
, andOP_CHECKSIG
.Security Features of Bitcoin Scripts
Bitcoin's scripting system includes several built-in security features:
Use Cases for Bitcoin Scripts
Bitcoin scripts go beyond simple payments. Here are some notable use cases:
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.