LCOV - code coverage report
Current view: top level - src/wallet - walletutil.h (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 97.1 % 35 34
Test Date: 2026-11-29 07:05:02 Functions: 100.0 % 5 5
Branches: 64.3 % 84 54

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2017-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_WALLETUTIL_H
       6                 :             : #define BITCOIN_WALLET_WALLETUTIL_H
       7                 :             : 
       8                 :             : #include <interfaces/chain.h>
       9                 :             : #include <script/descriptor.h>
      10                 :             : #include <util/fs.h>
      11                 :             : 
      12                 :             : #include <vector>
      13                 :             : 
      14                 :             : namespace wallet {
      15                 :             : 
      16                 :             : enum WalletFlags : uint64_t {
      17                 :             :     // wallet flags in the upper section (> 1 << 31) will lead to not opening the wallet if flag is unknown
      18                 :             :     // unknown wallet flags in the lower section <= (1 << 31) will be tolerated
      19                 :             : 
      20                 :             :     // will categorize coins as clean (not reused) and dirty (reused), and handle
      21                 :             :     // them with privacy considerations in mind
      22                 :             :     WALLET_FLAG_AVOID_REUSE = (1ULL << 0),
      23                 :             : 
      24                 :             :     // Indicates that the metadata has already been upgraded to contain key origins
      25                 :             :     WALLET_FLAG_KEY_ORIGIN_METADATA = (1ULL << 1),
      26                 :             : 
      27                 :             :     // Indicates that the descriptor cache has been upgraded to cache last hardened xpubs
      28                 :             :     WALLET_FLAG_LAST_HARDENED_XPUB_CACHED = (1ULL << 2),
      29                 :             : 
      30                 :             :     // will enforce the rule that the wallet can't contain any private keys (only watch-only/pubkeys)
      31                 :             :     WALLET_FLAG_DISABLE_PRIVATE_KEYS = (1ULL << 32),
      32                 :             : 
      33                 :             :     //! Flag set when a wallet contains no HD seed and no private keys, scripts,
      34                 :             :     //! addresses, and other watch only things, and is therefore "blank."
      35                 :             :     //!
      36                 :             :     //! The main function this flag serves is to distinguish a blank wallet from
      37                 :             :     //! a newly created wallet when the wallet database is loaded, to avoid
      38                 :             :     //! initialization that should only happen on first run.
      39                 :             :     //!
      40                 :             :     //! A secondary function of this flag, which applies to descriptor wallets
      41                 :             :     //! only, is to serve as an ongoing indication that descriptors in the
      42                 :             :     //! wallet should be created manually, and that the wallet should not
      43                 :             :     //! generate automatically generate new descriptors if it is later
      44                 :             :     //! encrypted. To support this behavior, descriptor wallets unlike legacy
      45                 :             :     //! wallets do not automatically unset the BLANK flag when things are
      46                 :             :     //! imported.
      47                 :             :     //!
      48                 :             :     //! This flag is also a mandatory flag to prevent previous versions of
      49                 :             :     //! bitcoin from opening the wallet, thinking it was newly created, and
      50                 :             :     //! then improperly reinitializing it.
      51                 :             :     WALLET_FLAG_BLANK_WALLET = (1ULL << 33),
      52                 :             : 
      53                 :             :     //! Indicate that this wallet supports DescriptorScriptPubKeyMan
      54                 :             :     WALLET_FLAG_DESCRIPTORS = (1ULL << 34),
      55                 :             : 
      56                 :             :     //! Indicates that the wallet needs an external signer
      57                 :             :     WALLET_FLAG_EXTERNAL_SIGNER = (1ULL << 35),
      58                 :             : };
      59                 :             : 
      60                 :             : // Version numbers for the wallet client that opens a wallet
      61                 :             : // These numbers will be written as the last client version in the "version" record and can be used to detect
      62                 :             : // when an upgrade-downgrade-upgrade was performed. However, we should prefer to use LastClientFeatures rather
      63                 :             : // than new version numbers.
      64                 :             : // New version numbers must be greater than 329900 which is guaranteed by setting bit 19
      65                 :             : enum WalletClientVersion : int32_t {
      66                 :             :     MIN_VERSION = (1L << 19),
      67                 :             : 
      68                 :             :     // The wallet client supports the records for LastClientFeatures
      69                 :             :     VERSION_LAST_CLIENT_FEATURES = MIN_VERSION + 1,
      70                 :             : 
      71                 :             :     VERSION_LATEST = VERSION_LAST_CLIENT_FEATURES
      72                 :             : };
      73                 :             : 
      74                 :             : enum LastClientFeatures : uint64_t {
      75                 :             :     // Flags indicating the automatic upgrade features supported by the wallet client that last opened a wallet file
      76                 :             :     // New automatic upgrades must define a flag here so that upgrade-downgrade-upgrade can be detected to determine whether
      77                 :             :     // an automatic upgrade should be performed.
      78                 :             : 
      79                 :             :     WALLET_CLIENT_FEATURES = 0
      80                 :             : };
      81                 :             : 
      82                 :             : //! Get the path of the wallet directory.
      83                 :             : fs::path GetWalletDir();
      84                 :             : 
      85                 :             : //! Determine the path that the wallet is stored in
      86                 :             : util::Result<fs::path> GetWalletPath(const std::string& name);
      87                 :             : 
      88                 :             : //! Add wallet name to persistent configuration so it will be loaded on startup.
      89                 :             : bool AddWalletSetting(interfaces::Chain& chain, const std::string& wallet_name);
      90                 :             : 
      91                 :             : //! Remove wallet name from persistent configuration so it will not be loaded on startup.
      92                 :             : bool RemoveWalletSetting(interfaces::Chain& chain, const std::string& wallet_name);
      93                 :             : 
      94                 :             : void UpdateWalletSetting(interfaces::Chain& chain,
      95                 :             :                                 const std::string& wallet_name,
      96                 :             :                                 std::optional<bool> load_on_startup,
      97                 :             :                                 std::vector<bilingual_str>& warnings);
      98                 :             : 
      99                 :             : /** Descriptor with some wallet metadata */
     100                 :             : class WalletDescriptor
     101                 :             : {
     102                 :             : private:
     103                 :             :     int32_t range_start = 0; // First item in range; start of range, inclusive, i.e. [range_start, range_end). This never changes.
     104                 :             :     int32_t next_index = 0; // Position of the next item to generate
     105                 :             :     int32_t range_end = 0; // Item after the last; end of range, exclusive, i.e. [range_start, range_end). This will increment with each TopUp()
     106                 :             : 
     107                 :             :     mutable std::optional<uint256> m_canonical_hash; // Hash of the canonical string, used as a shortcut for comparing canonical strings
     108                 :             : 
     109                 :             :     uint256 GetCanonicalHash() const;
     110                 :             : 
     111                 :             : public:
     112                 :             :     const std::shared_ptr<const Descriptor> descriptor;
     113                 :             :     uint64_t creation_time = 0;
     114                 :             :     DescriptorCache cache;
     115                 :             : 
     116         [ +  + ]:        5231 :     int32_t GetStart() const { return range_start; }
     117   [ +  +  +  +  :      178429 :     int32_t GetNext() const { return next_index; }
          +  +  +  -  +  
                -  +  - ]
                 [ +  + ]
     118   [ +  +  +  -  :      285741 :     int32_t GetEnd() const { return range_end; }
          +  +  +  -  +  
          +  +  +  -  +  
                   -  + ]
     119                 :             : 
     120                 :             :     //! Increments the next_index of the descriptor.
     121                 :       44329 :     void IncNext()
     122                 :             :     {
     123                 :       44329 :         next_index++;
     124                 :             :     }
     125                 :             : 
     126                 :             :     //! Increments the next_index of the descriptor.
     127                 :         107 :     void DecNext()
     128                 :             :     {
     129                 :         107 :         next_index--;
     130                 :             :     }
     131                 :             : 
     132                 :             :     //! Sets the range_end of the descriptor.
     133                 :       80177 :     void SetEnd(int32_t end)
     134                 :             :     {
     135         [ +  + ]:       80177 :         if (!descriptor->IsRange()) {
     136                 :       13735 :             CHECK_NONFATAL(end == 1);
     137                 :             :         }
     138                 :       80177 :         range_end = end;
     139                 :       80177 :     }
     140                 :             : 
     141                 :             :     template <typename Stream>
     142                 :      105036 :     void Serialize(Stream& s) const
     143                 :             :     {
     144         [ +  - ]:      105036 :         std::string descriptor_str = descriptor->ToString();
     145   [ +  -  +  -  :      210072 :         s << descriptor_str << creation_time << next_index << range_start << range_end;
             +  -  +  - ]
     146                 :      105036 :     }
     147                 :             : 
     148                 :             :     template <typename Stream>
     149         [ +  - ]:        3186 :     static WalletDescriptor FromStream(deserialize_type, Stream& s)
     150                 :             :     {
     151         [ +  - ]:        3186 :         std::string descriptor_str;
     152                 :             :         uint64_t creation_time;
     153                 :             :         int32_t next_index, range_start, range_end;
     154   [ +  -  +  -  :        6372 :         s >> descriptor_str >> creation_time >> next_index >> range_start >> range_end;
             +  -  +  - ]
     155                 :             : 
     156                 :        3186 :         std::string error;
     157         [ -  + ]:        3186 :         FlatSigningProvider keys;
     158         [ +  - ]:        3186 :         auto descs = Parse(descriptor_str, keys, error, true);
     159         [ +  + ]:        3186 :         if (descs.empty()) {
     160   [ +  -  +  - ]:           4 :             throw std::ios_base::failure("Invalid descriptor: " + error);
     161                 :             :         }
     162         [ -  + ]:        3184 :         if (descs.size() > 1) {
     163         [ #  # ]:           0 :             throw std::ios_base::failure("Can't load a multipath descriptor from databases");
     164                 :             :         }
     165   [ +  -  +  -  :        3184 :         return WalletDescriptor(std::move(descs.at(0)), creation_time, range_start, range_end, next_index);
                   +  - ]
     166                 :        3190 :     }
     167                 :             : 
     168                 :             :     WalletDescriptor() = delete;
     169                 :        8570 :     WalletDescriptor(std::shared_ptr<Descriptor> descriptor, uint64_t creation_time, int32_t range_start, int32_t range_end, int32_t next_index)
     170         [ +  + ]:        8570 :     : range_start(descriptor->IsRange() ? range_start : 0),
     171                 :        8570 :       next_index(next_index),
     172         [ +  + ]:        8570 :       range_end(descriptor->IsRange() ? range_end : 1),
     173         [ +  - ]:        8570 :       descriptor(descriptor),
     174         [ +  - ]:       17140 :       creation_time(creation_time)
     175                 :        8570 :     {}
     176                 :             : 
     177                 :             :     /** Replaces all metadata (range, start, end, creation time), and cache from another WalletDescriptor if it has the same canonical descriptor string.
     178                 :             :      *  The descriptor itself is not replaced to preserve existing serialization to maintain compatibility with previous software versions that expect
     179                 :             :      *  specific serialization.
     180                 :             :      */
     181                 :             :     void UpdateFrom(const WalletDescriptor& other);
     182                 :             : 
     183                 :             :     // Compare by using the canonical string to make the hardened indicators consistent for comparison
     184                 :             :     bool IsCanonicallyEquivalent(const WalletDescriptor& other) const;
     185                 :             : };
     186                 :             : 
     187                 :             : WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& output_type, bool internal);
     188                 :             : } // namespace wallet
     189                 :             : 
     190                 :             : #endif // BITCOIN_WALLET_WALLETUTIL_H
        

Generated by: LCOV version 2.0-1