Skip to main content

Minsc Basics: Learn How to Write Miniscript Contracts

· 5 min read
featured image

Minsc is a high-level scripting language expressing Bitcoin Script spending conditions, using a simple and familiar syntax. Minsc is based on Miniscript with added features on top of it.

Minsc's features

  • variables,
  • functions,
  • logical operators,
  • human-readable times,
  • arrays, ...

Minsc eliminates the complexity of dealing with Bitcoin's stack-based script language. Its syntax is much more familiar, resembling that of common programming languages, making it easier for developers to write and understand code. This is crucial because it ensures applications are built correctly and efficiently, reduces the likelihood of errors. Additionally, Minsc optimizes the code for lower spending costs and makes constructing spending conditions safer, enhancing both the economic efficiency and security of your transactions.

Pre-requisites

What can I use minsc for?

You can use minsc to generate addresses based on your defined spending conditions. It's safer and more efficient to use minsc then to write your own raw Bitcoin script as it abstracts away complexity.

Examples

Lets go over two examples showing how you can quickly and easily generate addresses using minsc. In order to test the minsc code, you can just copy/paste it into minsc IDE.

Stupid Simple Locking Script

In our our Stupid Simple Locking Script With Python article we showed how to construct a locking address that can be unlocked by solving a stupid simple mathematical equation. This is how to construct the same address using minsc:

$script = `<4> OP_ADD  <12> OP_EQUAL`;
address(wsh($script), regtest)

Link to this script on Minsc IDE

HTLC

We've wrote an article how to create a HTLC contract. Using minsc it's a lot more straightforward do define a locking policy which is also more cost efficient compared to the version of HTLC that we used in our article. Note that albeit the locking policy is the same, the address is different because the minsc HTLC version is safer (it adds OP_SIZE) and is more optimized which results in a different address.

$sender_pubkey = pk(02d1f480e3ef0ca342c41bcd5a1e33765e6891281b829bffa32c19e920ca5a446c);
$recipient_pubkey = pk(03a3ec5348a50c6b8420b5eb91eecc706c84d3e76db694ddd2fb01b41ab441937a);
$preimage = 6bd66227651d0fe5c43863d7b29a4097e31dbf51e6603eee75947bff5e96ae43;

$script = (
($recipient_pubkey && sha256($preimage)) || ($sender_pubkey && older(5 blocks))
);
address(wsh($script), regtest)

Link to this script on Minsc IDE

Multisig (2 of 3)

Lets construct a multisig (2 of 3) contract

$alice = pk(034da006f958beba78ec54443df4a3f52237253f7ae8cbdb17dccf3feaa57f3126);
$bob = pk(0310c283aac7b35b4ae6fab201d36e8322c3408331149982e16013a5bcb917081c);
$carol = pk(0392a762e0123945455b7afe675e5ab98fb1586de43e5682514b9454d6edced724);

$script = wsh(2 of [$alice, $bob, $carol]);
address($script, regtest)

Link to this script on Minsc IDE

Wrapping Up

The examples above demonstrate how to use Minsc to efficiently create addresses for your Bitcoin contracts. Minsc ensures that your contracts are both secure and efficient.

However, Minsc does not support the construction of spending transactions. To spend funds sent to an address generated with Minsc, you will need to do it manually. If you are unsure how to proceed, refer to our articles that guide you through generating an address and spending the funds using Python.