Skip to content

Commit 9164c2e

Browse files
committed
miniscript: restrict multi() usage to P2WSH context
CHECKMULTISIG is disabled for Tapscript. Instead, we'll introduce a multi_a() fragment with the same semantic as multi().
1 parent 91b4db8 commit 9164c2e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/script/miniscript.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <assert.h>
1111

1212
namespace miniscript {
13+
1314
namespace internal {
1415

1516
Type SanitizeType(Type e) {

src/script/miniscript.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <primitives/transaction.h>
2121
#include <script/script.h>
2222
#include <span.h>
23+
#include <util/check.h>
2324
#include <util/spanparsing.h>
2425
#include <util/strencodings.h>
2526
#include <util/string.h>
@@ -234,6 +235,16 @@ enum class MiniscriptContext {
234235
TAPSCRIPT,
235236
};
236237

238+
/** Whether the context Tapscript, ensuring the only other possibility is P2WSH. */
239+
constexpr bool IsTapscript(MiniscriptContext ms_ctx)
240+
{
241+
switch (ms_ctx) {
242+
case MiniscriptContext::P2WSH: return false;
243+
case MiniscriptContext::TAPSCRIPT: return true;
244+
}
245+
assert(false);
246+
}
247+
237248
namespace internal {
238249

239250
//! Helper function for Node::CalcType.
@@ -585,7 +596,8 @@ struct Node {
585596
};
586597
// The upward function computes for a node, given its followed-by-OP_VERIFY status
587598
// and the CScripts of its child nodes, the CScript of the node.
588-
auto upfn = [&ctx](bool verify, const Node& node, Span<CScript> subs) -> CScript {
599+
const bool is_tapscript{IsTapscript(m_script_ctx)};
600+
auto upfn = [&ctx, is_tapscript](bool verify, const Node& node, Span<CScript> subs) -> CScript {
589601
switch (node.fragment) {
590602
case Fragment::PK_K: return BuildScript(ctx.ToPKBytes(node.keys[0]));
591603
case Fragment::PK_H: return BuildScript(OP_DUP, OP_HASH160, ctx.ToPKHBytes(node.keys[0]), OP_EQUALVERIFY);
@@ -618,6 +630,7 @@ struct Node {
618630
case Fragment::OR_I: return BuildScript(OP_IF, subs[0], OP_ELSE, subs[1], OP_ENDIF);
619631
case Fragment::ANDOR: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[2], OP_ELSE, subs[1], OP_ENDIF);
620632
case Fragment::MULTI: {
633+
CHECK_NONFATAL(!is_tapscript);
621634
CScript script = BuildScript(node.k);
622635
for (const auto& key : node.keys) {
623636
script = BuildScript(std::move(script), ctx.ToPKBytes(key));
@@ -653,7 +666,8 @@ struct Node {
653666
};
654667
// The upward function computes for a node, given whether its parent is a wrapper,
655668
// and the string representations of its child nodes, the string representation of the node.
656-
auto upfn = [&ctx](bool wrapped, const Node& node, Span<std::string> subs) -> std::optional<std::string> {
669+
const bool is_tapscript{IsTapscript(m_script_ctx)};
670+
auto upfn = [&ctx, is_tapscript](bool wrapped, const Node& node, Span<std::string> subs) -> std::optional<std::string> {
657671
std::string ret = wrapped ? ":" : "";
658672

659673
switch (node.fragment) {
@@ -717,6 +731,7 @@ struct Node {
717731
if (node.subs[2]->fragment == Fragment::JUST_0) return std::move(ret) + "and_n(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
718732
return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
719733
case Fragment::MULTI: {
734+
CHECK_NONFATAL(!is_tapscript);
720735
auto str = std::move(ret) + "multi(" + ::ToString(node.k);
721736
for (const auto& key : node.keys) {
722737
auto key_str = ctx.ToString(key);
@@ -1631,6 +1646,7 @@ inline NodeRef<Key> Parse(Span<const char> in, const Ctx& ctx)
16311646
in = in.subspan(arg_size + 1);
16321647
script_size += 1 + (num > 16) + (num > 0x7f) + (num > 0x7fff) + (num > 0x7fffff);
16331648
} else if (Const("multi(", in)) {
1649+
if (IsTapscript(ctx.MsContext())) return {};
16341650
// Get threshold
16351651
int next_comma = FindNextChar(in, ',');
16361652
if (next_comma < 1) return {};
@@ -1989,6 +2005,7 @@ inline NodeRef<Key> DecodeScript(I& in, I last, const Ctx& ctx)
19892005
}
19902006
// Multi
19912007
if (last - in >= 3 && in[0].first == OP_CHECKMULTISIG) {
2008+
if (IsTapscript(ctx.MsContext())) return {};
19922009
std::vector<Key> keys;
19932010
const auto n = ParseScriptNumber(in[1]);
19942011
if (!n || last - in < 3 + *n) return {};

0 commit comments

Comments
 (0)