An Introduction to Bitcoin Script
Table of Interests
Looking for the best article on Bitcoin Script?, then An Introduction to Bitcoin Script is f0r you.
When a key holder wants to give money to someone else, they unlock their box. They make out a new cheque that references the older one (which is then destroyed) and lock it in a box that the recipient can open. To spend that, the new recipient repeats the process.
How does Bitcoin work?
When you make a transaction, you’re broadcasting that combination to the network. Each node that receives it will check the program, which tells it whether the transaction is valid. If not, it’ll just be discarded, and you won’t be able to spend the locked funds.
Understanding the Bitcoin stack
Elements A, B, and C being added and “popped” from the stack.
That was just a made-up program. Let’s look at some actual ones now.
Pay-to-Pubkey (P2PK)
Pay-to-Pubkey (P2PK) is incredibly straightforward. It involves locking funds to a particular public key. If you wanted to receive funds in this manner, you would provide the sender with your public key, as opposed to a Bitcoin address.
For reasons we’ll elaborate on in the next section, P2PK isn’t really used anymore.
Pay-to-Pubkey-Hash (P2PKH)
Pay-to-Pubkey-Hash (P2PKH) is now the most common type of transaction. Unless you’re going out of your way to download archaic software, your wallet is likely doing these by default.
The scriptPubKey in P2PKH is the following:
OP_DUP OP_HASH160 <public key hash> OP_EQUALVERIFY OP_CHECKSIG
Before we introduce the scriptSig, let’s break down what the new opcodes will do:
OP_DUP
OP_HASH160
This pops the first element and hashes it twice. The first round will hash with the SHA-256 algorithm. The SHA-256 output is then hashed with the RIPEMD-160 algorithm. The resulting output is added back onto the stack.
OP_EQUALVERIFY
OP_EQUALVERIFY combines two other operators – OP_EQUAL and OP_VERIFY. OP_EQUAL pops two elements and checks if they’re identical. If they are, it adds a 1 to the stack. If not, it adds a 0. OP_VERIFY pops the top element and checks if it’s True (i.e., non-zero). If it isn’t, the transaction fails. Combined, OP_EQUALVERIFY causes the transaction to fail if the top two elements don’t match.
This time, the scriptSig looks like this:
<signature> <public key>
You need to provide a signature and the corresponding public key to unlock P2PKH outputs.
You can see what’s going on in the above GIF. It’s not too different from a P2PK script. We’re just adding an extra step to check that the public key matches the hash in the script.
Pay-to-Script-Hash (P2SH)
e145fe9ed5c23aa71fdb443de00c7d9b4a69f8a27a2e4fbb1fe1d0dbfb6583f1
You don’t need to know the hash’s input to lock funds to it. The spender, however, needs to provide the script that was used to hash it and needs to satisfy the conditions of that script.
The above hash was created from the following script:
<multiply by 2> <4> <check if equal>
In real life, the scriptPubKey for a P2SH output is:
OP_HASH160 <redeemScript hash> OP_EQUAL
<signature> <public key> <redeemScript>
Our evaluation differs a bit from the stack execution we’ve seen so far. It happens in two parts. The first simply checks that you’ve provided the correct hash.
You’ll note that we don’t do anything with the elements preceding the redeemScript. They’re not used at this point. We’ve reached the end of this mini-program, and the top element is non-zero. That means it’s valid.
Once your redeemScript has been expanded, you can see that we have a situation that looks exactly like a regular P2PKH transaction. From there, you just run it as you would a normal one.
We’ve demonstrated what’s called a P2SH(P2PKH) script here, but you’re unlikely to find one of those in the wild. Nothing’s stopping you from making one, but it gives you no added benefits and ends up taking up more space in a block (and, therefore, costs more).
But with P2SH, it doesn’t matter how complex the spending conditions are. The redeemScript’s hash is always of a fixed size. The costs are therefore passed onto the user(s) who want to unlock the locking script.
SegWit compatibility is another case where P2SH comes in handy (we’ll get into the details of how the transaction structure differs in the next section). SegWit was a soft fork that resulted in a change to block/transaction formats. Because it’s an opt-in upgrade, not all wallet software recognizes the changes.
That doesn’t matter if clients wrap the SegWit script hash in P2SH. As with all transactions of this type, they don’t need to know what the unlocking redeemScript will be.
SegWit Transactions (P2WPKH and P2WSH)
If you’ve come across addresses beginning with ‘bc1’, those are what we call SegWit-native (as opposed to just SegWit-compatible, which begin with a ‘3’ since they’re P2SH addresses).
Pay-to-Witness-Pubkey-Hash (P2WPKH)
Pay-to-Witness-Pubkey-Hash (P2WPKH) is the SegWit version of P2PKH. Our witness looks like this:
<signature> <public key>
You’ll note that this is the same as the scriptSig from P2PKH. Here, the scriptSig is empty. Meanwhile, the scriptPubKey resembles the following:
<OP_0> <public key hash>
That looks a bit strange, doesn’t it? Where are the opcodes to let us compare the signature, public key and its hash?
Pay-to-Witness-Script-Hash (P2WSH)
Pay-to-Witness-Script Hash (P2WSH) is the new P2SH. If you’ve made it this far, you can probably figure out how this will look, but we’ll run through it anyway. Our witness is what we’d normally put in the scriptSig. In a P2WSH that wraps a P2PKH transaction, for instance, it might look something like this:
<signature 1> <public key>
Here’s our scriptPubKey:
<OP_0> <script hash>
The same rules hold. SegWit nodes read the length of the script hash and determine that it’s a P2WSH output, which is evaluated similarly to P2SH. Meanwhile, old nodes just see it as an anyone-can-spend output.
Closing thoughts
In this article, we’ve learned a bit about the building blocks of Bitcoin. Let’s summarize them quickly:
Script type | Description |
---|---|
Pay-to-Pubkey (P2PK) |
Locks funds to a particular public key |
Pay-to-Pubkey-Hash (P2PKH) |
Locks funds to a particular public key hash (i.e., an address) |
Pay-to-Script-Hash (P2SH) |
Locks funds to the hash of a script that the recipient can provide |
Pay-to-Witness-Pubkey-Hash (P2WPKH) |
The SegWit version of P2PK |
Pay-to-Witness-Script-Hash (P2WSH) |
The SegWit version of P2SH |
Once you dig deeper into Bitcoin, you begin to understand why it has so much potential. Transactions can be made up of many different components. By manipulating these building blocks, users have a great deal of flexibility when it comes to setting conditions on how and when funds can be spent.
Common Cryptocurrency Scams on Mobile Devices
Ultimate Guide to Symmetric versus Asymmetric Encryption
How Blockchain Is Used in The Internet of Things (IoT)
Ultimate Guide to Understanding What Makes a Blockchain Secure
Upto Date Blockchain Use Cases
Initial Coin Offering List – Comprehensive List of Projects Currently Doing ICO
Ultimate Guide to Initial Coin Offering (ICO)
Ultimate Guide to Understanding What Fractional Reserve is
Understanding Why Public WiFi Is Insecure
The Ultimate History of Cryptography
Understanding what DoS Attack is
Ultimate Guide to what Fiat Currency is
Ultimate Guide to zk-SNARKs and zk-STARKs
Things to Avoid When Using Binance Chain
How Blockchain Is Used In Charity
How Blockchain is Used in Supply Chain
Ultimate Guide to What a Replay Attack is
Ultimate Guide to Delegated Proof of Stake
Ultimate Guide to what Ransomware is
Ultimate Guide to understanding Cryptojacking
Ultimate Guide to Understanding Inflation for Beginners
How to Know Cryptocurrencies Pyramid and Ponzi Schemes
Ultimate Beginner’s Guide to Bitcoin’s Lightning Network
Advantages and Disadvantages Of Blockchain
Ultimate Guide to Ethereum Plasma
Leave a Reply