Have you heard or read about Bitcoin Miniscript and wondered what it’s all about? Have you already tried reading a few guides and still don’t understand what the hell they’re talking about? Us too. That’s why we’ve written this super simple guide to explain everything, step-by-step, in simple terms that everyone should be able to understand.
Contents
What Is Bitcoin Script?
Before we talk about Miniscript we need to first talk about what Bitcoin Script is. Bitcoin Script, in the most simplest terms, is a programming language that’s built into Bitcoin and has been from day one. It allows users to add conditions to an output (commonly called a UTXO) that must be met before a transaction is allowed.
For example, someone might use Bitcoin Script to set a time lock on some of their bitcoin. This would mean that even if you have the Private Keys and could usually spend those funds, the Script would prevent you from doing so until a certain time has passed, say 100 blocks or roughly 16.7 hours.
You can think of Bitcoin Script like a set of conditional statements that are applied to the funds stored on the blockchain. “Only allow spending of the funds IF condition X is met”. Such scripts allow simple or complex forms of custom logic for transactions like Multisig Wallets or the Lightning Network.
To hopefully make this a bit clearer, let’s look at a “simple” Script template example:
<A> OP_CHECKSIG OP_IFDUP OP_NOTIF OP_DUP OP_HASH160 <hash160(B)> OP_EQUALVERIFY OP_CHECKSIGVERIFY <1024> OP_CSV OP_ENDIF
Yeah. As you can see, it looks like someone just barfed up a bunch of random letters everywhere and is super confusing, even to us! This is one of the main reasons why Miniscript was created!
What Is Bitcoin Miniscript?
Bitcoin Miniscript is another, separate and simpler programming language that represents Bitcoin Scripts. For example, the above “simple” Script template can be converted to Miniscript notation as:
or_d(c:pk(A),and_v(vc:pk_h(B),older(1024)))
While this might still appear a bit complicated or foreign to those who don’t program very often, it reads much better. Just by looking at it and following the bracketed parts we can see that there is an OR section and then a second AND section inside that. Inside that there’s an OLDER section with something about 1024.
In reality this is a Miniscript with spending conditions that allow the output to be spent by providing a signature that matches the public key A (the pk(A) part) OR by providing a signature that matches the other public key B, so longer as it’s been more than 1024 blocks since the original transaction was made.
This bitcoin scripting style is commonly used to give redundancy with private keys. For example if you lose your private key A, you can still spend the funds using private key B so long as you wait 1024 blocks or about 1 week.
Miniscript Uses
As you can see, Miniscript abstracts away a lot of the complexities of Bitcoin Script and offers a more streamlined and simpler way for developers to write useful, clear and secure scripts. It’s extremely important to be able to have an easily written policy language as otherwise software developers will make mistakes, which could cost millions depending on how much the script is implemented on.
While Script offers a much lower level, raw power for creating bitcoin spending conditions, it’s confusing, not efficient and can cause mistakes to be made. Miniscript in contrast is higher-level, more streamlined and just generally clearer to read and write, making it easier to verify expressions and know what the script execution will do.
Beyond these benefits, Miniscript is also the most economical script which results in fewer bytes being written to the Bitcoin Blockchain. As there’s less data, the Bitcoin transactions are smaller and so the transaction fees are also lower.
New to Athena Alpha? Start today!
Complex Spending Conditions
You can also use Miniscript to create complex spending conditions such as inheritance time locks, dead man switches and proof of reserves, which is where someone can prove they can spend some funds without actually spending them.
Just like with other software programming languages, seemingly simple fragments such as AND or OR can be combined and built upon to create everything from basic types of logic to a whole host of intricate and personalized spending conditions.
Even simple policy fragments, such as thresh(k, pol_1,…, pol_n), can allow you to build an output that can only be spent if “k” of the specified policies are met. When combined with the sheer amount of economic power that can be contained in a single Bitcoin transaction (millions or even billions), it’s amazing to imagine what complex spending policies scenarios might be possible.
An Optimized Script For Bitcoin Transactions
An important point about Miniscript is that it’s not meant to replace Bitcoin Script. Instead it’s simply meant to be a more user friendly interface that helps developers to write the most secure and compatible scripts possible.
A big part of this help is the Miniscript compiler, which automatically and instantly finds the most optimal code given a certain spending policy. While everyone has great ideas, there’s a high probability that someone else has already thought of it and implemented it.
The Bitcoin scripts provided by the compiler ensure that the script is built in the correct, structured way while at the same time increasing adoption and usage. This way a developer can start fresh, get a perfect, secure result instantly and be compatible with everyone else all at once!
Wallet Interoperability & Automation
This compatibility is another extremely important part of what Miniscript brings to the table. If you go to our Crypto Wallet Comparison page, you can click on the Filters button in the top left. In the Features area you’ll notice “Miniscript” as something you can filter by.
This is because both hardware and software wallet developers can choose to build in support for Miniscript, allowing interoperable script policies between the various different wallets that are out there.
This means that people using completely different wallet software can all still come together and help sign the same script, providing all their wallets support Miniscript of course.
Miniscript also allows that same software to look at any script and figure out what witness data needs to be built to spend bitcoins protected by that script. Again this automation speeds things up, makes life easier for everyone and helps the entire Bitcoin system be faster and more secure.
Miniscript Policies And Fragments
Finally, for those interested, let’s go a bit deeper into what policies and fragments are. While Miniscript compiles down to Bitcoin Script, there’s actually a third step we skipped over.
Miniscript is written using the Miniscript Policy language. This language, like other programming languages, is made up of specific coding fragments such as “AND” or “OR” that were in the example we went through before. What we skipped over is that these policy fragments are first compiled into Miniscript and then finally compiled into Bitcoin Script.
This probably all sounds confusing again, so let’s do another simple example. Let’s say we want an output to only be spent if someone can provide a signature that matches two public keys. First we build up our Miniscript Policy language using two fragments, “and_b” and then two “pk()”:
and_b(pk(key1),pk(key2))
This Miniscript Policy is compiled down into Miniscript as:
and_b(pk(key_1),s:pk(key_2))
This Miniscript is then finally compiled down into Bitcoin Script as:
<key_1> OP_CHECKSIG OP_SWAP <key_2> OP_CHECKSIG OP_BOOLAND
These steps are important to know as the policy language (the 1st part) isn’t always the same as the Miniscript result (the 2nd part) as you can see above (note the “key1” vs “key_1” parts).
There’s a ton more to learn about Miniscript as it’s literally an entire programming language just like C++ or Javascript, so we’ll end this for now. However one of the best resources out there that covers all the semantics, Miniscript fragments, stack elements, Miniscript form, key expressions, wrapped expressions, resource limitations and many other Miniscript functions is this page here from Blockstream Research.
FAQ
What Is A Bitcoin Opcode?
Opcodes or operational codes set transaction conditions on the Bitcoin protocol. Each code has a specific, defined rule for it and when combined, makes up the Bitcoin Script language. For example the OP_CHECKSIG code checks the entire transaction to make sure the signatures used are valid.
What Are Miniscript Policies?
A Miniscript Policy is written using the Miniscript Policy language. It outlines what spending conditions are required to spend a certain Bitcoin output. This policy is then compiled down into Miniscript which is then itself compiled down into Bitcoin Script.