6#include <chainparams.h>
24#include <validation.h>
53static std::optional<std::pair<WalletDescriptor, FlatSigningProvider>> CreateWalletDescriptor(FuzzedDataProvider& fuzzed_data_provider)
57 if (!desc_str.has_value())
return std::nullopt;
60 FlatSigningProvider keys;
62 std::vector<std::unique_ptr<Descriptor>> parsed_descs =
Parse(desc_str.value(), keys, error,
false);
63 if (parsed_descs.empty())
return std::nullopt;
66 return std::make_pair(w_desc, keys);
73 if (!spk_manager_res)
return nullptr;
74 return &spk_manager_res.value().get();
77FUZZ_TARGET(scriptpubkeyman, .init = initialize_spkm)
80 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
83 Chainstate& chainstate{node.chainman->ActiveChainstate()};
93 auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
94 if (!wallet_desc.has_value())
return;
95 auto spk_manager{
CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
96 if (spk_manager ==
nullptr)
return;
99 auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
100 if (!wallet_desc.has_value()) {
104 if (spk_manager->CanUpdateToWalletDescriptor(wallet_desc->first, error)) {
105 auto new_spk_manager{
CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
106 if (new_spk_manager !=
nullptr) spk_manager = new_spk_manager;
110 bool good_data{
true};
113 fuzzed_data_provider,
116 if (spk_manager->IsMine(script)) {
117 assert(spk_manager->GetScriptPubKeys().contains(script));
121 auto spks{spk_manager->GetScriptPubKeys()};
122 for (
const CScript& spk : spks) {
123 assert(spk_manager->IsMine(spk));
128 PKHash pk_hash{std::get_if<PKHash>(&dest) && fuzzed_data_provider.
ConsumeBool() ?
129 *std::get_if<PKHash>(&dest) :
132 (void)spk_manager->SignMessage(msg, pk_hash, str_sig);
133 (void)spk_manager->GetMetadata(dest);
138 auto spks{spk_manager->GetScriptPubKeys()};
140 auto& spk{
PickValue(fuzzed_data_provider, spks)};
141 (void)spk_manager->MarkUnusedAddresses(spk);
145 LOCK(spk_manager->cs_desc_man);
146 auto wallet_desc{spk_manager->GetWalletDescriptor()};
148 auto output_type{wallet_desc.
descriptor->GetOutputType()};
149 if (output_type.has_value()) {
150 auto dest{spk_manager->GetNewDestination(*output_type)};
153 assert(spk_manager->IsHDEnabled());
159 CMutableTransaction tx_to;
167 std::map<COutPoint, Coin> coins{
ConsumeCoins(fuzzed_data_provider)};
169 std::map<int, bilingual_str> input_errors;
170 (void)spk_manager->SignTransaction(tx_to, coins, sighash, input_errors);
178 auto psbt{*opt_psbt};
181 if (sighash_type == 151) sighash_type = std::nullopt;
183 auto bip32derivs = fuzzed_data_provider.
ConsumeBool();
184 auto finalize = fuzzed_data_provider.
ConsumeBool();
185 (void)spk_manager->FillPSBT(psbt, txdata, sighash_type,
sign, bip32derivs,
nullptr, finalize);
190 std::string descriptor;
191 (void)spk_manager->GetDescriptorString(descriptor, fuzzed_data_provider.
ConsumeBool());
192 (
void)spk_manager->GetEndRange();
193 (
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.
const TestingSetup * g_setup
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
uint256 GetBlockHash() const
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
int Height() const
Return the maximal height in the chain.
int64_t m_keypool_size
Number of pre-generated keys/scripts by each spkm (part of the look-ahead process,...
RecursiveMutex cs_wallet
Main wallet lock.
util::Result< std::reference_wrapper< DescriptorScriptPubKeyMan > > AddWalletDescriptor(WalletDescriptor &desc, const FlatSigningProvider &signing_provider, const std::string &label, bool internal) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Add a descriptor to the wallet, return a ScriptPubKeyMan & associated output type.
std::string ConsumeRandomLengthString(size_t max_length)
T ConsumeIntegralInRange(T min, T max)
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.
std::shared_ptr< Descriptor > descriptor
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Descriptor with some wallet metadata.
static UniValue Parse(std::string_view raw, ParamFormat format=ParamFormat::JSON)
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.
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
void BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(void SetWalletFlag(uint64_t flags)
Blocks until the wallet state is up-to-date to /at least/ the current chain at the time this function...
void SetLastBlockProcessed(int block_height, uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Set last block processed height, and write to database.
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)
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.
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span{v}))
Like the std::span constructor, but for (const) unsigned char member types only.
bool IsTooExpensive(std::span< const uint8_t > buffer)
Deriving "expensive" descriptors will consume useful fuzz compute. The compute is better spent on a s...
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh) 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)
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const P ¶ms, const std::optional< size_t > &max_length=std::nullopt) noexcept
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.