PIVX Core  5.6.99
P2P Digital Currency
walletdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 The Bitcoin developers
3 // Copyright (c) 2016-2021 The PIVX Core developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef PIVX_WALLET_WALLETDB_H
8 #define PIVX_WALLET_WALLETDB_H
9 
10 #include "amount.h"
11 #include "wallet/db.h"
12 #include "wallet/hdchain.h"
13 #include "key.h"
14 #include "keystore.h"
15 #include "script/keyorigin.h"
16 
17 #include <list>
18 #include <stdint.h>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
35 static const bool DEFAULT_FLUSHWALLET = true;
36 
37 struct CBlockLocator;
38 class CKeyPool;
39 class CMasterKey;
40 class CScript;
41 class CWallet;
42 class CWalletTx;
43 class uint160;
44 class uint256;
45 
48 
50 enum DBErrors {
57 };
58 
60 {
61 public:
62  // Metadata versions
63  static const int VERSION_BASIC = 1;
64  static const int VERSION_WITH_KEY_ORIGIN = 12;
65  // Active version
67 
68  int nVersion;
69  int64_t nCreateTime; // 0 means unknown
70  CKeyID hd_seed_id; //id of the HD seed used to derive this key
71  KeyOriginInfo key_origin; // Key origin info with path and fingerprint
72 
74  {
75  SetNull();
76  }
77 
78  explicit CKeyMetadata(int64_t nCreateTime_)
79  {
80  SetNull();
81  nCreateTime = nCreateTime_;
82  }
83 
85  {
86  READWRITE(obj.nVersion, obj.nCreateTime);
87  if (obj.HasKeyOrigin()) {
88  READWRITE(obj.hd_seed_id, obj.key_origin);
89  }
90  }
91 
92  void SetNull()
93  {
95  nCreateTime = 0;
97  key_origin.clear();
98  }
99 
100  bool HasKeyOrigin() const
101  {
102  return this->nVersion >= VERSION_WITH_KEY_ORIGIN;
103  }
104 };
105 
112 {
113 private:
114  template <typename K, typename T>
115  bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
116  {
117  if (!m_batch.Write(key, value, fOverwrite)) {
118  return false;
119  }
121  return true;
122  }
123 
124  template <typename K>
125  bool EraseIC(const K& key)
126  {
127  if (!m_batch.Erase(key)) {
128  return false;
129  }
131  return true;
132  }
133 
134 public:
135  explicit WalletBatch(WalletDatabase& database, const char* pszMode = "r+", bool _fFlushOnClose = true) :
136  m_batch(database, pszMode, _fFlushOnClose),
137  m_database(database)
138  {
139  }
140  WalletBatch(const WalletBatch&) = delete;
141  WalletBatch& operator=(const WalletBatch&) = delete;
142 
143  bool WriteName(const std::string& strAddress, const std::string& strName);
144  bool EraseName(const std::string& strAddress);
145 
146  bool WritePurpose(const std::string& strAddress, const std::string& purpose);
147  bool ErasePurpose(const std::string& strAddress);
148 
149  bool WriteTx(const CWalletTx& wtx);
150  bool EraseTx(uint256 hash);
151 
152  bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata& keyMeta);
153  bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata& keyMeta);
154  bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
155 
156  bool WriteCScript(const uint160& hash, const CScript& redeemScript);
157 
158  bool WriteWatchOnly(const CScript& script);
159  bool EraseWatchOnly(const CScript& script);
160 
161  bool WriteBestBlock(const CBlockLocator& locator);
162  bool ReadBestBlock(CBlockLocator& locator);
163 
164  bool WriteOrderPosNext(int64_t nOrderPosNext);
165 
166  bool WriteStakeSplitThreshold(const CAmount& nStakeSplitThreshold);
167  bool WriteUseCustomFee(bool fUse);
168  bool WriteCustomFeeValue(const CAmount& nCustomFee);
169  bool WriteAutoCombineSettings(bool fEnable, CAmount nCombineThreshold, int frequency);
170 
171  bool ReadPool(int64_t nPool, CKeyPool& keypool);
172  bool WritePool(int64_t nPool, const CKeyPool& keypool);
173  bool ErasePool(int64_t nPool);
174 
175  bool WriteMinVersion(int nVersion);
176 
178  bool WriteHDChain(const CHDChain& chain);
179 
183  const CKeyMetadata &keyMeta);
184 
187 
189  const std::vector<unsigned char>& vchCryptedSecret,
190  const CKeyMetadata &keyMeta);
191 
193  bool WriteSaplingCommonOVK(const uint256& ovk);
194  bool ReadSaplingCommonOVK(uint256& ovkRet);
195 
196  bool WriteWitnessCacheSize(int64_t nWitnessCacheSize);
197 
199  bool WriteDestData(const std::string& address, const std::string& key, const std::string& value);
201  bool EraseDestData(const std::string& address, const std::string& key);
202 
204  DBErrors LoadWallet(CWallet* pwallet);
205  DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
206  DBErrors ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx);
207  /* Try to (very carefully!) recover wallet database (with a possible key type filter) */
208  static bool Recover(const fs::path& wallet_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
209  /* Recover convenience-function to bypass the key filter callback, called when verify fails, recovers everything */
210  static bool Recover(const fs::path& wallet_path, std::string& out_backup_filename);
211  /* Recover filter (used as callback), will only let keys (cryptographical keys) as KV/key-type pass through */
212  static bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue);
213  /* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
214  static bool IsKeyType(const std::string& strType);
215  /* verifies the database environment */
216  static bool VerifyEnvironment(const fs::path& wallet_path, std::string& errorStr);
217  /* verifies the database file */
218  static bool VerifyDatabaseFile(const fs::path& wallet_path, std::string& warningStr, std::string& errorStr);
219 
221  bool TxnBegin();
223  bool TxnCommit();
225  bool TxnAbort();
227  bool ReadVersion(int& nVersion);
229  bool WriteVersion(int nVersion);
230 private:
233 };
234 
236 bool AutoBackupWallet(CWallet& wallet, std::string& strBackupWarning, std::string& strBackupError);
237 
239 void MaybeCompactWalletDB();
240 
241 #endif // PIVX_WALLET_WALLETDB_H
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
RAII class that provides access to a Berkeley database.
Definition: db.h:180
bool Write(const K &key, const T &value, bool fOverwrite=true)
Definition: db.h:245
bool Erase(const K &key)
Definition: db.h:274
An instance of this class represents one database.
Definition: db.h:105
void IncrementUpdateCounter()
Definition: db.cpp:538
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
static const int VERSION_WITH_KEY_ORIGIN
Definition: walletdb.h:64
KeyOriginInfo key_origin
Definition: walletdb.h:71
static const int VERSION_BASIC
Definition: walletdb.h:63
int nVersion
Definition: walletdb.h:68
static const int CURRENT_VERSION
Definition: walletdb.h:66
CKeyMetadata()
Definition: walletdb.h:73
void SetNull()
Definition: walletdb.h:92
bool HasKeyOrigin() const
Definition: walletdb.h:100
SERIALIZE_METHODS(CKeyMetadata, obj)
Definition: walletdb.h:84
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:78
int64_t nCreateTime
Definition: walletdb.h:69
CKeyID hd_seed_id
Definition: walletdb.h:70
A key pool entry.
Definition: wallet.h:125
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key.
Definition: crypter.h:39
An encapsulated public key.
Definition: pubkey.h:44
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,...
Definition: wallet.h:577
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:325
Access to the wallet database.
Definition: walletdb.h:112
bool TxnCommit()
Commit current transaction.
Definition: walletdb.cpp:974
bool WriteCustomFeeValue(const CAmount &nCustomFee)
Definition: walletdb.cpp:240
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:191
static bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue)
Definition: walletdb.cpp:926
bool ErasePool(int64_t nPool)
Definition: walletdb.cpp:265
WalletBatch(const WalletBatch &)=delete
bool WriteName(const std::string &strAddress, const std::string &strName)
Definition: walletdb.cpp:70
bool WriteSaplingPaymentAddress(const libzcash::SaplingPaymentAddress &addr, const libzcash::SaplingIncomingViewingKey &ivk)
Definition: walletdb.cpp:148
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:914
bool WriteMinVersion(int nVersion)
Definition: walletdb.cpp:270
DBErrors ZapWalletTx(CWallet *pwallet, std::vector< CWalletTx > &vWtx)
Definition: walletdb.cpp:795
DBErrors ReorderTransactions(CWallet *pwallet)
Definition: walletdb.cpp:282
BerkeleyBatch m_batch
Definition: walletdb.h:231
static bool IsKeyType(const std::string &strType)
Definition: walletdb.cpp:628
bool ErasePurpose(const std::string &strAddress)
Definition: walletdb.cpp:87
bool WriteCScript(const uint160 &hash, const CScript &redeemScript)
Definition: walletdb.cpp:196
bool EraseWatchOnly(const CScript &script)
Definition: walletdb.cpp:206
bool WriteCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:117
DBErrors FindWalletTx(CWallet *pwallet, std::vector< uint256 > &vTxHash, std::vector< CWalletTx > &vWtx)
Definition: walletdb.cpp:735
bool ReadPool(int64_t nPool, CKeyPool &keypool)
Definition: walletdb.cpp:255
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:115
bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:102
bool WriteCryptedSaplingZKey(const libzcash::SaplingExtendedFullViewingKey &extfvk, const std::vector< unsigned char > &vchCryptedSecret, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:154
bool EraseDestData(const std::string &address, const std::string &key)
Erase destination data tuple from wallet database.
Definition: walletdb.cpp:964
bool ReadVersion(int &nVersion)
Read wallet version.
Definition: walletdb.cpp:984
WalletBatch(WalletDatabase &database, const char *pszMode="r+", bool _fFlushOnClose=true)
Definition: walletdb.h:135
static bool VerifyEnvironment(const fs::path &wallet_path, std::string &errorStr)
Definition: walletdb.cpp:949
bool WriteAutoCombineSettings(bool fEnable, CAmount nCombineThreshold, int frequency)
Definition: walletdb.cpp:245
bool ReadSaplingCommonOVK(uint256 &ovkRet)
Definition: walletdb.cpp:181
bool WriteHDChain(const CHDChain &chain)
write the hdchain model (external/internal chain child index counter)
Definition: walletdb.cpp:275
bool ReadBestBlock(CBlockLocator &locator)
Definition: walletdb.cpp:217
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:225
bool WriteTx(const CWalletTx &wtx)
Definition: walletdb.cpp:92
bool EraseIC(const K &key)
Definition: walletdb.h:125
static bool VerifyDatabaseFile(const fs::path &wallet_path, std::string &warningStr, std::string &errorStr)
Definition: walletdb.cpp:954
bool TxnBegin()
Begin a new transaction.
Definition: walletdb.cpp:969
bool TxnAbort()
Abort current transaction.
Definition: walletdb.cpp:979
bool WritePool(int64_t nPool, const CKeyPool &keypool)
Definition: walletdb.cpp:260
bool WriteWitnessCacheSize(int64_t nWitnessCacheSize)
Definition: walletdb.cpp:186
bool WriteUseCustomFee(bool fUse)
Definition: walletdb.cpp:235
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:211
WalletBatch & operator=(const WalletBatch &)=delete
bool WriteVersion(int nVersion)
Write wallet version.
Definition: walletdb.cpp:989
bool WriteWatchOnly(const CScript &script)
Definition: walletdb.cpp:201
bool EraseTx(uint256 hash)
Definition: walletdb.cpp:97
bool WriteStakeSplitThreshold(const CAmount &nStakeSplitThreshold)
Definition: walletdb.cpp:230
bool WriteSaplingCommonOVK(const uint256 &ovk)
Common output viewing key, used when shielding transparent funds.
Definition: walletdb.cpp:176
bool WritePurpose(const std::string &strAddress, const std::string &purpose)
Definition: walletdb.cpp:82
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:959
bool EraseName(const std::string &strAddress)
Definition: walletdb.cpp:75
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:635
bool WriteSaplingZKey(const libzcash::SaplingIncomingViewingKey &ivk, const libzcash::SaplingExtendedSpendingKey &key, const CKeyMetadata &keyMeta)
Write extended spending key to wallet database, where the key is the incoming viewing key.
Definition: walletdb.cpp:137
WalletDatabase & m_database
Definition: walletdb.h:232
void SetNull()
Definition: uint256.h:44
Sapling functions.
Definition: address.h:30
160-bit opaque blob.
Definition: uint256.h:127
256-bit opaque blob.
Definition: uint256.h:138
#define T(expected, seed, data)
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
CPrivKey is a serialized private key, with all parameters included (PRIVATE_KEY_SIZE bytes)
Definition: key.h:20
#define READWRITE(...)
Definition: serialize.h:183
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:154
void clear()
Definition: keyorigin.h:23
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:812
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:50
@ DB_TOO_NEW
Definition: walletdb.h:54
@ DB_CORRUPT
Definition: walletdb.h:52
@ DB_LOAD_OK
Definition: walletdb.h:51
@ DB_LOAD_FAIL
Definition: walletdb.h:55
@ DB_NONCRITICAL_ERROR
Definition: walletdb.h:53
@ DB_NEED_REWRITE
Definition: walletdb.h:56
bool AutoBackupWallet(CWallet &wallet, std::string &strBackupWarning, std::string &strBackupError)
Called during init: Automatic backups.
Definition: walletdb.cpp:880