Bitcoin Core  22.0.0
P2P Digital Currency
walletdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <amount.h>
10 #include <script/sign.h>
11 #include <wallet/db.h>
12 #include <wallet/walletutil.h>
13 #include <key.h>
14 
15 #include <stdint.h>
16 #include <string>
17 #include <vector>
18 
31 static const bool DEFAULT_FLUSHWALLET = true;
32 
33 struct CBlockLocator;
34 class CKeyPool;
35 class CMasterKey;
36 class CScript;
37 class CWallet;
38 class CWalletTx;
39 class uint160;
40 class uint256;
41 
43 enum class DBErrors
44 {
45  LOAD_OK,
46  CORRUPT,
48  TOO_NEW,
49  LOAD_FAIL,
51 };
52 
53 namespace DBKeys {
54 extern const std::string ACENTRY;
55 extern const std::string ACTIVEEXTERNALSPK;
56 extern const std::string ACTIVEINTERNALSPK;
57 extern const std::string BESTBLOCK;
58 extern const std::string BESTBLOCK_NOMERKLE;
59 extern const std::string CRYPTED_KEY;
60 extern const std::string CSCRIPT;
61 extern const std::string DEFAULTKEY;
62 extern const std::string DESTDATA;
63 extern const std::string FLAGS;
64 extern const std::string HDCHAIN;
65 extern const std::string KEY;
66 extern const std::string KEYMETA;
67 extern const std::string MASTER_KEY;
68 extern const std::string MINVERSION;
69 extern const std::string NAME;
70 extern const std::string OLD_KEY;
71 extern const std::string ORDERPOSNEXT;
72 extern const std::string POOL;
73 extern const std::string PURPOSE;
74 extern const std::string SETTINGS;
75 extern const std::string TX;
76 extern const std::string VERSION;
77 extern const std::string WALLETDESCRIPTOR;
78 extern const std::string WALLETDESCRIPTORCKEY;
79 extern const std::string WALLETDESCRIPTORKEY;
80 extern const std::string WATCHMETA;
81 extern const std::string WATCHS;
82 } // namespace DBKeys
83 
84 /* simple HD chain data model */
85 class CHDChain
86 {
87 public:
91 
92  static const int VERSION_HD_BASE = 1;
93  static const int VERSION_HD_CHAIN_SPLIT = 2;
95  int nVersion;
96 
97  CHDChain() { SetNull(); }
98 
100  {
101  READWRITE(obj.nVersion, obj.nExternalChainCounter, obj.seed_id);
102  if (obj.nVersion >= VERSION_HD_CHAIN_SPLIT) {
103  READWRITE(obj.nInternalChainCounter);
104  }
105  }
106 
107  void SetNull()
108  {
112  seed_id.SetNull();
113  }
114 
115  bool operator==(const CHDChain& chain) const
116  {
117  return seed_id == chain.seed_id;
118  }
119 };
120 
122 {
123 public:
124  static const int VERSION_BASIC=1;
125  static const int VERSION_WITH_HDDATA=10;
126  static const int VERSION_WITH_KEY_ORIGIN = 12;
128  int nVersion;
129  int64_t nCreateTime; // 0 means unknown
130  std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility
131  CKeyID hd_seed_id; //id of the HD seed used to derive this key
132  KeyOriginInfo key_origin; // Key origin info with path and fingerprint
133  bool has_key_origin = false;
134 
136  {
137  SetNull();
138  }
139  explicit CKeyMetadata(int64_t nCreateTime_)
140  {
141  SetNull();
142  nCreateTime = nCreateTime_;
143  }
144 
146  {
147  READWRITE(obj.nVersion, obj.nCreateTime);
148  if (obj.nVersion >= VERSION_WITH_HDDATA) {
149  READWRITE(obj.hdKeypath, obj.hd_seed_id);
150  }
151  if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN)
152  {
153  READWRITE(obj.key_origin);
154  READWRITE(obj.has_key_origin);
155  }
156  }
157 
158  void SetNull()
159  {
161  nCreateTime = 0;
162  hdKeypath.clear();
164  key_origin.clear();
165  has_key_origin = false;
166  }
167 };
168 
177 {
178 private:
179  template <typename K, typename T>
180  bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
181  {
182  if (!m_batch->Write(key, value, fOverwrite)) {
183  return false;
184  }
186  if (m_database.nUpdateCounter % 1000 == 0) {
187  m_batch->Flush();
188  }
189  return true;
190  }
191 
192  template <typename K>
193  bool EraseIC(const K& key)
194  {
195  if (!m_batch->Erase(key)) {
196  return false;
197  }
199  if (m_database.nUpdateCounter % 1000 == 0) {
200  m_batch->Flush();
201  }
202  return true;
203  }
204 
205 public:
206  explicit WalletBatch(WalletDatabase &database, bool _fFlushOnClose = true) :
207  m_batch(database.MakeBatch(_fFlushOnClose)),
208  m_database(database)
209  {
210  }
211  WalletBatch(const WalletBatch&) = delete;
212  WalletBatch& operator=(const WalletBatch&) = delete;
213 
214  bool WriteName(const std::string& strAddress, const std::string& strName);
215  bool EraseName(const std::string& strAddress);
216 
217  bool WritePurpose(const std::string& strAddress, const std::string& purpose);
218  bool ErasePurpose(const std::string& strAddress);
219 
220  bool WriteTx(const CWalletTx& wtx);
221  bool EraseTx(uint256 hash);
222 
223  bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite);
224  bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
225  bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
226  bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
227 
228  bool WriteCScript(const uint160& hash, const CScript& redeemScript);
229 
230  bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
231  bool EraseWatchOnly(const CScript &script);
232 
233  bool WriteBestBlock(const CBlockLocator& locator);
234  bool ReadBestBlock(CBlockLocator& locator);
235 
236  bool WriteOrderPosNext(int64_t nOrderPosNext);
237 
238  bool ReadPool(int64_t nPool, CKeyPool& keypool);
239  bool WritePool(int64_t nPool, const CKeyPool& keypool);
240  bool ErasePool(int64_t nPool);
241 
242  bool WriteMinVersion(int nVersion);
243 
244  bool WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey);
245  bool WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret);
246  bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor);
247  bool WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index);
248  bool WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
249  bool WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
250  bool WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache);
251 
253  bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
255  bool EraseDestData(const std::string &address, const std::string &key);
256 
257  bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal);
258  bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal);
259 
260  DBErrors LoadWallet(CWallet* pwallet);
261  DBErrors FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWalletTx>& vWtx);
262  DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
263  /* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
264  static bool IsKeyType(const std::string& strType);
265 
267  bool WriteHDChain(const CHDChain& chain);
268 
269  bool WriteWalletFlags(const uint64_t flags);
271  bool TxnBegin();
273  bool TxnCommit();
275  bool TxnAbort();
276 private:
277  std::unique_ptr<DatabaseBatch> m_batch;
279 };
280 
282 void MaybeCompactWalletDB();
283 
285 using KeyFilterFn = std::function<bool(const std::string&)>;
286 
288 bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr);
289 
291 std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase();
292 
294 std::unique_ptr<WalletDatabase> CreateMockWalletDatabase();
295 
296 #endif // BITCOIN_WALLET_WALLETDB_H
bool TxnCommit()
Commit current transaction.
Definition: walletdb.cpp:1060
static const int CURRENT_VERSION
Definition: walletdb.h:94
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:139
bool WriteName(const std::string &strAddress, const std::string &strName)
Definition: walletdb.cpp:66
bool EraseIC(const K &key)
Definition: walletdb.h:193
bool EraseDestData(const std::string &address, const std::string &key)
Erase destination data tuple from wallet database.
Definition: walletdb.cpp:1039
const std::string POOL
Definition: walletdb.cpp:48
static const int VERSION_BASIC
Definition: walletdb.h:124
const std::string FLAGS
Definition: walletdb.cpp:39
std::string hdKeypath
Definition: walletdb.h:130
bool ErasePurpose(const std::string &strAddress)
Definition: walletdb.cpp:83
bool ReadBestBlock(CBlockLocator &locator)
Definition: walletdb.cpp:176
void SetNull()
Definition: uint256.h:39
const std::string NAME
Definition: walletdb.cpp:45
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:114
bool WriteCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:118
bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal)
Definition: walletdb.cpp:213
bool operator==(const CHDChain &chain) const
Definition: walletdb.h:115
const std::string SETTINGS
Definition: walletdb.cpp:50
const std::string WALLETDESCRIPTORCKEY
Definition: walletdb.cpp:56
bool WriteDescriptorDerivedCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index, uint32_t der_index)
Definition: walletdb.cpp:244
WalletBatch(WalletDatabase &database, bool _fFlushOnClose=true)
Definition: walletdb.h:206
const std::string CRYPTED_KEY
Definition: walletdb.cpp:35
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:180
const std::string DEFAULTKEY
Definition: walletdb.cpp:37
DBErrors ZapSelectTx(std::vector< uint256 > &vHashIn, std::vector< uint256 > &vHashOut)
Definition: walletdb.cpp:969
bool WriteMinVersion(int nVersion)
Definition: walletdb.cpp:202
bool WriteHDChain(const CHDChain &chain)
write the hdchain model (external chain child index counter)
Definition: walletdb.cpp:1045
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:239
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:33
bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta)
Definition: walletdb.cpp:154
const std::string KEYMETA
Definition: walletdb.cpp:41
bool WriteDescriptorCacheItems(const uint256 &desc_id, const DescriptorCache &cache)
Definition: walletdb.cpp:265
uint32_t nExternalChainCounter
Definition: walletdb.h:88
bool TxnBegin()
Begin a new transaction.
Definition: walletdb.cpp:1055
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
SERIALIZE_METHODS(CKeyMetadata, obj)
Definition: walletdb.h:145
bool WriteTx(const CWalletTx &wtx)
Definition: walletdb.cpp:88
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:737
bool WriteDescriptorParentCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index)
Definition: walletdb.cpp:251
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:43
const std::string ORDERPOSNEXT
Definition: walletdb.cpp:47
bool WriteWalletFlags(const uint64_t flags)
Definition: walletdb.cpp:1050
const std::string VERSION
Definition: walletdb.cpp:52
const std::string MASTER_KEY
Definition: walletdb.cpp:43
bool EraseWatchOnly(const CScript &script)
Definition: walletdb.cpp:162
void clear()
Definition: keyorigin.h:23
Access to the wallet database.
Definition: walletdb.h:176
bool WriteCryptedDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const std::vector< unsigned char > &secret)
Definition: walletdb.cpp:230
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:170
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
secure_allocator is defined in allocators.h CPrivKey is a serialized private key, with all parameters...
Definition: key.h:24
bool TxnAbort()
Abort current transaction.
Definition: walletdb.cpp:1065
const std::string OLD_KEY
Definition: walletdb.cpp:46
bool WriteDestData(const std::string &address, const std::string &key, const std::string &value)
Write destination data key,value tuple to database.
Definition: walletdb.cpp:1034
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1156
const std::string HDCHAIN
Definition: walletdb.cpp:40
static bool IsKeyType(const std::string &strType)
Definition: walletdb.cpp:731
const std::string WALLETDESCRIPTOR
Definition: walletdb.cpp:53
bool WriteCScript(const uint160 &hash, const CScript &redeemScript)
Definition: walletdb.cpp:149
static const int CURRENT_VERSION
Definition: walletdb.h:127
std::unique_ptr< DatabaseBatch > m_batch
Definition: walletdb.h:277
const std::string ACTIVEINTERNALSPK
Definition: walletdb.cpp:32
WalletDatabase & m_database
Definition: walletdb.h:278
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:1007
An encapsulated public key.
Definition: pubkey.h:32
const std::string WATCHMETA
Definition: walletdb.cpp:58
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:144
const std::string WALLETDESCRIPTORKEY
Definition: walletdb.cpp:57
const std::string DESTDATA
Definition: walletdb.cpp:38
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:182
Descriptor with some wallet metadata.
Definition: walletutil.h:75
KeyOriginInfo key_origin
Definition: walletdb.h:132
int64_t nCreateTime
Definition: walletdb.h:129
bool has_key_origin
Whether the key_origin is useful.
Definition: walletdb.h:133
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1150
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:68
bool WriteKeyMetadata(const CKeyMetadata &meta, const CPubKey &pubkey, const bool overwrite)
Definition: walletdb.cpp:98
SERIALIZE_METHODS(CHDChain, obj)
Definition: walletdb.h:99
int nVersion
Definition: walletdb.h:128
int flags
Definition: bitcoin-tx.cpp:512
bool ErasePool(int64_t nPool)
Definition: walletdb.cpp:197
bool WritePool(int64_t nPool, const CKeyPool &keypool)
Definition: walletdb.cpp:192
256-bit opaque blob.
Definition: uint256.h:124
const std::string ACENTRY
Definition: walletdb.cpp:30
bool WriteDescriptorLastHardenedCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index)
Definition: walletdb.cpp:258
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:148
bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id, bool internal)
Definition: walletdb.cpp:207
Cache for single descriptor&#39;s derived extended pubkeys.
Definition: descriptor.h:19
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
const std::string MINVERSION
Definition: walletdb.cpp:44
static const bool DEFAULT_FLUSHWALLET
Overview of wallet database classes:
Definition: walletdb.h:31
static const int VERSION_WITH_HDDATA
Definition: walletdb.h:125
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
const std::string ACTIVEEXTERNALSPK
Definition: walletdb.cpp:31
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:226
bool WritePurpose(const std::string &strAddress, const std::string &purpose)
Definition: walletdb.cpp:78
160-bit opaque blob.
Definition: uint256.h:113
static const int VERSION_WITH_KEY_ORIGIN
Definition: walletdb.h:126
bool ReadPool(int64_t nPool, CKeyPool &keypool)
Definition: walletdb.cpp:187
static const int VERSION_HD_CHAIN_SPLIT
Definition: walletdb.h:93
virtual void IncrementUpdateCounter()=0
CHDChain()
Definition: walletdb.h:97
bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:103
const std::string WATCHS
Definition: walletdb.cpp:59
void SetNull()
Definition: walletdb.h:107
CKeyID hd_seed_id
Definition: walletdb.h:131
const std::string BESTBLOCK
Definition: walletdb.cpp:34
const std::string CSCRIPT
Definition: walletdb.cpp:36
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:724
bool EraseTx(uint256 hash)
Definition: walletdb.cpp:93
DBErrors FindWalletTx(std::vector< uint256 > &vTxHash, std::list< CWalletTx > &vWtx)
Definition: walletdb.cpp:918
CKeyID seed_id
seed hash160
Definition: walletdb.h:90
#define READWRITE(...)
Definition: serialize.h:147
std::function< bool(const std::string &)> KeyFilterFn
Callback for filtering key types to deserialize in ReadKeyValue.
Definition: walletdb.h:285
const std::string BESTBLOCK_NOMERKLE
Definition: walletdb.cpp:33
const std::string TX
Definition: walletdb.cpp:51
const std::string KEY
Definition: walletdb.cpp:42
int nVersion
Definition: walletdb.h:95
void SetNull()
Definition: walletdb.h:158
static const int VERSION_HD_BASE
Definition: walletdb.h:92
WalletBatch & operator=(const WalletBatch &)=delete
uint32_t nInternalChainCounter
Definition: walletdb.h:89
bool WriteDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const CPrivKey &privkey)
Definition: walletdb.cpp:219
A key from a CWallet&#39;s keypool.
bool EraseName(const std::string &strAddress)
Definition: walletdb.cpp:71
const std::string PURPOSE
Definition: walletdb.cpp:49
An instance of this class represents one database.
Definition: db.h:103