Branch data Line data Source code
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 <addresstype.h>
6 : : #include <chainparams.h>
7 : : #include <coins.h>
8 : : #include <key.h>
9 : : #include <primitives/transaction.h>
10 : : #include <psbt.h>
11 : : #include <script/descriptor.h>
12 : : #include <script/interpreter.h>
13 : : #include <script/script.h>
14 : : #include <script/signingprovider.h>
15 : : #include <sync.h>
16 : : #include <test/fuzz/FuzzedDataProvider.h>
17 : : #include <test/fuzz/fuzz.h>
18 : : #include <test/fuzz/util.h>
19 : : #include <test/fuzz/util/descriptor.h>
20 : : #include <test/util/setup_common.h>
21 : : #include <util/check.h>
22 : : #include <util/time.h>
23 : : #include <util/translation.h>
24 : : #include <validation.h>
25 : : #include <wallet/scriptpubkeyman.h>
26 : : #include <wallet/test/util.h>
27 : : #include <wallet/types.h>
28 : : #include <wallet/wallet.h>
29 : : #include <wallet/walletutil.h>
30 : :
31 : : #include <map>
32 : : #include <memory>
33 : : #include <optional>
34 : : #include <string>
35 : : #include <utility>
36 : : #include <variant>
37 : :
38 : : namespace wallet {
39 : : namespace {
40 : : const TestingSetup* g_setup;
41 : :
42 : : //! The converter of mocked descriptors, needs to be initialized when the target is.
43 : : MockedDescriptorConverter MOCKED_DESC_CONVERTER;
44 : :
45 : 1 : void initialize_spkm()
46 : : {
47 [ + - + - : 1 : static const auto testing_setup{MakeNoLogFileContext<const TestingSetup>()};
+ - ]
48 : 1 : g_setup = testing_setup.get();
49 : 1 : SelectParams(ChainType::MAIN);
50 : 1 : MOCKED_DESC_CONVERTER.Init();
51 : 1 : }
52 : :
53 : 14896 : static std::optional<std::pair<WalletDescriptor, FlatSigningProvider>> CreateWalletDescriptor(FuzzedDataProvider& fuzzed_data_provider)
54 : : {
55 : 14896 : const std::string mocked_descriptor{fuzzed_data_provider.ConsumeRandomLengthString()};
56 [ - + + - ]: 14896 : const auto desc_str{MOCKED_DESC_CONVERTER.GetDescriptor(mocked_descriptor)};
57 [ + + ]: 14896 : if (!desc_str.has_value()) return std::nullopt;
58 [ + - + + ]: 14756 : if (IsTooExpensive(MakeUCharSpan(*desc_str))) return {};
59 : :
60 : 14380 : FlatSigningProvider keys;
61 [ + - ]: 14380 : std::string error;
62 [ + - - + : 14380 : std::vector<std::unique_ptr<Descriptor>> parsed_descs = Parse(desc_str.value(), keys, error, false);
+ - ]
63 [ + + ]: 14380 : if (parsed_descs.empty()) return std::nullopt;
64 : :
65 [ + - + - : 12359 : WalletDescriptor w_desc{std::move(parsed_descs.at(0)), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/1};
+ - ]
66 [ + - ]: 12359 : return std::make_pair(w_desc, keys);
67 : 29276 : }
68 : :
69 : 10119 : static DescriptorScriptPubKeyMan* CreateDescriptor(WalletDescriptor& wallet_desc, FlatSigningProvider& keys, CWallet& keystore)
70 : : {
71 : 10119 : LOCK(keystore.cs_wallet);
72 [ + - + - ]: 10119 : auto spk_manager_res = keystore.AddWalletDescriptor(wallet_desc, keys, /*label=*/"", /*internal=*/false);
73 [ + + ]: 10119 : if (!spk_manager_res) return nullptr;
74 : 10028 : return &spk_manager_res.value().get();
75 [ + - ]: 20238 : };
76 : :
77 [ + - ]: 12656 : FUZZ_TARGET(scriptpubkeyman, .init = initialize_spkm)
78 : : {
79 : 12206 : SeedRandomStateForTest(SeedRand::ZEROS);
80 : 12206 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
81 : 12206 : SetMockTime(ConsumeTime(fuzzed_data_provider));
82 : 12206 : const auto& node{g_setup->m_node};
83 : 12206 : Chainstate& chainstate{node.chainman->ActiveChainstate()};
84 [ + - + - ]: 12206 : std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
85 [ + - ]: 12206 : CWallet& wallet{*wallet_ptr};
86 : 12206 : {
87 [ + - ]: 12206 : LOCK(wallet.cs_wallet);
88 [ + - ]: 12206 : wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
89 [ - + + - ]: 24412 : wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash());
90 [ + - ]: 12206 : wallet.m_keypool_size = 1;
91 : 0 : }
92 : :
93 [ + - ]: 12206 : auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
94 [ + + ]: 12206 : if (!wallet_desc.has_value()) return;
95 [ + - ]: 9967 : auto spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
96 [ + + ]: 9967 : if (spk_manager == nullptr) return;
97 : :
98 [ + + ]: 9876 : if (fuzzed_data_provider.ConsumeBool()) {
99 [ + - ]: 2690 : auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
100 [ + + ]: 2690 : if (!wallet_desc.has_value()) {
101 : 298 : return;
102 : : }
103 [ + - ]: 2392 : std::string error;
104 [ + - + + ]: 2392 : if (spk_manager->CanUpdateToWalletDescriptor(wallet_desc->first, error)) {
105 [ + - ]: 152 : auto new_spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
106 [ + - ]: 152 : if (new_spk_manager != nullptr) spk_manager = new_spk_manager;
107 : : }
108 [ + - ]: 5082 : }
109 : :
110 : 9578 : bool good_data{true};
111 [ + + + + : 128335 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 20) {
+ + ]
112 [ + - ]: 55729 : CallOneOf(
113 : : fuzzed_data_provider,
114 : 1857 : [&] {
115 : 1857 : const CScript script{ConsumeScript(fuzzed_data_provider)};
116 [ + - + + ]: 1857 : if (spk_manager->IsMine(script)) {
117 [ + - + - : 112 : assert(spk_manager->GetScriptPubKeys().contains(script));
- + ]
118 : : }
119 : 1857 : },
120 : 12097 : [&] {
121 : 12097 : auto spks{spk_manager->GetScriptPubKeys()};
122 [ + + + - ]: 50997 : for (const CScript& spk : spks) {
123 [ - + + - ]: 38900 : assert(spk_manager->IsMine(spk));
124 : 38900 : CTxDestination dest;
125 [ + - ]: 38900 : bool extract_dest{ExtractDestination(spk, dest)};
126 [ + + ]: 38900 : if (extract_dest) {
127 [ + - ]: 29474 : const std::string msg{fuzzed_data_provider.ConsumeRandomLengthString()};
128 [ + + + + ]: 29474 : PKHash pk_hash{std::get_if<PKHash>(&dest) && fuzzed_data_provider.ConsumeBool() ?
129 : 8441 : *std::get_if<PKHash>(&dest) :
130 : 29474 : PKHash{ConsumeUInt160(fuzzed_data_provider)}};
131 [ + - ]: 29474 : std::string str_sig;
132 [ + - ]: 29474 : (void)spk_manager->SignMessage(msg, pk_hash, str_sig);
133 [ + - ]: 29474 : (void)spk_manager->GetMetadata(dest);
134 : 29474 : }
135 : 38900 : }
136 : 12097 : },
137 : 3941 : [&] {
138 : 3941 : auto spks{spk_manager->GetScriptPubKeys()};
139 [ + - ]: 3941 : if (!spks.empty()) {
140 : 3941 : auto& spk{PickValue(fuzzed_data_provider, spks)};
141 [ + - ]: 3941 : (void)spk_manager->MarkUnusedAddresses(spk);
142 : : }
143 : 3941 : },
144 : 19387 : [&] {
145 : 19387 : LOCK(spk_manager->cs_desc_man);
146 [ + - ]: 19387 : auto wallet_desc{spk_manager->GetWalletDescriptor()};
147 [ + - + + ]: 19387 : if (wallet_desc.descriptor->IsSingleType()) {
148 [ + - ]: 19113 : auto output_type{wallet_desc.descriptor->GetOutputType()};
149 [ + + ]: 19113 : if (output_type.has_value()) {
150 [ + - ]: 14275 : auto dest{spk_manager->GetNewDestination(*output_type)};
151 [ + + ]: 14275 : if (dest) {
152 [ + - - + ]: 12704 : assert(IsValidDestination(*dest));
153 [ + - - + ]: 12704 : assert(spk_manager->IsHDEnabled());
154 : : }
155 : 14275 : }
156 : : }
157 [ + - ]: 38774 : },
158 : 5082 : [&] {
159 : 5082 : CMutableTransaction tx_to;
160 : 5082 : const std::optional<CMutableTransaction> opt_tx_to{ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS)};
161 [ + + ]: 5082 : if (!opt_tx_to) {
162 : 315 : good_data = false;
163 [ - + ]: 315 : return;
164 : : }
165 [ + - ]: 4767 : tx_to = *opt_tx_to;
166 : :
167 : 4767 : std::map<COutPoint, Coin> coins{ConsumeCoins(fuzzed_data_provider)};
168 : 4767 : const int sighash{fuzzed_data_provider.ConsumeIntegral<int>()};
169 [ + - ]: 4767 : std::map<int, bilingual_str> input_errors;
170 [ + - ]: 4767 : (void)spk_manager->SignTransaction(tx_to, coins, sighash, input_errors);
171 [ + - ]: 14931 : },
172 : 13365 : [&] {
173 : 13365 : std::optional<PartiallySignedTransaction> opt_psbt{ConsumeDeserializable<PartiallySignedTransaction>(fuzzed_data_provider)};
174 [ + + ]: 13365 : if (!opt_psbt) {
175 : 1964 : good_data = false;
176 : 1964 : return;
177 : : }
178 [ + - ]: 11401 : auto psbt{*opt_psbt};
179 [ + - ]: 11401 : const PrecomputedTransactionData txdata{PrecomputePSBTData(psbt)};
180 [ + + ]: 11401 : std::optional<int> sighash_type{fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 151)};
181 [ + + ]: 11401 : if (sighash_type == 151) sighash_type = std::nullopt;
182 : 11401 : auto sign = fuzzed_data_provider.ConsumeBool();
183 : 11401 : auto bip32derivs = fuzzed_data_provider.ConsumeBool();
184 : 11401 : auto finalize = fuzzed_data_provider.ConsumeBool();
185 [ + - ]: 11401 : (void)spk_manager->FillPSBT(psbt, txdata, sighash_type, sign, bip32derivs, nullptr, finalize);
186 : 13365 : }
187 : : );
188 : : }
189 : :
190 : 9578 : std::string descriptor;
191 [ + - ]: 9578 : (void)spk_manager->GetDescriptorString(descriptor, /*priv=*/fuzzed_data_provider.ConsumeBool());
192 [ + - ]: 9578 : (void)spk_manager->GetEndRange();
193 [ + - ]: 9578 : (void)spk_manager->GetKeyPoolSize();
194 [ + - + - : 33990 : }
+ - ]
195 : :
196 : : } // namespace
197 : : } // namespace wallet
|