Bitcoin Smart Contracts: A Beginner’s Guide
June 27, 2023
Bitcoin smart contracts are not often talked about, but they do exist. This guide will introduce you to Bitcoin smart contracts and discuss the different types with which you might be interacting without knowing it.
What is a Bitcoin Smart Contract?
A Bitcoin smart contract is blockchain-based code that autonomously executes actions according to predetermined conditions.
The Bitcoin blockchain enables smart contracts using Script, its programming language. Script is used to lock and unlock Bitcoin transactions, allowing the incorporation of smart contract capabilities by defining spending conditions for BTC, referred to as the locking script or the ScriptPubKey. Conversely, the data and script that meet the predefined conditions are known as the ScriptSig. In the case of utilizing Segregated Witness (a Bitcoin upgrade introduced in 2017), the data and script that meet the criteria are called ScriptWitness.
As Script is not Turing-complete, it is only programmable to a limited degree. Turing-completeness is a concept named after computer scientist Alan Turing. Turing suggested that a Turing-complete machine or programming language can solve any computational problem if it has enough resources like time and memory.
Bitcoin was deliberately designed not to allow for unlimited, fully expressive programmability in order to minimize programming errors. This ensures the Bitcoin network maintains robust security and remains resistant to DoS attacks.
While the Turing-incompleteness of Script does benefit the Bitcoin network tremendously, giving it more robustness in terms of its code base, it has prevented Bitcoin from being labeled as a “smart contract platform.” This is because some individuals believe a smart contract language must be Turing-complete, which is the case with almost all the existing smart contract blockchains in existence.
Despite this bias, we now know that Bitcoin’s Script language supports smart contracts, although in a more basic, less expressive form.
What Types of Bitcoin Smart Contracts Exist & How Do They Work?
Bitcoin smart contracts can be created on the base layer or layer-2 solutions. Below are the various types of smart contracts on the Bitcoin mainchain.
Pay-to-Public-Key-Hash (P2PKH)
P2PKH is the widely adopted Bitcoin smart contract for regular transactions. It enables BTC to be sent to an address where only the holder of the associated private key can spend the funds. P2PKH smart contracts enforce a condition that necessitates a user to provide a signature matching the script's specified public key hash to spend the bitcoin. In essence, the P2PKH smart contract grants exclusive spending rights to the recipient of a Bitcoin transaction.
Discreet Log Contracts (DLCs)
DLCs are employed when two parties enter into an agreement contingent upon the outcome of a forthcoming real-world event. For instance, DLCs may be used when two people make a bet on a soccer match. To obtain the match results, the contract relies on oracles that fetch the score from the real world and bring it on-chain. Once the smart contract receives this information, it autonomously executes based on the predefined conditions.
Hashed Time Lock Contract (HTLC)
A hashed time lock contract (HTLC) ensures that BTC cannot be spent until a specified duration has passed or a particular block height is achieved. Consequently, Bitcoin transactions can be time-locked for various purposes and conditions. For example, investors can utilize this contract to lock their Bitcoin investment for extended periods, spanning months or even years.
Multi-signature (Multisig)
Multisig smart contracts require multiple signatures for locked BTC to be spent. The smart contract specifies n public keys and m signatures. m may be equal to or less than n. Bitcoin can only be spent if users provide m signatures, each of which matches one of the n public keys specified.
For example, a multisig setup might look like this: 3-out-of-5. In this setup, three signatures are needed from a group of five public keys to initiate spending conditions. Multisig smart contracts are especially ideal for people who hold BTC collectively, as they prevent individuals from defrauding others.
Pay-to-Script-Hash (P2SH)
P2SH permits users to send BTC to the hash of any script, including those mentioned above. It is a type of ScriptPubKey that enables users to spend BTC based on the criteria of the script whose hash is specified in the transaction. After the SegWit upgrade, P2SH was extended to incorporate P2WSH. P2SH is highly flexible since users can construct multiple scripts.
Consider a situation in which Bob transfers 0.005 BTC to Alice through a P2SH transaction, including the script's hash in the transaction. This hash is required to access and spend the Bitcoin. The script might demand Alice to sign the transaction with her private keys and/or meet other criteria. So, when Alice wants to spend the 0.005 BTC, she will reconstruct the hash that Bob included in the transaction and sign with any private keys per the script’s criteria.
Pay-to-Taproot (P2TR)
P2TR is a smart contract introduced by the 2021 Taproot upgrade. It offers more flexibility and privacy to users. P2TR is a type of ScriptPubKey that locks BTC to a script that can again be unlocked by Merkelized Alternative Script Tree (MAST) or a public key. MAST encodes multiple scripts within a single transaction. Users can utilize any of these scripts to spend the locked BTC, but the unused scripts remain private. Also, if the public key is used to unlock the BTC, its status isn’t disclosed.
Examples of Bitcoin Layers with Smart Contract Capabilities
Bitcoin smart contracts can also be executed on layers built on the Bitcoin blockchain. Here are four examples of Bitcoin layers with smart contract capabilities:
Lighting Network
The Lightning Network (LN) is a layer-2 solution that uses smart contracts to enable instant Bitcoin payments. LN uses Bitcoin’s Script smart contract language, allowing people to conduct Bitcoin transactions at near-instant settlement and low cost. LN smart contracts include DLCs, HTLCs, and multisigs.
Joseph Poon and Thaddeus Dryja proposed LN in a paper published in 2016. Dryja also went ahead to write the first code for LN, which Lightning Labs later used as the foundation to implement their “first version” of the Lightning Network.
Liquid Network
The Liquid Network is a sidechain-based layer-2 solution rolled out by Blockstream in October 2018. It provides confidential and faster Bitcoin transactions. Additionally, Liquid allows developers building on the network to deploy financial assets on top of it. Also, financial products can be deployed using the Liquid network, such as options contracts and limit orders using smart contracts.
Rootstock
Rootstock (RSK) is a Bitcoin sidechain offering EVM-compatible smart contract functionality while relying on the Bitcoin network for its security. Therefore, a smart contract on RSK could theoretically be deployed on Ethereum as well because the sidechain supports Ethereum smart contract languages like Solidity. Thanks to its smart contract functionality, developers can build dApps on Rootstock for the DeFi ecosystem.
The creator behind RSK is RSK Labs, which launched the sidechain in January 2018.
Stacks
Stacks is a layer-2 solution with smart contract functionality. Smart contracts on Stacks are made possible by a programming language called Clarity. The smart contract functionality allows developers to build dApps that power a Bitcoin-based DeFi ecosystem.
Stacks co-founders are Muneeb Ali and Ryan Shea. They launched the Bitcoin layer’s mainnet in October 2018 and have developed it further ever since.
How Smart Contracts Are Expanding the Bitcoin Ecosystem
Although smart contracts on the Bitcoin mainchain aren’t fully expressive, layer-2 networks are opening up the Bitcoin network to more use cases with their smart contract functionalities.
Smart contracts that execute on Bitcoin offer:
- Improved programmability compared to the base layer: The programming capabilities of native Bitcoin smart contracts are limited. Nevertheless, smart contracts implemented on Bitcoin layers offer enhanced programmability, expanding the range of possibilities for building on the Bitcoin network.
- Scalability of the Bitcoin mainchain: Bitcoin layers help scale Bitcoin by leveraging smart contracts. They provide faster transaction speeds, making the Bitcoin ecosystem more efficient altogether.
These enhanced capabilities come without altering the original Bitcoin protocol, allowing the Bitcoin network to maintain its robust security, which is widely considered integral for long-term success.
FAQs
Does Bitcoin have smart contracts?
Yes. Bitcoin’s Script programming language supports smart contracts, although their programmability is limited. Interestingly, when you make a basic Bitcoin transaction, like sending BTC to someone else, you use a basic form of a smart contract. However, most people don’t realize that Bitcoin transactions are essentially smart contracts because the blockchain is not marketed as a smart contract platform.
Besides native Bitcoin smart contracts, Bitcoin layer-2 solutions like RSK, Stacks, and Liquid support smart contracts with greater functionalities.
What are Bitcoin layer-2 smart contracts?
Bitcoin layer-2 smart contracts are smart contracts that execute on Bitcoin layers like RSK, Stacks, Lightning, and Liquid. These layer-2 smart contracts offer more programmability capabilities compared to Bitcoin-native smart contracts. Therefore, they enable dApps to be built on top of Bitcoin, opening DeFi users to Bitcoin-secured applications. Also, Bitcoin layer-2 smart contracts like those used by Lightning help scale Bitcoin by allowing faster transaction speeds.
What are Bitcoin Lightning Network smart contracts?
Bitcoin Lightning Network smart contracts enable two parties to conduct BTC transactions off the mainchain at fast speeds and low costs through payment channels. By taking transactions off-chain, the Lightning Network takes the load off the base layer, minimizing network congestion. Smart contracts also allow parties to transact with each other without intermediaries.