Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #ifndef BITCOIN_WALLET_WALLETDB_H
7 : : #define BITCOIN_WALLET_WALLETDB_H
8 : :
9 : : #include <script/sign.h>
10 : : #include <wallet/db.h>
11 : : #include <wallet/walletutil.h>
12 : : #include <key.h>
13 : :
14 : : #include <stdint.h>
15 : : #include <string>
16 : : #include <vector>
17 : :
18 : : class CScript;
19 : : class uint160;
20 : : class uint256;
21 : : struct CBlockLocator;
22 : :
23 : : namespace wallet {
24 : : class CMasterKey;
25 : : class CWallet;
26 : : class CWalletTx;
27 : : struct WalletContext;
28 : :
29 : : /**
30 : : * Overview of wallet database classes:
31 : : *
32 : : * - WalletBatch is an abstract modifier object for the wallet database, and encapsulates a database
33 : : * batch update as well as methods to act on the database. It should be agnostic to the database implementation.
34 : : */
35 : :
36 : : /** Error statuses for the wallet database.
37 : : * Values are in order of severity. When multiple errors occur, the most severe (highest value) will be returned.
38 : : */
39 : : enum class DBErrors : int
40 : : {
41 : : LOAD_OK = 0,
42 : : NEED_RESCAN = 1,
43 : : NEED_REWRITE = 2,
44 : : EXTERNAL_SIGNER_SUPPORT_REQUIRED = 3,
45 : : NONCRITICAL_ERROR = 4,
46 : : TOO_NEW = 5,
47 : : UNKNOWN_DESCRIPTOR = 6,
48 : : LOAD_FAIL = 7,
49 : : UNEXPECTED_LEGACY_ENTRY = 8,
50 : : LEGACY_WALLET = 9,
51 : : CORRUPT = 10,
52 : : };
53 : :
54 : : namespace DBKeys {
55 : : extern const std::string ACENTRY;
56 : : extern const std::string ACTIVEEXTERNALSPK;
57 : : extern const std::string ACTIVEINTERNALSPK;
58 : : extern const std::string BESTBLOCK;
59 : : extern const std::string BESTBLOCK_NOMERKLE;
60 : : extern const std::string CRYPTED_KEY;
61 : : extern const std::string CSCRIPT;
62 : : extern const std::string DEFAULTKEY;
63 : : extern const std::string DESTDATA;
64 : : extern const std::string FLAGS;
65 : : extern const std::string HDCHAIN;
66 : : extern const std::string KEY;
67 : : extern const std::string KEYMETA;
68 : : extern const std::string LOCKED_UTXO;
69 : : extern const std::string MASTER_KEY;
70 : : extern const std::string MINVERSION;
71 : : extern const std::string NAME;
72 : : extern const std::string OLD_KEY;
73 : : extern const std::string ORDERPOSNEXT;
74 : : extern const std::string POOL;
75 : : extern const std::string PURPOSE;
76 : : extern const std::string SETTINGS;
77 : : extern const std::string TX;
78 : : extern const std::string VERSION;
79 : : extern const std::string WALLETDESCRIPTOR;
80 : : extern const std::string WALLETDESCRIPTORCKEY;
81 : : extern const std::string WALLETDESCRIPTORKEY;
82 : : extern const std::string WATCHMETA;
83 : : extern const std::string WATCHS;
84 : :
85 : : // Keys in this set pertain only to the legacy wallet (LegacyScriptPubKeyMan) and are removed during migration from legacy to descriptors.
86 : : extern const std::unordered_set<std::string> LEGACY_TYPES;
87 : : } // namespace DBKeys
88 : :
89 : : /* simple HD chain data model */
90 : : class CHDChain
91 : : {
92 : : public:
93 : : uint32_t nExternalChainCounter;
94 : : uint32_t nInternalChainCounter;
95 : : CKeyID seed_id; //!< seed hash160
96 : : int64_t m_next_external_index{0}; // Next index in the keypool to be used. Memory only.
97 : : int64_t m_next_internal_index{0}; // Next index in the keypool to be used. Memory only.
98 : :
99 : : static const int VERSION_HD_BASE = 1;
100 : : static const int VERSION_HD_CHAIN_SPLIT = 2;
101 : : static const int CURRENT_VERSION = VERSION_HD_CHAIN_SPLIT;
102 : : int nVersion;
103 : :
104 : 254 : CHDChain() { SetNull(); }
105 : :
106 : 27 : SERIALIZE_METHODS(CHDChain, obj)
107 : : {
108 : 27 : READWRITE(obj.nVersion, obj.nExternalChainCounter, obj.seed_id);
109 [ + - ]: 27 : if (obj.nVersion >= VERSION_HD_CHAIN_SPLIT) {
110 : 27 : READWRITE(obj.nInternalChainCounter);
111 : : }
112 : 27 : }
113 : :
114 : 254 : void SetNull()
115 : : {
116 : 254 : nVersion = CHDChain::CURRENT_VERSION;
117 : 254 : nExternalChainCounter = 0;
118 : 254 : nInternalChainCounter = 0;
119 : 254 : seed_id.SetNull();
120 : : }
121 : :
122 : : bool operator==(const CHDChain& chain) const
123 : : {
124 : : return seed_id == chain.seed_id;
125 : : }
126 : : };
127 : :
128 : 1169 : class CKeyMetadata
129 : : {
130 : : public:
131 : : static const int VERSION_BASIC=1;
132 : : static const int VERSION_WITH_HDDATA=10;
133 : : static const int VERSION_WITH_KEY_ORIGIN = 12;
134 : : static const int CURRENT_VERSION=VERSION_WITH_KEY_ORIGIN;
135 : : int nVersion;
136 : : int64_t nCreateTime; // 0 means unknown
137 : : std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility
138 : : CKeyID hd_seed_id; //id of the HD seed used to derive this key
139 : : KeyOriginInfo key_origin; // Key origin info with path and fingerprint
140 : : bool has_key_origin = false; //!< Whether the key_origin is useful
141 : :
142 : 1169 : CKeyMetadata()
143 : 1169 : {
144 : 1169 : SetNull();
145 : 1169 : }
146 : : explicit CKeyMetadata(int64_t nCreateTime_)
147 : : {
148 : : SetNull();
149 : : nCreateTime = nCreateTime_;
150 : : }
151 : :
152 : 486 : SERIALIZE_METHODS(CKeyMetadata, obj)
153 : : {
154 : 243 : READWRITE(obj.nVersion, obj.nCreateTime);
155 [ + - ]: 243 : if (obj.nVersion >= VERSION_WITH_HDDATA) {
156 : 243 : READWRITE(obj.hdKeypath, obj.hd_seed_id);
157 : : }
158 [ + - ]: 243 : if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN)
159 : : {
160 : 243 : READWRITE(obj.key_origin);
161 : 243 : READWRITE(obj.has_key_origin);
162 : : }
163 : 243 : }
164 : :
165 : 1169 : void SetNull()
166 : : {
167 : 1169 : nVersion = CKeyMetadata::CURRENT_VERSION;
168 : 1169 : nCreateTime = 0;
169 : 1169 : hdKeypath.clear();
170 : 1169 : hd_seed_id.SetNull();
171 : 1169 : key_origin.clear();
172 : 1169 : has_key_origin = false;
173 : 1169 : }
174 : : };
175 : :
176 : 18 : struct DbTxnListener
177 : : {
178 : : std::function<void()> on_commit, on_abort;
179 : : };
180 : :
181 : : /** Access to the wallet database.
182 : : * Opens the database and provides read and write access to it. Each read and write is its own transaction.
183 : : * Multiple operation transactions can be started using TxnBegin() and committed using TxnCommit()
184 : : * Otherwise the transaction will be committed when the object goes out of scope.
185 : : * Optionally (on by default) it will flush to disk on close.
186 : : * Every 1000 writes will automatically trigger a flush to disk.
187 : : */
188 [ - - ][ - + : 151911 : class WalletBatch
+ + + - +
- - - +
- ]
[ + - + - ]
189 : : {
190 : : private:
191 : : template <typename K, typename T>
192 : 225116 : bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
193 : : {
194 [ - + - + : 225116 : if (!m_batch->Write(key, value, fOverwrite)) {
+ - - + +
- - + + -
- + + - -
+ + - - +
+ - - + +
- - + + -
- + + - -
+ + - - +
- + + + -
+ - - - -
- - - - +
- - + - -
- - - - -
- - - - -
- - - - +
- + + + -
- + + - -
+ ]
195 : 2 : return false;
196 : : }
197 : : return true;
198 : : }
199 : :
200 : : template <typename K>
201 : 393 : bool EraseIC(const K& key)
202 : : {
203 [ + + - + : 393 : if (!m_batch->Erase(key)) {
+ - - + +
- - + + -
- + - - -
- - - - -
+ - - + +
- - + + -
- + + - -
+ ]
204 : 0 : return false;
205 : : }
206 : : return true;
207 : : }
208 : :
209 : : public:
210 : 151911 : explicit WalletBatch(WalletDatabase &database) :
211 [ + - + - : 151911 : m_batch(database.MakeBatch())
+ - + - +
- ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - +
- ][ - - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - - - -
- + - + -
+ - + - +
- + - +
- ][ + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ + - +
- + - +
- ]
212 : : {
213 : 1 : }
214 : : WalletBatch(const WalletBatch&) = delete;
215 : : WalletBatch& operator=(const WalletBatch&) = delete;
216 : :
217 : : bool WriteName(const std::string& strAddress, const std::string& strName);
218 : : bool EraseName(const std::string& strAddress);
219 : :
220 : : bool WritePurpose(const std::string& strAddress, const std::string& purpose);
221 : : bool ErasePurpose(const std::string& strAddress);
222 : :
223 : : bool WriteTx(const CWalletTx& wtx);
224 : : bool EraseTx(uint256 hash);
225 : :
226 : : bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite);
227 : : bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
228 : : bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
229 : : bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
230 : : bool EraseMasterKey(unsigned int id);
231 : :
232 : : bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
233 : : bool EraseWatchOnly(const CScript &script);
234 : :
235 : : bool WriteBestBlock(const CBlockLocator& locator);
236 : : bool ReadBestBlock(CBlockLocator& locator);
237 : :
238 : : // Returns true if wallet stores encryption keys
239 : : bool IsEncrypted();
240 : :
241 : : bool WriteOrderPosNext(int64_t nOrderPosNext);
242 : :
243 : : bool WriteMinVersion(int nVersion);
244 : :
245 : : bool WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey);
246 : : bool WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret);
247 : : bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor);
248 : : bool WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index);
249 : : bool WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
250 : : bool WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
251 : : bool WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache);
252 : :
253 : : bool WriteLockedUTXO(const COutPoint& output);
254 : : bool EraseLockedUTXO(const COutPoint& output);
255 : :
256 : : bool WriteAddressPreviouslySpent(const CTxDestination& dest, bool previously_spent);
257 : : bool WriteAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& receive_request);
258 : : bool EraseAddressReceiveRequest(const CTxDestination& dest, const std::string& id);
259 : : bool EraseAddressData(const CTxDestination& dest);
260 : :
261 : : bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal);
262 : : bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal);
263 : :
264 : : DBErrors LoadWallet(CWallet* pwallet);
265 : :
266 : : //! Delete records of the given types
267 : : bool EraseRecords(const std::unordered_set<std::string>& types);
268 : :
269 : : bool WriteWalletFlags(const uint64_t flags);
270 : : //! Begin a new transaction
271 : : bool TxnBegin();
272 : : //! Commit current transaction
273 : : bool TxnCommit();
274 : : //! Abort current transaction
275 : : bool TxnAbort();
276 : 10 : bool HasActiveTxn() { return m_batch->HasActiveTxn(); }
277 : :
278 : : //! Registers db txn callback functions
279 : : void RegisterTxnListener(const DbTxnListener& l);
280 : :
281 : : private:
282 : : std::unique_ptr<DatabaseBatch> m_batch;
283 : :
284 : : // External functions listening to the current db txn outcome.
285 : : // Listeners are cleared at the end of the transaction.
286 : : std::vector<DbTxnListener> m_txn_listeners;
287 : : };
288 : :
289 : : /**
290 : : * Executes the provided function 'func' within a database transaction context.
291 : : *
292 : : * This function ensures that all db modifications performed within 'func()' are
293 : : * atomically committed to the db at the end of the process. And, in case of a
294 : : * failure during execution, all performed changes are rolled back.
295 : : *
296 : : * @param database The db connection instance to perform the transaction on.
297 : : * @param process_desc A description of the process being executed, used for logging purposes in the event of a failure.
298 : : * @param func The function to be executed within the db txn context. It returns a boolean indicating whether to commit or roll back the txn.
299 : : * @return true if the db txn executed successfully, false otherwise.
300 : : */
301 : : bool RunWithinTxn(WalletDatabase& database, std::string_view process_desc, const std::function<bool(WalletBatch&)>& func);
302 : :
303 : : bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
304 : : bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
305 : : bool LoadEncryptionKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
306 : : bool LoadHDChain(CWallet* pwallet, DataStream& ssValue, std::string& strErr);
307 : :
308 : : //! Returns true if there are any DBKeys::LEGACY_TYPES record in the wallet db
309 : : bool HasLegacyRecords(CWallet& wallet);
310 : : bool HasLegacyRecords(CWallet& wallet, DatabaseBatch& batch);
311 : : } // namespace wallet
312 : :
313 : : #endif // BITCOIN_WALLET_WALLETDB_H
|