Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
walletdb.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Copyright (c) 2017-2020 The Bitcoin developers
4// Distributed under the MIT software license, see the accompanying
5// file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7#ifndef BITCOIN_WALLET_WALLETDB_H
8#define BITCOIN_WALLET_WALLETDB_H
9
10#include <key.h>
11#include <script/sign.h>
12#include <script/standard.h> // for CTxDestination
13#include <wallet/bdb.h>
14#include <wallet/walletutil.h>
15
16#include <cstdint>
17#include <string>
18#include <vector>
19
33static const bool DEFAULT_FLUSHWALLET = true;
34
35struct CBlockLocator;
36class CKeyPool;
37class CMasterKey;
38class CScript;
39class CWallet;
40class CWalletTx;
41class uint160;
42class uint256;
43
45enum class DBErrors {
46 LOAD_OK,
47 CORRUPT,
49 TOO_NEW,
52};
53
54namespace DBKeys {
55extern const std::string ACENTRY;
56extern const std::string ACTIVEEXTERNALSPK;
57extern const std::string ACTIVEINTERNALSPK;
58extern const std::string BESTBLOCK;
59extern const std::string BESTBLOCK_NOMERKLE;
60extern const std::string CRYPTED_KEY;
61extern const std::string CSCRIPT;
62extern const std::string DEFAULTKEY;
63extern const std::string DESTDATA;
64extern const std::string FLAGS;
65extern const std::string HDCHAIN;
66extern const std::string KEY;
67extern const std::string KEYMETA;
68extern const std::string MASTER_KEY;
69extern const std::string MINVERSION;
70extern const std::string NAME;
71extern const std::string OLD_KEY;
72extern const std::string ORDERPOSNEXT;
73extern const std::string POOL;
74extern const std::string PURPOSE;
75extern const std::string SETTINGS;
76extern const std::string TX;
77extern const std::string VERSION;
78extern const std::string WALLETDESCRIPTOR;
79extern const std::string WALLETDESCRIPTORCACHE;
80extern const std::string WALLETDESCRIPTORCKEY;
81extern const std::string WALLETDESCRIPTORKEY;
82extern const std::string WATCHMETA;
83extern const std::string WATCHS;
84} // namespace DBKeys
85
86/* simple HD chain data model */
87class CHDChain {
88public:
93
94 static const int VERSION_HD_BASE = 1;
95 static const int VERSION_HD_CHAIN_SPLIT = 2;
98
100
102 READWRITE(obj.nVersion, obj.nExternalChainCounter, obj.seed_id);
103 if (obj.nVersion >= VERSION_HD_CHAIN_SPLIT) {
104 READWRITE(obj.nInternalChainCounter);
105 }
106 }
107
114
115 bool operator==(const CHDChain &chain) const {
116 return seed_id == chain.seed_id;
117 }
118};
119
121public:
122 static const int VERSION_BASIC = 1;
123 static const int VERSION_WITH_HDDATA = 10;
124 static const int VERSION_WITH_KEY_ORIGIN = 12;
127 // 0 means unknown.
129 // optional HD/bip32 keypath. Still used to determine whether a key is a
130 // seed. Also kept for backwards compatibility
131 std::string hdKeypath;
132 // Id of the HD seed used to derive this key.
134 // Key origin info with path and fingerprint
137 bool has_key_origin = false;
138
144
146 READWRITE(obj.nVersion, obj.nCreateTime);
147 if (obj.nVersion >= VERSION_WITH_HDDATA) {
148 READWRITE(obj.hdKeypath, obj.hd_seed_id);
149 }
150 if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN) {
151 READWRITE(obj.key_origin);
152 READWRITE(obj.has_key_origin);
153 }
154 }
155
156 void SetNull() {
158 nCreateTime = 0;
159 hdKeypath.clear();
162 has_key_origin = false;
163 }
164};
165
176private:
177 template <typename K, typename T>
178 bool WriteIC(const K &key, const T &value, bool fOverwrite = true) {
179 if (!m_batch->Write(key, value, fOverwrite)) {
180 return false;
181 }
183 if (m_database.nUpdateCounter % 1000 == 0) {
184 m_batch->Flush();
185 }
186 return true;
187 }
188
189 template <typename K> bool EraseIC(const K &key) {
190 if (!m_batch->Erase(key)) {
191 return false;
192 }
194 if (m_database.nUpdateCounter % 1000 == 0) {
195 m_batch->Flush();
196 }
197 return true;
198 }
199
200public:
201 explicit WalletBatch(WalletDatabase &database, bool _fFlushOnClose = true)
202 : m_batch(database.MakeBatch(_fFlushOnClose)), m_database(database) {}
203 WalletBatch(const WalletBatch &) = delete;
205
206 bool WriteName(const CTxDestination &address, const std::string &strName);
207 bool EraseName(const CTxDestination &address);
208
209 bool WritePurpose(const CTxDestination &address,
210 const std::string &purpose);
211 bool ErasePurpose(const CTxDestination &address);
212
213 bool WriteTx(const CWalletTx &wtx);
214 bool EraseTx(uint256 hash);
215
216 bool WriteKeyMetadata(const CKeyMetadata &meta, const CPubKey &pubkey,
217 const bool overwrite);
218 bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey,
219 const CKeyMetadata &keyMeta);
220 bool WriteCryptedKey(const CPubKey &vchPubKey,
221 const std::vector<uint8_t> &vchCryptedSecret,
222 const CKeyMetadata &keyMeta);
223 bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey);
224
225 bool WriteCScript(const uint160 &hash, const CScript &redeemScript);
226
227 bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
228 bool EraseWatchOnly(const CScript &script);
229
232
234
236 bool WritePool(int64_t nPool, const CKeyPool &keypool);
237 bool ErasePool(int64_t nPool);
238
239 bool WriteMinVersion(int nVersion);
240
241 bool WriteDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey,
242 const CPrivKey &privkey);
244 const CPubKey &pubkey,
245 const std::vector<uint8_t> &secret);
246 bool WriteDescriptor(const uint256 &desc_id,
247 const WalletDescriptor &descriptor);
249 const uint256 &desc_id,
253 const uint256 &desc_id,
255
257 bool WriteDestData(const CTxDestination &address, const std::string &key,
258 const std::string &value);
260 bool EraseDestData(const CTxDestination &address, const std::string &key);
261
262 bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id,
263 bool internal);
265
266 DBErrors LoadWallet(CWallet *pwallet);
267 DBErrors FindWalletTx(std::vector<TxId> &txIds, std::list<CWalletTx> &vWtx);
268 DBErrors ZapSelectTx(std::vector<TxId> &txIdsIn,
269 std::vector<TxId> &txIdsOut);
270 /* Function to determine if a certain KV/key-type is a key (cryptographical
271 * key) type */
272 static bool IsKeyType(const std::string &strType);
273
275 bool WriteHDChain(const CHDChain &chain);
276
277 bool WriteWalletFlags(const uint64_t flags);
279 bool TxnBegin();
281 bool TxnCommit();
283 bool TxnAbort();
284
285private:
286 std::unique_ptr<DatabaseBatch> m_batch;
288};
289
293
295using KeyFilterFn = std::function<bool(const std::string &)>;
296
298bool ReadKeyValue(CWallet *pwallet, CDataStream &ssKey, CDataStream &ssValue,
299 std::string &strType, std::string &strErr,
300 const KeyFilterFn &filter_fn = nullptr);
301
305std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase();
306
308std::unique_ptr<WalletDatabase> CreateMockWalletDatabase();
309
310#endif // BITCOIN_WALLET_WALLETDB_H
int flags
Double ended buffer combining vector and stream-like interfaces.
Definition streams.h:177
static const int VERSION_HD_BASE
Definition walletdb.h:94
SERIALIZE_METHODS(CHDChain, obj)
Definition walletdb.h:101
uint32_t nInternalChainCounter
Definition walletdb.h:90
CHDChain()
Definition walletdb.h:99
bool operator==(const CHDChain &chain) const
Definition walletdb.h:115
CKeyID seed_id
seed hash160
Definition walletdb.h:92
void SetNull()
Definition walletdb.h:108
static const int VERSION_HD_CHAIN_SPLIT
Definition walletdb.h:95
int nVersion
Definition walletdb.h:97
static const int CURRENT_VERSION
Definition walletdb.h:96
uint32_t nExternalChainCounter
Definition walletdb.h:89
A reference to a CKey: the Hash160 of its serialized public key.
Definition pubkey.h:22
static const int VERSION_WITH_KEY_ORIGIN
Definition walletdb.h:124
KeyOriginInfo key_origin
Definition walletdb.h:135
bool has_key_origin
Whether the key_origin is useful.
Definition walletdb.h:137
static const int VERSION_BASIC
Definition walletdb.h:122
static const int CURRENT_VERSION
Definition walletdb.h:125
void SetNull()
Definition walletdb.h:156
std::string hdKeypath
Definition walletdb.h:131
SERIALIZE_METHODS(CKeyMetadata, obj)
Definition walletdb.h:145
CKeyMetadata(int64_t nCreateTime_)
Definition walletdb.h:140
int64_t nCreateTime
Definition walletdb.h:128
CKeyID hd_seed_id
Definition walletdb.h:133
static const int VERSION_WITH_HDDATA
Definition walletdb.h:123
A key from a CWallet's keypool.
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key.
Definition crypter.h:31
An encapsulated public key.
Definition pubkey.h:31
Serialized script, used inside transaction inputs and outputs.
Definition script.h:431
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition wallet.h:254
A transaction with a bunch of additional info that only the owner cares about.
Definition transaction.h:65
Access to the wallet database.
Definition walletdb.h:175
bool TxnCommit()
Commit current transaction.
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition walletdb.cpp:253
bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id, bool internal)
Definition walletdb.cpp:213
bool WriteDescriptorDerivedCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index, uint32_t der_index)
Definition walletdb.cpp:258
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition walletdb.cpp:154
bool WriteName(const CTxDestination &address, const std::string &strName)
Definition walletdb.cpp:60
bool ErasePool(int64_t nPool)
Definition walletdb.cpp:205
WalletBatch(const WalletBatch &)=delete
bool WritePurpose(const CTxDestination &address, const std::string &purpose)
Definition walletdb.cpp:81
bool WriteMinVersion(int nVersion)
Definition walletdb.cpp:209
bool WriteDescriptorParentCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index)
Definition walletdb.cpp:270
static bool IsKeyType(const std::string &strType)
Definition walletdb.cpp:769
bool ErasePurpose(const CTxDestination &address)
Definition walletdb.cpp:91
bool WriteCScript(const uint160 &hash, const CScript &redeemScript)
Definition walletdb.cpp:159
bool EraseWatchOnly(const CScript &script)
Definition walletdb.cpp:172
bool EraseDestData(const CTxDestination &address, const std::string &key)
Erase destination data tuple from wallet database.
bool WriteDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const CPrivKey &privkey)
Definition walletdb.cpp:226
bool ReadPool(int64_t nPool, CKeyPool &keypool)
Definition walletdb.cpp:197
bool WriteWalletFlags(const uint64_t flags)
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition walletdb.h:178
bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey, const CKeyMetadata &keyMeta)
Definition walletdb.cpp:113
std::unique_ptr< DatabaseBatch > m_batch
Definition walletdb.h:286
bool WriteHDChain(const CHDChain &chain)
write the hdchain model (external chain child index counter)
bool ReadBestBlock(CBlockLocator &locator)
Definition walletdb.cpp:186
DBErrors FindWalletTx(std::vector< TxId > &txIds, std::list< CWalletTx > &vWtx)
Definition walletdb.cpp:956
bool WriteKeyMetadata(const CKeyMetadata &meta, const CPubKey &pubkey, const bool overwrite)
Definition walletdb.cpp:107
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition walletdb.cpp:193
bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal)
Definition walletdb.cpp:220
bool WriteTx(const CWalletTx &wtx)
Definition walletdb.cpp:99
bool EraseIC(const K &key)
Definition walletdb.h:189
bool TxnBegin()
Begin a new transaction.
bool TxnAbort()
Abort current transaction.
bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta)
Definition walletdb.cpp:164
bool EraseName(const CTxDestination &address)
Definition walletdb.cpp:70
bool WritePool(int64_t nPool, const CKeyPool &keypool)
Definition walletdb.cpp:201
bool WriteCryptedKey(const CPubKey &vchPubKey, const std::vector< uint8_t > &vchCryptedSecret, const CKeyMetadata &keyMeta)
Definition walletdb.cpp:129
bool WriteBestBlock(const CBlockLocator &locator)
Definition walletdb.cpp:179
bool WriteCryptedDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const std::vector< uint8_t > &secret)
Definition walletdb.cpp:240
bool EraseTx(uint256 hash)
Definition walletdb.cpp:103
DBErrors ZapSelectTx(std::vector< TxId > &txIdsIn, std::vector< TxId > &txIdsOut)
DBErrors LoadWallet(CWallet *pwallet)
Definition walletdb.cpp:774
bool WriteDestData(const CTxDestination &address, const std::string &key, const std::string &value)
Write destination data key,value tuple to database.
WalletBatch(WalletDatabase &database, bool _fFlushOnClose=true)
Definition walletdb.h:201
WalletBatch & operator=(const WalletBatch &)=delete
WalletDatabase & m_database
Definition walletdb.h:287
An instance of this class represents one database.
Definition db.h:100
std::atomic< unsigned int > nUpdateCounter
Definition db.h:156
virtual void IncrementUpdateCounter()=0
Descriptor with some wallet metadata.
Definition walletutil.h:80
void SetNull()
Definition uint256.h:41
160-bit opaque blob.
Definition uint256.h:117
256-bit opaque blob.
Definition uint256.h:129
std::vector< uint8_t, secure_allocator< uint8_t > > CPrivKey
secure_allocator is defined in allocators.h CPrivKey is a serialized private key, with all parameters...
Definition key.h:22
const std::string ORDERPOSNEXT
Definition walletdb.cpp:42
const std::string DEFAULTKEY
Definition walletdb.cpp:32
const std::string CSCRIPT
Definition walletdb.cpp:31
const std::string TX
Definition walletdb.cpp:46
const std::string BESTBLOCK_NOMERKLE
Definition walletdb.cpp:28
const std::string DESTDATA
Definition walletdb.cpp:33
const std::string WALLETDESCRIPTORKEY
Definition walletdb.cpp:51
const std::string SETTINGS
Definition walletdb.cpp:45
const std::string POOL
Definition walletdb.cpp:43
const std::string WALLETDESCRIPTORCKEY
Definition walletdb.cpp:50
const std::string ACTIVEEXTERNALSPK
Definition walletdb.cpp:26
const std::string NAME
Definition walletdb.cpp:40
const std::string MINVERSION
Definition walletdb.cpp:39
const std::string HDCHAIN
Definition walletdb.cpp:35
const std::string WALLETDESCRIPTORCACHE
Definition walletdb.cpp:49
const std::string WALLETDESCRIPTOR
Definition walletdb.cpp:48
const std::string VERSION
Definition walletdb.cpp:47
const std::string KEY
Definition walletdb.cpp:37
const std::string KEYMETA
Definition walletdb.cpp:36
const std::string WATCHMETA
Definition walletdb.cpp:52
const std::string ACENTRY
Definition walletdb.cpp:25
const std::string MASTER_KEY
Definition walletdb.cpp:38
const std::string ACTIVEINTERNALSPK
Definition walletdb.cpp:27
const std::string FLAGS
Definition walletdb.cpp:34
const std::string WATCHS
Definition walletdb.cpp:53
const std::string PURPOSE
Definition walletdb.cpp:44
const std::string OLD_KEY
Definition walletdb.cpp:41
const std::string CRYPTED_KEY
Definition walletdb.cpp:30
const std::string BESTBLOCK
Definition walletdb.cpp:29
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
#define READWRITE(...)
Definition serialize.h:166
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition standard.h:85
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition block.h:105
void clear()
Definition keyorigin.h:26
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
bool ReadKeyValue(CWallet *pwallet, CDataStream &ssKey, CDataStream &ssValue, std::string &strType, std::string &strErr, const KeyFilterFn &filter_fn=nullptr)
Unserialize a given Key-Value pair and load it into the wallet.
Definition walletdb.cpp:760
DBErrors
Error statuses for the wallet database.
Definition walletdb.h:45
@ NONCRITICAL_ERROR
std::function< bool(const std::string &)> KeyFilterFn
Callback for filtering key types to deserialize in ReadKeyValue.
Definition walletdb.h:295
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
static const bool DEFAULT_FLUSHWALLET
Overview of wallet database classes:
Definition walletdb.h:33
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.