Subject: [bitcoin-dev] Minsc, a Miniscript-based scripting language

From: Nadav Ivgi <[email-masked]>
Date: Wed, 29 Jul 2020 18:10:42 +0300

Hi all,

I recently released Minsc, a high-level scripting language for expressing
Bitcoin Script spending conditions using a simple and familiar syntax.

Minsc is based on the Miniscript Policy language, with additional features
and syntactic sugar sprinkled on top, including variables, functions, infix
notation, human-readable times and more.

A live compiler (Minsc->Policy->Miniscript->Script) and documentation are
available on the website: https://min.sc

Source code (in Rust) is available on github:
https://github.com/shesek/minsc

Some example Minsc scripts:

- A user and a 2FA service need to sign off, but after 90 days the user
alone is enough

      pk(user_pk) && (9@pk(service_pk) || older(90 days))

- Traditional preimage-based HTLC

      $redeem = pk(A) && sha256(H);
      $refund = pk(B) && older(10);

      likely@$redeem || $refund

- Liquid-like federated pegin with emergency recovery keys

      $federation = 4 of [ pk(A), pk(B), pk(C), pk(D), pk(E) ];
      $recovery = 2 of [ pk(F), pk(G), pk(H) ];
      $timeout = older(heightwise 2 weeks);

      likely@$federation || ($timeout && $recovery)

- The BOLT #3 received HTLC policy

      fn htlc_received($revoke_pk, $local_pk, $remote_pk, $secret, $delay) {
        $success = pk($local_pk) && hash160($secret);
        $timeout = older($delay);

        pk($revoke_pk) || (pk($remote_pk) && ($success || $timeout))
      }

      htlc_received(A, B, C, H, 3 hours)

- 2FA where the user has a 2-of-2 setup and the service provider is a
3-of-4 federation

      fn two_factor($user, $provider, $delay) =
        $user && (likely@$provider || older($delay));

      $user = pk(user_desktop) && pk(user_mobile);
      $providers = [ pk(P1), pk(P2), pk(P3), pk(P4) ];

      two_factor($user, 3 of $providers, 4 months)

- Easily add NSA backdoors to everything 🕵️🚪

      _backdoor=pk(usgovt), _pk=pk, _older=older, _after=after,
      _sha256=sha256, _ripemd160=ripemd160;

      fn pk(x) = _pk(x) || _backdoor;
      fn older(x) = _older(x) || _backdoor;
      fn after(x) = _after(x) || _backdoor;
      fn sha256(x) = _sha256(x) || _backdoor;
      fn ripemd160(x) = _ripemd160(x) || _backdoor;

      (pk(A) && sha256(H)) || (pk(B) && older(10))

Feedback is appreciated!

Nadav

P.S Since every Miniscript Policy is also a valid Minsc expression, the
min.sc web code editor UI could also be useful for experimenting with bare
policies. You'll get syntax highlighting, parentheses matching, real-time
compilation (in a web worker so the browser doesn't freeze) and syntax
error reporting.