LCOV - code coverage report
Current view: top level - src/wallet - walletutil.h (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 97.3 % 37 36
Test Date: 2026-08-29 06:21:54 Functions: 100.0 % 8 8
Branches: 58.5 % 82 48

             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 <script/descriptor.h>
       9                 :             : #include <util/fs.h>
      10                 :             : 
      11                 :             : #include <vector>
      12                 :             : 
      13                 :             : namespace wallet {
      14                 :             : 
      15                 :             : enum WalletFlags : uint64_t {
      16                 :             :     // wallet flags in the upper section (> 1 << 31) will lead to not opening the wallet if flag is unknown
      17                 :             :     // unknown wallet flags in the lower section <= (1 << 31) will be tolerated
      18                 :             : 
      19                 :             :     // will categorize coins as clean (not reused) and dirty (reused), and handle
      20                 :             :     // them with privacy considerations in mind
      21                 :             :     WALLET_FLAG_AVOID_REUSE = (1ULL << 0),
      22                 :             : 
      23                 :             :     // Indicates that the metadata has already been upgraded to contain key origins
      24                 :             :     WALLET_FLAG_KEY_ORIGIN_METADATA = (1ULL << 1),
      25                 :             : 
      26                 :             :     // Indicates that the descriptor cache has been upgraded to cache last hardened xpubs
      27                 :             :     WALLET_FLAG_LAST_HARDENED_XPUB_CACHED = (1ULL << 2),
      28                 :             : 
      29                 :             :     // will enforce the rule that the wallet can't contain any private keys (only watch-only/pubkeys)
      30                 :             :     WALLET_FLAG_DISABLE_PRIVATE_KEYS = (1ULL << 32),
      31                 :             : 
      32                 :             :     //! Flag set when a wallet contains no HD seed and no private keys, scripts,
      33                 :             :     //! addresses, and other watch only things, and is therefore "blank."
      34                 :             :     //!
      35                 :             :     //! The main function this flag serves is to distinguish a blank wallet from
      36                 :             :     //! a newly created wallet when the wallet database is loaded, to avoid
      37                 :             :     //! initialization that should only happen on first run.
      38                 :             :     //!
      39                 :             :     //! A secondary function of this flag, which applies to descriptor wallets
      40                 :             :     //! only, is to serve as an ongoing indication that descriptors in the
      41                 :             :     //! wallet should be created manually, and that the wallet should not
      42                 :             :     //! generate automatically generate new descriptors if it is later
      43                 :             :     //! encrypted. To support this behavior, descriptor wallets unlike legacy
      44                 :             :     //! wallets do not automatically unset the BLANK flag when things are
      45                 :             :     //! imported.
      46                 :             :     //!
      47                 :             :     //! This flag is also a mandatory flag to prevent previous versions of
      48                 :             :     //! bitcoin from opening the wallet, thinking it was newly created, and
      49                 :             :     //! then improperly reinitializing it.
      50                 :             :     WALLET_FLAG_BLANK_WALLET = (1ULL << 33),
      51                 :             : 
      52                 :             :     //! Indicate that this wallet supports DescriptorScriptPubKeyMan
      53                 :             :     WALLET_FLAG_DESCRIPTORS = (1ULL << 34),
      54                 :             : 
      55                 :             :     //! Indicates that the wallet needs an external signer
      56                 :             :     WALLET_FLAG_EXTERNAL_SIGNER = (1ULL << 35),
      57                 :             : };
      58                 :             : 
      59                 :             : //! Get the path of the wallet directory.
      60                 :             : fs::path GetWalletDir();
      61                 :             : 
      62                 :             : /** Descriptor with some wallet metadata */
      63                 :             : class WalletDescriptor
      64                 :             : {
      65                 :             : private:
      66                 :             :     int32_t range_start = 0; // First item in range; start of range, inclusive, i.e. [range_start, range_end). This never changes.
      67                 :             :     int32_t next_index = 0; // Position of the next item to generate
      68                 :             :     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()
      69                 :             : public:
      70                 :             :     std::shared_ptr<Descriptor> descriptor;
      71                 :             :     uint256 id; // Descriptor ID (calculated once at descriptor initialization/deserialization)
      72                 :             :     uint64_t creation_time = 0;
      73                 :             :     DescriptorCache cache;
      74                 :             : 
      75         [ +  + ]:        4383 :     int32_t GetStart() const { return range_start; }
      76   [ +  +  #  #  :       96024 :     int32_t GetNext() const { return next_index; }
          #  #  #  #  #  
           # ][ +  +  +  
          +  +  -  +  -  
                   +  - ]
      77   [ +  +  +  -  :      271439 :     int32_t GetEnd() const { return range_end; }
          +  +  +  -  +  
          +  +  +  -  +  
                   -  + ]
      78                 :             : 
      79                 :             :     //! Increments the next_index of the descriptor.
      80                 :       43847 :     void IncNext()
      81                 :             :     {
      82                 :       43847 :         next_index++;
      83                 :             :     }
      84                 :             : 
      85                 :             :     //! Increments the next_index of the descriptor.
      86                 :         105 :     void DecNext()
      87                 :             :     {
      88                 :         105 :         next_index--;
      89                 :             :     }
      90                 :             : 
      91                 :             :     //! Sets the range_end of the descriptor.
      92                 :       78015 :     void SetEnd(int32_t end)
      93                 :             :     {
      94         [ +  + ]:       78015 :         if (!descriptor->IsRange()) {
      95                 :       13112 :             CHECK_NONFATAL(end == 1);
      96                 :             :         }
      97                 :       78015 :         range_end = end;
      98                 :       78015 :     }
      99                 :             : 
     100                 :        2781 :     void DeserializeDescriptor(const std::string& str)
     101                 :             :     {
     102                 :        2781 :         std::string error;
     103                 :        2781 :         FlatSigningProvider keys;
     104   [ -  +  +  - ]:        2781 :         auto descs = Parse(str, keys, error, true);
     105         [ +  + ]:        2781 :         if (descs.empty()) {
     106   [ +  -  +  - ]:           4 :             throw std::ios_base::failure("Invalid descriptor: " + error);
     107                 :             :         }
     108   [ -  +  -  + ]:        2779 :         if (descs.size() > 1) {
     109         [ #  # ]:           0 :             throw std::ios_base::failure("Can't load a multipath descriptor from databases");
     110                 :             :         }
     111   [ +  -  +  - ]:        2779 :         descriptor = std::move(descs.at(0));
     112         [ +  - ]:        2779 :         id = DescriptorID(*descriptor);
     113                 :        2783 :     }
     114                 :             : 
     115         [ +  - ]:      210378 :     SERIALIZE_METHODS(WalletDescriptor, obj)
     116                 :             :     {
     117                 :      105189 :         std::string descriptor_str;
     118         [ +  - ]:      207597 :         SER_WRITE(obj, descriptor_str = obj.descriptor->ToString());
     119         [ +  - ]:      105189 :         READWRITE(descriptor_str, obj.creation_time, obj.next_index, obj.range_start, obj.range_end);
     120         [ +  + ]:      107968 :         SER_READ(obj, obj.DeserializeDescriptor(descriptor_str));
     121                 :      105187 :     }
     122                 :             : 
     123                 :        6713 :     WalletDescriptor() = default;
     124                 :        5091 :     WalletDescriptor(std::shared_ptr<Descriptor> descriptor, uint64_t creation_time, int32_t range_start, int32_t range_end, int32_t next_index)
     125         [ +  + ]:        5091 :     : range_start(descriptor->IsRange() ? range_start : 0),
     126                 :        5091 :       next_index(next_index),
     127         [ +  + ]:        5091 :       range_end(descriptor->IsRange() ? range_end : 1),
     128   [ +  -  +  - ]:       10182 :       descriptor(descriptor),
     129         [ +  - ]:        5091 :       id(DescriptorID(*descriptor)),
     130         [ -  - ]:        5091 :       creation_time(creation_time) {}
     131                 :             : };
     132                 :             : 
     133                 :             : WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& output_type, bool internal);
     134                 :             : } // namespace wallet
     135                 :             : 
     136                 :             : #endif // BITCOIN_WALLET_WALLETUTIL_H
        

Generated by: LCOV version 2.0-1