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 /* simple HD chain data model */
59 class CHDChain
60 {
61 public:
65 
66  static const int VERSION_HD_BASE = 1;
67  static const int VERSION_HD_CHAIN_SPLIT = 2;
68  static const int CURRENT_VERSION = VERSION_HD_CHAIN_SPLIT;
69  int nVersion;
70 
71  CHDChain() { SetNull(); }
73  template <typename Stream, typename Operation>
74  inline void SerializationOp(Stream& s, Operation ser_action)
75  {
76  READWRITE(this->nVersion);
77  READWRITE(nExternalChainCounter);
78  READWRITE(seed_id);
79  if (this->nVersion >= VERSION_HD_CHAIN_SPLIT)
80  READWRITE(nInternalChainCounter);
81  }
82 
83  void SetNull()
84  {
85  nVersion = CHDChain::CURRENT_VERSION;
86  nExternalChainCounter = 0;
87  nInternalChainCounter = 0;
88  seed_id.SetNull();
89  }
90 };
91 
93 {
94 public:
95  static const int VERSION_BASIC=1;
96  static const int VERSION_WITH_HDDATA=10;
97  static const int VERSION_WITH_KEY_ORIGIN = 12;
98  static const int CURRENT_VERSION=VERSION_WITH_KEY_ORIGIN;
99  int nVersion;
100  int64_t nCreateTime; // 0 means unknown
101  std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility
102  CKeyID hd_seed_id; //id of the HD seed used to derive this key
103  KeyOriginInfo key_origin; // Key origin info with path and fingerprint
104  bool has_key_origin = false; //< Whether the key_origin is useful
105 
107  {
108  SetNull();
109  }
110  explicit CKeyMetadata(int64_t nCreateTime_)
111  {
112  SetNull();
113  nCreateTime = nCreateTime_;
114  }
115 
117 
118  template <typename Stream, typename Operation>
119  inline void SerializationOp(Stream& s, Operation ser_action) {
120  READWRITE(this->nVersion);
121  READWRITE(nCreateTime);
122  if (this->nVersion >= VERSION_WITH_HDDATA)
123  {
124  READWRITE(hdKeypath);
125  READWRITE(hd_seed_id);
126  }
127  if (this->nVersion >= VERSION_WITH_KEY_ORIGIN)
128  {
129  READWRITE(key_origin);
130  READWRITE(has_key_origin);
131  }
132  }
133 
134  void SetNull()
135  {
137  nCreateTime = 0;
138  hdKeypath.clear();
139  hd_seed_id.SetNull();
140  key_origin.clear();
141  has_key_origin = false;
142  }
143 };
144 
153 {
154 private:
155  template <typename K, typename T>
156  bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
157  {
158  if (!m_batch.Write(key, value, fOverwrite)) {
159  return false;
160  }
161  m_database.IncrementUpdateCounter();
162  if (m_database.nUpdateCounter % 1000 == 0) {
163  m_batch.Flush();
164  }
165  return true;
166  }
167 
168  template <typename K>
169  bool EraseIC(const K& key)
170  {
171  if (!m_batch.Erase(key)) {
172  return false;
173  }
174  m_database.IncrementUpdateCounter();
175  if (m_database.nUpdateCounter % 1000 == 0) {
176  m_batch.Flush();
177  }
178  return true;
179  }
180 
181 public:
182  explicit WalletBatch(WalletDatabase& database, const char* pszMode = "r+", bool _fFlushOnClose = true) :
183  m_batch(database, pszMode, _fFlushOnClose),
184  m_database(database)
185  {
186  }
187  WalletBatch(const WalletBatch&) = delete;
188  WalletBatch& operator=(const WalletBatch&) = delete;
189 
190  bool WriteName(const std::string& strAddress, const std::string& strName);
191  bool EraseName(const std::string& strAddress);
192 
193  bool WritePurpose(const std::string& strAddress, const std::string& purpose);
194  bool ErasePurpose(const std::string& strAddress);
195 
196  bool WriteTx(const CWalletTx& wtx);
197  bool EraseTx(uint256 hash);
198 
199  bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite);
200  bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
201  bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
202  bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
203 
204  bool WriteCScript(const uint160& hash, const CScript& redeemScript);
205 
206  bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
207  bool EraseWatchOnly(const CScript &script);
208 
209  bool WriteBestBlock(const CBlockLocator& locator);
210  bool ReadBestBlock(CBlockLocator& locator);
211 
212  bool WriteOrderPosNext(int64_t nOrderPosNext);
213 
214  bool ReadPool(int64_t nPool, CKeyPool& keypool);
215  bool WritePool(int64_t nPool, const CKeyPool& keypool);
216  bool ErasePool(int64_t nPool);
217 
218  bool WriteMinVersion(int nVersion);
219 
221  bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
223  bool EraseDestData(const std::string &address, const std::string &key);
224 
225  DBErrors LoadWallet(CWallet* pwallet);
226  DBErrors FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
227  DBErrors ZapWalletTx(std::vector<CWalletTx>& vWtx);
228  DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
229  /* Try to (very carefully!) recover wallet database (with a possible key type filter) */
230  static bool Recover(const fs::path& wallet_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
231  /* Recover convenience-function to bypass the key filter callback, called when verify fails, recovers everything */
232  static bool Recover(const fs::path& wallet_path, std::string& out_backup_filename);
233  /* Recover filter (used as callback), will only let keys (cryptographical keys) as KV/key-type pass through */
234  static bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue);
235  /* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
236  static bool IsKeyType(const std::string& strType);
237  /* verifies the database environment */
238  static bool VerifyEnvironment(const fs::path& wallet_path, std::string& errorStr);
239  /* verifies the database file */
240  static bool VerifyDatabaseFile(const fs::path& wallet_path, std::string& warningStr, std::string& errorStr);
241 
243  bool WriteHDChain(const CHDChain& chain);
244 
245  bool WriteWalletFlags(const uint64_t flags);
247  bool TxnBegin();
249  bool TxnCommit();
251  bool TxnAbort();
253  bool ReadVersion(int& nVersion);
255  bool WriteVersion(int nVersion);
256 private:
259 };
260 
262 void MaybeCompactWalletDB();
263 
264 #endif // BITCOIN_WALLET_WALLETDB_H
static const int CURRENT_VERSION
Definition: walletdb.h:68
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:110
bool EraseIC(const K &key)
Definition: walletdb.h:169
std::string hdKeypath
Definition: walletdb.h:101
void SetNull()
Definition: uint256.h:39
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
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:156
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:34
An instance of this class represents one database.
Definition: db.h:113
uint32_t nExternalChainCounter
Definition: walletdb.h:62
std::shared_ptr< CWallet > LoadWallet(interfaces::Chain &chain, const std::string &name, std::string &error, std::string &warning)
Definition: dummywallet.cpp:72
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:182
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:48
ADD_SERIALIZE_METHODS
Definition: walletdb.h:72
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletdb.h:74
void clear()
Definition: sign.h:41
Access to the wallet database.
Definition: walletdb.h:152
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
static const int CURRENT_VERSION
Definition: walletdb.h:98
WalletDatabase & m_database
Definition: walletdb.h:258
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:669
An encapsulated public key.
Definition: pubkey.h:30
ADD_SERIALIZE_METHODS
Definition: walletdb.h:116
RAII class that provides access to a Berkeley database.
Definition: db.h:202
KeyOriginInfo key_origin
Definition: walletdb.h:103
int64_t nCreateTime
Definition: walletdb.h:100
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:421
int nVersion
Definition: walletdb.h:99
int flags
Definition: bitcoin-tx.cpp:507
256-bit opaque blob.
Definition: uint256.h:121
BerkeleyBatch m_batch
Definition: walletdb.h:257
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
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:692
160-bit opaque blob.
Definition: uint256.h:110
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletdb.h:119
CHDChain()
Definition: walletdb.h:71
void SetNull()
Definition: walletdb.h:83
CKeyID hd_seed_id
Definition: walletdb.h:102
CKeyID seed_id
seed hash160
Definition: walletdb.h:64
#define READWRITE(...)
Definition: serialize.h:184
int nVersion
Definition: walletdb.h:69
void SetNull()
Definition: walletdb.h:134
uint32_t nInternalChainCounter
Definition: walletdb.h:63
A key from a CWallet&#39;s keypool.
Definition: wallet.h:188