From 662e3fcb669eae01b7de2fe5a217ea3031ece596 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Wed, 23 Sep 2026 17:39:50 +0200 Subject: [PATCH 01/15] util: add strict UTF-8 validation Add a shared RFC 3629 validator with tests for ASCII, multibyte boundaries, incomplete sequences, overlong encodings, surrogate code points, and values above U+10FFFF. --- src/test/CMakeLists.txt | 1 + src/test/utf8_tests.cpp | 59 +++++++++++++++++++++++++++++++++++++++++ src/util/utf8.h | 32 ++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/test/utf8_tests.cpp create mode 100644 src/util/utf8.h diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 00a2161aa4a1..a4d95389dcea 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -132,6 +132,7 @@ add_executable(test_bitcoin util_not_null_tests.cpp util_expected_tests.cpp util_string_tests.cpp + utf8_tests.cpp util_tests.cpp util_threadnames_tests.cpp util_trace_tests.cpp diff --git a/src/test/utf8_tests.cpp b/src/test/utf8_tests.cpp new file mode 100644 index 000000000000..ed6cb5b3d85f --- /dev/null +++ b/src/test/utf8_tests.cpp @@ -0,0 +1,59 @@ +// Copyright (c) The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +BOOST_AUTO_TEST_SUITE(utf8_tests) + +BOOST_AUTO_TEST_CASE(valid_utf8) +{ + BOOST_CHECK(IsValidUTF8({})); + std::array ascii; + for (size_t i{0}; i < ascii.size(); ++i) ascii[i] = i; + BOOST_CHECK(IsValidUTF8(ascii)); + + std::vector combined; + for (const std::string hex : {"c280", "dfbf", "e0a080", "ed9fbf", "ee8080", "efbfbf", "efbbbf", "f0908080", "f48fbfbf"}) { + BOOST_TEST_CONTEXT(hex) { + const auto text{ParseHex(hex)}; + BOOST_CHECK(IsValidUTF8(text)); + // Every proper, nonempty prefix of a multibyte character is incomplete. + for (size_t size{1}; size < text.size(); ++size) { + BOOST_CHECK(!IsValidUTF8(std::span{text}.first(size))); + } + combined.insert(combined.end(), text.begin(), text.end()); + combined.push_back(0); + } + } + BOOST_CHECK(IsValidUTF8(combined)); +} + +BOOST_AUTO_TEST_CASE(invalid_utf8) +{ + // Stray continuation bytes, overlong encodings, bad continuations, + // surrogate code points, and encodings beyond U+10FFFF. + for (const std::string hex : {"80", "bf", "c080", "c1bf", "c241", "e08080", "e228a1", + "eda080", "edbfbf", "f0808080", "f0908041", "f4908080", + "f5808080", "f888808080", "fc8480808080", "ff"}) { + BOOST_TEST_CONTEXT(hex) { + auto text{ParseHex(hex)}; + BOOST_CHECK(!IsValidUTF8(text)); + text.insert(text.begin(), 'a'); + text.push_back('z'); + BOOST_CHECK(!IsValidUTF8(text)); + } + } +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/src/util/utf8.h b/src/util/utf8.h new file mode 100644 index 000000000000..4821728c65b4 --- /dev/null +++ b/src/util/utf8.h @@ -0,0 +1,32 @@ +// Copyright (c) The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_UTIL_UTF8_H +#define BITCOIN_UTIL_UTF8_H + +#include +#include +#include + +/** Validate UTF-8 as specified by RFC 3629, including empty strings and embedded NULs. */ +inline bool IsValidUTF8(std::span text) +{ + for (size_t pos{0}; pos < text.size();) { + const uint8_t first{text[pos++]}; + if (first < 0x80) continue; + const size_t following{first >= 0xc2 && first <= 0xdf ? 1U : + first >= 0xe0 && first <= 0xef ? 2U : + first >= 0xf0 && first <= 0xf4 ? 3U : 0U}; + if (following == 0 || following > text.size() - pos) return false; + // Exclude overlong encodings, surrogate code points, and values above U+10FFFF. + if ((first == 0xe0 && text[pos] < 0xa0) || (first == 0xed && text[pos] >= 0xa0) || + (first == 0xf0 && text[pos] < 0x90) || (first == 0xf4 && text[pos] >= 0x90)) return false; + for (size_t i{0}; i < following; ++i) { + if ((text[pos++] & 0xc0) != 0x80) return false; + } + } + return true; +} + +#endif // BITCOIN_UTIL_UTF8_H From b6240b65578f37e83b6a6b1dbd30914a4aff3366 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Wed, 23 Sep 2026 17:41:59 +0200 Subject: [PATCH 02/15] univalue: use strict UTF-8 validation for JSON strings Preserve raw string bytes and validate them with the shared UTF-8 utility instead of decoding and normalizing malformed input. Keep escaped UTF-16 surrogate handling local, reject interrupted pairs, and correct supplementary-plane decoding. Add regressions for malformed values and object keys, mixed raw bytes and escapes, and surrogate-pair boundaries. --- src/univalue/include/univalue_utffilter.h | 47 +++++++---------------- src/univalue/test/unitester.cpp | 37 +++++++++++++++++- 2 files changed, 49 insertions(+), 35 deletions(-) diff --git a/src/univalue/include/univalue_utffilter.h b/src/univalue/include/univalue_utffilter.h index 41d8e6bb05f7..b2de934a6de1 100644 --- a/src/univalue/include/univalue_utffilter.h +++ b/src/univalue/include/univalue_utffilter.h @@ -4,6 +4,9 @@ #ifndef BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_UTFFILTER_H #define BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_UTFFILTER_H +#include +#include + #include /** @@ -20,36 +23,14 @@ class JSONUTF8StringFilter // Write single 8-bit char (may be part of UTF-8 sequence) void push_back(unsigned char ch) { - if (state == 0) { - if (ch < 0x80) // 7-bit ASCII, fast direct pass-through - str.push_back(ch); - else if (ch < 0xc0) // Mid-sequence character, invalid in this state - is_valid = false; - else if (ch < 0xe0) { // Start of 2-byte sequence - codepoint = (ch & 0x1f) << 6; - state = 6; - } else if (ch < 0xf0) { // Start of 3-byte sequence - codepoint = (ch & 0x0f) << 12; - state = 12; - } else if (ch < 0xf8) { // Start of 4-byte sequence - codepoint = (ch & 0x07) << 18; - state = 18; - } else // Reserved, invalid - is_valid = false; - } else { - if ((ch & 0xc0) != 0x80) // Not a continuation, invalid - is_valid = false; - state -= 6; - codepoint |= (ch & 0x3f) << state; - if (state == 0) - push_back_u(codepoint); - } + if (surpair) // A surrogate pair must consist of adjacent Unicode escapes. + is_valid = false; + // Preserve raw bytes so validation cannot normalize an invalid encoding. + str.push_back(char(ch)); } // Write codepoint directly, possibly collating surrogate pairs void push_back_u(unsigned int codepoint_) { - if (state) // Only accept full codepoints in open state - is_valid = false; if (codepoint_ >= 0xD800 && codepoint_ < 0xDC00) { // First half of surrogate pair if (surpair) // Two subsequent surrogate pair openers - fail is_valid = false; @@ -58,7 +39,7 @@ class JSONUTF8StringFilter } else if (codepoint_ >= 0xDC00 && codepoint_ < 0xE000) { // Second half of surrogate pair if (surpair) { // Open surrogate pair, expect second half // Compute code point from UTF-16 surrogate pair - append_codepoint(0x10000 | ((surpair - 0xD800)<<10) | (codepoint_ - 0xDC00)); + append_codepoint(0x10000 + ((surpair - 0xD800)<<10) + (codepoint_ - 0xDC00)); surpair = 0; } else // Second half doesn't follow a first half - fail is_valid = false; @@ -73,17 +54,13 @@ class JSONUTF8StringFilter // No open sequences, no open surrogate pairs, etc bool finalize() { - if (state || surpair) + if (surpair) is_valid = false; - return is_valid; + return is_valid && IsValidUTF8(MakeUCharSpan(str)); } private: std::string &str; bool is_valid{true}; - // Current UTF-8 decoding state - unsigned int codepoint{0}; - int state{0}; // Top bit to be filled in for next UTF-8 byte, or 0 - // Keep track of the following state to handle the following section of // RFC4627: // @@ -107,11 +84,13 @@ class JSONUTF8StringFilter str.push_back((char)(0xE0 | (codepoint_ >> 12))); str.push_back((char)(0x80 | ((codepoint_ >> 6) & 0x3F))); str.push_back((char)(0x80 | (codepoint_ & 0x3F))); - } else if (codepoint_ <= 0x1FFFFF) { + } else if (codepoint_ <= 0x10FFFF) { str.push_back((char)(0xF0 | (codepoint_ >> 18))); str.push_back((char)(0x80 | ((codepoint_ >> 12) & 0x3F))); str.push_back((char)(0x80 | ((codepoint_ >> 6) & 0x3F))); str.push_back((char)(0x80 | (codepoint_ & 0x3F))); + } else { + is_valid = false; } } }; diff --git a/src/univalue/test/unitester.cpp b/src/univalue/test/unitester.cpp index e82ea74dec48..60cd19e2db88 100644 --- a/src/univalue/test/unitester.cpp +++ b/src/univalue/test/unitester.cpp @@ -4,7 +4,6 @@ // file COPYING or https://opensource.org/licenses/mit-license.php. #include - #include #include #include @@ -66,6 +65,7 @@ #include #include #include +#include static std::string rtrim(std::string s) { @@ -177,6 +177,40 @@ void unescape_unicode_test() testResult = val.read("[\"\\ud834\\udd61\"]"); assert(testResult); assert(val[0].get_str() == "\xf0\x9d\x85\xa1"); + // The offset for supplementary characters must carry into the next plane. + assert(val.read("[\"\\ud840\\udc00\"]")); + assert(val[0].get_str() == "\xf0\xa0\x80\x80"); // U+20000 + assert(val.read("[\"\\udbff\\udfff\"]")); + assert(val[0].get_str() == "\xf4\x8f\xbf\xbf"); // U+10FFFF +} + +void utf8_test() +{ + UniValue val; + for (const std::string text : {"ascii", "\xc2\x80", "\xed\x9f\xbf", "\xee\x80\x80", + "\xf0\x90\x80\x80", "\xf4\x8f\xbf\xbf"}) { + assert(val.read("\"" + text + "\"")); + assert(val.get_str() == text); + assert(val.read("{\"" + text + "\":0}")); + assert(val.getKeys()[0] == text); + } + + // Invalid UTF-8 must be rejected, not normalized into valid characters. + for (const std::string text : {"\xc0\x80", "\xe0\x80\x80", "\xf0\x80\x80\x80", + "\xed\xa0\x80", "\xed\xa0\x80\xed\xb0\x80", + "\xf4\x90\x80\x80", "\xf8\x88\x80\x80\x80", + "\x80", "\xc2", "\xe2\x28\xa1"}) { + assert(!val.read("\"" + text + "\"")); + assert(!val.read("{\"" + text + "\":0}")); + } + + // Escapes cannot complete a raw UTF-8 sequence or an interrupted surrogate pair. + for (const std::string text : {"\xc2\\u0080", "\xc2\\n", "\\ud800x\\udc00", + "\\ud800\\n\\udc00", "\\ud800\\u0041\\udc00", + "\\ud800\xc2\xa2\\udc00", "\xed\xa0\x80\\udc00", + "\\ud800\xed\xb0\x80"}) { + assert(!val.read("\"" + text + "\"")); + } } void no_nul_test() @@ -193,6 +227,7 @@ int main(int argc, char* argv[]) } unescape_unicode_test(); + utf8_test(); no_nul_test(); return 0; From 4c1031871426db246166425efb87be0d11cc359f Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Wed, 14 Jan 2026 15:09:32 +0100 Subject: [PATCH 03/15] wallet: add WalletDescriptorInfo helper for descriptor serialization Introduces WalletDescriptorInfo struct and DescriptorInfoToUniValue() helper to avoid code duplication when serializing descriptor metadata to UniValue. Refactors listdescriptors RPC to use the new helper. --- src/wallet/CMakeLists.txt | 1 + src/wallet/descriptor_info.cpp | 32 ++++++++++++++++++++++++++++++++ src/wallet/descriptor_info.h | 30 ++++++++++++++++++++++++++++++ src/wallet/rpc/backup.cpp | 21 ++++----------------- 4 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 src/wallet/descriptor_info.cpp create mode 100644 src/wallet/descriptor_info.h diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index 1ecacf3af5bf..3520fbc391f4 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -9,6 +9,7 @@ add_library(bitcoin_wallet STATIC EXCLUDE_FROM_ALL context.cpp crypter.cpp db.cpp + descriptor_info.cpp dump.cpp export.cpp external_signer_scriptpubkeyman.cpp diff --git a/src/wallet/descriptor_info.cpp b/src/wallet/descriptor_info.cpp new file mode 100644 index 000000000000..0e2940e0046e --- /dev/null +++ b/src/wallet/descriptor_info.cpp @@ -0,0 +1,32 @@ +// Copyright (c) The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include + +namespace wallet { + +UniValue DescriptorInfoToUniValue(const WalletDescriptorInfo& info) +{ + UniValue obj(UniValue::VOBJ); + obj.pushKV("desc", info.descriptor); + obj.pushKV("timestamp", info.creation_time); + obj.pushKV("active", info.active); + if (info.internal.has_value()) { + obj.pushKV("internal", info.internal.value()); + } + if (info.range.has_value()) { + UniValue range(UniValue::VARR); + range.push_back(info.range->first); + // range_end is exclusive internally, display as inclusive (hence -1) + range.push_back(info.range->second - 1); + obj.pushKV("range", std::move(range)); + obj.pushKV("next", info.next_index); + obj.pushKV("next_index", info.next_index); + } + return obj; +} + +} // namespace wallet diff --git a/src/wallet/descriptor_info.h b/src/wallet/descriptor_info.h new file mode 100644 index 000000000000..6bf37e625533 --- /dev/null +++ b/src/wallet/descriptor_info.h @@ -0,0 +1,30 @@ +// Copyright (c) The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_WALLET_DESCRIPTOR_INFO_H +#define BITCOIN_WALLET_DESCRIPTOR_INFO_H + +#include + +class UniValue; + +namespace wallet { + +/** + * Information about a wallet descriptor, used for serialization to JSON. + * This struct captures all the metadata needed for listdescriptors output + * and importdescriptors input. + */ +using WalletDescriptorInfo = WalletDescInfo; + +/** + * Convert a WalletDescriptorInfo to a UniValue object. + * The output format is compatible with both listdescriptors output and + * importdescriptors input. + */ +UniValue DescriptorInfoToUniValue(const WalletDescriptorInfo& info); + +} // namespace wallet + +#endif // BITCOIN_WALLET_DESCRIPTOR_INFO_H diff --git a/src/wallet/rpc/backup.cpp b/src/wallet/rpc/backup.cpp index dfd4727a0ca3..c126c09492c8 100644 --- a/src/wallet/rpc/backup.cpp +++ b/src/wallet/rpc/backup.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -326,23 +327,9 @@ RPCMethod listdescriptors() }); UniValue descriptors(UniValue::VARR); - for (const WalletDescInfo& info : wallet_descriptors) { - UniValue spk(UniValue::VOBJ); - spk.pushKV("desc", info.descriptor); - spk.pushKV("timestamp", info.creation_time); - spk.pushKV("active", info.active); - if (info.internal.has_value()) { - spk.pushKV("internal", info.internal.value()); - } - if (info.range.has_value()) { - UniValue range(UniValue::VARR); - range.push_back(info.range->first); - range.push_back(info.range->second - 1); - spk.pushKV("range", std::move(range)); - spk.pushKV("next", info.next_index); - spk.pushKV("next_index", info.next_index); - } - descriptors.push_back(std::move(spk)); + for (const WalletDescriptorInfo& info : wallet_descriptors) { + UniValue descriptor{DescriptorInfoToUniValue(info)}; + descriptors.push_back(std::move(descriptor)); } UniValue response(UniValue::VOBJ); From edc6b96dca54a25c9e99f6adf3534b663b8b4344 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 22 May 2026 17:45:55 +0200 Subject: [PATCH 04/15] script: exclude observable pubkeys from descriptors Expose descriptor public-key collection that can omit keys observable from spent outputs. This lets encrypted-backup code derive recipient keys from non-observable descriptor xpubs without adding a separate BIP138-specific descriptor API. --- src/script/descriptor.cpp | 50 ++++++++++++++++++++-- src/script/descriptor.h | 10 ++++- src/test/descriptor_tests.cpp | 64 ++++++++++++++++++++++++++++ src/wallet/test/walletload_tests.cpp | 3 +- 4 files changed, 122 insertions(+), 5 deletions(-) diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index 44d8763351ef..c8e509db95bf 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -265,6 +265,21 @@ struct PubkeyProvider virtual std::optional GetRootPubKey() const = 0; /** Return the extended public key for this PubkeyProvider, if it has one. */ virtual std::optional GetRootExtPubKey() const = 0; + /** Whether this PubkeyProvider is a bare xpub with no derivation. */ + virtual bool IsBareExtPubKey() const { return false; } + /** Add the extended public key represented by this provider, if any. + * + * @param[out] ext_pubs Any extended public keys + * @param[in] exclude_observable Whether to exclude xpubs that may be observable from spends + */ + virtual void GetExtPubKeys(std::set& ext_pubs, bool exclude_observable) const + { + // A bare xpub has no derivation boundary separating the descriptor key from the + // pubkey revealed by a spend, unlike xpub/path or xpub/* entries. + if (exclude_observable && IsBareExtPubKey()) return; + std::optional ext_pub = GetRootExtPubKey(); + if (ext_pub) ext_pubs.insert(*ext_pub); + } /** Make a deep copy of this PubkeyProvider */ virtual std::unique_ptr Clone() const = 0; @@ -370,6 +385,10 @@ class OriginPubkeyProvider final : public PubkeyProvider { return m_provider->GetRootExtPubKey(); } + bool IsBareExtPubKey() const override + { + return m_provider->IsBareExtPubKey(); + } std::unique_ptr Clone() const override { return std::make_unique(m_expr_index, m_origin, m_provider->Clone(), m_apostrophe); @@ -655,6 +674,7 @@ class BIP32PubkeyProvider final : public PubkeyProvider { return m_root_extkey; } + bool IsBareExtPubKey() const override { return m_path.empty() && !IsRange(); } std::unique_ptr Clone() const override { return std::make_unique(m_expr_index, m_root_extkey, m_path, m_derive, m_apostrophe); @@ -849,6 +869,19 @@ class MuSigPubkeyProvider final : public PubkeyProvider { return std::nullopt; } + void GetExtPubKeys(std::set& ext_pubs, bool exclude_observable) const override + { + if (!exclude_observable) { + PubkeyProvider::GetExtPubKeys(ext_pubs, exclude_observable); + return; + } + // MuSig's participant xpubs are not descriptor root keys, so GetPubKeys() intentionally + // omits them. Recurse into participants for backup key discovery, where the aggregate key + // revealed by a spend does not reveal the participant xpubs. + for (const auto& prov : m_participants) { + prov->GetExtPubKeys(ext_pubs, /*exclude_observable=*/false); + } + } std::unique_ptr Clone() const override { @@ -1114,16 +1147,27 @@ class DescriptorImpl : public Descriptor std::optional MaxSatisfactionElems() const override { return {}; } // NOLINTNEXTLINE(misc-no-recursion) - void GetPubKeys(std::set& pubkeys, std::set& ext_pubs) const override + void GetPubKeys(std::set& pubkeys, std::set& ext_pubs, bool only_observable) const override { for (const auto& p : m_pubkey_args) { std::optional pub = p->GetRootPubKey(); if (pub) pubkeys.insert(*pub); std::optional ext_pub = p->GetRootExtPubKey(); - if (ext_pub) ext_pubs.insert(*ext_pub); + if (ext_pub && (!only_observable || p->IsBareExtPubKey())) ext_pubs.insert(*ext_pub); + } + for (const auto& arg : m_subdescriptor_args) { + arg->GetPubKeys(pubkeys, ext_pubs, only_observable); + } + } + + // NOLINTNEXTLINE(misc-no-recursion) + void GetExtPubKeys(std::set& ext_pubs, bool exclude_observable) const override + { + for (const auto& p : m_pubkey_args) { + p->GetExtPubKeys(ext_pubs, exclude_observable); } for (const auto& arg : m_subdescriptor_args) { - arg->GetPubKeys(pubkeys, ext_pubs); + arg->GetExtPubKeys(ext_pubs, exclude_observable); } } diff --git a/src/script/descriptor.h b/src/script/descriptor.h index b7dffeab9977..9cd99e85e31a 100644 --- a/src/script/descriptor.h +++ b/src/script/descriptor.h @@ -195,8 +195,16 @@ struct Descriptor { * * @param[out] pubkeys Any public keys * @param[out] ext_pubs Any extended public keys + * @param[in] only_observable Only return literal pubkeys and bare xpubs outside MuSig aggregates */ - virtual void GetPubKeys(std::set& pubkeys, std::set& ext_pubs) const = 0; + virtual void GetPubKeys(std::set& pubkeys, std::set& ext_pubs, bool only_observable = false) const = 0; + + /** Return all extended public keys for this descriptor, including any from subdescriptors. + * + * @param[out] ext_pubs Any extended public keys + * @param[in] exclude_observable Whether to exclude xpubs that may be observable from spends + */ + virtual void GetExtPubKeys(std::set& ext_pubs, bool exclude_observable = false) const = 0; /** Whether this descriptor produces any scripts with the Expand functions */ virtual bool HasScripts() const = 0; diff --git a/src/test/descriptor_tests.cpp b/src/test/descriptor_tests.cpp index c54f355a0c4f..eda5d1372ec4 100644 --- a/src/test/descriptor_tests.cpp +++ b/src/test/descriptor_tests.cpp @@ -1468,6 +1468,70 @@ void CheckUnused(const std::string& prv, const std::string& pub) BOOST_CHECK_EQUAL(pub_pubkeys.size() + pub_extpubs.size(), 1); } +std::set GetExtPubKeysForDescriptor(const std::string& desc, bool exclude_observable = false) +{ + FlatSigningProvider keys; + std::string error; + std::set ext_pubkeys; + auto parsed{Parse(desc, keys, error)}; + BOOST_REQUIRE_MESSAGE(!parsed.empty(), error); + for (const auto& parsed_desc : parsed) { + parsed_desc->GetExtPubKeys(ext_pubkeys, exclude_observable); + } + return ext_pubkeys; +} + +BOOST_AUTO_TEST_CASE(descriptor_get_extpubkeys_exclude_observable_test) +{ + const std::string xpub{"xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y"}; + const std::string xpub2{"xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL"}; + const std::string literal_pubkey{"03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd"}; + + auto ext_pubkeys{GetExtPubKeysForDescriptor("wpkh(" + xpub + ")")}; + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 1); + ext_pubkeys = GetExtPubKeysForDescriptor("wpkh(" + xpub + ")", /*exclude_observable=*/true); + BOOST_CHECK(ext_pubkeys.empty()); + + ext_pubkeys = GetExtPubKeysForDescriptor("wpkh([01234567]" + xpub + "/0)"); + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 1); + ext_pubkeys = GetExtPubKeysForDescriptor("wpkh([01234567]" + xpub + "/0)", /*exclude_observable=*/true); + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 1); + + ext_pubkeys = GetExtPubKeysForDescriptor("wpkh(" + xpub + "/*)"); + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 1); + ext_pubkeys = GetExtPubKeysForDescriptor("wpkh(" + xpub + "/*)", /*exclude_observable=*/true); + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 1); + + ext_pubkeys = GetExtPubKeysForDescriptor("wpkh(" + xpub + "/<0;1>)"); + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 1); + ext_pubkeys = GetExtPubKeysForDescriptor("wpkh(" + xpub + "/<0;1>)", /*exclude_observable=*/true); + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 1); + + ext_pubkeys = GetExtPubKeysForDescriptor("wsh(multi(1," + xpub + "/0," + literal_pubkey + "))"); + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 1); + ext_pubkeys = GetExtPubKeysForDescriptor("wsh(multi(1," + xpub + "/0," + literal_pubkey + "))", /*exclude_observable=*/true); + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 1); + + ext_pubkeys = GetExtPubKeysForDescriptor("tr(" + xpub + "/0/*,pk(" + xpub + "/1/*))"); + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 1); + ext_pubkeys = GetExtPubKeysForDescriptor("tr(" + xpub + "/0/*,pk(" + xpub + "/1/*))", /*exclude_observable=*/true); + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 1); + + ext_pubkeys = GetExtPubKeysForDescriptor("tr(musig(" + xpub + "/0," + xpub2 + "/0))"); + BOOST_CHECK(ext_pubkeys.empty()); + ext_pubkeys = GetExtPubKeysForDescriptor("tr(musig(" + xpub + "/0," + xpub2 + "/0))", /*exclude_observable=*/true); + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 2); + + ext_pubkeys = GetExtPubKeysForDescriptor("tr(musig(" + xpub + "," + xpub2 + "))"); + BOOST_CHECK(ext_pubkeys.empty()); + ext_pubkeys = GetExtPubKeysForDescriptor("tr(musig(" + xpub + "," + xpub2 + "))", /*exclude_observable=*/true); + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 2); + ext_pubkeys = GetExtPubKeysForDescriptor("tr(musig(" + xpub + "," + xpub2 + ")/*)"); + BOOST_CHECK(ext_pubkeys.empty()); + ext_pubkeys = GetExtPubKeysForDescriptor("tr(musig(" + xpub + "," + xpub2 + ")/*)", /*exclude_observable=*/true); + BOOST_REQUIRE_EQUAL(ext_pubkeys.size(), 2); +} + // unused() descriptors don't produce scripts, so these need to be tested separately BOOST_AUTO_TEST_CASE(unused_descriptor_test) { diff --git a/src/wallet/test/walletload_tests.cpp b/src/wallet/test/walletload_tests.cpp index 7e3802dd7cc3..0df40e84b0c8 100644 --- a/src/wallet/test/walletload_tests.cpp +++ b/src/wallet/test/walletload_tests.cpp @@ -39,7 +39,8 @@ class DummyDescriptor final : public Descriptor { std::optional ScriptSize() const override { return {}; } std::optional MaxSatisfactionWeight(bool) const override { return {}; } std::optional MaxSatisfactionElems() const override { return {}; } - void GetPubKeys(std::set& pubkeys, std::set& ext_pubs) const override {} + void GetPubKeys(std::set& pubkeys, std::set& ext_pubs, bool only_observable) const override {} + void GetExtPubKeys(std::set& ext_pubs, bool exclude_observable) const override {} bool HasScripts() const override { return true; } std::vector Warnings() const override { return {}; } uint32_t GetMaxKeyExpr() const override { return 0; } From 3f6001de5d659c4954f8a7ccded7ace05c43519b Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Wed, 14 Jan 2026 16:24:06 +0100 Subject: [PATCH 05/15] wallet: BIP138 key extraction primitives Extract and normalize eligible descriptor BIP32 public key expressions for BIP138 encrypted backups. Extended public keys that are not directly observable from descriptor spends are converted to x-only keys, while literal pubkeys, directly observable xpubs, and the BIP341 NUMS point are excluded. For MuSig2 descriptors, participant xpubs remain eligible because the on-chain aggregate key does not reveal them. Add a test vector for aggregate derivation from bare participant xpubs using the BIP138 recipient vectors. --- src/test/CMakeLists.txt | 1 + src/test/data/bip138_recipient_keys.json | 131 +++++++++++++++++++++ src/wallet/CMakeLists.txt | 1 + src/wallet/encrypted_backup.cpp | 66 +++++++++++ src/wallet/encrypted_backup.h | 50 ++++++++ src/wallet/test/CMakeLists.txt | 1 + src/wallet/test/encrypted_backup_tests.cpp | 55 +++++++++ 7 files changed, 305 insertions(+) create mode 100644 src/test/data/bip138_recipient_keys.json create mode 100644 src/wallet/encrypted_backup.cpp create mode 100644 src/wallet/encrypted_backup.h create mode 100644 src/wallet/test/encrypted_backup_tests.cpp diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index a4d95389dcea..d0289de23901 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -149,6 +149,7 @@ include(TargetDataSources) target_json_data_sources(test_bitcoin data/base58_encode_decode.json data/bip341_wallet_vectors.json + data/bip138_recipient_keys.json data/bip352_send_and_receive_vectors.json data/blockfilters.json data/key_io_invalid.json diff --git a/src/test/data/bip138_recipient_keys.json b/src/test/data/bip138_recipient_keys.json new file mode 100644 index 000000000000..f12adb0b99b6 --- /dev/null +++ b/src/test/data/bip138_recipient_keys.json @@ -0,0 +1,131 @@ +[ + { + "description": "Xpub with origin and multipath wildcard", + "descriptors": [ + "tr([58b7f8dc/48'/1'/0'/2']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/<0;1>/*)" + ], + "expected_keys": [ + "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + ] + }, + { + "description": "Xpub with single-path wildcard", + "descriptors": [ + "tr([58b7f8dc/48'/1'/0'/2']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/0/*)" + ], + "expected_keys": [ + "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + ] + }, + { + "description": "Xpub with bare wildcard (no preceding path)", + "descriptors": [ + "tr([58b7f8dc/48'/1'/0'/2']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/*)" + ], + "expected_keys": [ + "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + ] + }, + { + "description": "Xpub with fixed deep derivation, no wildcard", + "descriptors": [ + "tr([58b7f8dc/48'/1'/0'/2']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/0/5)" + ], + "expected_keys": [ + "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + ] + }, + { + "description": "Xpub with multipath, no wildcard", + "descriptors": [ + "tr([58b7f8dc/48'/1'/0'/2']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/<0;1>)" + ], + "expected_keys": [ + "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + ] + }, + { + "description": "Bare xpub (no derivation, no wildcard); DISALLOWED", + "descriptors": [ + "tr([d4ab66f1/48'/1'/1'/2']tpubDFTxBKyUCgkwp5enwZh3t2FJ5AMJqmCWoh1NRT13qNYQb1iKTUrAG6u5gpsDYhG8cZGXouYWuQtzcuSVjPStTc4dwU6JqPMFtgaLGvSQXhi)" + ], + "expected_keys": null + }, + { + "description": "Compressed literal pubkey; DISALLOWED", + "descriptors": [ + "tr(02ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708)" + ], + "expected_keys": null + }, + { + "description": "X-only literal pubkey; DISALLOWED", + "descriptors": [ + "tr(ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708)" + ], + "expected_keys": null + }, + { + "description": "Uncompressed literal pubkey; DISALLOWED", + "descriptors": [ + "pk(04ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b517089e956909c4c07e8529f45f3ff8904d28df5a181619e21bdf748a896322530039)" + ], + "expected_keys": null + }, + { + "description": "Bare MuSig participant xpubs", + "descriptors": [ + "tr(musig(tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw,tpubDC5FSnBiZDMmhiuCmWAYsLwgLYrrT9rAqvTySfuCCrgsWz8wxMXUS9Tb9iVMvcRbvFcAHGkMD5Kx8koh4GquNGNTfohfk7pgjhaPCdXpoba))" + ], + "expected_keys": [ + "8b42cd4776376c82791b494155151f56c2d7b471e0c7a526a7ce60dd872e3867", + "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + ] + }, + { + "description": "Derivation after MuSig aggregation", + "descriptors": [ + "tr(musig(tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw,tpubDC5FSnBiZDMmhiuCmWAYsLwgLYrrT9rAqvTySfuCCrgsWz8wxMXUS9Tb9iVMvcRbvFcAHGkMD5Kx8koh4GquNGNTfohfk7pgjhaPCdXpoba)/<0;1>/*)" + ], + "expected_keys": [ + "8b42cd4776376c82791b494155151f56c2d7b471e0c7a526a7ce60dd872e3867", + "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + ] + }, + { + "description": "Same root in several descriptor sets contributes once", + "descriptors": [ + "tr(tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/0/*)", + "tr(tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/1/*)", + "tr(tpubDC5FSnBiZDMmhiuCmWAYsLwgLYrrT9rAqvTySfuCCrgsWz8wxMXUS9Tb9iVMvcRbvFcAHGkMD5Kx8koh4GquNGNTfohfk7pgjhaPCdXpoba/*)" + ], + "expected_keys": [ + "8b42cd4776376c82791b494155151f56c2d7b471e0c7a526a7ce60dd872e3867", + "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + ] + }, + { + "description": "Root used both bare and derived is excluded", + "descriptors": [ + "wsh(or_i(pk(tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/0/*),pk(tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw)))" + ], + "expected_keys": null + }, + { + "description": "Xpub root (03ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708) excluded by an opposite-parity literal in another descriptor set", + "descriptors": [ + "tr(tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/*)", + "wpkh(02ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708)" + ], + "expected_keys": null + }, + { + "description": "MuSig participant exposed in a script leaf", + "descriptors": [ + "tr(musig(tpubDC5FSnBiZDMmhiuCmWAYsLwgLYrrT9rAqvTySfuCCrgsWz8wxMXUS9Tb9iVMvcRbvFcAHGkMD5Kx8koh4GquNGNTfohfk7pgjhaPCdXpoba,tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw)/0/*,pk(tpubDC5FSnBiZDMmhiuCmWAYsLwgLYrrT9rAqvTySfuCCrgsWz8wxMXUS9Tb9iVMvcRbvFcAHGkMD5Kx8koh4GquNGNTfohfk7pgjhaPCdXpoba))" + ], + "expected_keys": [ + "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + ] + } +] diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index 3520fbc391f4..e676a8a90eaa 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -11,6 +11,7 @@ add_library(bitcoin_wallet STATIC EXCLUDE_FROM_ALL db.cpp descriptor_info.cpp dump.cpp + encrypted_backup.cpp export.cpp external_signer_scriptpubkeyman.cpp feebumper.cpp diff --git a/src/wallet/encrypted_backup.cpp b/src/wallet/encrypted_backup.cpp new file mode 100644 index 000000000000..8fe00bcb8f9f --- /dev/null +++ b/src/wallet/encrypted_backup.cpp @@ -0,0 +1,66 @@ +// Copyright (c) The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include +#include + +#include +#include