6#include <chainparams.h>
26#include <validation.h>
50 static const auto testing_setup{MakeNoLogFileContext<const TestingSetup>()};
55void initialize_spkm_migration()
57 static const auto testing_setup{MakeNoLogFileContext<const TestingSetup>()};
65 if (!desc_str.has_value())
return std::nullopt;
70 std::vector<std::unique_ptr<Descriptor>> parsed_descs =
Parse(desc_str.value(), keys, error,
false);
71 if (parsed_descs.empty())
return std::nullopt;
73 WalletDescriptor w_desc{std::move(parsed_descs.at(0)), 0, 0, 1, 1};
74 return std::make_pair(w_desc, keys);
79 LOCK(keystore.cs_wallet);
80 auto spk_manager_res = keystore.AddWalletDescriptor(wallet_desc, keys,
"",
false);
81 if (!spk_manager_res)
return nullptr;
82 return &spk_manager_res.value().get();
93 CWallet&
wallet{*wallet_ptr};
97 wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash());
102 if (!wallet_desc.has_value())
return;
104 if (spk_manager ==
nullptr)
return;
108 if (!wallet_desc.has_value()) {
112 if (spk_manager->CanUpdateToWalletDescriptor(wallet_desc->first, error)) {
114 if (new_spk_manager !=
nullptr) spk_manager = new_spk_manager;
118 bool good_data{
true};
124 if (spk_manager->IsMine(
script)) {
125 assert(spk_manager->GetScriptPubKeys().contains(
script));
129 auto spks{spk_manager->GetScriptPubKeys()};
130 for (
const CScript& spk : spks) {
131 assert(spk_manager->IsMine(spk));
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()};
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());
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 if (sighash_type == 151) sighash_type = std::nullopt;
193 (void)spk_manager->FillPSBT(psbt, txdata, sighash_type,
sign, bip32derivs,
nullptr, finalize);
198 std::string descriptor;
200 (void)spk_manager->GetEndRange();
201 (void)spk_manager->GetKeyPoolSize();
213 CWallet&
wallet{*wallet_ptr};
214 wallet.m_keypool_size = 1;
218 wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash());
221 auto& legacy_data{*
wallet.GetOrCreateLegacyDataSPKM()};
223 std::vector<CKey> keys;
226 if (!key.IsValid())
return;
228 if (!pub_key.IsFullyValid())
return;
229 if (legacy_data.LoadKey(key, pub_key) && std::find(keys.begin(), keys.end(), key) == keys.end()) keys.push_back(key);
238 hd_chain.nVersion = version;
240 legacy_data.LoadHDChain(hd_chain);
244 if (add_inactive_hd_chain) {
248 legacy_data.AddInactiveHDChain(hd_chain);
251 bool watch_only =
false;
253 if (!pub_key || !pub_key->IsFullyValid())
return;
258 if (legacy_data.LoadWatchOnly(script_dest)) watch_only =
true;
260 size_t added_script{0};
261 bool good_data{
true};
268 key = PickValue(fuzzed_data_provider, keys);
270 key = ConsumePrivateKey(fuzzed_data_provider, fuzzed_data_provider.ConsumeBool());
273 auto pub_key{key.GetPubKey()};
289 script = script_opt.value();
304 std::vector<CPubKey> pubkeys;
306 for (
size_t i = 1; i < num_keys; i++) {
315 if (pubkeys.size() < num_keys)
return;
317 if (!legacy_data.HaveCScript(
CScriptID(multisig_script)) && legacy_data.AddCScript(multisig_script)) {
324 auto result{legacy_data.MigrateToDescriptor()};
326 size_t added_chains{
static_cast<size_t>(add_hd_chain) +
static_cast<size_t>(add_inactive_hd_chain)};
330 size_t added_size{keys.size() + added_chains};
331 if (added_script > 0) {
332 assert(result->desc_spkms.size() >= added_size);
334 assert(result->desc_spkms.size() == added_size);
336 if (watch_only)
assert(!result->watch_descs.empty());
337 if (!result->solvable_descs.empty())
assert(added_script > 0);
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.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
const TestingSetup * g_setup
An encapsulated private key.
bool IsValid() const
Check whether this private key is valid.
CPubKey GetPubKey() const
Compute the public key from a private key.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Serialized script, used inside transaction inputs and outputs.
A reference to a CScript: the Hash160 of its serialization.
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.
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
static const int VERSION_HD_BASE
static const int VERSION_HD_CHAIN_SPLIT
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.
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.
static const int MAX_PUBKEYS_PER_MULTISIG
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span{v}))
Like the std::span constructor, but for (const) unsigned char member types only.
A mutable version of CTransaction.
Testing setup that configures a complete environment.
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
CKey ConsumePrivateKey(FuzzedDataProvider &fuzzed_data_provider, std::optional< bool > compressed) 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.
FuzzedDataProvider & fuzzed_data_provider
is a home for public enum and struct type definitions that are used by internally by wallet code,...