Bitcoin Core  0.19.99
P2P Digital Currency
walletdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 <key.h>
13 
14 #include <stdint.h>
15 #include <string>
16 #include <vector>
17 
30 static const bool DEFAULT_FLUSHWALLET = true;
31 
32 struct CBlockLocator;
33 class CKeyPool;
34 class CMasterKey;
35 class CScript;
36 class CWallet;
37 class CWalletTx;
38 class uint160;
39 class uint256;
40 
43 
45 enum class DBErrors
46 {
47  LOAD_OK,
48  CORRUPT,
50  TOO_NEW,
51  LOAD_FAIL,
53 };
54 
55 namespace DBKeys {
56 extern const std::string ACENTRY;
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 WATCHMETA;
78 extern const std::string WATCHS;
79 } // namespace DBKeys
80 
81 /* simple HD chain data model */
82 class CHDChain
83 {
84 public:
88 
89  static const int VERSION_HD_BASE = 1;
90  static const int VERSION_HD_CHAIN_SPLIT = 2;
91  static const int CURRENT_VERSION = VERSION_HD_CHAIN_SPLIT;
92  int nVersion;
93 
94  CHDChain() { SetNull(); }
96  template <typename Stream, typename Operation>
97  inline void SerializationOp(Stream& s, Operation ser_action)
98  {
99  READWRITE(this->nVersion);
100  READWRITE(nExternalChainCounter);
101  READWRITE(seed_id);
102  if (this->nVersion >= VERSION_HD_CHAIN_SPLIT)
103  READWRITE(nInternalChainCounter);
104  }
105 
106  void SetNull()
107  {
108  nVersion = CHDChain::CURRENT_VERSION;
109  nExternalChainCounter = 0;
110  nInternalChainCounter = 0;
111  seed_id.SetNull();
112  }
113 };
114 
116 {
117 public:
118  static const int VERSION_BASIC=1;
119  static const int VERSION_WITH_HDDATA=10;
120  static const int VERSION_WITH_KEY_ORIGIN = 12;
121  static const int CURRENT_VERSION=VERSION_WITH_KEY_ORIGIN;
122  int nVersion;
123  int64_t nCreateTime; // 0 means unknown
124  std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility
125  CKeyID hd_seed_id; //id of the HD seed used to derive this key
126  KeyOriginInfo key_origin; // Key origin info with path and fingerprint
127  bool has_key_origin = false;
128 
130  {
131  SetNull();
132  }
133  explicit CKeyMetadata(int64_t nCreateTime_)
134  {
135  SetNull();
136  nCreateTime = nCreateTime_;
137  }
138 
140 
141  template <typename Stream, typename Operation>
142  inline void SerializationOp(Stream& s, Operation ser_action) {
143  READWRITE(this->nVersion);
144  READWRITE(nCreateTime);
145  if (this->nVersion >= VERSION_WITH_HDDATA)
146  {
147  READWRITE(hdKeypath);
148  READWRITE(hd_seed_id);
149  }
150  if (this->nVersion >= VERSION_WITH_KEY_ORIGIN)
151  {
152  READWRITE(key_origin);
153  READWRITE(has_key_origin);
154  }
155  }
156 
157  void SetNull()
158  {
160  nCreateTime = 0;
161  hdKeypath.clear();
162  hd_seed_id.SetNull();
163  key_origin.clear();
164  has_key_origin = false;
165  }
166 };
167 
176 {
177 private:
178  template <typename K, typename T>
179  bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
180  {
181  if (!m_batch.Write(key, value, fOverwrite)) {
182  return false;
183  }
184  m_database.IncrementUpdateCounter();
185  if (m_database.nUpdateCounter % 1000 == 0) {
186  m_batch.Flush();
187  }
188  return true;
189  }
190 
191  template <typename K>
192  bool EraseIC(const K& key)
193  {
194  if (!m_batch.Erase(key)) {
195  return false;
196  }
197  m_database.IncrementUpdateCounter();
198  if (m_database.nUpdateCounter % 1000 == 0) {
199  m_batch.Flush();
200  }
201  return true;
202  }
203 
204 public:
205  explicit WalletBatch(WalletDatabase& database, const char* pszMode = "r+", bool _fFlushOnClose = true) :
206  m_batch(database, pszMode, _fFlushOnClose),
207  m_database(database)
208  {
209  }
210  WalletBatch(const WalletBatch&) = delete;
211  WalletBatch& operator=(const WalletBatch&) = delete;
212 
213  bool WriteName(const std::string& strAddress, const std::string& strName);
214  bool EraseName(const std::string& strAddress);
215 
216  bool WritePurpose(const std::string& strAddress, const std::string& purpose);
217  bool ErasePurpose(const std::string& strAddress);
218 
219  bool WriteTx(const CWalletTx& wtx);
220  bool EraseTx(uint256 hash);
221 
222  bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite);
223  bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
224  bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
225  bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
226 
227  bool WriteCScript(const uint160& hash, const CScript& redeemScript);
228 
229  bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
230  bool EraseWatchOnly(const CScript &script);
231 
232  bool WriteBestBlock(const CBlockLocator& locator);
233  bool ReadBestBlock(CBlockLocator& locator);
234 
235  bool WriteOrderPosNext(int64_t nOrderPosNext);
236 
237  bool ReadPool(int64_t nPool, CKeyPool& keypool);
238  bool WritePool(int64_t nPool, const CKeyPool& keypool);
239  bool ErasePool(int64_t nPool);
240 
241  bool WriteMinVersion(int nVersion);
242 
244  bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
246  bool EraseDestData(const std::string &address, const std::string &key);
247 
248  DBErrors LoadWallet(CWallet* pwallet);
249  DBErrors FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
250  DBErrors ZapWalletTx(std::vector<CWalletTx>& vWtx);
251  DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
252  /* Try to (very carefully!) recover wallet database (with a possible key type filter) */
253  static bool Recover(const fs::path& wallet_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
254  /* Recover convenience-function to bypass the key filter callback, called when verify fails, recovers everything */
255  static bool Recover(const fs::path& wallet_path, std::string& out_backup_filename);
256  /* Recover filter (used as callback), will only let keys (cryptographical keys) as KV/key-type pass through */
257  static bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue);
258  /* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
259  static bool IsKeyType(const std::string& strType);
260  /* verifies the database environment */
261  static bool VerifyEnvironment(const fs::path& wallet_path, std::string& errorStr);
262  /* verifies the database file */
263  static bool VerifyDatabaseFile(const fs::path& wallet_path, std::vector<std::string>& warnings, std::string& errorStr);
264 
266  bool WriteHDChain(const CHDChain& chain);
267 
268  bool WriteWalletFlags(const uint64_t flags);
270  bool TxnBegin();
272  bool TxnCommit();
274  bool TxnAbort();
275 private:
278 };
279 
281 void MaybeCompactWalletDB();
282 
283 #endif // BITCOIN_WALLET_WALLETDB_H
static const int CURRENT_VERSION
Definition: walletdb.h:91
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:133
bool EraseIC(const K &key)
Definition: walletdb.h:192
const std::string POOL
Definition: walletdb.cpp:39
const std::string FLAGS
Definition: walletdb.cpp:30
std::string hdKeypath
Definition: walletdb.h:124
void SetNull()
Definition: uint256.h:38
const std::string NAME
Definition: walletdb.cpp:36
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:126
const std::string SETTINGS
Definition: walletdb.cpp:41
const std::string CRYPTED_KEY
Definition: walletdb.cpp:26
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:179
const std::string DEFAULTKEY
Definition: walletdb.cpp:28
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:33
const std::string KEYMETA
Definition: walletdb.cpp:32
An instance of this class represents one database.
Definition: db.h:111
uint32_t nExternalChainCounter
Definition: walletdb.h:85
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:201
WalletBatch(WalletDatabase &database, const char *pszMode="r+", bool _fFlushOnClose=true)
Definition: walletdb.h:205
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:45
ADD_SERIALIZE_METHODS
Definition: walletdb.h:95
const std::string ORDERPOSNEXT
Definition: walletdb.cpp:38
const std::string MASTER_KEY
Definition: walletdb.cpp:34
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletdb.h:97
void clear()
Definition: keyorigin.h:29
Access to the wallet database.
Definition: walletdb.h:175
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
const std::string OLD_KEY
Definition: walletdb.cpp:37
const std::string HDCHAIN
Definition: walletdb.cpp:31
static const int CURRENT_VERSION
Definition: walletdb.h:121
WalletDatabase & m_database
Definition: walletdb.h:277
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:662
An encapsulated public key.
Definition: pubkey.h:30
const std::string WATCHMETA
Definition: walletdb.cpp:44
std::shared_ptr< CWallet > LoadWallet(interfaces::Chain &chain, const std::string &name, std::string &error, std::vector< std::string > &warnings)
Definition: dummywallet.cpp:75
ADD_SERIALIZE_METHODS
Definition: walletdb.h:139
RAII class that provides access to a Berkeley database.
Definition: db.h:200
const std::string DESTDATA
Definition: walletdb.cpp:29
KeyOriginInfo key_origin
Definition: walletdb.h:126
int64_t nCreateTime
Definition: walletdb.h:123
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:256
int nVersion
Definition: walletdb.h:122
int flags
Definition: bitcoin-tx.cpp:509
256-bit opaque blob.
Definition: uint256.h:120
BerkeleyBatch m_batch
Definition: walletdb.h:276
const std::string ACENTRY
Definition: walletdb.cpp:23
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
const std::string MINVERSION
Definition: walletdb.cpp:35
static const bool DEFAULT_FLUSHWALLET
Overview of wallet database classes:
Definition: walletdb.h:30
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:609
160-bit opaque blob.
Definition: uint256.h:109
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletdb.h:142
CHDChain()
Definition: walletdb.h:94
const std::string WATCHS
Definition: walletdb.cpp:45
void SetNull()
Definition: walletdb.h:106
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:26
CKeyID hd_seed_id
Definition: walletdb.h:125
const std::string BESTBLOCK
Definition: walletdb.cpp:25
const std::string CSCRIPT
Definition: walletdb.cpp:27
CKeyID seed_id
seed hash160
Definition: walletdb.h:87
#define VERSION
#define READWRITE(...)
Definition: serialize.h:191
const std::string BESTBLOCK_NOMERKLE
Definition: walletdb.cpp:24
const std::string KEY
Definition: walletdb.cpp:33
int nVersion
Definition: walletdb.h:92
void SetNull()
Definition: walletdb.h:157
uint32_t nInternalChainCounter
Definition: walletdb.h:86
A key from a CWallet&#39;s keypool.
const std::string PURPOSE
Definition: walletdb.cpp:40