From e22aa8b22d640b425287493e8c88ed494f044ca8 Mon Sep 17 00:00:00 2001 From: furszy Date: Mon, 2 Dec 2024 11:12:50 -0500 Subject: [PATCH 1/3] refactor: replace hardcoded number, introduce 'MAX_BARE_MULTISIG_PUBKEYS_NUM' --- src/policy/policy.cpp | 2 +- src/policy/policy.h | 2 ++ src/script/descriptor.cpp | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/policy/policy.cpp b/src/policy/policy.cpp index ed336928235..ebd999ef1b8 100644 --- a/src/policy/policy.cpp +++ b/src/policy/policy.cpp @@ -87,7 +87,7 @@ bool IsStandard(const CScript& scriptPubKey, const std::optional& max_ unsigned char m = vSolutions.front()[0]; unsigned char n = vSolutions.back()[0]; // Support up to x-of-3 multisig txns as standard - if (n < 1 || n > 3) + if (n < 1 || n > MAX_BARE_MULTISIG_PUBKEYS_NUM) return false; if (m < 1 || m > n) return false; diff --git a/src/policy/policy.h b/src/policy/policy.h index 4412f2db87a..d73e39b9d74 100644 --- a/src/policy/policy.h +++ b/src/policy/policy.h @@ -37,6 +37,8 @@ static constexpr unsigned int DEFAULT_INCREMENTAL_RELAY_FEE{1000}; static constexpr unsigned int DEFAULT_BYTES_PER_SIGOP{20}; /** Default for -permitbaremultisig */ static constexpr bool DEFAULT_PERMIT_BAREMULTISIG{true}; +/** The maximum number of pubkeys in a bare multisig output script */ +static constexpr unsigned int MAX_BARE_MULTISIG_PUBKEYS_NUM{3}; /** The maximum number of witness stack items in a standard P2WSH script */ static constexpr unsigned int MAX_STANDARD_P2WSH_STACK_ITEMS{100}; /** The maximum size in bytes of each witness stack item in a standard P2WSH script */ diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index 5026470edcf..9a1c442cf07 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -1852,8 +1852,8 @@ std::vector> ParseScript(uint32_t& key_exp_index return {}; } if (ctx == ParseScriptContext::TOP) { - if (providers.size() > 3) { - error = strprintf("Cannot have %u pubkeys in bare multisig; only at most 3 pubkeys", providers.size()); + if (providers.size() > MAX_BARE_MULTISIG_PUBKEYS_NUM) { + error = strprintf("Cannot have %u pubkeys in bare multisig; only at most %d pubkeys", providers.size(), MAX_BARE_MULTISIG_PUBKEYS_NUM); return {}; } } From cdaa3a58dc16d6a27248dc4cdec2dd1909eee7fe Mon Sep 17 00:00:00 2001 From: furszy Date: Tue, 26 Nov 2024 18:03:17 -0500 Subject: [PATCH 2/3] wallet: bugfix, stop treating multisig consensus-invalid/unspendable scripts as ours Ensure legacy wallet migration skips the never standard bare multisig with +3 keys and consensus-invalid multisig scripts. Treating them as valid causes migration to crash because we are enforcing this rules within the descriptors parsing logic. --- src/wallet/scriptpubkeyman.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index 62384056dc6..9a6d2b809bc 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include