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