Bitcoin Core 29.99.0
P2P Digital Currency
scriptpubkeyman.cpp
Go to the documentation of this file.
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>
10#include <psbt.h>
11#include <script/descriptor.h>
12#include <script/interpreter.h>
13#include <script/script.h>
15#include <sync.h>
17#include <test/fuzz/fuzz.h>
18#include <test/fuzz/util.h>
21#include <util/check.h>
22#include <util/time.h>
23#include <util/translation.h>
24#include <validation.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
38namespace wallet {
39namespace {
40const TestingSetup* g_setup;
41
44
45void initialize_spkm()
46{
47 static const auto testing_setup{MakeNoLogFileContext<const TestingSetup>()};
48 g_setup = testing_setup.get();
51}
52
58static bool TooDeepDerivPath(std::string_view desc)
59{
60 const FuzzBufferType desc_buf{reinterpret_cast<const unsigned char *>(desc.data()), desc.size()};
61 return HasDeepDerivPath(desc_buf);
62}
63
64static std::optional<std::pair<WalletDescriptor, FlatSigningProvider>> CreateWalletDescriptor(FuzzedDataProvider& fuzzed_data_provider)
65{
66 const std::string mocked_descriptor{fuzzed_data_provider.ConsumeRandomLengthString()};
67 if (TooDeepDerivPath(mocked_descriptor)) return {};
68 const auto desc_str{MOCKED_DESC_CONVERTER.GetDescriptor(mocked_descriptor)};
69 if (!desc_str.has_value()) return std::nullopt;
70
72 std::string error;
73 std::vector<std::unique_ptr<Descriptor>> parsed_descs = Parse(desc_str.value(), keys, error, false);
74 if (parsed_descs.empty()) return std::nullopt;
75
76 WalletDescriptor w_desc{std::move(parsed_descs.at(0)), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/1};
77 return std::make_pair(w_desc, keys);
78}
79
80static DescriptorScriptPubKeyMan* CreateDescriptor(WalletDescriptor& wallet_desc, FlatSigningProvider& keys, CWallet& keystore)
81{
82 LOCK(keystore.cs_wallet);
83 DescriptorScriptPubKeyMan* descriptor_spk_manager = nullptr;
84 auto spk_manager = *Assert(keystore.AddWalletDescriptor(wallet_desc, keys, /*label=*/"", /*internal=*/false));
85 if (spk_manager) {
86 descriptor_spk_manager = dynamic_cast<DescriptorScriptPubKeyMan*>(spk_manager);
87 }
88 return descriptor_spk_manager;
89};
90
91FUZZ_TARGET(scriptpubkeyman, .init = initialize_spkm)
92{
94 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
95 SetMockTime(ConsumeTime(fuzzed_data_provider));
96 const auto& node{g_setup->m_node};
97 Chainstate& chainstate{node.chainman->ActiveChainstate()};
98 std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
99 CWallet& wallet{*wallet_ptr};
100 {
101 LOCK(wallet.cs_wallet);
102 wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
103 wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash());
104 wallet.m_keypool_size = 1;
105 }
106
107 auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
108 if (!wallet_desc.has_value()) return;
109 auto spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
110 if (spk_manager == nullptr) return;
111
112 if (fuzzed_data_provider.ConsumeBool()) {
113 auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
114 if (!wallet_desc.has_value()) {
115 return;
116 }
117 std::string error;
118 if (spk_manager->CanUpdateToWalletDescriptor(wallet_desc->first, error)) {
119 auto new_spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
120 if (new_spk_manager != nullptr) spk_manager = new_spk_manager;
121 }
122 }
123
124 bool good_data{true};
125 LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 20) {
126 CallOneOf(
127 fuzzed_data_provider,
128 [&] {
129 const CScript script{ConsumeScript(fuzzed_data_provider)};
130 auto is_mine{spk_manager->IsMine(script)};
131 if (is_mine == isminetype::ISMINE_SPENDABLE) {
132 assert(spk_manager->GetScriptPubKeys().count(script));
133 }
134 },
135 [&] {
136 auto spks{spk_manager->GetScriptPubKeys()};
137 for (const CScript& spk : spks) {
138 assert(spk_manager->IsMine(spk) == ISMINE_SPENDABLE);
139 CTxDestination dest;
140 bool extract_dest{ExtractDestination(spk, dest)};
141 if (extract_dest) {
142 const std::string msg{fuzzed_data_provider.ConsumeRandomLengthString()};
143 PKHash pk_hash{std::get_if<PKHash>(&dest) && fuzzed_data_provider.ConsumeBool() ?
144 *std::get_if<PKHash>(&dest) :
145 PKHash{ConsumeUInt160(fuzzed_data_provider)}};
146 std::string str_sig;
147 (void)spk_manager->SignMessage(msg, pk_hash, str_sig);
148 (void)spk_manager->GetMetadata(dest);
149 }
150 }
151 },
152 [&] {
153 auto spks{spk_manager->GetScriptPubKeys()};
154 if (!spks.empty()) {
155 auto& spk{PickValue(fuzzed_data_provider, spks)};
156 (void)spk_manager->MarkUnusedAddresses(spk);
157 }
158 },
159 [&] {
160 LOCK(spk_manager->cs_desc_man);
161 auto wallet_desc{spk_manager->GetWalletDescriptor()};
162 if (wallet_desc.descriptor->IsSingleType()) {
163 auto output_type{wallet_desc.descriptor->GetOutputType()};
164 if (output_type.has_value()) {
165 auto dest{spk_manager->GetNewDestination(*output_type)};
166 if (dest) {
168 assert(spk_manager->IsHDEnabled());
169 }
170 }
171 }
172 },
173 [&] {
175 const std::optional<CMutableTransaction> opt_tx_to{ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS)};
176 if (!opt_tx_to) {
177 good_data = false;
178 return;
179 }
180 tx_to = *opt_tx_to;
181
182 std::map<COutPoint, Coin> coins{ConsumeCoins(fuzzed_data_provider)};
183 const int sighash{fuzzed_data_provider.ConsumeIntegral<int>()};
184 std::map<int, bilingual_str> input_errors;
185 (void)spk_manager->SignTransaction(tx_to, coins, sighash, input_errors);
186 },
187 [&] {
188 std::optional<PartiallySignedTransaction> opt_psbt{ConsumeDeserializable<PartiallySignedTransaction>(fuzzed_data_provider)};
189 if (!opt_psbt) {
190 good_data = false;
191 return;
192 }
193 auto psbt{*opt_psbt};
195 const int sighash_type{fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 150)};
196 auto sign = fuzzed_data_provider.ConsumeBool();
197 auto bip32derivs = fuzzed_data_provider.ConsumeBool();
198 auto finalize = fuzzed_data_provider.ConsumeBool();
199 (void)spk_manager->FillPSBT(psbt, txdata, sighash_type, sign, bip32derivs, nullptr, finalize);
200 }
201 );
202 }
203
204 std::string descriptor;
205 (void)spk_manager->GetDescriptorString(descriptor, /*priv=*/fuzzed_data_provider.ConsumeBool());
206 (void)spk_manager->GetEndRange();
207 (void)spk_manager->GetKeyPoolSize();
208}
209
210} // namespace
211} // namespace wallet
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
Definition: addresstype.cpp:49
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination corresponds to one with an address.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
#define Assert(val)
Identity function.
Definition: check.h:106
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:506
std::string ConsumeRandomLengthString(size_t max_length)
T ConsumeIntegralInRange(T min, T max)
Converts a mocked descriptor string to a valid one.
Definition: descriptor.h:21
void Init()
When initializing the target, populate the list of keys.
Definition: descriptor.cpp:10
std::optional< std::string > GetDescriptor(std::string_view mocked_desc) const
Get an actual descriptor string from a descriptor string whose keys were mocked.
Definition: descriptor.cpp:51
static UniValue Parse(std::string_view raw)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
Definition: client.cpp:317
MockedDescriptorConverter MOCKED_DESC_CONVERTER
The converter of mocked descriptors, needs to be initialized when the target is.
std::span< const uint8_t > FuzzBufferType
Definition: fuzz.h:25
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
static int sign(const secp256k1_context *ctx, struct signer_secrets *signer_secrets, struct signer *signer, const secp256k1_musig_keyagg_cache *cache, const unsigned char *msg32, unsigned char *sig64)
Definition: musig.c:105
Definition: messages.h:20
FUZZ_TARGET(coin_grinder)
wallet::ScriptPubKeyMan * CreateDescriptor(CWallet &keystore, const std::string &desc_str, const bool success)
Definition: util.cpp:197
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:187
@ ISMINE_SPENDABLE
Definition: types.h:44
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:74
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction &psbt)
Compute a PrecomputedTransactionData object from a psbt.
Definition: psbt.cpp:358
node::NodeContext m_node
Definition: setup_common.h:66
A mutable version of CTransaction.
Definition: transaction.h:378
Testing setup that configures a complete environment.
Definition: setup_common.h:121
#define LOCK(cs)
Definition: sync.h:257
bool HasDeepDerivPath(const FuzzBufferType &buff, const int max_depth)
Whether the buffer, if it represents a valid descriptor, contains a derivation path deeper than a giv...
Definition: descriptor.cpp:77
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh) noexcept
Definition: util.cpp:93
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
std::map< COutPoint, Coin > ConsumeCoins(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.cpp:166
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
Definition: util.h:47
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:35
uint160 ConsumeUInt160(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:162
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:40
assert(!tx.IsCoinBase())
is a home for public enum and struct type definitions that are used by internally by wallet code,...