How to Write Smart Contracts on Bitcoin by@allanmangeni

How to Write Smart Contracts on Bitcoin

Smart Contracts are like castles of math, freely trading with each other. They play an integral role in building a trustless network where users can perform transactions and make agreements without a third party involved. The idea of smart contracts was first proposed in 1994 by Nick Szabo who later went on to invent Bit Gold -- a virtual currency, in 1998. Since then the adoption of smart contracts has evolved to the point that they have various use cases across several industries across the world. According to Investopedia, “A smart contract is a self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of code.” Bitcoin was the first product to adopt smart contracts in 2009. However, designing smart contracts on Bitcoin was often cumbersome and costly to execute. Such issues led to the rise of other Blockchains like Ethereum, Algorand, Polkadot, Solana that make it easier and cheaper to design and execute smart contracts on Bitcoin.
image
Allan Mang'eni HackerNoon profile picture

Allan Mang'eni

software craftsman

github social icontwitter social icon


Smart Contracts are like castles of math, freely trading with each other.


They play an integral role in building a trustless network where users can perform transactions and make agreements without a third party involved.


The idea of smart contracts was first proposed in 1994 by Nick Szabo who later went on to invent Bit Gold -- a virtual currency, in 1998. Since then the adoption of smart contracts has evolved to the point that they have various use cases across several industries across the world.


According to Investopedia, “A smart contract is a self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of code.”


Bitcoin was the first product to adopt smart contracts in 2009. However, designing smart contracts on Bitcoin was often cumbersome and costly to execute.


Such issues led to the rise of other Blockchains like Ethereum, Algorand, Polkadot, Solana that make it easier and cheaper to design and execute smart contracts on Bitcoin.


Despite the rise in new Blockchains that make smart contracts easily executable, there is a problem with the Scalability Trilemma.


Scalability Trilema is a scenario where for a Blockchain to be scalable, it may need to sacrifice speed, security, or Decentralization.


Many Blockchains have sacrificed decentralization and security by limiting the number of Node validators, using new Consensus Mechanisms without conducting proper research and this has led to issues like DDOS attacks and loss of funds.


Bitcoin is decentralized and secure.


It may not be as scalable as other networks but with the recent Bitcoin Improvement proposals, we may see a major shift towards Bitcoin solving the scalability trilemma without sacrificing speed, decentralization, or security and also the adoption of smart contracts on Bitcoin.

Which Tools/Networks Enable Smart Contracts on Bitcoin?

  1. Stacks


Stacks is an open-source network that runs Decentralized Applications on Bitcoin. It achieves this by using a consensus Mechanism called proof of Transfer. Proof of Transfer enables Decentralized Apps on Stacks to use Bitcoin as the settlement and security layer.


Smart Contracts on Bitcoin are enabled via Stacks using the Clarity Smart Contract Language. Clarity is a decidable language meaning users are able to know how a smart contract will behave in certain situations without executing it in those situations.


This minimizes bugs and read of uninitialized values. Clarity language is also interpreted meaning it does not need a compiler. The contract is published and executed by Blockchain nodes which limit bugs and enhance understandability. Stacks has tools like Clarinet which enable users to create and test smart contracts.


Stacks faced some criticism over its Blocktime which was slow since it produces blocks at the same rate as Bitcoin which is roughly once every 10 minutes.


Luckily, Stacks introduced Microblocks which solves the issue of Transaction confirmation and also enhances the UX for Dapps within the eco-system.


  1. Taproot


Taproot is a recent soft fork on the Bitcoin network that enhances flexibility for smart contracts on Bitcoin. It also enhances privacy by scrambling transactions and multiple signatures making it difficult to identify transaction inputs on the Bitcoin network.


Taproot was recently approved through a soft fork in which previous Bitcoin Blocks and transactions are made invalid. For Taproot, it was achievable when a majority of minors upgraded to enforce the rules.


Taproot enables smart contracts on Bitcoin using Tapscript which is a scripting language used for Taproot script-path spending.


Tapscript makes the availability of Schnorr signatures, batch validation, and signature hash.


Tapscript achieves this by modifying signature opcodes OP_CHECKSIGand OP_CHECKSIGVERIFY to verify Schnorr's signatures.


Tapscript also disables the inefficient OP_CHECKMULTISIG and OP_CHECKMULTISIGVERIFY and introduces a new opcode OP_CHECKSIGADD that allows creating of multisignature policies in a batch-verifiable way.


The signature3 opcode limit by tapscript also fixes complicated interactions with the transaction weight.


  1. BIPS (300+301)


BIPs(300+301) entails two Bitcoin improvement proposals which are dubbed as Drivechains.


Drivechains’ main goal is to create sidechains on the Bitcoin network that allows users to create, delete, send and receive BTC from sidechains, which are Layer-2 solutions.


BIP 300 proposes a hash rate escrow in which transactions are signed by the accumulation of hash power over time rather than the use of cryptographic keys. It allows the creation of new Blockchain messages which are :


  • M1- Propose new sidechains
  • M2 - ACK proposal
  • M3- Propose Bundle
  • M4- ACK Bundle
  • M5- deposit Transfer of BTC from main to side-chain
  • M6- Withdrawal is a transfer of BTC from side to the main-chain.


Nodes will also organize the messages into two caches:


  • ESCROW_DB - tracks the 256 hashrate escrows
  • WITHDRAWAL_DB - tracks coins leaving the Sidechain.


These are the current networks/tools bringing smart contracts to Bitcoin. In 2022, we may see many new projects and BIPs coming up which will be focused on Building on Bitcoin and also making Bitcoin scalable.


Previously published here.

react to story with heart
react to story with light
react to story with boat
react to story with money

Related Stories

L O A D I N G
. . . comments & more!