Bitcoin Core  31.0.0
P2P Digital Currency
wallet.h
Go to the documentation of this file.
1 // Copyright (c) 2024-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 #ifndef BITCOIN_TEST_FUZZ_UTIL_WALLET_H
6 #define BITCOIN_TEST_FUZZ_UTIL_WALLET_H
7 
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>
17 
18 namespace wallet {
19 
23 struct FuzzedWallet {
24  std::shared_ptr<CWallet> wallet;
25  FuzzedWallet(interfaces::Chain& chain, const std::string& name, const std::string& seed_insecure)
26  {
27  wallet = std::make_shared<CWallet>(&chain, name, CreateMockableWalletDatabase());
28  {
29  LOCK(wallet->cs_wallet);
30  wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
31  auto height{*Assert(chain.getHeight())};
32  wallet->SetLastBlockProcessed(height, chain.getBlockHash(height));
33  }
34  wallet->m_keypool_size = 1; // Avoid timeout in TopUp()
35  assert(wallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
36  ImportDescriptors(seed_insecure);
37  }
38  void ImportDescriptors(const std::string& seed_insecure)
39  {
40  const std::vector<std::string> DESCS{
41  "pkh(%s/%s/*)",
42  "sh(wpkh(%s/%s/*))",
43  "tr(%s/%s/*)",
44  "wpkh(%s/%s/*)",
45  };
46 
47  for (const std::string& desc_fmt : DESCS) {
48  for (bool internal : {true, false}) {
49  const auto descriptor{strprintf(tfm::RuntimeFormat{desc_fmt}, "[5aa9973a/66h/4h/2h]" + seed_insecure, int{internal})};
50 
52  std::string error;
53  auto parsed_desc = std::move(Parse(descriptor, keys, error, /*require_checksum=*/false).at(0));
54  assert(parsed_desc);
55  assert(error.empty());
56  assert(parsed_desc->IsRange());
57  assert(parsed_desc->IsSingleType());
58  assert(!keys.keys.empty());
59  WalletDescriptor w_desc{std::move(parsed_desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/0};
60  assert(!wallet->GetDescriptorScriptPubKeyMan(w_desc));
61  LOCK(wallet->cs_wallet);
62  auto& spk_manager = Assert(wallet->AddWalletDescriptor(w_desc, keys, /*label=*/"", internal))->get();
63  wallet->AddActiveScriptPubKeyMan(spk_manager.GetID(), *Assert(w_desc.descriptor->GetOutputType()), internal);
64  }
65  }
66  }
68  {
71  return *Assert(wallet->GetNewDestination(type, ""));
72  } else {
73  return *Assert(wallet->GetNewChangeDestination(type));
74  }
75  }
77 };
78 }
79 
80 #endif // BITCOIN_TEST_FUZZ_UTIL_WALLET_H
static UniValue Parse(std::string_view raw, ParamFormat format=ParamFormat::JSON)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
Definition: client.cpp:395
Wraps a descriptor wallet for fuzzing.
Definition: wallet.h:23
FuzzedWallet(interfaces::Chain &chain, const std::string &name, const std::string &seed_insecure)
Definition: wallet.h:25
assert(!tx.IsCoinBase())
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
void ImportDescriptors(const std::string &seed_insecure)
Definition: wallet.h:38
std::map< CKeyID, CKey > keys
static constexpr auto OUTPUT_TYPES
Definition: outputtype.h:26
#define LOCK(cs)
Definition: sync.h:258
const char * name
Definition: rest.cpp:48
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:53
Descriptor with some wallet metadata.
Definition: walletutil.h:63
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:211
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:117
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:38
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
CScript GetScriptPubKey(FuzzedDataProvider &fuzzed_data_provider)
Definition: wallet.h:76
std::shared_ptr< CWallet > wallet
Definition: wallet.h:24
T PickValueInArray(const T(&array)[size])
CTxDestination GetDestination(FuzzedDataProvider &fuzzed_data_provider)
Definition: wallet.h:67
#define Assert(val)
Identity function.
Definition: check.h:113