PRCYCoin  2.0.0.7rc1
P2P Digital Currency
walletdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Copyright (c) 2015-2018 The PIVX developers
4 // Copyright (c) 2018-2020 The DAPS Project developers
5 // Distributed under the MIT/X11 software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #ifndef BITCOIN_WALLETDB_H
9 #define BITCOIN_WALLETDB_H
10 
11 #include "amount.h"
12 #include "wallet/db.h"
13 #include "key.h"
14 #include "keystore.h"
15 
16 #include <list>
17 #include <stdint.h>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 #include "bignum.h"
22 
23 class CAccount;
24 class CStealthAccount;
25 class CAccountingEntry;
26 struct CBlockLocator;
27 class CKeyPool;
28 class CMasterKey;
29 class CScript;
30 class CWallet;
31 class CWalletTx;
32 class uint160;
33 class uint256;
34 
36 enum DBErrors {
43 };
44 
46 {
47 public:
48  static const int CURRENT_VERSION = 1;
49  int nVersion;
50  int64_t nCreateTime; // 0 means unknown
51 
53  {
54  SetNull();
55  }
56  CKeyMetadata(int64_t nCreateTime_)
57  {
59  nCreateTime = nCreateTime_;
60  }
61 
63 
64  template <typename Stream, typename Operation>
65  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
66  {
67  READWRITE(this->nVersion);
68  nVersion = this->nVersion;
69  READWRITE(nCreateTime);
70  }
71 
72  void SetNull()
73  {
75  nCreateTime = 0;
76  }
77 };
78 
80 class CWalletDB : public CDB
81 {
82 public:
83  CWalletDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnClose = true) : CDB(strFilename, pszMode, fFlushOnClose)
84  {
85  }
86 
87  bool WriteName(const std::string& strAddress, const std::string& strName);
88  bool EraseName(const std::string& strAddress);
89 
90  bool WritePurpose(const std::string& strAddress, const std::string& purpose);
91  bool ErasePurpose(const std::string& strAddress);
92 
93  bool WriteTx(uint256 hash, const CWalletTx& wtx);
94  bool EraseTx(uint256 hash);
95 
96  bool WriteStakingStatus(bool status);
97  bool ReadStakingStatus();
98 
99  bool Write2FA(bool status);
100  bool Read2FA();
101 
102  bool Write2FASecret(std::string secret);
103  std::string Read2FASecret();
104 
105  bool Write2FAPeriod(int period);
106  int Read2FAPeriod();
107 
108  bool Write2FALastTime(uint64_t lastTime);
109  uint64_t Read2FALastTime();
110 
111  bool WriteScannedBlockHeight(int height);
112  bool ReadScannedBlockHeight(int& height);
113 
114  bool WriteReserveAmount(const double &amount);
115  bool ReadReserveAmount(double &amount);
116 
117  bool WriteTxPrivateKey(const std::string& outpointKey, const std::string& k);
118  bool ReadTxPrivateKey(const std::string& outpointKey, std::string& k);
119 
120  bool WriteKeyImage(const std::string& outpointKey, const CKeyImage& k);
121  bool ReadKeyImage(const std::string& outpointKey, CKeyImage& k);
122 
123  bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata& keyMeta);
124  bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata& keyMeta);
125  bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
126 
127  bool WriteCScript(const uint160& hash, const CScript& redeemScript);
128 
129  bool WriteWatchOnly(const CScript& script);
130  bool EraseWatchOnly(const CScript& script);
131 
132  bool WriteMultiSig(const CScript& script);
133  bool EraseMultiSig(const CScript& script);
134 
135  bool WriteBestBlock(const CBlockLocator& locator);
136  bool ReadBestBlock(CBlockLocator& locator);
137 
138  bool WriteOrderPosNext(int64_t nOrderPosNext);
139 
140  // presstab
141  bool WriteStakeSplitThreshold(uint64_t nStakeSplitThreshold);
142  bool WriteMultiSend(std::vector<std::pair<std::string, int> > vMultiSend);
143  bool EraseMultiSend(std::vector<std::pair<std::string, int> > vMultiSend);
144  bool WriteMSettings(bool fMultiSendStake, bool fMultiSendMasternode, int nLastMultiSendHeight);
145  bool WriteMSDisabledAddresses(std::vector<std::string> vDisabledAddresses);
146  bool EraseMSDisabledAddresses(std::vector<std::string> vDisabledAddresses);
147  bool WriteAutoCombineSettings(bool fEnable, CAmount nCombineThreshold);
148 
149  bool WriteDefaultKey(const CPubKey& vchPubKey);
150 
151  bool ReadPool(int64_t nPool, CKeyPool& keypool);
152  bool WritePool(int64_t nPool, const CKeyPool& keypool);
153  bool ErasePool(int64_t nPool);
154 
155  bool WriteMinVersion(int nVersion);
156 
159  bool WriteAccountingEntry_Backend(const CAccountingEntry& acentry);
160 
161  bool ReadAccount(const std::string& strAccount, CAccount& account);
162  bool WriteAccount(const std::string& strAccount, const CAccount& account);
163 
164  bool WriteAutoConsolidateSettingTime(uint32_t settingTime);
166 
167  bool ReadStealthAccount(const std::string& strAccount, CStealthAccount& account);
168  bool WriteStealthAccount(const std::string& strAccount, const CStealthAccount& account);
169 
171  bool WriteDestData(const std::string& address, const std::string& key, const std::string& value);
173  bool EraseDestData(const std::string& address, const std::string& key);
174 
175  bool WriteHDChain(const CHDChain& chain);
176  bool WriteCryptedHDChain(const CHDChain& chain);
177  bool WriteHDPubKey(const CHDPubKey& hdPubKey, const CKeyMetadata& keyMeta);
178 
179  CAmount GetAccountCreditDebit(const std::string& strAccount);
180  void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
181 
183  DBErrors LoadWallet(CWallet* pwallet);
184  DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
185  DBErrors ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx);
186  static bool Compact(CDBEnv& dbenv, const std::string& strFile);
187  static bool Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys);
188  static bool Recover(CDBEnv& dbenv, std::string filename);
189 
190  bool AppendStealthAccountList(const std::string& accountName);
191  bool ReadStealthAccountList(std::string& accountList);
192 
193  static void IncrementUpdateCounter();
194  static unsigned int GetUpdateCounter();
195 private:
196  CWalletDB(const CWalletDB&);
197  void operator=(const CWalletDB&);
198 
199  bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
200 };
201 
202 void NotifyBacked(const CWallet& wallet, bool fSuccess, std::string strMessage);
203 bool BackupWallet(const CWallet& wallet, const fs::path& strDest, bool fEnableCustom = true);
204 bool AttemptBackupWallet(const CWallet& wallet, const fs::path& pathSrc, const fs::path& pathDest);
205 void ThreadFlushWalletDB(const std::string& strFile);
206 
207 #endif // BITCOIN_WALLETDB_H
DB_TOO_NEW
@ DB_TOO_NEW
Definition: walletdb.h:40
CWalletDB::WriteBestBlock
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:172
CWalletDB::ReadReserveAmount
bool ReadReserveAmount(double &amount)
Definition: walletdb.cpp:167
CWalletDB::ListAccountCreditDebit
void ListAccountCreditDebit(const std::string &strAccount, std::list< CAccountingEntry > &acentries)
Definition: walletdb.cpp:425
CWalletDB::EraseMultiSig
bool EraseMultiSig(const CScript &script)
Definition: walletdb.cpp:155
CAccountingEntry
Internal transfers.
Definition: wallet.h:982
CWalletDB::WriteOrderPosNext
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:185
CWalletDB::WriteKeyImage
bool WriteKeyImage(const std::string &outpointKey, const CKeyImage &k)
Definition: walletdb.cpp:1281
NotifyBacked
void NotifyBacked(const CWallet &wallet, bool fSuccess, std::string strMessage)
Definition: walletdb.cpp:1039
DB_LOAD_FAIL
@ DB_LOAD_FAIL
Definition: walletdb.h:41
ThreadFlushWalletDB
void ThreadFlushWalletDB(const std::string &strFile)
Definition: walletdb.cpp:984
CWalletDB::FindWalletTx
DBErrors FindWalletTx(CWallet *pwallet, std::vector< uint256 > &vTxHash, std::vector< CWalletTx > &vWtx)
Definition: walletdb.cpp:907
CWalletDB::WriteAutoConsolidateSettingTime
bool WriteAutoConsolidateSettingTime(uint32_t settingTime)
Definition: walletdb.cpp:368
CWalletDB::Write2FAPeriod
bool Write2FAPeriod(int period)
Definition: walletdb.cpp:338
CKeyPool
A key pool entry.
Definition: wallet.h:126
CWalletDB::WriteCryptedHDChain
bool WriteCryptedHDChain(const CHDChain &chain)
Definition: walletdb.cpp:1303
CWalletDB::WritePurpose
bool WritePurpose(const std::string &strAddress, const std::string &purpose)
Definition: walletdb.cpp:64
CKeyMetadata::nVersion
int nVersion
Definition: walletdb.h:49
CWalletDB::ReadStakingStatus
bool ReadStakingStatus()
Definition: walletdb.cpp:296
CWalletDB::ZapWalletTx
DBErrors ZapWalletTx(CWallet *pwallet, std::vector< CWalletTx > &vWtx)
Definition: walletdb.cpp:967
CWalletDB::ErasePurpose
bool ErasePurpose(const std::string &strAddress)
Definition: walletdb.cpp:70
CWalletDB::operator=
void operator=(const CWalletDB &)
CWalletDB::WriteAccountingEntry_Backend
bool WriteAccountingEntry_Backend(const CAccountingEntry &acentry)
This writes directly to the database, and will not update the CWallet's cached accounting entries!...
Definition: walletdb.cpp:408
CWalletDB::EraseTx
bool EraseTx(uint256 hash)
Definition: walletdb.cpp:82
CWalletDB::WriteMSDisabledAddresses
bool WriteMSDisabledAddresses(std::vector< std::string > vDisabledAddresses)
Definition: walletdb.cpp:234
CWalletDB::ReadAccount
bool ReadAccount(const std::string &strAccount, CAccount &account)
Definition: walletdb.cpp:362
CWalletDB::WriteMultiSig
bool WriteMultiSig(const CScript &script)
Definition: walletdb.cpp:149
CPrivKey
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
secp256k1: const unsigned int PRIVATE_KEY_SIZE = 279; const unsigned int PUBLIC_KEY_SIZE = 65; const ...
Definition: key.h:20
CWalletDB::Read2FASecret
std::string Read2FASecret()
Definition: walletdb.cpp:330
CWalletDB::WriteCScript
bool WriteCScript(const uint160 &hash, const CScript &redeemScript)
Definition: walletdb.cpp:131
DB_LOAD_OK
@ DB_LOAD_OK
Definition: walletdb.h:37
CWalletDB::CWalletDB
CWalletDB(const std::string &strFilename, const char *pszMode="r+", bool fFlushOnClose=true)
Definition: walletdb.h:83
CWalletDB::Write2FALastTime
bool Write2FALastTime(uint64_t lastTime)
Definition: walletdb.cpp:350
CWalletDB::EraseMultiSend
bool EraseMultiSend(std::vector< std::pair< std::string, int > > vMultiSend)
Definition: walletdb.cpp:212
CMasterKey
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key.
Definition: crypter.h:34
CDBEnv
Definition: db.h:28
CWalletDB::ReadScannedBlockHeight
bool ReadScannedBlockHeight(int &height)
Definition: walletdb.cpp:308
CStealthAccount
Definition: wallet.h:195
db.h
DB_NEED_REWRITE
@ DB_NEED_REWRITE
Definition: walletdb.h:42
CWalletDB::WriteCryptedKey
bool WriteCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:105
CWalletDB::ReadKeyImage
bool ReadKeyImage(const std::string &outpointKey, CKeyImage &k)
Definition: walletdb.cpp:1285
CWalletDB::ReadAutoConsolidateSettingTime
uint32_t ReadAutoConsolidateSettingTime()
Definition: walletdb.cpp:373
CHDPubKey
Definition: hdchain.h:92
CWalletDB::EraseWatchOnly
bool EraseWatchOnly(const CScript &script)
Definition: walletdb.cpp:143
CDB
RAII class that provides access to a Berkeley database.
Definition: db.h:95
DB_NONCRITICAL_ERROR
@ DB_NONCRITICAL_ERROR
Definition: walletdb.h:39
CWalletDB::WritePool
bool WritePool(int64_t nPool, const CKeyPool &keypool)
Definition: walletdb.cpp:275
CWalletDB::WriteAccountingEntry
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry &acentry)
Definition: walletdb.cpp:403
CWalletDB::Read2FAPeriod
int Read2FAPeriod()
Definition: walletdb.cpp:342
bignum.h
CWalletDB::LoadWallet
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:811
CWalletDB::Write2FA
bool Write2FA(bool status)
Definition: walletdb.cpp:313
CWalletDB::WriteMasterKey
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:125
CWalletDB
Access to the wallet database (wallet.dat)
Definition: walletdb.h:80
CWalletDB::Read2FALastTime
uint64_t Read2FALastTime()
Definition: walletdb.cpp:354
CAmount
int64_t CAmount
Amount in PRCY (Can be negative)
Definition: amount.h:17
CWalletDB::ReadPool
bool ReadPool(int64_t nPool, CKeyPool &keypool)
Definition: walletdb.cpp:270
CDB::fFlushOnClose
bool fFlushOnClose
Definition: db.h:102
CWalletDB::WriteAutoCombineSettings
bool WriteAutoCombineSettings(bool fEnable, CAmount nCombineThreshold)
Definition: walletdb.cpp:255
CWalletDB::ErasePool
bool ErasePool(int64_t nPool)
Definition: walletdb.cpp:281
keystore.h
CAccount
Account information.
Definition: wallet.h:167
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
CWalletDB::IncrementUpdateCounter
static void IncrementUpdateCounter()
Definition: walletdb.cpp:1326
CWalletDB::WriteKey
bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:88
CWalletDB::Read2FA
bool Read2FA()
Definition: walletdb.cpp:317
CWalletDB::WriteWatchOnly
bool WriteWatchOnly(const CScript &script)
Definition: walletdb.cpp:137
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:363
CWalletDB::WriteDefaultKey
bool WriteDefaultKey(const CPubKey &vchPubKey)
Definition: walletdb.cpp:264
CWalletDB::ReadBestBlock
bool ReadBestBlock(CBlockLocator &locator)
Definition: walletdb.cpp:179
CWalletDB::GetAccountCreditDebit
CAmount GetAccountCreditDebit(const std::string &strAccount)
Definition: walletdb.cpp:413
AttemptBackupWallet
bool AttemptBackupWallet(const CWallet &wallet, const fs::path &pathSrc, const fs::path &pathDest)
Definition: walletdb.cpp:1149
CKeyMetadata::CKeyMetadata
CKeyMetadata()
Definition: walletdb.h:52
CKeyMetadata::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: walletdb.h:65
key.h
CWalletDB::WriteAccount
bool WriteAccount(const std::string &strAccount, const CAccount &account)
Definition: walletdb.cpp:383
DBErrors
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:36
uint160
160-bit unsigned big integer.
Definition: uint256.h:27
CPubKey
An encapsulated public key.
Definition: pubkey.h:37
READWRITE
#define READWRITE(obj)
Definition: serialize.h:164
CWalletDB::ReorderTransactions
DBErrors ReorderTransactions(CWallet *pwallet)
Definition: walletdb.cpp:466
CHDChain
Definition: hdchain.h:11
CWalletDB::WriteMSettings
bool WriteMSettings(bool fMultiSendStake, bool fMultiSendMasternode, int nLastMultiSendHeight)
Definition: walletdb.cpp:225
key
CKey key
Definition: bip38tooldialog.cpp:173
CWalletDB::WriteTx
bool WriteTx(uint256 hash, const CWalletTx &wtx)
Definition: walletdb.cpp:76
CWallet
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,...
Definition: wallet.h:243
CKeyMetadata::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: walletdb.h:62
CWalletTx
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:792
CWalletDB::Write2FASecret
bool Write2FASecret(std::string secret)
Definition: walletdb.cpp:326
CWalletDB::Compact
static bool Compact(CDBEnv &dbenv, const std::string &strFile)
Definition: walletdb.cpp:1180
CKeyMetadata::CKeyMetadata
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:56
CWalletDB::WriteDestData
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:1265
CWalletDB::WriteStakeSplitThreshold
bool WriteStakeSplitThreshold(uint64_t nStakeSplitThreshold)
Definition: walletdb.cpp:192
CWalletDB::ReadTxPrivateKey
bool ReadTxPrivateKey(const std::string &outpointKey, std::string &k)
Definition: walletdb.cpp:1276
CWalletDB::Recover
static bool Recover(CDBEnv &dbenv, std::string filename, bool fOnlyKeys)
Definition: walletdb.cpp:1189
CKeyMetadata::nCreateTime
int64_t nCreateTime
Definition: walletdb.h:50
CWalletDB::EraseMSDisabledAddresses
bool EraseMSDisabledAddresses(std::vector< std::string > vDisabledAddresses)
Definition: walletdb.cpp:245
CWalletDB::GetUpdateCounter
static unsigned int GetUpdateCounter()
Definition: walletdb.cpp:1331
CWalletDB::EraseDestData
bool EraseDestData(const std::string &address, const std::string &key)
Erase destination data tuple from wallet database.
Definition: walletdb.cpp:1291
CWalletDB::WriteReserveAmount
bool WriteReserveAmount(const double &amount)
Definition: walletdb.cpp:161
CBlockLocator
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:249
CKeyMetadata::CURRENT_VERSION
static const int CURRENT_VERSION
Definition: walletdb.h:48
CWalletDB::WriteHDPubKey
bool WriteHDPubKey(const CHDPubKey &hdPubKey, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:1316
CWalletDB::ReadStealthAccount
bool ReadStealthAccount(const std::string &strAccount, CStealthAccount &account)
Definition: walletdb.cpp:388
CWalletDB::WriteStealthAccount
bool WriteStealthAccount(const std::string &strAccount, const CStealthAccount &account)
Definition: walletdb.cpp:396
amount.h
CWalletDB::ReadStealthAccountList
bool ReadStealthAccountList(std::string &accountList)
Definition: walletdb.cpp:46
DB_CORRUPT
@ DB_CORRUPT
Definition: walletdb.h:38
CWalletDB::WriteScannedBlockHeight
bool WriteScannedBlockHeight(int height)
Definition: walletdb.cpp:304
CKeyMetadata
Definition: walletdb.h:45
CKeyMetadata::SetNull
void SetNull()
Definition: walletdb.h:72
CWalletDB::AppendStealthAccountList
bool AppendStealthAccountList(const std::string &accountName)
Definition: walletdb.cpp:33
CWalletDB::WriteStakingStatus
bool WriteStakingStatus(bool status)
Definition: walletdb.cpp:292
CWalletDB::WriteMinVersion
bool WriteMinVersion(int nVersion)
Definition: walletdb.cpp:287
CWalletDB::WriteTxPrivateKey
bool WriteTxPrivateKey(const std::string &outpointKey, const std::string &k)
Definition: walletdb.cpp:1271
CWalletDB::WriteName
bool WriteName(const std::string &strAddress, const std::string &strName)
Definition: walletdb.cpp:50
CWalletDB::WriteMultiSend
bool WriteMultiSend(std::vector< std::pair< std::string, int > > vMultiSend)
Definition: walletdb.cpp:199
BackupWallet
bool BackupWallet(const CWallet &wallet, const fs::path &strDest, bool fEnableCustom=true)
Definition: walletdb.cpp:1045
CDB::strFile
std::string strFile
Definition: db.h:99
CWalletDB::WriteHDChain
bool WriteHDChain(const CHDChain &chain)
Definition: walletdb.cpp:1297
CWalletDB::EraseName
bool EraseName(const std::string &strAddress)
Definition: walletdb.cpp:56