Bitcoin ABC  0.22.13
P2P Digital Currency
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 <amount.h>
11 #include <key.h>
12 #include <script/sign.h>
13 #include <script/standard.h> // for CTxDestination
14 #include <wallet/bdb.h>
15 #include <wallet/walletutil.h>
16 
17 #include <cstdint>
18 #include <string>
19 #include <vector>
20 
34 static const bool DEFAULT_FLUSHWALLET = true;
35 
36 struct CBlockLocator;
37 class CKeyPool;
38 class CMasterKey;
39 class CScript;
40 class CWallet;
41 class CWalletTx;
42 class uint160;
43 class uint256;
44 
47 
49 enum class DBErrors {
50  LOAD_OK,
51  CORRUPT,
53  TOO_NEW,
54  LOAD_FAIL,
56 };
57 
58 namespace DBKeys {
59 extern const std::string ACENTRY;
60 extern const std::string ACTIVEEXTERNALSPK;
61 extern const std::string ACTIVEINTERNALSPK;
62 extern const std::string BESTBLOCK;
63 extern const std::string BESTBLOCK_NOMERKLE;
64 extern const std::string CRYPTED_KEY;
65 extern const std::string CSCRIPT;
66 extern const std::string DEFAULTKEY;
67 extern const std::string DESTDATA;
68 extern const std::string FLAGS;
69 extern const std::string HDCHAIN;
70 extern const std::string KEY;
71 extern const std::string KEYMETA;
72 extern const std::string MASTER_KEY;
73 extern const std::string MINVERSION;
74 extern const std::string NAME;
75 extern const std::string OLD_KEY;
76 extern const std::string ORDERPOSNEXT;
77 extern const std::string POOL;
78 extern const std::string PURPOSE;
79 extern const std::string SETTINGS;
80 extern const std::string TX;
81 extern const std::string VERSION;
82 extern const std::string WALLETDESCRIPTOR;
83 extern const std::string WALLETDESCRIPTORCACHE;
84 extern const std::string WALLETDESCRIPTORCKEY;
85 extern const std::string WALLETDESCRIPTORKEY;
86 extern const std::string WATCHMETA;
87 extern const std::string WATCHS;
88 } // namespace DBKeys
89 
90 /* simple HD chain data model */
91 class CHDChain {
92 public:
97 
98  static const int VERSION_HD_BASE = 1;
99  static const int VERSION_HD_CHAIN_SPLIT = 2;
100  static const int CURRENT_VERSION = VERSION_HD_CHAIN_SPLIT;
101  int nVersion;
102 
103  CHDChain() { SetNull(); }
105  template <typename Stream, typename Operation>
106  inline void SerializationOp(Stream &s, Operation ser_action) {
107  READWRITE(this->nVersion);
108  READWRITE(nExternalChainCounter);
109  READWRITE(seed_id);
110  if (this->nVersion >= VERSION_HD_CHAIN_SPLIT) {
111  READWRITE(nInternalChainCounter);
112  }
113  }
114 
115  void SetNull() {
116  nVersion = CHDChain::CURRENT_VERSION;
117  nExternalChainCounter = 0;
118  nInternalChainCounter = 0;
119  seed_id.SetNull();
120  }
121 };
122 
124 public:
125  static const int VERSION_BASIC = 1;
126  static const int VERSION_WITH_HDDATA = 10;
127  static const int VERSION_WITH_KEY_ORIGIN = 12;
128  static const int CURRENT_VERSION = VERSION_WITH_KEY_ORIGIN;
129  int nVersion;
130  // 0 means unknown.
131  int64_t nCreateTime;
132  // optional HD/bip32 keypath. Still used to determine whether a key is a
133  // seed. Also kept for backwards compatibility
134  std::string hdKeypath;
135  // Id of the HD seed used to derive this key.
137  // Key origin info with path and fingerprint
140  bool has_key_origin = false;
141 
142  CKeyMetadata() { SetNull(); }
143  explicit CKeyMetadata(int64_t nCreateTime_) {
144  SetNull();
145  nCreateTime = nCreateTime_;
146  }
147 
149 
150  template <typename Stream, typename Operation>
151  inline void SerializationOp(Stream &s, Operation ser_action) {
152  READWRITE(this->nVersion);
153  READWRITE(nCreateTime);
154  if (this->nVersion >= VERSION_WITH_HDDATA) {
155  READWRITE(hdKeypath);
156  READWRITE(hd_seed_id);
157  }
158  if (this->nVersion >= VERSION_WITH_KEY_ORIGIN) {
159  READWRITE(key_origin);
160  READWRITE(has_key_origin);
161  }
162  }
163 
164  void SetNull() {
166  nCreateTime = 0;
167  hdKeypath.clear();
168  hd_seed_id.SetNull();
169  key_origin.clear();
170  has_key_origin = false;
171  }
172 };
173 
183 class WalletBatch {
184 private:
185  template <typename K, typename T>
186  bool WriteIC(const K &key, const T &value, bool fOverwrite = true) {
187  if (!m_batch.Write(key, value, fOverwrite)) {
188  return false;
189  }
190  m_database.IncrementUpdateCounter();
191  if (m_database.nUpdateCounter % 1000 == 0) {
192  m_batch.Flush();
193  }
194  return true;
195  }
196 
197  template <typename K> bool EraseIC(const K &key) {
198  if (!m_batch.Erase(key)) {
199  return false;
200  }
201  m_database.IncrementUpdateCounter();
202  if (m_database.nUpdateCounter % 1000 == 0) {
203  m_batch.Flush();
204  }
205  return true;
206  }
207 
208 public:
209  explicit WalletBatch(WalletDatabase &database, const char *pszMode = "r+",
210  bool _fFlushOnClose = true)
211  : m_batch(database, pszMode, _fFlushOnClose), m_database(database) {}
212  WalletBatch(const WalletBatch &) = delete;
213  WalletBatch &operator=(const WalletBatch &) = delete;
214 
215  bool WriteName(const CTxDestination &address, const std::string &strName);
216  bool EraseName(const CTxDestination &address);
217 
218  bool WritePurpose(const CTxDestination &address,
219  const std::string &purpose);
220  bool ErasePurpose(const CTxDestination &address);
221 
222  bool WriteTx(const CWalletTx &wtx);
223  bool EraseTx(uint256 hash);
224 
225  bool WriteKeyMetadata(const CKeyMetadata &meta, const CPubKey &pubkey,
226  const bool overwrite);
227  bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey,
228  const CKeyMetadata &keyMeta);
229  bool WriteCryptedKey(const CPubKey &vchPubKey,
230  const std::vector<uint8_t> &vchCryptedSecret,
231  const CKeyMetadata &keyMeta);
232  bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey);
233 
234  bool WriteCScript(const uint160 &hash, const CScript &redeemScript);
235 
236  bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
237  bool EraseWatchOnly(const CScript &script);
238 
239  bool WriteBestBlock(const CBlockLocator &locator);
240  bool ReadBestBlock(CBlockLocator &locator);
241 
242  bool WriteOrderPosNext(int64_t nOrderPosNext);
243 
244  bool ReadPool(int64_t nPool, CKeyPool &keypool);
245  bool WritePool(int64_t nPool, const CKeyPool &keypool);
246  bool ErasePool(int64_t nPool);
247 
248  bool WriteMinVersion(int nVersion);
249 
250  bool WriteDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey,
251  const CPrivKey &privkey);
252  bool WriteCryptedDescriptorKey(const uint256 &desc_id,
253  const CPubKey &pubkey,
254  const std::vector<uint8_t> &secret);
255  bool WriteDescriptor(const uint256 &desc_id,
256  const WalletDescriptor &descriptor);
257  bool WriteDescriptorDerivedCache(const CExtPubKey &xpub,
258  const uint256 &desc_id,
259  uint32_t key_exp_index,
260  uint32_t der_index);
261  bool WriteDescriptorParentCache(const CExtPubKey &xpub,
262  const uint256 &desc_id,
263  uint32_t key_exp_index);
264 
266  bool WriteDestData(const CTxDestination &address, const std::string &key,
267  const std::string &value);
269  bool EraseDestData(const CTxDestination &address, const std::string &key);
270 
271  bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id,
272  bool internal);
273 
274  DBErrors LoadWallet(CWallet *pwallet);
275  DBErrors FindWalletTx(std::vector<TxId> &txIds,
276  std::vector<CWalletTx> &vWtx);
277  DBErrors ZapWalletTx(std::vector<CWalletTx> &vWtx);
278  DBErrors ZapSelectTx(std::vector<TxId> &txIdsIn,
279  std::vector<TxId> &txIdsOut);
280  /* Function to determine if a certain KV/key-type is a key (cryptographical
281  * key) type */
282  static bool IsKeyType(const std::string &strType);
283  /* verifies the database environment */
284  static bool VerifyEnvironment(const fs::path &wallet_path,
285  bilingual_str &errorStr);
286  /* verifies the database file */
287  static bool VerifyDatabaseFile(const fs::path &wallet_path,
288  bilingual_str &errorStr);
289 
291  bool WriteHDChain(const CHDChain &chain);
292 
293  bool WriteWalletFlags(const uint64_t flags);
295  bool TxnBegin();
297  bool TxnCommit();
299  bool TxnAbort();
300 
301 private:
304 };
305 
308 void MaybeCompactWalletDB();
309 
311 bool ReadKeyValue(CWallet *pwallet, CDataStream &ssKey, CDataStream &ssValue,
312  std::string &strType, std::string &strErr);
313 
315 bool IsWalletLoaded(const fs::path &wallet_path);
316 
317 #endif // BITCOIN_WALLET_WALLETDB_H
static const int CURRENT_VERSION
Definition: walletdb.h:100
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:143
bool EraseIC(const K &key)
Definition: walletdb.h:197
const std::string POOL
Definition: walletdb.cpp:40
const std::string FLAGS
Definition: walletdb.cpp:31
std::string hdKeypath
Definition: walletdb.h:134
void SetNull()
Definition: uint256.h:35
const std::string NAME
Definition: walletdb.cpp:37
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:110
bool IsWalletLoaded(const fs::path &wallet_path)
Return whether a wallet database is currently loaded.
Definition: walletdb.cpp:1004
const std::string SETTINGS
Definition: walletdb.cpp:42
Bilingual messages:
Definition: translation.h:17
const std::string WALLETDESCRIPTORCKEY
Definition: walletdb.cpp:47
const std::string CRYPTED_KEY
Definition: walletdb.cpp:27
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:186
const std::string DEFAULTKEY
Definition: walletdb.cpp:29
std::shared_ptr< CWallet > LoadWallet(const CChainParams &chainParams, interfaces::Chain &chain, const std::string &name, bilingual_str &error, std::vector< bilingual_str > &warnings)
Definition: dummywallet.cpp:57
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:31
const std::string KEYMETA
Definition: walletdb.cpp:33
An instance of this class represents one database.
Definition: bdb.h:97
uint32_t nExternalChainCounter
Definition: walletdb.h:93
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:196
WalletBatch(WalletDatabase &database, const char *pszMode="r+", bool _fFlushOnClose=true)
Definition: walletdb.h:209
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:49
ADD_SERIALIZE_METHODS
Definition: walletdb.h:104
const std::string ORDERPOSNEXT
Definition: walletdb.cpp:39
const std::string MASTER_KEY
Definition: walletdb.cpp:35
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
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletdb.h:106
void clear()
Definition: keyorigin.h:29
Access to the wallet database.
Definition: walletdb.h:183
const std::string OLD_KEY
Definition: walletdb.cpp:38
const std::string HDCHAIN
Definition: walletdb.cpp:32
const std::string WALLETDESCRIPTOR
Definition: walletdb.cpp:45
static const int CURRENT_VERSION
Definition: walletdb.h:128
const std::string ACTIVEINTERNALSPK
Definition: walletdb.cpp:24
WalletDatabase & m_database
Definition: walletdb.h:303
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:934
An encapsulated public key.
Definition: pubkey.h:31
const std::string WATCHMETA
Definition: walletdb.cpp:49
ADD_SERIALIZE_METHODS
Definition: walletdb.h:148
RAII class that provides access to a Berkeley database.
Definition: bdb.h:210
const std::string WALLETDESCRIPTORKEY
Definition: walletdb.cpp:48
const std::string DESTDATA
Definition: walletdb.cpp:30
Descriptor with some wallet metadata.
Definition: walletutil.h:99
KeyOriginInfo key_origin
Definition: walletdb.h:138
int64_t nCreateTime
Definition: walletdb.h:131
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:280
int nVersion
Definition: walletdb.h:129
const std::string WALLETDESCRIPTORCACHE
Definition: walletdb.cpp:46
int flags
Definition: bitcoin-tx.cpp:529
256-bit opaque blob.
Definition: uint256.h:120
BerkeleyBatch m_batch
Definition: walletdb.h:302
const std::string ACENTRY
Definition: walletdb.cpp:22
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:21
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
const std::string MINVERSION
Definition: walletdb.cpp:36
static const bool DEFAULT_FLUSHWALLET
Overview of wallet database classes:
Definition: walletdb.h:34
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
const std::string ACTIVEEXTERNALSPK
Definition: walletdb.cpp:23
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:679
160-bit opaque blob.
Definition: uint256.h:108
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletdb.h:151
CHDChain()
Definition: walletdb.h:103
const std::string WATCHS
Definition: walletdb.cpp:50
bool ReadKeyValue(CWallet *pwallet, CDataStream &ssKey, CDataStream &ssValue, std::string &strType, std::string &strErr)
Unserialize a given Key-Value pair and load it into the wallet.
Definition: walletdb.cpp:642
void SetNull()
Definition: walletdb.h:115
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:29
CKeyID hd_seed_id
Definition: walletdb.h:136
const std::string BESTBLOCK
Definition: walletdb.cpp:26
const std::string CSCRIPT
Definition: walletdb.cpp:28
CKeyID seed_id
seed hash160
Definition: walletdb.h:96
#define READWRITE(...)
Definition: serialize.h:191
const std::string BESTBLOCK_NOMERKLE
Definition: walletdb.cpp:25
const std::string KEY
Definition: walletdb.cpp:34
int nVersion
Definition: walletdb.h:101
void SetNull()
Definition: walletdb.h:164
boost::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:87
uint32_t nInternalChainCounter
Definition: walletdb.h:94
A key from a CWallet&#39;s keypool.
const std::string PURPOSE
Definition: walletdb.cpp:41