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