6#include <chainparams.h>
24#include <validation.h>
47 static const auto testing_setup{MakeNoLogFileContext<const TestingSetup>()};
48 g_setup = testing_setup.get();
58static bool TooDeepDerivPath(std::string_view desc)
60 const FuzzBufferType desc_buf{
reinterpret_cast<const unsigned char *
>(desc.data()), desc.size()};
64static std::optional<std::pair<WalletDescriptor, FlatSigningProvider>> CreateWalletDescriptor(
FuzzedDataProvider& fuzzed_data_provider)
67 if (TooDeepDerivPath(mocked_descriptor))
return {};
69 if (!desc_str.has_value())
return std::nullopt;
73 std::vector<std::unique_ptr<Descriptor>> parsed_descs =
Parse(desc_str.value(), keys, error,
false);
74 if (parsed_descs.empty())
return std::nullopt;
76 WalletDescriptor w_desc{std::move(parsed_descs.at(0)), 0, 0, 1, 1};
77 return std::make_pair(w_desc, keys);
82 LOCK(keystore.cs_wallet);
83 auto spk_manager_res = keystore.AddWalletDescriptor(wallet_desc, keys,
"",
false);
84 if (!spk_manager_res)
return nullptr;
85 return &spk_manager_res.value().get();
96 CWallet&
wallet{*wallet_ptr};
100 wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash());
101 wallet.m_keypool_size = 1;
104 auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
105 if (!wallet_desc.has_value())
return;
107 if (spk_manager ==
nullptr)
return;
110 auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
111 if (!wallet_desc.has_value()) {
115 if (spk_manager->CanUpdateToWalletDescriptor(wallet_desc->first, error)) {
117 if (new_spk_manager !=
nullptr) spk_manager = new_spk_manager;
121 bool good_data{
true};
124 fuzzed_data_provider,
127 auto is_mine{spk_manager->IsMine(
script)};
133 auto spks{spk_manager->GetScriptPubKeys()};
134 for (
const CScript& spk : spks) {
140 PKHash pk_hash{std::get_if<PKHash>(&dest) && fuzzed_data_provider.
ConsumeBool() ?
141 *std::get_if<PKHash>(&dest) :
144 (void)spk_manager->SignMessage(
msg, pk_hash, str_sig);
145 (void)spk_manager->GetMetadata(dest);
150 auto spks{spk_manager->GetScriptPubKeys()};
152 auto& spk{
PickValue(fuzzed_data_provider, spks)};
153 (void)spk_manager->MarkUnusedAddresses(spk);
157 LOCK(spk_manager->cs_desc_man);
158 auto wallet_desc{spk_manager->GetWalletDescriptor()};
159 if (wallet_desc.descriptor->IsSingleType()) {
160 auto output_type{wallet_desc.descriptor->GetOutputType()};
161 if (output_type.has_value()) {
162 auto dest{spk_manager->GetNewDestination(*output_type)};
165 assert(spk_manager->IsHDEnabled());
172 const std::optional<CMutableTransaction> opt_tx_to{ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider,
TX_WITH_WITNESS)};
179 std::map<COutPoint, Coin> coins{
ConsumeCoins(fuzzed_data_provider)};
181 std::map<int, bilingual_str> input_errors;
182 (void)spk_manager->SignTransaction(tx_to, coins, sighash, input_errors);
185 std::optional<PartiallySignedTransaction> opt_psbt{ConsumeDeserializable<PartiallySignedTransaction>(fuzzed_data_provider)};
190 auto psbt{*opt_psbt};
193 if (sighash_type == 151) sighash_type = std::nullopt;
195 auto bip32derivs = fuzzed_data_provider.
ConsumeBool();
196 auto finalize = fuzzed_data_provider.
ConsumeBool();
197 (void)spk_manager->FillPSBT(psbt, txdata, sighash_type,
sign, bip32derivs,
nullptr, finalize);
202 std::string descriptor;
203 (void)spk_manager->GetDescriptorString(descriptor, fuzzed_data_provider.
ConsumeBool());
204 (void)spk_manager->GetEndRange();
205 (void)spk_manager->GetKeyPoolSize();
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
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.
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
Serialized script, used inside transaction inputs and outputs.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
std::string ConsumeRandomLengthString(size_t max_length)
T ConsumeIntegralInRange(T min, T max)
Converts a mocked descriptor string to a valid one.
void Init()
When initializing the target, populate the list of keys.
std::optional< std::string > GetDescriptor(std::string_view mocked_desc) const
Get an actual descriptor string from a descriptor string whose keys were mocked.
static UniValue Parse(std::string_view raw)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
MockedDescriptorConverter MOCKED_DESC_CONVERTER
The converter of mocked descriptors, needs to be initialized when the target is.
std::span< const uint8_t > FuzzBufferType
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
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)
wallet::DescriptorScriptPubKeyMan * CreateDescriptor(CWallet &keystore, const std::string &desc_str, const bool success)
FUZZ_TARGET(coin_grinder)
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
static constexpr TransactionSerParams TX_WITH_WITNESS
PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction &psbt)
Compute a PrecomputedTransactionData object from a psbt.
A mutable version of CTransaction.
Testing setup that configures a complete environment.
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...
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh) noexcept
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
std::map< COutPoint, Coin > ConsumeCoins(FuzzedDataProvider &fuzzed_data_provider) noexcept
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
uint160 ConsumeUInt160(FuzzedDataProvider &fuzzed_data_provider) noexcept
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
@ ZEROS
Seed with a compile time constant of zeros.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
is a home for public enum and struct type definitions that are used by internally by wallet code,...