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:
21 <key A, 33 bytes> ac 73 64 76 a9 14 <hash of key B, 20 bytes> 88 ad 03 74cd00 b2 68A 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.
The policy Liana writes
Liana first writes the keys and waits you set as a policy, the input of the Miniscript compiler:
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:
or_d(pk(A),and_v(v:pkh(B),older(52596)))Fragment by fragment:
pk(A): KeyAsigns.pkh(B): KeyBsigns. The script holds only a 20-byte hash ofB. The spender revealsBitself, which suits a key that rarely signs.v:: Stops the whole script ifpkh(B)fails, instead of returning false.older(52596): The coin is at least 52,596 blocks old.and_v(X,Y):X, thenY, both required.or_d(X,Z):X, or elseZ. The script runsZonly whenXreturns 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:
<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_ENDIFTwo 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_CHECKSIGleaves 1, whichOP_IFDUPcopies.OP_NOTIFconsumes the copy and skips the recovery block. The script ends on 1. - Recovery path: A signature from
B, the keyB, then an empty item in place ofA's signature.OP_CHECKSIGleaves 0 without failing, soOP_NOTIFruns the recovery block.
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,…)andmulti_a(k,…):kof the listed keys sign, in SegWit (P2WSH) and Taproot (P2TR) respectively.thresh(k,…):kof the listed conditions hold.or_i(X,Z):XorZ, as chosen by a 1 or a 0 in the witness.a:: Sets the running count aside while its fragment runs, so thatthreshcan 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:
lianadcomputes 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,
lianadassembles 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.
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.