fuzz coverage

Coverage Report

Created: 2025-06-01 19:34

/Users/eugenesiegel/btc/bitcoin/src/wallet/scriptpubkeyman.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2019-2022 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_WALLET_SCRIPTPUBKEYMAN_H
6
#define BITCOIN_WALLET_SCRIPTPUBKEYMAN_H
7
8
#include <addresstype.h>
9
#include <common/messages.h>
10
#include <common/signmessage.h>
11
#include <common/types.h>
12
#include <logging.h>
13
#include <node/types.h>
14
#include <psbt.h>
15
#include <script/descriptor.h>
16
#include <script/script.h>
17
#include <script/signingprovider.h>
18
#include <util/result.h>
19
#include <util/time.h>
20
#include <wallet/crypter.h>
21
#include <wallet/types.h>
22
#include <wallet/walletdb.h>
23
#include <wallet/walletutil.h>
24
25
#include <boost/signals2/signal.hpp>
26
27
#include <functional>
28
#include <optional>
29
#include <unordered_map>
30
31
enum class OutputType;
32
33
namespace wallet {
34
struct MigrationData;
35
class ScriptPubKeyMan;
36
37
// Wallet storage things that ScriptPubKeyMans need in order to be able to store things to the wallet database.
38
// It provides access to things that are part of the entire wallet and not specific to a ScriptPubKeyMan such as
39
// wallet flags, wallet version, encryption keys, encryption status, and the database itself. This allows a
40
// ScriptPubKeyMan to have callbacks into CWallet without causing a circular dependency.
41
// WalletStorage should be the same for all ScriptPubKeyMans of a wallet.
42
class WalletStorage
43
{
44
public:
45
0
    virtual ~WalletStorage() = default;
46
    virtual std::string GetDisplayName() const = 0;
47
    virtual WalletDatabase& GetDatabase() const = 0;
48
    virtual bool IsWalletFlagSet(uint64_t) const = 0;
49
    virtual void UnsetBlankWalletFlag(WalletBatch&) = 0;
50
    virtual bool CanSupportFeature(enum WalletFeature) const = 0;
51
    virtual void SetMinVersion(enum WalletFeature, WalletBatch* = nullptr) = 0;
52
    //! Pass the encryption key to cb().
53
    virtual bool WithEncryptionKey(std::function<bool (const CKeyingMaterial&)> cb) const = 0;
54
    virtual bool HasEncryptionKeys() const = 0;
55
    virtual bool IsLocked() const = 0;
56
    //! Callback function for after TopUp completes containing any scripts that were added by a SPKMan
57
    virtual void TopUpCallback(const std::set<CScript>&, ScriptPubKeyMan*) = 0;
58
};
59
60
//! Constant representing an unknown spkm creation time
61
static constexpr int64_t UNKNOWN_TIME = std::numeric_limits<int64_t>::max();
62
63
//! Default for -keypool
64
static const unsigned int DEFAULT_KEYPOOL_SIZE = 1000;
65
66
std::vector<CKeyID> GetAffectedKeys(const CScript& spk, const SigningProvider& provider);
67
68
struct WalletDestination
69
{
70
    CTxDestination dest;
71
    std::optional<bool> internal;
72
};
73
74
/*
75
 * A class implementing ScriptPubKeyMan manages some (or all) scriptPubKeys used in a wallet.
76
 * It contains the scripts and keys related to the scriptPubKeys it manages.
77
 * A ScriptPubKeyMan will be able to give out scriptPubKeys to be used, as well as marking
78
 * when a scriptPubKey has been used. It also handles when and how to store a scriptPubKey
79
 * and its related scripts and keys, including encryption.
80
 */
81
class ScriptPubKeyMan
82
{
83
protected:
84
    WalletStorage& m_storage;
85
86
public:
87
0
    explicit ScriptPubKeyMan(WalletStorage& storage) : m_storage(storage) {}
88
0
    virtual ~ScriptPubKeyMan() = default;
89
0
    virtual util::Result<CTxDestination> GetNewDestination(const OutputType type) { return util::Error{Untranslated("Not supported")}; }
90
0
    virtual isminetype IsMine(const CScript& script) const { return ISMINE_NO; }
91
92
    //! Check that the given decryption key is valid for this ScriptPubKeyMan, i.e. it decrypts all of the keys handled by it.
93
0
    virtual bool CheckDecryptionKey(const CKeyingMaterial& master_key) { return false; }
94
0
    virtual bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) { return false; }
95
96
0
    virtual util::Result<CTxDestination> GetReservedDestination(const OutputType type, bool internal, int64_t& index) { return util::Error{Untranslated("Not supported")}; }
97
0
    virtual void KeepDestination(int64_t index, const OutputType& type) {}
98
0
    virtual void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) {}
99
100
    /** Fills internal address pool. Use within ScriptPubKeyMan implementations should be used sparingly and only
101
      * when something from the address pool is removed, excluding GetNewDestination and GetReservedDestination.
102
      * External wallet code is primarily responsible for topping up prior to fetching new addresses
103
      */
104
0
    virtual bool TopUp(unsigned int size = 0) { return false; }
105
106
    /** Mark unused addresses as being used
107
     * Affects all keys up to and including the one determined by provided script.
108
     *
109
     * @param script determines the last key to mark as used
110
     *
111
     * @return All of the addresses affected
112
     */
113
0
    virtual std::vector<WalletDestination> MarkUnusedAddresses(const CScript& script) { return {}; }
114
115
    /** Sets up the key generation stuff, i.e. generates new HD seeds and sets them as active.
116
      * Returns false if already setup or setup fails, true if setup is successful
117
      * Set force=true to make it re-setup if already setup, used for upgrades
118
      */
119
0
    virtual bool SetupGeneration(bool force = false) { return false; }
120
121
    /* Returns true if HD is enabled */
122
0
    virtual bool IsHDEnabled() const { return false; }
123
124
    /* Returns true if the wallet can give out new addresses. This means it has keys in the keypool or can generate new keys */
125
0
    virtual bool CanGetAddresses(bool internal = false) const { return false; }
126
127
    /** Upgrades the wallet to the specified version */
128
0
    virtual bool Upgrade(int prev_version, int new_version, bilingual_str& error) { return true; }
129
130
0
    virtual bool HavePrivateKeys() const { return false; }
131
0
    virtual bool HaveCryptedKeys() const { return false; }
132
133
    //! The action to do when the DB needs rewrite
134
0
    virtual void RewriteDB() {}
135
136
0
    virtual std::optional<int64_t> GetOldestKeyPoolTime() const { return GetTime(); }
137
138
0
    virtual unsigned int GetKeyPoolSize() const { return 0; }
139
140
0
    virtual int64_t GetTimeFirstKey() const { return 0; }
141
142
0
    virtual std::unique_ptr<CKeyMetadata> GetMetadata(const CTxDestination& dest) const { return nullptr; }
143
144
0
    virtual std::unique_ptr<SigningProvider> GetSolvingProvider(const CScript& script) const { return nullptr; }
145
146
    /** Whether this ScriptPubKeyMan can provide a SigningProvider (via GetSolvingProvider) that, combined with
147
      * sigdata, can produce solving data.
148
      */
149
0
    virtual bool CanProvide(const CScript& script, SignatureData& sigdata) { return false; }
150
151
    /** Creates new signatures and adds them to the transaction. Returns whether all inputs were signed */
152
0
    virtual bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors) const { return false; }
153
    /** Sign a message with the given script */
154
0
    virtual SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const { return SigningResult::SIGNING_FAILED; };
155
    /** Adds script and derivation path information to a PSBT, and optionally signs it. */
156
0
    virtual std::optional<common::PSBTError> FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type = SIGHASH_DEFAULT, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr, bool finalize = true) const { return common::PSBTError::UNSUPPORTED; }
157
158
0
    virtual uint256 GetID() const { return uint256(); }
159
160
    /** Returns a set of all the scriptPubKeys that this ScriptPubKeyMan watches */
161
0
    virtual std::unordered_set<CScript, SaltedSipHasher> GetScriptPubKeys() const { return {}; };
162
163
    /** Prepends the wallet name in logging output to ease debugging in multi-wallet use cases */
164
    template <typename... Params>
165
    void WalletLogPrintf(util::ConstevalFormatString<sizeof...(Params)> wallet_fmt, const Params&... params) const
166
0
    {
167
0
        LogInfo("%s %s", m_storage.GetDisplayName(), tfm::format(wallet_fmt, params...));
Line
Count
Source
261
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
        LogInfo("%s %s", m_storage.GetDisplayName(), tfm::format(wallet_fmt, params...));
Line
Count
Source
261
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
        LogInfo("%s %s", m_storage.GetDisplayName(), tfm::format(wallet_fmt, params...));
Line
Count
Source
261
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
168
0
    };
Unexecuted instantiation: _ZNK6wallet15ScriptPubKeyMan15WalletLogPrintfIJA12_cjjNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEEEEvN4util21ConstevalFormatStringIXsZT_EEEDpRKT_
Unexecuted instantiation: _ZNK6wallet15ScriptPubKeyMan15WalletLogPrintfIJA20_ciEEEvN4util21ConstevalFormatStringIXsZT_EEEDpRKT_
Unexecuted instantiation: _ZNK6wallet15ScriptPubKeyMan15WalletLogPrintfIJA20_cEEEvN4util21ConstevalFormatStringIXsZT_EEEDpRKT_
169
170
    /** Watch-only address added */
171
    boost::signals2::signal<void (bool fHaveWatchOnly)> NotifyWatchonlyChanged;
172
173
    /** Keypool has new keys */
174
    boost::signals2::signal<void ()> NotifyCanGetAddressesChanged;
175
176
    /** Birth time changed */
177
    boost::signals2::signal<void (const ScriptPubKeyMan* spkm, int64_t new_birth_time)> NotifyFirstKeyTimeChanged;
178
};
179
180
/** OutputTypes supported by the LegacyScriptPubKeyMan */
181
static const std::unordered_set<OutputType> LEGACY_OUTPUT_TYPES {
182
    OutputType::LEGACY,
183
    OutputType::P2SH_SEGWIT,
184
    OutputType::BECH32,
185
};
186
187
// Manages the data for a LegacyScriptPubKeyMan.
188
// This is the minimum necessary to load a legacy wallet so that it can be migrated.
189
class LegacyDataSPKM : public ScriptPubKeyMan, public FillableSigningProvider
190
{
191
private:
192
    using WatchOnlySet = std::set<CScript>;
193
    using WatchKeyMap = std::map<CKeyID, CPubKey>;
194
    using CryptedKeyMap = std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char>>>;
195
196
    CryptedKeyMap mapCryptedKeys GUARDED_BY(cs_KeyStore);
197
    WatchOnlySet setWatchOnly GUARDED_BY(cs_KeyStore);
198
    WatchKeyMap mapWatchKeys GUARDED_BY(cs_KeyStore);
199
200
    /* the HD chain data model (external chain counters) */
201
    CHDChain m_hd_chain;
202
    std::unordered_map<CKeyID, CHDChain, SaltedSipHasher> m_inactive_hd_chains;
203
204
    //! keeps track of whether Unlock has run a thorough check before
205
    bool fDecryptionThoroughlyChecked = true;
206
207
    bool AddWatchOnlyInMem(const CScript &dest);
208
    virtual bool AddKeyPubKeyInner(const CKey& key, const CPubKey &pubkey);
209
    bool AddCryptedKeyInner(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
210
211
    // Helper function to retrieve a conservative superset of all output scripts that may be relevant to this LegacyDataSPKM.
212
    // It may include scripts that are invalid or not actually watched by this LegacyDataSPKM.
213
    // Used only in migration.
214
    std::unordered_set<CScript, SaltedSipHasher> GetCandidateScriptPubKeys() const;
215
216
    isminetype IsMine(const CScript& script) const override;
217
    bool CanProvide(const CScript& script, SignatureData& sigdata) override;
218
public:
219
    using ScriptPubKeyMan::ScriptPubKeyMan;
220
221
    // Map from Key ID to key metadata.
222
    std::map<CKeyID, CKeyMetadata> mapKeyMetadata GUARDED_BY(cs_KeyStore);
223
224
    // Map from Script ID to key metadata (for watch-only keys).
225
    std::map<CScriptID, CKeyMetadata> m_script_metadata GUARDED_BY(cs_KeyStore);
226
227
    // ScriptPubKeyMan overrides
228
    bool CheckDecryptionKey(const CKeyingMaterial& master_key) override;
229
    std::unordered_set<CScript, SaltedSipHasher> GetScriptPubKeys() const override;
230
    std::unique_ptr<SigningProvider> GetSolvingProvider(const CScript& script) const override;
231
0
    uint256 GetID() const override { return uint256::ONE; }
232
233
    // FillableSigningProvider overrides
234
    bool HaveKey(const CKeyID &address) const override;
235
    bool GetKey(const CKeyID &address, CKey& keyOut) const override;
236
    bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
237
    bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
238
239
    //! Load metadata (used by LoadWallet)
240
    virtual void LoadKeyMetadata(const CKeyID& keyID, const CKeyMetadata &metadata);
241
    virtual void LoadScriptMetadata(const CScriptID& script_id, const CKeyMetadata &metadata);
242
243
    //! Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
244
    bool LoadWatchOnly(const CScript &dest);
245
    //! Returns whether the watch-only script is in the wallet
246
    bool HaveWatchOnly(const CScript &dest) const;
247
    //! Returns whether there are any watch-only things in the wallet
248
    bool HaveWatchOnly() const;
249
    //! Adds a key to the store, without saving it to disk (used by LoadWallet)
250
    bool LoadKey(const CKey& key, const CPubKey &pubkey);
251
    //! Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
252
    bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret, bool checksum_valid);
253
    //! Adds a CScript to the store
254
    bool LoadCScript(const CScript& redeemScript);
255
    //! Load a HD chain model (used by LoadWallet)
256
    void LoadHDChain(const CHDChain& chain);
257
    void AddInactiveHDChain(const CHDChain& chain);
258
0
    const CHDChain& GetHDChain() const { return m_hd_chain; }
259
260
    //! Fetches a pubkey from mapWatchKeys if it exists there
261
    bool GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const;
262
263
    /**
264
     * Retrieves scripts that were imported by bugs into the legacy spkm and are
265
     * simply invalid, such as a sh(sh(pkh())) script, or not watched.
266
     */
267
    std::unordered_set<CScript, SaltedSipHasher> GetNotMineScriptPubKeys() const;
268
269
    /** Get the DescriptorScriptPubKeyMans (with private keys) that have the same scriptPubKeys as this LegacyScriptPubKeyMan.
270
     * Does not modify this ScriptPubKeyMan. */
271
    std::optional<MigrationData> MigrateToDescriptor();
272
    /** Delete all the records of this LegacyScriptPubKeyMan from disk*/
273
    bool DeleteRecords();
274
    bool DeleteRecordsWithDB(WalletBatch& batch);
275
};
276
277
/** Wraps a LegacyScriptPubKeyMan so that it can be returned in a new unique_ptr. Does not provide privkeys */
278
class LegacySigningProvider : public SigningProvider
279
{
280
private:
281
    const LegacyDataSPKM& m_spk_man;
282
public:
283
0
    explicit LegacySigningProvider(const LegacyDataSPKM& spk_man) : m_spk_man(spk_man) {}
284
285
0
    bool GetCScript(const CScriptID &scriptid, CScript& script) const override { return m_spk_man.GetCScript(scriptid, script); }
286
0
    bool HaveCScript(const CScriptID &scriptid) const override { return m_spk_man.HaveCScript(scriptid); }
287
0
    bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const override { return m_spk_man.GetPubKey(address, pubkey); }
288
0
    bool GetKey(const CKeyID &address, CKey& key) const override { return false; }
289
0
    bool HaveKey(const CKeyID &address) const override { return false; }
290
0
    bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override { return m_spk_man.GetKeyOrigin(keyid, info); }
291
};
292
293
class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
294
{
295
    friend class LegacyDataSPKM;
296
private:
297
    using ScriptPubKeyMap = std::map<CScript, int32_t>; // Map of scripts to descriptor range index
298
    using PubKeyMap = std::map<CPubKey, int32_t>; // Map of pubkeys involved in scripts to descriptor range index
299
    using CryptedKeyMap = std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char>>>;
300
    using KeyMap = std::map<CKeyID, CKey>;
301
302
    ScriptPubKeyMap m_map_script_pub_keys GUARDED_BY(cs_desc_man);
303
    PubKeyMap m_map_pubkeys GUARDED_BY(cs_desc_man);
304
    int32_t m_max_cached_index = -1;
305
306
    KeyMap m_map_keys GUARDED_BY(cs_desc_man);
307
    CryptedKeyMap m_map_crypted_keys GUARDED_BY(cs_desc_man);
308
309
    //! keeps track of whether Unlock has run a thorough check before
310
    bool m_decryption_thoroughly_checked = false;
311
312
    //! Number of pre-generated keys/scripts (part of the look-ahead process, used to detect payments)
313
    int64_t m_keypool_size GUARDED_BY(cs_desc_man){DEFAULT_KEYPOOL_SIZE};
314
315
    bool AddDescriptorKeyWithDB(WalletBatch& batch, const CKey& key, const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
316
317
    KeyMap GetKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
318
319
    // Cached FlatSigningProviders to avoid regenerating them each time they are needed.
320
    mutable std::map<int32_t, FlatSigningProvider> m_map_signing_providers;
321
    // Fetch the SigningProvider for the given script and optionally include private keys
322
    std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CScript& script, bool include_private = false) const;
323
    // Fetch the SigningProvider for a given index and optionally include private keys. Called by the above functions.
324
    std::unique_ptr<FlatSigningProvider> GetSigningProvider(int32_t index, bool include_private = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
325
326
protected:
327
    WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man);
328
329
    //! Same as 'TopUp' but designed for use within a batch transaction context
330
    bool TopUpWithDB(WalletBatch& batch, unsigned int size = 0);
331
332
public:
333
    DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size)
334
0
        :   ScriptPubKeyMan(storage),
335
0
            m_keypool_size(keypool_size),
336
0
            m_wallet_descriptor(descriptor)
337
0
        {}
338
    DescriptorScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size)
339
0
        :   ScriptPubKeyMan(storage),
340
0
            m_keypool_size(keypool_size)
341
0
        {}
342
343
    mutable RecursiveMutex cs_desc_man;
344
345
    util::Result<CTxDestination> GetNewDestination(const OutputType type) override;
346
    isminetype IsMine(const CScript& script) const override;
347
348
    bool CheckDecryptionKey(const CKeyingMaterial& master_key) override;
349
    bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) override;
350
351
    util::Result<CTxDestination> GetReservedDestination(const OutputType type, bool internal, int64_t& index) override;
352
    void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) override;
353
354
    // Tops up the descriptor cache and m_map_script_pub_keys. The cache is stored in the wallet file
355
    // and is used to expand the descriptor in GetNewDestination. DescriptorScriptPubKeyMan relies
356
    // more on ephemeral data than LegacyScriptPubKeyMan. For wallets using unhardened derivation
357
    // (with or without private keys), the "keypool" is a single xpub.
358
    bool TopUp(unsigned int size = 0) override;
359
360
    std::vector<WalletDestination> MarkUnusedAddresses(const CScript& script) override;
361
362
    bool IsHDEnabled() const override;
363
364
    //! Setup descriptors based on the given CExtkey
365
    bool SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal);
366
367
    bool HavePrivateKeys() const override;
368
    bool HasPrivKey(const CKeyID& keyid) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
369
    //! Retrieve the particular key if it is available. Returns nullopt if the key is not in the wallet, or if the wallet is locked.
370
    std::optional<CKey> GetKey(const CKeyID& keyid) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
371
    bool HaveCryptedKeys() const override;
372
373
    std::optional<int64_t> GetOldestKeyPoolTime() const override;
374
    unsigned int GetKeyPoolSize() const override;
375
376
    int64_t GetTimeFirstKey() const override;
377
378
    std::unique_ptr<CKeyMetadata> GetMetadata(const CTxDestination& dest) const override;
379
380
    bool CanGetAddresses(bool internal = false) const override;
381
382
    std::unique_ptr<SigningProvider> GetSolvingProvider(const CScript& script) const override;
383
384
    bool CanProvide(const CScript& script, SignatureData& sigdata) override;
385
386
    // Fetch the SigningProvider for the given pubkey and always include private keys. This should only be called by signing code.
387
    std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CPubKey& pubkey) const;
388
389
    bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors) const override;
390
    SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const override;
391
    std::optional<common::PSBTError> FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type = SIGHASH_DEFAULT, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr, bool finalize = true) const override;
392
393
    uint256 GetID() const override;
394
395
    void SetCache(const DescriptorCache& cache);
396
397
    bool AddKey(const CKeyID& key_id, const CKey& key);
398
    bool AddCryptedKey(const CKeyID& key_id, const CPubKey& pubkey, const std::vector<unsigned char>& crypted_key);
399
400
    bool HasWalletDescriptor(const WalletDescriptor& desc) const;
401
    util::Result<void> UpdateWalletDescriptor(WalletDescriptor& descriptor);
402
    bool CanUpdateToWalletDescriptor(const WalletDescriptor& descriptor, std::string& error);
403
    void AddDescriptorKey(const CKey& key, const CPubKey &pubkey);
404
    void WriteDescriptor();
405
406
    WalletDescriptor GetWalletDescriptor() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
407
    std::unordered_set<CScript, SaltedSipHasher> GetScriptPubKeys() const override;
408
    std::unordered_set<CScript, SaltedSipHasher> GetScriptPubKeys(int32_t minimum_index) const;
409
    int32_t GetEndRange() const;
410
411
    [[nodiscard]] bool GetDescriptorString(std::string& out, const bool priv) const;
412
413
    void UpgradeDescriptorCache();
414
};
415
416
/** struct containing information needed for migrating legacy wallets to descriptor wallets */
417
struct MigrationData
418
{
419
    CExtKey master_key;
420
    std::vector<std::pair<std::string, int64_t>> watch_descs;
421
    std::vector<std::pair<std::string, int64_t>> solvable_descs;
422
    std::vector<std::unique_ptr<DescriptorScriptPubKeyMan>> desc_spkms;
423
    std::shared_ptr<CWallet> watchonly_wallet{nullptr};
424
    std::shared_ptr<CWallet> solvable_wallet{nullptr};
425
};
426
427
} // namespace wallet
428
429
#endif // BITCOIN_WALLET_SCRIPTPUBKEYMAN_H