LCOV - code coverage report
Current view: top level - src/wallet - hdchain.h (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 25 44 56.8 %
Date: 2026-06-25 07:23:51 Functions: 8 31 25.8 %

          Line data    Source code
       1             : // Copyright (c) 2014-2025 The Dash Core developers
       2             : // Distributed under the MIT software license, see the accompanying
       3             : #ifndef BITCOIN_WALLET_HDCHAIN_H
       4             : #define BITCOIN_WALLET_HDCHAIN_H
       5             : 
       6             : #include <key.h>
       7             : #include <script/keyorigin.h>
       8             : #include <sync.h>
       9             : 
      10             : namespace wallet {
      11             : /* hd account data model */
      12           0 : struct CHDAccount {
      13           0 :     uint32_t nExternalChainCounter{0};
      14           0 :     uint32_t nInternalChainCounter{0};
      15             : 
      16           0 :     SERIALIZE_METHODS(CHDAccount, obj)
      17             :     {
      18           0 :         READWRITE(obj.nExternalChainCounter, obj.nInternalChainCounter);
      19           0 :     }
      20             : };
      21             : 
      22             : /* simple HD chain data model */
      23             : class CHDChain
      24             : {
      25             : private:
      26             :     mutable RecursiveMutex cs;
      27             : 
      28             :     static const int CURRENT_VERSION = 1;
      29       22336 :     int nVersion GUARDED_BY(cs) {CURRENT_VERSION};
      30             : 
      31             :     uint256 id GUARDED_BY(cs);
      32             : 
      33       22336 :     bool fCrypted GUARDED_BY(cs) {false};
      34             : 
      35             :     SecureVector vchSeed GUARDED_BY(cs);
      36             :     SecureVector vchMnemonic GUARDED_BY(cs);
      37             :     SecureVector vchMnemonicPassphrase GUARDED_BY(cs);
      38             : 
      39             :     std::map<uint32_t, CHDAccount> GUARDED_BY(cs) mapAccounts;
      40             : 
      41             : public:
      42             :     /** Default for -mnemonicbits */
      43             :     static constexpr int DEFAULT_MNEMONIC_BITS = 128; // 128 bits == 12 words
      44             : 
      45       67008 :     CHDChain() = default;
      46       44628 :     CHDChain(const CHDChain& other) :
      47       22314 :         nVersion(other.nVersion),
      48       22314 :         id(other.id),
      49       22314 :         fCrypted(other.fCrypted),
      50       22314 :         vchSeed(other.vchSeed),
      51       22314 :         vchMnemonic(other.vchMnemonic),
      52       22314 :         vchMnemonicPassphrase(other.vchMnemonicPassphrase),
      53       22314 :         mapAccounts(other.mapAccounts)
      54       44628 :         {}
      55             : 
      56           0 :     SERIALIZE_METHODS(CHDChain, obj)
      57             :     {
      58           0 :         LOCK(obj.cs);
      59           0 :         READWRITE(
      60             :                 obj.nVersion,
      61             :                 obj.id,
      62             :                 obj.fCrypted,
      63             :                 obj.vchSeed,
      64             :                 obj.vchMnemonic,
      65             :                 obj.vchMnemonicPassphrase,
      66             :                 obj.mapAccounts
      67             :                 );
      68           0 :     }
      69             : 
      70       22314 :     void swap(CHDChain& first, CHDChain& second) noexcept
      71             :     {
      72             :         // enable ADL (not necessary in our case, but good practice)
      73             :         using std::swap;
      74             : 
      75             :         // by swapping the members of two classes,
      76             :         // the two classes are effectively swapped
      77       22314 :         LOCK2(first.cs, second.cs);
      78       22314 :         swap(first.nVersion, second.nVersion);
      79       22314 :         swap(first.id, second.id);
      80       22314 :         swap(first.fCrypted, second.fCrypted);
      81       22314 :         swap(first.vchSeed, second.vchSeed);
      82       22314 :         swap(first.vchMnemonic, second.vchMnemonic);
      83       22314 :         swap(first.vchMnemonicPassphrase, second.vchMnemonicPassphrase);
      84       22314 :         swap(first.mapAccounts, second.mapAccounts);
      85       22314 :     }
      86       22314 :     CHDChain& operator=(CHDChain from)
      87             :     {
      88       22314 :         swap(*this, from);
      89       22314 :         return *this;
      90             :     }
      91             : 
      92             :     bool SetNull();
      93             :     bool IsNull() const;
      94             : 
      95             :     void SetCrypted(bool fCryptedIn);
      96             :     bool IsCrypted() const;
      97             : 
      98             :     bool SetMnemonic(const SecureVector& vchMnemonic, const SecureVector& vchMnemonicPassphrase, bool fUpdateID);
      99             :     bool SetMnemonic(const SecureString& ssMnemonic, const SecureString& ssMnemonicPassphrase, bool fUpdateID);
     100             :     bool GetMnemonic(SecureVector& vchMnemonicRet, SecureVector& vchMnemonicPassphraseRet) const;
     101             :     bool GetMnemonic(SecureString& ssMnemonicRet, SecureString& ssMnemonicPassphraseRet) const;
     102             : 
     103             :     bool SetSeed(const SecureVector& vchSeedIn, bool fUpdateID);
     104             :     SecureVector GetSeed() const;
     105             : 
     106           0 :     uint256 GetID() const { LOCK(cs); return id; }
     107             : 
     108             :     uint256 GetSeedHash();
     109             :     void DeriveChildExtKey(uint32_t nAccountIndex, bool fInternal, uint32_t nChildIndex, CExtKey& extKeyRet, KeyOriginInfo& key_origin);
     110             : 
     111             :     void AddAccount();
     112             :     bool GetAccount(uint32_t nAccountIndex, CHDAccount& hdAccountRet);
     113             :     bool SetAccount(uint32_t nAccountIndex, const CHDAccount& hdAccount);
     114             :     size_t CountAccounts();
     115             : };
     116             : 
     117             : /* hd pubkey data model */
     118             : class CHDPubKey
     119             : {
     120             : private:
     121             :     static const int CURRENT_VERSION = 1;
     122           0 :     [[maybe_unused]] int nVersion{CHDPubKey::CURRENT_VERSION};
     123             : 
     124             : public:
     125           0 :     CExtPubKey extPubKey{};
     126             :     uint256 hdchainID;
     127           0 :     uint32_t nAccountIndex{0};
     128           0 :     uint32_t nChangeIndex{0};
     129             : 
     130           0 :     CHDPubKey() = default;
     131             : 
     132           0 :     SERIALIZE_METHODS(CHDPubKey, obj)
     133             :     {
     134           0 :         READWRITE(obj.nVersion, obj.extPubKey, obj.hdchainID, obj.nAccountIndex, obj.nChangeIndex);
     135           0 :     }
     136             : 
     137             :     std::string GetKeyPath() const;
     138             : };
     139             : 
     140             : /** Purpose code used for DIP9 (feature derivation paths) */
     141             : constexpr uint8_t BIP32_PURPOSE_FEATURE{9};
     142             : /** Purpose code allotted to BIP 44 (standard derivation paths) */
     143             : constexpr uint8_t BIP32_PURPOSE_STANDARD{44};
     144             : 
     145             : } // namespace wallet
     146             : 
     147             : #endif // BITCOIN_WALLET_HDCHAIN_H

Generated by: LCOV version 1.16