6 #include <chainparams.h> 23 #include <validation.h> 44 void initialize_spkm()
46 static const auto testing_setup{MakeNoLogFileContext<const TestingSetup>()};
47 g_setup = testing_setup.get();
57 static bool TooDeepDerivPath(std::string_view desc)
59 const FuzzBufferType desc_buf{
reinterpret_cast<const unsigned char *
>(desc.data()), desc.size()};
63 static std::optional<std::pair<WalletDescriptor, FlatSigningProvider>> CreateWalletDescriptor(
FuzzedDataProvider& fuzzed_data_provider)
66 if (TooDeepDerivPath(mocked_descriptor))
return {};
68 if (!desc_str.has_value())
return std::nullopt;
72 std::unique_ptr<Descriptor> parsed_desc{
Parse(desc_str.value(), keys, error,
false)};
73 if (!parsed_desc)
return std::nullopt;
75 WalletDescriptor w_desc{std::move(parsed_desc), 0, 0, 1, 1};
76 return std::make_pair(w_desc, keys);
81 LOCK(keystore.cs_wallet);
82 keystore.AddWalletDescriptor(wallet_desc, keys,
"",
false);
83 return keystore.GetDescriptorScriptPubKeyMan(wallet_desc);
92 CWallet&
wallet{*wallet_ptr};
96 wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash());
100 auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
101 if (!wallet_desc.has_value())
return;
103 if (spk_manager ==
nullptr)
return;
106 auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
107 if (!wallet_desc.has_value()) {
111 if (spk_manager->CanUpdateToWalletDescriptor(wallet_desc->first, error)) {
113 if (new_spk_manager !=
nullptr) spk_manager = new_spk_manager;
117 bool good_data{
true};
120 fuzzed_data_provider,
123 auto is_mine{spk_manager->IsMine(
script)};
129 auto spks{spk_manager->GetScriptPubKeys()};
130 for (
const CScript& spk : spks) {
136 PKHash pk_hash{std::get_if<PKHash>(&dest) && fuzzed_data_provider.
ConsumeBool() ?
137 *std::get_if<PKHash>(&dest) :
140 (void)spk_manager->SignMessage(
msg, pk_hash, str_sig);
141 (void)spk_manager->GetMetadata(dest);
146 auto spks{spk_manager->GetScriptPubKeys()};
148 auto& spk{
PickValue(fuzzed_data_provider, spks)};
149 (void)spk_manager->MarkUnusedAddresses(spk);
153 LOCK(spk_manager->cs_desc_man);
154 auto wallet_desc{spk_manager->GetWalletDescriptor()};
155 if (wallet_desc.descriptor->IsSingleType()) {
156 auto output_type{wallet_desc.descriptor->GetOutputType()};
157 if (output_type.has_value()) {
158 auto dest{spk_manager->GetNewDestination(*output_type)};
161 assert(spk_manager->IsHDEnabled());
168 const std::optional<CMutableTransaction> opt_tx_to{ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider,
TX_WITH_WITNESS)};
175 std::map<COutPoint, Coin> coins{
ConsumeCoins(fuzzed_data_provider)};
177 std::map<int, bilingual_str> input_errors;
178 (void)spk_manager->SignTransaction(tx_to, coins,
sighash, input_errors);
181 std::optional<PartiallySignedTransaction> opt_psbt{ConsumeDeserializable<PartiallySignedTransaction>(fuzzed_data_provider)};
186 auto psbt{*opt_psbt};
189 (void)spk_manager->FillPSBT(psbt, txdata, sighash_type, fuzzed_data_provider.
ConsumeBool(), fuzzed_data_provider.
ConsumeBool(),
nullptr, fuzzed_data_provider.
ConsumeBool());
194 std::string descriptor;
195 (void)spk_manager->GetDescriptorString(descriptor, fuzzed_data_provider.
ConsumeBool());
196 (void)spk_manager->GetEndRange();
197 (void)spk_manager->GetKeyPoolSize();
static const std::string sighash
static UniValue Parse(std::string_view raw)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
wallet::ScriptPubKeyMan * CreateDescriptor(CWallet &keystore, const std::string &desc_str, const bool success)
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination corresponds to one with an address.
FUZZ_TARGET(coin_grinder)
std::map< COutPoint, Coin > ConsumeCoins(FuzzedDataProvider &fuzzed_data_provider) noexcept
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh) noexcept
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
std::span< const uint8_t > FuzzBufferType
Chainstate stores and provides an API to update our local knowledge of the current best chain...
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
std::string ConsumeRandomLengthString(size_t max_length)
Indicate that this wallet supports DescriptorScriptPubKeyMan.
void Init()
When initializing the target, populate the list of keys.
is a home for public enum and struct type definitions that are used by internally by wallet code...
std::optional< std::string > GetDescriptor(std::string_view mocked_desc) const
Get an actual descriptor string from a descriptor string whose keys were mocked.
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Converts a mocked descriptor string to a valid one.
Serialized script, used inside transaction inputs and outputs.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
A mutable version of CTransaction.
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
MockedDescriptorConverter MOCKED_DESC_CONVERTER
The converter of mocked descriptors, needs to be initialized when the target is.
uint160 ConsumeUInt160(FuzzedDataProvider &fuzzed_data_provider) noexcept
T ConsumeIntegralInRange(T min, T max)
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...
Testing setup that configures a complete environment.
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
static constexpr TransactionSerParams TX_WITH_WITNESS
PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction &psbt)
Compute a PrecomputedTransactionData object from a psbt.