BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
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 <wallet/db.h>
12 #include <key.h>
13 
14 #include <list>
15 #include <stdint.h>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
32 static const bool DEFAULT_FLUSHWALLET = true;
33 
34 struct CBlockLocator;
35 class CKeyPool;
36 class CMasterKey;
37 class CScript;
38 class CWallet;
39 class CWalletTx;
40 class uint160;
41 class uint256;
42 
45 
47 enum class DBErrors
48 {
49  LOAD_OK,
50  CORRUPT,
52  TOO_NEW,
53  LOAD_FAIL,
55 };
56 
57 /* simple HD chain data model */
58 class CHDChain
59 {
60 public:
64 
65  static const int VERSION_HD_BASE = 1;
66  static const int VERSION_HD_CHAIN_SPLIT = 2;
68  int nVersion;
69 
70  CHDChain() { SetNull(); }
72  template <typename Stream, typename Operation>
73  inline void SerializationOp(Stream& s, Operation ser_action)
74  {
75  READWRITE(this->nVersion);
78  if (this->nVersion >= VERSION_HD_CHAIN_SPLIT)
80  }
81 
82  void SetNull()
83  {
87  seed_id.SetNull();
88  }
89 };
90 
92 {
93 public:
94  static const int VERSION_BASIC=1;
95  static const int VERSION_WITH_HDDATA=10;
97  int nVersion;
98  int64_t nCreateTime; // 0 means unknown
99  std::string hdKeypath; //optional HD/bip32 keypath
100  CKeyID hd_seed_id; //id of the HD seed used to derive this key
101 
103  {
104  SetNull();
105  }
106  explicit CKeyMetadata(int64_t nCreateTime_)
107  {
108  SetNull();
109  nCreateTime = nCreateTime_;
110  }
111 
113 
114  template <typename Stream, typename Operation>
115  inline void SerializationOp(Stream& s, Operation ser_action) {
116  READWRITE(this->nVersion);
118  if (this->nVersion >= VERSION_WITH_HDDATA)
119  {
122  }
123  }
124 
125  void SetNull()
126  {
128  nCreateTime = 0;
129  hdKeypath.clear();
131  }
132 };
133 
140 {
141 private:
142  template <typename K, typename T>
143  bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
144  {
145  if (!m_batch.Write(key, value, fOverwrite)) {
146  return false;
147  }
149  return true;
150  }
151 
152  template <typename K>
153  bool EraseIC(const K& key)
154  {
155  if (!m_batch.Erase(key)) {
156  return false;
157  }
159  return true;
160  }
161 
162 public:
163  explicit WalletBatch(WalletDatabase& database, const char* pszMode = "r+", bool _fFlushOnClose = true) :
164  m_batch(database, pszMode, _fFlushOnClose),
165  m_database(database)
166  {
167  }
168  WalletBatch(const WalletBatch&) = delete;
169  WalletBatch& operator=(const WalletBatch&) = delete;
170 
171  bool WriteName(const std::string& strAddress, const std::string& strName);
172  bool EraseName(const std::string& strAddress);
173 
174  bool WritePurpose(const std::string& strAddress, const std::string& purpose);
175  bool ErasePurpose(const std::string& strAddress);
176 
177  bool WriteTx(const CWalletTx& wtx);
178  bool EraseTx(uint256 hash);
179 
180  bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
181  bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
182  bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
183 
184  bool WriteCScript(const uint160& hash, const CScript& redeemScript);
185 
186  bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
187  bool EraseWatchOnly(const CScript &script);
188 
189  bool WriteBestBlock(const CBlockLocator& locator);
190  bool ReadBestBlock(CBlockLocator& locator);
191 
192  bool WriteOrderPosNext(int64_t nOrderPosNext);
193 
194  bool ReadPool(int64_t nPool, CKeyPool& keypool);
195  bool WritePool(int64_t nPool, const CKeyPool& keypool);
196  bool ErasePool(int64_t nPool);
197 
198  bool WriteMinVersion(int nVersion);
199 
201  bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
203  bool EraseDestData(const std::string &address, const std::string &key);
204 
205  DBErrors LoadWallet(CWallet* pwallet);
206  DBErrors FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
207  DBErrors ZapWalletTx(std::vector<CWalletTx>& vWtx);
208  DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
209  /* Try to (very carefully!) recover wallet database (with a possible key type filter) */
210  static bool Recover(const fs::path& wallet_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
211  /* Recover convenience-function to bypass the key filter callback, called when verify fails, recovers everything */
212  static bool Recover(const fs::path& wallet_path, std::string& out_backup_filename);
213  /* Recover filter (used as callback), will only let keys (cryptographical keys) as KV/key-type pass through */
214  static bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue);
215  /* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
216  static bool IsKeyType(const std::string& strType);
217  /* verifies the database environment */
218  static bool VerifyEnvironment(const fs::path& wallet_path, std::string& errorStr);
219  /* verifies the database file */
220  static bool VerifyDatabaseFile(const fs::path& wallet_path, std::string& warningStr, std::string& errorStr);
221 
223  bool WriteHDChain(const CHDChain& chain);
224 
225  bool WriteWalletFlags(const uint64_t flags);
227  bool TxnBegin();
229  bool TxnCommit();
231  bool TxnAbort();
233  bool ReadVersion(int& nVersion);
235  bool WriteVersion(int nVersion);
236 private:
239 };
240 
242 void MaybeCompactWalletDB();
243 
244 #endif // BITCOIN_WALLET_WALLETDB_H
bool TxnCommit()
Commit current transaction.
Definition: walletdb.cpp:757
static const int CURRENT_VERSION
Definition: walletdb.h:67
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:106
bool WriteName(const std::string &strAddress, const std::string &strName)
Definition: walletdb.cpp:28
bool EraseIC(const K &key)
Definition: walletdb.h:153
bool WriteVersion(int nVersion)
Write wallet version.
Definition: walletdb.cpp:772
bool EraseDestData(const std::string &address, const std::string &key)
Erase destination data tuple from wallet database.
Definition: walletdb.cpp:736
static const int VERSION_BASIC
Definition: walletdb.h:94
std::string hdKeypath
Definition: walletdb.h:99
bool ErasePurpose(const std::string &strAddress)
Definition: walletdb.cpp:45
bool ReadBestBlock(CBlockLocator &locator)
Definition: walletdb.cpp:123
void SetNull()
Definition: uint256.h:40
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:128
bool WriteCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:75
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:143
DBErrors FindWalletTx(std::vector< uint256 > &vTxHash, std::vector< CWalletTx > &vWtx)
Definition: walletdb.cpp:539
DBErrors ZapSelectTx(std::vector< uint256 > &vHashIn, std::vector< uint256 > &vHashOut)
Definition: walletdb.cpp:598
bool WriteMinVersion(int nVersion)
Definition: walletdb.cpp:149
bool WriteHDChain(const CHDChain &chain)
write the hdchain model (external chain child index counter)
Definition: walletdb.cpp:742
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:34
bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta)
Definition: walletdb.cpp:101
bool Write(const K &key, const T &value, bool fOverwrite=true)
Definition: db.h:244
static bool VerifyDatabaseFile(const fs::path &wallet_path, std::string &warningStr, std::string &errorStr)
Definition: walletdb.cpp:726
An instance of this class represents one database.
Definition: db.h:104
uint32_t nExternalChainCounter
Definition: walletdb.h:61
bool TxnBegin()
Begin a new transaction.
Definition: walletdb.cpp:752
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:221
DBErrors ZapWalletTx(std::vector< CWalletTx > &vWtx)
Definition: walletdb.cpp:636
bool WriteTx(const CWalletTx &wtx)
Definition: walletdb.cpp:50
WalletBatch(WalletDatabase &database, const char *pszMode="r+", bool _fFlushOnClose=true)
Definition: walletdb.h:163
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:437
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:47
ADD_SERIALIZE_METHODS
Definition: walletdb.h:71
bool WriteWalletFlags(const uint64_t flags)
Definition: walletdb.cpp:747
static bool Recover(const fs::path &wallet_path, void *callbackDataIn, bool(*recoverKVcallback)(void *callbackData, CDataStream ssKey, CDataStream ssValue), std::string &out_backup_filename)
Definition: walletdb.cpp:686
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletdb.h:73
bool EraseWatchOnly(const CScript &script)
Definition: walletdb.cpp:109
Access to the wallet database.
Definition: walletdb.h:139
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:117
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:762
bool Erase(const K &key)
Definition: db.h:273
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:731
static bool IsKeyType(const std::string &strType)
Definition: walletdb.cpp:431
bool WriteCScript(const uint160 &hash, const CScript &redeemScript)
Definition: walletdb.cpp:96
static const int CURRENT_VERSION
Definition: walletdb.h:96
static bool VerifyEnvironment(const fs::path &wallet_path, std::string &errorStr)
Definition: walletdb.cpp:721
WalletDatabase & m_database
Definition: walletdb.h:238
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:653
An encapsulated public key.
Definition: pubkey.h:30
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:91
ADD_SERIALIZE_METHODS
Definition: walletdb.h:112
RAII class that provides access to a Berkeley database.
Definition: db.h:178
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:129
int64_t nCreateTime
Definition: walletdb.h:98
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:295
int nVersion
Definition: walletdb.h:97
bool ErasePool(int64_t nPool)
Definition: walletdb.cpp:144
bool WritePool(int64_t nPool, const CKeyPool &keypool)
Definition: walletdb.cpp:139
256-bit opaque blob.
Definition: uint256.h:122
BerkeleyBatch m_batch
Definition: walletdb.h:237
void IncrementUpdateCounter()
Definition: db.cpp:538
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:384
static const int VERSION_WITH_HDDATA
Definition: walletdb.h:95
A reference to a CKey: the Hash360 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:599
bool WritePurpose(const std::string &strAddress, const std::string &purpose)
Definition: walletdb.cpp:40
160-bit opaque blob.
Definition: uint256.h:111
bool ReadPool(int64_t nPool, CKeyPool &keypool)
Definition: walletdb.cpp:134
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletdb.h:115
static const int VERSION_HD_CHAIN_SPLIT
Definition: walletdb.h:66
CHDChain()
Definition: walletdb.h:70
bool ReadVersion(int &nVersion)
Read wallet version.
Definition: walletdb.cpp:767
bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:60
int flags
Definition: bsha3-tx.cpp:509
void SetNull()
Definition: walletdb.h:82
CKeyID hd_seed_id
Definition: walletdb.h:100
bool EraseTx(uint256 hash)
Definition: walletdb.cpp:55
CKeyID seed_id
seed hash360
Definition: walletdb.h:63
#define READWRITE(...)
Definition: serialize.h:173
int nVersion
Definition: walletdb.h:68
void SetNull()
Definition: walletdb.h:125
static bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue)
Definition: walletdb.cpp:698
static const int VERSION_HD_BASE
Definition: walletdb.h:65
WalletBatch & operator=(const WalletBatch &)=delete
uint32_t nInternalChainCounter
Definition: walletdb.h:62
A key pool entry.
Definition: wallet.h:117
bool EraseName(const std::string &strAddress)
Definition: walletdb.cpp:33