From 177c15d2f7cd5406ddbce8217fc023057539b828 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Fri, 4 Jun 2021 16:37:41 -0400 Subject: [PATCH] Limit LegacyScriptPubKeyMan address types Make sure that LegacyScriptPubKeyMan can only be used for legacy, p2sh-segwit, and bech32 address types. --- src/wallet/scriptpubkeyman.cpp | 9 +++++++++ src/wallet/scriptpubkeyman.h | 7 +++++++ src/wallet/wallet.cpp | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index c8baa0665e9..4212b6f34a4 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -22,6 +22,11 @@ const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000; bool LegacyScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDestination& dest, std::string& error) { + if (LEGACY_OUTPUT_TYPES.count(type) == 0) { + error = _("Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types").translated; + return false; + } + LOCK(cs_KeyStore); error.clear(); @@ -291,6 +296,10 @@ bool LegacyScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, WalletBat bool LegacyScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool) { + if (LEGACY_OUTPUT_TYPES.count(type) == 0) { + return false; + } + LOCK(cs_KeyStore); if (!CanGetAddresses(internal)) { return false; diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h index 3c4603608c1..3c6a29e5d16 100644 --- a/src/wallet/scriptpubkeyman.h +++ b/src/wallet/scriptpubkeyman.h @@ -254,6 +254,13 @@ public: boost::signals2::signal NotifyCanGetAddressesChanged; }; +/** OutputTypes supported by the LegacyScriptPubKeyMan */ +static const std::unordered_set LEGACY_OUTPUT_TYPES { + OutputType::LEGACY, + OutputType::P2SH_SEGWIT, + OutputType::BECH32, +}; + class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProvider { private: diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 256faf2b239..63d0f4cf414 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3033,7 +3033,7 @@ void CWallet::SetupLegacyScriptPubKeyMan() } auto spk_manager = std::unique_ptr(new LegacyScriptPubKeyMan(*this)); - for (const auto& type : OUTPUT_TYPES) { + for (const auto& type : LEGACY_OUTPUT_TYPES) { m_internal_spk_managers[type] = spk_manager.get(); m_external_spk_managers[type] = spk_manager.get(); }