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