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