Ivy for Bitcoin: a smart contract language that compiles to Bitcoin Script

Today, we’re delighted to release an open-source compiler and developer environment for writing Bitcoin smart contracts using Ivy, a smart contract language developed at Chain. Ivy helps you write custom, SegWit-compatibleBitcoin addresses that enforce arbitrary combinations of conditions supported by the Bitcoin protocol, including signature checks, hash commitments, and timelocks.

Ivy is designed to make it easy to manipulate value the way other programming languages let you manipulate data. You can try out Ivy using the Ivy Playground for Bitcoin, and read more about it in the docs. (Please note that Ivy is relatively untested prototype software and should be used for educational and research purposes only.)

How Bitcoin smart contracts work

If you’re interested in blockchain networks, you’ve probably heard of “smart contracts” — programs that can control on-chain resources using custom logic executed as part of blockchain validation. This concept is usually associated with Ethereum, which supports stateful, Turing-complete smart contracts. But did you know that you can also write smart contracts for the Bitcoin network?

Bitcoin allows you to write such contracts using a low-level programming language, Bitcoin Script. Every Bitcoin address corresponds to a Bitcoin Script program.

When you send bitcoins to an address, you are essentially locking them in a safety deposit box on the blockchain. These boxes are typically referred to as “unspent transaction outputs,” but we’ll refer to them as “contracts.” Each contract has an address, which corresponds to a program that must be satisfied in order to unlock and spend the bitcoins.

Bitcoin Script is the low-level “assembly language” used to write custom addresses in the Bitcoin protocol. Programs in Bitcoin Script look like this:

A Bitcoin Script program from BIP 112, by BtcDrak, Mark Friedenbach, and Eric Lombrozo

Bitcoin’s “virtual machine” — the piece of the protocol responsible for executing Bitcoin Script programs — is far more limited than the virtual machines used in other smart contract platforms, such as Ethereum or the Chain Protocol, and its instruction set is not even Turing-complete. But Bitcoin Script does provide a set of useful primitives — signature checks, hash computations, and absolute and relative timelocks — and the freedom to combine those primitives.

Right now, Bitcoin Script is used by relatively few people. Bitcoin wallets, exchanges, and payment platforms sometimes use Bitcoin Script to create security solutions, like multisignature wallets. Protocol developers have used Bitcoin Script to design interesting solutions for scaling, privacy, and interoperability, such as Interledger and the Lightning Network. But Bitcoin Script development is considered somewhat esoteric.

One reason for this is the limitations of the underlying virtual machine, but another is the relative difficulty of reading and writing Bitcoin Script programs, and of creating and using addresses from those programs.

Introducing Ivy

The same Bitcoin Script program as above, written as a contract template in Ivy

Ivy is a higher-level language that supports compilation to Bitcoin Script. Ivy programs look like this (explained here):

Ivy is a language for writing contract templates. Each contract template, when instantiated with specific parameters, can compile to a Bitcoin address (specifically, a Segregated Witness-compatible address).

To try out Ivy, you can use the Ivy Playground for Bitcoin, which allows you to design, create, and spend simulated Bitcoin contracts. The playground includes preloaded templates demonstrating a small fraction of possible contracts. The docs explain those examples and describe how you can use the playground to write and test your own contracts.

We hope that Ivy makes it easier to learn about Bitcoin, smart contracts, and programmable value. We can’t wait to see what you do with it.

Leave a Reply

Your email address will not be published. Required fields are marked *