From 70dcdd7aa7f9093c6faac84760db8ccc95090209 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Wed, 18 May 2022 02:10:52 +0000 Subject: [PATCH] Wallet: Avoid using change types newer than user's preferred address type --- src/wallet/test/util.cpp | 1 + src/wallet/wallet.cpp | 12 +++++++++++- test/functional/wallet_taproot.py | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/wallet/test/util.cpp b/src/wallet/test/util.cpp index b7bf312edf..b5bda0415e 100644 --- a/src/wallet/test/util.cpp +++ b/src/wallet/test/util.cpp @@ -20,6 +20,7 @@ std::unique_ptr CreateSyncedWallet(interfaces::Chain& chain, CChain& cc { LOCK2(wallet->cs_wallet, ::cs_main); wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash()); + wallet->m_default_address_type = OutputType::BECH32M; } wallet->LoadWallet(); { diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 4e8c0c0e5e..bf5cd46bfa 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2267,7 +2267,7 @@ OutputType CWallet::TransactionChangeType(const std::optional& chang } const bool has_bech32m_spkman(GetScriptPubKeyMan(OutputType::BECH32M, /*internal=*/true)); - if (has_bech32m_spkman && any_tr) { + if (has_bech32m_spkman && any_tr && m_default_address_type == OutputType::BECH32M) { // Currently tr is the only type supported by the BECH32M spkman return OutputType::BECH32M; } @@ -2287,6 +2287,16 @@ OutputType CWallet::TransactionChangeType(const std::optional& chang // Currently pkh is the only type supported by the LEGACY spkman return OutputType::LEGACY; } + if (!GetScriptPubKeyMan(m_default_address_type, /*internal=*/true)) { + // Default type not available, so look for anything else to fallback to + // NOTE: Sane behaviour assumes OUTPUT_TYPES is sorted oldest to newest + for (const auto& ot : OUTPUT_TYPES) { + if (GetScriptPubKeyMan(ot, /*internal=*/true)) { + return ot; + } + } + } + return m_default_address_type; if (has_bech32m_spkman) { return OutputType::BECH32M; diff --git a/test/functional/wallet_taproot.py b/test/functional/wallet_taproot.py index b52892704f..c129a01189 100755 --- a/test/functional/wallet_taproot.py +++ b/test/functional/wallet_taproot.py @@ -194,6 +194,8 @@ class WalletTaprootTest(BitcoinTestFramework): self.num_nodes = 2 self.setup_clean_chain = True self.extra_args = [['-keypool=100'], ['-keypool=100']] + for ea in self.extra_args: + ea.append('-addresstype=bech32m') self.supports_cli = False def skip_test_if_missing_module(self):