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 34243 : struct CHDAccount {
13 34243 : uint32_t nExternalChainCounter{0};
14 34243 : uint32_t nInternalChainCounter{0};
15 :
16 99378 : SERIALIZE_METHODS(CHDAccount, obj)
17 : {
18 33126 : READWRITE(obj.nExternalChainCounter, obj.nInternalChainCounter);
19 33126 : }
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 208588 : int nVersion GUARDED_BY(cs) {CURRENT_VERSION};
30 :
31 : uint256 id GUARDED_BY(cs);
32 :
33 208588 : 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 625764 : CHDChain() = default;
46 477878 : CHDChain(const CHDChain& other) :
47 238939 : nVersion(other.nVersion),
48 238939 : id(other.id),
49 238939 : fCrypted(other.fCrypted),
50 238939 : vchSeed(other.vchSeed),
51 238939 : vchMnemonic(other.vchMnemonic),
52 238939 : vchMnemonicPassphrase(other.vchMnemonicPassphrase),
53 238939 : mapAccounts(other.mapAccounts)
54 477878 : {}
55 :
56 99420 : SERIALIZE_METHODS(CHDChain, obj)
57 : {
58 33140 : LOCK(obj.cs);
59 33140 : READWRITE(
60 : obj.nVersion,
61 : obj.id,
62 : obj.fCrypted,
63 : obj.vchSeed,
64 : obj.vchMnemonic,
65 : obj.vchMnemonicPassphrase,
66 : obj.mapAccounts
67 : );
68 33140 : }
69 :
70 238894 : 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 238894 : LOCK2(first.cs, second.cs);
78 238894 : swap(first.nVersion, second.nVersion);
79 238894 : swap(first.id, second.id);
80 238894 : swap(first.fCrypted, second.fCrypted);
81 238894 : swap(first.vchSeed, second.vchSeed);
82 238894 : swap(first.vchMnemonic, second.vchMnemonic);
83 238894 : swap(first.vchMnemonicPassphrase, second.vchMnemonicPassphrase);
84 238894 : swap(first.mapAccounts, second.mapAccounts);
85 238894 : }
86 238894 : CHDChain& operator=(CHDChain from)
87 : {
88 238894 : swap(*this, from);
89 238894 : 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 85277 : 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 111572 : [[maybe_unused]] int nVersion{CHDPubKey::CURRENT_VERSION};
123 :
124 : public:
125 111572 : CExtPubKey extPubKey{};
126 : uint256 hdchainID;
127 111572 : uint32_t nAccountIndex{0};
128 111572 : uint32_t nChangeIndex{0};
129 :
130 446288 : CHDPubKey() = default;
131 :
132 167358 : SERIALIZE_METHODS(CHDPubKey, obj)
133 : {
134 55786 : READWRITE(obj.nVersion, obj.extPubKey, obj.hdchainID, obj.nAccountIndex, obj.nChangeIndex);
135 55786 : }
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
|