Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2024-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_TEST_FUZZ_UTIL_WALLET_H6#define BITCOIN_TEST_FUZZ_UTIL_WALLET_H8#include <test/fuzz/FuzzedDataProvider.h>9#include <test/fuzz/fuzz.h>10#include <test/fuzz/util.h>11#include <policy/policy.h>12#include <wallet/coincontrol.h>13#include <wallet/fees.h>14#include <wallet/spend.h>15#include <wallet/test/util.h>16#include <wallet/wallet.h>18namespace wallet {20/**21* Wraps a descriptor wallet for fuzzing.22*/23struct FuzzedWallet {24std::shared_ptr<CWallet> wallet;25FuzzedWallet(interfaces::Chain& chain, const std::string& name, const std::string& seed_insecure)26{27wallet = std::make_shared<CWallet>(&chain, name, CreateMockableWalletDatabase());28{29LOCK(wallet->cs_wallet);30wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);31auto height{*Assert(chain.getHeight())};32wallet->SetLastBlockProcessed(height, chain.getBlockHash(height));33}34wallet->m_keypool_size = 1; // Avoid timeout in TopUp()35assert(wallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));36ImportDescriptors(seed_insecure);37}38void ImportDescriptors(const std::string& seed_insecure)39{40const std::vector<std::string> DESCS{41"pkh(%s/%s/*)",42"sh(wpkh(%s/%s/*))",43"tr(%s/%s/*)",44"wpkh(%s/%s/*)",45};47for (const std::string& desc_fmt : DESCS) {48for (bool internal : {true, false}) {49const auto descriptor{strprintf(tfm::RuntimeFormat{desc_fmt}, "[5aa9973a/66h/4h/2h]" + seed_insecure, int{internal})};51FlatSigningProvider keys;52std::string error;53auto parsed_desc = std::move(Parse(descriptor, keys, error, /*require_checksum=*/false).at(0));54assert(parsed_desc);55assert(error.empty());56assert(parsed_desc->IsRange());57assert(parsed_desc->IsSingleType());58assert(!keys.keys.empty());59WalletDescriptor w_desc{std::move(parsed_desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/0};60assert(!wallet->GetDescriptorScriptPubKeyMan(w_desc));61LOCK(wallet->cs_wallet);62auto& spk_manager = Assert(wallet->AddWalletDescriptor(w_desc, keys, /*label=*/"", internal))->get();63wallet->AddActiveScriptPubKeyMan(spk_manager.GetID(), *Assert(w_desc.descriptor->GetOutputType()), internal);64}65}66}67CTxDestination GetDestination(FuzzedDataProvider& fuzzed_data_provider)68{69auto type{fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES)};70if (fuzzed_data_provider.ConsumeBool()) {71return *Assert(wallet->GetNewDestination(type, ""));72} else {73return *Assert(wallet->GetNewChangeDestination(type));74}75}76CScript GetScriptPubKey(FuzzedDataProvider& fuzzed_data_provider) { return GetScriptForDestination(GetDestination(fuzzed_data_provider)); }77};78}80#endif // BITCOIN_TEST_FUZZ_UTIL_WALLET_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.