Source code for bip32_descriptors.descriptors.miniscript

# Copyright (c) The btclib developers
# Distributed under the MIT software license, see the accompanying
# LICENSE file or https://opensource.org/license/mit for the full text.

"""Miniscript: the expression, its type, its script, both ways, BIP379.

A miniscript is a bitcoin script written as a tree of *fragments*, which
is what makes a non-trivial spending condition something a wallet can
read rather than something it has to recognize. `parse` reads the text
BIP379 defines and `from_script` reads a script back into it; `str` and
`Miniscript.script` are the two ways out. The round trip is the point:
`from_script(node.script())` is `node` again, so a signer handed a witness
script can say what it means without being told. `reads_back` is that
round trip asked of a script instead of assumed of it -- whether the bytes
in hand are the expression they look like -- which is the question a
caller has about a script somebody else wrote.

`satisfy` is the third thing it does: the witness that spends the script,
which for a miniscript is a choice and not an assembly -- several branches
may be open at once, and which one to take is what BIP379's non-malleable
satisfaction algorithm decides. It reads the signatures a caller has and a
`SpendContext`: the hash preimages, and the lock times the transaction
being built will carry, because an ``older()`` or an ``after()`` is a
branch only the right transaction opens. Non-malleable or refused, which
is Bitcoin Core's default too: a witness a third party could rewrite is
worse than none.

The bounds are the same analysis read statically: `max_ops`,
`max_stack_items`, `max_exec_stack_items` and `max_witness_size` answer
what a spend may cost before there is a spend, and `is_sane()` is the
conjunction Bitcoin Core requires of a miniscript before it accepts a
descriptor holding one. `max_witness_stack` is the last of those broken
into its elements, which is what an estimator needs: the largest witness
this can be satisfied by, element by element, with every signature assumed
and every lock time taken as met. Its bytes and `max_witness_size` are the
same number by two roads -- one over the type tables, one over the
satisfaction -- and the test that they agree on every vector is what says
neither transcription drifted.

The type system is why a fragment can be trusted to compose. Every
expression has one of four basic types -- "B" base, "V" verify, "K" key,
"W" wrapped -- and a set of properties saying how it consumes the stack,
whether it can be dissatisfied, and whether a third party can rewrite a
witness for it. `Miniscript.properties` is that set, one character per
BIP379 property, and an expression whose properties are empty is one the
rules refuse: `parse` refuses it too, naming the innermost fragment that
failed, because "which fragment" is the answer a caller wants.

Two contexts, `P2WSH` and `TAPSCRIPT`, because BIP379 has two: the
fragments are the same, but ``multi()`` belongs to the first and
``multi_a()`` to the second, a key is 33 bytes there and 32 here, the
``d:`` wrapper is "u" only under tapscript -- MINIMALIF being consensus
for taproot and policy for P2WSH -- and each context bounds its own
resources. The context is therefore a parameter of everything, and
`descriptors` passes the one the position gives: `P2WSH` inside ``wsh()``,
`TAPSCRIPT` for a leaf of a ``tr()`` script tree.

A hash fragment also reads ``preimage(HEX)`` in place of its digest, as
in ``sha256(preimage(HEX))``: the private form, as a WIF or an xprv is a
key's. No BIP specifies it and Bitcoin Core does not read it. It is
proposed on Delving Bitcoin, in "Descriptor syntax for hash preimages"
(https://delvingbitcoin.org/t/descriptor-syntax-for-hash-preimages/2935),
and the syntax may change. `parse` keeps the digest in the node and hands
the preimage back through the `preimages` mapping a caller passes in, as
`descriptors.parse` hands an xprv back through `prv_keys`, so `str` writes
the digest. `descriptors.private_string` writes ``preimage(HEX)`` back.

Above `key_expression`, whose KEY expressions the fragments hold, and
below `descriptors`, which reads a miniscript wherever a SCRIPT
expression may be one. This module imports the first and not the second.

BIP379: https://github.com/bitcoin/bips/blob/master/bip-0379.md
"""

from __future__ import annotations

import re
from collections import Counter, deque
from collections.abc import Callable, Iterable, Mapping, MutableMapping, Sequence
from contextlib import suppress
from contextvars import ContextVar
from dataclasses import dataclass, field, replace
from itertools import accumulate, product
from typing import SupportsIndex, TypeVar

from btclib.alias import Octets, ScriptList
from btclib.exceptions import BTClibTypeError, BTClibValueError
from btclib.hashes import hash160, hash256, ripemd160, sha256
from btclib.network import network_from_xkeyversion
from btclib.script.limits import (
    MAX_OPS_PER_SCRIPT,
    MAX_PUBKEYS_PER_MULTISIG,
    MAX_STACK_SIZE,
)
from btclib.script.script import (
    BYTE_FROM_OP_CODE_NAME,
    op_code_spans,
    push_int,
    serialize,
)
from btclib.tx.limits import (
    LOCKTIME_THRESHOLD,
    SEQUENCE_LOCKTIME_DISABLE_FLAG,
    SEQUENCE_LOCKTIME_MASK,
    SEQUENCE_LOCKTIME_TYPE_FLAG,
)
from btclib.utils import (
    assert_type,
    bytes_from_octets,
    decode_num,
    encode_num,
    is_integer,
)
from btclib.var_int import serialize as var_int_serialize
from typing_extensions import override

from bip32_descriptors._refusal import (
    _label,
    _position,
    _quoted,
    _relayed,
    _size,
    _type_name,
)
from bip32_descriptors.bip32.bip32 import BIP32KeyData
from bip32_descriptors.bip32.der_path import (
    _MAX_DIGITS,
    _assert_few_digits,
    _int_from_digits,
)
from bip32_descriptors.descriptors.key_expression import (
    KeyExpression,
    PrvKeys,
    _assert_compressed,
    _assert_outside_taproot,
    _assert_prv_keys,
    _checked_state,
    _offered_signature,
    _parse_key,
    _script_keys,
    _split_arguments,
    _wifs_filed_in,
)

__all__ = [
    "P2WSH",
    "TAPSCRIPT",
    "Miniscript",
    "Preimages",
    "Quorum",
    "SpendContext",
    "SpendingPath",
    "from_script",
    "parse",
    "reads_back",
]

# the two contexts BIP379 is specified for, P2SH and bare scripts being
# excluded from it. Spelled as the BIP spells them, these being what an
# error message has to name
P2WSH = "P2WSH"
TAPSCRIPT = "tapscript"

# what a p2wsh witness script may hold and still be relayed: Bitcoin
# Core's MAX_STANDARD_P2WSH_SCRIPT_SIZE and MAX_STANDARD_P2WSH_STACK_ITEMS,
# both policy and not consensus, which is why they are here and not in
# `script.limits` beside the consensus caps
_MAX_STANDARD_P2WSH_SCRIPT_SIZE = 3600
_MAX_STANDARD_P2WSH_STACK_ITEMS = 100

# a tapscript has no size limit of its own, so what bounds one is the
# weight of a transaction that could spend it: Bitcoin Core's
# `MaxScriptSize` takes the standard transaction weight, less the body of a
# spending transaction and less a maximal witness. Derived here rather than
# copied, every term being a constant with a name -- a bound this loose is
# not one a real script runs into, so a wrong copy of it would go unnoticed
_MAX_STANDARD_TX_WEIGHT = 400000
_WITNESS_SCALE_FACTOR = 4
# version and nLockTime, the input count and one input's 41 bytes without
# its witness, the output count and one p2wsh output's 43 bytes, and the
# two bytes of the segwit marker, which are witness and not body
_TX_BODY_LEEWAY_WEIGHT = (8 + 1 + 41 + 1 + 43) * _WITNESS_SCALE_FACTOR + 2
# and the largest witness: a full stack of the largest element a tapscript
# miniscript puts on it -- a BIP340 signature and its sighash byte -- each
# with its length prefix, plus the largest control block, which is 33 bytes
# and 128 nodes of 32
_MAX_TAPSCRIPT_SAT_SIZE = (
    len(var_int_serialize(MAX_STACK_SIZE))
    + (1 + 65) * MAX_STACK_SIZE
    + len(var_int_serialize(33 + 32 * 128))
    + (33 + 32 * 128)
)
_TAPSCRIPT_WEIGHT_LEFT = (
    _MAX_STANDARD_TX_WEIGHT - _TX_BODY_LEEWAY_WEIGHT - _MAX_TAPSCRIPT_SAT_SIZE
)
# less what says how long the script itself is, that being part of the same
# witness
_MAX_TAPSCRIPT_SIZE = _TAPSCRIPT_WEIGHT_LEFT - len(
    var_int_serialize(_TAPSCRIPT_WEIGHT_LEFT)
)

# BIP342 puts no bound of its own on the keys of a multi_a(), so the bound
# is the stack: one element per key, and no more than 1000 elements
_MAX_PUBKEYS_PER_MULTI_A = 999

# the most arguments a parse reads in one thresh(). The stack analysis of a
# thresh() takes time quadratic in its arguments and the size of a tapscript
# alone allows thousands; a thresh() of keys over this many is unsatisfiable
# anyway, its witness holding one element per key. This is a limit of
# this library and not of the protocol: it refuses some thresh() Bitcoin Core
# parses. It bounds one thresh() and not nested ones, which `_Built`'s
# running total of script size bounds
_MAX_THRESH_ARGUMENTS = MAX_STACK_SIZE

# 1 <= n < 2**31 for older() and after(): a script number is signed, so
# 2**31 is the first value CHECKSEQUENCEVERIFY cannot be handed, and zero
# locks nothing
_MAX_TIMELOCK = 0x80000000

# the wrappers, which keep the colon they are written with: `str` writes a
# tag as it stands, and "v" alone would name a fragment that is not one
_WRAPPERS = ("a:", "s:", "c:", "d:", "v:", "j:", "n:")

# and the two-argument combinators, which is what makes them one case in
# every table below. andor() is the only three-argument fragment
_BINARY = ("and_v", "and_b", "or_b", "or_c", "or_d", "or_i")

# the hash fragments, the op code each commits with, and the size of the
# digest it holds. The tables built of the fragments read the tuple: a
# dict handed on is one the census cannot follow
_HASHES = ("sha256", "hash256", "ripemd160", "hash160")
_HASH_OP_CODES = dict(
    zip(_HASHES, ("OP_SHA256", "OP_HASH256", "OP_RIPEMD160", "OP_HASH160"), strict=True)
)
_DATA_SIZE = {
    "sha256": 32,
    "hash256": 32,
    "ripemd160": 20,
    "hash160": 20,
    # the hash160 of the key a raw_pk_h() holds in place of the key
    "raw_pk_h": 20,
}
# and the function each hash fragment commits with, which is what turns a
# ``preimage(HEX)`` into the digest the node holds
_HASH_FUNCTIONS: dict[str, Callable[[bytes], bytes]] = {
    "sha256": sha256,
    "hash256": hash256,
    "ripemd160": ripemd160,
    "hash160": hash160,
}
# BIP379 satisfies a hash fragment with a preimage of 32 bytes and no other
_PREIMAGE_SIZE = 32

# what `parse` hands back beside the expression, as `prv_keys` is: the
# preimage each ``preimage(HEX)`` was read with, filed under the digest the
# node keeps. Digest to preimage, as `SpendContext`'s preimage mappings
# are keyed
Preimages = Mapping[bytes, bytes]

# the preimages `descriptors.private_string` writes back, None outside it:
# a context variable, as `key_expression._PRINTED_PRV_KEYS` is
_PRINTED_PREIMAGES: ContextVar[Preimages | None] = ContextVar(
    "_PRINTED_PREIMAGES", default=None
)

# how many subexpressions each fragment takes. thresh() is not here: it
# takes one or more, which its threshold then bounds
_ARITY = {
    **dict.fromkeys(
        ("0", "1", "pk_k", "pk_h", "raw_pk_h", "older", "after", "multi", "multi_a"),
        0,
    ),
    **dict.fromkeys(_HASHES, 0),
    **dict.fromkeys(_WRAPPERS, 1),
    **dict.fromkeys(_BINARY, 2),
    "andor": 3,
}

# a number, as the fragments that take one accept it: digits, leading
# zeros included, and nothing else
_NUMBER = re.compile(r"[0-9]+")

# Bitcoin Core's `IsHex`: an even, non-zero count of hex characters, and
# nothing else. `bytes.fromhex` skips whitespace, so a hash holding a space
# would print back without it, changing the checksum.
_HEX = re.compile(r"(?:[0-9a-fA-F]{2})+")


def _assert_valid_context(context: str) -> None:
    """Refuse a spend context that is not one of BIP379's two.

    Every rule below reads the context by asking whether it is
    `TAPSCRIPT`, so a third value is not an unknown context: it is the
    p2wsh one, silently, and the expression is type-checked and sized
    under rules it was not offered to. A function rather than two lines
    at the one caller, so that `parse` keeps the branch count ruff's
    C901 allows it.
    """
    assert_type(context, str, "context")
    if context not in {P2WSH, TAPSCRIPT}:
        err_msg = "unknown spend context"
        err_msg += f"; it must be one of {sorted((P2WSH, TAPSCRIPT))}"
        raise BTClibValueError(err_msg)


def _assert_fragment_type(fragment: object) -> None:
    """Refuse a fragment that is no `str`, a subclass of it included.

    The fragment is looked up in dicts and sets, and a subclass can hash
    as no `str` does, or not hash at all. `_named_fragment` makes the same
    test. The refusal names the type alone, echoing no value.
    """
    if type(fragment) is not str:
        err_msg = f"invalid fragment type: {_type_name(fragment)}"
        raise BTClibTypeError(err_msg)


def _multisig_context_fault(fragment: str) -> str:
    """Return the fault of a multisig outside its context."""
    if fragment == "multi":
        return "multi() is not allowed in a tapscript"
    return "multi_a() is only allowed in a tapscript"


def _assert_key_number(fragment: str, keys: int) -> None:
    """Refuse a number of keys the fragment does not take."""
    if fragment in {"pk_k", "pk_h"}:
        allowed = keys == 1
    elif fragment == "multi":
        allowed = 1 <= keys <= MAX_PUBKEYS_PER_MULTISIG
    elif fragment == "multi_a":
        allowed = 1 <= keys <= _MAX_PUBKEYS_PER_MULTI_A
    else:
        allowed = keys == 0
    if not allowed:
        err_msg = (
            f"invalid number of keys in {_label(fragment, _NAMES)}: {_quoted(keys)}"
        )
        raise BTClibValueError(err_msg)


def _t(properties: str) -> frozenset[str]:
    """Return a set of type properties, one character each."""
    return frozenset(properties)


_NONE: frozenset[str] = frozenset()


def _has(properties: frozenset[str], required: str) -> bool:
    """Answer whether every required property is among those held.

    Bitcoin Core's ``<<`` operator, and the subset rule it stands for: an
    expression that is "Bdu" is one wherever a "B", a "d", a "Bu" or a
    "Bdu" is asked for.
    """
    return _t(required) <= properties


def _if(condition: bool, properties: frozenset[str]) -> frozenset[str]:
    """Return the properties where the condition holds, none where it does not.

    Bitcoin Core's `Type::If`, which is what lets a table row be written as
    one expression.
    """
    return properties if condition else _NONE


def _mixed(x: frozenset[str], y: frozenset[str]) -> bool:
    """Answer whether two subexpressions need incompatible timelocks.

    Which is what the "g", "h", "i" and "j" properties are recorded for: a
    relative time lock ("g") and a relative height lock ("h") cannot both
    be met by one nSequence, an absolute time lock ("i") and an absolute
    height lock ("j") cannot both be met by one nLockTime, and the two
    pairs are independent of each other.
    """
    return (
        (_has(x, "g") and _has(y, "h"))
        or (_has(x, "h") and _has(y, "g"))
        or (_has(x, "i") and _has(y, "j"))
        or (_has(x, "j") and _has(y, "i"))
    )


# the leaf fragments whose type is the same wherever they appear. older()
# and after() are not among them, each carrying which kind of timelock it
# is, and neither is the "u" that only a tapscript ``d:`` has
_LEAF_PROPERTIES = {
    "0": "Bzudemsxk",
    "1": "Bzufmxk",
    "pk_k": "Konudemsxk",
    "pk_h": "Knudemsxk",
    "raw_pk_h": "Knudemsxk",
    "multi": "Bnudemsk",
    "multi_a": "Budemsk",
    **dict.fromkeys(_HASHES, "Bonudmk"),
}


def _leaf_properties(fragment: str, threshold: int) -> frozenset[str]:
    """Return the type of a fragment that has no subexpressions."""
    if fragment == "older":
        return (
            _if(bool(threshold & SEQUENCE_LOCKTIME_TYPE_FLAG), _t("g"))
            | _if(not threshold & SEQUENCE_LOCKTIME_TYPE_FLAG, _t("h"))
            | _t("Bzfmxk")
        )
    if fragment == "after":
        return (
            _if(threshold >= LOCKTIME_THRESHOLD, _t("i"))
            | _if(threshold < LOCKTIME_THRESHOLD, _t("j"))
            | _t("Bzfmxk")
        )
    return _t(_LEAF_PROPERTIES[fragment])


# a return per branch is past ruff's limit on return statements
def _wrapper_properties(  # sourcery skip: lift-return-into-if
    fragment: str, x: frozenset[str], context: str
) -> frozenset[str]:
    """Return the type of a wrapped expression, from its argument's."""
    if fragment == "a:":
        properties = _if(_has(x, "B"), _t("W")) | (x & _t("ghijkudfems")) | _t("x")
    elif fragment == "c:":
        properties = _if(_has(x, "K"), _t("B")) | (x & _t("ghijkondfem")) | _t("us")
    elif fragment == "d:":
        properties = (
            _if(_has(x, "Vz"), _t("B"))
            | _if(_has(x, "z"), _t("o"))
            | _if(_has(x, "f"), _t("e"))
            | (x & _t("ghijkms"))
            # what ``d:`` leaves on the stack is what it duplicated, and
            # only MINIMALIF makes that an exact 1 rather than any nonzero
            # value: consensus under tapscript, policy under P2WSH
            | _if(context == TAPSCRIPT, _t("u"))
            | _t("ndx")
        )
    elif fragment == "j:":
        properties = (
            _if(_has(x, "Bn"), _t("B"))
            | _if(_has(x, "f"), _t("e"))
            | (x & _t("ghijkoums"))
            | _t("ndx")
        )
    elif fragment == "s:":
        properties = _if(_has(x, "Bo"), _t("W")) | (x & _t("ghijkudfemsx"))
    elif fragment == "v:":
        properties = _if(_has(x, "B"), _t("V")) | (x & _t("ghijkzonms")) | _t("fx")
    else:
        properties = (x & _t("ghijkBzondfems")) | _t("ux")
    return properties


def _and_properties(
    fragment: str, x: frozenset[str], y: frozenset[str]
) -> frozenset[str]:
    """Return the type of and_v() or and_b(), the two conjunctions."""
    timelocks = ((x | y) & _t("ghij")) | _if(
        _has(x & y, "k") and not _mixed(x, y), _t("k")
    )
    if fragment == "and_v":
        return (
            _if(_has(x, "V"), y & _t("KVB"))
            | (x & _t("n"))
            | _if(_has(x, "z"), y & _t("n"))
            | _if(_has(x | y, "z"), (x | y) & _t("o"))
            | (x & y & _t("mz"))
            | ((x | y) & _t("s"))
            | _if(_has(y, "f") or _has(x, "s"), _t("f"))
            | (y & _t("ux"))
            | timelocks
        )
    return (
        _if(_has(y, "W"), x & _t("B"))
        | _if(_has(x | y, "z"), (x | y) & _t("o"))
        | (x & _t("n"))
        | _if(_has(x, "z"), y & _t("n"))
        | _if(_has(x & y, "s"), x & y & _t("e"))
        | (x & y & _t("dzm"))
        | _if(_has(x & y, "f") or _has(x, "sf") or _has(y, "sf"), _t("f"))
        | ((x | y) & _t("s"))
        | _t("ux")
        | timelocks
    )


def _or_properties(
    fragment: str, x: frozenset[str], y: frozenset[str]
) -> frozenset[str]:
    """Return the type of one of the four disjunctions.

    Every one of them may mix timelocks: the two branches are alternatives,
    so a satisfaction needs the locks of one of them and never of both.
    """
    timelocks = ((x | y) & _t("ghij")) | (x & y & _t("k"))
    if fragment == "or_b":
        return (
            _if(_has(x, "Bd") and _has(y, "Wd"), _t("B"))
            | _if(_has(x | y, "z"), (x | y) & _t("o"))
            | _if(_has(x | y, "s") and _has(x & y, "e"), x & y & _t("m"))
            | (x & y & _t("zse"))
            | _t("dux")
            | timelocks
        )
    if fragment == "or_c":
        return (
            _if(_has(x, "Bdu"), y & _t("V"))
            | _if(_has(y, "z"), x & _t("o"))
            | _if(_has(x, "e") and _has(x | y, "s"), x & y & _t("m"))
            | (x & y & _t("zs"))
            | _t("fx")
            | timelocks
        )
    if fragment == "or_d":
        return (
            _if(_has(x, "Bdu"), y & _t("B"))
            | _if(_has(y, "z"), x & _t("o"))
            | _if(_has(x, "e") and _has(x | y, "s"), x & y & _t("m"))
            | (x & y & _t("zs"))
            | (y & _t("ufde"))
            | _t("x")
            | timelocks
        )
    return (
        (x & y & _t("VBKufs"))
        | _if(_has(x & y, "z"), _t("o"))
        | _if(_has(x | y, "f"), (x | y) & _t("e"))
        | _if(_has(x | y, "s"), x & y & _t("m"))
        | ((x | y) & _t("d"))
        | _t("x")
        | timelocks
    )


def _andor_properties(
    x: frozenset[str], y: frozenset[str], z: frozenset[str]
) -> frozenset[str]:
    """Return the type of andor(X,Y,Z): X and Y, or else Z.

    The timelock rule is and_v()'s and applies to X and Y alone: Z is the
    branch taken when X is dissatisfied, so its locks are never needed
    together with X's.
    """
    return (
        _if(_has(x, "Bdu"), y & z & _t("BKV"))
        | (x & y & z & _t("z"))
        | _if(_has(x | (y & z), "z"), (x | (y & z)) & _t("o"))
        | (y & z & _t("u"))
        | _if(_has(x, "s") or _has(y, "f"), z & _t("fe"))
        | (z & _t("d"))
        | _if(_has(x, "e") and _has(x | y | z, "s"), x & y & z & _t("m"))
        | (z & (x | y) & _t("s"))
        | _t("x")
        | ((x | y | z) & _t("ghij"))
        | _if(_has(x & y & z, "k") and not _mixed(x, y), _t("k"))
    )


def _thresh_properties(
    subs: Sequence[frozenset[str]], threshold: int
) -> frozenset[str]:
    """Return the type of a thresh(), which has to walk its subexpressions.

    The first is "Bdu" and the rest "Wdu", the stack elements they consume
    are what tell "z" from "o", and the timelock accumulator carries the
    mixing rule: two branches with incompatible locks are a problem only
    where the threshold makes both of them necessary.
    """
    all_e = True
    all_m = True
    arguments = 0
    signed = 0
    timelocks = _t("k")
    for position, sub in enumerate(subs):
        if not _has(sub, "Wdu" if position else "Bdu"):
            return _NONE
        all_e = all_e and _has(sub, "e")
        all_m = all_m and _has(sub, "m")
        signed += _has(sub, "s")
        arguments += 0 if _has(sub, "z") else 1 if _has(sub, "o") else 2
        timelocks = ((timelocks | sub) & _t("ghij")) | _if(
            _has(timelocks & sub, "k")
            and (threshold <= 1 or not _mixed(timelocks, sub)),
            _t("k"),
        )
    return (
        _t("Bdu")
        | _if(arguments == 0, _t("z"))
        | _if(arguments == 1, _t("o"))
        | _if(all_e and signed == len(subs), _t("e"))
        | _if(all_e and all_m and signed >= len(subs) - threshold, _t("m"))
        | _if(signed >= len(subs) - threshold + 1, _t("s"))
        | timelocks
    )


def _computed_properties(node: Miniscript) -> frozenset[str]:
    """Return the type and properties of a node, from its subexpressions'.

    BIP379's three tables at once, as Bitcoin Core's `ComputeType` holds
    them: the correctness table, the timelock-mixing rule and the
    malleability table are computed by the same expressions, so they cannot
    be separated.

    Empty where the requirements are not met, which is what makes an
    ill-typed expression invalid rather than an error, and what propagates:
    a subexpression with no type gives its parent none either.
    """
    subs = [sub.properties for sub in node.subs]
    fragment = node.fragment
    if not subs:
        return _sanitized(_leaf_properties(fragment, node.threshold))
    if fragment in _WRAPPERS:
        return _sanitized(_wrapper_properties(fragment, subs[0], node.context))
    if fragment in {"and_v", "and_b"}:
        return _sanitized(_and_properties(fragment, subs[0], subs[1]))
    if fragment == "andor":
        return _sanitized(_andor_properties(subs[0], subs[1], subs[2]))
    if fragment == "thresh":
        return _sanitized(_thresh_properties(subs, node.threshold))
    return _sanitized(_or_properties(fragment, subs[0], subs[1]))


def _pushed_number(number: int) -> ScriptList:
    """Return the minimal push of a script number, as a command.

    `script.push_int`, which is that choice written once: an op code
    where one means the number -- OP_16 and not the byte 0x10, so that
    what is written is what `from_script` reads back -- and the
    CScriptNum encoding above it. A list, because the callers are
    building one.
    """
    return [push_int(number)]


def _pushed_size(number: int) -> int:
    """Return the length in bytes of the minimal push of a number."""
    return len(serialize(_pushed_number(number)))


# what each fragment adds around its subexpressions: its own op codes,
# one byte each -- which is why this is both the bytes it adds to the
# script and the ops it adds to a spend, the two being the same op codes
# counted twice. ``v:`` is not here, adding a byte only where the last op
# code of its argument has no VERIFY form, and neither is thresh(), which
# adds an OP_ADD per subexpression and a threshold to push
_OVERHEAD = {
    "and_v": 0,
    "s:": 1,
    "c:": 1,
    "n:": 1,
    "and_b": 1,
    "or_b": 1,
    "a:": 2,
    "or_c": 2,
    "d:": 3,
    "or_d": 3,
    "or_i": 3,
    "andor": 3,
    "j:": 4,
}


# a return per branch is past ruff's limit on return statements
def _leaf_script_size(node: Miniscript) -> int:  # sourcery skip: lift-return-into-if
    """Return the length of the script of a fragment with no subexpressions."""
    fragment = node.fragment
    keys = len(node.keys)
    if fragment in {"0", "1"}:
        size = 1
    elif fragment == "pk_k":
        size = 33 if node.context == TAPSCRIPT else 34
    elif fragment in {"pk_h", "raw_pk_h"}:
        # OP_DUP OP_HASH160 <20 bytes> OP_EQUALVERIFY
        size = 3 + 21
    elif fragment in {"older", "after"}:
        size = 1 + _pushed_size(node.threshold)
    elif fragment == "multi":
        size = 1 + _pushed_size(keys) + _pushed_size(node.threshold) + 34 * keys
    elif fragment == "multi_a":
        size = (1 + 32 + 1) * keys + _pushed_size(node.threshold) + 1
    else:
        # a hash fragment: OP_SIZE <32> OP_EQUALVERIFY, the hash op code,
        # the digest, and OP_EQUAL
        size = 4 + 2 + (33 if _DATA_SIZE[fragment] == 32 else 21)
    return size


def _computed_script_size(node: Miniscript) -> int:
    """Return the length of the script, without serializing it.

    Which is what the parser needs before it has an index to derive the
    keys at: the size of a key is the context's and not the key's, so
    every fragment's script has a length its subexpressions' determine.
    """
    if not node.subs:
        return _leaf_script_size(node)
    size = sum(sub.script_size for sub in node.subs)
    if node.fragment == "v:":
        return size + _has(node.subs[0].properties, "x")
    if node.fragment == "thresh":
        return size + len(node.subs) + _pushed_size(node.threshold)
    return size + _OVERHEAD[node.fragment]


def _add(first: int | None, second: int | None) -> int | None:
    """Add two bounds, None being "no such satisfaction"."""
    return None if first is None or second is None else first + second


def _worst(first: int | None, second: int | None) -> int | None:
    """Return the larger of two bounds, disregarding the absent ones."""
    if first is None:
        return second
    return first if second is None else max(first, second)


@dataclass(frozen=True)
class _Bounds:
    """A bound on satisfying and one on dissatisfying, either may be absent."""

    sat: int | None
    dsat: int | None


@dataclass(frozen=True)
class _Trace:
    """What executing a script does to the stack, at worst.

    `net` is how many more elements the stack holds when execution starts
    than when it ends -- negative where the script leaves more than it was
    given -- and `peak` how many more it holds at its highest point than
    at the end. Two numbers because two limits are asked: a p2wsh witness
    may carry a hundred elements, and no script may reach a thousand on
    the stack while it runs.
    """

    net: int
    peak: int


def _union(first: _Trace | None, second: _Trace | None) -> _Trace | None:
    """Return the worse of two possible executions, either may be impossible."""
    if first is None:
        return second
    if second is None:
        return first
    return _Trace(max(first.net, second.net), max(first.peak, second.peak))


def _concat(first: _Trace | None, second: _Trace | None) -> _Trace | None:
    """Return the execution of one script followed by another.

    The nets add; the peak is the second script's own, or the first's once
    what the second consumes is accounted for. Not commutative, which is
    the whole reason this is a function and not a maximum: "OP_1 OP_DROP"
    reaches one element above where it ends and "OP_DROP OP_1" reaches
    none.
    """
    if first is None or second is None:
        return None
    return _Trace(first.net + second.net, max(second.peak, second.net + first.peak))


# the stack effect of the op codes a fragment's script is built from. A
# push is one element the stack did not hold before, hence a net of -1;
# OP_IF consumes one, hence 1; and what neither pushes nor pops -- the
# hash op codes, the two repurposed nops, OP_ELSE, OP_ENDIF -- is the
# identity of the concatenation
_EMPTY = _Trace(0, 0)
_PUSH = _Trace(-1, 0)
_NOP = _Trace(0, 0)
_IF = _Trace(1, 1)
_BINARY_OP = _Trace(1, 1)
_ONE_ARG_OP = _Trace(1, 1)
_EQUALVERIFY = _Trace(2, 2)


def _hash_trace() -> _Trace | None:
    """Return the execution of a hash fragment's script.

    OP_SIZE duplicates the element, the pushed 32 and OP_EQUALVERIFY
    consume both, the hash op code rewrites the element in place, and the
    pushed digest and OP_EQUAL consume it: one element in, one out.
    """
    return _concat(
        _concat(_concat(_PUSH, _PUSH), _concat(_EQUALVERIFY, _NOP)),
        _concat(_PUSH, _ONE_ARG_OP),
    )


def _leaf_ops(node: Miniscript) -> tuple[int, _Bounds]:
    """Return the op codes of a leaf's script, and what its spend adds.

    The count is the non-push op codes, and the two bounds the keys of an
    OP_CHECKMULTISIG that may be executed: BIP141 counts those against the
    201 of a p2wsh spend, and how many are counted depends on which branch
    runs.
    """
    fragment = node.fragment
    keys = len(node.keys)
    if fragment == "multi":
        # one op code, and the keys of the OP_CHECKMULTISIG that runs
        return 1, _Bounds(keys, keys)
    if fragment == "multi_a":
        # a CHECKSIG or a CHECKSIGADD per key, and the OP_NUMEQUAL
        return keys + 1, _Bounds(0, 0)
    count, sat, dsat = _LEAF_OPS[fragment]
    return count, _Bounds(sat, dsat)


# what each leaf costs: its non-push op codes, and the keys of an
# OP_CHECKMULTISIG its satisfaction and its dissatisfaction may execute --
# None where there is no such thing to do. multi() and multi_a() are not
# here, both counting their keys
_LEAF_OPS: dict[str, tuple[int, int | None, int | None]] = {
    "1": (0, 0, None),
    "0": (0, None, 0),
    "pk_k": (0, 0, 0),
    "pk_h": (3, 0, 0),
    "raw_pk_h": (3, 0, 0),
    "older": (1, 0, None),
    "after": (1, 0, None),
    **dict.fromkeys(_HASHES, (4, 0, None)),
}


def _wrapper_ops(node: Miniscript) -> tuple[int, _Bounds]:
    """Return the ops of a wrapped expression."""
    sub = node.subs[0]
    if node.fragment == "v:":
        return sub._static_ops + _has(sub.properties, "x"), _Bounds(sub._ops.sat, None)
    # ``d:`` and ``j:`` are dissatisfied by the element their OP_IF reads,
    # so nothing of the argument runs and nothing of it is counted
    dsat = 0 if node.fragment in {"d:", "j:"} else sub._ops.dsat
    static = _OVERHEAD[node.fragment] + sub._static_ops
    return static, _Bounds(sub._ops.sat, dsat)


def _binary_ops(node: Miniscript) -> tuple[int, _Bounds]:
    """Return the ops of a combinator, from its subexpressions'."""
    x, y = (sub._ops for sub in node.subs[:2])
    fragment = node.fragment
    if fragment == "and_v":
        bounds = _Bounds(_add(x.sat, y.sat), None)
    elif fragment == "and_b":
        bounds = _Bounds(_add(x.sat, y.sat), _add(x.dsat, y.dsat))
    elif fragment == "or_b":
        sat = _worst(_add(x.sat, y.dsat), _add(y.sat, x.dsat))
        bounds = _Bounds(sat, _add(x.dsat, y.dsat))
    elif fragment == "or_c":
        bounds = _Bounds(_worst(x.sat, _add(y.sat, x.dsat)), None)
    elif fragment == "or_d":
        bounds = _Bounds(_worst(x.sat, _add(y.sat, x.dsat)), _add(x.dsat, y.dsat))
    elif fragment == "or_i":
        bounds = _Bounds(_worst(x.sat, y.sat), _worst(x.dsat, y.dsat))
    else:
        z = node.subs[2]._ops
        sat = _worst(_add(y.sat, x.sat), _add(x.dsat, z.sat))
        bounds = _Bounds(sat, _add(x.dsat, z.dsat))
    static = sum(sub._static_ops for sub in node.subs)
    return static + _OVERHEAD[fragment], bounds


def _thresh_ops(node: Miniscript) -> tuple[int, _Bounds]:
    """Return the ops of a thresh(), over every way to reach its threshold.

    `reached[j]` is the worst cost of satisfying exactly j of the
    subexpressions read so far, which is the only way to bound this: the
    threshold says how many are satisfied and not which, and the
    expensive ones are not always the ones that count.
    """
    static = 0
    reached: list[int | None] = [0]
    for sub in node.subs:
        static += sub._static_ops + 1
        following: list[int | None] = [_add(reached[0], sub._ops.dsat)]
        following.extend(
            _worst(_add(reached[j], sub._ops.dsat), _add(reached[j - 1], sub._ops.sat))
            for j in range(1, len(reached))
        )
        following.append(_add(reached[-1], sub._ops.sat))
        reached = following
    return static, _Bounds(reached[node.threshold], reached[0])


def _computed_ops(node: Miniscript) -> tuple[int, _Bounds]:
    """Return the static op codes of the script and the ops a spend adds."""
    if not node.subs:
        return _leaf_ops(node)
    if node.fragment in _WRAPPERS:
        return _wrapper_ops(node)
    return _thresh_ops(node) if node.fragment == "thresh" else _binary_ops(node)


# what satisfying and dissatisfying each leaf does to the stack, None
# where there is no such thing to do. multi() and multi_a() are not here,
# both counting their keys; the pk_h() trace is OP_DUP, the hash, the
# pushed digest and the OP_EQUALVERIFY, which is one element in and one out
_LEAF_STACK: dict[str, tuple[_Trace | None, _Trace | None]] = {
    "0": (None, _PUSH),
    "1": (_PUSH, None),
    "older": (_concat(_PUSH, _NOP), None),
    "after": (_concat(_PUSH, _NOP), None),
    "pk_k": (_PUSH, _PUSH),
    "pk_h": (
        _concat(_concat(_PUSH, _NOP), _concat(_PUSH, _EQUALVERIFY)),
        _concat(_concat(_PUSH, _NOP), _concat(_PUSH, _EQUALVERIFY)),
    ),
    "raw_pk_h": (
        _concat(_concat(_PUSH, _NOP), _concat(_PUSH, _EQUALVERIFY)),
        _concat(_concat(_PUSH, _NOP), _concat(_PUSH, _EQUALVERIFY)),
    ),
    **dict.fromkeys(_HASHES, (_hash_trace(), None)),
}


def _leaf_stack(node: Miniscript) -> tuple[_Trace | None, _Trace | None]:
    """Return what satisfying and dissatisfying a leaf does to the stack."""
    fragment = node.fragment
    if fragment == "multi":
        # k+1 elements in -- a dummy and k signatures -- and one out,
        # having reached k+n+2 more than it ends with: the n keys, the two
        # thresholds, and what it started with
        return (_Trace(node.threshold, node.threshold + len(node.keys) + 2),) * 2
    if fragment == "multi_a":
        # one element per key in, one out, and one more than the keys at
        # its peak: the first key pushed on top of them all
        return (_Trace(len(node.keys) - 1, len(node.keys)),) * 2
    return _LEAF_STACK[fragment]


def _wrapper_stack(node: Miniscript) -> tuple[_Trace | None, _Trace | None]:
    """Return the stack effect of a wrapped expression."""
    fragment = node.fragment
    sat, dsat = node.subs[0]._stack
    if fragment in {"a:", "s:", "n:"}:
        return sat, dsat
    if fragment == "c:":
        return _concat(sat, _ONE_ARG_OP), _concat(dsat, _ONE_ARG_OP)
    if fragment == "d:":
        prefix = _concat(_PUSH, _IF)
        return _concat(prefix, sat), prefix
    if fragment == "v:":
        return _concat(sat, _ONE_ARG_OP), None
    prefix = _concat(_concat(_PUSH, _NOP), _IF)
    return _concat(prefix, sat), prefix


# a return per branch is past ruff's limit on return statements
def _binary_stack(  # sourcery skip: lift-return-into-if
    node: Miniscript,
) -> tuple[_Trace | None, _Trace | None]:
    """Return the stack effect of a combinator, from its subexpressions'."""
    fragment = node.fragment
    (x_sat, x_dsat), (y_sat, y_dsat) = (sub._stack for sub in node.subs[:2])
    traces: tuple[_Trace | None, _Trace | None]
    if fragment == "and_b":
        traces = (
            _concat(_concat(x_sat, y_sat), _BINARY_OP),
            _concat(_concat(x_dsat, y_dsat), _BINARY_OP),
        )
    elif fragment == "and_v":
        traces = _concat(x_sat, y_sat), None
    elif fragment == "or_b":
        sat = _union(_concat(x_sat, y_dsat), _concat(x_dsat, y_sat))
        traces = (
            _concat(sat, _BINARY_OP),
            _concat(_concat(x_dsat, y_dsat), _BINARY_OP),
        )
    elif fragment == "or_c":
        traces = _union(_concat(x_sat, _IF), _concat(_concat(x_dsat, _IF), y_sat)), None
    elif fragment == "or_d":
        # OP_IFDUP duplicates what it reads only where that is nonzero,
        # which is the branch taken when X was satisfied
        taken = _concat(_concat(x_sat, _PUSH), _IF)
        not_taken = _concat(_concat(x_dsat, _EMPTY), _IF)
        traces = _union(taken, _concat(not_taken, y_sat)), _concat(not_taken, y_dsat)
    elif fragment == "or_i":
        traces = (
            _concat(_IF, _union(x_sat, y_sat)),
            _concat(_IF, _union(x_dsat, y_dsat)),
        )
    else:
        z_sat, z_dsat = node.subs[2]._stack
        traces = (
            _union(
                _concat(_concat(x_sat, _IF), y_sat),
                _concat(_concat(x_dsat, _IF), z_sat),
            ),
            _concat(_concat(x_dsat, _IF), z_dsat),
        )
    return traces


def _thresh_stack(node: Miniscript) -> tuple[_Trace | None, _Trace | None]:
    """Return the stack effect of a thresh(), over every way to reach it."""
    reached: list[_Trace | None] = [_EMPTY]
    for position, sub in enumerate(node.subs):
        sat, dsat = sub._stack
        add = _BINARY_OP if position else _EMPTY
        following: list[_Trace | None] = [_concat(_concat(reached[0], dsat), add)]
        following.extend(
            _concat(
                _union(_concat(reached[j], dsat), _concat(reached[j - 1], sat)), add
            )
            for j in range(1, len(reached))
        )
        following.append(_concat(_concat(reached[-1], sat), add))
        reached = following
    equal = _concat(_PUSH, _ONE_ARG_OP)
    return (
        _concat(reached[node.threshold], equal),
        _concat(reached[0], equal),
    )


def _computed_stack(node: Miniscript) -> tuple[_Trace | None, _Trace | None]:
    """Return what satisfying and dissatisfying costs the stack."""
    if not node.subs:
        return _leaf_stack(node)
    if node.fragment in _WRAPPERS:
        return _wrapper_stack(node)
    if node.fragment == "thresh":
        return _thresh_stack(node)
    return _binary_stack(node)


def _signature_size(context: str) -> int:
    """Return the largest signature the context's CHECKSIG takes.

    A low-s DER signature and its sighash byte under P2WSH; a BIP340
    signature and its sighash byte under tapscript, that byte being what a
    non-default sighash type costs there.
    """
    return 65 if context == TAPSCRIPT else 72


def _pub_key_size(context: str) -> int:
    """Return the public key size the context writes: x-only, or SEC."""
    return 32 if context == TAPSCRIPT else 33


# a return per branch is past ruff's limit on return statements
def _leaf_witness(node: Miniscript) -> _Bounds:  # sourcery skip: lift-return-into-if
    """Return the witness bytes a leaf's satisfaction and dissatisfaction take.

    A signature is 72 bytes and a sighash byte under P2WSH -- the largest
    a low-s DER signature is -- and 64 bytes and a sighash byte under
    tapscript, plus in both cases the byte that says how long the element
    is. A dissatisfaction is the empty push, which is one byte.
    """
    signature = 1 + _signature_size(node.context)
    pub_key = 1 + _pub_key_size(node.context)
    fragment = node.fragment
    if fragment == "0":
        bounds = _Bounds(None, 0)
    elif fragment in {"1", "older", "after"}:
        bounds = _Bounds(0, None)
    elif fragment == "pk_k":
        bounds = _Bounds(signature, 1)
    elif fragment in {"pk_h", "raw_pk_h"}:
        bounds = _Bounds(signature + pub_key, 1 + pub_key)
    elif fragment == "multi":
        # the dummy element OP_CHECKMULTISIG pops, plus a signature per
        # key the threshold asks for
        bounds = _Bounds(node.threshold * signature + 1, node.threshold + 1)
    elif fragment == "multi_a":
        # one element per key: a signature for those that sign, and the
        # empty push for the rest
        bounds = _Bounds(
            node.threshold * signature + len(node.keys) - node.threshold,
            len(node.keys),
        )
    else:
        # a hash fragment is satisfied by its 32-byte preimage, and BIP379
        # allows no other size
        bounds = _Bounds(1 + 32, None)
    return bounds


def _wrapper_witness(node: Miniscript) -> _Bounds:
    """Return the witness bounds of a wrapped expression."""
    sub = node.subs[0]._witness
    fragment = node.fragment
    if fragment in {"a:", "s:", "n:", "c:"}:
        return sub
    if fragment == "d:":
        # the element ``d:`` duplicates is part of the witness: OP_1 to
        # satisfy, and the empty push to dissatisfy
        return _Bounds(_add(1 + 1, sub.sat), 1)
    return _Bounds(sub.sat, None) if fragment == "v:" else _Bounds(sub.sat, 1)


# a return per branch is past ruff's limit on return statements
def _binary_witness(node: Miniscript) -> _Bounds:  # sourcery skip: lift-return-into-if
    """Return the witness bounds of a combinator."""
    fragment = node.fragment
    x, y = (sub._witness for sub in node.subs[:2])
    if fragment == "and_b":
        bounds = _Bounds(_add(x.sat, y.sat), _add(x.dsat, y.dsat))
    elif fragment == "and_v":
        bounds = _Bounds(_add(x.sat, y.sat), None)
    elif fragment == "or_b":
        bounds = _Bounds(
            _worst(_add(x.dsat, y.sat), _add(x.sat, y.dsat)), _add(x.dsat, y.dsat)
        )
    elif fragment == "or_c":
        bounds = _Bounds(_worst(x.sat, _add(x.dsat, y.sat)), None)
    elif fragment == "or_d":
        bounds = _Bounds(_worst(x.sat, _add(x.dsat, y.sat)), _add(x.dsat, y.dsat))
    elif fragment == "or_i":
        # the branch selector, which is OP_1 or the empty push: two bytes
        # for the first branch and one for the second
        bounds = _Bounds(
            _worst(_add(x.sat, 2), _add(y.sat, 1)),
            _worst(_add(x.dsat, 2), _add(y.dsat, 1)),
        )
    else:
        z = node.subs[2]._witness
        bounds = _Bounds(
            _worst(_add(x.sat, y.sat), _add(x.dsat, z.sat)), _add(x.dsat, z.dsat)
        )
    return bounds


def _thresh_witness(node: Miniscript) -> _Bounds:
    """Return the witness bounds of a thresh(), over every way to reach it."""
    reached: list[int | None] = [0]
    for sub in node.subs:
        following: list[int | None] = [_add(reached[0], sub._witness.dsat)]
        following.extend(
            _worst(
                _add(reached[j], sub._witness.dsat),
                _add(reached[j - 1], sub._witness.sat),
            )
            for j in range(1, len(reached))
        )
        following.append(_add(reached[-1], sub._witness.sat))
        reached = following
    return _Bounds(reached[node.threshold], reached[0])


def _computed_witness(node: Miniscript) -> _Bounds:
    """Return the witness bytes satisfying and dissatisfying may take."""
    if not node.subs:
        return _leaf_witness(node)
    if node.fragment in _WRAPPERS:
        return _wrapper_witness(node)
    if node.fragment == "thresh":
        return _thresh_witness(node)
    return _binary_witness(node)


# the fields of `Miniscript` its build computes, in the order of `_derived`
_DERIVED = ("properties", "script_size", "_static_ops", "_ops", "_stack", "_witness")


def _derived(node: Miniscript) -> tuple[object, ...]:
    """Return the fields of `_DERIVED`, computed for the node.

    From its own fields and the derived fields its subexpressions hold.
    """
    static, ops = _computed_ops(node)
    return (
        _computed_properties(node),
        _computed_script_size(node),
        static,
        ops,
        _computed_stack(node),
        _computed_witness(node),
    )


def _assert_derived(root: Miniscript) -> None:
    """Refuse a tree a node of which holds fields its build did not compute.

    `Miniscript` computes its type properties, its script size and its
    bounds when a node is built, and `object.__setattr__` can set them
    after, so a forged node can hold a type its subexpressions do not
    give it. Each node is asked after its subexpressions, so that what
    it is computed from has been asked already, and is refused first for
    its fragment, its arity and its number as its build refuses them,
    without which nothing can be computed. A node is refused where it
    lacks a field its build computes.

    Every node is asked, a multisig of more keys than its fragment takes
    included: what its build computes reads their number, whatever it is.
    The caller has checked the type of every node, its context, and the
    types of its fragment, its subexpressions and its keys.
    """
    nodes: list[Miniscript] = []
    stack = [root]
    while stack:
        node = stack.pop()
        nodes.append(node)
        stack += node.subs
    for node in reversed(nodes):
        node._assert_arity()
        node._assert_number()
        # None, which no build computes, for a field a forged node lacks
        stored = tuple(getattr(node, name, None) for name in _DERIVED)
        if _derived(node) == stored:
            continue
        err_msg = f"{_label(node.fragment, _NAMES)} holds a type or bounds its build"
        err_msg += " does not compute"
        raise BTClibValueError(err_msg)


[docs] @dataclass(frozen=True) class Miniscript: """A miniscript expression: a fragment, its arguments, its own type. The fragment is the name BIP379 gives it, a wrapper keeping the colon it is written with. `subs` are the subexpressions; `keys` the KEY expressions of ``pk_k()``, ``pk_h()``, ``multi()`` and ``multi_a()``; `threshold` the number of ``older()``, ``after()``, ``thresh()``, ``multi()`` and ``multi_a()``; and `data` the digest of a hash fragment. One field per kind of argument, no fragment using all four. What is derived is computed once, when the node is built: the type properties, the script's length, and the bounds on ops, stack and witness. Not for speed but for depth -- an expression nests as deep as its script is long, so a value computed on demand would be computed by a recursion the length of the tree, where a parser building the tree bottom-up has each subexpression's answer already. """ fragment: str context: str = P2WSH subs: tuple[Miniscript, ...] = () keys: tuple[KeyExpression, ...] = () threshold: int = 0 data: bytes = b"" # BIP379's type properties, the script size, and the three bounds: # functions of the fields above, and therefore out of the comparison # and the repr properties: frozenset[str] = field(init=False, compare=False, repr=False) script_size: int = field(init=False, compare=False, repr=False) _static_ops: int = field(init=False, compare=False, repr=False) _ops: _Bounds = field(init=False, compare=False, repr=False) _stack: tuple[_Trace | None, _Trace | None] = field( init=False, compare=False, repr=False ) _witness: _Bounds = field(init=False, compare=False, repr=False) def __post_init__(self) -> None: """Check the shape of the node, then compute what it determines.""" self._assert_shape() for name, value in zip(_DERIVED, _derived(self), strict=True): object.__setattr__(self, name, value) def __setstate__(self, state: object) -> None: """Refuse a pickle holding a field the build refuses. As `_checked_state` says. A subexpression that is no `Miniscript` is refused first, naming its type alone. """ subs = state.get("subs") if isinstance(state, dict) else None for sub in subs if isinstance(subs, tuple) else (): assert_type(sub, Miniscript, "subexpression") _checked_state(self, state) def _assert_shape(self) -> None: """Refuse a node no fragment has the shape of. The context, the arity, the keys, the digest and the number: a caller building a node by hand can get any of them wrong, and `parse` and `from_script` leave some of them to it. Refused rather than typed empty: an ill-*shaped* node is not an expression the rules have anything to say about, where an ill-*typed* one is an expression they refuse. A fragment or a context given by hand is any value, so its refusal names only the library's own. The fragment, the subexpressions, the keys, the digest and the number each have their type asserted just before their value is checked, the fragment as `_assert_fragment_type` says, the subexpressions and the keys as a tuple and each key as a `KeyExpression`: a field of another type leaves as a `BTClibTypeError` naming the field and the type alone. """ assert_type(self.context, str, "context") if self.context not in (P2WSH, TAPSCRIPT): err_msg = "unknown spend context" err_msg += f"; it must be one of {sorted((P2WSH, TAPSCRIPT))}" raise BTClibValueError(err_msg) self._assert_arity() fragment = self.fragment # each node writes its keys in its own context, so a subexpression # of the other one would put keys of the wrong size in the script if any(sub.context != self.context for sub in self.subs): err_msg = f"a subexpression of {_label(fragment, _NAMES)} is not in" err_msg += f" {_label(self.context, _CONTEXTS)}" raise BTClibValueError(err_msg) self._assert_keys() assert_type(self.data, bytes, "data") # sized by the bytes it holds, which no subclass's __len__ answers size = bytes.__len__(self.data) if size != _DATA_SIZE.get(fragment, 0): expected = _DATA_SIZE.get(fragment, 0) err_msg = ( f"{_label(fragment, _NAMES)} takes {_position(expected)} bytes" f" of data, {_size(self.data)} given" ) raise BTClibValueError(err_msg) self._assert_number() def _assert_arity(self) -> None: """Refuse a fragment the library does not define, and its arity. The fragment's type is asserted first, as `_assert_fragment_type` says, and the subexpressions are asserted a tuple once the fragment is known. """ fragment = self.fragment _assert_fragment_type(fragment) if fragment != "thresh" and fragment not in _ARITY: raise BTClibValueError("unknown miniscript fragment") assert_type(self.subs, tuple, "subs") if fragment == "thresh": if not self.subs: raise BTClibValueError("thresh() takes at least one subexpression") elif len(self.subs) != _ARITY[fragment]: err_msg = ( f"{_label(fragment, _NAMES)} takes {_ARITY[fragment]} subexpressions, " f"{_size(self.subs)} given" ) raise BTClibValueError(err_msg) def _assert_keys(self) -> None: """Refuse the keys no fragment holds, and the ones a context forbids. A node of either context refuses an uncompressed key, and a P2WSH node a ``musig()`` key and an x-only one, with `parse`'s message, since its text would not parse back. A ``musig()`` key would also want an ECDSA signature, which a MuSig2 group does not make (BIP327). """ assert_type(self.keys, tuple, "keys") for key in self.keys: assert_type(key, KeyExpression, "key") fragment = self.fragment keys = len(self.keys) if fragment in {"multi", "multi_a"} and (fragment == "multi_a") != ( self.context == TAPSCRIPT ): raise BTClibValueError(_multisig_context_fault(fragment)) # a key the context refuses, before the number of keys: `parse` # reads the keys of a fragment that takes them before it counts # them. A fragment that takes none and holds such a key is refused # for the key too for key in self.keys: if self.context == P2WSH: _assert_outside_taproot(key) _assert_compressed(key) _assert_key_number(fragment, keys) def _assert_number(self) -> None: """Refuse a lock time or a threshold not an integer or out of bounds. One `parse` refuses when it reads the text `str` writes is refused first, as `_assert_number_read` says, so that no refusal quotes a number longer than `parse` quotes. """ self._assert_number_read() fragment = self.fragment threshold = self.threshold name = _label(fragment, _NAMES) if fragment in {"older", "after"}: if not 1 <= threshold < _MAX_TIMELOCK: raise BTClibValueError(f"invalid {name}() value: {_quoted(threshold)}") elif fragment in {"multi", "multi_a"}: if not 1 <= threshold <= len(self.keys): err_msg = f"invalid k in k-of-n {name}: {_quoted(threshold)}" raise BTClibValueError(err_msg) elif fragment == "thresh": if not 1 <= threshold <= len(self.subs): err_msg = "invalid thresh() threshold:" err_msg += f" {_quoted(threshold)} of {_size(self.subs)}" raise BTClibValueError(err_msg) elif threshold: raise BTClibValueError(f"{name} takes no number: {_quoted(threshold)}") def _assert_number_read(self) -> None: """Refuse a number of another type, then one `parse` cannot read. As `_assert_read_number` says. A walk of a forged node calls this before anything writes the node's text: Python refuses to write an `int` past its limit on digits. """ if not is_integer(self.threshold): err_msg = f"invalid threshold type: {_type_name(self.threshold)}" raise BTClibTypeError(err_msg) _assert_read_number(self.fragment, self.threshold) @property def is_valid(self) -> bool: """Answer whether the expression is typed and fits its context.""" return bool(self.properties) and self.script_size <= _max_script_size( self.context ) @property def is_valid_top_level(self) -> bool: """Answer whether the expression can be a script on its own. Which asks one thing beyond validity: the type must be "B", a script being satisfied by what it leaves on the stack. """ return self.is_valid and _has(self.properties, "B") @property def is_non_malleable(self) -> bool: """Answer whether every satisfaction can be made non-malleable.""" return _has(self.properties, "m") @property def is_signature_required(self) -> bool: """Answer whether every satisfaction requires a signature. False for a malleable expression, of which BIP379 says "s" tells nothing. """ return _has(self.properties, "s") @property def mixes_timelocks(self) -> bool: """Answer whether a satisfaction needs incompatible timelocks. A height lock and a time lock of the same kind in one branch: the script is valid and that branch is unspendable, which is the expression saying something other than what it means. """ return not _has(self.properties, "k") @property def key_expressions(self) -> tuple[KeyExpression, ...]: """Return every KEY expression of the tree, left to right.""" keys: list[KeyExpression] = [] stack = [self] while stack: node = stack.pop() keys.extend(node.keys) stack.extend(reversed(node.subs)) return tuple(keys)
[docs] def has_duplicate_keys(self, prv_keys: PrvKeys | None = None) -> bool: """Answer whether one public key appears more than once. Which BIP379's malleability analysis assumes away: a signature made for one check of a key satisfies every other check of it, so an expression naming a key twice has satisfactions the type system does not predict. The keys compared are the ones the KEY expressions derive at index 0, as Bitcoin Core's `KeyCompare` does, so the origin, the spelling of a hardened step and the wildcard do not make two spellings of a key two keys. In a tapscript the keys are compared by their x coordinate, the 32 bytes the script holds, so two keys of one x are one key. Bitcoin Core compares them with their parity, and accepts a leaf naming both. In P2WSH the keys are compared whole, parity included, as Core does. A hardened step is derived with `prv_keys`, as in `KeyExpression.sec`. A key it cannot derive is compared by its expression, origin and hardening symbol included, and never matches a key that can be derived: Bitcoin Core does the same since bitcoin/bitcoin@7b15e2cb44. A ``rawpkh()`` is a key by its hash160, which Core has no fragment for: a hash repeated, or the hash of another key of the expression, counts as the key repeated. """ _assert_prv_keys(prv_keys) keys = [_key_identity(key, prv_keys) for key in self.key_expressions] if self.context == TAPSCRIPT: # the key as the script writes it: its x coordinate keys = [key[1:] if isinstance(key, bytes) else key for key in keys] if len(set(keys)) != len(keys): return True # a ``rawpkh()`` names a key by its hash: the same hash twice, or # the hash of a key the expression names, is that key twice hashes = _raw_key_hashes(self) known = {hash160(key) for key in keys if isinstance(key, bytes)} return len(set(hashes)) != len(hashes) or not known.isdisjoint(hashes)
@property def spending_paths(self) -> tuple[SpendingPath, ...]: """Return every way to satisfy the expression, as `SpendingPath`. A disjunction is a path per branch and a conjunction joins its branches into one, so ``or_d(pk(A),and_v(v:pkh(B),older(144)))`` has two: A alone, and B alone once 144 blocks have passed. The keys a path needs are `Quorum`, Liana's `PathInfo`: a ``multi()``, a ``multi_a()`` and a ``thresh()`` of keys alone are one quorum, their threshold of their keys, and the keys a conjunction needs all of are one quorum of all of them. Paths that differ only in a quorum of one are one path, with a quorum of one of their keys joined, as Liana joins keys that each spend alone. A ``thresh()`` of anything else is a path for each way to choose the branches that are not keys, with a quorum of the keys for the rest of its threshold. A conjunction no single threshold states, such as two of three keys and three of six others, is a path of several quorums. A path no transaction can take, one needing two lock times of different kinds, is not listed: `mixes_timelocks` says the expression has one. A key the expression names twice is listed and counted twice, which `has_duplicate_keys` answers for. Where `mixes_timelocks` is true, the bound on the paths, below, can refuse an expression with fewer paths than the bound: a ``thresh()`` counts toward it a choice of its branches that only branches needing a lock time of another kind could complete. Refused for a node its own build refuses, as an unpickled one can hold once forged, as `Miniscript._assert_shape` says, or one holding a type or bounds its build does not compute, as `_assert_derived` says; for an expression `script` refuses, one built by hand without a type; for a ``rawpkh()``, which names its key by its hash alone; and past this library's bounds, which the refusal states: on the number of paths, which conjoined disjunctions multiply; on the pairs of paths a conjunction joins; and on the keys and hashes the whole call reads, a path holding every quorum and hash conjoined into it. Each conjunction counts its pairs and what they read, and each disjunction the paths it reads, before reading them, so a call past a bound is refused before that work. """ return _miniscript_paths(self, _Work()) @property def max_ops(self) -> int | None: """Return the ops a satisfaction may cost, None where none exists. The non-push op codes of the script plus the keys of every OP_CHECKMULTISIG that may be executed, which is what BIP141 counts against the 201 of a p2wsh spend. """ return _add(self._static_ops, self._ops.sat) @property def max_stack_items(self) -> int | None: """Return the witness elements a satisfaction needs, None where none. The initial stack of the script, which is what a p2wsh witness carries and what standardness bounds at a hundred. """ trace = self._stack[0] return None if trace is None else trace.net + self._leaves_a_value @property def max_exec_stack_items(self) -> int | None: """Return the elements the stack may hold while it runs, or None. The bound consensus puts at a thousand, and the one a tapscript can reach without reaching any other: nothing bounds the size of a tapscript witness, so the stack during execution is what bounds the script. """ trace = self._stack[0] return None if trace is None else trace.peak + self._leaves_a_value @property def _leaves_a_value(self) -> bool: """Answer whether the expression leaves a value: anything but a "V".""" return bool(self.properties & _t("BKW")) @property def max_witness_size(self) -> int | None: """Return the bytes a satisfying witness may take, None where none. The stack elements alone: what pushes them is the witness script, and a caller counting a whole input adds it. """ return self._witness.sat @property def max_witness_stack(self) -> tuple[int, ...] | None: """Return the size of every element of the largest satisfying witness. In witness order, the script excluded, and None where no satisfaction exists at all. What `max_witness_size` answers as one number, broken up: an estimator wants the elements, because it is the transaction's layout that turns them into bytes -- a count prefix and a length prefix each -- and only the transaction knows that. Estimated and not satisfied: every signature and preimage is assumed to turn up and every lock time to be met, so what comes back is the largest witness a signer could end up broadcasting rather than the one a particular caller can build now. That is what a fee wants to be computed from, and it is why this asks for nothing: a signature's size is the context's, not the key's. """ def up(_state: None, node: Miniscript, subs: list[_Inputs]) -> _Inputs: return _computed_input( node, subs, SpendContext(), {}, 0, "mainnet", None, estimate=True ) satisfaction = _tree_eval(self, None, lambda *_: None, up).sat if satisfaction.stack is None: return None return tuple(len(element) for element in satisfaction.stack) @property def is_satisfiable(self) -> bool: """Answer whether any satisfaction exists at all.""" return self.max_stack_items is not None @property def is_within_resource_limits(self) -> bool: """Answer whether a satisfaction is guaranteed to be spendable. The limits that depend on the satisfaction rather than on the script: the ops of a p2wsh spend and its hundred witness elements, both standardness, and the thousand elements consensus allows on the stack of a tapscript while it runs. """ if not self.is_valid: return False if self.context == TAPSCRIPT: items = self.max_exec_stack_items return items is None or items <= MAX_STACK_SIZE ops = self.max_ops elements = self.max_stack_items return (ops is None or ops <= MAX_OPS_PER_SCRIPT) and ( elements is None or elements <= _MAX_STANDARD_P2WSH_STACK_ITEMS )
[docs] def is_sane_subexpression(self, prv_keys: PrvKeys | None = None) -> bool: """Answer whether the expression means what it says, as a part. `prv_keys` is `has_duplicate_keys`'s. """ _assert_prv_keys(prv_keys) return ( self.is_within_resource_limits and self.is_non_malleable and not self.mixes_timelocks and not self.has_duplicate_keys(prv_keys) )
[docs] def is_sane(self, prv_keys: PrvKeys | None = None) -> bool: """Answer whether the expression is safe as a script on its own. Which adds to its parts being sane the two things only a whole script is asked: that it is a "B" of a size its context allows, and that it cannot be satisfied without a signature -- without one, an attacker is free to change the nSequence and the nLockTime the timelocks were checked against, and to rewrite the witness. `prv_keys` is `has_duplicate_keys`'s. """ _assert_prv_keys(prv_keys) return ( self.is_valid_top_level and self.is_sane_subexpression(prv_keys) and self.is_signature_required )
[docs] def insane_sub(self, prv_keys: PrvKeys | None = None) -> Miniscript | None: """Return the deepest subexpression that is not sane, or None. The deepest, because that is the one to name: an expression is insane where one of its parts is, so the part is the answer and the whole is the symptom. `prv_keys` is `has_duplicate_keys`'s. """ _assert_prv_keys(prv_keys) def up( _state: None, node: Miniscript, subs: list[Miniscript | None] ) -> Miniscript | None: for insane in subs: if insane is not None: return insane return None if node.is_sane_subexpression(prv_keys) else node return _tree_eval(self, None, lambda *_: None, up)
[docs] def script( self, index: int = 0, network: str = "mainnet", prv_keys: PrvKeys | None = None, ) -> bytes: """Return the script of the expression, its keys derived at `index`. The three parameters are `KeyExpression.sec`'s: an index for a ranged key, a network for the extended keys, and the private material that a hardened step needs and an xpub cannot take. """ _assert_prv_keys(prv_keys) if not self.is_valid: raise BTClibValueError("invalid miniscript") def up(verify: bool, node: Miniscript, subs: list[bytes]) -> bytes: return _fragment_script( node, subs, verify, index=index, network=network, prv_keys=prv_keys ) return _tree_eval(self, False, _verify_state, up)
[docs] def satisfy( self, signatures: Mapping[Octets, Octets] | None = None, spend: SpendContext | None = None, index: int = 0, network: str = "mainnet", prv_keys: PrvKeys | None = None, ) -> list[bytes]: """Return the witness elements that satisfy the expression. In witness order, the script itself excluded: what a p2wsh spend puts in front of the witness script, or a tapscript spend in front of the script and its control block. Non-malleable or refused, which is BIP379's algorithm and Bitcoin Core's default: of the stacks that would satisfy this script, the one reported is the one no third party could rewrite, and where every candidate is rewritable there is no answer -- a witness that is valid and malleable is worse than none, because it is one a relay can change under the transaction that carries it. A satisfaction with no signature in it is refused for the same reason: without one, the nLockTime and the nSequence the timelocks were checked against are a third party's to change too. `signatures` maps a public key to the signature made with it, as `Descriptor.satisfy` takes it; `spend` is the rest of what a satisfaction reads. The refusal says which of the two was short, because adding to either changes the answer: a preimage or a higher sequence can turn "none" into a satisfaction, and can also turn a non-malleable one malleable, which is why the two are separate messages. """ _assert_prv_keys(prv_keys) offered = ( {} if signatures is None else { bytes_from_octets(key): bytes_from_octets(signature) for key, signature in signatures.items() } ) context = SpendContext() if spend is None else spend def up(_state: None, node: Miniscript, subs: list[_Inputs]) -> _Inputs: return _computed_input( node, subs, context, offered, index, network, prv_keys, estimate=False, ) satisfaction = _tree_eval(self, None, lambda *_: None, up).sat if satisfaction.stack is None: err_msg = ( "no satisfaction with the signatures, preimages and lock times given" ) raise BTClibValueError(err_msg) if satisfaction.malleable or not satisfaction.has_sig: err_msg = ( "no non-malleable satisfaction: every witness that " "would satisfy it is one a third party could rewrite" ) raise BTClibValueError(err_msg) return list(satisfaction.stack)
@override def __str__(self) -> str: """Return the expression as BIP379 writes it. Which is not always as it was read: the sugared fragments are written sugared -- ``pk()`` for ``c:pk_k()``, ``t:`` for ``and_v(X,1)``, ``l:`` and ``u:`` for the two ``or_i()`` with a ``0``, ``and_n()`` for ``andor(X,Y,0)`` -- so an expression built by hand or read from a script comes out in them too. Bitcoin Core's `ToString` writes the same, and the round trip through the script is what both keep. """ return _tree_eval(self, False, _wrapper_state, _fragment_text)
def _max_script_size(context: str) -> int: """Return the largest script the context allows.""" if context == TAPSCRIPT: return _MAX_TAPSCRIPT_SIZE return _MAX_STANDARD_P2WSH_SCRIPT_SIZE _State = TypeVar("_State") _Result = TypeVar("_Result") def _tree_eval( root: Miniscript, root_state: _State, down: Callable[[_State, Miniscript, int], _State], up: Callable[[_State, Miniscript, list[_Result]], _Result], ) -> _Result: """Compute a value for every node bottom-up, with a state handed down. Bitcoin Core's `TreeEval`, and iterative for its reason: a miniscript nests as deep as its script is long -- a tapscript may hold a thousand nested fragments -- and CPython's frame limit is not a rule about scripts. `down` computes the state of a child from its parent's, `up` the value of a node from its children's values and its own state. """ stack: list[tuple[Miniscript, int, _State]] = [(root, 0, root_state)] results: list[_Result] = [] while stack: node, expanded, state = stack[-1] if expanded < len(node.subs): stack[-1] = (node, expanded + 1, state) stack.append((node.subs[expanded], 0, down(state, node, expanded))) continue stack.pop() cut = len(results) - len(node.subs) subs = results[cut:] del results[cut:] results.append(up(state, node, subs)) return results[0] def _verify_state(verify: bool, node: Miniscript, index: int) -> bool: """Answer whether a subexpression's script is followed by an OP_VERIFY. Which is what lets ``v:`` cost nothing: the last op code of the argument becomes its VERIFY form instead. The property is the argument's own for ``v:``, and inherited by the last subexpression of an ``and_v()`` and by the argument of an ``s:`` -- the two places where a subexpression's script ends where its parent's does. """ if node.fragment == "v:": return True if node.fragment == "s:" or (node.fragment == "and_v" and index == 1): return verify return False # every combinator's script as BIP379's translation table gives it: its # own op codes, and an integer where a subexpression's script goes. The # fragments left out are the ones whose last op code has a VERIFY form to # take -- ``c:``, multi(), multi_a() and thresh() -- and ``v:``, which is # that transformation _SCRIPT_TEMPLATES: dict[str, tuple[str | int, ...]] = { "a:": ("OP_TOALTSTACK", 0, "OP_FROMALTSTACK"), "s:": ("OP_SWAP", 0), "d:": ("OP_DUP", "OP_IF", 0, "OP_ENDIF"), "j:": ("OP_SIZE", "OP_0NOTEQUAL", "OP_IF", 0, "OP_ENDIF"), "n:": (0, "OP_0NOTEQUAL"), "and_v": (0, 1), "and_b": (0, 1, "OP_BOOLAND"), "or_b": (0, 1, "OP_BOOLOR"), "or_c": (0, "OP_NOTIF", 1, "OP_ENDIF"), "or_d": (0, "OP_IFDUP", "OP_NOTIF", 1, "OP_ENDIF"), "or_i": ("OP_IF", 0, "OP_ELSE", 1, "OP_ENDIF"), "andor": (0, "OP_NOTIF", 2, "OP_ELSE", 1, "OP_ENDIF"), } def _sec( node: Miniscript, key: KeyExpression, index: int, network: str, prv_keys: PrvKeys | None, ) -> bytes: """Return the public key as the context writes it: x-only, or SEC.""" sec = key.sec(index, network, prv_keys) return sec[1:] if node.context == TAPSCRIPT else sec def _plain_data(node: Miniscript) -> bytes: """Return the bytes a node's data holds, through none of its methods. What the script pushes and a spending path holds: `serialize` sizes a push by `len()`, as a caller may size a path's digest, and a subclass answers `len()` as it likes. """ return bytes.__getitem__(node.data, slice(None)) # a return per branch is past ruff's limit on return statements def _leaf_fragment_script( # sourcery skip: lift-return-into-if node: Miniscript, verify: bool, index: int, network: str, prv_keys: PrvKeys | None, ) -> ScriptList: """Return the commands of a fragment that has no subexpressions.""" fragment = node.fragment commands: ScriptList if fragment in {"0", "1"}: commands = [f"OP_{fragment}"] elif fragment == "pk_k": commands = [_sec(node, node.keys[0], index, network, prv_keys)] elif fragment == "pk_h": key_hash = hash160(_sec(node, node.keys[0], index, network, prv_keys)) commands = ["OP_DUP", "OP_HASH160", key_hash, "OP_EQUALVERIFY"] elif fragment == "raw_pk_h": commands = ["OP_DUP", "OP_HASH160", _plain_data(node), "OP_EQUALVERIFY"] elif fragment == "older": commands = [*_pushed_number(node.threshold), "OP_CHECKSEQUENCEVERIFY"] elif fragment == "after": commands = [*_pushed_number(node.threshold), "OP_CHECKLOCKTIMEVERIFY"] elif fragment in _HASH_OP_CODES: commands = [ "OP_SIZE", *_pushed_number(32), "OP_EQUALVERIFY", _HASH_OP_CODES[fragment], _plain_data(node), "OP_EQUALVERIFY" if verify else "OP_EQUAL", ] else: commands = _multi_fragment_script(node, verify, index, network, prv_keys) return commands def _multi_fragment_script( node: Miniscript, verify: bool, index: int, network: str, prv_keys: PrvKeys | None, ) -> ScriptList: """Return the commands of a ``multi()`` or of a ``multi_a()``. The one op code of the first against one per key of the second, which is the whole difference between them: OP_CHECKMULTISIG pops the signatures it was built to pop, where OP_CHECKSIGADD counts every signature that verifies and the OP_NUMEQUAL compares the count. """ keys = [_sec(node, key, index, network, prv_keys) for key in node.keys] if node.fragment == "multi": return [ *_pushed_number(node.threshold), *keys, *_pushed_number(len(keys)), "OP_CHECKMULTISIGVERIFY" if verify else "OP_CHECKMULTISIG", ] commands: ScriptList = [keys[0], "OP_CHECKSIG"] for key in keys[1:]: commands += [key, "OP_CHECKSIGADD"] return [ *commands, *_pushed_number(node.threshold), "OP_NUMEQUALVERIFY" if verify else "OP_NUMEQUAL", ] def _fragment_script( node: Miniscript, subs: list[bytes], verify: bool, *, index: int, network: str, prv_keys: PrvKeys | None, ) -> bytes: """Return the script of one fragment, its subexpressions' being built.""" fragment = node.fragment if not node.subs: return serialize(_leaf_fragment_script(node, verify, index, network, prv_keys)) match fragment: case "c:": return subs[0] + serialize( ["OP_CHECKSIGVERIFY" if verify else "OP_CHECKSIG"] ) case "v:": # where the argument's last op code has a VERIFY form it has # already been written as that form, the argument having been # told it is verified; where it has none, "x", the OP_VERIFY # goes here return ( subs[0] + serialize(["OP_VERIFY"]) if _has(node.subs[0].properties, "x") else subs[0] ) case "thresh": script = subs[0] for sub in subs[1:]: script += sub + serialize(["OP_ADD"]) return script + serialize( [ *_pushed_number(node.threshold), "OP_EQUALVERIFY" if verify else "OP_EQUAL", ] ) case _: return b"".join( subs[part] if isinstance(part, int) else serialize([part]) for part in _SCRIPT_TEMPLATES[fragment] ) def _wrapper_state(state: bool, node: Miniscript, index: int) -> bool: """Answer whether a subexpression is written behind a wrapper. Which is what puts the colon in ``s:pk(K)``: the wrappers are written as a run of letters, and the colon separates that run from the fragment it wraps -- so it belongs to the innermost expression, and that expression is the one told it is wrapped. The three sugared fragments that stand for a wrapper are wrappers here too. """ return _is_wrapper(node) def _is_wrapper(node: Miniscript) -> bool: """Answer whether the node is written as a wrapper: a letter and a colon.""" if node.fragment in _WRAPPERS: return True if node.fragment == "and_v": return node.subs[1].fragment == "1" return node.fragment == "or_i" and "0" in ( node.subs[0].fragment, node.subs[1].fragment, ) def _raw_key_hashes(node: Miniscript) -> list[bytes]: """Return the hash of every ``rawpkh()`` among the fragments.""" hashes: list[bytes] = [] stack = [node] while stack: node = stack.pop() if node.fragment == "raw_pk_h": hashes.append(node.data) stack.extend(node.subs) return hashes def _has_raw_key_hash(node: Miniscript) -> bool: """Return True where a ``rawpkh()`` is among the fragments.""" return bool(_raw_key_hashes(node)) # how the fragment under a ``c:`` is written in the sugar of ``pk()``, # ``pkh()`` and ``rawpkh()`` _KEY_CALLS: dict[str, Callable[[Miniscript], str]] = { "pk_k": lambda node: f"pk({node.keys[0]})", "pk_h": lambda node: f"pkh({node.keys[0]})", "raw_pk_h": lambda node: f"rawpkh({node.data.hex()})", } def _sugared_text(node: Miniscript, subs: list[str], prefix: str) -> str | None: """Return the sugared spelling of a fragment, None where it has none. BIP379's five: ``pk()`` and ``pkh()`` for a ``c:`` over the two key fragments, ``t:`` for the ``and_v()`` that appends a ``1``, ``l:`` and ``u:`` for the ``or_i()`` that adds a ``0`` on either side, and ``and_n()`` for the ``andor()`` whose third branch is ``0``. Three of them are written as a function and take the colon a wrapped expression carries; the two written as a wrapper letter do not, that colon being the one their own letter shares with the letters in front of it. """ fragment = node.fragment if fragment == "c:" and node.subs[0].fragment in _KEY_CALLS: return prefix + _KEY_CALLS[node.subs[0].fragment](node.subs[0]) if fragment == "and_v" and node.subs[1].fragment == "1": return "t" + subs[0] if fragment == "or_i" and node.subs[0].fragment == "0": return "l" + subs[1] if fragment == "or_i" and node.subs[1].fragment == "0": return "u" + subs[0] if fragment == "andor" and node.subs[2].fragment == "0": return f"{prefix}and_n({subs[0]},{subs[1]})" return None def _fragment_text(wrapped: bool, node: Miniscript, subs: list[str]) -> str: """Return one fragment as text, its subexpressions' being written.""" fragment = node.fragment prefix = ":" if wrapped else "" sugared = _sugared_text(node, subs, prefix) if sugared is not None: return sugared if fragment in _WRAPPERS: return fragment[0] + subs[0] return prefix + _plain_text(node, subs) # a return per branch is past ruff's limit on return statements def _plain_text( # sourcery skip: lift-return-into-if node: Miniscript, subs: list[str] ) -> str: """Return a fragment written as its name and its arguments. Which arguments those are is the one thing that differs between the rows: a key, a number, a digest, a threshold and keys, a threshold and subexpressions, or subexpressions alone. """ fragment = node.fragment if fragment in {"0", "1"}: text = fragment elif fragment in {"pk_k", "pk_h"}: text = f"{fragment}({node.keys[0]})" elif fragment == "raw_pk_h": text = f"raw_pk_h({node.data.hex()})" elif fragment in {"older", "after"}: text = f"{fragment}({node.threshold})" elif fragment in _HASH_OP_CODES: text = f"{fragment}({_hash_argument(node)})" elif fragment in {"multi", "multi_a"}: keys = ",".join(str(key) for key in node.keys) text = f"{fragment}({node.threshold},{keys})" elif fragment == "thresh": text = f"thresh({node.threshold},{','.join(subs)})" else: text = f"{fragment}({','.join(subs)})" return text def _hash_argument(node: Miniscript) -> str: """Return the argument a hash fragment is written with. Its digest, or ``preimage(HEX)`` inside `descriptors.private_string` where the preimages it was handed hold, under the digest, a preimage that this fragment's own hash takes to it. One keyed by digest serves every fragment: a ``sha256()`` preimage filed under its digest is no preimage of a ``hash256()`` holding the same bytes, and that fragment is written with its digest. An entry that is no 32 bytes is refused, quoting none of it. """ preimages = _PRINTED_PREIMAGES.get() preimage = preimages.get(node.data) if preimages else None if preimage is None: return node.data.hex() assert_type(preimage, bytes, "preimage") if len(preimage) != _PREIMAGE_SIZE: err_msg = f"invalid {_label(node.fragment, _NAMES)}() preimage:" err_msg += f" {_PREIMAGE_SIZE} bytes expected" raise BTClibValueError(err_msg) if _HASH_FUNCTIONS[node.fragment](preimage) != node.data: return node.data.hex() return f"preimage({preimage.hex()})" def _key_identity(key: KeyExpression, prv_keys: PrvKeys | None) -> object: """Return what two KEY expressions are compared by. The public key at index 0, which is what Bitcoin Core compares. The expression itself where the key cannot be derived with `prv_keys`, or is no key at all, as a wallet-policy placeholder is. The network is that of the first participant with an extended key, whichever place it has. """ xkeys = [k.xkey for k in (key.participants or (key,)) if k.xkey] try: network = ( network_from_xkeyversion(BIP32KeyData.b58decode(xkeys[0]).version) if xkeys else "mainnet" ) return key.sec(0, network, prv_keys) except BTClibValueError: return str(key) def _sanitized(properties: frozenset[str]) -> frozenset[str]: """Return the properties, or none where they name no one basic type. The four basic types conflict, so an expression is a "B", a "V", a "K" or a "W" and never two of them; what the tables above return where their requirements are not met is the properties without any, which is not a type. Bitcoin Core's `SanitizeType`, whose other checks are invariants of those tables rather than answers about an expression. A malleable expression, one without "m", keeps neither "s", "f" nor "e": BIP379 says they tell nothing about it, and an API that returns them should return them false. """ if len(properties & _t("BVKW")) != 1: return _NONE return properties if _has(properties, "m") else properties - _t("sfe") # how a fragment's arguments are written, for the parser to read: the two # key fragments and their sugared forms, and the combinators that take # subexpressions rather than data _KEY_FRAGMENTS = ("pk", "pkh", "pk_k", "pk_h") # the same for a key known by its hash160 alone. The fragment under it, # ``raw_pk_h``, is read only through this sugar: no specification names it _RAW_KEY_FRAGMENTS = ("rawpkh",) _COMBINATORS = ("andor", "and_n", *_BINARY) # and every fragment whose arguments are not subexpressions, which is # what makes the name of one either a leaf or a word the language has not _LEAVES = ( *_KEY_FRAGMENTS, *_RAW_KEY_FRAGMENTS, *_HASHES, "older", "after", "multi", "multi_a", ) # the names a refusal prints as they are, by `_label`: every fragment, the # sugar and the wrappers that are not one, the sorted multisigs, and the # two spend contexts _NAMES = frozenset(_ARITY) | frozenset( ( *_LEAVES, *_COMBINATORS, "thresh", "l:", "u:", "t:", "sortedmulti", "sortedmulti_a", ) ) _CONTEXTS = (P2WSH, TAPSCRIPT) # what `_read_number` refuses a lock time with, as `_relayed` reads it _NUMBER_FAULTS = ( *( (fault, fault) for name in ("older", "after") for fault in ( f"invalid {name}() number: digits expected", f"invalid {name}() number: more than {_MAX_DIGITS} digits", ) ), ) # the states of `parse` and of `from_script`, both of which walk a stack # of them rather than recursing: an expression nests as deep as its script # is long. A state that builds a node is named by the fragment it builds, # so that one function can build any of them; these are the rest _WRAPPED_EXPR = "a wrapped expression" _EXPR = "an expression" _WRAP_T = "the t: wrapper" _WRAP_U = "the u: wrapper" _MORE_THRESH = "another thresh() argument" _COMMA = "," _CLOSE = ")" _NAME_CHARACTERS = frozenset("abcdefghijklmnopqrstuvwxyz_0123456789") class _CoreFallbackError(BTClibValueError): """A fault on which Core's miniscript parser stops without naming a key. Core's descriptor reader then names its own fallback for the position, which the descriptor reader here names too. A key Core refuses, and a type or sanity fault Core finds on the expression it built, are not one. The standalone parser keeps this diagnostic. """ class _TypeFaultError(BTClibValueError): """A miniscript the type system refuses, or one that is not a "B". Core's parser builds such a miniscript from its text all the same, so where Core's descriptor reader allows no miniscript it names the position instead, which the descriptor reader here names too. The standalone parser keeps this diagnostic. A thresh() past the bound on its arguments is the exception: `parse` refuses its type there without reading on, where Core can stop on a later fault. """ def _expression_end(text: str, pos: int, *, stop_at_close: bool) -> int: """Return the index one past the expression starting at `pos`. Which ends at the comma or the close bracket that is not inside a bracket of its own: a KEY expression may hold a ``musig()`` with commas of its own, and a subexpression may hold anything. A multisig stops at its matching parenthesis, leaving any trailing text for the caller, so its keys are read before that text is refused. """ depth = 0 for i in range(pos, len(text)): char = text[i] if char == "(": depth += 1 elif char == ")": if not depth: return i depth -= 1 if stop_at_close and not depth: return i + 1 elif char == "," and not depth: return i return len(text) def _leaf_end(text: str, pos: int) -> int: """Return the index where Core's ``Expr`` ends the text at `pos`. The argument of a key, hash or timelock fragment, and a tr() key or leaf. ``Expr`` counts a brace as a bracket: in ``pk(K){,pk(K2))`` the key is ``K){,pk(K2)``, so the key is what is refused. A multisig is cut by ``_expression_end`` instead, Core reading its keys up to the next comma. """ depth = 0 for i in range(pos, len(text)): char = text[i] if char in "({": depth += 1 elif char in ")}" and depth: depth -= 1 elif char in ",)}" and not depth: return i return len(text) class _Built(list[Miniscript]): """The fragments a parse has finished, refused once their sum is too large. Every fragment held ends up in the script the parse returns, so their sizes added are a lower bound of its size. Keeping the sum, and refusing as the fragment that passes the limit is added, bounds the work of a parse by the script size of its context, however the thresh() in it are nested: each is analysed in time quadratic in its arguments, and the sum is what no nesting escapes. The first fragment added without a type is kept, and refused by `assert_typed` once the parse has read the whole expression. Core's parser builds a fragment whatever its type, and its descriptor reader refuses an ill-typed one only once the parser has finished, so a later fault that stops the parser is the one Core names. The first fragment built without a type is the innermost one that failed, its arguments being built before it. """ def __init__(self, context: str) -> None: super().__init__() self._limit = _max_script_size(context) self._context = context self._total = 0 self._ill_typed: Miniscript | None = None @override def append(self, node: Miniscript) -> None: self._total += node.script_size if self._total > self._limit: err_msg = ( f"miniscript too large for {_label(self._context, _CONTEXTS)}: " f"at least {_position(self._total)} bytes of script" ) raise _CoreFallbackError(err_msg) if not node.properties and self._ill_typed is None: self._ill_typed = node super().append(node) def assert_typed(self) -> None: """Refuse the first fragment added without a type, if there is one.""" if self._ill_typed is not None: _assert_typed(self._ill_typed) @override def pop(self, index: SupportsIndex = -1) -> Miniscript: node = super().pop(index) self._total -= node.script_size return node def drop_last(self, count: int) -> tuple[Miniscript, ...]: """Remove and return the last `count` fragments.""" nodes = tuple(self[len(self) - count :]) del self[len(self) - count :] self._total -= sum(node.script_size for node in nodes) return nodes def _assert_typed(node: Miniscript) -> Miniscript: """Return the node, refusing one the type system does not allow. Asked of the first node built without a type, so that what is named is the innermost fragment that failed: the type of an expression is a function of its arguments' types, so an argument without a type gives its parent none, and the outermost fragment is only where that is noticed. """ if not node.properties: arguments = ", ".join( _quoted("".join(sorted(sub.properties))) for sub in node.subs ) err_msg = ( f"ill-typed miniscript: {_label(node.fragment, _NAMES)} over {arguments}" ) raise _TypeFaultError(err_msg) return _assert_fits(node) def _assert_fits(node: Miniscript) -> Miniscript: """Return the node, refusing one past the script size of its context. A forged node holds any context, so only the library's own is named. """ if node.script_size > _max_script_size(node.context): context = TAPSCRIPT if node.context == TAPSCRIPT else P2WSH err_msg = ( f"miniscript too large for {context}: " f"{_position(node.script_size)} bytes of script" ) raise _CoreFallbackError(err_msg) return node def _built(fragment: str, built: _Built, context: str) -> None: """Build a node from the arguments already built, and check its size. The sugared wrappers are built as what they stand for: ``t:X`` is ``and_v(X,1)``, ``u:X`` is ``or_i(X,0)`` and ``and_n(X,Y)`` is ``andor(X,Y,0)``, so nothing downstream has to know they were written the short way -- `str` writes them short again by recognizing the shape. """ arguments: tuple[Miniscript, ...] if fragment == _WRAP_T: arguments = (built.pop(), Miniscript("1", context)) fragment = "and_v" elif fragment == _WRAP_U: arguments = (built.pop(), Miniscript("0", context)) fragment = "or_i" elif fragment == "and_n": second = built.pop() arguments = (built.pop(), second, Miniscript("0", context)) fragment = "andor" else: count = 1 if fragment in _WRAPPERS else _ARITY[fragment] arguments = built.drop_last(count) built.append(_assert_fits(Miniscript(fragment, context, arguments))) def _read_wrappers( expression: str, pos: int, to_parse: list[tuple[str, int, int]], built: _Built, context: str, ) -> int: """Read the wrappers in front of an expression, and stack them. They are written as a run of letters ended by a colon, so what says there are none is the absence of that colon: `l:` and `u:` are the two that are not fragments of their own, and the first of them needs the ``0`` its ``or_i()`` puts on the left pushed before the argument is read. """ colon = None for i in range(pos + 1, len(expression)): if expression[i] == ":": colon = i break if expression[i] not in _NAME_CHARACTERS: break letters = expression[pos:colon] if colon else "" if "vv" in letters: # Core refuses it as it reads the wrappers, before the expression raise _CoreFallbackError("invalid miniscript wrappers: vv:") for letter in letters: if letter == "l": built.append(Miniscript("0", context)) to_parse.append(("or_i", 0, 0)) elif letter == "u": to_parse.append((_WRAP_U, 0, 0)) elif letter == "t": to_parse.append((_WRAP_T, 0, 0)) elif f"{letter}:" in _WRAPPERS: to_parse.append((f"{letter}:", 0, 0)) else: raise _CoreFallbackError(f"unknown miniscript wrapper: {_quoted(letter)}") to_parse.append((_EXPR, 0, 0)) return colon + 1 if colon else pos def _read_name(expression: str, pos: int) -> tuple[str, int]: """Return the name of the fragment at `pos`, and where it ends.""" end = pos while end < len(expression) and expression[end] in _NAME_CHARACTERS: end += 1 if end == pos: raise _CoreFallbackError("not a miniscript fragment: a name expected") return expression[pos:end], end def _read_key_fragment( name: str, argument: str, context: str, prv_keys: MutableMapping[str, str], ) -> Miniscript: """Return the node of a fragment whose argument is a KEY expression. ``pk()`` and ``pkh()`` are BIP379's sugar for a ``c:`` over the other two, and are built as that. The key is read as BIP380 defines one, with the two restrictions the context puts on it: uncompressed keys are unspendable inside a witness program, so neither context allows one, and a tapscript holds the 32 bytes of an x-only key. """ tapscript = context == TAPSCRIPT key = _parse_key( argument, prv_keys, x_only=tapscript, compressed=True, musig_allowed=tapscript, ) fragment = "pk_h" if name in {"pkh", "pk_h"} else "pk_k" node = Miniscript(fragment, context, keys=(key,)) return node if name.startswith("pk_") else Miniscript("c:", context, (node,)) def _read_raw_key_hash(argument: str, context: str) -> Miniscript: """Return the node of a ``rawpkh()``: ``pkh()`` with the hash160 alone. The argument is read as the descriptor ``rawpkh()`` reads it, whole bytes of hex and nothing else, and refused with its messages. The 20 bytes are the hash160 of the public key the context writes: of the 33-byte SEC key in a p2wsh script, and of the 32-byte x-only key in a tapscript, as `_leaf_fragment_script` hashes them for a ``pkh()``. """ if not _HEX.fullmatch(argument): raise BTClibValueError("Rawpkh hash is not hex") if len(argument) != 2 * _DATA_SIZE["raw_pk_h"]: raise BTClibValueError("160 bits digest expected") node = Miniscript("raw_pk_h", context, data=bytes.fromhex(argument)) return Miniscript("c:", context, (node,)) def _read_multi( name: str, argument: str, context: str, prv_keys: MutableMapping[str, str], ) -> Miniscript: """Return the node of a ``multi()`` or a ``multi_a()``: a k and keys. A bracket that leaves the arguments unpaired is refused after the keys Core reads before it, since Core counts no bracket in them. """ # `_split_arguments` refuses unpaired brackets alone, and the refusal # is raised after `suppress`, so that it chains nothing arguments = None with suppress(BTClibValueError): arguments = _split_arguments(argument) if arguments is None: unsplit_threshold, *unsplit_keys = argument.split(",") _read_keys_as_core(unsplit_threshold, unsplit_keys, context, prv_keys) raise _CoreFallbackError("unbalanced brackets") label = _label(name, _NAMES) if len(arguments) < 2: err_msg = f"{label}() takes a threshold and at least one key" raise _CoreFallbackError(err_msg) threshold, *keys = arguments if not _NUMBER.fullmatch(threshold): # Core reads the keys after a negative threshold _read_keys_as_core(threshold, argument.split(",")[1:], context, prv_keys) raise _CoreFallbackError(f"invalid {label}() threshold: digits expected") if _int64_read_as_core(threshold) is None: # Core reads no key after a threshold past an int64_t raise _CoreFallbackError(f"invalid {label}() threshold: past an int64_t") tapscript = context == TAPSCRIPT read = [] for key in keys: # Core refuses an empty key as structure, reading no key after it. if not key: raise _CoreFallbackError(f"empty key in {label}()") read.append( _parse_key( key, prv_keys, x_only=tapscript, compressed=True, musig_allowed=tapscript, ) ) # a threshold of too many digits, the number of keys, or a threshold # out of its bounds: the refusal is written from the threshold and the # keys read here, nothing of the text caught, and raised after the # `suppress` blocks, so that it chains nothing fault = f"invalid {label}() threshold: more than {_MAX_DIGITS} digits" with suppress(BTClibValueError): number = _int_from_digits(threshold, f"{label}() threshold") with suppress(BTClibValueError): return Miniscript(name, context, keys=tuple(read), threshold=number) fault = _multi_fault(name, number, len(read)) raise _CoreFallbackError(fault) def _multi_fault(name: str, threshold: int, count: int) -> str: """Return the refusal of a multisig node of `count` keys, as it refuses. The number of keys first, as the node checks it, then the threshold; any other refusal of a node built of keys `parse` read is the threshold's. """ label = _label(name, _NAMES) with suppress(BTClibValueError): _assert_key_number(name, count) if 1 <= threshold <= count: return f"invalid {label}() threshold" return f"invalid k in k-of-n {label}: {_quoted(threshold)}" return f"invalid number of keys in {label}: {_quoted(count)}" def _assert_read_number(fragment: str, number: int) -> None: """Refuse a number of a node as `parse` refuses its text when reading it. With `parse`'s message for the fragment, which quotes none of it: a long one may be a private key in decimal. A negative number is refused as `parse` refuses the ``-`` that `str` writes. Past that, ``older()`` and ``after()`` are refused past `_int_from_digits`'s bound, a multisig past an int64_t and then past that bound, and ``thresh()`` past an int64_t or below 1, as `_read_thresh_threshold` reads it before the subexpressions. A fragment `parse` reads no number for is refused past `_int_from_digits`'s bound, quoting none of it either; a wrapper is named ``c: wrapper``, as other refusals name one. """ label = _label(fragment, _NAMES) if fragment not in {"older", "after", "thresh", "multi", "multi_a"}: name = f"{label} wrapper" if fragment in _WRAPPERS else f"{label}()" _assert_few_digits(number, f"{name} number") return noun = "threshold" if fragment in {"multi", "multi_a"} else "number" if number < 0: raise BTClibValueError(f"invalid {label}() {noun}: digits expected") if fragment == "thresh" and number < 1: raise BTClibValueError(f"invalid thresh() threshold: {_quoted(number)}") if fragment not in {"older", "after"} and number >= 2**63: raise BTClibValueError(f"invalid {label}() threshold: past an int64_t") if fragment != "thresh": _assert_few_digits(number, f"{label}() {noun}") def _int64_read_as_core(text: str) -> int | None: """Return the int64_t Core reads in `text`, or None where it reads none. Core's ``ToIntegral<int64_t>`` is ``std::from_chars``: an optional "-", then digits, leading zeros read, and nothing past an int64_t. """ digits = text.removeprefix("-") if not _NUMBER.fullmatch(digits): return None significant = digits.lstrip("0") if len(significant) > 19: return None value = int(significant or "0") if digits != text: value = -value return value if -(2**63) <= value < 2**63 else None def _read_keys_as_core( threshold: str, keys: list[str], context: str, prv_keys: MutableMapping[str, str] ) -> None: """Read the keys of a multisig in its own context, as Core splits them. Core reads the threshold as an int64_t, and reads no key past one that is not. It then reads each key up to the next comma, counting no bracket, and stops at an empty key. """ if _int64_read_as_core(threshold) is None: return tapscript = context == TAPSCRIPT for key in keys: if not key: return _parse_key( key, prv_keys, x_only=tapscript, compressed=True, musig_allowed=tapscript ) def _read_unclosed_multisig_keys( fragment: str, context: str, prv_keys: MutableMapping[str, str] ) -> None: """Read the keys Core reads in a multisig with no closing parenthesis. Core reads the threshold and then the keys, as `_multisig_keys_as_core` cuts them, before it finds that the fragment does not close. """ start = fragment.index("(") end = _expression_end(fragment, start, stop_at_close=True) if fragment[end - 1 : end] == ")": return threshold, _, keys = fragment[start + 1 :].partition(",") _read_keys_as_core(threshold, _multisig_keys_as_core(keys), context, prv_keys) def _multisig_keys_as_core(text: str) -> list[str]: """Return the keys of a multisig as Core's ``FindNextChar`` cuts them. A key ends at the next comma, counting no bracket. Once no comma is left, the first ")" ends the last key, and with no ")" Core reads no last key. """ keys: list[str] = [] while (comma := text.find(",")) != -1: keys.append(text[:comma]) text = text[comma + 1 :] close = text.find(")") return keys if close == -1 else [*keys, text[:close]] def _read_number(name: str, argument: str) -> int: """Return the number a fragment's argument is, refusing anything else. Digits and nothing else. Leading zeros are read, as Bitcoin Core reads them (its ``ToIntegral`` is ``std::from_chars``): ``older(0144)`` is ``older(144)``, and prints back as that. A sign is refused. ``from_chars`` refuses ``+``, and reads ``-`` only to give a number below 1, which every fragment that takes one refuses. """ label = _label(name, _NAMES) if not _NUMBER.fullmatch(argument): raise _CoreFallbackError(f"invalid {label}() number: digits expected") # `_int_from_digits`'s own text, where `_NUMBER_FAULTS` knows it, and # raised after the `except` block, so that it chains nothing fault: str try: return _int_from_digits(argument, f"{label}() number") except BTClibValueError as error: fault = _relayed(error, _NUMBER_FAULTS, f"invalid {label}() number") raise _CoreFallbackError(fault) def _read_preimage( name: str, text: str, context: str, preimages: MutableMapping[bytes, bytes] ) -> Miniscript: """Return the node of a hash fragment read from ``preimage(HEX)``. `text` is what the parentheses of ``preimage()`` enclose. The node holds the digest, and the preimage goes to the caller's `preimages` under it. The hex is read as a digest's is, whole bytes and no whitespace, and is of the one size BIP379 satisfies a hash fragment with. The refusals quote none of it: a preimage is a secret. """ if not _HEX.fullmatch(text): raise BTClibValueError( f"invalid {_label(name, _NAMES)}() preimage: hex expected" ) if len(text) != 2 * _PREIMAGE_SIZE: err_msg = f"invalid {_label(name, _NAMES)}() preimage:" err_msg += f" {_PREIMAGE_SIZE} bytes expected" raise BTClibValueError(err_msg) preimage = bytes.fromhex(text) digest = _HASH_FUNCTIONS[name](preimage) preimages[digest] = preimage return Miniscript(name, context, data=digest) def _read_leaf( name: str, argument: str, context: str, prv_keys: MutableMapping[str, str], preimages: MutableMapping[bytes, bytes], ) -> Miniscript: """Return the node of a fragment whose arguments are not subexpressions.""" if name in _KEY_FRAGMENTS: return _read_key_fragment(name, argument, context, prv_keys) if name in _RAW_KEY_FRAGMENTS: return _read_raw_key_hash(argument, context) if name in _HASH_OP_CODES: if argument.startswith("preimage(") and argument.endswith(")"): text = argument[len("preimage(") : -1] return _read_preimage(name, text, context, preimages) if not _HEX.fullmatch(argument): raise _CoreFallbackError( f"invalid {_label(name, _NAMES)}() hash: hex expected" ) # the size `bytes_from_octets` refuses, with its text, asked # first: btclib's refusal is not chained size = _DATA_SIZE[name] if len(argument) != 2 * size: err_msg = f"invalid size: {len(argument) // 2} bytes instead of {size}" raise _CoreFallbackError(err_msg) digest = bytes_from_octets(argument, size) return Miniscript(name, context, data=digest) if name in {"older", "after"}: # `_read_number`'s own text, where `_NUMBER_FAULTS` knows it, and # the number quoted where `_assert_number` refuses it: each raised # after its `except` block, so that it chains nothing fault: str label = _label(name, _NAMES) try: threshold = _read_number(name, argument) except BTClibValueError as error: fault = _relayed(error, _NUMBER_FAULTS, f"invalid {label}() number") else: with suppress(BTClibValueError): return Miniscript(name, context, threshold=threshold) fault = f"invalid {label}() value: {_quoted(threshold)}" raise _CoreFallbackError(fault) return _read_multi(name, argument, context, prv_keys) def _read_thresh_threshold(argument: str) -> int: """Return the threshold of a thresh(), read as Core reads it. Core reads it as an int64_t, and refuses one it cannot read, or one below 1, before the subexpressions; one above their count is refused once they are read. """ if not _NUMBER.fullmatch(argument): raise _CoreFallbackError("invalid thresh() number: digits expected") threshold = _int64_read_as_core(argument) if threshold is None: raise _CoreFallbackError("invalid thresh() threshold: past an int64_t") if threshold < 1: raise _CoreFallbackError(f"invalid thresh() threshold: {_quoted(threshold)}") return threshold def _read_fragment( expression: str, pos: int, to_parse: list[tuple[str, int, int]], built: _Built, context: str, prv_keys: MutableMapping[str, str], preimages: MutableMapping[bytes, bytes], ) -> int: """Read one fragment, building it or stacking what its arguments need.""" name, pos = _read_name(expression, pos) if name in {"0", "1"}: built.append(Miniscript(name, context)) return pos if expression[pos : pos + 1] != "(": raise _CoreFallbackError("not a miniscript fragment: '(' expected after a name") if name in _COMBINATORS: to_parse.extend(((name, 0, 0), (_CLOSE, 0, 0))) for _ in range(2 if name == "andor" else 1): to_parse.extend(((_WRAPPED_EXPR, 0, 0), (_COMMA, 0, 0))) to_parse.append((_WRAPPED_EXPR, 0, 0)) return pos + 1 if name == "thresh": end = _expression_end(expression, pos + 1, stop_at_close=False) if expression[end : end + 1] != ",": raise _CoreFallbackError("thresh() takes a threshold and subexpressions") threshold = _read_thresh_threshold(expression[pos + 1 : end]) # the first argument is read before the state that loops over the # rest, so the count starts at one to_parse.append((_MORE_THRESH, 1, threshold)) to_parse.append((_WRAPPED_EXPR, 0, 0)) return end + 1 if name not in _LEAVES: raise _CoreFallbackError("unknown miniscript fragment") return _read_leaf_fragment( name, expression, pos, built, context, prv_keys, preimages ) def _read_leaf_fragment( name: str, expression: str, pos: int, built: _Built, context: str, prv_keys: MutableMapping[str, str], preimages: MutableMapping[bytes, bytes], ) -> int: """Read a fragment with no subexpression, and build it.""" own = "multi_a" if context == TAPSCRIPT else "multi" if name in {"multi", "multi_a"} and name != own: # Core refuses a multisig outside its own context before any key raise _CoreFallbackError(_multisig_context_fault(name)) if name == own: end = _expression_end(expression, pos, stop_at_close=True) else: end = _leaf_end(expression, pos) if expression[end - 1 : end] != ")": if name in {"multi", "multi_a"}: _read_unclosed_multisig_keys( expression[pos - len(name) :], context, prv_keys ) raise _CoreFallbackError(f"unbalanced brackets in {_label(name, _NAMES)}()") built.append( _assert_fits( _read_leaf( name, expression[pos + 1 : end - 1], context, prv_keys, preimages ) ) ) return end def _read_more_thresh( expression: str, pos: int, to_parse: list[tuple[str, int, int]], built: _Built, context: str, count: int, threshold: int, ) -> int: """Read another thresh() argument, or close the thresh() and build it.""" char = expression[pos : pos + 1] if char == ",": if count == _MAX_THRESH_ARGUMENTS: # Core types the expression once it has read every argument; # the rest is not read past this bound built.assert_typed() err_msg = f"thresh() takes at most {_MAX_THRESH_ARGUMENTS} arguments" raise BTClibValueError(err_msg) to_parse.extend(((_MORE_THRESH, count + 1, threshold), (_WRAPPED_EXPR, 0, 0))) return pos + 1 if char != ")": raise _CoreFallbackError("unbalanced brackets in thresh()") if threshold > count: err_msg = f"invalid thresh() threshold: {_quoted(threshold)} of {count}" raise _CoreFallbackError(err_msg) arguments = built.drop_last(count) built.append( _assert_fits(Miniscript("thresh", context, arguments, threshold=threshold)) ) return pos + 1 def _assert_no_trailing_text(expression: str, pos: int, built: _Built) -> None: """Refuse text after the miniscript that ends at `pos`, then its type. Core's descriptor reader hands its miniscript parser the text up to the first ",", ")" or "}" outside brackets, so text from one of them on is no fault of the miniscript's, and comes after its type. Here a "}" is read as any other text. Any other text stops Core's parser, before the type. """ err_msg = "trailing characters after the miniscript" if pos < len(expression) and expression[pos] not in ",)": raise _CoreFallbackError(err_msg) built.assert_typed() if pos < len(expression): raise BTClibValueError(err_msg)
[docs] def parse( expression: str, context: str = P2WSH, prv_keys: MutableMapping[str, str] | None = None, preimages: MutableMapping[bytes, bytes] | None = None, wifs: MutableMapping[str, str] | None = None, ) -> Miniscript: """Return the Miniscript of a BIP379 expression, in its context. Refused where the type system refuses it, naming the innermost fragment that failed once the expression is read or a thresh() reaches the bound on its arguments, and refused where the top-level expression is not a "B" of a size the context allows: a miniscript that is not both is not a script, and every caller of this wants a script. `prv_keys` is `descriptors.parse`'s: the mapping an extended private key is filed in, under the extended public key that replaces it, so that what a parsed expression holds is public. `preimages` is the same for a hash fragment's ``preimage(HEX)``, filed under its digest, as the module docstring says, and `wifs` for a WIF, filed as `descriptors.parse` files one. What a mapping was filed with stays in it where the expression is then refused. A `str`, a BIP379 expression being text, and refused as a type for the reason `descriptors.parse` gives: what was neither reached the slicing below and left as "object of type X has no len()". The context is one of the two BIP379 has, and `prv_keys` a mutable mapping or `None`, as `descriptors.parse` asks for the same pair: a context no fragment table knows was compared against `TAPSCRIPT`, found unequal, and every rule then read as the p2wsh one, so an expression was type-checked under a context that does not exist. """ assert_type(expression, str, "miniscript") _assert_valid_context(context) assert_type(prv_keys, (MutableMapping, type(None)), "prv_keys") assert_type(preimages, (MutableMapping, type(None)), "preimages") assert_type(wifs, (MutableMapping, type(None)), "wifs") if prv_keys is None: prv_keys = {} if preimages is None: preimages = {} with _wifs_filed_in({} if wifs is None else wifs): return _read_expression(expression, context, prv_keys, preimages)
def _read_expression( expression: str, context: str, prv_keys: MutableMapping[str, str], preimages: MutableMapping[bytes, bytes], ) -> Miniscript: """Return the Miniscript of a BIP379 expression, as `parse` refuses it.""" to_parse: list[tuple[str, int, int]] = [(_WRAPPED_EXPR, 0, 0)] built = _Built(context) pos = 0 while to_parse: state, count, threshold = to_parse.pop() if state == _WRAPPED_EXPR: pos = _read_wrappers(expression, pos, to_parse, built, context) elif state == _EXPR: pos = _read_fragment( expression, pos, to_parse, built, context, prv_keys, preimages ) elif state in {_COMMA, _CLOSE}: if expression[pos : pos + 1] != state: raise _CoreFallbackError(f"expected {state!r} in the miniscript") pos += 1 elif state == _MORE_THRESH: pos = _read_more_thresh( expression, pos, to_parse, built, context, count, threshold ) else: _built(state, built, context) _assert_no_trailing_text(expression, pos, built) node = built[0] _assert_script(node) return node def _assert_script(node: Miniscript) -> None: """Refuse an expression that is no script: untyped, too large, not a "B".""" _assert_typed(node) if not _has(node.properties, "B"): basic = "".join(sorted(node.properties & _t("BVKW"))) err_msg = f"not a miniscript script: a {_quoted(basic)}, not a B" raise _TypeFaultError(err_msg) def _op(name: str) -> int: """Return the byte of an op code, by the name `script` knows it by.""" return BYTE_FROM_OP_CODE_NAME[name][0] _OP_0 = _op("OP_0") _OP_1 = _op("OP_1") _OP_16 = _op("OP_16") _OP_PUSHDATA4 = _op("OP_PUSHDATA4") _OP_IF = _op("OP_IF") _OP_NOTIF = _op("OP_NOTIF") _OP_ELSE = _op("OP_ELSE") _OP_ENDIF = _op("OP_ENDIF") _OP_VERIFY = _op("OP_VERIFY") _OP_TOALTSTACK = _op("OP_TOALTSTACK") _OP_FROMALTSTACK = _op("OP_FROMALTSTACK") _OP_DUP = _op("OP_DUP") _OP_IFDUP = _op("OP_IFDUP") _OP_SWAP = _op("OP_SWAP") _OP_SIZE = _op("OP_SIZE") _OP_EQUAL = _op("OP_EQUAL") _OP_0NOTEQUAL = _op("OP_0NOTEQUAL") _OP_ADD = _op("OP_ADD") _OP_BOOLAND = _op("OP_BOOLAND") _OP_BOOLOR = _op("OP_BOOLOR") _OP_NUMEQUAL = _op("OP_NUMEQUAL") _OP_HASH160 = _op("OP_HASH160") _OP_CHECKSIG = _op("OP_CHECKSIG") _OP_CHECKSIGADD = _op("OP_CHECKSIGADD") _OP_CHECKMULTISIG = _op("OP_CHECKMULTISIG") _OP_CHECKLOCKTIMEVERIFY = _op("OP_CHECKLOCKTIMEVERIFY") _OP_CHECKSEQUENCEVERIFY = _op("OP_CHECKSEQUENCEVERIFY") # the fragment each hash op code belongs to, which is the table above read # the other way: a script is decoded by its op codes _HASH_FRAGMENTS = {_op(code): fragment for fragment, code in _HASH_OP_CODES.items()} # the four op codes that have a VERIFY form, and the form each one takes. # A script is decoded with them split apart -- the VERIFY becoming an op # code of its own -- because that is what the ``v:`` wrapper writes, and # what a fragment ending in one of these writes when it is verified _VERIFY_FORMS = { _op("OP_CHECKSIGVERIFY"): _OP_CHECKSIG, _op("OP_CHECKMULTISIGVERIFY"): _OP_CHECKMULTISIG, _op("OP_EQUALVERIFY"): _OP_EQUAL, _op("OP_NUMEQUALVERIFY"): _OP_NUMEQUAL, } # the states of the decoder that are not the name of a fragment to build _SINGLE = "one expression" _MAYBE_AND_V = "another and_v() argument, if there is one" _W_EXPR = "a W expression" _THRESH_BRANCH = "another thresh() branch" _THRESH_END = "the end of a thresh()" _ENDIF = "what an OP_ENDIF closes" _ENDIF_NOTIF = "an or_c() or an or_d()" _ENDIF_ELSE = "an or_i() or an andor()" # _SINGLE reads one B, V or K expression, and this reads a run of them: # and_v() writes its arguments one after the other, so a script does not # say where one ends and the next begins, and a run of them is what any # one of them may be _BKV = (_SINGLE, _MAYBE_AND_V) def _assert_minimal_push(op_code: int, data: bytes, encoded: bytes) -> None: """Refuse a push that is not the shortest way to write itself. BIP62's rule, and the reason a script has one miniscript and not several: a non-minimal push would decode to the same fragment and serialize back to different bytes, so the round trip would not hold. The interpreter enforces the same rule under MINIMALDATA, on the scripts it executes rather than on the ones it reads. """ if len(data) == 1 and (data[0] == 0x81 or 1 <= data[0] <= 16): err_msg = "non-minimal push: an op code pushes the same number" raise BTClibValueError(err_msg) if serialize([data]) != encoded: err_msg = f"non-minimal push of {_size(data)} bytes: op code {_quoted(op_code)}" raise BTClibValueError(err_msg) def _decomposed(script: bytes) -> list[tuple[int, bytes]]: """Return the op codes of a script and their data, last one first. Last first because that is the end a script is read from: a fragment is recognized by the op code that closes it -- an OP_ENDIF, an OP_BOOLOR, a CHECKSIG -- and what precedes it is then read as its arguments. An OP_n is answered as the push of the number it pushes, so that a threshold written as an op code and one written as a byte read the same; the four VERIFY forms are split into the op code and an OP_VERIFY, which is how the ``v:`` wrapper writes them; and a script that writes one of those four *as* two op codes is refused, there being one spelling of it. """ entries: list[tuple[int, bytes]] = [] spans = list(op_code_spans(script)) if not (spans and spans[-1][2] == len(script)) and script: raise BTClibValueError("not a script: a push runs past its end") for position, (op_code, start, stop) in enumerate(spans): if _OP_1 <= op_code <= _OP_16: entries.append((op_code, bytes([op_code - _OP_1 + 1]))) continue if op_code in _VERIFY_FORMS: entries.extend(((_VERIFY_FORMS[op_code], b""), (_OP_VERIFY, b""))) continue if _OP_0 < op_code <= _OP_PUSHDATA4: prefix = 1 if op_code <= 75 else 1 + 2 ** (op_code - 76) data = script[start + prefix : stop] _assert_minimal_push(op_code, data, script[start:stop]) entries.append((op_code, data)) continue if ( op_code in {_OP_CHECKSIG, _OP_CHECKMULTISIG, _OP_EQUAL, _OP_NUMEQUAL} and position + 1 < len(spans) and spans[position + 1][0] == _OP_VERIFY ): err_msg = f"non-minimal VERIFY after op code {_quoted(op_code)}" raise BTClibValueError(err_msg) entries.append((op_code, b"")) entries.reverse() return entries def _script_number(entry: tuple[int, bytes]) -> int | None: """Return the number an entry pushes, None where it pushes none. The four-byte bound and the minimal encoding of Bitcoin Core's CScriptNum: a longer push is not a number a script can compare, and a number written with a byte to spare is a second spelling of itself. """ op_code, data = entry if op_code == _OP_0: return 0 if not data or len(data) > 4: return None number = decode_num(data) return number if encode_num(number) == data else None def _key_from_sec(sec: bytes, context: str) -> KeyExpression: """Return the KEY expression of a public key read from a script.""" if context == TAPSCRIPT: # the even-y lift of BIP340, which is what those 32 bytes mean return KeyExpression(pub_key=b"\x02" + sec, x_only=True) return KeyExpression(pub_key=sec) @dataclass class _Decoder: """The stack machine that reads a script back into an expression. Bitcoin Core's `DecodeScript`, and a machine rather than a recursion for `_tree_eval`'s reason. `entries` are the op codes last one first, `pos` how far into them the walk has come, `to_parse` what is still expected, and `built` the expressions completed so far -- the last of them being the leftmost, since the script is read from its end. """ entries: list[tuple[int, bytes]] context: str key_hashes: dict[bytes, bytes] pos: int = 0 to_parse: list[tuple[str, int, int]] = field(default_factory=list) built: list[Miniscript] = field(default_factory=list) def _remaining(self) -> int: return len(self.entries) - self.pos def _op_code(self, offset: int) -> int: return self.entries[self.pos + offset][0] def _data(self, offset: int) -> bytes: return self.entries[self.pos + offset][1] def _expect(self, *states: str) -> None: """Stack what is expected next, the first named being read first.""" self.to_parse.extend((state, 0, 0) for state in reversed(states)) def _constant(self) -> Miniscript | None: fragment = {_OP_1: "1", _OP_0: "0"}.get(self._op_code(0)) if fragment is None: return None self.pos += 1 return Miniscript(fragment, self.context) def _key(self) -> Miniscript | None: """Read a pk_k(), which is a key, or a pk_h(), which is its hash. The hash is all a ``pk_h()`` leaves in the script, so reading one back needs the key from somewhere else: `key_hashes` is that somewhere, and without the entry the script cannot be read -- which is Bitcoin Core's answer too, its own inference asking a signing provider for the key behind the hash. """ data = self._data(0) if len(data) in {32, 33}: expected = 32 if self.context == TAPSCRIPT else 33 if len(data) != expected: err_msg = f"not a {_label(self.context, _CONTEXTS)} public key:" err_msg += f" {_size(data)} bytes" raise BTClibValueError(err_msg) self.pos += 1 return Miniscript( "pk_k", self.context, keys=(_key_from_sec(data, self.context),) ) if ( self._remaining() >= 5 and self._op_code(0) == _OP_VERIFY and self._op_code(1) == _OP_EQUAL and self._op_code(3) == _OP_HASH160 and self._op_code(4) == _OP_DUP and len(self._data(2)) == 20 ): key_hash = self._data(2) if key_hash not in self.key_hashes: err_msg = "no public key for a hash160 the script holds" raise BTClibValueError(err_msg) sec = self.key_hashes[key_hash] # the answer is checked against the question, which is the one # thing the script itself cannot say: a mapping filing a key # under a hash that is not its own reads the script into an # expression writing a different script back, and the inverse # this module documents would hold for every input but that one if hash160(sec) != key_hash: err_msg = "the key answered for a hash160 hashes to another" raise BTClibValueError(err_msg) # checked here as a pushed key is, before the even-y lift # prefixes a byte the caller never wrote and the refusal names it if self.context == TAPSCRIPT and len(sec) != 32: err_msg = f"not a {_label(self.context, _CONTEXTS)} public key:" err_msg += f" {_size(sec)} bytes" raise BTClibValueError(err_msg) self.pos += 5 key = _key_from_sec(sec, self.context) return Miniscript("pk_h", self.context, keys=(key,)) return None def _timelock(self) -> Miniscript | None: locks = { _OP_CHECKSEQUENCEVERIFY: "older", _OP_CHECKLOCKTIMEVERIFY: "after", } fragment = locks.get(self._op_code(0)) if fragment is None or self._remaining() < 2: return None number = _script_number(self.entries[self.pos + 1]) if number is None: return None self.pos += 2 return Miniscript(fragment, self.context, threshold=number) def _hash(self) -> Miniscript | None: """Read a hash fragment: the size check, the digest, the comparison.""" if self._remaining() < 7 or self._op_code(0) != _OP_EQUAL: return None if not ( self._op_code(3) == _OP_VERIFY and self._op_code(4) == _OP_EQUAL and _script_number(self.entries[self.pos + 5]) == 32 and self._op_code(6) == _OP_SIZE ): return None fragment = _HASH_FRAGMENTS.get(self._op_code(2)) if fragment is None or len(self._data(1)) != _DATA_SIZE[fragment]: return None digest = self._data(1) self.pos += 7 return Miniscript(fragment, self.context, data=digest) def _multi(self) -> Miniscript | None: if self._remaining() < 3 or self._op_code(0) != _OP_CHECKMULTISIG: return None if self.context == TAPSCRIPT: raise BTClibValueError("multi() is not allowed in a tapscript") count = _script_number(self.entries[self.pos + 1]) if count is None or self._remaining() < 3 + count: raise BTClibValueError("not a multi(): no number of keys") # Core's DecodeScript refuses the count before it reads a key _assert_key_number("multi", count) keys = [] for i in range(count): sec = self._data(2 + i) if len(sec) != 33: raise BTClibValueError(f"not a multi() key: {_size(sec)} bytes") keys.append(_key_from_sec(sec, self.context)) threshold = _script_number(self.entries[self.pos + 2 + count]) if threshold is None: raise BTClibValueError("not a multi(): no threshold") self.pos += 3 + count # the keys were read from the end of the script, so the last read # is the first the script pushes keys.reverse() return Miniscript("multi", self.context, keys=tuple(keys), threshold=threshold) def _multi_a(self) -> Miniscript | None: """Read a multi_a(): a CHECKSIG, then a CHECKSIGADD per further key.""" if self._remaining() < 4 or self._op_code(0) != _OP_NUMEQUAL: return None if self.context != TAPSCRIPT: raise BTClibValueError("multi_a() is only allowed in a tapscript") threshold = _script_number(self.entries[self.pos + 1]) if threshold is None: raise BTClibValueError("not a multi_a(): no threshold") keys = [] offset = 2 while True: if self._remaining() < offset + 2: raise BTClibValueError("not a multi_a(): the keys end the script") op_code = self._op_code(offset) if op_code not in {_OP_CHECKSIGADD, _OP_CHECKSIG}: raise BTClibValueError("not a multi_a(): no CHECKSIG for a key") sec = self._data(offset + 1) if len(sec) != 32: raise BTClibValueError(f"not a multi_a() key: {_size(sec)} bytes") keys.append(_key_from_sec(sec, self.context)) offset += 2 if op_code == _OP_CHECKSIG: break self.pos += offset keys.reverse() return Miniscript( "multi_a", self.context, keys=tuple(keys), threshold=threshold ) def _single(self) -> None: """Read one B, V or K expression, which is what every state expects.""" if not self._remaining(): raise BTClibValueError("the script ends where an expression is expected") for reader in ( self._constant, self._key, self._timelock, self._hash, self._multi, self._multi_a, ): node = reader() if node is not None: self.built.append(node) return self._combinator() def _combinator(self) -> None: """Read the op code that closes a fragment, and expect its arguments. The wrappers ask for one expression and not a run of them, which is what and_v() commutes with: ``c:and_v(X,Y)`` and ``and_v(X,c:Y)`` have the same script, and the second is the one that is valid. """ op_code = self._op_code(0) wrappers = {_OP_CHECKSIG: "c:", _OP_VERIFY: "v:", _OP_0NOTEQUAL: "n:"} if op_code in wrappers: self.pos += 1 self._expect(_SINGLE, wrappers[op_code]) return if op_code == _OP_ENDIF: self.pos += 1 self._expect(*_BKV, _ENDIF) return # and_b() and or_b() take a W as their second argument, and it is # the first one read: the script writes it last pairs = {_OP_BOOLAND: "and_b", _OP_BOOLOR: "or_b"} if op_code in pairs: self.pos += 1 self._expect(_W_EXPR, _SINGLE, pairs[op_code]) return number = ( _script_number(self.entries[self.pos + 1]) if self._remaining() >= 3 else None ) if op_code == _OP_EQUAL and number is not None: if number < 1: err_msg = f"invalid thresh() threshold: {_quoted(number)}" raise BTClibValueError(err_msg) self.pos += 2 self.to_parse.append((_THRESH_BRANCH, 0, number)) return err_msg = f"not a miniscript: op code {_quoted(op_code)} closes no fragment" raise BTClibValueError(err_msg) def _wrapped(self) -> None: """Read a W expression, which is an ``a:`` or an ``s:``.""" if not self._remaining(): raise BTClibValueError("the script ends where a W expression is expected") if self._op_code(0) == _OP_FROMALTSTACK: self.pos += 1 self._expect(*_BKV, "a:") else: self._expect(*_BKV, "s:") def _maybe_and_v(self) -> None: """Expect another and_v() argument where the script could hold one. The op codes that cannot end a well-formed expression are the ones that begin something else: what follows them is not an argument of an and_v() but the branch or the wrapper they belong to. """ ends = {_OP_IF, _OP_ELSE, _OP_NOTIF, _OP_TOALTSTACK, _OP_SWAP} if self._remaining() and self._op_code(0) not in ends: self._expect(*_BKV, "and_v") def _thresh_branch(self, count: int, threshold: int) -> None: if not self._remaining(): raise BTClibValueError("the script ends inside a thresh()") if self._op_code(0) == _OP_ADD: self.pos += 1 self.to_parse.append((_THRESH_BRANCH, count + 1, threshold)) self.to_parse.append((_W_EXPR, 0, 0)) else: self.to_parse.append((_THRESH_END, count + 1, threshold)) # every argument of a thresh() is "d", so none is an and_v() self.to_parse.append((_SINGLE, 0, 0)) def _thresh_end(self, count: int, threshold: int) -> None: if threshold > count: err_msg = f"invalid thresh() threshold: {_quoted(threshold)} of {count}" raise BTClibValueError(err_msg) # read backwards, so the last branch built is the first one written arguments = tuple(reversed(self.built[len(self.built) - count :])) del self.built[len(self.built) - count :] self.built.append( Miniscript("thresh", self.context, arguments, threshold=threshold) ) def _endif(self) -> None: """Read what an OP_ENDIF closed, once its first argument is read.""" if not self._remaining(): raise BTClibValueError("the script ends inside an OP_IF") op_code = self._op_code(0) if op_code == _OP_ELSE: self.pos += 1 self._expect(*_BKV, _ENDIF_ELSE) return if op_code == _OP_NOTIF: self.pos += 1 self.to_parse.append((_ENDIF_NOTIF, 0, 0)) return if op_code != _OP_IF: err_msg = f"not a miniscript: op code {_quoted(op_code)} before an OP_ENDIF" raise BTClibValueError(err_msg) if self._remaining() >= 2 and self._op_code(1) == _OP_DUP: self.pos += 2 self.to_parse.append(("d:", 0, 0)) return if ( self._remaining() >= 3 and self._op_code(1) == _OP_0NOTEQUAL and self._op_code(2) == _OP_SIZE ): self.pos += 3 self.to_parse.append(("j:", 0, 0)) return raise BTClibValueError("not a miniscript: an OP_IF wrapping nothing known") def _endif_notif(self) -> None: """Tell an or_d() from an or_c(): the first duplicates what it read.""" if not self._remaining(): raise BTClibValueError("the script ends inside an OP_NOTIF") if self._op_code(0) == _OP_IFDUP: self.pos += 1 self.to_parse.append(("or_d", 0, 0)) else: self.to_parse.append(("or_c", 0, 0)) # both take a "d" as their first argument, so neither is an and_v() self.to_parse.append((_SINGLE, 0, 0)) def _endif_else(self) -> None: """Tell an or_i() from an andor(), which tests its first argument.""" if not self._remaining(): raise BTClibValueError("the script ends inside an OP_ELSE") if self._op_code(0) == _OP_IF: self.pos += 1 self._build("or_i", 2) return if self._op_code(0) != _OP_NOTIF: err_msg = ( f"not a miniscript: op code {_quoted(self._op_code(0))} before OP_ELSE" ) raise BTClibValueError(err_msg) self.pos += 1 self.to_parse.append(("andor", 0, 0)) self.to_parse.append((_SINGLE, 0, 0)) def _wrap_stacked(self, fragment: str) -> None: """Consume the op code that moved the argument, then wrap it. A "W" expression is written around its argument -- ``a:`` puts it on the altstack and takes it back, ``s:`` swaps what is under it -- so the op code in front of the argument is read after it, the script being read backwards. """ expected = _OP_TOALTSTACK if fragment == "a:" else _OP_SWAP if not self._remaining() or self._op_code(0) != expected: err_msg = f"not a {fragment} wrapper: its first op code is missing" raise BTClibValueError(err_msg) self.pos += 1 self._build(fragment, 1) def _build(self, fragment: str, count: int) -> None: """Build a node from the arguments read, which are in reverse order.""" arguments = tuple(reversed(self.built[len(self.built) - count :])) del self.built[len(self.built) - count :] if fragment == "andor": # the script writes andor(X,Y,Z) as [X] NOTIF [Z] ELSE [Y] # ENDIF, so what is read last is X and what is read first is Y arguments = (arguments[0], arguments[2], arguments[1]) self.built.append(Miniscript(fragment, self.context, arguments)) def decode(self) -> Miniscript: """Read the whole script, and return the expression it is.""" # the top level, so the type is B: a W expression cannot be one self._expect(*_BKV) # what each state that reads the script does; the rest build a node readers = { _SINGLE: self._single, _MAYBE_AND_V: self._maybe_and_v, _W_EXPR: self._wrapped, _ENDIF: self._endif, _ENDIF_NOTIF: self._endif_notif, _ENDIF_ELSE: self._endif_else, } while self.to_parse: if self.built and not self.built[-1].is_valid: _assert_typed(self.built[-1]) state, count, threshold = self.to_parse.pop() reader = readers.get(state) if reader is not None: reader() elif state == _THRESH_BRANCH: self._thresh_branch(count, threshold) elif state == _THRESH_END: self._thresh_end(count, threshold) elif state in {"a:", "s:"}: self._wrap_stacked(state) elif state in _WRAPPERS: self._build(state, 1) else: self._build(state, _ARITY[state]) return self.built[0]
[docs] def from_script( script: Octets, context: str = P2WSH, key_hashes: Mapping[Octets, Octets] | None = None, ) -> Miniscript: """Return the Miniscript a script is, refusing one that is not miniscript. The other direction of `Miniscript.script`, and its inverse: what this returns writes back the very bytes it was read from, so a wallet handed a witness script can say what spends it. Not every script is a miniscript, and this is what answers the question -- a caller asking it catches the refusal. A key is read as Bitcoin Core's signer reads it, by its size and, in P2WSH, its prefix: a key that is no point is read too. The `str` of what holds it is then a text that `parse` refuses, as Core's parser does, and a descriptor refuses the key when built. `key_hashes` maps a hash160 to the public key behind it, for the one fragment that keeps no key in the script: ``pk_h()`` and its sugared ``pkh()`` write the hash alone, so a script holding one is readable only where the key is supplied. Bitcoin Core asks a signing provider the same question, and the answer is held to it: a key filed under a hash that is not its own is refused rather than read, that being the one input for which the inverse above did not hold. `reads_back` is this question asked without the refusal, for a caller that holds a script and wants to know whether any language reads it. """ script = bytes_from_octets(script) if len(script) > _max_script_size(context): err_msg = ( f"script too large for {_label(context, _CONTEXTS)}: {_size(script)} bytes" ) raise BTClibValueError(err_msg) decoder = _Decoder( _decomposed(script), context, { bytes_from_octets(key_hash): bytes_from_octets(key) for key_hash, key in (key_hashes or {}).items() }, ) with _script_keys(): node = decoder.decode() if decoder.pos != len(decoder.entries): err_msg = "not a miniscript: the script holds more than one expression" raise BTClibValueError(err_msg) _assert_script(node) return node
[docs] def reads_back( script: Octets, context: str = P2WSH, key_hashes: Mapping[Octets, Octets] | None = None, ) -> bool: """Whether a script is the miniscript it reads as. The round trip as a question: the script is read back into an expression and the expression writes a script, and the answer is whether those are the same bytes. What it is asked about is a script somebody else wrote -- a witness script off a psbt, the pre-image a wallet computes -- where "this is a 2-of-3 with a timelock" is an intention, and reading it back is the only thing that says the bytes agree with it. A script that is well formed by accident hashes to a perfectly good address, and nothing else notices. False is the answer wherever no language reads the script: not every script is a miniscript -- an ``OP_DROP`` where nothing drops, a quorum whose count does not match its keys -- and a caller wanting to know *what* is wrong with it calls `from_script` and reads the refusal. Written as the round trip rather than as "`from_script` accepted it", which is what it comes to today: the decoder refuses every second spelling of one expression -- a non-minimal push, a number with a byte to spare, a VERIFY written as two op codes -- and a key answered for the wrong hash, so what it accepts writes itself back. That is the claim, and this is the proof of it rather than a restatement. """ script = bytes_from_octets(script) try: node = from_script(script, context, key_hashes) except BTClibValueError: return False return node.script() == script
def _assert_sane(node: Miniscript, prv_keys: PrvKeys) -> None: """Refuse a miniscript that does not mean what it says. What Bitcoin Core requires of a miniscript before it accepts a descriptor holding one, and the message it answers with: the subexpression at fault and the first thing wrong with it. A caller that wants the answer rather than the refusal calls `is_sane(prv_keys)`: this question without the message. Satisfiability is asked beside sanity and not inside it, which is Bitcoin Core's split too: an expression with no satisfaction at all is a script nobody can spend, and every *part* of a sane expression may be one -- the ``0`` of an ``or_i()`` among them. """ if node.is_sane(prv_keys) and node.is_satisfiable: return insane = node.insane_sub(prv_keys) or node if not node.is_satisfiable: raise BTClibValueError("the miniscript is not satisfiable") if not insane.is_non_malleable: raise BTClibValueError( "a sub-expression is not sane: malleable witnesses exist" ) if insane is node and not insane.is_signature_required: err_msg = "a sub-expression is not sane: witnesses without signature exist" raise BTClibValueError(err_msg) if insane.mixes_timelocks: err_msg = ( "a sub-expression is not sane: it mixes timelocks " "expressed in blocks and in seconds" ) raise BTClibValueError(err_msg) if insane.has_duplicate_keys(prv_keys): raise BTClibValueError("a sub-expression is not sane: it repeats a public key") err_msg = ( "a sub-expression is not sane: satisfying it may exceed the resource limits" ) raise BTClibValueError(err_msg) def _assert_sane_script(node: Miniscript) -> None: """Refuse a hand-built miniscript that `parse` refuses without `prv_keys`. A descriptor holding one would not parse back from its own text. A key that is no point is the descriptor's to refuse. With no private key a key behind a hardened step is compared by its expression, as `Miniscript.has_duplicate_keys` says, so `parse` with `prv_keys` may still refuse what this accepts. """ _assert_script(node) _assert_sane(node, {})
[docs] @dataclass(frozen=True) class SpendContext: """What a miniscript satisfaction reads beside the signatures. The signatures are `Descriptor.satisfy`'s own parameter and are not here: one source of truth for them, and this is the rest of what a satisfaction may need -- the preimage of a hash fragment, and the lock times the transaction being built will carry, which say whether an ``older()`` or an ``after()`` can be met at all. The four preimage mappings are the four of btclib-wallet's `PsbtIn`, field for field, and keyed the same way: the digest to the bytes that hash to it -- bytes and not "bytes or hex", where the signatures of `satisfy` take either, because these come from a psbt rather than from a keyboard. A psbt carries them because BIP174 gave them fields, which is what lets btclib-wallet's `psbt.miniscript_solver` build a context out of one. `sequence` is the input's own, `locktime` the transaction's, and `version` matters for the same reason it matters to the interpreter: BIP68's relative locks are enforced from version 2, so an ``older()`` in a version-1 transaction is a branch nothing can spend. """ sha256_preimages: Mapping[bytes, bytes] = field(default_factory=dict) hash256_preimages: Mapping[bytes, bytes] = field(default_factory=dict) ripemd160_preimages: Mapping[bytes, bytes] = field(default_factory=dict) hash160_preimages: Mapping[bytes, bytes] = field(default_factory=dict) locktime: int = 0 sequence: int = 0 version: int = 2 def _preimage(self, fragment: str, digest: bytes) -> bytes | None: """Return the preimage of a digest, None where the caller has none.""" preimage = { "sha256": self.sha256_preimages, "hash256": self.hash256_preimages, "ripemd160": self.ripemd160_preimages, "hash160": self.hash160_preimages, }[fragment].get(digest) # BIP379 allows a preimage of 32 bytes and no other size, which is # what makes the size of a satisfaction predictable; a mapping # holding another is a caller's mistake and not a missing preimage return None if preimage is None else bytes_from_octets(preimage, 32) def _after(self, value: int) -> bool: """Answer whether the transaction's lock time meets an ``after()``. BIP65's rule, which is what the interpreter enforces in `script.engine`: the two must be the same kind of lock time -- both block heights or both timestamps, either side of the 500000000 threshold -- and the transaction's must have reached the fragment's. The final sequence that would let a transaction ignore its own lock time is a refusal there and cannot be one here: `sequence` is what this context carries, and a caller that set it to 0xffffffff has asked for a transaction with no lock time at all. """ if (value >= LOCKTIME_THRESHOLD) != (self.locktime >= LOCKTIME_THRESHOLD): return False return value <= self.locktime and self.sequence != 0xFFFFFFFF def _older(self, value: int) -> bool: """Answer whether the input's sequence meets an ``older()``. BIP112's rule, again the interpreter's: from version 2, with the disable bit clear, the same unit on bit 22 -- blocks against 512-second intervals -- and a relative lock time at least the fragment's. """ if self.version < 2 or self.sequence & SEQUENCE_LOCKTIME_DISABLE_FLAG: return False if value & SEQUENCE_LOCKTIME_TYPE_FLAG != ( self.sequence & SEQUENCE_LOCKTIME_TYPE_FLAG ): return False return value & SEQUENCE_LOCKTIME_MASK <= self.sequence & SEQUENCE_LOCKTIME_MASK
[docs] @dataclass(frozen=True) class Quorum: """Any `threshold` of `keys` signing: Liana's `PathInfo`. One key is a quorum of one of one. The keys are KEY expressions, in the order the expression names them. A ``musig()`` is one key, whose participants are its `KeyExpression.participants`. Liana's `PathInfo`: https://github.com/wizardsardine/liana/blob/433ad8ca38377aa2df40b60274abdb54fb8f0c2a/liana/src/descriptors/analysis.rs#L174-L179 """ threshold: int keys: tuple[KeyExpression, ...] def __post_init__(self) -> None: """Refuse a field of the wrong type, and a threshold outside 1 to n.""" if not is_integer(self.threshold): err_msg = f"invalid threshold type: {_type_name(self.threshold)}" raise BTClibTypeError(err_msg) assert_type(self.keys, tuple, "keys") for key in self.keys: assert_type(key, KeyExpression, "key") if not 1 <= self.threshold <= len(self.keys): err_msg = f"invalid quorum: threshold not from 1 to {_size(self.keys)}" raise BTClibValueError(err_msg)
[docs] @dataclass(frozen=True) class SpendingPath: """One way to spend: who signs, what it waits for, what it reveals. Every one of `quorums` signs. One quorum is Liana's `PathInfo`, and several are a conjunction no single threshold states. `older` is the value of the ``older()`` the path waits for, which is also the nSequence that meets it, and `after` the value of the ``after()``, which is the nLockTime that meets it: None where the path waits for none. These are the timelocks Liana's `LianaPolicy` keys its recovery paths by, its primary path being the one with neither. `hashes` holds, for each preimage the path reveals, the hash fragment and the digest it holds. A path of `Miniscript.spending_paths` has its quorums in the order the expression names their first keys, and its hashes in the order the expression names them. `leaf` is the number of the ``tr()`` leaf the path spends, in the order `TrDescriptor.taproot_tree` lists the leaves, and None for every other path, the key path of a ``tr()`` included. Liana's `LianaPolicy`: https://github.com/wizardsardine/liana/blob/433ad8ca38377aa2df40b60274abdb54fb8f0c2a/liana/src/descriptors/analysis.rs#L473-L477 """ quorums: tuple[Quorum, ...] = () older: int | None = None after: int | None = None hashes: tuple[tuple[str, bytes], ...] = () leaf: int | None = None def __post_init__(self) -> None: """Refuse a field of the wrong type, and a value no path holds. A lock time is one an ``older()`` or an ``after()`` takes, a hash is one of the hash fragments with a digest of its size, and a leaf number is not negative. """ assert_type(self.quorums, tuple, "quorums") for quorum in self.quorums: assert_type(quorum, Quorum, "quorum") _assert_lock_time("older", self.older) _assert_lock_time("after", self.after) assert_type(self.hashes, tuple, "hashes") for hash_lock in self.hashes: _assert_hash_lock(hash_lock) if self.leaf is not None: if not is_integer(self.leaf): err_msg = f"invalid leaf type: {_type_name(self.leaf)}" raise BTClibTypeError(err_msg) if self.leaf < 0: raise BTClibValueError("negative leaf")
@dataclass(frozen=True) class _Input: """One witness stack that satisfies or dissatisfies an expression. `stack` is None where there is no such stack at all -- Bitcoin Core's Availability::NO, and BIP379's *(none)*. The three flags are what the non-malleable algorithm chooses by: whether the stack carries a signature, whether a third party could rewrite it, and whether it is one of the options BIP379 lists as unnecessary. `size` is the witness bytes it takes, which is what breaks a tie. """ stack: tuple[bytes, ...] | None has_sig: bool = False malleable: bool = False non_canonical: bool = False size: int = 0 def _element(data: bytes) -> _Input: """Return a one-element stack, its length prefix counted.""" return _Input((data,), size=len(data) + 1) _NO_WITNESS = _Input(None) _NO_PUSHES = _Input(()) _ZERO_PUSH = _element(b"") _ONE_PUSH = _element(b"\x01") # the dissatisfaction of a hash fragment: any 32 bytes that are not the # preimage, and malleable by construction -- a third party can put other # 32 bytes there, which is why BIP379 rules it out of a non-malleable # satisfaction rather than merely listing it _ZERO32_PUSH = _Input((bytes(32),), malleable=True, size=33) def _both(first: _Input, second: _Input) -> _Input: """Return the stack that is the first followed by the second. Which is Bitcoin Core's ``+``: the elements of one and then of the other, and every flag of either. Absent where either is absent -- a satisfaction needing two things has neither if one is missing. """ if first.stack is None or second.stack is None: return _NO_WITNESS return _Input( first.stack + second.stack, has_sig=first.has_sig or second.has_sig, malleable=first.malleable or second.malleable, non_canonical=first.non_canonical or second.non_canonical, size=first.size + second.size, ) def _better(first: _Input, second: _Input) -> _Input: """Return the one of two stacks a non-malleable satisfaction takes. BIP379's algorithm, as Bitcoin Core's ``|`` implements it: a stack without a signature is one an attacker can produce too, so where only one option carries a signature the other is what a malleator would use and is therefore the one to report; where neither does, both are malleable, because either could be swapped for the other; where both do, the non-malleable one wins, and between two of a kind the smaller witness does. """ if first.stack is None: return second if second.stack is None: return first if first.has_sig != second.has_sig: return second if first.has_sig else first if not first.has_sig: return replace(first if first.size <= second.size else second, malleable=True) if first.malleable != second.malleable: return second if first.malleable else first return first if first.size <= second.size else second # which of two candidate stacks a walk keeps: `_better` for the witness a # signer will build, `_larger` for how large one can get _Choice = Callable[["_Input", "_Input"], "_Input"] def _larger(first: _Input, second: _Input) -> _Input: """Return the larger of two stacks, which is what an estimate keeps. Where `_better` answers "which of these will be built", this answers "how large can this get": the malleability rules of the first are about choosing a witness and say nothing about the size of the one a signer ends up with when the cheap branch is the one that is shut. It is what makes this the same number Bitcoin Core's `MaxSatSize` reports, which is the static bound of `max_witness_size` and not the satisfaction of any particular spend. """ if first.stack is None: return second if second.stack is None: return first # a malleable option is one no satisfaction of this library builds, so # its size bounds nothing that will be broadcast: this is the one rule # of `_better` an estimate keeps, and it is what makes the answer agree # with `max_witness_size`, whose own tables count the canonical # satisfactions alone if first.malleable != second.malleable: return second if first.malleable else first return first if first.size >= second.size else second @dataclass(frozen=True) class _Inputs: """The best stack for satisfying an expression, and for dissatisfying it.""" sat: _Input dsat: _Input def _assumed_signature(node: Miniscript) -> _Input: """Return the stack of a signature the caller does not have yet. Which is what an estimate is made of: the largest signature the context takes, in bytes that are not a signature and never leave this module -- only their length is read. Nothing marks them as assumed, and nothing has to: an estimate assumes every signature and a satisfaction assumes none, so the two never compare a real stack against a supposed one. That is the whole of what Bitcoin Core's Availability::MAYBE arbitrates, and why there is no third state here. """ return replace(_element(bytes(_signature_size(node.context))), has_sig=True) def _raw_key_input(node: Miniscript, *, estimate: bool) -> _Inputs: """Return the stacks of a ``raw_pk_h()``, whose key is not known. The key is on the stack beside the signature, so neither a satisfaction nor a dissatisfaction can be made without it: there is none, and an estimate assumes both, as it does every signature. """ if not estimate: return _Inputs(_NO_WITNESS, _NO_WITNESS) key = _element(bytes(_pub_key_size(node.context))) return _Inputs(_both(_assumed_signature(node), key), _both(_ZERO_PUSH, key)) def _key_input( node: Miniscript, signatures: Mapping[bytes, bytes], index: int, network: str, prv_keys: PrvKeys | None, *, estimate: bool, ) -> _Inputs: """Return the stacks of a ``pk_k()`` or a ``pk_h()``. A ``pk_h()`` puts the key on the stack beside the signature, the script holding its hash alone; both are dissatisfied by the empty push where a signature belongs. """ # the 33-byte form for the lookup, whatever the script writes: a # signer hands back a signature under either spelling of a taproot key, # which is what `_offered_signature` answers for if node.fragment == "raw_pk_h": return _raw_key_input(node, estimate=estimate) sec = node.keys[0].sec(index, network, prv_keys) offered = _offered_signature(signatures, sec, x_only=node.context == TAPSCRIPT) if offered is not None: signature = replace(_element(offered), has_sig=True) else: signature = _assumed_signature(node) if estimate else _NO_WITNESS if node.fragment == "pk_k": return _Inputs(signature, _ZERO_PUSH) key = _element(sec[1:] if node.context == TAPSCRIPT else sec) return _Inputs(_both(signature, key), _both(_ZERO_PUSH, key)) def _multi_input( node: Miniscript, signatures: Mapping[bytes, bytes], index: int, network: str, prv_keys: PrvKeys | None, choose: _Choice, *, estimate: bool, ) -> _Inputs: """Return the stacks of a ``multi()`` or a ``multi_a()``. `reached[j]` is the best stack carrying j signatures of the keys read so far, which is the only way to choose: any k of the n keys satisfy, and which k are available is not known until they have been asked for. The two differ in what an unused key costs -- nothing to OP_CHECKMULTISIG, which pops only the signatures it is given, and an empty push to OP_CHECKSIGADD, which counts them all -- and in the order, the first key's signature being on top for one and at the bottom for the other. """ multi_a = node.fragment == "multi_a" keys = list(reversed(node.keys)) if multi_a else list(node.keys) # OP_CHECKMULTISIG pops one element more than it was given, which is # the empty push every satisfaction of it starts with reached = [_NO_PUSHES if multi_a else _ZERO_PUSH] for key in keys: sec = key.sec(index, network, prv_keys) offered = _offered_signature(signatures, sec, x_only=node.context == TAPSCRIPT) if offered is not None: signature = replace(_element(offered), has_sig=True) else: signature = _assumed_signature(node) if estimate else _NO_WITNESS unused = _ZERO_PUSH if multi_a else _NO_PUSHES following = [_both(reached[0], unused)] following.extend( choose(_both(reached[j], unused), _both(reached[j - 1], signature)) for j in range(1, len(reached)) ) following.append(_both(reached[-1], signature)) reached = following if multi_a: # one element per key, so dissatisfying is satisfying none of them return _Inputs(reached[node.threshold], reached[0]) # and k+1 empty pushes dissatisfy a multi(), the threshold being how # many signatures OP_CHECKMULTISIG will look for dissatisfaction = _ZERO_PUSH for _ in range(node.threshold): dissatisfaction = _both(dissatisfaction, _ZERO_PUSH) return _Inputs(reached[node.threshold], dissatisfaction) def _leaf_input( node: Miniscript, spend: SpendContext, signatures: Mapping[bytes, bytes], index: int, network: str, prv_keys: PrvKeys | None, choose: _Choice, *, estimate: bool, ) -> _Inputs: """Return the stacks of a fragment with no subexpressions. Estimating, the three things a caller may not have are assumed: a signature, a preimage, and a transaction whose lock times meet the fragment. That is what makes the answer an upper bound over the branches rather than the spend of one of them. """ fragment = node.fragment if fragment == "0": return _Inputs(_NO_WITNESS, _NO_PUSHES) if fragment == "1": return _Inputs(_NO_PUSHES, _NO_WITNESS) if fragment in {"pk_k", "pk_h", "raw_pk_h"}: return _key_input(node, signatures, index, network, prv_keys, estimate=estimate) if fragment in {"older", "after"}: met = ( spend._older(node.threshold) if fragment == "older" else spend._after(node.threshold) ) # a lock time is met by the transaction or it is not: nothing goes # on the stack either way, and nothing dissatisfies one. # Estimating, it is taken as met *and* as unknown -- the branch may # be the one taken, and a caller is owed the larger of the two satisfaction = _NO_PUSHES if estimate or met else _NO_WITNESS return _Inputs(satisfaction, _NO_WITNESS) if fragment in _HASH_OP_CODES: preimage = spend._preimage(fragment, node.data) if preimage is not None: satisfaction = _element(preimage) elif estimate: # BIP379 allows a preimage of 32 bytes and no other size, so # its size is known without the preimage satisfaction = _element(bytes(32)) else: satisfaction = _NO_WITNESS return _Inputs(satisfaction, _ZERO32_PUSH) return _multi_input( node, signatures, index, network, prv_keys, choose, estimate=estimate ) def _wrapper_input(node: Miniscript, x: _Inputs) -> _Inputs: """Return the stacks of a wrapped expression, from its argument's.""" fragment = node.fragment if fragment in {"a:", "s:", "c:", "n:"}: return x if fragment == "d:": # the element the OP_IF reads is part of the witness return _Inputs(_both(x.sat, _ONE_PUSH), _ZERO_PUSH) if fragment == "v:": return _Inputs(x.sat, _NO_WITNESS) # ``j:`` is dissatisfied by the empty push its OP_SIZE reads; where the # argument can also be dissatisfied without a signature, a third party # has two ways to dissatisfy this and the choice is malleable dissatisfiable = x.dsat.stack is not None and not x.dsat.has_sig return _Inputs(x.sat, replace(_ZERO_PUSH, malleable=dissatisfiable)) def _and_input(node: Miniscript, x: _Inputs, y: _Inputs, choose: _Choice) -> _Inputs: """Return the stacks of and_v() or and_b(). The arguments are written left to right and consume the stack from the top, so what satisfies the second is under what satisfies the first. """ if node.fragment == "and_v": # dissatisfying a "V" is impossible, so this stack is listed for # completeness and never chosen return _Inputs( _both(y.sat, x.sat), replace(_both(y.dsat, x.sat), non_canonical=True) ) overcomplete = choose( replace(_both(y.sat, x.dsat), malleable=True, non_canonical=True), replace(_both(y.dsat, x.sat), malleable=True, non_canonical=True), ) return _Inputs(_both(y.sat, x.sat), choose(_both(y.dsat, x.dsat), overcomplete)) def _or_input(node: Miniscript, x: _Inputs, z: _Inputs, choose: _Choice) -> _Inputs: """Return the stacks of one of the four disjunctions.""" fragment = node.fragment if fragment == "or_b": # satisfying both branches is overcomplete: either half can be # turned back into a dissatisfaction and the OP_BOOLOR still holds both = replace(_both(z.sat, x.sat), malleable=True, non_canonical=True) satisfaction = choose(choose(_both(z.dsat, x.sat), _both(z.sat, x.dsat)), both) return _Inputs(satisfaction, _both(z.dsat, x.dsat)) satisfaction = choose(x.sat, _both(z.sat, x.dsat)) if fragment == "or_c": return _Inputs(satisfaction, _NO_WITNESS) if fragment == "or_d": return _Inputs(satisfaction, _both(z.dsat, x.dsat)) # or_i() carries the branch selector in the witness, which is what its # OP_IF reads: OP_1 for the first branch and the empty push for the # second return _Inputs( choose(_both(x.sat, _ONE_PUSH), _both(z.sat, _ZERO_PUSH)), choose(_both(x.dsat, _ONE_PUSH), _both(z.dsat, _ZERO_PUSH)), ) def _andor_input(x: _Inputs, y: _Inputs, z: _Inputs, choose: _Choice) -> _Inputs: """Return the stacks of andor(X,Y,Z): X and Y, or else Z.""" return _Inputs( choose(_both(y.sat, x.sat), _both(z.sat, x.dsat)), choose( replace(_both(y.dsat, x.sat), non_canonical=True), _both(z.dsat, x.dsat), ), ) def _thresh_input(node: Miniscript, subs: list[_Inputs], choose: _Choice) -> _Inputs: """Return the stacks of a thresh(), over every way to reach its threshold. `reached[j]` is the best stack satisfying j of the branches read so far, read from the last one: the branches are written left to right and the last one's stack is at the bottom of the witness. Every count but the threshold dissatisfies, and every count but zero does it with more satisfactions than the OP_EQUAL wants -- overcomplete, so malleable and non-canonical, and never chosen while the all-zero dissatisfaction exists. """ reached = [_NO_PUSHES] for sub in reversed(subs): following = [_both(reached[0], sub.dsat)] following.extend( choose(_both(reached[j], sub.dsat), _both(reached[j - 1], sub.sat)) for j in range(1, len(reached)) ) following.append(_both(reached[-1], sub.sat)) reached = following dissatisfaction = _NO_WITNESS for count, reaching in enumerate(reached): if count == node.threshold: continue # every count but zero satisfies more branches than the OP_EQUAL # asks for: overcomplete, so malleable, and never chosen while the # all-dissatisfied stack exists stack = ( replace(reaching, malleable=True, non_canonical=True) if count else reaching ) dissatisfaction = choose(dissatisfaction, stack) return _Inputs(reached[node.threshold], dissatisfaction) def _computed_input( node: Miniscript, subs: list[_Inputs], spend: SpendContext, signatures: Mapping[bytes, bytes], index: int, network: str, prv_keys: PrvKeys | None, *, estimate: bool, ) -> _Inputs: """Return the best stacks of one fragment, from its subexpressions'.""" choose = _larger if estimate else _better if not subs: return _leaf_input( node, spend, signatures, index, network, prv_keys, choose, estimate=estimate ) if node.fragment in _WRAPPERS: return _wrapper_input(node, subs[0]) if node.fragment in {"and_v", "and_b"}: return _and_input(node, subs[0], subs[1], choose) if node.fragment == "andor": return _andor_input(subs[0], subs[1], subs[2], choose) if node.fragment == "thresh": return _thresh_input(node, subs, choose) return _or_input(node, subs[0], subs[1], choose) def _assert_lock_time(name: str, value: int | None) -> None: """Refuse a lock time no ``older()`` or ``after()`` takes, None aside.""" if value is None: return if not is_integer(value): raise BTClibTypeError(f"invalid {name} type: {_type_name(value)}") if not 1 <= value < _MAX_TIMELOCK: err_msg = f"invalid {name}() value: not from 1 to {_MAX_TIMELOCK - 1}" raise BTClibValueError(err_msg) def _assert_hash_lock(hash_lock: tuple[str, bytes]) -> None: """Refuse what is not a hash fragment with a digest of its size. The digest is sized by the bytes it holds, which no subclass's `__len__` answers. """ assert_type(hash_lock, tuple, "hash") if len(hash_lock) != 2: raise BTClibValueError("a hash is a fragment and a digest") fragment, digest = hash_lock assert_type(fragment, str, "hash fragment") assert_type(digest, bytes, "digest") if fragment not in _HASH_OP_CODES: raise BTClibValueError("unknown hash fragment") if bytes.__len__(digest) != _DATA_SIZE[fragment]: err_msg = ( f"{_label(fragment, _NAMES)} takes {_DATA_SIZE[fragment]} bytes of data" ) raise BTClibValueError(err_msg) # the most paths `Miniscript.spending_paths` lists, or any of its branches # does on the way. A limit of this library and not of the protocol, as # `_MAX_THRESH_ARGUMENTS` is: the paths of conjoined disjunctions multiply, # so a script of a few hundred bytes can have more than anybody reads _MAX_SPENDING_PATHS = 1000 # the most pairs of paths a conjunction joins, the work its bound on the # paths does not cap: a pair no transaction can take, or one giving a path # another pair gave, adds no path but is work all the same. Each side is # within `_MAX_SPENDING_PATHS`, so the pairs are within its square # already; this keeps a conjunction to a hundred times the paths it may # give rather than a thousand _MAX_PATH_PAIRS = 100_000 # the most one call of `Miniscript.spending_paths` reads, or a tr()'s # leaves together, as `_Work` counts it: the work the bounds on the paths # and on the pairs do not cap, since a path grows with every quorum and # hash conjoined into it, and the same paths can be joined again and # again. The value is what one conjunction alone reads where as many # pairs as the bound on the paths join two paths of a thousand each, a # multi_a() of its most keys being one. In a call, the paths a # conjunction joins were first read by what made them, so a call # reaches the bound sooner than that conjunction alone would _MAX_PATH_WORK = 2_000_000 def _assert_shapes(root: Miniscript) -> None: """Refuse a node of the tree that its own build refuses. A forged tree's nodes escape their builds, so each node is asked its type, and its subexpressions theirs, before `Miniscript._assert_shape` reads them. """ assert_type(root, Miniscript, "miniscript node") stack = [root] while stack: node = stack.pop() node._assert_arity() for sub in node.subs: assert_type(sub, Miniscript, "miniscript node") node._assert_shape() stack.extend(node.subs) def _assert_path_count(count: int) -> None: """Refuse a list of spending paths past `_MAX_SPENDING_PATHS`.""" if count > _MAX_SPENDING_PATHS: err_msg = f"more than {_MAX_SPENDING_PATHS} spending paths" raise BTClibValueError(err_msg) def _joined_older(first: int | None, second: int | None) -> tuple[bool, int | None]: """Return whether one nSequence meets two ``older()``, and which it meets. BIP68's relative lock is in blocks or in 512-second intervals, as bit 22 of the sequence says, so two of different units are met by no sequence. Two of one unit are met by the larger, compared on the bits BIP68 reads, and two equal there by the larger value, so that the path is the same in whichever order the two come. """ if first is None or second is None: return True, second if first is None else first if first & SEQUENCE_LOCKTIME_TYPE_FLAG != second & SEQUENCE_LOCKTIME_TYPE_FLAG: return False, None return True, max( first, second, key=lambda value: (value & SEQUENCE_LOCKTIME_MASK, value) ) def _joined_after(first: int | None, second: int | None) -> tuple[bool, int | None]: """Return whether one nLockTime meets two ``after()``, and which it meets. BIP65's lock is a height or a time, either side of 500000000, so two of different kinds are met by no lock time. """ if first is None or second is None: return True, second if first is None else first if (first >= LOCKTIME_THRESHOLD) != (second >= LOCKTIME_THRESHOLD): return False, None return True, max(first, second) class _Work: """What one call reads of its spending paths, against `_MAX_PATH_WORK`. In `_path_size`'s units. A conjunction adds what its pairs read, and a disjunction the paths it asks, each before reading them. A ``tr()``'s leaves share one, so that the bound is on the call and not on a leaf. """ def __init__(self) -> None: self.read = 0 def add(self, size: int) -> None: """Count what is about to be read, refusing past the bound.""" self.read += size if self.read > _MAX_PATH_WORK: err_msg = ( f"more than {_MAX_PATH_WORK} keys and hashes of spending paths to read" ) raise BTClibValueError(err_msg) def _miniscript_paths(root: Miniscript, work: _Work) -> tuple[SpendingPath, ...]: """Return `Miniscript.spending_paths`, counting what it reads in `work`.""" _assert_shapes(root) _assert_derived(root) if not root.is_valid: raise BTClibValueError("invalid miniscript") order = _Order(root, work) def up( _state: None, node: Miniscript, subs: list[list[SpendingPath]] ) -> list[SpendingPath]: return _computed_paths(node, subs, order) paths: list[SpendingPath] = _tree_eval(root, None, lambda *_: None, up) return tuple(paths) class _Order: """Where each key and each hash of an expression first stands. Every path is built in one form, whatever order the expression reaches its conditions in: a quorum's keys in the order the expression first names them, a path's quorums in the order of their keys, and its hashes in the order the expression first names them. Two ways to one path are then one path, which the bounds count once, and the form is already the order a caller reads. `work` is what the call has read, which every conjunction and disjunction of the expression adds to, and `kept` the paths the call has made, each with its hash, by their id. """ def __init__(self, root: Miniscript, work: _Work) -> None: self.work = work self.kept: dict[int, _Keyed] = {} self.keys: dict[KeyExpression, int] = {} for index, key in enumerate(root.key_expressions): self.keys.setdefault(key, index) self.hashes: dict[tuple[str, bytes], int] = {} self.locks: dict[ tuple[int | None, int | None, tuple[tuple[str, bytes], ...]], int ] = {} # depth first, left to right, as `key_expressions` reads the tree stack = [root] while stack: node = stack.pop() if node.fragment in _HASH_OP_CODES: self.hashes.setdefault((node.fragment, node.data), len(self.hashes)) stack.extend(reversed(node.subs)) def lock(self, path: SpendingPath) -> int: """Return a number for the path's locks, the same for the same locks. So that a key holding them reads one number, not every hash again. """ return self.locks.setdefault( (path.older, path.after, path.hashes), len(self.locks) ) def key(self, path: SpendingPath) -> _Keyed: """Return the path with its hash, taken once in the call.""" keyed = self.kept.get(id(path)) return self.keep(_Keyed(path)) if keyed is None else keyed def keep(self, keyed: _Keyed) -> _Keyed: """Hold the path and its hash until the call ends. Held, the path is the only one with its id for as long as the hash is kept. """ self.kept[id(keyed.path)] = keyed return keyed def quorum(self, threshold: int, keys: Sequence[KeyExpression]) -> Quorum: """Return the quorum, its keys where the expression names them.""" return Quorum(threshold, tuple(sorted(keys, key=self.keys.__getitem__))) def path( self, quorums: Sequence[Quorum], older: int | None, after: int | None, hashes: Iterable[tuple[str, bytes]], ) -> SpendingPath: """Return the path, its quorums and its hashes in their order.""" ordered = sorted( quorums, key=lambda quorum: ( tuple(self.keys[key] for key in quorum.keys), quorum.threshold, ), ) revealed = sorted(set(hashes), key=self.hashes.__getitem__) return SpendingPath(tuple(ordered), older, after, tuple(revealed)) def _conjoined(quorums: Sequence[Quorum], order: _Order) -> list[Quorum]: """Return the quorums, those wanting all their keys made one. n of n keys and m of m others are n+m of all of them, and a threshold lower than its keys stays a quorum of its own. """ whole = [quorum for quorum in quorums if quorum.threshold == len(quorum.keys)] if len(whole) < 2: return list(quorums) keys = [key for quorum in whole for key in quorum.keys] joined = [quorum for quorum in quorums if quorum.threshold != len(quorum.keys)] return [*joined, order.quorum(len(keys), keys)] def _path_and( first: SpendingPath, second: SpendingPath, order: _Order ) -> SpendingPath | None: """Return the path that takes both, None where no transaction can.""" older_met, older = _joined_older(first.older, second.older) after_met, after = _joined_after(first.after, second.after) if not (older_met and after_met): return None quorums = _conjoined(first.quorums + second.quorums, order) return order.path(quorums, older, after, first.hashes + second.hashes) def _key_size(key: KeyExpression) -> int: """Return what hashing the key reads: itself, its paths, its participants. A key's hash serializes its origin, every step of it, each time. It reads the steps of its path, and each participant of a ``musig()`` whole, each time too where the interpreter caches no hash of a tuple. """ steps = len(key.der_path) + (0 if key.origin is None else len(key.origin)) return 1 + steps + sum(map(_key_size, key.participants)) def _path_size(path: SpendingPath) -> int: """Return what joining the path reads: its keys, its hashes, and itself. A quorum is read by its keys, which order it, check it and hash it, each as big as `_key_size` says. """ keys = sum(_key_size(key) for quorum in path.quorums for key in quorum.keys) return 1 + len(path.hashes) + keys def _paths_and( first: Sequence[SpendingPath], second: Sequence[SpendingPath], order: _Order ) -> list[SpendingPath]: """Return the paths of a conjunction: each of the first with each other. The bound on the paths is on those that come out: a pair no transaction can take is none, and the same path reached twice is one. The work is bounded apart, before the first pair is joined: the pairs, by `_MAX_PATH_PAIRS`, and what they read, added to the call's `_Work`. A pair joins the locks, the quorums and the hashes of its two paths, and hashes the path they give, no bigger than the two together: its work is at most in proportion to their `_path_size` together. Each path of one side is in a pair with each path of the other, so the work of the pairs is at most in proportion to each side's sizes times the other side's paths, which is what is added. A side with no path makes no pair, and neither side is read. """ if not (first and second): return [] if len(first) * len(second) > _MAX_PATH_PAIRS: err_msg = f"more than {_MAX_PATH_PAIRS} pairs of spending paths to join" raise BTClibValueError(err_msg) order.work.add( len(second) * sum(map(_path_size, first)) + len(first) * sum(map(_path_size, second)) ) # a dict for its order and its keys: the same path reached twice is # one path paths: dict[_Keyed, None] = {} for one in first: for other in second: path = _path_and(one, other, order) if path is not None: paths[_Keyed(path)] = None _assert_path_count(len(paths)) return [order.keep(keyed).path for keyed in paths] class _Keyed: """A path as a key of a dict, with its hash taken once. A path keeps no hash of its own. Each path is hashed once, as it is first wrapped, and `_Order.keep` holds the paths handed on until the expression's paths are made, when the `_Order` goes with their hashes. """ __slots__ = ("digest", "path") def __init__(self, path: SpendingPath) -> None: self.path = path self.digest = hash(path) @override def __hash__(self) -> int: return self.digest @override def __eq__(self, other: object) -> bool: return isinstance(other, _Keyed) and self.path == other.path def _rest_keys(path: SpendingPath, locks: int) -> dict[tuple[int, int], list[int]]: """Return a key for each rest of the path, with the places of its quorums. A rest is the path but one quorum of one key: what two paths have to share to be one, that quorum taking either's keys, as either set of keys signs alone, which is how Liana reads ``or_i(pk(A),pk(B))``. The key is the number `_Order.lock` gives the path's locks, and the sum of the hashes of the quorums but that one, so that one pass over the quorums gives every rest's. A sum is a hash and not the rest: `_same_rest` compares the quorums of two paths whose keys match, the locks being in the key. Two quorums of one hash leave rests of one key, which keeps the place of each, for `_Union.merge` to try. """ hashes = [hash(quorum) for quorum in path.quorums] total = sum(hashes) keys: dict[tuple[int, int], list[int]] = {} for index, (quorum, digest) in enumerate(zip(path.quorums, hashes, strict=True)): if quorum.threshold == 1: keys.setdefault((locks, total - digest), []).append(index) return keys def _same_rest(one: SpendingPath, mine: int, other: SpendingPath, theirs: int) -> bool: """Answer whether two paths but a quorum each ask the same quorums.""" rest = Counter(one.quorums) rest[one.quorums[mine]] -= 1 others = Counter(other.quorums) others[other.quorums[theirs]] -= 1 return +rest == +others class _Union: """The paths of a disjunction as they merge, each by its place. `owners` is which paths each rest key was seen in, so that a path asks only those for a partner. """ def __init__(self, order: _Order) -> None: self.order = order self.live: dict[int, _Keyed] = {} self.places: dict[_Keyed, int] = {} self.keys: dict[int, dict[tuple[int, int], list[int]]] = {} self.owners: dict[tuple[int, int], set[int]] = {} def add(self, place: int, keyed: _Keyed) -> None: """Hold the path at its place, under each of its rest keys.""" self.live[place] = self.order.keep(keyed) self.places[keyed] = place self.keys[place] = _rest_keys(keyed.path, self.order.lock(keyed.path)) for key in self.keys[place]: self.owners.setdefault(key, set()).add(place) def drop(self, place: int) -> None: """Let go of the path at the place, and of its rest keys.""" del self.places[self.live.pop(place)] for key in self.keys.pop(place): self.owners[key].discard(place) def merge(self, place: int) -> int | None: """Merge the path at the place with the first sharing a rest. Return the place of the merged path, to be asked again, or None where the path shares no rest, or where the merged path is one held already. The path is tried at each place the key keeps of it, against each place the key keeps of the other path. Each pair past the first compares the two paths whole again, and is added to the call's `_Work` before it is compared. """ path = self.live[place].path for key, places in self.keys[place].items(): for other in sorted(self.owners[key] - {place}): owned = self.live[other].path pairs = product(places, self.keys[other][key]) for tried, (mine, theirs) in enumerate(pairs): if tried: self.order.work.add(_path_size(path) + _path_size(owned)) if not _same_rest(path, mine, owned, theirs): continue quorums = list(owned.quorums) joined = quorums[theirs].keys + path.quorums[mine].keys quorums[theirs] = self.order.quorum(1, list(dict.fromkeys(joined))) merged = _Keyed( self.order.path(quorums, owned.older, owned.after, owned.hashes) ) # asked again, as each path read was self.order.work.add(_path_size(merged.path)) self.drop(place) self.drop(other) if merged in self.places: return None self.add(min(place, other), merged) return min(place, other) return None def _paths_or( first: Sequence[SpendingPath], second: Sequence[SpendingPath], order: _Order ) -> list[SpendingPath]: """Return the paths of a disjunction: either's, two sharing a rest as one. A merge can make two paths share a rest that did not, so a merged path is asked again, until no two paths share one. A path keeps the place of the first of the paths it was merged from. Which two merge, where a path could join either of two others, follows that order, and either reading lists the same spends. The bound is on the paths that come out, a path both branches have being one. Each branch is within it already, so at most twice the bound are read. A path is asked once as it comes in and once more for each merge, and a merge leaves one path fewer, so the paths asked are at most twice those read. Asking a path reads its quorums once, as `_rest_keys` says, and compares two paths whole only where their keys match: the work is the paths asked times their quorums, each read by its keys, as big as `_key_size` says. Each path is added to the call's `_Work` before it is asked: those read as they come in, and each merged path as it is made. A rest key keeps more than one place of a path where two of its one-of quorums have one hash. Each pair of places past the first is added too, as `_Union.merge` says. """ order.work.add(sum(map(_path_size, first)) + sum(map(_path_size, second))) union = _Union(order) work: deque[int] = deque() for path in (*first, *second): keyed = order.key(path) if keyed not in union.places: work.append(len(work)) union.add(work[-1], keyed) while work: place = work.popleft() if place in union.live: merged = union.merge(place) if merged is not None: work.append(merged) _assert_path_count(len(union.live)) return [union.live[place].path for place in sorted(union.live)] def _is_key(paths: list[SpendingPath]) -> bool: """Answer whether the paths are one key's signature and nothing else.""" if len(paths) != 1: return False [path] = paths return ( path.older is None and path.after is None and not path.hashes and len(path.quorums) == 1 and len(path.quorums[0].keys) == 1 ) def _thresh_paths( threshold: int, subs: list[list[SpendingPath]], order: _Order ) -> list[SpendingPath]: """Return the paths of a thresh(), its keys kept as one quorum. `reached[count]` is the paths that satisfy `count` of the branches that are not keys, read from the last one so that a path's conditions come in the order the branches are written. The keys make up the rest of the threshold, as a quorum of all of them. A count that the branches still to read and the keys can no longer take to the threshold is dropped as soon as it is, so that the bound on the paths is met by choices that can still reach the threshold: a thresh() of all of thirteen branches is one path, not the choices of fewer of them that come on the way. A branch with no path, a ``0``, takes no count further, so `viable[left]` counts only the branches still to read that have one. `viable` counts a branch with paths even where none of them can join a choice, each needing a lock time of another kind than the choice's. That choice is kept, so an expression that mixes lock times can be refused past the bound on the paths though it has fewer paths. """ keys = tuple(paths[0].quorums[0].keys[0] for paths in subs if _is_key(paths)) others = [paths for paths in subs if not _is_key(paths)] reached: list[list[SpendingPath]] = [[SpendingPath()]] reached += [[] for _ in range(min(threshold, len(others)))] viable = [0, *accumulate(1 if paths else 0 for paths in others)] for left, paths in reversed(list(enumerate(others))): least = threshold - len(keys) - viable[left] for count in range(len(reached) - 1, max(least, 1) - 1, -1): satisfied = _paths_and(paths, reached[count - 1], order) reached[count] = _paths_or(satisfied, reached[count], order) for count in range(min(least, len(reached))): reached[count] = [] result: list[SpendingPath] = [] for count, paths in enumerate(reached): signers = threshold - count if signers > len(keys): continue if signers: quorum = order.path([order.quorum(signers, keys)], None, None, ()) paths = _paths_and(paths, [quorum], order) # noqa: PLW2901 result = _paths_or(result, paths, order) return result def _leaf_paths(node: Miniscript, order: _Order) -> list[SpendingPath]: """Return the paths of a fragment with no subexpressions.""" fragment = node.fragment if fragment == "raw_pk_h": err_msg = "rawpkh() names its key by its hash alone: no spending path lists it" raise BTClibValueError(err_msg) if fragment in {"pk_k", "pk_h", "multi", "multi_a"}: threshold = node.threshold if fragment in {"multi", "multi_a"} else 1 return [order.path([order.quorum(threshold, node.keys)], None, None, ())] if fragment == "older": return [SpendingPath(older=node.threshold)] if fragment == "after": return [SpendingPath(after=node.threshold)] if fragment in _HASH_OP_CODES: return [SpendingPath(hashes=((fragment, _plain_data(node)),))] # "1" is satisfied by nothing at all, and "0" not at all return [SpendingPath()] if fragment == "1" else [] def _computed_paths( node: Miniscript, subs: list[list[SpendingPath]], order: _Order ) -> list[SpendingPath]: """Return the spending paths of one fragment, from its subexpressions'.""" if not subs: return _leaf_paths(node, order) if node.fragment in _WRAPPERS: return subs[0] if node.fragment in {"and_v", "and_b"}: return _paths_and(subs[0], subs[1], order) if node.fragment == "andor": return _paths_or(_paths_and(subs[0], subs[1], order), subs[2], order) if node.fragment == "thresh": return _thresh_paths(node.threshold, subs, order) return _paths_or(subs[0], subs[1], order)