From c1e6c542af6d89a499e2a65465865aec651c4d67 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 4 Oct 2023 10:56:52 -0400 Subject: descriptors: disallow hybrid public keys The descriptor documentation (doc/descriptors.md) and BIP380 explicitly require that hex-encoded public keys start with 02 or 03 (compressed) or 04 (uncompressed). However, the current parsing/inference code permit 06 and 07 (hybrid) encoding as well. Fix this. --- src/script/descriptor.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/script') diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index 2f3f2c7a1d..896fb0b5b3 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -1290,6 +1290,10 @@ std::unique_ptr ParsePubkeyInner(uint32_t key_exp_index, const S if (IsHex(str)) { std::vector data = ParseHex(str); CPubKey pubkey(data); + if (pubkey.IsValid() && !pubkey.IsValidNonHybrid()) { + error = "Hybrid public keys are not allowed"; + return nullptr; + } if (pubkey.IsFullyValid()) { if (permit_uncompressed || pubkey.IsCompressed()) { return std::make_unique(key_exp_index, pubkey, false); @@ -1448,7 +1452,7 @@ struct KeyParser { { assert(m_in); CPubKey pubkey(begin, end); - if (pubkey.IsValid()) { + if (pubkey.IsValidNonHybrid()) { Key key = m_keys.size(); m_keys.push_back(InferPubkey(pubkey, ParseScriptContext::P2WSH, *m_in)); return key; @@ -1795,7 +1799,7 @@ std::unique_ptr InferScript(const CScript& script, ParseScriptCo if (txntype == TxoutType::PUBKEY && (ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH || ctx == ParseScriptContext::P2WSH)) { CPubKey pubkey(data[0]); - if (pubkey.IsValid()) { + if (pubkey.IsValidNonHybrid()) { return std::make_unique(InferPubkey(pubkey, ctx, provider)); } } -- cgit v1.2.3