What is Miniscript? Part 1


Last updated July 20, 2020, 1:06 p.m. | Posted by Leon Johnson

andrew_poelstra_miniscript

Introduction

Bitcoin transactions are powerful constructs. When you create a bitcoin transaction your wallet states the conditions by which someone can access the bitcoin you’re sending. These spending conditions are created using bitcoin Script. Unlike traditional banking, you can create complex and interesting spending conditions so that the bitcoin you are sending can only be spent, say, after a given date, or only when 2 out of 3 signatories produce a valid signature etc. As an example, a wallet developer might write a script that looks like this


0101010293 //these are bytes

When bitcoin full nodes see this within a transaction, using bitcoin Script, they will interpret this as


01 02 OP_ADD

which simply adds two numbers together.

What is Miniscript?

Miniscript is a method of creating and analyzing bitcoin scripts in a simple and easy way.

What problem does it solve?

Bitcoin developers are unable to use Script’s full potential because creating scripts for non-basic tasks is tough.This means developers find it difficult to interpret complex spending policies, but also reliably assess the security of the script. Using Bitcoin Script, it is hard to argue that your script is correct and that it does what you want it to do (secure). Also, the developer has to know about Script’s quirks. Miniscript solves this by providing a terse language for developers to use to describe their bitcoin scripts, but also provides software that compiles this into Bitcoin Script and provides a visual method for the user to assess and reason about the spendability of the script.

Who designed it?

Pieter Wuille and Sanket Kanjalkar, both engineers at Blockstream, have been working on this since 2018 alongside Andrew Poelstra, Director of Engineering Blockstream. Andrew gave a really good talk and workshop on Miniscript at the Advancing Bitcoin Conference 2020.

How does it work?

There are two parts to Miniscript – the policy language and the software. The policy language is a programming language that allows developers to describe the conditions under which a coin can be spent. This is then compiled into a Bitcoin script. The policy language directly corresponds to bitcoin Script, and it is an alternate encoding of a subset of Bitcoin Script. This is an example of Miniscript which creates a 3-of-3 multi-sig that turns into a 2-of-3 after 90 days


thresh(3,pk(key_1),s:pk(key_2),s:pk(key_3),sdv:older(12960))

It is both engineer readable and machine analyzable, and has a type system that allows automatic verification of correctness. The Miniscript software directly encodes a spending policy into a descriptor then into a bitcoin transaction. Miniscript software creates tree-based diagrams that describe the spendability of a [complex] bitcoin script. This diagram also makes it easy to walk through and verify that the script does what you expect it to do.

Miniscript tree

It’s important to note that program optimization is outside of the scope of Miniscript as the optimized output makes it difficult to reason and understand why the compiler made those choices.


Andrew Poelstra states:
"The idea behind Miniscript is that we can take a policy [like that]. We have all these components, keys and hashes and so forth. And we just map these directly into script. We have these script templates which when executed, do the required check kind of thing. You also have the script templates that will work as ANDs or ORs. You can compose these, you can take like a template that's got a couple of gaps in them, just fill in other script components and basically [it provides] an encoding of the spending policies in bitcoin Script." - Advancing Bitcoin Conference 2020

Whilst the software displays this graphically as a tree of ands and ors or as a program that can be executed but it can also be displayed graphically as a flow chart but also short sentences similar to how Applescript works or IFTTT works.

Benefits of Miniscript

Firstly, it allows counterparties to decode complex scripts to better understand the spendability of the bitcoin locked in a transaction. This improves the user experience (developers, business owners) but also reduces the need for manual security audits of proposed scripts. Secondly, it uses templates to construct the bitcoin Script so developers are no longer 'reinventing the wheel'. Lastly, it allows for ‘composability’, so a user can visually just focus on her segment of the spending policy.

Where can I learn more?

You can watch Andrew Poelstra’s entire video presentation on Miniscript here

Thanks for reading!

If you enjoyed it, please share it on Twitter.