Smart contracts play a crucial role in the blockchain world because they help people transact with each other without intermediaries. In this guide, we will explore Bitcoin smart contracts, focusing on how they work and the different types that exist.
Smart contracts are pieces of code that self-execute when parties involved fulfill certain pre-defined agreement conditions. Developers deploy smart contracts on a blockchain, where they operate without traditional intermediaries.
Smart contracts are typically used in blockchain-powered financial activities like asset swaps, token bridging, lending and borrowing, and assigning and reassigning NFT ownership.
Computer scientist Nick Szabo introduced the term “smart contract” in a 1994 paper. He defined it as a computerized protocol that executes contractual terms. Szabo also proposed different areas that could benefit from smart contracts, such as synthetic assets and so-called “smart property.” Today, blockchain technology enables these two use cases.
For example, synthetic blockchain-based assets are generated using smart contracts. Smart properties are created by ‘embedding’ smart contracts in digital or physical assets, such as decentralized physical infrastructure network (DePIN) projects. These contracts can assign ownership to the rightful owner and reassign it when ownership is transferred.
Although Bitcoin isn’t generally known as a smart-contract blockchain, it does, in fact, have smart contracts. However, its smart-contract functionality is basic due to the intentional Turing incompleteness of the Bitcoin Script.
Turing incompleteness means that Bitcoin’s Script language can only perform limited computations. It also means that Bitcoin cannot natively support the highly expressive smart contracts needed to power decentralized applications (dapps).
Bitcoin creator Satoshi Nakamoto intentionally created the Script programming language this way to minimize programming errors and prevent Denial of Service (DoS) attacks.
History has shown that smart contracts aren’t foolproof and can be exploited. For example, a Decentralized Autonomous Organization (DAO) on Ethereum was exploited for $70 million in ETH in 2016 due to a smart contract flaw. This event resulted in a hard fork creating two blockchains—Ethereum and Ethereum Classic—where the latter is the original Ethereum chain.
So, Bitcoin’s limited smart contract functionality helps make the Bitcoin blockchain more secure.
Bitcoin supports several smart contracts using the Script computing language. This language leverages a lock-and-key mechanism to execute smart contracts.
When a user wants to send BTC to someone, Script allows them to set the criteria for this transaction. The criteria act as a lock known as the ScriptPubKey. The recipient satisfies the set criteria by providing a matching key. This key is called ScriptSig or Script Witness (for SegWit transactions).
For example, a common ScriptPubKey criterion is "The funds can be spent by providing a valid digital signature from the private key corresponding to this Bitcoin address." In this case, the ScriptSig is the digital signature created by the recipient's private key, which unlocks the funds.
Smart-contract functionality can be built into any Bitcoin transaction using Script. However, the commands and functions of the Script language have limited programmability. These commands and functions are called operation codes or opcodes for short. They can be upgraded through the Bitcoin Improvement Proposal (BIP) process.
Bitcoin smart contracts are present in both Layers 1 and 2. Here’s a breakdown of the different smart contracts in each layer.
Bitcoin natively supports the following scripts:
Pay-to-Public-Key-Hash (P2PKH) is the most common Bitcoin transaction. It allows users to send BTC to the recipient’s address. These funds can only be accessed by the person holding the corresponding private key. In other words, the recipient must provide a signature that pairs with the public key whose hash is defined by the script. P2PKH is similar to locking a door and giving the key to a person.
Unlike P2PKH, which requires one signature for a transaction to be valid, multi-signature or multisig scripts require more than one signature. That means BTC locked in a multisig script can only be spent when m signatures matching one of the n public keys are given. This multisig design is known as m-of-n.
For instance, a popular multisig setup is 2-of-3, where three people can hold money as a group and provide two signatures to spend their funds.
Hashed Timelock Contracts (HTLCs) are Bitcoin transactions that are only valid for a specified period. They were implemented through BIP 65 and 112, which introduced new opcodes. HTLCs are suitable for financial agreements, such as modifying the BTC spending requirements.
For example, an HTLC may state that three signatures are needed to spend BTC for a given duration; after that, only one signature is needed.
Pay-to-Script-Hash (P2SH) is a smart contract that allows BTC to be sent to the hash of any script. It doesn’t disclose the details of a script until BTC is spent, providing a certain level of privacy to users. Additionally, P2SH reduces the cost of sending BTC to complex smart contracts. To spend the coins locked in this script, a user must submit the script corresponding to the hash and meet the set conditions.
P2SH was introduced via BIP 16. It is similar to Pay-to-Witness-Script-Hash (P2WSH), a script used in SegWit transactions.
DLCs are smart contracts that use oracles to facilitate peer-to-peer (P2P) transactions. These oracles retrieve the outcomes of real-world events and communicate those details on-chain for the smart contract to self-execute according to the preset conditions.
DLCs are utilized when two parties enter a financial agreement, for example, a bet based on the outcome of a future event like a sports game or a bitcoin price threshold.
Off-chain scaling protocols enhance Bitcoin’s smart-contract functionality without requiring any Layer 1 (L1) upgrades. They therefore help Bitcoin retain its robust security while allowing its users to enjoy more use cases thanks to expressive smart contracts.
Below are some examples of Bitcoin layers with smart contracts.
Stacks is a layer built on Bitcoin to improve utility with smart contract-powered dapps. It creates and deploys smart contracts using the Clarity programming language, which is non-Turing complete but expressive. Clarity is also a decidable language, allowing developers to predictably analyze and verify the behavior of smart contracts written in Clarity. This characteristic is beneficial for security and correctness guarantees.
The Lightning Network is a Layer 2 scaling solution operating on Bitcoin. It uses off-chain state channels to facilitate near-instant and low-cost micropayments using HTLCs. These smart contracts enable the trustless routing of payments between parties while ensuring that every party that assists in the routing process is paid a small fee. Lightning also uses discrete log contracts.
Rootstock is a sidechain that scales Bitcoin using smart contracts compatible with the Ethereum Virtual Machine (EVM). That means Rootstock smart contracts can be written in Ethereum’s Solidity language, which is Turing-complete. It also means developers can migrate existing smart contracts and dapps from Ethereum to Rootstock.
Rootstock’s improved smart-contract functionality enables projects to build a variety of dapps, such as the Sovryn DeFi platform, giving Bitcoin more utility.
Sovryn’s BitcoinOS is an upcoming superchain of interoperable rollups running on top of Bitcoin. It is scheduled to launch in Q4 2024 with an EVM-compatible execution environment where developers can create Turing-complete smart contracts. As a result, BitcoinOs will scale Bitcoin through improved programmability and added utility. The use of Bitcoin rollups will also boost transaction throughputs.
After the launch, Sovryn’s existing DeFi products, such as its lending and borrowing protocol, will be offered on the BitcoinOS network.
Now, let’s take a look at how complex smart contracts can enhance the Bitcoin ecosystem.
Better smart-contract capabilities can bring more innovation to the Bitcoin ecosystem as developers experiment with new ideas. Since Bitcoin offers several unique features, such as high degrees of stability and security, developers should be interested in using them to give users the most secure products in the blockchain industry.
Bitcoin doesn’t have a large DeFi market like Ethereum because of its limited smart-contract functionality. However, Bitcoin’s DeFi market is growing thanks to smart-contract Layer 2 networks. More DeFi projects will likely continue building DeFi dapps on these layers as they seek to tap Bitcoin’s reputable security, decentralization, and stability properties.
Better smart contracts result in the development of dapps, which in turn attract more users. As building on Bitcoin continues to expand, more and more users are likely to come to the ecosystem to interact with the different products available. Bitcoin layers that increase transaction efficiency, integrate dapp interoperability, and enable the easy transfer of assets from L1 to L2 will likely attract the most users.
Buy SOV to participate in Sovryn’s Bitocracy and have your say in the new era of Bitcoin.
Bitcoin smart contracts are less expressive than Ethereum smart contracts because the script language is limited in programmability. This helps lower programming errors and prevent DoS attacks. As a result, the Bitcoin network is highly secure, but it cannot support native dapps. To solve this problem, various scaling solutions like BitcoinOS are being built on Bitcoin to improve programmability.
Bitcoin smart contracts are written and deployed in Script, a non-Turing complete programming language. The commands and functions of Script, called opcodes, have limited programmability. For this reason, it’s difficult for developers to build intricate native dapps on Bitcoin. A Turing-complete programming language can solve any computational problem given sufficient resources.
Bitcoin’s creator, Satoshi Nakamoto, designed Script as a simple programming language to create a highly secure monetary network. Because Bitcoin was intended to be a base-layer monetary network, he prioritized security over programmability. This decision has made Bitcoin more secure than Ethereum but limited in smart-contract capabilities. As a result, developers cannot build complex native dapps on Bitcoin.