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 <test/util/time.h>
22 : : #include <util/check.h>
23 : : #include <util/time.h>
24 : : #include <util/translation.h>
25 : : #include <util/string.h>
26 : : #include <validation.h>
27 : : #include <wallet/context.h>
28 : : #include <wallet/scriptpubkeyman.h>
29 : : #include <wallet/test/util.h>
30 : : #include <wallet/types.h>
31 : : #include <wallet/wallet.h>
32 : : #include <wallet/walletutil.h>
33 : :
34 : : #include <map>
35 : : #include <memory>
36 : : #include <optional>
37 : : #include <string>
38 : : #include <utility>
39 : : #include <variant>
40 : :
41 : : namespace wallet {
42 : : namespace {
43 : : const TestingSetup* g_setup;
44 : :
45 : : //! The converter of mocked descriptors, needs to be initialized when the target is.
46 : : MockedDescriptorConverter MOCKED_DESC_CONVERTER;
47 : :
48 : 1 : void initialize_spkm()
49 : : {
50 [ + - + - : 2 : static const auto testing_setup{MakeNoLogFileContext<const TestingSetup>()};
+ - ]
51 : 1 : g_setup = testing_setup.get();
52 : 1 : MOCKED_DESC_CONVERTER.Init();
53 : 1 : }
54 : :
55 : 1 : void initialize_spkm_migration()
56 : : {
57 [ + - + - : 2 : static const auto testing_setup{MakeNoLogFileContext<const TestingSetup>()};
+ - ]
58 : 1 : g_setup = testing_setup.get();
59 : 1 : }
60 : :
61 : 11816 : static std::optional<std::pair<WalletDescriptor, FlatSigningProvider>> CreateWalletDescriptor(FuzzedDataProvider& fuzzed_data_provider)
62 : : {
63 : 11816 : const std::string mocked_descriptor{fuzzed_data_provider.ConsumeRandomLengthString()};
64 [ - + + - ]: 11816 : const auto desc_str{MOCKED_DESC_CONVERTER.GetDescriptor(mocked_descriptor)};
65 [ + + ]: 11816 : if (!desc_str.has_value()) return std::nullopt;
66 [ + - + + ]: 11702 : if (IsTooExpensive(MakeUCharSpan(*desc_str))) return {};
67 : :
68 : 11691 : FlatSigningProvider keys;
69 [ + - ]: 11691 : std::string error;
70 [ + - - + : 11691 : std::vector<std::unique_ptr<Descriptor>> parsed_descs = Parse(desc_str.value(), keys, error, false);
+ - ]
71 [ + + ]: 11691 : if (parsed_descs.empty()) return std::nullopt;
72 : :
73 [ + - + - : 9908 : WalletDescriptor w_desc{std::move(parsed_descs.at(0)), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/1};
+ - ]
74 [ + - ]: 9908 : return std::make_pair(w_desc, keys);
75 : 23507 : }
76 : :
77 : 8418 : static DescriptorScriptPubKeyMan* CreateDescriptor(WalletDescriptor& wallet_desc, FlatSigningProvider& keys, CWallet& keystore)
78 : : {
79 : 8418 : LOCK(keystore.cs_wallet);
80 [ + - + - ]: 8418 : auto spk_manager_res = keystore.AddWalletDescriptor(wallet_desc, keys, /*label=*/"", /*internal=*/false);
81 [ + + ]: 8418 : if (!spk_manager_res) return nullptr;
82 : 8341 : return &spk_manager_res.value().get();
83 [ + - ]: 16836 : };
84 : :
85 [ + - ]: 10485 : FUZZ_TARGET(scriptpubkeyman, .init = initialize_spkm)
86 : : {
87 : 10025 : SeedRandomStateForTest(SeedRand::ZEROS);
88 : 10025 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
89 : 10025 : NodeClockContext clock_ctx{ConsumeTime(fuzzed_data_provider)};
90 : 10025 : const auto& node{g_setup->m_node};
91 [ + - ]: 10025 : Chainstate& chainstate{node.chainman->ActiveChainstate()};
92 [ + - + - ]: 10025 : std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
93 [ + - ]: 10025 : CWallet& wallet{*wallet_ptr};
94 : 10025 : {
95 [ + - ]: 10025 : LOCK(wallet.cs_wallet);
96 [ + - ]: 10025 : wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
97 [ - + + - ]: 20050 : wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash());
98 [ + - ]: 10025 : wallet.m_keypool_size = 1;
99 : 0 : }
100 : :
101 [ + - ]: 10025 : auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
102 [ + + ]: 10025 : if (!wallet_desc.has_value()) return;
103 [ + - ]: 8316 : auto spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
104 [ + + ]: 8316 : if (spk_manager == nullptr) return;
105 : :
106 [ + + ]: 8239 : if (fuzzed_data_provider.ConsumeBool()) {
107 [ + - ]: 1791 : auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
108 [ + + ]: 1791 : if (!wallet_desc.has_value()) {
109 : 199 : return;
110 : : }
111 [ + - ]: 1592 : std::string error;
112 [ + - + + ]: 1592 : if (spk_manager->CanUpdateToWalletDescriptor(wallet_desc->first, error)) {
113 [ + - ]: 102 : auto new_spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
114 [ + - ]: 102 : if (new_spk_manager != nullptr) spk_manager = new_spk_manager;
115 : : }
116 [ + - ]: 3383 : }
117 : :
118 : 8040 : bool good_data{true};
119 [ + + + + : 89911 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 20) {
+ + ]
120 [ + - ]: 37833 : CallOneOf(
121 : : fuzzed_data_provider,
122 : 1281 : [&] {
123 : 1281 : const CScript script{ConsumeScript(fuzzed_data_provider)};
124 [ + - + + ]: 1281 : if (spk_manager->IsMine(script)) {
125 [ + - + - : 112 : assert(spk_manager->GetScriptPubKeys().contains(script));
- + ]
126 : : }
127 : 1281 : },
128 : 7728 : [&] {
129 : 7728 : auto spks{spk_manager->GetScriptPubKeys()};
130 [ + + + - ]: 32544 : for (const CScript& spk : spks) {
131 [ - + + - ]: 24816 : assert(spk_manager->IsMine(spk));
132 : 24816 : CTxDestination dest;
133 [ + - ]: 24816 : bool extract_dest{ExtractDestination(spk, dest)};
134 [ + + ]: 24816 : if (extract_dest) {
135 [ + - ]: 19404 : const std::string msg{fuzzed_data_provider.ConsumeRandomLengthString()};
136 [ + + + + ]: 19404 : PKHash pk_hash{std::get_if<PKHash>(&dest) && fuzzed_data_provider.ConsumeBool() ?
137 : 4741 : *std::get_if<PKHash>(&dest) :
138 : 19404 : PKHash{ConsumeUInt160(fuzzed_data_provider)}};
139 [ + - ]: 19404 : std::string str_sig;
140 [ + - ]: 19404 : (void)spk_manager->SignMessage(msg, pk_hash, str_sig);
141 [ + - ]: 19404 : (void)spk_manager->GetMetadata(dest);
142 : 19404 : }
143 : 24816 : }
144 : 7728 : },
145 : 2899 : [&] {
146 : 2899 : auto spks{spk_manager->GetScriptPubKeys()};
147 [ + - ]: 2899 : if (!spks.empty()) {
148 : 2899 : auto& spk{PickValue(fuzzed_data_provider, spks)};
149 [ + - ]: 2899 : (void)spk_manager->MarkUnusedAddresses(spk);
150 : : }
151 : 2899 : },
152 : 14731 : [&] {
153 : 14731 : LOCK(spk_manager->cs_desc_man);
154 [ + - ]: 14731 : auto wallet_desc{spk_manager->GetWalletDescriptor()};
155 [ + - + + ]: 14731 : if (wallet_desc.descriptor->IsSingleType()) {
156 [ + - ]: 14574 : auto output_type{wallet_desc.descriptor->GetOutputType()};
157 [ + + ]: 14574 : if (output_type.has_value()) {
158 [ + - ]: 11802 : auto dest{spk_manager->GetNewDestination(*output_type)};
159 [ + + ]: 11802 : if (dest) {
160 [ + - - + ]: 10859 : assert(IsValidDestination(*dest));
161 [ + - - + ]: 10859 : assert(spk_manager->IsHDEnabled());
162 : : }
163 : 11802 : }
164 : : }
165 [ + - ]: 29462 : },
166 : 4325 : [&] {
167 : 4325 : CMutableTransaction tx_to;
168 : 4325 : const std::optional<CMutableTransaction> opt_tx_to{ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS)};
169 [ + + ]: 4325 : if (!opt_tx_to) {
170 : 185 : good_data = false;
171 [ - + ]: 185 : return;
172 : : }
173 [ + - ]: 4140 : tx_to = *opt_tx_to;
174 : :
175 : 4140 : std::map<COutPoint, Coin> coins{ConsumeCoins(fuzzed_data_provider)};
176 : 4140 : const int sighash{fuzzed_data_provider.ConsumeIntegral<int>()};
177 [ + - ]: 4140 : std::map<int, bilingual_str> input_errors;
178 [ + - ]: 4140 : (void)spk_manager->SignTransaction(tx_to, coins, sighash, input_errors);
179 [ + - ]: 12790 : },
180 : 6869 : [&] {
181 : 6869 : std::optional<PartiallySignedTransaction> opt_psbt{ConsumeDeserializableConstructor<PartiallySignedTransaction>(fuzzed_data_provider)};
182 [ + + ]: 6869 : if (!opt_psbt) {
183 : 1650 : good_data = false;
184 : 1650 : return;
185 : : }
186 [ + - ]: 5219 : auto psbt{*opt_psbt};
187 [ + - ]: 5219 : std::optional<PrecomputedTransactionData> txdata_res = PrecomputePSBTData(psbt);
188 [ - + ]: 5219 : if (!txdata_res) {
189 : 0 : return;
190 : : }
191 : 5219 : const PrecomputedTransactionData& txdata = *txdata_res;
192 : 5219 : common::PSBTFillOptions options{
193 : 5219 : .sign = fuzzed_data_provider.ConsumeBool(),
194 : 5219 : .sighash_type = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 151),
195 : 10438 : .finalize = fuzzed_data_provider.ConsumeBool(),
196 : 10438 : .bip32_derivs = fuzzed_data_provider.ConsumeBool()
197 : 5219 : };
198 [ + - ]: 5219 : if (options.sighash_type == 151) options.sighash_type = std::nullopt;
199 [ + - ]: 5219 : (void)spk_manager->FillPSBT(psbt, txdata, options);
200 : 6869 : }
201 : : );
202 : : }
203 : :
204 : 8040 : std::string descriptor;
205 [ + - ]: 8040 : (void)spk_manager->GetDescriptorString(descriptor, /*priv=*/fuzzed_data_provider.ConsumeBool());
206 [ + - ]: 8040 : (void)spk_manager->GetEndRange();
207 [ + - ]: 8040 : (void)spk_manager->GetKeyPoolSize();
208 [ + - + - : 28090 : }
+ - ]
209 : :
210 [ + - ]: 1238 : FUZZ_TARGET(spkm_migration, .init = initialize_spkm_migration)
211 : : {
212 : 778 : SeedRandomStateForTest(SeedRand::ZEROS);
213 : 778 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
214 : 778 : NodeClockContext clock_ctx{ConsumeTime(fuzzed_data_provider)};
215 : 778 : const auto& node{g_setup->m_node};
216 [ + - ]: 778 : Chainstate& chainstate{node.chainman->ActiveChainstate()};
217 : :
218 [ + - + - ]: 778 : std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
219 [ + - ]: 778 : CWallet& wallet{*wallet_ptr};
220 : 778 : wallet.m_keypool_size = 1;
221 : 778 : {
222 [ + - ]: 778 : LOCK(wallet.cs_wallet);
223 [ + - ]: 778 : wallet.UnsetWalletFlag(WALLET_FLAG_DESCRIPTORS);
224 [ - + + - ]: 1556 : wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash());
225 : 0 : }
226 : :
227 [ + - ]: 778 : auto& legacy_data{*wallet.GetOrCreateLegacyDataSPKM()};
228 : :
229 : 778 : std::vector<CKey> keys;
230 [ + + - + ]: 3605 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30) {
231 : 2831 : const auto key{ConsumePrivateKey(fuzzed_data_provider)};
232 [ + + ]: 2831 : if (!key.IsValid()) return;
233 [ + - ]: 2827 : auto pub_key{key.GetPubKey()};
234 [ + - + - ]: 2827 : if (!pub_key.IsFullyValid()) return;
235 [ + - + - : 2827 : if (legacy_data.LoadKey(key, pub_key) && std::find(keys.begin(), keys.end(), key) == keys.end()) keys.push_back(key);
+ + + - ]
236 : 2831 : }
237 : :
238 [ + + + + ]: 774 : bool add_hd_chain{fuzzed_data_provider.ConsumeBool() && !keys.empty()};
239 : 774 : CHDChain hd_chain;
240 [ + + ]: 774 : auto version{fuzzed_data_provider.ConsumeBool() ? CHDChain::VERSION_HD_CHAIN_SPLIT : CHDChain::VERSION_HD_BASE};
241 : 774 : CKey hd_key;
242 [ + + ]: 774 : if (add_hd_chain) {
243 [ + - ]: 263 : hd_key = PickValue(fuzzed_data_provider, keys);
244 : 263 : hd_chain.nVersion = version;
245 [ + - + - ]: 263 : hd_chain.seed_id = hd_key.GetPubKey().GetID();
246 [ + - ]: 263 : legacy_data.LoadHDChain(hd_chain);
247 : : }
248 : :
249 [ + + + + ]: 774 : bool add_inactive_hd_chain{fuzzed_data_provider.ConsumeBool() && !keys.empty()};
250 : 262 : if (add_inactive_hd_chain) {
251 [ + - ]: 262 : hd_key = PickValue(fuzzed_data_provider, keys);
252 [ + + ]: 262 : hd_chain.nVersion = fuzzed_data_provider.ConsumeBool() ? CHDChain::VERSION_HD_CHAIN_SPLIT : CHDChain::VERSION_HD_BASE;
253 [ + - + - ]: 262 : hd_chain.seed_id = hd_key.GetPubKey().GetID();
254 [ + - ]: 262 : legacy_data.AddInactiveHDChain(hd_chain);
255 : : }
256 : :
257 : 774 : bool watch_only = false;
258 : 774 : const auto pub_key = ConsumeDeserializable<CPubKey>(fuzzed_data_provider);
259 [ + + + - : 774 : if (!pub_key || !pub_key->IsFullyValid()) return;
+ + ]
260 [ + - + - ]: 701 : auto script_dest{GetScriptForDestination(WitnessV0KeyHash{*pub_key})};
261 [ + + ]: 701 : if (fuzzed_data_provider.ConsumeBool()) {
262 [ + - + - ]: 818 : script_dest = GetScriptForDestination(CTxDestination{PKHash(*pub_key)});
263 : : }
264 [ + - + - ]: 701 : if (legacy_data.LoadWatchOnly(script_dest)) watch_only = true;
265 : :
266 : 701 : size_t added_script{0};
267 : 701 : bool good_data{true};
268 [ + + + + : 13294 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 30) {
+ + ]
269 [ + - ]: 5954 : CallOneOf(
270 : : fuzzed_data_provider,
271 : 1204 : [&] {
272 : 1204 : CKey key;
273 [ + + ]: 1204 : if (!keys.empty()) {
274 [ + - ]: 423 : key = PickValue(fuzzed_data_provider, keys);
275 : : } else {
276 : 781 : key = ConsumePrivateKey(fuzzed_data_provider, /*compressed=*/fuzzed_data_provider.ConsumeBool());
277 : : }
278 [ + + ]: 1204 : if (!key.IsValid()) return;
279 [ + - ]: 1191 : auto pub_key{key.GetPubKey()};
280 : 1191 : CScript script;
281 [ + - ]: 1191 : CallOneOf(
282 : : fuzzed_data_provider,
283 : 430 : [&] {
284 [ + - ]: 430 : script = GetScriptForDestination(CTxDestination{PKHash(pub_key)});
285 : 430 : },
286 : 211 : [&] {
287 [ + - ]: 211 : script = GetScriptForDestination(WitnessV0KeyHash(pub_key));
288 : 211 : },
289 : 550 : [&] {
290 : 550 : std::optional<CScript> script_opt{ConsumeDeserializable<CScript>(fuzzed_data_provider)};
291 [ + + ]: 550 : if (!script_opt) {
292 : 16 : good_data = false;
293 : 16 : return;
294 : : }
295 : 534 : script = script_opt.value();
296 : 550 : }
297 : : );
298 [ + + + - : 1624 : if (fuzzed_data_provider.ConsumeBool()) script = GetScriptForDestination(ScriptHash(script));
+ - ]
299 [ + - + - : 1191 : if (!legacy_data.HaveCScript(CScriptID(script)) && legacy_data.AddCScript(script)) added_script++;
+ + + - +
- ]
300 : 1204 : },
301 : 4750 : [&] {
302 : 4750 : CKey key;
303 [ + + ]: 4750 : if (!keys.empty()) {
304 [ + - ]: 4054 : key = PickValue(fuzzed_data_provider, keys);
305 : : } else {
306 : 696 : key = ConsumePrivateKey(fuzzed_data_provider, /*compressed=*/fuzzed_data_provider.ConsumeBool());
307 : : }
308 [ + + ]: 4750 : if (!key.IsValid()) return;
309 : 4705 : const auto num_keys{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, MAX_PUBKEYS_PER_MULTISIG)};
310 : 4705 : std::vector<CPubKey> pubkeys;
311 [ + - + - ]: 4705 : pubkeys.emplace_back(key.GetPubKey());
312 [ + + ]: 41613 : for (size_t i = 1; i < num_keys; i++) {
313 [ + + ]: 37036 : if (fuzzed_data_provider.ConsumeBool()) {
314 [ + - + - ]: 35207 : pubkeys.emplace_back(key.GetPubKey());
315 : : } else {
316 : 1829 : CKey private_key{ConsumePrivateKey(fuzzed_data_provider, /*compressed=*/fuzzed_data_provider.ConsumeBool())};
317 [ + + ]: 1829 : if (!private_key.IsValid()) return;
318 [ + - + - ]: 1701 : pubkeys.emplace_back(private_key.GetPubKey());
319 : 1829 : }
320 : : }
321 [ - + + - ]: 4577 : if (pubkeys.size() < num_keys) return;
322 [ + - ]: 4577 : CScript multisig_script{GetScriptForMultisig(num_keys, pubkeys)};
323 [ + - + - : 4577 : if (!legacy_data.HaveCScript(CScriptID(multisig_script)) && legacy_data.AddCScript(multisig_script)) {
+ + + - +
+ ]
324 : 3155 : added_script++;
325 : : }
326 : 4878 : }
327 : : );
328 : : }
329 : :
330 [ + - ]: 701 : auto result{legacy_data.MigrateToDescriptor()};
331 [ - + ]: 701 : assert(result);
332 : 701 : size_t added_chains{static_cast<size_t>(add_hd_chain) + static_cast<size_t>(add_inactive_hd_chain)};
333 [ + + + + ]: 701 : if ((add_hd_chain && version >= CHDChain::VERSION_HD_CHAIN_SPLIT) || (!add_hd_chain && add_inactive_hd_chain)) {
334 : 256 : added_chains *= 2;
335 : : }
336 [ - + ]: 701 : size_t added_size{keys.size() + added_chains};
337 [ + + ]: 701 : if (added_script > 0) {
338 [ - + - + ]: 646 : assert(result->desc_spkms.size() >= added_size);
339 : : } else {
340 [ - + - + ]: 55 : assert(result->desc_spkms.size() == added_size);
341 : : }
342 [ + - - + ]: 701 : if (watch_only) assert(!result->watch_descs.empty());
343 [ + + - + ]: 701 : if (!result->solvable_descs.empty()) assert(added_script > 0);
344 [ + - + - ]: 1556 : }
345 : :
346 : : } // namespace
347 : : } // namespace wallet
|