|
| 1 | +// Copyright (c) 2023-present The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <chainparams.h> |
| 6 | +#include <validation.h> |
| 7 | +#include <test/fuzz/FuzzedDataProvider.h> |
| 8 | +#include <test/fuzz/fuzz.h> |
| 9 | +#include <test/fuzz/util.h> |
| 10 | +#include <test/fuzz/util/descriptor.h> |
| 11 | +#include <test/util/setup_common.h> |
| 12 | +#include <wallet/scriptpubkeyman.h> |
| 13 | +#include <wallet/wallet.h> |
| 14 | +#include <wallet/test/util.h> |
| 15 | + |
| 16 | +namespace wallet { |
| 17 | +namespace { |
| 18 | +const TestingSetup* g_setup; |
| 19 | + |
| 20 | +//! The converter of mocked descriptors, needs to be initialized when the target is. |
| 21 | +MockedDescriptorConverter MOCKED_DESC_CONVERTER; |
| 22 | + |
| 23 | +void initialize_spkm() |
| 24 | +{ |
| 25 | + static const auto testing_setup{MakeNoLogFileContext<const TestingSetup>()}; |
| 26 | + g_setup = testing_setup.get(); |
| 27 | + SelectParams(ChainType::MAIN); |
| 28 | + MOCKED_DESC_CONVERTER.Init(); |
| 29 | +} |
| 30 | + |
| 31 | +static std::optional<std::pair<WalletDescriptor, FlatSigningProvider>> CreateWalletDescriptor(FuzzedDataProvider& fuzzed_data_provider) |
| 32 | +{ |
| 33 | + const std::string mocked_descriptor{fuzzed_data_provider.ConsumeRandomLengthString()}; |
| 34 | + const auto desc_str{MOCKED_DESC_CONVERTER.GetDescriptor(mocked_descriptor)}; |
| 35 | + if (!desc_str.has_value()) return std::nullopt; |
| 36 | + |
| 37 | + FlatSigningProvider keys; |
| 38 | + std::string error; |
| 39 | + std::unique_ptr<Descriptor> parsed_desc{Parse(desc_str.value(), keys, error, false)}; |
| 40 | + if (!parsed_desc) return std::nullopt; |
| 41 | + |
| 42 | + WalletDescriptor w_desc{std::move(parsed_desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/1}; |
| 43 | + return std::make_pair(w_desc, keys); |
| 44 | +} |
| 45 | + |
| 46 | +static DescriptorScriptPubKeyMan* CreateDescriptor(WalletDescriptor& wallet_desc, FlatSigningProvider& keys, CWallet& keystore) |
| 47 | +{ |
| 48 | + LOCK(keystore.cs_wallet); |
| 49 | + keystore.AddWalletDescriptor(wallet_desc, keys, /*label=*/"", /*internal=*/false); |
| 50 | + return keystore.GetDescriptorScriptPubKeyMan(wallet_desc); |
| 51 | +}; |
| 52 | + |
| 53 | +FUZZ_TARGET(scriptpubkeyman, .init = initialize_spkm) |
| 54 | +{ |
| 55 | + FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; |
| 56 | + const auto& node{g_setup->m_node}; |
| 57 | + Chainstate& chainstate{node.chainman->ActiveChainstate()}; |
| 58 | + std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())}; |
| 59 | + CWallet& wallet{*wallet_ptr}; |
| 60 | + { |
| 61 | + LOCK(wallet.cs_wallet); |
| 62 | + wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS); |
| 63 | + wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash()); |
| 64 | + } |
| 65 | + |
| 66 | + auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)}; |
| 67 | + if (!wallet_desc.has_value()) return; |
| 68 | + auto spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)}; |
| 69 | + if (spk_manager == nullptr) return; |
| 70 | + |
| 71 | + bool good_data{true}; |
| 72 | + LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 300) { |
| 73 | + CallOneOf( |
| 74 | + fuzzed_data_provider, |
| 75 | + [&] { |
| 76 | + auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)}; |
| 77 | + if (!wallet_desc.has_value()) { |
| 78 | + good_data = false; |
| 79 | + return; |
| 80 | + } |
| 81 | + std::string error; |
| 82 | + if (spk_manager->CanUpdateToWalletDescriptor(wallet_desc->first, error)) { |
| 83 | + auto new_spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)}; |
| 84 | + if (new_spk_manager != nullptr) spk_manager = new_spk_manager; |
| 85 | + } |
| 86 | + }, |
| 87 | + [&] { |
| 88 | + const CScript script{ConsumeScript(fuzzed_data_provider)}; |
| 89 | + auto is_mine{spk_manager->IsMine(script)}; |
| 90 | + if (is_mine == isminetype::ISMINE_SPENDABLE) { |
| 91 | + assert(spk_manager->GetScriptPubKeys().count(script)); |
| 92 | + } |
| 93 | + }, |
| 94 | + [&] { |
| 95 | + auto spks{spk_manager->GetScriptPubKeys()}; |
| 96 | + for (const CScript& spk : spks) { |
| 97 | + assert(spk_manager->IsMine(spk) == ISMINE_SPENDABLE); |
| 98 | + CTxDestination dest; |
| 99 | + bool extract_dest{ExtractDestination(spk, dest)}; |
| 100 | + if (extract_dest) { |
| 101 | + const std::string msg{fuzzed_data_provider.ConsumeRandomLengthString()}; |
| 102 | + PKHash pk_hash{fuzzed_data_provider.ConsumeBool() ? PKHash{ConsumeUInt160(fuzzed_data_provider)} : *std::get_if<PKHash>(&dest)}; |
| 103 | + std::string str_sig; |
| 104 | + (void)spk_manager->SignMessage(msg, pk_hash, str_sig); |
| 105 | + } |
| 106 | + } |
| 107 | + }, |
| 108 | + [&] { |
| 109 | + CKey key{ConsumePrivateKey(fuzzed_data_provider, /*compressed=*/fuzzed_data_provider.ConsumeBool())}; |
| 110 | + if (!key.IsValid()) { |
| 111 | + good_data = false; |
| 112 | + return; |
| 113 | + } |
| 114 | + spk_manager->AddDescriptorKey(key, key.GetPubKey()); |
| 115 | + spk_manager->TopUp(); |
| 116 | + }, |
| 117 | + [&] { |
| 118 | + std::string descriptor; |
| 119 | + (void)spk_manager->GetDescriptorString(descriptor, /*priv=*/fuzzed_data_provider.ConsumeBool()); |
| 120 | + }, |
| 121 | + [&] { |
| 122 | + LOCK(spk_manager->cs_desc_man); |
| 123 | + auto wallet_desc{spk_manager->GetWalletDescriptor()}; |
| 124 | + if (wallet_desc.descriptor->IsSingleType()) { |
| 125 | + auto output_type{wallet_desc.descriptor->GetOutputType()}; |
| 126 | + if (output_type.has_value()) { |
| 127 | + auto dest{spk_manager->GetNewDestination(*output_type)}; |
| 128 | + if (dest) { |
| 129 | + assert(IsValidDestination(*dest)); |
| 130 | + assert(spk_manager->IsHDEnabled()); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + }, |
| 135 | + [&] { |
| 136 | + CMutableTransaction tx_to; |
| 137 | + const std::optional<CMutableTransaction> opt_tx_to{ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS)}; |
| 138 | + if (!opt_tx_to) { |
| 139 | + good_data = false; |
| 140 | + return; |
| 141 | + } |
| 142 | + tx_to = *opt_tx_to; |
| 143 | + |
| 144 | + std::map<COutPoint, Coin> coins{ConsumeCoins(fuzzed_data_provider)}; |
| 145 | + const int sighash{fuzzed_data_provider.ConsumeIntegral<int>()}; |
| 146 | + std::map<int, bilingual_str> input_errors; |
| 147 | + (void)spk_manager->SignTransaction(tx_to, coins, sighash, input_errors); |
| 148 | + }, |
| 149 | + [&] { |
| 150 | + std::optional<PartiallySignedTransaction> opt_psbt{ConsumeDeserializable<PartiallySignedTransaction>(fuzzed_data_provider)}; |
| 151 | + if (!opt_psbt) { |
| 152 | + good_data = false; |
| 153 | + return; |
| 154 | + } |
| 155 | + auto psbt{*opt_psbt}; |
| 156 | + const PrecomputedTransactionData txdata{PrecomputePSBTData(psbt)}; |
| 157 | + const int sighash_type{fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 150)}; |
| 158 | + (void)spk_manager->FillPSBT(psbt, txdata, sighash_type, fuzzed_data_provider.ConsumeBool(), fuzzed_data_provider.ConsumeBool(), nullptr, fuzzed_data_provider.ConsumeBool()); |
| 159 | + } |
| 160 | + ); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +} // namespace |
| 165 | +} // namespace wallet |
0 commit comments