Bitcoin Core 31.99.0
P2P Digital Currency
miniscript.h
Go to the documentation of this file.
1// Copyright (c) 2019-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_SCRIPT_MINISCRIPT_H
6#define BITCOIN_SCRIPT_MINISCRIPT_H
7
9#include <crypto/hex_base.h>
10#include <policy/policy.h>
11#include <script/interpreter.h>
12#include <script/parsing.h>
13#include <script/script.h>
14#include <serialize.h>
15#include <util/check.h>
16#include <util/strencodings.h>
17#include <util/string.h>
18#include <util/vector.h>
19
20#include <algorithm>
21#include <compare>
22#include <concepts>
23#include <cstdint>
24#include <cstdlib>
25#include <functional>
26#include <memory>
27#include <optional>
28#include <set>
29#include <span>
30#include <stdexcept>
31#include <string>
32#include <string_view>
33#include <tuple>
34#include <utility>
35#include <variant>
36#include <vector>
37
38namespace miniscript {
39
131class Type {
133 uint32_t m_flags;
134
136 explicit constexpr Type(uint32_t flags) : m_flags(flags) {}
137
138public:
140 friend consteval Type operator""_mst(const char* c, size_t l);
141
143 constexpr Type operator|(Type x) const { return Type(m_flags | x.m_flags); }
144
146 constexpr Type operator&(Type x) const { return Type(m_flags & x.m_flags); }
147
149 constexpr bool operator<<(Type x) const { return (x.m_flags & ~m_flags) == 0; }
150
152 constexpr bool operator<(Type x) const { return m_flags < x.m_flags; }
153
155 constexpr bool operator==(Type x) const { return m_flags == x.m_flags; }
156
158 constexpr Type If(bool x) const { return Type(x ? m_flags : 0); }
159};
160
162inline consteval Type operator""_mst(const char* c, size_t l)
163{
164 Type typ{0};
165
166 for (const char *p = c; p < c + l; p++) {
167 typ = typ | Type(
168 *p == 'B' ? 1 << 0 : // Base type
169 *p == 'V' ? 1 << 1 : // Verify type
170 *p == 'K' ? 1 << 2 : // Key type
171 *p == 'W' ? 1 << 3 : // Wrapped type
172 *p == 'z' ? 1 << 4 : // Zero-arg property
173 *p == 'o' ? 1 << 5 : // One-arg property
174 *p == 'n' ? 1 << 6 : // Nonzero arg property
175 *p == 'd' ? 1 << 7 : // Dissatisfiable property
176 *p == 'u' ? 1 << 8 : // Unit property
177 *p == 'e' ? 1 << 9 : // Expression property
178 *p == 'f' ? 1 << 10 : // Forced property
179 *p == 's' ? 1 << 11 : // Safe property
180 *p == 'm' ? 1 << 12 : // Nonmalleable property
181 *p == 'x' ? 1 << 13 : // Expensive verify
182 *p == 'g' ? 1 << 14 : // older: contains relative time timelock (csv_time)
183 *p == 'h' ? 1 << 15 : // older: contains relative height timelock (csv_height)
184 *p == 'i' ? 1 << 16 : // after: contains time timelock (cltv_time)
185 *p == 'j' ? 1 << 17 : // after: contains height timelock (cltv_height)
186 *p == 'k' ? 1 << 18 : // does not contain a combination of height and time locks
187 (throw std::logic_error("Unknown character in _mst literal"), 0)
188 );
189 }
190
191 return typ;
192}
193
194using Opcode = std::pair<opcodetype, std::vector<unsigned char>>;
195
196template<typename Key> class Node;
197
199template <typename Key, std::invocable<const Node<Key>&> Fn>
200void ForEachNode(const Node<Key>& root, Fn&& fn)
201{
202 std::vector<std::reference_wrapper<const Node<Key>>> stack{root};
203 while (!stack.empty()) {
204 const Node<Key>& node = stack.back();
205 std::invoke(fn, node);
206 stack.pop_back();
207 for (const auto& sub : node.Subs()) {
208 stack.emplace_back(sub);
209 }
210 }
211}
212
214enum class Fragment {
215 JUST_0,
216 JUST_1,
217 PK_K,
218 PK_H,
219 OLDER,
220 AFTER,
221 SHA256,
222 HASH256,
223 RIPEMD160,
224 HASH160,
225 WRAP_A,
226 WRAP_S,
227 WRAP_C,
228 WRAP_D,
229 WRAP_V,
230 WRAP_J,
231 WRAP_N,
232 AND_V,
233 AND_B,
234 OR_B,
235 OR_C,
236 OR_D,
237 OR_I,
238 ANDOR,
239 THRESH,
240 MULTI,
241 MULTI_A,
242 // AND_N(X,Y) is represented as ANDOR(X,Y,0)
243 // WRAP_T(X) is represented as AND_V(X,1)
244 // WRAP_L(X) is represented as OR_I(0,X)
245 // WRAP_U(X) is represented as OR_I(X,0)
246};
247
248enum class Availability {
249 NO,
250 YES,
251 MAYBE,
252};
253
255 P2WSH,
256 TAPSCRIPT,
257};
258
260constexpr bool IsTapscript(MiniscriptContext ms_ctx)
261{
262 switch (ms_ctx) {
263 case MiniscriptContext::P2WSH: return false;
264 case MiniscriptContext::TAPSCRIPT: return true;
265 }
266 assert(false);
267}
268
269namespace internal {
270
272static constexpr uint32_t MAX_TAPMINISCRIPT_STACK_ELEM_SIZE{65};
273
275constexpr uint32_t TX_OVERHEAD{4 + 4};
277constexpr uint32_t TXIN_BYTES_NO_WITNESS{36 + 4 + 1};
279constexpr uint32_t P2WSH_TXOUT_BYTES{8 + 1 + 1 + 33};
285constexpr uint32_t MaxScriptSize(MiniscriptContext ms_ctx)
286{
287 if (IsTapscript(ms_ctx)) {
288 // Leaf scripts under Tapscript are not explicitly limited in size. They are only implicitly
289 // bounded by the maximum standard size of a spending transaction. Let the maximum script
290 // size conservatively be small enough such that even a maximum sized witness and a reasonably
291 // sized spending transaction can spend an output paying to this script without running into
292 // the maximum standard tx size limit.
294 return max_size - GetSizeOfCompactSize(max_size);
295 }
297}
298
300Type ComputeType(Fragment fragment, Type x, Type y, Type z, const std::vector<Type>& sub_types, uint32_t k, size_t data_size, size_t n_subs, size_t n_keys, MiniscriptContext ms_ctx);
301
303size_t ComputeScriptLen(Fragment fragment, Type sub0typ, size_t subsize, uint32_t k, size_t n_subs, size_t n_keys, MiniscriptContext ms_ctx);
304
307
317 bool has_sig = false;
319 bool malleable = false;
322 bool non_canon = false;
324 size_t size = 0;
326 std::vector<std::vector<unsigned char>> stack;
328 InputStack() = default;
330 InputStack(std::vector<unsigned char> in) : size(in.size() + 1), stack(Vector(std::move(in))) {}
338 InputStack& SetMalleable(bool x = true);
343};
344
346static const auto ZERO = InputStack(std::vector<unsigned char>());
348static const auto ZERO32 = InputStack(std::vector<unsigned char>(32, 0)).SetMalleable();
350static const auto ONE = InputStack(Vector((unsigned char)1));
352static const auto EMPTY = InputStack();
355
359
360 template<typename A, typename B>
361 InputResult(A&& in_nsat, B&& in_sat) : nsat(std::forward<A>(in_nsat)), sat(std::forward<B>(in_sat)) {}
362};
363
365template <typename I>
367{
368 bool valid;
370
371public:
372 MaxInt() : valid(false), value(0) {}
373 MaxInt(I val) : valid(true), value(val) {}
374
375 bool Valid() const { return valid; }
376 I Value() const { return value; }
377
378 friend MaxInt<I> operator+(const MaxInt<I>& a, const MaxInt<I>& b) {
379 if (!a.valid || !b.valid) return {};
380 return a.value + b.value;
381 }
382
383 friend MaxInt<I> operator|(const MaxInt<I>& a, const MaxInt<I>& b) {
384 if (!a.valid) return b;
385 if (!b.valid) return a;
386 return std::max(a.value, b.value);
387 }
388};
389
390struct Ops {
392 uint32_t count;
397
398 Ops(uint32_t in_count, MaxInt<uint32_t> in_sat, MaxInt<uint32_t> in_dsat) : count(in_count), sat(in_sat), dsat(in_dsat) {};
399};
400
443{
445 bool valid;
447 int32_t netdiff;
449 int32_t exec;
450
451public:
453 constexpr SatInfo() noexcept : valid(false), netdiff(0), exec(0) {}
454
456 constexpr SatInfo(int32_t in_netdiff, int32_t in_exec) noexcept :
457 valid{true}, netdiff{in_netdiff}, exec{in_exec} {}
458
459 bool Valid() const { return valid; }
460 int32_t NetDiff() const { return netdiff; }
461 int32_t Exec() const { return exec; }
462
464 constexpr friend SatInfo operator|(const SatInfo& a, const SatInfo& b) noexcept
465 {
466 // Union with an empty set is itself.
467 if (!a.valid) return b;
468 if (!b.valid) return a;
469 // Otherwise the netdiff and exec of the union is the maximum of the individual values.
470 return {std::max(a.netdiff, b.netdiff), std::max(a.exec, b.exec)};
471 }
472
474 constexpr friend SatInfo operator+(const SatInfo& a, const SatInfo& b) noexcept
475 {
476 // Concatenation with an empty set yields an empty set.
477 if (!a.valid || !b.valid) return {};
478 // Otherwise, the maximum stack size difference for the combined scripts is the sum of the
479 // netdiffs, and the maximum stack size difference anywhere is either b.exec (if the
480 // maximum occurred in b) or b.netdiff+a.exec (if the maximum occurred in a).
481 return {a.netdiff + b.netdiff, std::max(b.exec, b.netdiff + a.exec)};
482 }
483
485 static constexpr SatInfo Empty() noexcept { return {0, 0}; }
487 static constexpr SatInfo Push() noexcept { return {-1, 0}; }
489 static constexpr SatInfo Hash() noexcept { return {0, 0}; }
491 static constexpr SatInfo Nop() noexcept { return {0, 0}; }
493 static constexpr SatInfo If() noexcept { return {1, 1}; }
495 static constexpr SatInfo BinaryOp() noexcept { return {1, 1}; }
496
497 // Scripts for specific individual opcodes.
498 static constexpr SatInfo OP_DUP() noexcept { return {-1, 0}; }
499 static constexpr SatInfo OP_IFDUP(bool nonzero) noexcept { return {nonzero ? -1 : 0, 0}; }
500 static constexpr SatInfo OP_EQUALVERIFY() noexcept { return {2, 2}; }
501 static constexpr SatInfo OP_EQUAL() noexcept { return {1, 1}; }
502 static constexpr SatInfo OP_SIZE() noexcept { return {-1, 0}; }
503 static constexpr SatInfo OP_CHECKSIG() noexcept { return {1, 1}; }
504 static constexpr SatInfo OP_0NOTEQUAL() noexcept { return {0, 0}; }
505 static constexpr SatInfo OP_VERIFY() noexcept { return {1, 1}; }
506};
507
509{
511
512public:
513 constexpr StackSize(SatInfo in_sat, SatInfo in_dsat) noexcept : sat(in_sat), dsat(in_dsat) {};
514 constexpr StackSize(SatInfo in_both) noexcept : sat(in_both), dsat(in_both) {};
515
516 const SatInfo& Sat() const { return sat; }
517 const SatInfo& Dsat() const { return dsat; }
518};
519
525
526 WitnessSize(MaxInt<uint32_t> in_sat, MaxInt<uint32_t> in_dsat) : sat(in_sat), dsat(in_dsat) {};
527};
528
529struct NoDupCheck {};
530
531} // namespace internal
532
534template <typename Key>
535class Node
536{
540 uint32_t k = 0;
542 std::vector<Key> keys;
544 std::vector<unsigned char> data;
546 std::vector<Node> subs;
549
550public:
551 // Permit 1 level deep recursion since we own instances of our own type.
552 // NOLINTBEGIN(misc-no-recursion)
554 {
555 // Destroy the subexpressions iteratively after moving out their
556 // subexpressions to avoid a stack-overflow due to recursive calls to
557 // the subs' destructors.
558 // We move vectors in order to only update array-pointers inside them
559 // rather than moving individual Node instances which would involve
560 // moving/copying each Node field.
561 std::vector<std::vector<Node>> queue;
562 queue.push_back(std::move(subs));
563 do {
564 auto flattening{std::move(queue.back())};
565 queue.pop_back();
566 for (Node& n : flattening) {
567 if (!n.subs.empty()) queue.push_back(std::move(n.subs));
568 }
569 } while (!queue.empty());
570 }
571 // NOLINTEND(misc-no-recursion)
572
574 {
575 // Use TreeEval() to avoid a stack-overflow due to recursion
576 auto upfn = [](const Node& node, std::span<Node> children) {
577 std::vector<Node> new_subs;
578 for (auto& child : children) {
579 // It's fine to move from children as they are new nodes having
580 // been produced by calling this function one level down.
581 new_subs.push_back(std::move(child));
582 }
583 return Node{internal::NoDupCheck{}, node.m_script_ctx, node.fragment, std::move(new_subs), node.keys, node.data, node.k};
584 };
585 return TreeEval<Node>(upfn);
586 }
587
588 enum Fragment Fragment() const { return fragment; }
589 uint32_t K() const { return k; }
590 const std::vector<Key>& Keys() const { return keys; }
591 const std::vector<unsigned char>& Data() const { return data; }
592 const std::vector<Node>& Subs() const { return subs; }
593
594private:
604 size_t scriptlen;
610 mutable std::optional<bool> has_duplicate_keys;
611
612 // Constructor which takes all of the data that a Node could possibly contain.
613 // This is kept private as no valid fragment has all of these arguments.
614 // Only used by Clone()
615 Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector<Node> sub, std::vector<Key> key, std::vector<unsigned char> arg, uint32_t val)
616 : fragment(nt), k(val), keys(std::move(key)), data(std::move(arg)), subs(std::move(sub)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
617
619 size_t CalcScriptLen() const
620 {
621 size_t subsize = 0;
622 for (const auto& sub : subs) {
623 subsize += sub.ScriptSize();
624 }
625 Type sub0type = subs.size() > 0 ? subs[0].GetType() : ""_mst;
626 return internal::ComputeScriptLen(fragment, sub0type, subsize, k, subs.size(), keys.size(), m_script_ctx);
627 }
628
629 /* Apply a recursive algorithm to a Miniscript tree, without actual recursive calls.
630 *
631 * The algorithm is defined by two functions: downfn and upfn. Conceptually, the
632 * result can be thought of as first using downfn to compute a "state" for each node,
633 * from the root down to the leaves. Then upfn is used to compute a "result" for each
634 * node, from the leaves back up to the root, which is then returned. In the actual
635 * implementation, both functions are invoked in an interleaved fashion, performing a
636 * depth-first traversal of the tree.
637 *
638 * In more detail, it is invoked as node.TreeEvalMaybe<Result>(root, downfn, upfn):
639 * - root is the state of the root node, of type State.
640 * - downfn is a callable (State&, const Node&, size_t) -> State, which given a
641 * node, its state, and an index of one of its children, computes the state of that
642 * child. It can modify the state. Children of a given node will have downfn()
643 * called in order.
644 * - upfn is a callable (State&&, const Node&, std::span<Result>) -> std::optional<Result>,
645 * which given a node, its state, and a span of the results of its children,
646 * computes the result of the node. If std::nullopt is returned by upfn,
647 * TreeEvalMaybe() immediately returns std::nullopt.
648 * The return value of TreeEvalMaybe is the result of the root node.
649 *
650 * Result type cannot be bool due to the std::vector<bool> specialization.
651 */
652 template<typename Result, typename State, typename DownFn, typename UpFn>
653 std::optional<Result> TreeEvalMaybe(State root_state, DownFn downfn, UpFn upfn) const
654 {
656 struct StackElem
657 {
658 const Node& node;
659 size_t expanded;
660 State state;
661
662 StackElem(const Node& node_, size_t exp_, State&& state_) :
663 node(node_), expanded(exp_), state(std::move(state_)) {}
664 };
665 /* Stack of tree nodes being explored. */
666 std::vector<StackElem> stack;
667 /* Results of subtrees so far. Their order and mapping to tree nodes
668 * is implicitly defined by stack. */
669 std::vector<Result> results;
670 stack.emplace_back(*this, 0, std::move(root_state));
671
672 /* Here is a demonstration of the algorithm, for an example tree A(B,C(D,E),F).
673 * State variables are omitted for simplicity.
674 *
675 * First: stack=[(A,0)] results=[]
676 * stack=[(A,1),(B,0)] results=[]
677 * stack=[(A,1)] results=[B]
678 * stack=[(A,2),(C,0)] results=[B]
679 * stack=[(A,2),(C,1),(D,0)] results=[B]
680 * stack=[(A,2),(C,1)] results=[B,D]
681 * stack=[(A,2),(C,2),(E,0)] results=[B,D]
682 * stack=[(A,2),(C,2)] results=[B,D,E]
683 * stack=[(A,2)] results=[B,C]
684 * stack=[(A,3),(F,0)] results=[B,C]
685 * stack=[(A,3)] results=[B,C,F]
686 * Final: stack=[] results=[A]
687 */
688 while (stack.size()) {
689 const Node& node = stack.back().node;
690 if (stack.back().expanded < node.subs.size()) {
691 /* We encounter a tree node with at least one unexpanded child.
692 * Expand it. By the time we hit this node again, the result of
693 * that child (and all earlier children) will be at the end of `results`. */
694 size_t child_index = stack.back().expanded++;
695 State child_state = downfn(stack.back().state, node, child_index);
696 stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
697 continue;
698 }
699 // Invoke upfn with the last node.subs.size() elements of results as input.
700 assert(results.size() >= node.subs.size());
701 std::optional<Result> result{upfn(std::move(stack.back().state), node,
702 std::span<Result>{results}.last(node.subs.size()))};
703 // If evaluation returns std::nullopt, abort immediately.
704 if (!result) return {};
705 // Replace the last node.subs.size() elements of results with the new result.
706 results.erase(results.end() - node.subs.size(), results.end());
707 results.push_back(std::move(*result));
708 stack.pop_back();
709 }
710 // The final remaining results element is the root result, return it.
711 assert(results.size() >= 1);
712 CHECK_NONFATAL(results.size() == 1);
713 return std::move(results[0]);
714 }
715
718 template<typename Result, typename UpFn>
719 std::optional<Result> TreeEvalMaybe(UpFn upfn) const
720 {
721 struct DummyState {};
722 return TreeEvalMaybe<Result>(DummyState{},
723 [](DummyState, const Node&, size_t) { return DummyState{}; },
724 [&upfn](DummyState, const Node& node, std::span<Result> subs) {
725 return upfn(node, subs);
726 }
727 );
728 }
729
731 template<typename Result, typename State, typename DownFn, typename UpFn>
732 Result TreeEval(State root_state, DownFn&& downfn, UpFn upfn) const
733 {
734 // Invoke TreeEvalMaybe with upfn wrapped to return std::optional<Result>, and then
735 // unconditionally dereference the result (it cannot be std::nullopt).
736 return std::move(*TreeEvalMaybe<Result>(std::move(root_state),
737 std::forward<DownFn>(downfn),
738 [&upfn](State&& state, const Node& node, std::span<Result> subs) {
739 Result res{upfn(std::move(state), node, subs)};
740 return std::optional<Result>(std::move(res));
741 }
742 ));
743 }
744
747 template<typename Result, typename UpFn>
748 Result TreeEval(UpFn upfn) const
749 {
750 struct DummyState {};
751 return std::move(*TreeEvalMaybe<Result>(DummyState{},
752 [](DummyState, const Node&, size_t) { return DummyState{}; },
753 [&upfn](DummyState, const Node& node, std::span<Result> subs) {
754 Result res{upfn(node, subs)};
755 return std::optional<Result>(std::move(res));
756 }
757 ));
758 }
759
761 friend int Compare(const Node<Key>& node1, const Node<Key>& node2)
762 {
763 std::vector<std::pair<const Node<Key>&, const Node<Key>&>> queue;
764 queue.emplace_back(node1, node2);
765 while (!queue.empty()) {
766 const auto& [a, b] = queue.back();
767 queue.pop_back();
768 if (std::tie(a.fragment, a.k, a.keys, a.data) < std::tie(b.fragment, b.k, b.keys, b.data)) return -1;
769 if (std::tie(b.fragment, b.k, b.keys, b.data) < std::tie(a.fragment, a.k, a.keys, a.data)) return 1;
770 if (a.subs.size() < b.subs.size()) return -1;
771 if (b.subs.size() < a.subs.size()) return 1;
772 size_t n = a.subs.size();
773 for (size_t i = 0; i < n; ++i) {
774 queue.emplace_back(a.subs[n - 1 - i], b.subs[n - 1 - i]);
775 }
776 }
777 return 0;
778 }
779
781 Type CalcType() const {
782 using namespace internal;
783
784 // THRESH has a variable number of subexpressions
785 std::vector<Type> sub_types;
786 if (fragment == Fragment::THRESH) {
787 for (const auto& sub : subs) sub_types.push_back(sub.GetType());
788 }
789 // All other nodes than THRESH can be computed just from the types of the 0-3 subexpressions.
790 Type x = subs.size() > 0 ? subs[0].GetType() : ""_mst;
791 Type y = subs.size() > 1 ? subs[1].GetType() : ""_mst;
792 Type z = subs.size() > 2 ? subs[2].GetType() : ""_mst;
793
794 return SanitizeType(ComputeType(fragment, x, y, z, sub_types, k, data.size(), subs.size(), keys.size(), m_script_ctx));
795 }
796
797public:
798 template<typename Ctx>
799 CScript ToScript(const Ctx& ctx) const
800 {
801 // To construct the CScript for a Miniscript object, we use the TreeEval algorithm.
802 // The State is a boolean: whether or not the node's script expansion is followed
803 // by an OP_VERIFY (which may need to be combined with the last script opcode).
804 auto downfn = [](bool verify, const Node& node, size_t index) {
805 // For WRAP_V, the subexpression is certainly followed by OP_VERIFY.
806 if (node.fragment == Fragment::WRAP_V) return true;
807 // The subexpression of WRAP_S, and the last subexpression of AND_V
808 // inherit the followed-by-OP_VERIFY property from the parent.
809 if (node.fragment == Fragment::WRAP_S ||
810 (node.fragment == Fragment::AND_V && index == 1)) return verify;
811 return false;
812 };
813 // The upward function computes for a node, given its followed-by-OP_VERIFY status
814 // and the CScripts of its child nodes, the CScript of the node.
815 const bool is_tapscript{IsTapscript(m_script_ctx)};
816 auto upfn = [&ctx, is_tapscript](bool verify, const Node& node, std::span<CScript> subs) -> CScript {
817 switch (node.fragment) {
818 case Fragment::PK_K: return BuildScript(ctx.ToPKBytes(node.keys[0]));
819 case Fragment::PK_H: return BuildScript(OP_DUP, OP_HASH160, ctx.ToPKHBytes(node.keys[0]), OP_EQUALVERIFY);
827 case Fragment::WRAP_S: return BuildScript(OP_SWAP, subs[0]);
828 case Fragment::WRAP_C: return BuildScript(std::move(subs[0]), verify ? OP_CHECKSIGVERIFY : OP_CHECKSIG);
830 case Fragment::WRAP_V: {
831 if (node.subs[0].GetType() << "x"_mst) {
832 return BuildScript(std::move(subs[0]), OP_VERIFY);
833 } else {
834 return std::move(subs[0]);
835 }
836 }
838 case Fragment::WRAP_N: return BuildScript(std::move(subs[0]), OP_0NOTEQUAL);
839 case Fragment::JUST_1: return BuildScript(OP_1);
840 case Fragment::JUST_0: return BuildScript(OP_0);
841 case Fragment::AND_V: return BuildScript(std::move(subs[0]), subs[1]);
842 case Fragment::AND_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLAND);
843 case Fragment::OR_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLOR);
844 case Fragment::OR_D: return BuildScript(std::move(subs[0]), OP_IFDUP, OP_NOTIF, subs[1], OP_ENDIF);
845 case Fragment::OR_C: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[1], OP_ENDIF);
846 case Fragment::OR_I: return BuildScript(OP_IF, subs[0], OP_ELSE, subs[1], OP_ENDIF);
847 case Fragment::ANDOR: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[2], OP_ELSE, subs[1], OP_ENDIF);
848 case Fragment::MULTI: {
849 CHECK_NONFATAL(!is_tapscript);
851 for (const auto& key : node.keys) {
852 script = BuildScript(std::move(script), ctx.ToPKBytes(key));
853 }
854 return BuildScript(std::move(script), node.keys.size(), verify ? OP_CHECKMULTISIGVERIFY : OP_CHECKMULTISIG);
855 }
856 case Fragment::MULTI_A: {
857 CHECK_NONFATAL(is_tapscript);
858 CScript script = BuildScript(ctx.ToPKBytes(*node.keys.begin()), OP_CHECKSIG);
859 for (auto it = node.keys.begin() + 1; it != node.keys.end(); ++it) {
860 script = BuildScript(std::move(script), ctx.ToPKBytes(*it), OP_CHECKSIGADD);
861 }
862 return BuildScript(std::move(script), node.k, verify ? OP_NUMEQUALVERIFY : OP_NUMEQUAL);
863 }
864 case Fragment::THRESH: {
865 CScript script = std::move(subs[0]);
866 for (size_t i = 1; i < subs.size(); ++i) {
867 script = BuildScript(std::move(script), subs[i], OP_ADD);
868 }
869 return BuildScript(std::move(script), node.k, verify ? OP_EQUALVERIFY : OP_EQUAL);
870 }
871 }
872 assert(false);
873 };
874 return TreeEval<CScript>(false, downfn, upfn);
875 }
876
877 template<typename CTx>
878 std::optional<std::string> ToString(const CTx& ctx) const {
879 bool dummy{false};
880 return ToString(ctx, dummy);
881 }
882
883 template<typename CTx>
884 std::optional<std::string> ToString(const CTx& ctx, bool& has_priv_key) const {
885 // To construct the std::string representation for a Miniscript object, we use
886 // the TreeEvalMaybe algorithm. The State is a boolean: whether the parent node is a
887 // wrapper. If so, non-wrapper expressions must be prefixed with a ":".
888 auto downfn = [](bool, const Node& node, size_t) {
889 return (node.fragment == Fragment::WRAP_A || node.fragment == Fragment::WRAP_S ||
890 node.fragment == Fragment::WRAP_D || node.fragment == Fragment::WRAP_V ||
891 node.fragment == Fragment::WRAP_J || node.fragment == Fragment::WRAP_N ||
892 node.fragment == Fragment::WRAP_C ||
893 (node.fragment == Fragment::AND_V && node.subs[1].fragment == Fragment::JUST_1) ||
894 (node.fragment == Fragment::OR_I && node.subs[0].fragment == Fragment::JUST_0) ||
895 (node.fragment == Fragment::OR_I && node.subs[1].fragment == Fragment::JUST_0));
896 };
897 auto toString = [&ctx, &has_priv_key](Key key) -> std::optional<std::string> {
898 bool fragment_has_priv_key{false};
899 auto key_str{ctx.ToString(key, fragment_has_priv_key)};
900 if (key_str) has_priv_key = has_priv_key || fragment_has_priv_key;
901 return key_str;
902 };
903 // The upward function computes for a node, given whether its parent is a wrapper,
904 // and the string representations of its child nodes, the string representation of the node.
905 const bool is_tapscript{IsTapscript(m_script_ctx)};
906 auto upfn = [is_tapscript, &toString](bool wrapped, const Node& node, std::span<std::string> subs) -> std::optional<std::string> {
907 std::string ret = wrapped ? ":" : "";
908
909 switch (node.fragment) {
910 case Fragment::WRAP_A: return "a" + std::move(subs[0]);
911 case Fragment::WRAP_S: return "s" + std::move(subs[0]);
912 case Fragment::WRAP_C:
913 if (node.subs[0].fragment == Fragment::PK_K) {
914 // pk(K) is syntactic sugar for c:pk_k(K)
915 auto key_str = toString(node.subs[0].keys[0]);
916 if (!key_str) return {};
917 return std::move(ret) + "pk(" + std::move(*key_str) + ")";
918 }
919 if (node.subs[0].fragment == Fragment::PK_H) {
920 // pkh(K) is syntactic sugar for c:pk_h(K)
921 auto key_str = toString(node.subs[0].keys[0]);
922 if (!key_str) return {};
923 return std::move(ret) + "pkh(" + std::move(*key_str) + ")";
924 }
925 return "c" + std::move(subs[0]);
926 case Fragment::WRAP_D: return "d" + std::move(subs[0]);
927 case Fragment::WRAP_V: return "v" + std::move(subs[0]);
928 case Fragment::WRAP_J: return "j" + std::move(subs[0]);
929 case Fragment::WRAP_N: return "n" + std::move(subs[0]);
930 case Fragment::AND_V:
931 // t:X is syntactic sugar for and_v(X,1).
932 if (node.subs[1].fragment == Fragment::JUST_1) return "t" + std::move(subs[0]);
933 break;
934 case Fragment::OR_I:
935 if (node.subs[0].fragment == Fragment::JUST_0) return "l" + std::move(subs[1]);
936 if (node.subs[1].fragment == Fragment::JUST_0) return "u" + std::move(subs[0]);
937 break;
938 default: break;
939 }
940 switch (node.fragment) {
941 case Fragment::PK_K: {
942 auto key_str = toString(node.keys[0]);
943 if (!key_str) return {};
944 return std::move(ret) + "pk_k(" + std::move(*key_str) + ")";
945 }
946 case Fragment::PK_H: {
947 auto key_str = toString(node.keys[0]);
948 if (!key_str) return {};
949 return std::move(ret) + "pk_h(" + std::move(*key_str) + ")";
950 }
951 case Fragment::AFTER: return std::move(ret) + "after(" + util::ToString(node.k) + ")";
952 case Fragment::OLDER: return std::move(ret) + "older(" + util::ToString(node.k) + ")";
953 case Fragment::HASH256: return std::move(ret) + "hash256(" + HexStr(node.data) + ")";
954 case Fragment::HASH160: return std::move(ret) + "hash160(" + HexStr(node.data) + ")";
955 case Fragment::SHA256: return std::move(ret) + "sha256(" + HexStr(node.data) + ")";
956 case Fragment::RIPEMD160: return std::move(ret) + "ripemd160(" + HexStr(node.data) + ")";
957 case Fragment::JUST_1: return std::move(ret) + "1";
958 case Fragment::JUST_0: return std::move(ret) + "0";
959 case Fragment::AND_V: return std::move(ret) + "and_v(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
960 case Fragment::AND_B: return std::move(ret) + "and_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
961 case Fragment::OR_B: return std::move(ret) + "or_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
962 case Fragment::OR_D: return std::move(ret) + "or_d(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
963 case Fragment::OR_C: return std::move(ret) + "or_c(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
964 case Fragment::OR_I: return std::move(ret) + "or_i(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
965 case Fragment::ANDOR:
966 // and_n(X,Y) is syntactic sugar for andor(X,Y,0).
967 if (node.subs[2].fragment == Fragment::JUST_0) return std::move(ret) + "and_n(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
968 return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
969 case Fragment::MULTI: {
970 CHECK_NONFATAL(!is_tapscript);
971 auto str = std::move(ret) + "multi(" + util::ToString(node.k);
972 for (const auto& key : node.keys) {
973 auto key_str = toString(key);
974 if (!key_str) return {};
975 str += "," + std::move(*key_str);
976 }
977 return std::move(str) + ")";
978 }
979 case Fragment::MULTI_A: {
980 CHECK_NONFATAL(is_tapscript);
981 auto str = std::move(ret) + "multi_a(" + util::ToString(node.k);
982 for (const auto& key : node.keys) {
983 auto key_str = toString(key);
984 if (!key_str) return {};
985 str += "," + std::move(*key_str);
986 }
987 return std::move(str) + ")";
988 }
989 case Fragment::THRESH: {
990 auto str = std::move(ret) + "thresh(" + util::ToString(node.k);
991 for (auto& sub : subs) {
992 str += "," + std::move(sub);
993 }
994 return std::move(str) + ")";
995 }
996 default: break;
997 }
998 assert(false);
999 };
1000
1001 return TreeEvalMaybe<std::string>(false, downfn, upfn);
1002 }
1003
1004private:
1006 switch (fragment) {
1007 case Fragment::JUST_1: return {0, 0, {}};
1008 case Fragment::JUST_0: return {0, {}, 0};
1009 case Fragment::PK_K: return {0, 0, 0};
1010 case Fragment::PK_H: return {3, 0, 0};
1011 case Fragment::OLDER:
1012 case Fragment::AFTER: return {1, 0, {}};
1013 case Fragment::SHA256:
1015 case Fragment::HASH256:
1016 case Fragment::HASH160: return {4, 0, {}};
1017 case Fragment::AND_V: return {subs[0].ops.count + subs[1].ops.count, subs[0].ops.sat + subs[1].ops.sat, {}};
1018 case Fragment::AND_B: {
1019 const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1020 const auto sat{subs[0].ops.sat + subs[1].ops.sat};
1021 const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1022 return {count, sat, dsat};
1023 }
1024 case Fragment::OR_B: {
1025 const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1026 const auto sat{(subs[0].ops.sat + subs[1].ops.dsat) | (subs[1].ops.sat + subs[0].ops.dsat)};
1027 const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1028 return {count, sat, dsat};
1029 }
1030 case Fragment::OR_D: {
1031 const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1032 const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1033 const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1034 return {count, sat, dsat};
1035 }
1036 case Fragment::OR_C: {
1037 const auto count{2 + subs[0].ops.count + subs[1].ops.count};
1038 const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1039 return {count, sat, {}};
1040 }
1041 case Fragment::OR_I: {
1042 const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1043 const auto sat{subs[0].ops.sat | subs[1].ops.sat};
1044 const auto dsat{subs[0].ops.dsat | subs[1].ops.dsat};
1045 return {count, sat, dsat};
1046 }
1047 case Fragment::ANDOR: {
1048 const auto count{3 + subs[0].ops.count + subs[1].ops.count + subs[2].ops.count};
1049 const auto sat{(subs[1].ops.sat + subs[0].ops.sat) | (subs[0].ops.dsat + subs[2].ops.sat)};
1050 const auto dsat{subs[0].ops.dsat + subs[2].ops.dsat};
1051 return {count, sat, dsat};
1052 }
1053 case Fragment::MULTI: return {1, (uint32_t)keys.size(), (uint32_t)keys.size()};
1054 case Fragment::MULTI_A: return {(uint32_t)keys.size() + 1, 0, 0};
1055 case Fragment::WRAP_S:
1056 case Fragment::WRAP_C:
1057 case Fragment::WRAP_N: return {1 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1058 case Fragment::WRAP_A: return {2 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1059 case Fragment::WRAP_D: return {3 + subs[0].ops.count, subs[0].ops.sat, 0};
1060 case Fragment::WRAP_J: return {4 + subs[0].ops.count, subs[0].ops.sat, 0};
1061 case Fragment::WRAP_V: return {subs[0].ops.count + (subs[0].GetType() << "x"_mst), subs[0].ops.sat, {}};
1062 case Fragment::THRESH: {
1063 uint32_t count = 0;
1064 auto sats = Vector(internal::MaxInt<uint32_t>(0));
1065 for (const auto& sub : subs) {
1066 count += sub.ops.count + 1;
1067 auto next_sats = Vector(sats[0] + sub.ops.dsat);
1068 for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + sub.ops.dsat) | (sats[j - 1] + sub.ops.sat));
1069 next_sats.push_back(sats[sats.size() - 1] + sub.ops.sat);
1070 sats = std::move(next_sats);
1071 }
1072 assert(k < sats.size());
1073 return {count, sats[k], sats[0]};
1074 }
1075 }
1076 assert(false);
1077 }
1078
1080 using namespace internal;
1081 switch (fragment) {
1082 case Fragment::JUST_0: return {{}, SatInfo::Push()};
1083 case Fragment::JUST_1: return {SatInfo::Push(), {}};
1084 case Fragment::OLDER:
1085 case Fragment::AFTER: return {SatInfo::Push() + SatInfo::Nop(), {}};
1086 case Fragment::PK_K: return {SatInfo::Push()};
1087 case Fragment::PK_H: return {SatInfo::OP_DUP() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY()};
1088 case Fragment::SHA256:
1090 case Fragment::HASH256:
1091 case Fragment::HASH160: return {
1092 SatInfo::OP_SIZE() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUAL(),
1093 {}
1094 };
1095 case Fragment::ANDOR: {
1096 const auto& x{subs[0].ss};
1097 const auto& y{subs[1].ss};
1098 const auto& z{subs[2].ss};
1099 return {
1100 (x.Sat() + SatInfo::If() + y.Sat()) | (x.Dsat() + SatInfo::If() + z.Sat()),
1101 x.Dsat() + SatInfo::If() + z.Dsat()
1102 };
1103 }
1104 case Fragment::AND_V: {
1105 const auto& x{subs[0].ss};
1106 const auto& y{subs[1].ss};
1107 return {x.Sat() + y.Sat(), {}};
1108 }
1109 case Fragment::AND_B: {
1110 const auto& x{subs[0].ss};
1111 const auto& y{subs[1].ss};
1112 return {x.Sat() + y.Sat() + SatInfo::BinaryOp(), x.Dsat() + y.Dsat() + SatInfo::BinaryOp()};
1113 }
1114 case Fragment::OR_B: {
1115 const auto& x{subs[0].ss};
1116 const auto& y{subs[1].ss};
1117 return {
1118 ((x.Sat() + y.Dsat()) | (x.Dsat() + y.Sat())) + SatInfo::BinaryOp(),
1119 x.Dsat() + y.Dsat() + SatInfo::BinaryOp()
1120 };
1121 }
1122 case Fragment::OR_C: {
1123 const auto& x{subs[0].ss};
1124 const auto& y{subs[1].ss};
1125 return {(x.Sat() + SatInfo::If()) | (x.Dsat() + SatInfo::If() + y.Sat()), {}};
1126 }
1127 case Fragment::OR_D: {
1128 const auto& x{subs[0].ss};
1129 const auto& y{subs[1].ss};
1130 return {
1131 (x.Sat() + SatInfo::OP_IFDUP(true) + SatInfo::If()) | (x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Sat()),
1132 x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Dsat()
1133 };
1134 }
1135 case Fragment::OR_I: {
1136 const auto& x{subs[0].ss};
1137 const auto& y{subs[1].ss};
1138 return {SatInfo::If() + (x.Sat() | y.Sat()), SatInfo::If() + (x.Dsat() | y.Dsat())};
1139 }
1140 // multi(k, key1, key2, ..., key_n) starts off with k+1 stack elements (a 0, plus k
1141 // signatures), then reaches n+k+3 stack elements after pushing the n keys, plus k and
1142 // n itself, and ends with 1 stack element (success or failure). Thus, it net removes
1143 // k elements (from k+1 to 1), while reaching k+n+2 more than it ends with.
1144 case Fragment::MULTI: return {SatInfo(k, k + keys.size() + 2)};
1145 // multi_a(k, key1, key2, ..., key_n) starts off with n stack elements (the
1146 // signatures), reaches 1 more (after the first key push), and ends with 1. Thus it net
1147 // removes n-1 elements (from n to 1) while reaching n more than it ends with.
1148 case Fragment::MULTI_A: return {SatInfo(keys.size() - 1, keys.size())};
1149 case Fragment::WRAP_A:
1150 case Fragment::WRAP_N:
1151 case Fragment::WRAP_S: return subs[0].ss;
1152 case Fragment::WRAP_C: return {
1153 subs[0].ss.Sat() + SatInfo::OP_CHECKSIG(),
1154 subs[0].ss.Dsat() + SatInfo::OP_CHECKSIG()
1155 };
1156 case Fragment::WRAP_D: return {
1157 SatInfo::OP_DUP() + SatInfo::If() + subs[0].ss.Sat(),
1158 SatInfo::OP_DUP() + SatInfo::If()
1159 };
1160 case Fragment::WRAP_V: return {subs[0].ss.Sat() + SatInfo::OP_VERIFY(), {}};
1161 case Fragment::WRAP_J: return {
1162 SatInfo::OP_SIZE() + SatInfo::OP_0NOTEQUAL() + SatInfo::If() + subs[0].ss.Sat(),
1163 SatInfo::OP_SIZE() + SatInfo::OP_0NOTEQUAL() + SatInfo::If()
1164 };
1165 case Fragment::THRESH: {
1166 // sats[j] is the SatInfo corresponding to all traces reaching j satisfactions.
1167 auto sats = Vector(SatInfo::Empty());
1168 for (size_t i = 0; i < subs.size(); ++i) {
1169 // Loop over the subexpressions, processing them one by one. After adding
1170 // element i we need to add OP_ADD (if i>0).
1171 auto add = i ? SatInfo::BinaryOp() : SatInfo::Empty();
1172 // Construct a variable that will become the next sats, starting with index 0.
1173 auto next_sats = Vector(sats[0] + subs[i].ss.Dsat() + add);
1174 // Then loop to construct next_sats[1..i].
1175 for (size_t j = 1; j < sats.size(); ++j) {
1176 next_sats.push_back(((sats[j] + subs[i].ss.Dsat()) | (sats[j - 1] + subs[i].ss.Sat())) + add);
1177 }
1178 // Finally construct next_sats[i+1].
1179 next_sats.push_back(sats[sats.size() - 1] + subs[i].ss.Sat() + add);
1180 // Switch over.
1181 sats = std::move(next_sats);
1182 }
1183 // To satisfy thresh we need k satisfactions; to dissatisfy we need 0. In both
1184 // cases a push of k and an OP_EQUAL follow.
1185 return {
1186 sats[k] + SatInfo::Push() + SatInfo::OP_EQUAL(),
1187 sats[0] + SatInfo::Push() + SatInfo::OP_EQUAL()
1188 };
1189 }
1190 }
1191 assert(false);
1192 }
1193
1195 const uint32_t sig_size = IsTapscript(m_script_ctx) ? 1 + 65 : 1 + 72;
1196 const uint32_t pubkey_size = IsTapscript(m_script_ctx) ? 1 + 32 : 1 + 33;
1197 switch (fragment) {
1198 case Fragment::JUST_0: return {{}, 0};
1199 case Fragment::JUST_1:
1200 case Fragment::OLDER:
1201 case Fragment::AFTER: return {0, {}};
1202 case Fragment::PK_K: return {sig_size, 1};
1203 case Fragment::PK_H: return {sig_size + pubkey_size, 1 + pubkey_size};
1204 case Fragment::SHA256:
1206 case Fragment::HASH256:
1207 case Fragment::HASH160: return {1 + 32, {}};
1208 case Fragment::ANDOR: {
1209 const auto sat{(subs[0].ws.sat + subs[1].ws.sat) | (subs[0].ws.dsat + subs[2].ws.sat)};
1210 const auto dsat{subs[0].ws.dsat + subs[2].ws.dsat};
1211 return {sat, dsat};
1212 }
1213 case Fragment::AND_V: return {subs[0].ws.sat + subs[1].ws.sat, {}};
1214 case Fragment::AND_B: return {subs[0].ws.sat + subs[1].ws.sat, subs[0].ws.dsat + subs[1].ws.dsat};
1215 case Fragment::OR_B: {
1216 const auto sat{(subs[0].ws.dsat + subs[1].ws.sat) | (subs[0].ws.sat + subs[1].ws.dsat)};
1217 const auto dsat{subs[0].ws.dsat + subs[1].ws.dsat};
1218 return {sat, dsat};
1219 }
1220 case Fragment::OR_C: return {subs[0].ws.sat | (subs[0].ws.dsat + subs[1].ws.sat), {}};
1221 case Fragment::OR_D: return {subs[0].ws.sat | (subs[0].ws.dsat + subs[1].ws.sat), subs[0].ws.dsat + subs[1].ws.dsat};
1222 case Fragment::OR_I: return {(subs[0].ws.sat + 1 + 1) | (subs[1].ws.sat + 1), (subs[0].ws.dsat + 1 + 1) | (subs[1].ws.dsat + 1)};
1223 case Fragment::MULTI: return {k * sig_size + 1, k + 1};
1224 case Fragment::MULTI_A: return {k * sig_size + static_cast<uint32_t>(keys.size()) - k, static_cast<uint32_t>(keys.size())};
1225 case Fragment::WRAP_A:
1226 case Fragment::WRAP_N:
1227 case Fragment::WRAP_S:
1228 case Fragment::WRAP_C: return subs[0].ws;
1229 case Fragment::WRAP_D: return {1 + 1 + subs[0].ws.sat, 1};
1230 case Fragment::WRAP_V: return {subs[0].ws.sat, {}};
1231 case Fragment::WRAP_J: return {subs[0].ws.sat, 1};
1232 case Fragment::THRESH: {
1233 auto sats = Vector(internal::MaxInt<uint32_t>(0));
1234 for (const auto& sub : subs) {
1235 auto next_sats = Vector(sats[0] + sub.ws.dsat);
1236 for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + sub.ws.dsat) | (sats[j - 1] + sub.ws.sat));
1237 next_sats.push_back(sats[sats.size() - 1] + sub.ws.sat);
1238 sats = std::move(next_sats);
1239 }
1240 assert(k < sats.size());
1241 return {sats[k], sats[0]};
1242 }
1243 }
1244 assert(false);
1245 }
1246
1247 template<typename Ctx>
1248 internal::InputResult ProduceInput(const Ctx& ctx) const {
1249 using namespace internal;
1250
1251 // Internal function which is invoked for every tree node, constructing satisfaction/dissatisfactions
1252 // given those of its subnodes.
1253 auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1254 switch (node.fragment) {
1255 case Fragment::PK_K: {
1256 std::vector<unsigned char> sig;
1257 Availability avail = ctx.Sign(node.keys[0], sig);
1258 return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1259 }
1260 case Fragment::PK_H: {
1261 std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1262 Availability avail = ctx.Sign(node.keys[0], sig);
1263 return {ZERO + InputStack(key), (InputStack(std::move(sig)).SetWithSig() + InputStack(key)).SetAvailable(avail)};
1264 }
1265 case Fragment::MULTI_A: {
1266 // sats[j] represents the best stack containing j valid signatures (out of the first i keys).
1267 // In the loop below, these stacks are built up using a dynamic programming approach.
1268 std::vector<InputStack> sats = Vector(EMPTY);
1269 for (size_t i = 0; i < node.keys.size(); ++i) {
1270 // Get the signature for the i'th key in reverse order (the signature for the first key needs to
1271 // be at the top of the stack, contrary to CHECKMULTISIG's satisfaction).
1272 std::vector<unsigned char> sig;
1273 Availability avail = ctx.Sign(node.keys[node.keys.size() - 1 - i], sig);
1274 // Compute signature stack for just this key.
1275 auto sat = InputStack(std::move(sig)).SetWithSig().SetAvailable(avail);
1276 // Compute the next sats vector: next_sats[0] is a copy of sats[0] (no signatures). All further
1277 // next_sats[j] are equal to either the existing sats[j] + ZERO, or sats[j-1] plus a signature
1278 // for the current (i'th) key. The very last element needs all signatures filled.
1279 std::vector<InputStack> next_sats;
1280 next_sats.push_back(sats[0] + ZERO);
1281 for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + ZERO) | (std::move(sats[j - 1]) + sat));
1282 next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1283 // Switch over.
1284 sats = std::move(next_sats);
1285 }
1286 // The dissatisfaction consists of as many empty vectors as there are keys, which is the same as
1287 // satisfying 0 keys.
1288 auto& nsat{sats[0]};
1289 CHECK_NONFATAL(node.k != 0);
1290 assert(node.k < sats.size());
1291 return {std::move(nsat), std::move(sats[node.k])};
1292 }
1293 case Fragment::MULTI: {
1294 // sats[j] represents the best stack containing j valid signatures (out of the first i keys).
1295 // In the loop below, these stacks are built up using a dynamic programming approach.
1296 // sats[0] starts off being {0}, due to the CHECKMULTISIG bug that pops off one element too many.
1297 std::vector<InputStack> sats = Vector(ZERO);
1298 for (size_t i = 0; i < node.keys.size(); ++i) {
1299 std::vector<unsigned char> sig;
1300 Availability avail = ctx.Sign(node.keys[i], sig);
1301 // Compute signature stack for just the i'th key.
1302 auto sat = InputStack(std::move(sig)).SetWithSig().SetAvailable(avail);
1303 // Compute the next sats vector: next_sats[0] is a copy of sats[0] (no signatures). All further
1304 // next_sats[j] are equal to either the existing sats[j], or sats[j-1] plus a signature for the
1305 // current (i'th) key. The very last element needs all signatures filled.
1306 std::vector<InputStack> next_sats;
1307 next_sats.push_back(sats[0]);
1308 for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back(sats[j] | (std::move(sats[j - 1]) + sat));
1309 next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1310 // Switch over.
1311 sats = std::move(next_sats);
1312 }
1313 // The dissatisfaction consists of k+1 stack elements all equal to 0.
1314 InputStack nsat = ZERO;
1315 for (size_t i = 0; i < node.k; ++i) nsat = std::move(nsat) + ZERO;
1316 assert(node.k < sats.size());
1317 return {std::move(nsat), std::move(sats[node.k])};
1318 }
1319 case Fragment::THRESH: {
1320 // sats[k] represents the best stack that satisfies k out of the *last* i subexpressions.
1321 // In the loop below, these stacks are built up using a dynamic programming approach.
1322 // sats[0] starts off empty.
1323 std::vector<InputStack> sats = Vector(EMPTY);
1324 for (size_t i = 0; i < subres.size(); ++i) {
1325 // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1326 auto& res = subres[subres.size() - i - 1];
1327 // Compute the next sats vector: next_sats[0] is sats[0] plus res.nsat (thus containing all dissatisfactions
1328 // so far. next_sats[j] is either sats[j] + res.nsat (reusing j earlier satisfactions) or sats[j-1] + res.sat
1329 // (reusing j-1 earlier satisfactions plus a new one). The very last next_sats[j] is all satisfactions.
1330 std::vector<InputStack> next_sats;
1331 next_sats.push_back(sats[0] + res.nsat);
1332 for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + res.nsat) | (std::move(sats[j - 1]) + res.sat));
1333 next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1334 // Switch over.
1335 sats = std::move(next_sats);
1336 }
1337 // At this point, sats[k].sat is the best satisfaction for the overall thresh() node. The best dissatisfaction
1338 // is computed by gathering all sats[i].nsat for i != k.
1339 InputStack nsat = INVALID;
1340 for (size_t i = 0; i < sats.size(); ++i) {
1341 // i==k is the satisfaction; i==0 is the canonical dissatisfaction;
1342 // the rest are non-canonical (a no-signature dissatisfaction - the i=0
1343 // form - is always available) and malleable (due to overcompleteness).
1344 // Marking the solutions malleable here is not strictly necessary, as they
1345 // should already never be picked in non-malleable solutions due to the
1346 // availability of the i=0 form.
1347 if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
1348 // Include all dissatisfactions (even these non-canonical ones) in nsat.
1349 if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
1350 }
1351 assert(node.k < sats.size());
1352 return {std::move(nsat), std::move(sats[node.k])};
1353 }
1354 case Fragment::OLDER: {
1355 return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
1356 }
1357 case Fragment::AFTER: {
1358 return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
1359 }
1360 case Fragment::SHA256: {
1361 std::vector<unsigned char> preimage;
1362 Availability avail = ctx.SatSHA256(node.data, preimage);
1363 return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1364 }
1365 case Fragment::RIPEMD160: {
1366 std::vector<unsigned char> preimage;
1367 Availability avail = ctx.SatRIPEMD160(node.data, preimage);
1368 return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1369 }
1370 case Fragment::HASH256: {
1371 std::vector<unsigned char> preimage;
1372 Availability avail = ctx.SatHASH256(node.data, preimage);
1373 return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1374 }
1375 case Fragment::HASH160: {
1376 std::vector<unsigned char> preimage;
1377 Availability avail = ctx.SatHASH160(node.data, preimage);
1378 return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1379 }
1380 case Fragment::AND_V: {
1381 auto& x = subres[0], &y = subres[1];
1382 // As the dissatisfaction here only consist of a single option, it doesn't
1383 // actually need to be listed (it's not required for reasoning about malleability of
1384 // other options), and is never required (no valid miniscript relies on the ability
1385 // to satisfy the type V left subexpression). It's still listed here for
1386 // completeness, as a hypothetical (not currently implemented) satisfier that doesn't
1387 // care about malleability might in some cases prefer it still.
1388 return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1389 }
1390 case Fragment::AND_B: {
1391 auto& x = subres[0], &y = subres[1];
1392 // Note that it is not strictly necessary to mark the 2nd and 3rd dissatisfaction here
1393 // as malleable. While they are definitely malleable, they are also non-canonical due
1394 // to the guaranteed existence of a no-signature other dissatisfaction (the 1st)
1395 // option. Because of that, the 2nd and 3rd option will never be chosen, even if they
1396 // weren't marked as malleable.
1397 return {(y.nsat + x.nsat) | (y.sat + x.nsat).SetMalleable().SetNonCanon() | (y.nsat + x.sat).SetMalleable().SetNonCanon(), y.sat + x.sat};
1398 }
1399 case Fragment::OR_B: {
1400 auto& x = subres[0], &z = subres[1];
1401 // The (sat(Z) sat(X)) solution is overcomplete (attacker can change either into dsat).
1402 return {z.nsat + x.nsat, (z.nsat + x.sat) | (z.sat + x.nsat) | (z.sat + x.sat).SetMalleable().SetNonCanon()};
1403 }
1404 case Fragment::OR_C: {
1405 auto& x = subres[0], &z = subres[1];
1406 return {INVALID, std::move(x.sat) | (z.sat + x.nsat)};
1407 }
1408 case Fragment::OR_D: {
1409 auto& x = subres[0], &z = subres[1];
1410 return {z.nsat + x.nsat, std::move(x.sat) | (z.sat + x.nsat)};
1411 }
1412 case Fragment::OR_I: {
1413 auto& x = subres[0], &z = subres[1];
1414 return {(x.nsat + ONE) | (z.nsat + ZERO), (x.sat + ONE) | (z.sat + ZERO)};
1415 }
1416 case Fragment::ANDOR: {
1417 auto& x = subres[0], &y = subres[1], &z = subres[2];
1418 return {(y.nsat + x.sat).SetNonCanon() | (z.nsat + x.nsat), (y.sat + x.sat) | (z.sat + x.nsat)};
1419 }
1420 case Fragment::WRAP_A:
1421 case Fragment::WRAP_S:
1422 case Fragment::WRAP_C:
1423 case Fragment::WRAP_N:
1424 return std::move(subres[0]);
1425 case Fragment::WRAP_D: {
1426 auto &x = subres[0];
1427 return {ZERO, x.sat + ONE};
1428 }
1429 case Fragment::WRAP_J: {
1430 auto &x = subres[0];
1431 // If a dissatisfaction with a nonzero top stack element exists, an alternative dissatisfaction exists.
1432 // As the dissatisfaction logic currently doesn't keep track of this nonzeroness property, and thus even
1433 // if a dissatisfaction with a top zero element is found, we don't know whether another one with a
1434 // nonzero top stack element exists. Make the conservative assumption that whenever the subexpression is weakly
1435 // dissatisfiable, this alternative dissatisfaction exists and leads to malleability.
1436 return {InputStack(ZERO).SetMalleable(x.nsat.available != Availability::NO && !x.nsat.has_sig), std::move(x.sat)};
1437 }
1438 case Fragment::WRAP_V: {
1439 auto &x = subres[0];
1440 return {INVALID, std::move(x.sat)};
1441 }
1442 case Fragment::JUST_0: return {EMPTY, INVALID};
1443 case Fragment::JUST_1: return {INVALID, EMPTY};
1444 }
1445 assert(false);
1446 return {INVALID, INVALID};
1447 };
1448
1449 auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1450 auto ret = helper(node, subres);
1451
1452 // Do a consistency check between the satisfaction code and the type checker
1453 // (the actual satisfaction code in ProduceInputHelper does not use GetType)
1454
1455 // For 'z' nodes, available satisfactions/dissatisfactions must have stack size 0.
1456 if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
1457 if (node.GetType() << "z"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 0);
1458
1459 // For 'o' nodes, available satisfactions/dissatisfactions must have stack size 1.
1460 if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
1461 if (node.GetType() << "o"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 1);
1462
1463 // For 'n' nodes, available satisfactions/dissatisfactions must have stack size 1 or larger. For satisfactions,
1464 // the top element cannot be 0.
1465 if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
1466 if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
1467 if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.stack.back().empty());
1468
1469 // For 'd' nodes, a dissatisfaction must exist, and they must not need a signature. If it is non-malleable,
1470 // it must be canonical.
1471 if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1472 if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
1473 if (node.GetType() << "d"_mst && !ret.nsat.malleable) CHECK_NONFATAL(!ret.nsat.non_canon);
1474
1475 // For 'f'/'s' nodes, dissatisfactions/satisfactions must have a signature.
1476 if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
1477 if (node.GetType() << "s"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.has_sig);
1478
1479 // For non-malleable 'e' nodes, a non-malleable dissatisfaction must exist.
1480 if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1481 if (node.GetType() << "me"_mst) CHECK_NONFATAL(!ret.nsat.malleable);
1482
1483 // For 'm' nodes, if a satisfaction exists, it must be non-malleable.
1484 if (node.GetType() << "m"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.malleable);
1485
1486 // If a non-malleable satisfaction exists, it must be canonical.
1487 if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
1488
1489 return ret;
1490 };
1491
1492 return TreeEval<InputResult>(tester);
1493 }
1494
1495public:
1501 template<typename Ctx> void DuplicateKeyCheck(const Ctx& ctx) const
1502 {
1503 // We cannot use a lambda here, as lambdas are non assignable, and the set operations
1504 // below require moving the comparators around.
1505 struct Comp {
1506 const Ctx* ctx_ptr;
1507 Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
1508 bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
1509 };
1510
1511 // state in the recursive computation:
1512 // - std::nullopt means "this node has duplicates"
1513 // - an std::set means "this node has no duplicate keys, and they are: ...".
1514 using keyset = std::set<Key, Comp>;
1515 using state = std::optional<keyset>;
1516
1517 auto upfn = [&ctx](const Node& node, std::span<state> subs) -> state {
1518 // If this node is already known to have duplicates, nothing left to do.
1519 if (node.has_duplicate_keys.has_value() && *node.has_duplicate_keys) return {};
1520
1521 // Check if one of the children is already known to have duplicates.
1522 for (auto& sub : subs) {
1523 if (!sub.has_value()) {
1524 node.has_duplicate_keys = true;
1525 return {};
1526 }
1527 }
1528
1529 // Start building the set of keys involved in this node and children.
1530 // Start by keys in this node directly.
1531 size_t keys_count = node.keys.size();
1532 keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1533 if (key_set.size() != keys_count) {
1534 // It already has duplicates; bail out.
1535 node.has_duplicate_keys = true;
1536 return {};
1537 }
1538
1539 // Merge the keys from the children into this set.
1540 for (auto& sub : subs) {
1541 keys_count += sub->size();
1542 // Small optimization: std::set::merge is linear in the size of the second arg but
1543 // logarithmic in the size of the first.
1544 if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1545 key_set.merge(*sub);
1546 if (key_set.size() != keys_count) {
1547 node.has_duplicate_keys = true;
1548 return {};
1549 }
1550 }
1551
1552 node.has_duplicate_keys = false;
1553 return key_set;
1554 };
1555
1556 TreeEval<state>(upfn);
1557 }
1558
1560 size_t ScriptSize() const { return scriptlen; }
1561
1563 std::optional<uint32_t> GetOps() const {
1564 if (!ops.sat.Valid()) return {};
1565 return ops.count + ops.sat.Value();
1566 }
1567
1569 uint32_t GetStaticOps() const { return ops.count; }
1570
1572 bool CheckOpsLimit() const {
1573 if (IsTapscript(m_script_ctx)) return true;
1574 if (const auto ops = GetOps()) return *ops <= MAX_OPS_PER_SCRIPT;
1575 return true;
1576 }
1577
1579 bool IsBKW() const {
1580 return !((GetType() & "BKW"_mst) == ""_mst);
1581 }
1582
1584 std::optional<uint32_t> GetStackSize() const {
1585 if (!ss.Sat().Valid()) return {};
1586 return ss.Sat().NetDiff() + static_cast<int32_t>(IsBKW());
1587 }
1588
1590 std::optional<uint32_t> GetExecStackSize() const {
1591 if (!ss.Sat().Valid()) return {};
1592 return ss.Sat().Exec() + static_cast<int32_t>(IsBKW());
1593 }
1594
1596 bool CheckStackSize() const {
1597 // Since in Tapscript there is no standardness limit on the script and witness sizes, we may run
1598 // into the maximum stack size while executing the script. Make sure it doesn't happen.
1599 if (IsTapscript(m_script_ctx)) {
1600 if (const auto exec_ss = GetExecStackSize()) return exec_ss <= MAX_STACK_SIZE;
1601 return true;
1602 }
1603 if (const auto ss = GetStackSize()) return *ss <= MAX_STANDARD_P2WSH_STACK_ITEMS;
1604 return true;
1605 }
1606
1608 bool IsNotSatisfiable() const { return !GetStackSize(); }
1609
1612 std::optional<uint32_t> GetWitnessSize() const {
1613 if (!ws.sat.Valid()) return {};
1614 return ws.sat.Value();
1615 }
1616
1618 Type GetType() const { return typ; }
1619
1621 MiniscriptContext GetMsCtx() const { return m_script_ctx; }
1622
1624 const Node* FindInsaneSub() const {
1625 return TreeEval<const Node*>([](const Node& node, std::span<const Node*> subs) -> const Node* {
1626 for (auto& sub: subs) if (sub) return sub;
1627 if (!node.IsSaneSubexpression()) return &node;
1628 return nullptr;
1629 });
1630 }
1631
1634 template<typename F>
1635 bool IsSatisfiable(F fn) const
1636 {
1637 // TreeEval() doesn't support bool as NodeType, so use int instead.
1638 return TreeEval<int>([&fn](const Node& node, std::span<int> subs) -> bool {
1639 switch (node.fragment) {
1640 case Fragment::JUST_0:
1641 return false;
1642 case Fragment::JUST_1:
1643 return true;
1644 case Fragment::PK_K:
1645 case Fragment::PK_H:
1646 case Fragment::MULTI:
1647 case Fragment::MULTI_A:
1648 case Fragment::AFTER:
1649 case Fragment::OLDER:
1650 case Fragment::HASH256:
1651 case Fragment::HASH160:
1652 case Fragment::SHA256:
1653 case Fragment::RIPEMD160:
1654 return bool{fn(node)};
1655 case Fragment::ANDOR:
1656 return (subs[0] && subs[1]) || subs[2];
1657 case Fragment::AND_V:
1658 case Fragment::AND_B:
1659 return subs[0] && subs[1];
1660 case Fragment::OR_B:
1661 case Fragment::OR_C:
1662 case Fragment::OR_D:
1663 case Fragment::OR_I:
1664 return subs[0] || subs[1];
1665 case Fragment::THRESH:
1666 return static_cast<uint32_t>(std::count(subs.begin(), subs.end(), true)) >= node.k;
1667 default: // wrappers
1668 assert(subs.size() >= 1);
1669 CHECK_NONFATAL(subs.size() == 1);
1670 return subs[0];
1671 }
1672 });
1673 }
1674
1676 bool IsValid() const {
1677 if (GetType() == ""_mst) return false;
1678 return ScriptSize() <= internal::MaxScriptSize(m_script_ctx);
1679 }
1680
1682 bool IsValidTopLevel() const { return IsValid() && GetType() << "B"_mst; }
1683
1685 bool IsNonMalleable() const { return GetType() << "m"_mst; }
1686
1688 bool NeedsSignature() const { return GetType() << "s"_mst; }
1689
1691 bool CheckTimeLocksMix() const { return GetType() << "k"_mst; }
1692
1694 bool CheckDuplicateKey() const { return has_duplicate_keys && !*has_duplicate_keys; }
1695
1697 bool ValidSatisfactions() const { return IsValid() && CheckOpsLimit() && CheckStackSize(); }
1698
1700 bool IsSaneSubexpression() const { return ValidSatisfactions() && IsNonMalleable() && CheckTimeLocksMix() && CheckDuplicateKey(); }
1701
1703 bool IsSane() const { return IsValidTopLevel() && IsSaneSubexpression() && NeedsSignature(); }
1704
1709 template<typename Ctx>
1710 Availability Satisfy(const Ctx& ctx, std::vector<std::vector<unsigned char>>& stack, bool nonmalleable = true) const {
1711 auto ret = ProduceInput(ctx);
1712 if (nonmalleable && (ret.sat.malleable || !ret.sat.has_sig)) return Availability::NO;
1713 stack = std::move(ret.sat.stack);
1714 return ret.sat.available;
1715 }
1716
1718 bool operator==(const Node<Key>& arg) const { return Compare(*this, arg) == 0; }
1719
1720 // Constructors with various argument combinations, which bypass the duplicate key check.
1721 Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector<Node> sub, std::vector<unsigned char> arg, uint32_t val = 0)
1722 : fragment(nt), k(val), data(std::move(arg)), subs(std::move(sub)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
1723 Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector<unsigned char> arg, uint32_t val = 0)
1724 : fragment(nt), k(val), data(std::move(arg)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
1725 Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector<Node> sub, std::vector<Key> key, uint32_t val = 0)
1726 : fragment(nt), k(val), keys(std::move(key)), m_script_ctx{script_ctx}, subs(std::move(sub)), ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
1727 Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector<Key> key, uint32_t val = 0)
1728 : fragment(nt), k(val), keys(std::move(key)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
1729 Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector<Node> sub, uint32_t val = 0)
1730 : fragment(nt), k(val), subs(std::move(sub)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
1731 Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, uint32_t val = 0)
1732 : fragment(nt), k(val), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
1733
1734 // Constructors with various argument combinations, which do perform the duplicate key check.
1735 template <typename Ctx> Node(const Ctx& ctx, enum Fragment nt, std::vector<Node> sub, std::vector<unsigned char> arg, uint32_t val = 0)
1736 : Node(internal::NoDupCheck{}, ctx.MsContext(), nt, std::move(sub), std::move(arg), val) { DuplicateKeyCheck(ctx); }
1737 template <typename Ctx> Node(const Ctx& ctx, enum Fragment nt, std::vector<unsigned char> arg, uint32_t val = 0)
1738 : Node(internal::NoDupCheck{}, ctx.MsContext(), nt, std::move(arg), val) { DuplicateKeyCheck(ctx);}
1739 template <typename Ctx> Node(const Ctx& ctx, enum Fragment nt, std::vector<Node> sub, std::vector<Key> key, uint32_t val = 0)
1740 : Node(internal::NoDupCheck{}, ctx.MsContext(), nt, std::move(sub), std::move(key), val) { DuplicateKeyCheck(ctx); }
1741 template <typename Ctx> Node(const Ctx& ctx, enum Fragment nt, std::vector<Key> key, uint32_t val = 0)
1742 : Node(internal::NoDupCheck{}, ctx.MsContext(), nt, std::move(key), val) { DuplicateKeyCheck(ctx); }
1743 template <typename Ctx> Node(const Ctx& ctx, enum Fragment nt, std::vector<Node> sub, uint32_t val = 0)
1744 : Node(internal::NoDupCheck{}, ctx.MsContext(), nt, std::move(sub), val) { DuplicateKeyCheck(ctx); }
1745 template <typename Ctx> Node(const Ctx& ctx, enum Fragment nt, uint32_t val = 0)
1746 : Node(internal::NoDupCheck{}, ctx.MsContext(), nt, val) { DuplicateKeyCheck(ctx); }
1747
1748 // Delete copy constructor and assignment operator, use Clone() instead
1749 Node(const Node&) = delete;
1750 Node& operator=(const Node&) = delete;
1751
1752 // subs is movable, circumventing recursion, so these are permitted.
1753 Node(Node&&) noexcept = default;
1754 Node& operator=(Node&&) noexcept = default;
1755};
1756
1757namespace internal {
1758
1759enum class ParseContext {
1763 EXPR,
1764
1766 SWAP,
1768 ALT,
1770 CHECK,
1772 DUP_IF,
1774 VERIFY,
1776 NON_ZERO,
1780 WRAP_U,
1782 WRAP_T,
1783
1785 AND_N,
1787 AND_V,
1789 AND_B,
1791 ANDOR,
1793 OR_B,
1795 OR_C,
1797 OR_D,
1799 OR_I,
1800
1805 THRESH,
1806
1808 COMMA,
1811};
1812
1813int FindNextChar(std::span<const char> in, char m);
1814
1816template<typename Key, typename Ctx>
1817std::optional<Key> ParseKey(const std::string& func, std::span<const char>& in, const Ctx& ctx)
1818{
1819 std::span<const char> expr = script::Expr(in);
1820 if (!script::Func(func, expr)) return {};
1821 return ctx.FromString(expr);
1822}
1823
1825template<typename Ctx>
1826std::optional<std::vector<unsigned char>> ParseHexStr(const std::string& func, std::span<const char>& in, const size_t expected_size,
1827 const Ctx& ctx)
1828{
1829 std::span<const char> expr = script::Expr(in);
1830 if (!script::Func(func, expr)) return {};
1831 std::string val = std::string(expr.begin(), expr.end());
1832 if (!IsHex(val)) return {};
1833 auto hash = ParseHex(val);
1834 if (hash.size() != expected_size) return {};
1835 return hash;
1836}
1837
1839template<typename Key>
1840void BuildBack(const MiniscriptContext script_ctx, Fragment nt, std::vector<Node<Key>>& constructed, const bool reverse = false)
1841{
1842 Node<Key> child{std::move(constructed.back())};
1843 constructed.pop_back();
1844 if (reverse) {
1845 constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(child), std::move(constructed.back()))};
1846 } else {
1847 constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(constructed.back()), std::move(child))};
1848 }
1849}
1850
1856template <typename Key, typename Ctx>
1857inline std::optional<Node<Key>> Parse(std::span<const char> in, const Ctx& ctx)
1858{
1859 using namespace script;
1860
1861 // Account for the minimum script size for all parsed fragments so far. It "borrows" 1
1862 // script byte from all leaf nodes, counting it instead whenever a space for a recursive
1863 // expression is added (through andor, and_*, or_*, thresh). This guarantees that all fragments
1864 // increment the script_size by at least one, except for:
1865 // - "0", "1": these leafs are only a single byte, so their subtracted-from increment is 0.
1866 // This is not an issue however, as "space" for them has to be created by combinators,
1867 // which do increment script_size.
1868 // - "v:": the v wrapper adds nothing as in some cases it results in no opcode being added
1869 // (instead transforming another opcode into its VERIFY form). However, the v: wrapper has
1870 // to be interleaved with other fragments to be valid, so this is not a concern.
1871 size_t script_size{1};
1872 size_t max_size{internal::MaxScriptSize(ctx.MsContext())};
1873
1874 // The two integers are used to hold state for thresh()
1875 std::vector<std::tuple<ParseContext, int64_t, int64_t>> to_parse;
1876 std::vector<Node<Key>> constructed;
1877
1878 to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
1879
1880 // Parses a multi() or multi_a() from its string representation. Returns false on parsing error.
1881 const auto parse_multi_exp = [&](std::span<const char>& in, const bool is_multi_a) -> bool {
1882 const auto max_keys{is_multi_a ? MAX_PUBKEYS_PER_MULTI_A : MAX_PUBKEYS_PER_MULTISIG};
1883 const auto required_ctx{is_multi_a ? MiniscriptContext::TAPSCRIPT : MiniscriptContext::P2WSH};
1884 if (ctx.MsContext() != required_ctx) return false;
1885 // Get threshold
1886 int next_comma = FindNextChar(in, ',');
1887 if (next_comma < 1) return false;
1888 const auto k_to_integral{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
1889 if (!k_to_integral.has_value()) return false;
1890 const int64_t k{k_to_integral.value()};
1891 in = in.subspan(next_comma + 1);
1892 // Get keys. It is compatible for both compressed and x-only keys.
1893 std::vector<Key> keys;
1894 while (next_comma != -1) {
1895 next_comma = FindNextChar(in, ',');
1896 int key_length = (next_comma == -1) ? FindNextChar(in, ')') : next_comma;
1897 if (key_length < 1) return false;
1898 std::span<const char> sp{in.begin(), in.begin() + key_length};
1899 auto key = ctx.FromString(sp);
1900 if (!key) return false;
1901 keys.push_back(std::move(*key));
1902 in = in.subspan(key_length + 1);
1903 }
1904 if (keys.size() < 1 || keys.size() > max_keys) return false;
1905 if (k < 1 || k > (int64_t)keys.size()) return false;
1906 if (is_multi_a) {
1907 // (push + xonly-key + CHECKSIG[ADD]) * n + k + OP_NUMEQUAL(VERIFY), minus one.
1908 script_size += (1 + 32 + 1) * keys.size() + BuildScript(k).size();
1909 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), k);
1910 } else {
1911 script_size += 2 + (keys.size() > 16) + (k > 16) + 34 * keys.size();
1912 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), k);
1913 }
1914 return true;
1915 };
1916
1917 while (!to_parse.empty()) {
1918 if (script_size > max_size) return {};
1919
1920 // Get the current context we are decoding within
1921 auto [cur_context, n, k] = to_parse.back();
1922 to_parse.pop_back();
1923
1924 switch (cur_context) {
1925 case ParseContext::WRAPPED_EXPR: {
1926 std::optional<size_t> colon_index{};
1927 for (size_t i = 1; i < in.size(); ++i) {
1928 if (in[i] == ':') {
1929 colon_index = i;
1930 break;
1931 }
1932 if (in[i] < 'a' || in[i] > 'z') break;
1933 }
1934 // If there is no colon, this loop won't execute
1935 bool last_was_v{false};
1936 for (size_t j = 0; colon_index && j < *colon_index; ++j) {
1937 if (script_size > max_size) return {};
1938 if (in[j] == 'a') {
1939 script_size += 2;
1940 to_parse.emplace_back(ParseContext::ALT, -1, -1);
1941 } else if (in[j] == 's') {
1942 script_size += 1;
1943 to_parse.emplace_back(ParseContext::SWAP, -1, -1);
1944 } else if (in[j] == 'c') {
1945 script_size += 1;
1946 to_parse.emplace_back(ParseContext::CHECK, -1, -1);
1947 } else if (in[j] == 'd') {
1948 script_size += 3;
1949 to_parse.emplace_back(ParseContext::DUP_IF, -1, -1);
1950 } else if (in[j] == 'j') {
1951 script_size += 4;
1952 to_parse.emplace_back(ParseContext::NON_ZERO, -1, -1);
1953 } else if (in[j] == 'n') {
1954 script_size += 1;
1955 to_parse.emplace_back(ParseContext::ZERO_NOTEQUAL, -1, -1);
1956 } else if (in[j] == 'v') {
1957 // do not permit "...vv...:"; it's not valid, and also doesn't trigger early
1958 // failure as script_size isn't incremented.
1959 if (last_was_v) return {};
1960 to_parse.emplace_back(ParseContext::VERIFY, -1, -1);
1961 } else if (in[j] == 'u') {
1962 script_size += 4;
1963 to_parse.emplace_back(ParseContext::WRAP_U, -1, -1);
1964 } else if (in[j] == 't') {
1965 script_size += 1;
1966 to_parse.emplace_back(ParseContext::WRAP_T, -1, -1);
1967 } else if (in[j] == 'l') {
1968 // The l: wrapper is equivalent to or_i(0,X)
1969 script_size += 4;
1970 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
1971 to_parse.emplace_back(ParseContext::OR_I, -1, -1);
1972 } else {
1973 return {};
1974 }
1975 last_was_v = (in[j] == 'v');
1976 }
1977 to_parse.emplace_back(ParseContext::EXPR, -1, -1);
1978 if (colon_index) in = in.subspan(*colon_index + 1);
1979 break;
1980 }
1981 case ParseContext::EXPR: {
1982 if (Const("0", in)) {
1983 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
1984 } else if (Const("1", in)) {
1985 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1);
1986 } else if (Const("pk(", in, /*skip=*/false)) {
1987 std::optional<Key> key = ParseKey<Key, Ctx>("pk", in, ctx);
1988 if (!key) return {};
1989 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(Node<Key>(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)))));
1990 script_size += IsTapscript(ctx.MsContext()) ? 33 : 34;
1991 } else if (Const("pkh(", in, /*skip=*/false)) {
1992 std::optional<Key> key = ParseKey<Key, Ctx>("pkh", in, ctx);
1993 if (!key) return {};
1994 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(Node<Key>(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)))));
1995 script_size += 24;
1996 } else if (Const("pk_k(", in, /*skip=*/false)) {
1997 std::optional<Key> key = ParseKey<Key, Ctx>("pk_k", in, ctx);
1998 if (!key) return {};
1999 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
2000 script_size += IsTapscript(ctx.MsContext()) ? 32 : 33;
2001 } else if (Const("pk_h(", in, /*skip=*/false)) {
2002 std::optional<Key> key = ParseKey<Key, Ctx>("pk_h", in, ctx);
2003 if (!key) return {};
2004 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2005 script_size += 23;
2006 } else if (Const("sha256(", in, /*skip=*/false)) {
2007 std::optional<std::vector<unsigned char>> hash = ParseHexStr("sha256", in, 32, ctx);
2008 if (!hash) return {};
2009 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, std::move(*hash));
2010 script_size += 38;
2011 } else if (Const("ripemd160(", in, /*skip=*/false)) {
2012 std::optional<std::vector<unsigned char>> hash = ParseHexStr("ripemd160", in, 20, ctx);
2013 if (!hash) return {};
2014 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::RIPEMD160, std::move(*hash));
2015 script_size += 26;
2016 } else if (Const("hash256(", in, /*skip=*/false)) {
2017 std::optional<std::vector<unsigned char>> hash = ParseHexStr("hash256", in, 32, ctx);
2018 if (!hash) return {};
2019 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH256, std::move(*hash));
2020 script_size += 38;
2021 } else if (Const("hash160(", in, /*skip=*/false)) {
2022 std::optional<std::vector<unsigned char>> hash = ParseHexStr("hash160", in, 20, ctx);
2023 if (!hash) return {};
2024 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, std::move(*hash));
2025 script_size += 26;
2026 } else if (Const("after(", in, /*skip=*/false)) {
2027 auto expr = Expr(in);
2028 if (!Func("after", expr)) return {};
2029 const auto num{ToIntegral<int64_t>(std::string_view(expr.begin(), expr.end()))};
2030 if (!num.has_value() || *num < 1 || *num >= 0x80000000L) return {};
2031 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2032 script_size += 1 + (*num > 16) + (*num > 0x7f) + (*num > 0x7fff) + (*num > 0x7fffff);
2033 } else if (Const("older(", in, /*skip=*/false)) {
2034 auto expr = Expr(in);
2035 if (!Func("older", expr)) return {};
2036 const auto num{ToIntegral<int64_t>(std::string_view(expr.begin(), expr.end()))};
2037 if (!num.has_value() || *num < 1 || *num >= 0x80000000L) return {};
2038 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2039 script_size += 1 + (*num > 16) + (*num > 0x7f) + (*num > 0x7fff) + (*num > 0x7fffff);
2040 } else if (Const("multi(", in)) {
2041 if (!parse_multi_exp(in, /* is_multi_a = */false)) return {};
2042 } else if (Const("multi_a(", in)) {
2043 if (!parse_multi_exp(in, /* is_multi_a = */true)) return {};
2044 } else if (Const("thresh(", in)) {
2045 int next_comma = FindNextChar(in, ',');
2046 if (next_comma < 1) return {};
2047 const auto k{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
2048 if (!k.has_value() || *k < 1) return {};
2049 in = in.subspan(next_comma + 1);
2050 // n = 1 here because we read the first WRAPPED_EXPR before reaching THRESH
2051 to_parse.emplace_back(ParseContext::THRESH, 1, *k);
2052 to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2053 script_size += 2 + (*k > 16) + (*k > 0x7f) + (*k > 0x7fff) + (*k > 0x7fffff);
2054 } else if (Const("andor(", in)) {
2055 to_parse.emplace_back(ParseContext::ANDOR, -1, -1);
2056 to_parse.emplace_back(ParseContext::CLOSE_BRACKET, -1, -1);
2057 to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2058 to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2059 to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2060 to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2061 to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2062 script_size += 5;
2063 } else {
2064 if (Const("and_n(", in)) {
2065 to_parse.emplace_back(ParseContext::AND_N, -1, -1);
2066 script_size += 5;
2067 } else if (Const("and_b(", in)) {
2068 to_parse.emplace_back(ParseContext::AND_B, -1, -1);
2069 script_size += 2;
2070 } else if (Const("and_v(", in)) {
2071 to_parse.emplace_back(ParseContext::AND_V, -1, -1);
2072 script_size += 1;
2073 } else if (Const("or_b(", in)) {
2074 to_parse.emplace_back(ParseContext::OR_B, -1, -1);
2075 script_size += 2;
2076 } else if (Const("or_c(", in)) {
2077 to_parse.emplace_back(ParseContext::OR_C, -1, -1);
2078 script_size += 3;
2079 } else if (Const("or_d(", in)) {
2080 to_parse.emplace_back(ParseContext::OR_D, -1, -1);
2081 script_size += 4;
2082 } else if (Const("or_i(", in)) {
2083 to_parse.emplace_back(ParseContext::OR_I, -1, -1);
2084 script_size += 4;
2085 } else {
2086 return {};
2087 }
2088 to_parse.emplace_back(ParseContext::CLOSE_BRACKET, -1, -1);
2089 to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2090 to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2091 to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2092 }
2093 break;
2094 }
2095 case ParseContext::ALT: {
2096 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2097 break;
2098 }
2099 case ParseContext::SWAP: {
2100 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2101 break;
2102 }
2103 case ParseContext::CHECK: {
2104 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2105 break;
2106 }
2107 case ParseContext::DUP_IF: {
2108 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2109 break;
2110 }
2111 case ParseContext::NON_ZERO: {
2112 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_J, Vector(std::move(constructed.back()))};
2113 break;
2114 }
2115 case ParseContext::ZERO_NOTEQUAL: {
2116 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2117 break;
2118 }
2119 case ParseContext::VERIFY: {
2120 script_size += (constructed.back().GetType() << "x"_mst);
2121 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2122 break;
2123 }
2124 case ParseContext::WRAP_U: {
2125 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::OR_I, Vector(std::move(constructed.back()), Node<Key>{internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0})};
2126 break;
2127 }
2128 case ParseContext::WRAP_T: {
2129 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::AND_V, Vector(std::move(constructed.back()), Node<Key>{internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1})};
2130 break;
2131 }
2132 case ParseContext::AND_B: {
2133 BuildBack(ctx.MsContext(), Fragment::AND_B, constructed);
2134 break;
2135 }
2136 case ParseContext::AND_N: {
2137 auto mid = std::move(constructed.back());
2138 constructed.pop_back();
2139 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(constructed.back()), std::move(mid), Node<Key>{internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0})};
2140 break;
2141 }
2142 case ParseContext::AND_V: {
2143 BuildBack(ctx.MsContext(), Fragment::AND_V, constructed);
2144 break;
2145 }
2146 case ParseContext::OR_B: {
2147 BuildBack(ctx.MsContext(), Fragment::OR_B, constructed);
2148 break;
2149 }
2150 case ParseContext::OR_C: {
2151 BuildBack(ctx.MsContext(), Fragment::OR_C, constructed);
2152 break;
2153 }
2154 case ParseContext::OR_D: {
2155 BuildBack(ctx.MsContext(), Fragment::OR_D, constructed);
2156 break;
2157 }
2158 case ParseContext::OR_I: {
2159 BuildBack(ctx.MsContext(), Fragment::OR_I, constructed);
2160 break;
2161 }
2162 case ParseContext::ANDOR: {
2163 auto right = std::move(constructed.back());
2164 constructed.pop_back();
2165 auto mid = std::move(constructed.back());
2166 constructed.pop_back();
2167 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(constructed.back()), std::move(mid), std::move(right))};
2168 break;
2169 }
2170 case ParseContext::THRESH: {
2171 if (in.size() < 1) return {};
2172 if (in[0] == ',') {
2173 in = in.subspan(1);
2174 to_parse.emplace_back(ParseContext::THRESH, n+1, k);
2175 to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2176 script_size += 2;
2177 } else if (in[0] == ')') {
2178 if (k > n) return {};
2179 in = in.subspan(1);
2180 // Children are constructed in reverse order, so iterate from end to beginning
2181 std::vector<Node<Key>> subs;
2182 for (int i = 0; i < n; ++i) {
2183 subs.push_back(std::move(constructed.back()));
2184 constructed.pop_back();
2185 }
2186 std::reverse(subs.begin(), subs.end());
2187 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2188 } else {
2189 return {};
2190 }
2191 break;
2192 }
2193 case ParseContext::COMMA: {
2194 if (in.size() < 1 || in[0] != ',') return {};
2195 in = in.subspan(1);
2196 break;
2197 }
2198 case ParseContext::CLOSE_BRACKET: {
2199 if (in.size() < 1 || in[0] != ')') return {};
2200 in = in.subspan(1);
2201 break;
2202 }
2203 }
2204 }
2205
2206 // Sanity checks on the produced miniscript
2207 assert(constructed.size() >= 1);
2208 CHECK_NONFATAL(constructed.size() == 1);
2209 assert(constructed[0].ScriptSize() == script_size);
2210 if (in.size() > 0) return {};
2211 Node<Key> tl_node{std::move(constructed.front())};
2212 tl_node.DuplicateKeyCheck(ctx);
2213 return tl_node;
2214}
2215
2224std::optional<std::vector<Opcode>> DecomposeScript(const CScript& script);
2225
2227std::optional<int64_t> ParseScriptNumber(const Opcode& in);
2228
2229enum class DecodeContext {
2235 BKV_EXPR,
2237 W_EXPR,
2238
2242 SWAP,
2245 ALT,
2247 CHECK,
2249 DUP_IF,
2251 VERIFY,
2253 NON_ZERO,
2256
2263 AND_V,
2265 AND_B,
2267 ANDOR,
2269 OR_B,
2271 OR_C,
2273 OR_D,
2274
2278 THRESH_W,
2281 THRESH_E,
2282
2286 ENDIF,
2294 ENDIF_ELSE,
2295};
2296
2298template <typename Key, typename Ctx, typename I>
2299inline std::optional<Node<Key>> DecodeScript(I& in, I last, const Ctx& ctx)
2300{
2301 // The two integers are used to hold state for thresh()
2302 std::vector<std::tuple<DecodeContext, int64_t, int64_t>> to_parse;
2303 std::vector<Node<Key>> constructed;
2304
2305 // This is the top level, so we assume the type is B
2306 // (in particular, disallowing top level W expressions)
2307 to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2308
2309 while (!to_parse.empty()) {
2310 // Exit early if the Miniscript is not going to be valid.
2311 if (!constructed.empty() && !constructed.back().IsValid()) return {};
2312
2313 // Get the current context we are decoding within
2314 auto [cur_context, n, k] = to_parse.back();
2315 to_parse.pop_back();
2316
2317 switch(cur_context) {
2318 case DecodeContext::SINGLE_BKV_EXPR: {
2319 if (in >= last) return {};
2320
2321 // Constants
2322 if (in[0].first == OP_1) {
2323 ++in;
2324 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1);
2325 break;
2326 }
2327 if (in[0].first == OP_0) {
2328 ++in;
2329 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
2330 break;
2331 }
2332 // Public keys
2333 if (in[0].second.size() == 33 || in[0].second.size() == 32) {
2334 auto key = ctx.FromPKBytes(in[0].second.begin(), in[0].second.end());
2335 if (!key) return {};
2336 ++in;
2337 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
2338 break;
2339 }
2340 if (last - in >= 5 && in[0].first == OP_VERIFY && in[1].first == OP_EQUAL && in[3].first == OP_HASH160 && in[4].first == OP_DUP && in[2].second.size() == 20) {
2341 auto key = ctx.FromPKHBytes(in[2].second.begin(), in[2].second.end());
2342 if (!key) return {};
2343 in += 5;
2344 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2345 break;
2346 }
2347 // Time locks
2348 std::optional<int64_t> num;
2349 if (last - in >= 2 && in[0].first == OP_CHECKSEQUENCEVERIFY && (num = ParseScriptNumber(in[1]))) {
2350 in += 2;
2351 if (*num < 1 || *num > 0x7FFFFFFFL) return {};
2352 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2353 break;
2354 }
2355 if (last - in >= 2 && in[0].first == OP_CHECKLOCKTIMEVERIFY && (num = ParseScriptNumber(in[1]))) {
2356 in += 2;
2357 if (num < 1 || num > 0x7FFFFFFFL) return {};
2358 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2359 break;
2360 }
2361 // Hashes
2362 if (last - in >= 7 && in[0].first == OP_EQUAL && in[3].first == OP_VERIFY && in[4].first == OP_EQUAL && (num = ParseScriptNumber(in[5])) && num == 32 && in[6].first == OP_SIZE) {
2363 if (in[2].first == OP_SHA256 && in[1].second.size() == 32) {
2364 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, in[1].second);
2365 in += 7;
2366 break;
2367 } else if (in[2].first == OP_RIPEMD160 && in[1].second.size() == 20) {
2368 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::RIPEMD160, in[1].second);
2369 in += 7;
2370 break;
2371 } else if (in[2].first == OP_HASH256 && in[1].second.size() == 32) {
2372 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH256, in[1].second);
2373 in += 7;
2374 break;
2375 } else if (in[2].first == OP_HASH160 && in[1].second.size() == 20) {
2376 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, in[1].second);
2377 in += 7;
2378 break;
2379 }
2380 }
2381 // Multi
2382 if (last - in >= 3 && in[0].first == OP_CHECKMULTISIG) {
2383 if (IsTapscript(ctx.MsContext())) return {};
2384 std::vector<Key> keys;
2385 const auto n = ParseScriptNumber(in[1]);
2386 if (!n || last - in < 3 + *n) return {};
2387 if (*n < 1 || *n > 20) return {};
2388 for (int i = 0; i < *n; ++i) {
2389 if (in[2 + i].second.size() != 33) return {};
2390 auto key = ctx.FromPKBytes(in[2 + i].second.begin(), in[2 + i].second.end());
2391 if (!key) return {};
2392 keys.push_back(std::move(*key));
2393 }
2394 const auto k = ParseScriptNumber(in[2 + *n]);
2395 if (!k || *k < 1 || *k > *n) return {};
2396 in += 3 + *n;
2397 std::reverse(keys.begin(), keys.end());
2398 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), *k);
2399 break;
2400 }
2401 // Tapscript's equivalent of multi
2402 if (last - in >= 4 && in[0].first == OP_NUMEQUAL) {
2403 if (!IsTapscript(ctx.MsContext())) return {};
2404 // The necessary threshold of signatures.
2405 const auto k = ParseScriptNumber(in[1]);
2406 if (!k) return {};
2407 if (*k < 1 || *k > MAX_PUBKEYS_PER_MULTI_A) return {};
2408 if (last - in < 2 + *k * 2) return {};
2409 std::vector<Key> keys;
2410 keys.reserve(*k);
2411 // Walk through the expected (pubkey, CHECKSIG[ADD]) pairs.
2412 for (int pos = 2;; pos += 2) {
2413 if (last - in < pos + 2) return {};
2414 // Make sure it's indeed an x-only pubkey and a CHECKSIG[ADD], then parse the key.
2415 if (in[pos].first != OP_CHECKSIGADD && in[pos].first != OP_CHECKSIG) return {};
2416 if (in[pos + 1].second.size() != 32) return {};
2417 auto key = ctx.FromPKBytes(in[pos + 1].second.begin(), in[pos + 1].second.end());
2418 if (!key) return {};
2419 keys.push_back(std::move(*key));
2420 // Make sure early we don't parse an arbitrary large expression.
2421 if (keys.size() > MAX_PUBKEYS_PER_MULTI_A) return {};
2422 // OP_CHECKSIG means it was the last one to parse.
2423 if (in[pos].first == OP_CHECKSIG) break;
2424 }
2425 if (keys.size() < (size_t)*k) return {};
2426 in += 2 + keys.size() * 2;
2427 std::reverse(keys.begin(), keys.end());
2428 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), *k);
2429 break;
2430 }
2434 // c: wrapper
2435 if (in[0].first == OP_CHECKSIG) {
2436 ++in;
2437 to_parse.emplace_back(DecodeContext::CHECK, -1, -1);
2438 to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2439 break;
2440 }
2441 // v: wrapper
2442 if (in[0].first == OP_VERIFY) {
2443 ++in;
2444 to_parse.emplace_back(DecodeContext::VERIFY, -1, -1);
2445 to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2446 break;
2447 }
2448 // n: wrapper
2449 if (in[0].first == OP_0NOTEQUAL) {
2450 ++in;
2451 to_parse.emplace_back(DecodeContext::ZERO_NOTEQUAL, -1, -1);
2452 to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2453 break;
2454 }
2455 // Thresh
2456 if (last - in >= 3 && in[0].first == OP_EQUAL && (num = ParseScriptNumber(in[1]))) {
2457 if (*num < 1) return {};
2458 in += 2;
2459 to_parse.emplace_back(DecodeContext::THRESH_W, 0, *num);
2460 break;
2461 }
2462 // OP_ENDIF can be WRAP_J, WRAP_D, ANDOR, OR_C, OR_D, or OR_I
2463 if (in[0].first == OP_ENDIF) {
2464 ++in;
2465 to_parse.emplace_back(DecodeContext::ENDIF, -1, -1);
2466 to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2467 break;
2468 }
2474 // and_b
2475 if (in[0].first == OP_BOOLAND) {
2476 ++in;
2477 to_parse.emplace_back(DecodeContext::AND_B, -1, -1);
2478 to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2479 to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2480 break;
2481 }
2482 // or_b
2483 if (in[0].first == OP_BOOLOR) {
2484 ++in;
2485 to_parse.emplace_back(DecodeContext::OR_B, -1, -1);
2486 to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2487 to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2488 break;
2489 }
2490 // Unrecognised expression
2491 return {};
2492 }
2493 case DecodeContext::BKV_EXPR: {
2494 to_parse.emplace_back(DecodeContext::MAYBE_AND_V, -1, -1);
2495 to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2496 break;
2497 }
2498 case DecodeContext::W_EXPR: {
2499 // a: wrapper
2500 if (in >= last) return {};
2501 if (in[0].first == OP_FROMALTSTACK) {
2502 ++in;
2503 to_parse.emplace_back(DecodeContext::ALT, -1, -1);
2504 } else {
2505 to_parse.emplace_back(DecodeContext::SWAP, -1, -1);
2506 }
2507 to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2508 break;
2509 }
2510 case DecodeContext::MAYBE_AND_V: {
2511 // If we reach a potential AND_V top-level, check if the next part of the script could be another AND_V child
2512 // These op-codes cannot end any well-formed miniscript so cannot be used in an and_v node.
2513 if (in < last && in[0].first != OP_IF && in[0].first != OP_ELSE && in[0].first != OP_NOTIF && in[0].first != OP_TOALTSTACK && in[0].first != OP_SWAP) {
2514 to_parse.emplace_back(DecodeContext::AND_V, -1, -1);
2515 // BKV_EXPR can contain more AND_V nodes
2516 to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2517 }
2518 break;
2519 }
2520 case DecodeContext::SWAP: {
2521 if (in >= last || in[0].first != OP_SWAP || constructed.empty()) return {};
2522 ++in;
2523 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2524 break;
2525 }
2526 case DecodeContext::ALT: {
2527 if (in >= last || in[0].first != OP_TOALTSTACK || constructed.empty()) return {};
2528 ++in;
2529 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2530 break;
2531 }
2532 case DecodeContext::CHECK: {
2533 if (constructed.empty()) return {};
2534 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2535 break;
2536 }
2537 case DecodeContext::DUP_IF: {
2538 if (constructed.empty()) return {};
2539 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2540 break;
2541 }
2542 case DecodeContext::VERIFY: {
2543 if (constructed.empty()) return {};
2544 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2545 break;
2546 }
2547 case DecodeContext::NON_ZERO: {
2548 if (constructed.empty()) return {};
2549 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_J, Vector(std::move(constructed.back()))};
2550 break;
2551 }
2552 case DecodeContext::ZERO_NOTEQUAL: {
2553 if (constructed.empty()) return {};
2554 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2555 break;
2556 }
2557 case DecodeContext::AND_V: {
2558 if (constructed.size() < 2) return {};
2559 BuildBack(ctx.MsContext(), Fragment::AND_V, constructed, /*reverse=*/true);
2560 break;
2561 }
2562 case DecodeContext::AND_B: {
2563 if (constructed.size() < 2) return {};
2564 BuildBack(ctx.MsContext(), Fragment::AND_B, constructed, /*reverse=*/true);
2565 break;
2566 }
2567 case DecodeContext::OR_B: {
2568 if (constructed.size() < 2) return {};
2569 BuildBack(ctx.MsContext(), Fragment::OR_B, constructed, /*reverse=*/true);
2570 break;
2571 }
2572 case DecodeContext::OR_C: {
2573 if (constructed.size() < 2) return {};
2574 BuildBack(ctx.MsContext(), Fragment::OR_C, constructed, /*reverse=*/true);
2575 break;
2576 }
2577 case DecodeContext::OR_D: {
2578 if (constructed.size() < 2) return {};
2579 BuildBack(ctx.MsContext(), Fragment::OR_D, constructed, /*reverse=*/true);
2580 break;
2581 }
2582 case DecodeContext::ANDOR: {
2583 if (constructed.size() < 3) return {};
2584 Node left{std::move(constructed.back())};
2585 constructed.pop_back();
2586 Node right{std::move(constructed.back())};
2587 constructed.pop_back();
2588 Node mid{std::move(constructed.back())};
2589 constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(left), std::move(mid), std::move(right))};
2590 break;
2591 }
2592 case DecodeContext::THRESH_W: {
2593 if (in >= last) return {};
2594 if (in[0].first == OP_ADD) {
2595 ++in;
2596 to_parse.emplace_back(DecodeContext::THRESH_W, n+1, k);
2597 to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2598 } else {
2599 to_parse.emplace_back(DecodeContext::THRESH_E, n+1, k);
2600 // All children of thresh have type modifier d, so cannot be and_v
2601 to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2602 }
2603 break;
2604 }
2605 case DecodeContext::THRESH_E: {
2606 if (k < 1 || k > n || constructed.size() < static_cast<size_t>(n)) return {};
2607 std::vector<Node<Key>> subs;
2608 for (int i = 0; i < n; ++i) {
2609 Node sub{std::move(constructed.back())};
2610 constructed.pop_back();
2611 subs.push_back(std::move(sub));
2612 }
2613 constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2614 break;
2615 }
2616 case DecodeContext::ENDIF: {
2617 if (in >= last) return {};
2618
2619 // could be andor or or_i
2620 if (in[0].first == OP_ELSE) {
2621 ++in;
2622 to_parse.emplace_back(DecodeContext::ENDIF_ELSE, -1, -1);
2623 to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2624 }
2625 // could be j: or d: wrapper
2626 else if (in[0].first == OP_IF) {
2627 if (last - in >= 2 && in[1].first == OP_DUP) {
2628 in += 2;
2629 to_parse.emplace_back(DecodeContext::DUP_IF, -1, -1);
2630 } else if (last - in >= 3 && in[1].first == OP_0NOTEQUAL && in[2].first == OP_SIZE) {
2631 in += 3;
2632 to_parse.emplace_back(DecodeContext::NON_ZERO, -1, -1);
2633 }
2634 else {
2635 return {};
2636 }
2637 // could be or_c or or_d
2638 } else if (in[0].first == OP_NOTIF) {
2639 ++in;
2640 to_parse.emplace_back(DecodeContext::ENDIF_NOTIF, -1, -1);
2641 }
2642 else {
2643 return {};
2644 }
2645 break;
2646 }
2647 case DecodeContext::ENDIF_NOTIF: {
2648 if (in >= last) return {};
2649 if (in[0].first == OP_IFDUP) {
2650 ++in;
2651 to_parse.emplace_back(DecodeContext::OR_D, -1, -1);
2652 } else {
2653 to_parse.emplace_back(DecodeContext::OR_C, -1, -1);
2654 }
2655 // or_c and or_d both require X to have type modifier d so, can't contain and_v
2656 to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2657 break;
2658 }
2659 case DecodeContext::ENDIF_ELSE: {
2660 if (in >= last) return {};
2661 if (in[0].first == OP_IF) {
2662 ++in;
2663 BuildBack(ctx.MsContext(), Fragment::OR_I, constructed, /*reverse=*/true);
2664 } else if (in[0].first == OP_NOTIF) {
2665 ++in;
2666 to_parse.emplace_back(DecodeContext::ANDOR, -1, -1);
2667 // andor requires X to have type modifier d, so it can't be and_v
2668 to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2669 } else {
2670 return {};
2671 }
2672 break;
2673 }
2674 }
2675 }
2676 if (constructed.size() != 1) return {};
2677 Node tl_node{std::move(constructed.front())};
2678 tl_node.DuplicateKeyCheck(ctx);
2679 // Note that due to how ComputeType works (only assign the type to the node if the
2680 // subs' types are valid) this would fail if any node of tree is badly typed.
2681 if (!tl_node.IsValidTopLevel()) return {};
2682 return tl_node;
2683}
2684
2685} // namespace internal
2686
2687template <typename Ctx>
2688inline std::optional<Node<typename Ctx::Key>> FromString(const std::string& str, const Ctx& ctx)
2689{
2690 return internal::Parse<typename Ctx::Key>(str, ctx);
2691}
2692
2693template <typename Ctx>
2694inline std::optional<Node<typename Ctx::Key>> FromScript(const CScript& script, const Ctx& ctx)
2695{
2696 using namespace internal;
2697 // A too large Script is necessarily invalid, don't bother parsing it.
2698 if (script.size() > MaxScriptSize(ctx.MsContext())) return {};
2699 auto decomposed = DecomposeScript(script);
2700 if (!decomposed) return {};
2701 auto it = decomposed->begin();
2702 auto ret = DecodeScript<typename Ctx::Key>(it, decomposed->end(), ctx);
2703 if (!ret) return {};
2704 if (it != decomposed->end()) return {};
2705 return ret;
2706}
2707
2708} // namespace miniscript
2709
2710#endif // BITCOIN_SCRIPT_MINISCRIPT_H
int ret
int flags
Definition: bitcoin-tx.cpp:530
#define CHECK_NONFATAL(condition)
Identity function.
Definition: check.h:112
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
A node in a miniscript expression.
Definition: miniscript.h:536
uint32_t GetStaticOps() const
Return the number of ops in the script (not counting the dynamic ones that depend on execution).
Definition: miniscript.h:1569
Result TreeEval(UpFn upfn) const
Like TreeEval, but without downfn or State type.
Definition: miniscript.h:748
const std::vector< Key > & Keys() const
Definition: miniscript.h:590
const Node * FindInsaneSub() const
Find an insane subnode which has no insane children. Nullptr if there is none.
Definition: miniscript.h:1624
Node & operator=(const Node &)=delete
bool IsBKW() const
Whether this node is of type B, K or W.
Definition: miniscript.h:1579
Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector< Node > sub, uint32_t val=0)
Definition: miniscript.h:1729
internal::InputResult ProduceInput(const Ctx &ctx) const
Definition: miniscript.h:1248
CScript ToScript(const Ctx &ctx) const
Definition: miniscript.h:799
bool CheckStackSize() const
Check the maximum stack size for this script against the policy limit.
Definition: miniscript.h:1596
Type typ
Cached expression type (computed by CalcType and fed through SanitizeType).
Definition: miniscript.h:602
std::vector< unsigned char > data
The data bytes in this expression (only for HASH160/HASH256/SHA256/RIPEMD160).
Definition: miniscript.h:544
internal::StackSize CalcStackSize() const
Definition: miniscript.h:1079
bool IsSaneSubexpression() const
Whether the apparent policy of this node matches its script semantics. Doesn't guarantee it is a safe...
Definition: miniscript.h:1700
Type GetType() const
Return the expression type.
Definition: miniscript.h:1618
enum Fragment Fragment() const
Definition: miniscript.h:588
const std::vector< unsigned char > & Data() const
Definition: miniscript.h:591
Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector< Node > sub, std::vector< unsigned char > arg, uint32_t val=0)
Definition: miniscript.h:1721
friend int Compare(const Node< Key > &node1, const Node< Key > &node2)
Compare two miniscript subtrees, using a non-recursive algorithm.
Definition: miniscript.h:761
std::optional< uint32_t > GetStackSize() const
Return the maximum number of stack elements needed to satisfy this script non-malleably.
Definition: miniscript.h:1584
std::optional< bool > has_duplicate_keys
Whether a public key appears more than once in this node.
Definition: miniscript.h:610
std::optional< uint32_t > GetExecStackSize() const
Return the maximum size of the stack during execution of this script.
Definition: miniscript.h:1590
Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector< Key > key, uint32_t val=0)
Definition: miniscript.h:1727
bool NeedsSignature() const
Check whether this script always needs a signature.
Definition: miniscript.h:1688
std::vector< Node > subs
Subexpressions (for WRAP_*‍/AND_*‍/OR_*‍/ANDOR/THRESH)
Definition: miniscript.h:546
std::optional< std::string > ToString(const CTx &ctx, bool &has_priv_key) const
Definition: miniscript.h:884
internal::WitnessSize ws
Cached witness size bounds.
Definition: miniscript.h:600
bool CheckOpsLimit() const
Check the ops limit of this script against the consensus limit.
Definition: miniscript.h:1572
std::vector< Key > keys
The keys used by this expression (only for PK_K/PK_H/MULTI)
Definition: miniscript.h:542
std::optional< uint32_t > GetWitnessSize() const
Return the maximum size in bytes of a witness to satisfy this script non-malleably.
Definition: miniscript.h:1612
Node(const Node &)=delete
uint32_t k
The k parameter (time for OLDER/AFTER, threshold for THRESH(_M))
Definition: miniscript.h:540
Node< Key > Clone() const
Definition: miniscript.h:573
Node(const Ctx &ctx, enum Fragment nt, std::vector< Node > sub, std::vector< unsigned char > arg, uint32_t val=0)
Definition: miniscript.h:1735
std::optional< Result > TreeEvalMaybe(UpFn upfn) const
Like TreeEvalMaybe, but without downfn or State type.
Definition: miniscript.h:719
internal::WitnessSize CalcWitnessSize() const
Definition: miniscript.h:1194
Node(Node &&) noexcept=default
Result TreeEval(State root_state, DownFn &&downfn, UpFn upfn) const
Like TreeEvalMaybe, but always produces a result.
Definition: miniscript.h:732
uint32_t K() const
Definition: miniscript.h:589
internal::Ops CalcOps() const
Definition: miniscript.h:1005
internal::StackSize ss
Cached stack size bounds.
Definition: miniscript.h:598
Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector< Node > sub, std::vector< Key > key, std::vector< unsigned char > arg, uint32_t val)
Definition: miniscript.h:615
std::optional< std::string > ToString(const CTx &ctx) const
Definition: miniscript.h:878
const std::vector< Node > & Subs() const
Definition: miniscript.h:592
Node(const Ctx &ctx, enum Fragment nt, std::vector< Key > key, uint32_t val=0)
Definition: miniscript.h:1741
size_t CalcScriptLen() const
Compute the length of the script for this miniscript (including children).
Definition: miniscript.h:619
Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector< Node > sub, std::vector< Key > key, uint32_t val=0)
Definition: miniscript.h:1725
Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, uint32_t val=0)
Definition: miniscript.h:1731
std::optional< Result > TreeEvalMaybe(State root_state, DownFn downfn, UpFn upfn) const
Definition: miniscript.h:653
bool IsSane() const
Check whether this node is safe as a script on its own.
Definition: miniscript.h:1703
bool IsValidTopLevel() const
Check whether this node is valid as a script on its own.
Definition: miniscript.h:1682
enum Fragment fragment
What node type this node is.
Definition: miniscript.h:538
Node(const Ctx &ctx, enum Fragment nt, std::vector< Node > sub, std::vector< Key > key, uint32_t val=0)
Definition: miniscript.h:1739
bool IsNotSatisfiable() const
Whether no satisfaction exists for this node.
Definition: miniscript.h:1608
bool IsNonMalleable() const
Check whether this script can always be satisfied in a non-malleable way.
Definition: miniscript.h:1685
Type CalcType() const
Compute the type for this miniscript.
Definition: miniscript.h:781
bool CheckDuplicateKey() const
Check whether there is no duplicate key across this fragment and all its sub-fragments.
Definition: miniscript.h:1694
size_t ScriptSize() const
Return the size of the script for this expression (faster than ToScript().size()).
Definition: miniscript.h:1560
MiniscriptContext m_script_ctx
The Script context for this node. Either P2WSH or Tapscript.
Definition: miniscript.h:548
bool ValidSatisfactions() const
Whether successful non-malleable satisfactions are guaranteed to be valid.
Definition: miniscript.h:1697
void DuplicateKeyCheck(const Ctx &ctx) const
Update duplicate key information in this Node.
Definition: miniscript.h:1501
Node(const Ctx &ctx, enum Fragment nt, std::vector< unsigned char > arg, uint32_t val=0)
Definition: miniscript.h:1737
bool operator==(const Node< Key > &arg) const
Equality testing.
Definition: miniscript.h:1718
Node(const Ctx &ctx, enum Fragment nt, std::vector< Node > sub, uint32_t val=0)
Definition: miniscript.h:1743
std::optional< uint32_t > GetOps() const
Return the maximum number of ops needed to satisfy this script non-malleably.
Definition: miniscript.h:1563
bool CheckTimeLocksMix() const
Check whether there is no satisfaction path that contains both timelocks and heightlocks.
Definition: miniscript.h:1691
internal::Ops ops
Cached ops counts.
Definition: miniscript.h:596
MiniscriptContext GetMsCtx() const
Return the script context for this node.
Definition: miniscript.h:1621
size_t scriptlen
Cached script length (computed by CalcScriptLen).
Definition: miniscript.h:604
bool IsValid() const
Check whether this node is valid at all.
Definition: miniscript.h:1676
Node(const Ctx &ctx, enum Fragment nt, uint32_t val=0)
Definition: miniscript.h:1745
Availability Satisfy(const Ctx &ctx, std::vector< std::vector< unsigned char > > &stack, bool nonmalleable=true) const
Produce a witness for this script, if possible and given the information available in the context.
Definition: miniscript.h:1710
Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector< unsigned char > arg, uint32_t val=0)
Definition: miniscript.h:1723
bool IsSatisfiable(F fn) const
Determine whether a Miniscript node is satisfiable.
Definition: miniscript.h:1635
This type encapsulates the miniscript type system properties.
Definition: miniscript.h:131
constexpr bool operator<<(Type x) const
Check whether the left hand's properties are superset of the right's (= left is a subtype of right).
Definition: miniscript.h:149
uint32_t m_flags
Internal bitmap of properties (see ""_mst operator for details).
Definition: miniscript.h:133
constexpr Type(uint32_t flags)
Internal constructor used by the ""_mst operator.
Definition: miniscript.h:136
constexpr Type If(bool x) const
The empty type if x is false, itself otherwise.
Definition: miniscript.h:158
constexpr Type operator&(Type x) const
Compute the type with the intersection of properties.
Definition: miniscript.h:146
constexpr bool operator<(Type x) const
Comparison operator to enable use in sets/maps (total ordering incompatible with <<).
Definition: miniscript.h:152
constexpr Type operator|(Type x) const
Compute the type with the union of properties.
Definition: miniscript.h:143
constexpr bool operator==(Type x) const
Equality operator.
Definition: miniscript.h:155
Class whose objects represent the maximum of a list of integers.
Definition: miniscript.h:367
friend MaxInt< I > operator+(const MaxInt< I > &a, const MaxInt< I > &b)
Definition: miniscript.h:378
friend MaxInt< I > operator|(const MaxInt< I > &a, const MaxInt< I > &b)
Definition: miniscript.h:383
A data structure to help the calculation of stack size limits.
Definition: miniscript.h:443
static constexpr SatInfo Nop() noexcept
A script consisting of just a repurposed nop (OP_CHECKLOCKTIMEVERIFY, OP_CHECKSEQUENCEVERIFY).
Definition: miniscript.h:491
static constexpr SatInfo OP_CHECKSIG() noexcept
Definition: miniscript.h:503
static constexpr SatInfo BinaryOp() noexcept
A script consisting of just a binary operator (OP_BOOLAND, OP_BOOLOR, OP_ADD).
Definition: miniscript.h:495
static constexpr SatInfo OP_VERIFY() noexcept
Definition: miniscript.h:505
static constexpr SatInfo Push() noexcept
A script consisting of a single push opcode.
Definition: miniscript.h:487
static constexpr SatInfo Empty() noexcept
The empty script.
Definition: miniscript.h:485
constexpr SatInfo(int32_t in_netdiff, int32_t in_exec) noexcept
Script set with a single script in it, with specified netdiff and exec.
Definition: miniscript.h:456
constexpr friend SatInfo operator|(const SatInfo &a, const SatInfo &b) noexcept
Script set union.
Definition: miniscript.h:464
int32_t exec
How much higher the stack size can be during execution compared to at the end.
Definition: miniscript.h:449
constexpr SatInfo() noexcept
Empty script set.
Definition: miniscript.h:453
static constexpr SatInfo OP_EQUALVERIFY() noexcept
Definition: miniscript.h:500
static constexpr SatInfo OP_IFDUP(bool nonzero) noexcept
Definition: miniscript.h:499
static constexpr SatInfo OP_DUP() noexcept
Definition: miniscript.h:498
static constexpr SatInfo OP_0NOTEQUAL() noexcept
Definition: miniscript.h:504
static constexpr SatInfo If() noexcept
A script consisting of just OP_IF or OP_NOTIF.
Definition: miniscript.h:493
bool valid
Whether a canonical satisfaction/dissatisfaction is possible at all.
Definition: miniscript.h:445
int32_t netdiff
How much higher the stack size at start of execution can be compared to at the end.
Definition: miniscript.h:447
static constexpr SatInfo OP_EQUAL() noexcept
Definition: miniscript.h:501
static constexpr SatInfo OP_SIZE() noexcept
Definition: miniscript.h:502
static constexpr SatInfo Hash() noexcept
A script consisting of a single hash opcode.
Definition: miniscript.h:489
constexpr friend SatInfo operator+(const SatInfo &a, const SatInfo &b) noexcept
Script set concatenation.
Definition: miniscript.h:474
constexpr StackSize(SatInfo in_both) noexcept
Definition: miniscript.h:514
const SatInfo & Dsat() const
Definition: miniscript.h:517
constexpr StackSize(SatInfo in_sat, SatInfo in_dsat) noexcept
Definition: miniscript.h:513
const SatInfo & Sat() const
Definition: miniscript.h:516
size_type size() const
Definition: prevector.h:247
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
uint160 RIPEMD160(std::span< const unsigned char > data)
Compute the 160-bit RIPEMD-160 hash of an array.
Definition: hash.h:222
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:30
@ TAPSCRIPT
Witness v1 with 32-byte program, not BIP16 P2SH-wrapped, script path spending, leaf version 0xc0; see...
static constexpr size_t TAPROOT_CONTROL_MAX_SIZE
Definition: interpreter.h:247
#define CHECK(cond)
Unconditional failure on condition failure.
Definition: util.h:35
size_t ComputeScriptLen(Fragment fragment, Type sub0typ, size_t subsize, uint32_t k, size_t n_subs, size_t n_keys, MiniscriptContext ms_ctx)
Helper function for Node::CalcScriptLen.
Definition: miniscript.cpp:264
std::optional< Node< Key > > Parse(std::span< const char > in, const Ctx &ctx)
Parse a miniscript from its textual descriptor form.
Definition: miniscript.h:1857
std::optional< int64_t > ParseScriptNumber(const Opcode &in)
Determine whether the passed pair (created by DecomposeScript) is pushing a number.
Definition: miniscript.cpp:408
Type SanitizeType(Type e)
A helper sanitizer/checker for the output of CalcType.
Definition: miniscript.cpp:19
std::optional< std::vector< Opcode > > DecomposeScript(const CScript &script)
Decode a script into opcode/push pairs.
Definition: miniscript.cpp:368
constexpr uint32_t TX_BODY_LEEWAY_WEIGHT
Data other than the witness in a transaction. Overhead + vin count + one vin + vout count + one vout ...
Definition: miniscript.h:281
std::optional< Node< Key > > DecodeScript(I &in, I last, const Ctx &ctx)
Parse a miniscript from a bitcoin script.
Definition: miniscript.h:2299
constexpr uint32_t TXIN_BYTES_NO_WITNESS
prevout + nSequence + scriptSig
Definition: miniscript.h:277
static const auto ONE
A stack consisting of a single 0x01 element (interpreted as 1 by the script interpreted in numeric co...
Definition: miniscript.h:350
static const auto ZERO32
A stack consisting of a single malleable 32-byte 0x0000...0000 element (for dissatisfying hash challe...
Definition: miniscript.h:348
constexpr uint32_t MaxScriptSize(MiniscriptContext ms_ctx)
The maximum size of a script depending on the context.
Definition: miniscript.h:285
constexpr uint32_t TX_OVERHEAD
version + nLockTime
Definition: miniscript.h:275
static const auto ZERO
A stack consisting of a single zero-length element (interpreted as 0 by the script interpreter in num...
Definition: miniscript.h:346
std::optional< Key > ParseKey(const std::string &func, std::span< const char > &in, const Ctx &ctx)
Parse a key expression fully contained within a fragment with the name given by 'func'.
Definition: miniscript.h:1817
std::optional< std::vector< unsigned char > > ParseHexStr(const std::string &func, std::span< const char > &in, const size_t expected_size, const Ctx &ctx)
Parse a hex string fully contained within a fragment with the name given by 'func'.
Definition: miniscript.h:1826
constexpr uint32_t P2WSH_TXOUT_BYTES
nValue + script len + OP_0 + pushdata 32.
Definition: miniscript.h:279
int FindNextChar(std::span< const char > sp, const char m)
Definition: miniscript.cpp:421
@ VERIFY
VERIFY wraps the top constructed node with v:
@ CLOSE_BRACKET
CLOSE_BRACKET expects the next element to be ')' and fails if not.
@ AND_N
AND_N will construct an andor(X,Y,0) node from the last two constructed nodes.
@ SWAP
SWAP wraps the top constructed node with s:
@ COMMA
COMMA expects the next element to be ',' and fails if not.
@ DUP_IF
DUP_IF wraps the top constructed node with d:
@ EXPR
A miniscript expression which does not begin with wrappers.
@ ZERO_NOTEQUAL
ZERO_NOTEQUAL wraps the top constructed node with n:
@ NON_ZERO
NON_ZERO wraps the top constructed node with j:
@ WRAP_T
WRAP_T will construct an and_v(X,1) node from the top constructed node.
@ ALT
ALT wraps the top constructed node with a:
@ WRAP_U
WRAP_U will construct an or_i(X,0) node from the top constructed node.
@ WRAPPED_EXPR
An expression which may be begin with wrappers followed by a colon.
static const auto INVALID
A stack representing the lack of any (dis)satisfactions.
Definition: miniscript.h:354
@ SINGLE_BKV_EXPR
A single expression of type B, K, or V.
@ ENDIF_NOTIF
If, inside an ENDIF context, we find an OP_NOTIF before finding an OP_ELSE, we could either be in an ...
@ BKV_EXPR
Potentially multiple SINGLE_BKV_EXPRs as children of (potentially multiple) and_v expressions.
@ ENDIF_ELSE
If, inside an ENDIF context, we find an OP_ELSE, then we could be in either an or_i or an andor node.
@ MAYBE_AND_V
MAYBE_AND_V will check if the next part of the script could be a valid miniscript sub-expression,...
@ W_EXPR
An expression of type W (a: or s: wrappers).
@ THRESH_E
THRESH_E constructs a thresh node from the appropriate number of constructed children.
@ ENDIF
ENDIF signals that we are inside some sort of OP_IF structure, which could be or_d,...
@ THRESH_W
In a thresh expression, all sub-expressions other than the first are W-type, and end in OP_ADD.
constexpr uint32_t MAX_TAPSCRIPT_SAT_SIZE
Maximum possible stack size to spend a Taproot output (excluding the script itself).
Definition: miniscript.h:283
static const auto EMPTY
The empty stack.
Definition: miniscript.h:352
Type ComputeType(Fragment fragment, Type x, Type y, Type z, const std::vector< Type > &sub_types, uint32_t k, size_t data_size, size_t n_subs, size_t n_keys, MiniscriptContext ms_ctx)
Helper function for Node::CalcType.
Definition: miniscript.cpp:39
void BuildBack(const MiniscriptContext script_ctx, Fragment nt, std::vector< Node< Key > > &constructed, const bool reverse=false)
BuildBack pops the last two elements off constructed and wraps them in the specified Fragment.
Definition: miniscript.h:1840
static constexpr uint32_t MAX_TAPMINISCRIPT_STACK_ELEM_SIZE
The maximum size of a witness item for a Miniscript under Tapscript context. (A BIP340 signature with...
Definition: miniscript.h:272
constexpr bool IsTapscript(MiniscriptContext ms_ctx)
Whether the context Tapscript, ensuring the only other possibility is P2WSH.
Definition: miniscript.h:260
std::optional< Node< typename Ctx::Key > > FromScript(const CScript &script, const Ctx &ctx)
Definition: miniscript.h:2694
void ForEachNode(const Node< Key > &root, Fn &&fn)
Unordered traversal of a miniscript node tree.
Definition: miniscript.h:200
std::optional< Node< typename Ctx::Key > > FromString(const std::string &str, const Ctx &ctx)
Definition: miniscript.h:2688
std::pair< opcodetype, std::vector< unsigned char > > Opcode
Definition: miniscript.h:194
Fragment
The different node types in miniscript.
Definition: miniscript.h:214
@ OR_I
OP_IF [X] OP_ELSE [Y] OP_ENDIF.
@ MULTI_A
[key_0] OP_CHECKSIG ([key_n] OP_CHECKSIGADD)* [k] OP_NUMEQUAL (only within Tapscript ctx)
@ RIPEMD160
OP_SIZE 32 OP_EQUALVERIFY OP_RIPEMD160 [hash] OP_EQUAL.
@ HASH160
OP_SIZE 32 OP_EQUALVERIFY OP_HASH160 [hash] OP_EQUAL.
@ OR_B
[X] [Y] OP_BOOLOR
@ WRAP_A
OP_TOALTSTACK [X] OP_FROMALTSTACK.
@ WRAP_V
[X] OP_VERIFY (or -VERIFY version of last opcode in X)
@ ANDOR
[X] OP_NOTIF [Z] OP_ELSE [Y] OP_ENDIF
@ THRESH
[X1] ([Xn] OP_ADD)* [k] OP_EQUAL
@ WRAP_N
[X] OP_0NOTEQUAL
@ WRAP_S
OP_SWAP [X].
@ OR_C
[X] OP_NOTIF [Y] OP_ENDIF
@ HASH256
OP_SIZE 32 OP_EQUALVERIFY OP_HASH256 [hash] OP_EQUAL.
@ OLDER
[n] OP_CHECKSEQUENCEVERIFY
@ SHA256
OP_SIZE 32 OP_EQUALVERIFY OP_SHA256 [hash] OP_EQUAL.
@ WRAP_J
OP_SIZE OP_0NOTEQUAL OP_IF [X] OP_ENDIF.
@ AFTER
[n] OP_CHECKLOCKTIMEVERIFY
@ OR_D
[X] OP_IFDUP OP_NOTIF [Y] OP_ENDIF
@ WRAP_D
OP_DUP OP_IF [X] OP_ENDIF.
@ AND_B
[X] [Y] OP_BOOLAND
@ PK_H
OP_DUP OP_HASH160 [keyhash] OP_EQUALVERIFY.
@ WRAP_C
[X] OP_CHECKSIG
@ MULTI
[k] [key_n]* [n] OP_CHECKMULTISIG (only available within P2WSH context)
Definition: messages.h:21
std::span< const char > Expr(std::span< const char > &sp)
Extract the expression that sp begins with.
Definition: parsing.cpp:31
bool Func(const std::string &str, std::span< const char > &sp)
Parse a function call.
Definition: parsing.cpp:22
bool Const(const std::string &str, std::span< const char > &sp, bool skip)
Parse a constant.
Definition: parsing.cpp:13
Definition: common.h:30
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:247
static constexpr unsigned int MAX_STANDARD_P2WSH_STACK_ITEMS
The maximum number of witness stack items in a standard P2WSH script.
Definition: policy.h:54
static constexpr int32_t MAX_STANDARD_TX_WEIGHT
The maximum weight for transactions we're willing to relay/mine.
Definition: policy.h:38
static constexpr unsigned int MAX_STANDARD_P2WSH_SCRIPT_SIZE
The maximum size in bytes of a standard witnessScript.
Definition: policy.h:60
@ OP_SHA256
Definition: script.h:187
@ OP_BOOLAND
Definition: script.h:170
@ OP_CHECKMULTISIG
Definition: script.h:193
@ OP_IF
Definition: script.h:105
@ OP_SWAP
Definition: script.h:132
@ OP_CHECKSIG
Definition: script.h:191
@ OP_CHECKLOCKTIMEVERIFY
Definition: script.h:198
@ OP_EQUAL
Definition: script.h:147
@ OP_NUMEQUAL
Definition: script.h:172
@ OP_NOTIF
Definition: script.h:106
@ OP_SIZE
Definition: script.h:140
@ OP_ENDIF
Definition: script.h:110
@ OP_DUP
Definition: script.h:126
@ OP_TOALTSTACK
Definition: script.h:115
@ OP_RIPEMD160
Definition: script.h:185
@ OP_HASH256
Definition: script.h:189
@ OP_FROMALTSTACK
Definition: script.h:116
@ OP_NUMEQUALVERIFY
Definition: script.h:173
@ OP_HASH160
Definition: script.h:188
@ OP_1
Definition: script.h:84
@ OP_VERIFY
Definition: script.h:111
@ OP_ADD
Definition: script.h:162
@ OP_CHECKMULTISIGVERIFY
Definition: script.h:194
@ OP_BOOLOR
Definition: script.h:171
@ OP_CHECKSIGADD
Definition: script.h:211
@ OP_ELSE
Definition: script.h:109
@ OP_CHECKSIGVERIFY
Definition: script.h:192
@ OP_0NOTEQUAL
Definition: script.h:160
@ OP_0
Definition: script.h:77
@ OP_IFDUP
Definition: script.h:123
@ OP_EQUALVERIFY
Definition: script.h:148
@ OP_CHECKSEQUENCEVERIFY
Definition: script.h:200
static constexpr unsigned int MAX_PUBKEYS_PER_MULTI_A
The limit of keys in OP_CHECKSIGADD-based scripts.
Definition: script.h:38
static const int MAX_STACK_SIZE
Definition: script.h:44
static const int MAX_OPS_PER_SCRIPT
Definition: script.h:32
CScript BuildScript(Ts &&... inputs)
Build a script by concatenating other scripts, or any argument accepted by CScript::operator<<.
Definition: script.h:611
static const int MAX_PUBKEYS_PER_MULTISIG
Definition: script.h:35
static bool verify(const CScriptNum10 &bignum, const CScriptNum &scriptnum)
constexpr unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 byt...
Definition: serialize.h:290
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
Definition: strencodings.h:68
A pair of a satisfaction and a dissatisfaction InputStack.
Definition: miniscript.h:357
InputResult(A &&in_nsat, B &&in_sat)
Definition: miniscript.h:361
An object representing a sequence of witness stack elements.
Definition: miniscript.h:309
bool malleable
Whether this stack is malleable (can be turned into an equally valid other stack by a third party).
Definition: miniscript.h:319
friend InputStack operator|(InputStack a, InputStack b)
Choose between two potential input stacks.
Definition: miniscript.cpp:339
friend InputStack operator+(InputStack a, InputStack b)
Concatenate two input stacks.
Definition: miniscript.cpp:325
std::vector< std::vector< unsigned char > > stack
Data elements.
Definition: miniscript.h:326
InputStack()=default
Construct an empty stack (valid).
bool has_sig
Whether this stack contains a digital signature.
Definition: miniscript.h:317
InputStack & SetAvailable(Availability avail)
Change availability.
Definition: miniscript.cpp:298
Availability available
Whether this stack is valid for its intended purpose (satisfaction or dissatisfaction of a Node).
Definition: miniscript.h:315
InputStack & SetMalleable(bool x=true)
Mark this input stack as malleable.
Definition: miniscript.cpp:320
size_t size
Serialized witness size.
Definition: miniscript.h:324
bool non_canon
Whether this stack is non-canonical (using a construction known to be unnecessary for satisfaction).
Definition: miniscript.h:322
InputStack(std::vector< unsigned char > in)
Construct a valid single-element stack (with an element up to 75 bytes).
Definition: miniscript.h:330
InputStack & SetWithSig()
Mark this input stack as having a signature.
Definition: miniscript.cpp:310
InputStack & SetNonCanon()
Mark this input stack as non-canonical (known to not be necessary in non-malleable satisfactions).
Definition: miniscript.cpp:315
Ops(uint32_t in_count, MaxInt< uint32_t > in_sat, MaxInt< uint32_t > in_dsat)
Definition: miniscript.h:398
MaxInt< uint32_t > sat
Number of keys in possibly executed OP_CHECKMULTISIG(VERIFY)s to satisfy.
Definition: miniscript.h:394
MaxInt< uint32_t > dsat
Number of keys in possibly executed OP_CHECKMULTISIG(VERIFY)s to dissatisfy.
Definition: miniscript.h:396
uint32_t count
Non-push opcodes.
Definition: miniscript.h:392
MaxInt< uint32_t > sat
Maximum witness size to satisfy;.
Definition: miniscript.h:522
MaxInt< uint32_t > dsat
Maximum witness size to dissatisfy;.
Definition: miniscript.h:524
WitnessSize(MaxInt< uint32_t > in_sat, MaxInt< uint32_t > in_dsat)
Definition: miniscript.h:526
std::vector< uint16_t > keys
Definition: dbwrapper.cpp:398
static const std::vector< uint8_t > EMPTY
Definition: script.h:22
static int count
bool IsHex(std::string_view str)
#define B
Definition: util_tests.cpp:563
assert(!tx.IsCoinBase())
std::vector< std::common_type_t< Args... > > Vector(Args &&... args)
Construct a vector with the specified elements.
Definition: vector.h:23