LearnUnder the hood

Miniscript

Why Liana writes every script in a structured subset of Bitcoin Script, and what that asks of your signing device.

Every coin in a Liana wallet sits behind a Bitcoin script with at least two ways to spend it. Liana has to understand that script before you sign.

What Script cannot tell you

Take the smallest SegWit (P2WSH) Liana wallet. Key A can spend a coin at any time. Key B can spend it once a timelock of 52,596 blocks (about 1 year) has run since the coin confirmed. Behind each address sits a script, a short program every node runs at spend time:

The script behind one coin, byte by byte
21 <key A, 33 bytes> ac 73 64 76 a9 14 <hash of key B, 20 bytes> 88 ad 03 74cd00 b2 68

A node only checks whether a given spend satisfies these 68 bytes. A wallet has to answer other questions before any spend exists:

  • Which keys and which waits can spend the coin? Is there any other way in?
  • What data must a spender supply, and in what order?
  • How large can a spend get? What fee does it pay?

BIP 379 calls answering them for an arbitrary script "highly nontrivial". Miniscript is how Liana answers them.

Miniscript gives Script a grammar

Miniscript, specified in BIP 379, writes a subset of Bitcoin Script as a tree of fragments instead of a flat row of opcodes. Each fragment is a fixed piece of Script. older(52596), for example, is always 52596 OP_CHECKSEQUENCEVERIFY.

Typing rules decide which fragments can nest inside which. From a tree that passes them, software reads the spending conditions, the data each one needs, and the size of the largest spend. It can also check that nobody without a key can turn a valid spend into another valid one.

Miniscript changes nothing on the Bitcoin network. It encodes to ordinary Script, which nodes run without knowing Miniscript exists. In a Liana wallet, the Miniscript sits inside the descriptor, in wsh(...) or tr(...).

One wallet, from policy to Script

Liana takes that wallet through three forms. The network only sees the last.

One Liana wallet in four stages: your key and wait choices, the policy Liana writes with odds 99@ and 1@, the compiler's Miniscript inside your wsh() descriptor, and the 68 bytes of Script that the network sees.

The policy Liana writes

Liana first writes the keys and waits you set as a policy, the input of the Miniscript compiler:

The policy, keys shortened
or(99@pk(A),1@and(pk(B),older(52596)))

The 99@ and 1@ are odds. Liana tells the compiler to expect the primary path on 99 spends out of 100. Among the correct scripts it tries, the compiler picks the one that costs least to spend with those odds.

The Miniscript the compiler returns

Liana wraps the result in wsh(...) to make your descriptor:

The Miniscript, keys shortened
or_d(pk(A),and_v(v:pkh(B),older(52596)))
The Miniscript or_d(pk(A),and_v(v:pkh(B),older(52596))) drawn as a tree: or_d at the root, pk(A) on the primary path, and and_v over v:pkh(B) and older(52596) on the recovery path.

Fragment by fragment:

  • pk(A): Key A signs.
  • pkh(B): Key B signs. The script holds only a 20-byte hash of B. The spender reveals B itself, which suits a key that rarely signs.
  • v:: Stops the whole script if pkh(B) fails, instead of returning false.
  • older(52596): The coin is at least 52,596 blocks old.
  • and_v(X,Y): X, then Y, both required.
  • or_d(X,Z): X, or else Z. The script runs Z only when X returns false.

Compiling the same policy again may not give the same Miniscript, hence not the same addresses. That is one reason you back up the descriptor, which records the Miniscript itself.

For a Taproot (P2TR) wallet, Liana compiles the same policy to tr(A,and_v(v:pk(B),older(52596))), with A as the Taproot internal key. The Script types page compares the two.

The Script the network runs

Encoded with the table in BIP 379, the fragments give back the 68 bytes above:

The same script, opcode by opcode
<A> OP_CHECKSIG                                   # pk(A)
OP_IFDUP OP_NOTIF                                 # or_d: enter only if A failed
  OP_DUP OP_HASH160 <HASH160(B)> OP_EQUALVERIFY   # pkh(B)
  OP_CHECKSIGVERIFY                               # v: CHECKSIG becomes CHECKSIGVERIFY
  52596 OP_CHECKSEQUENCEVERIFY                    # older(52596), stored as 74cd00
OP_ENDIF

Two witnesses for two paths

The witness is the data a spender supplies to the script. In P2WSH, the script itself comes last. The Miniscript tells Liana what comes before it:

  • Primary path: A signature from A. OP_CHECKSIG leaves 1, which OP_IFDUP copies. OP_NOTIF consumes the copy and skips the recovery block. The script ends on 1.
  • Recovery path: A signature from B, the key B, then an empty item in place of A's signature. OP_CHECKSIG leaves 0 without failing, so OP_NOTIF runs the recovery block.
The same P2WSH script run on two witnesses side by side, with the stack after each opcode: the primary path ends on 1 after skipping the recovery block, the recovery path enters it and ends on 52596.

That block checks B against its hash, then B's signature, then the wait. OP_CHECKSEQUENCEVERIFY checks the wait, as the Timelocks page explains. The 52596 then left on the stack ends the script on a true value. Every node enforces both paths, with or without Liana.

The fragments you meet in Liana descriptors

A Liana policy holds only keys, thresholds and relative timelocks, so its Miniscript never contains a hash lock or after(). Larger policies bring in a few more fragments:

  • multi(k,…) and multi_a(k,…): k of the listed keys sign, in SegWit (P2WSH) and Taproot (P2TR) respectively.
  • thresh(k,…): k of the listed conditions hold.
  • or_i(X,Z): X or Z, as chosen by a 1 or a 0 in the witness.
  • a:: Sets the running count aside while its fragment runs, so that thresh can add results up.

What Liana does with Miniscript

Liana reasons about your script through the rust-miniscript library:

  • Create the descriptor: Liana compiles your policy, and refuses one whose script would be malleable or break a limit of Bitcoin Script.
  • Read a descriptor back: Liana extracts each spending path, with its keys, threshold and timelock, and shows the paths in words.
  • Price a spend: lianad computes the largest witness each input can need, and sizes the fee from it before anyone signs.
  • Follow the signatures: Liana counts how many signatures each path of a PSBT still needs.
  • Finish the transaction: Before it broadcasts, lianad assembles each witness from the collected signatures, in the order the script expects. With Liana Connect, its server prices and finishes the transaction instead.

What Miniscript requires from the signing device

A signing device must parse the Miniscript to check the policy, the addresses and the change, so Liana requires devices that support it. The Descriptor registration page explains these checks.

A computer running Liana, untrusted under BIP 388, sits above a trust boundary and only carries a descriptor, an address index and a PSBT down to the signing device. The device shows you the policy, derives the address and checks the change and fee on its own screen, then sends a signature back.

The Signing devices page shows which devices support Miniscript, for SegWit (P2WSH) and for Taproot (P2TR).

Next steps

Choose a policy sets the keys and waits that Liana compiles into your Miniscript.