What Are Bitcoin Smart Contracts?

6 min read

What Is a Bitcoin Smart Contract?

A smart contract refers to a digital agreement that is self-executing based on predefined conditions. Bitcoin smart contracts operate on the Bitcoin blockchain, automatically executing transactions when the predefined conditions are met, without the need for intermediaries. 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.

Learn more about Bitcoin transactions

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.

History of Bitcoin Smart Contracts

Initially, Bitcoin was primarily perceived as a peer-to-peer electronic cash system. Its scripting capabilities were seen as a secondary feature. However, as the community realized the potential of Bitcoin’s scripting language, it led to the exploration and development of various types of smart contracts, from simple multi-signature setups to more elaborate scripts enabling conditional transactions.

An important moment was the introduction of Pay-to-Script-Hash (P2SH) in 2012, which significantly enhanced Bitcoin’s smart contract flexibility. It allowed transactions to be made to scripts whose conditions are revealed only when spending. The Taproot upgrade, activated in November 2021, marked another milestone by introducing Schnorr signatures and Merkelized Abstract Syntax Trees (MAST). Taproot further expanded the potential for privacy, efficiency, and complexity in Bitcoin smart contracts.

These developments reflect Bitcoin’s adaptive nature and the community’s commitment to exploring the balance of innovation with security and scalability.

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.

Multi-Signature Scripts

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.

Pay-to-Script-Hash (P2SH)

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.

Learn more about the benefits of Taproot.

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.

Learn more about the Lightning Network.

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.

Key Takeaways

  • 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.