Inside Bitcoin
Opening the selected file…
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.
251std::optional<MigrationData> MigrateToDescriptor();252/** Delete the legacy wallet records from disk. */253bool DeleteRecordsWithDB(WalletBatch& batch);254};256/** SigningProvider wrapper for LegacyDataSPKM that does not provide private keys. */257class LegacySigningProvider : public SigningProvider258{259private:260const LegacyDataSPKM& m_spk_man;261public:262explicit LegacySigningProvider(const LegacyDataSPKM& spk_man) : m_spk_man(spk_man) {}264bool GetCScript(const CScriptID &scriptid, CScript& script) const override { return m_spk_man.GetCScript(scriptid, script); }265bool HaveCScript(const CScriptID &scriptid) const override { return m_spk_man.HaveCScript(scriptid); }266bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const override { return m_spk_man.GetPubKey(address, pubkey); }267bool GetKey(const CKeyID &address, CKey& key) const override { return false; }268bool HaveKey(const CKeyID &address) const override { return false; }269bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override { return m_spk_man.GetKeyOrigin(keyid, info); }270};272class DescriptorScriptPubKeyMan : public ScriptPubKeyMan273{274private:275using ScriptPubKeyMap = std::map<CScript, int32_t>; // Map of scripts to descriptor range index276using PubKeyMap = std::map<CPubKey, int32_t>; // Map of pubkeys involved in scripts to descriptor range index278ScriptPubKeyMap m_map_script_pub_keys GUARDED_BY(cs_desc_man);279PubKeyMap m_map_pubkeys GUARDED_BY(cs_desc_man);280int32_t m_max_cached_index = -1;282KeyMap m_map_keys GUARDED_BY(cs_desc_man);283CryptedKeyMap m_map_crypted_keys GUARDED_BY(cs_desc_man);285//! keeps track of whether Unlock has run a thorough check before286bool m_decryption_thoroughly_checked = false;288//! Number of pre-generated keys/scripts (part of the look-ahead process, used to detect payments)289int64_t m_keypool_size GUARDED_BY(cs_desc_man){DEFAULT_KEYPOOL_SIZE};291/** Map of a session id to MuSig2 secnonce292*293* Stores MuSig2 secnonces while the MuSig2 signing session is still ongoing.294* Note that these secnonces must not be reused. In order to avoid being tricked into295* reusing a nonce, this map is held only in memory and must not be written to disk.296* The side effect is that signing sessions cannot persist across restarts, but this297* must be done in order to prevent nonce reuse.298*299* The session id is an arbitrary value set by the signer in order for the signing logic300* to find ongoing signing sessions, see MuSig2SessionID.301*/302mutable std::map<uint256, MuSig2SecNonce> m_musig2_secnonces;304const uint256 m_id;306bool AddDescriptorKeyWithDB(WalletBatch& batch, const CKey& key, const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);308KeyMap GetKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);310// Cached FlatSigningProviders to avoid regenerating them each time they are needed.311mutable std::map<int32_t, FlatSigningProvider> m_map_signing_providers;312// Fetch the SigningProvider for the given script and optionally include private keys313std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CScript& script, bool include_private = false) const;314// Fetch the SigningProvider for a given index and optionally include private keys. Called by the above functions.315std::unique_ptr<FlatSigningProvider> GetSigningProvider(int32_t index, bool include_private = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);317void Load();319void UpdateWithSigningProvider(WalletBatch& batch, const FlatSigningProvider& signing_provider) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);321protected:322//! Create a DescriptorScriptPubKeyMan from existing data (i.e. during loading)323DescriptorScriptPubKeyMan(WalletStorage& storage, const uint256& id, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys);325//! Create a new DescriptorScriptPubKeyMan from a descriptor (e.g. from an import, newly generated)326DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size)327: ScriptPubKeyMan(storage),328m_keypool_size(keypool_size),329m_id(CompatDescriptorHash(*descriptor.descriptor)),330m_wallet_descriptor(descriptor)331{}333WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man);334void IncIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);335void DecIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);336void SetRangeEnd(int32_t end) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);338//! Same as 'TopUp' but designed for use within a batch transaction context339bool TopUpWithDB(WalletBatch& batch, unsigned int size = 0);341public:342static std::unique_ptr<DescriptorScriptPubKeyMan> LoadFromStorage(WalletStorage& storage, const uint256& id, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys);343static std::unique_ptr<DescriptorScriptPubKeyMan> CreateFromImport(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const FlatSigningProvider& provider);344static std::unique_ptr<DescriptorScriptPubKeyMan> CreateFromMigration(WalletStorage& storage, WalletBatch& batch, WalletDescriptor& descriptor, int64_t keypool_size, const FlatSigningProvider& provider);345static std::unique_ptr<DescriptorScriptPubKeyMan> GenerateNewSingleSig(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, const CExtKey& master_key, OutputType addr_type, bool internal);347mutable RecursiveMutex cs_desc_man;349util::Result<CTxDestination> GetNewDestination(OutputType type) override;350bool IsMine(const CScript& script) const override;352bool CheckDecryptionKey(const CKeyingMaterial& master_key) override;353bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) override;355util::Result<CTxDestination> GetReservedDestination(OutputType type, bool internal, int64_t& index) override;356void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) override;358// Tops up the descriptor cache and m_map_script_pub_keys. The cache is stored in the wallet file359// and is used to expand the descriptor in GetNewDestination. Descriptor wallets rely more on360// ephemeral data than legacy wallets. For wallets using unhardened derivation361// (with or without private keys), the "keypool" is a single xpub.362bool TopUp(unsigned int size = 0) override;364std::vector<WalletDestination> MarkUnusedAddresses(const CScript& script) override;366bool IsHDEnabled() const override;368bool HavePrivateKeys() const override;369bool HasPrivKey(const CKeyID& keyid) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);370//! Retrieve the particular key if it is available. Returns nullopt if the key is not in the wallet, or if the wallet is locked.371std::optional<CKey> GetKey(const CKeyID& keyid) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);372bool HaveCryptedKeys() const override;374unsigned int GetKeyPoolSize() const override;376int64_t GetTimeFirstKey() const override;378std::unique_ptr<CKeyMetadata> GetMetadata(const CTxDestination& dest) const override;380bool CanGetAddresses(bool internal = false) const override;382std::unique_ptr<SigningProvider> GetSolvingProvider(const CScript& script) const override;384bool CanProvide(const CScript& script, SignatureData& sigdata) override;386// Fetch the SigningProvider for the given pubkey and always include private keys. This should only be called by signing code.387std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CPubKey& pubkey) const;389bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors) const override;390SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const override;391std::optional<common::PSBTError> FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, const common::PSBTFillOptions& options, int* n_signed = nullptr) const override;393uint256 GetID() const override;395bool HasWalletDescriptor(const WalletDescriptor& desc) const;396util::Result<void> UpdateWalletDescriptor(WalletDescriptor& descriptor, const FlatSigningProvider& provider);397bool CanUpdateToWalletDescriptor(const WalletDescriptor& descriptor, std::string& error);398void WriteDescriptor();400WalletDescriptor GetWalletDescriptor() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);401std::unordered_set<CScript, SaltedSipHasher> GetScriptPubKeys() const override;402std::unordered_set<CScript, SaltedSipHasher> GetScriptPubKeys(int32_t minimum_index) const;403int32_t GetEndRange() const;405[[nodiscard]] bool GetDescriptorString(std::string& out, bool priv) const;407void UpgradeDescriptorCache();408};410/** struct containing information needed for migrating legacy wallets to descriptor wallets */411struct MigrationData412{413CExtKey master_key;414std::vector<std::pair<std::string, int64_t>> watch_descs;415std::vector<std::pair<std::string, int64_t>> solvable_descs;416std::vector<std::unique_ptr<DescriptorScriptPubKeyMan>> desc_spkms;417std::shared_ptr<CWallet> watchonly_wallet{nullptr};418std::shared_ptr<CWallet> solvable_wallet{nullptr};419};421} // namespace wallet423#endif // BITCOIN_WALLET_SCRIPTPUBKEYMAN_H