What Are Bitcoin Smart Contracts?
Table of Contents
- A smart contract is a digital agreement which is automatically executed and based on predefined criteria.
- Bitcoin’s scripting language enables a variety of smart contracts.
- Layers such as the Lightning Network and sidechains can open more possibilities for smart contracting on Bitcoin.
- The Taproot upgrade will expand the flexibility, efficiency, and utility of smart contracting features for Bitcoin.
What Is a Bitcoin Smart Contract?
A smart contract is a digital agreement which is automatically executed and based on predefined criteria. For example, a smart contract might specify that bitcoin should be automatically sent from one user to another after a certain time delay.
Smart contracts can be extremely complex and include multiple conditional criteria, or they can be as simple as requiring a digital signature to spend money.
How Bitcoin Does Smart Contracts
The Bitcoin network supports a wide range of smart contracts using its powerful scripting language, called Script. Script allows users to establish criteria for their bitcoin to be spent, and Bitcoin transactions lock specific amounts of bitcoin to these scripts. A user must satisfy these criteria in order to spend the bitcoin locked to the script. In this way, all Bitcoin transactions are smart contracts.
The spending criteria is called the scriptPubKey or locking script, and the data and script that satisfies the criteria is called the ScriptSig or ScriptWitness, depending on whether the input uses SegWit or not.
Bitcoin Script and Turing Completeness
Script has proven useful for powering the Bitcoin network for over a decade, but it is not Turing Complete, meaning it does not allow for logical loops. This feature keeps the Bitcoin network safe from Denial of Service (DoS) attacks, which have plagued other cryptocurrency networks.
Types of Bitcoin Smart Contracts
Bitcoin’s most popular script type is Pay-to-Public-Key-Hash (P2PKH). P2PKH scripts allow bitcoin to be sent to a Bitcoin address, such that only the owner of the corresponding private key can spend the bitcoin.
On a technical level, P2PKH scripts establish the specific requirement that, in order to spend bitcoin, a user must provide an ECDSA signature that matches the public key whose hash is specified by the script. Since a valid signature can only be created by the owner of the private key corresponding to the public key hash, the bitcoin belong exclusively to the owner of the private key.
Advanced Bitcoin Smart Contracts
Pay-to-Public-Key-Hash is one of the simpler Bitcoin smart contracts, but its utility and simplicity make it the most popular. More complex smart contracts are also possible using Bitcoin Script, and infinitely many are possible on additional layers.
While P2PKH scripts only require a single signature, multisig scripts can require any number of signatures, optionally belonging to any number of users. Multisignature scripts work as follows. A list of n public keys and a number m, which is less than or equal to n, are specified. The bitcoin locked to this script can only be spent if m signatures are provided, each of which correspond to one of the n public keys listed. This design is called m-of-n multisig.
One common multisig setup is 2-of-3, which requires two signatures from a group of three public keys. This allows three parties to hold money cooperatively, while ensuring that no single party can steal the funds or stop the majority from spending them as they wish. 2-of-3 multisig is used by decentralized services like Bisq and Hodl Hodl for trust-minimized escrow.
Time Locked Bitcoin Transactions
Bitcoin transactions can be time locked, meaning they are only valid after a certain time. Time locks can also be used as part of the locking scripts to change the spending requirements of a bitcoin. For example, a script could require 3 signatures to spend the bitcoin before a certain time, after which only 1 signature is required. This makes fallback options possible, ideally preventing a loss of funds.
Arbitrarily complex scripts have been made possible by the Pay-to-Script-Hash (P2SH) standard, which was extended to include P2WSH as part of the SegWit upgrade. P2SH and P2WSH smart contracts allow bitcoin to be sent to the hash of any script, including any of the above examples. This design minimizes the cost of sending bitcoin to a complex smart contract and maintains greater privacy until the bitcoin is spent.
Taproot and Bitcoin Smart Contracts
Bitcoin’s Taproot upgrade will introduce a new script type called Pay-to-Taproot (P2TR), which will unite the functionality of P2PKH and P2SH scripts, allowing bitcoin to be sent to both a public key and arbitrary scripts. However, while P2SH and P2WSH allowed bitcoin to be sent to a single script, P2TR uses Merkelized Alternative Script Trees (MAST) to allow bitcoin to be sent to up to 2^128 different, arbitrary scripts. Any one of these scripts can be satisfied to spend the bitcoin.
Bitcoin’s Taproot upgrade gives Bitcoin users significant flexibility in constructing complex smart contracts on the bitcoin chain. It also enables efficiency and privacy gains for Lightning Channels, which are themselves a type of smart contract.
Smart Contract Layers on top of Bitcoin
All of the smart contracts mentioned above are executed on Bitcoin’s blockchain as regular Bitcoin transactions. However, bitcoin can also be spent and used to power smart contracts on additional layers, such as the Lighting Network.
The Lightning Network
The Lightning Network relies on multisig transactions called Hashed Time-Locked Contracts (HTLCs) to enable instant and nearly free Bitcoin payments. HTLCs allow bitcoin payments to be routed between different parties in a trustless manner, while guaranteeing that each user who assists in routing the payment is paid a small fee.
Additional Bitcoin Layers
The Lighting Network is only one of many protocols that allow bitcoin to be transferred off-chain. Others, such as the Liquid Network, side chains, and state chains, also rely on Bitcoin’s smart contracting ability to enable even more use cases. As Bitcoin development continues, we will see a growing number of ways to use bitcoin, the asset, on networks other than the Bitcoin blockchain network. However, all of these solutions will remain anchored to the security of Bitcoin’s blockchain, and most will continue to rely heavily on Bitcoin’s powerful scripting language.