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 [ + - ]: 10483 : 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 [ + + + + : 95609 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 20) {
+ + ]
120 [ + - ]: 40592 : CallOneOf(
121 : : fuzzed_data_provider,
122 : 1295 : [&] {
123 : 1295 : const CScript script{ConsumeScript(fuzzed_data_provider)};
124 [ + - + + ]: 1295 : if (spk_manager->IsMine(script)) {
125 [ + - + - : 112 : assert(spk_manager->GetScriptPubKeys().contains(script));
- + ]
126 : : }
127 : 1295 : },
128 : 7943 : [&] {
129 : 7943 : auto spks{spk_manager->GetScriptPubKeys()};
130 [ + + + - ]: 32994 : for (const CScript& spk : spks) {
131 [ - + + - ]: 25051 : assert(spk_manager->IsMine(spk));
132 : 25051 : CTxDestination dest;
133 [ + - ]: 25051 : bool extract_dest{ExtractDestination(spk, dest)};
134 [ + + ]: 25051 : if (extract_dest) {
135 [ + - ]: 19425 : const std::string msg{fuzzed_data_provider.ConsumeRandomLengthString()};
136 [ + + + + ]: 19425 : PKHash pk_hash{std::get_if<PKHash>(&dest) && fuzzed_data_provider.ConsumeBool() ?
137 : 4749 : *std::get_if<PKHash>(&dest) :
138 : 19425 : PKHash{ConsumeUInt160(fuzzed_data_provider)}};
139 [ + - ]: 19425 : std::string str_sig;
140 [ + - ]: 19425 : (void)spk_manager->SignMessage(msg, pk_hash, str_sig);
141 [ + - ]: 19425 : (void)spk_manager->GetMetadata(dest);
142 : 19425 : }
143 : 25051 : }
144 : 7943 : },
145 : 2924 : [&] {
146 : 2924 : auto spks{spk_manager->GetScriptPubKeys()};
147 [ + - ]: 2924 : if (!spks.empty()) {
148 : 2924 : auto& spk{PickValue(fuzzed_data_provider, spks)};
149 [ + - ]: 2924 : (void)spk_manager->MarkUnusedAddresses(spk);
150 : : }
151 : 2924 : },
152 : 14758 : [&] {
153 : 14758 : LOCK(spk_manager->cs_desc_man);
154 [ + - ]: 14758 : auto wallet_desc{spk_manager->GetWalletDescriptor()};
155 [ + - + + ]: 14758 : if (wallet_desc.descriptor->IsSingleType()) {
156 [ + - ]: 14601 : auto output_type{wallet_desc.descriptor->GetOutputType()};
157 [ + + ]: 14601 : 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 [ + - ]: 29516 : },
166 : 4333 : [&] {
167 : 4333 : CMutableTransaction tx_to;
168 : 4333 : const std::optional<CMutableTransaction> opt_tx_to{ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS)};
169 [ + + ]: 4333 : if (!opt_tx_to) {
170 : 191 : good_data = false;
171 [ - + ]: 191 : return;
172 : : }
173 [ + - ]: 4142 : tx_to = *opt_tx_to;
174 : :
175 : 4142 : std::map<COutPoint, Coin> coins{ConsumeCoins(fuzzed_data_provider)};
176 : 4142 : const int sighash{fuzzed_data_provider.ConsumeIntegral<int>()};
177 [ + - ]: 4142 : std::map<int, bilingual_str> input_errors;
178 [ + - ]: 4142 : (void)spk_manager->SignTransaction(tx_to, coins, sighash, input_errors);
179 [ + - ]: 12808 : },
180 : 9339 : [&] {
181 : 9339 : std::optional<PartiallySignedTransaction> opt_psbt{ConsumeDeserializable<PartiallySignedTransaction>(fuzzed_data_provider)};
182 [ + + ]: 9339 : if (!opt_psbt) {
183 : 1464 : good_data = false;
184 : 1464 : return;
185 : : }
186 [ + - ]: 7875 : auto psbt{*opt_psbt};
187 [ + - ]: 7875 : const PrecomputedTransactionData txdata{PrecomputePSBTData(psbt)};
188 [ + + ]: 7875 : std::optional<int> sighash_type{fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 151)};
189 [ + + ]: 7875 : if (sighash_type == 151) sighash_type = std::nullopt;
190 : 7875 : auto sign = fuzzed_data_provider.ConsumeBool();
191 : 7875 : auto bip32derivs = fuzzed_data_provider.ConsumeBool();
192 : 7875 : auto finalize = fuzzed_data_provider.ConsumeBool();
193 [ + - ]: 7875 : (void)spk_manager->FillPSBT(psbt, txdata, sighash_type, sign, bip32derivs, nullptr, finalize);
194 : 9339 : }
195 : : );
196 : : }
197 : :
198 : 8040 : std::string descriptor;
199 [ + - ]: 8040 : (void)spk_manager->GetDescriptorString(descriptor, /*priv=*/fuzzed_data_provider.ConsumeBool());
200 [ + - ]: 8040 : (void)spk_manager->GetEndRange();
201 [ + - ]: 8040 : (void)spk_manager->GetKeyPoolSize();
202 [ + - + - : 28090 : }
+ - ]
203 : :
204 [ + - ]: 1236 : FUZZ_TARGET(spkm_migration, .init = initialize_spkm_migration)
205 : : {
206 : 778 : SeedRandomStateForTest(SeedRand::ZEROS);
207 : 778 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
208 : 778 : NodeClockContext clock_ctx{ConsumeTime(fuzzed_data_provider)};
209 : 778 : const auto& node{g_setup->m_node};
210 [ + - ]: 778 : Chainstate& chainstate{node.chainman->ActiveChainstate()};
211 : :
212 [ + - + - ]: 778 : std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
213 [ + - ]: 778 : CWallet& wallet{*wallet_ptr};
214 : 778 : wallet.m_keypool_size = 1;
215 : 778 : {
216 [ + - ]: 778 : LOCK(wallet.cs_wallet);
217 [ + - ]: 778 : wallet.UnsetWalletFlag(WALLET_FLAG_DESCRIPTORS);
218 [ - + + - ]: 1556 : wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash());
219 : 0 : }
220 : :
221 [ + - ]: 778 : auto& legacy_data{*wallet.GetOrCreateLegacyDataSPKM()};
222 : :
223 : 778 : std::vector<CKey> keys;
224 [ + + - + ]: 3605 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30) {
225 : 2831 : const auto key{ConsumePrivateKey(fuzzed_data_provider)};
226 [ + + ]: 2831 : if (!key.IsValid()) return;
227 [ + - ]: 2827 : auto pub_key{key.GetPubKey()};
228 [ + - + - ]: 2827 : if (!pub_key.IsFullyValid()) return;
229 [ + - + - : 2827 : if (legacy_data.LoadKey(key, pub_key) && std::find(keys.begin(), keys.end(), key) == keys.end()) keys.push_back(key);
+ + + - ]
230 : 2831 : }
231 : :
232 [ + + + + ]: 774 : bool add_hd_chain{fuzzed_data_provider.ConsumeBool() && !keys.empty()};
233 : 774 : CHDChain hd_chain;
234 [ + + ]: 774 : auto version{fuzzed_data_provider.ConsumeBool() ? CHDChain::VERSION_HD_CHAIN_SPLIT : CHDChain::VERSION_HD_BASE};
235 : 774 : CKey hd_key;
236 [ + + ]: 774 : if (add_hd_chain) {
237 [ + - ]: 263 : hd_key = PickValue(fuzzed_data_provider, keys);
238 : 263 : hd_chain.nVersion = version;
239 [ + - + - ]: 263 : hd_chain.seed_id = hd_key.GetPubKey().GetID();
240 [ + - ]: 263 : legacy_data.LoadHDChain(hd_chain);
241 : : }
242 : :
243 [ + + + + ]: 774 : bool add_inactive_hd_chain{fuzzed_data_provider.ConsumeBool() && !keys.empty()};
244 : 262 : if (add_inactive_hd_chain) {
245 [ + - ]: 262 : hd_key = PickValue(fuzzed_data_provider, keys);
246 [ + + ]: 262 : hd_chain.nVersion = fuzzed_data_provider.ConsumeBool() ? CHDChain::VERSION_HD_CHAIN_SPLIT : CHDChain::VERSION_HD_BASE;
247 [ + - + - ]: 262 : hd_chain.seed_id = hd_key.GetPubKey().GetID();
248 [ + - ]: 262 : legacy_data.AddInactiveHDChain(hd_chain);
249 : : }
250 : :
251 : 774 : bool watch_only = false;
252 : 774 : const auto pub_key = ConsumeDeserializable<CPubKey>(fuzzed_data_provider);
253 [ + + + - : 774 : if (!pub_key || !pub_key->IsFullyValid()) return;
+ + ]
254 [ + - + - ]: 701 : auto script_dest{GetScriptForDestination(WitnessV0KeyHash{*pub_key})};
255 [ + + ]: 701 : if (fuzzed_data_provider.ConsumeBool()) {
256 [ + - + - ]: 818 : script_dest = GetScriptForDestination(CTxDestination{PKHash(*pub_key)});
257 : : }
258 [ + - + - ]: 701 : if (legacy_data.LoadWatchOnly(script_dest)) watch_only = true;
259 : :
260 : 701 : size_t added_script{0};
261 : 701 : bool good_data{true};
262 [ + + + + : 13294 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 30) {
+ + ]
263 [ + - ]: 5954 : CallOneOf(
264 : : fuzzed_data_provider,
265 : 1204 : [&] {
266 : 1204 : CKey key;
267 [ + + ]: 1204 : if (!keys.empty()) {
268 [ + - ]: 423 : key = PickValue(fuzzed_data_provider, keys);
269 : : } else {
270 : 781 : key = ConsumePrivateKey(fuzzed_data_provider, /*compressed=*/fuzzed_data_provider.ConsumeBool());
271 : : }
272 [ + + ]: 1204 : if (!key.IsValid()) return;
273 [ + - ]: 1191 : auto pub_key{key.GetPubKey()};
274 : 1191 : CScript script;
275 [ + - ]: 1191 : CallOneOf(
276 : : fuzzed_data_provider,
277 : 430 : [&] {
278 [ + - ]: 430 : script = GetScriptForDestination(CTxDestination{PKHash(pub_key)});
279 : 430 : },
280 : 211 : [&] {
281 [ + - ]: 211 : script = GetScriptForDestination(WitnessV0KeyHash(pub_key));
282 : 211 : },
283 : 550 : [&] {
284 : 550 : std::optional<CScript> script_opt{ConsumeDeserializable<CScript>(fuzzed_data_provider)};
285 [ + + ]: 550 : if (!script_opt) {
286 : 16 : good_data = false;
287 : 16 : return;
288 : : }
289 : 534 : script = script_opt.value();
290 : 550 : }
291 : : );
292 [ + + + - : 1624 : if (fuzzed_data_provider.ConsumeBool()) script = GetScriptForDestination(ScriptHash(script));
+ - ]
293 [ + - + - : 1191 : if (!legacy_data.HaveCScript(CScriptID(script)) && legacy_data.AddCScript(script)) added_script++;
+ + + - +
- ]
294 : 1204 : },
295 : 4750 : [&] {
296 : 4750 : CKey key;
297 [ + + ]: 4750 : if (!keys.empty()) {
298 [ + - ]: 4054 : key = PickValue(fuzzed_data_provider, keys);
299 : : } else {
300 : 696 : key = ConsumePrivateKey(fuzzed_data_provider, /*compressed=*/fuzzed_data_provider.ConsumeBool());
301 : : }
302 [ + + ]: 4750 : if (!key.IsValid()) return;
303 : 4705 : const auto num_keys{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, MAX_PUBKEYS_PER_MULTISIG)};
304 : 4705 : std::vector<CPubKey> pubkeys;
305 [ + - + - ]: 4705 : pubkeys.emplace_back(key.GetPubKey());
306 [ + + ]: 41613 : for (size_t i = 1; i < num_keys; i++) {
307 [ + + ]: 37036 : if (fuzzed_data_provider.ConsumeBool()) {
308 [ + - + - ]: 35207 : pubkeys.emplace_back(key.GetPubKey());
309 : : } else {
310 : 1829 : CKey private_key{ConsumePrivateKey(fuzzed_data_provider, /*compressed=*/fuzzed_data_provider.ConsumeBool())};
311 [ + + ]: 1829 : if (!private_key.IsValid()) return;
312 [ + - + - ]: 1701 : pubkeys.emplace_back(private_key.GetPubKey());
313 : 1829 : }
314 : : }
315 [ - + + - ]: 4577 : if (pubkeys.size() < num_keys) return;
316 [ + - ]: 4577 : CScript multisig_script{GetScriptForMultisig(num_keys, pubkeys)};
317 [ + - + - : 4577 : if (!legacy_data.HaveCScript(CScriptID(multisig_script)) && legacy_data.AddCScript(multisig_script)) {
+ + + - +
+ ]
318 : 3155 : added_script++;
319 : : }
320 : 4878 : }
321 : : );
322 : : }
323 : :
324 [ + - ]: 701 : auto result{legacy_data.MigrateToDescriptor()};
325 [ - + ]: 701 : assert(result);
326 : 701 : size_t added_chains{static_cast<size_t>(add_hd_chain) + static_cast<size_t>(add_inactive_hd_chain)};
327 [ + + + + ]: 701 : if ((add_hd_chain && version >= CHDChain::VERSION_HD_CHAIN_SPLIT) || (!add_hd_chain && add_inactive_hd_chain)) {
328 : 256 : added_chains *= 2;
329 : : }
330 [ - + ]: 701 : size_t added_size{keys.size() + added_chains};
331 [ + + ]: 701 : if (added_script > 0) {
332 [ - + - + ]: 646 : assert(result->desc_spkms.size() >= added_size);
333 : : } else {
334 [ - + - + ]: 55 : assert(result->desc_spkms.size() == added_size);
335 : : }
336 [ + - - + ]: 701 : if (watch_only) assert(!result->watch_descs.empty());
337 [ + + - + ]: 701 : if (!result->solvable_descs.empty()) assert(added_script > 0);
338 [ + - + - ]: 1556 : }
339 : :
340 : : } // namespace
341 : : } // namespace wallet
|