Dash Core  0.12.2.1
P2P Digital Currency
walletdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 "wallet/db.h"
11 #include "hdchain.h"
12 #include "key.h"
13 
14 #include <list>
15 #include <stdint.h>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 static const bool DEFAULT_FLUSHWALLET = true;
21 
22 class CAccount;
23 class CAccountingEntry;
24 struct CBlockLocator;
25 class CKeyPool;
26 class CMasterKey;
27 class CScript;
28 class CWallet;
29 class CWalletTx;
30 class uint160;
31 class uint256;
32 
35 {
42 };
43 
45 {
46 public:
47  static const int CURRENT_VERSION=1;
48  int nVersion;
49  int64_t nCreateTime; // 0 means unknown
50 
52  {
53  SetNull();
54  }
55  CKeyMetadata(int64_t nCreateTime_)
56  {
57  SetNull();
58  nCreateTime = nCreateTime_;
59  }
60 
62 
63  template <typename Stream, typename Operation>
64  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
65  READWRITE(this->nVersion);
66  nVersion = this->nVersion;
67  READWRITE(nCreateTime);
68  }
69 
70  void SetNull()
71  {
73  nCreateTime = 0;
74  }
75 };
76 
78 class CWalletDB : public CDB
79 {
80 public:
81  CWalletDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnClose = true) : CDB(strFilename, pszMode, fFlushOnClose)
82  {
83  }
84 
85  bool WriteName(const std::string& strAddress, const std::string& strName);
86  bool EraseName(const std::string& strAddress);
87 
88  bool WritePurpose(const std::string& strAddress, const std::string& purpose);
89  bool ErasePurpose(const std::string& strAddress);
90 
91  bool WriteTx(uint256 hash, const CWalletTx& wtx);
92  bool EraseTx(uint256 hash);
93 
94  bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
95  bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
96  bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
97 
98  bool WriteCScript(const uint160& hash, const CScript& redeemScript);
99 
100  bool WriteWatchOnly(const CScript &script);
101  bool EraseWatchOnly(const CScript &script);
102 
103  bool WriteBestBlock(const CBlockLocator& locator);
104  bool ReadBestBlock(CBlockLocator& locator);
105 
106  bool WriteOrderPosNext(int64_t nOrderPosNext);
107 
108  bool WriteDefaultKey(const CPubKey& vchPubKey);
109 
110  bool ReadPool(int64_t nPool, CKeyPool& keypool);
111  bool WritePool(int64_t nPool, const CKeyPool& keypool);
112  bool ErasePool(int64_t nPool);
113 
114  bool WriteMinVersion(int nVersion);
115 
118  bool WriteAccountingEntry_Backend(const CAccountingEntry& acentry);
119  bool ReadAccount(const std::string& strAccount, CAccount& account);
120  bool WriteAccount(const std::string& strAccount, const CAccount& account);
121 
123  bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
125  bool EraseDestData(const std::string &address, const std::string &key);
126 
127  CAmount GetAccountCreditDebit(const std::string& strAccount);
128  void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
129 
131  DBErrors LoadWallet(CWallet* pwallet);
132  DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
133  DBErrors ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx);
134  static bool Recover(CDBEnv& dbenv, const std::string& filename, bool fOnlyKeys);
135  static bool Recover(CDBEnv& dbenv, const std::string& filename);
136 
138  bool WriteHDChain(const CHDChain& chain);
139  bool WriteCryptedHDChain(const CHDChain& chain);
140  bool WriteHDPubKey(const CHDPubKey& hdPubKey, const CKeyMetadata& keyMeta);
141 
142 private:
143  CWalletDB(const CWalletDB&);
144  void operator=(const CWalletDB&);
145 
146  bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
147 };
148 
149 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
150 void ThreadFlushWalletDB(const std::string& strFile);
151 
152 bool AutoBackupWallet (CWallet* wallet, std::string strWalletFile, std::string& strBackupWarning, std::string& strBackupError);
153 
154 #endif // BITCOIN_WALLET_WALLETDB_H
bool ErasePurpose(const std::string &strAddress)
Definition: walletdb.cpp:52
bool WriteCScript(const uint160 &hash, const CScript &redeemScript)
Definition: walletdb.cpp:114
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry &acentry)
Definition: walletdb.cpp:190
#define READWRITE(obj)
Definition: serialize.h:175
bool AutoBackupWallet(CWallet *wallet, std::string strWalletFile, std::string &strBackupWarning, std::string &strBackupError)
Definition: walletdb.cpp:946
int nVersion
Definition: walletdb.h:48
bool BackupWallet(const CWallet &wallet, const std::string &strDest)
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:1152
bool WriteHDPubKey(const CHDPubKey &hdPubKey, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:1182
DBErrors ZapWalletTx(CWallet *pwallet, std::vector< CWalletTx > &vWtx)
Definition: walletdb.cpp:825
int64_t nCreateTime
Definition: walletdb.h:49
void SetNull()
Definition: walletdb.h:70
DBErrors
Definition: walletdb.h:34
bool WriteCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:87
static const int CURRENT_VERSION
Definition: walletdb.h:47
bool EraseTx(uint256 hash)
Definition: walletdb.cpp:64
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:145
Definition: db.h:28
bool ReadBestBlock(CBlockLocator &locator)
Definition: walletdb.cpp:139
Definition: db.h:94
CWalletDB(const std::string &strFilename, const char *pszMode="r+", bool fFlushOnClose=true)
Definition: walletdb.h:81
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: walletdb.h:64
bool WriteHDChain(const CHDChain &chain)
write the hdchain model (external chain child index counter)
Definition: walletdb.cpp:1164
DBErrors FindWalletTx(CWallet *pwallet, std::vector< uint256 > &vTxHash, std::vector< CWalletTx > &vWtx)
Definition: walletdb.cpp:759
int64_t CAmount
Definition: amount.h:14
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
Definition: key.h:32
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:648
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:55
void ListAccountCreditDebit(const std::string &strAccount, std::list< CAccountingEntry > &acentries)
Definition: walletdb.cpp:212
bool ReadAccount(const std::string &strAccount, CAccount &account)
Definition: walletdb.cpp:179
bool WritePool(int64_t nPool, const CKeyPool &keypool)
Definition: walletdb.cpp:162
void operator=(const CWalletDB &)
bool WriteCryptedHDChain(const CHDChain &chain)
Definition: walletdb.cpp:1170
ADD_SERIALIZE_METHODS
Definition: walletdb.h:61
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:132
static bool Recover(CDBEnv &dbenv, const std::string &filename, bool fOnlyKeys)
Definition: walletdb.cpp:1066
bool WriteTx(uint256 hash, const CWalletTx &wtx)
Definition: walletdb.cpp:58
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:108
bool WriteName(const std::string &strAddress, const std::string &strName)
Definition: walletdb.cpp:32
bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:70
bool EraseWatchOnly(const CScript &script)
Definition: walletdb.cpp:126
Definition: wallet.py:1
bool ErasePool(int64_t nPool)
Definition: walletdb.cpp:168
bool WriteMinVersion(int nVersion)
Definition: walletdb.cpp:174
bool WriteDefaultKey(const CPubKey &vchPubKey)
Definition: walletdb.cpp:151
bool WriteAccountingEntry_Backend(const CAccountingEntry &acentry)
Definition: walletdb.cpp:195
DBErrors ReorderTransactions(CWallet *pwallet)
Definition: walletdb.cpp:255
void ThreadFlushWalletDB(const std::string &strFile)
bool EraseName(const std::string &strAddress)
Definition: walletdb.cpp:38
bool fFlushOnClose
Definition: db.h:101
CAmount GetAccountCreditDebit(const std::string &strAccount)
Definition: walletdb.cpp:200
static const bool DEFAULT_FLUSHWALLET
Definition: walletdb.h:20
bool ReadPool(int64_t nPool, CKeyPool &keypool)
Definition: walletdb.cpp:157
bool WriteAccount(const std::string &strAccount, const CAccount &account)
Definition: walletdb.cpp:185
bool WriteWatchOnly(const CScript &script)
Definition: walletdb.cpp:120
Definition: pubkey.h:37
bool EraseDestData(const std::string &address, const std::string &key)
Erase destination data tuple from wallet database.
Definition: walletdb.cpp:1158
bool WritePurpose(const std::string &strAddress, const std::string &purpose)
Definition: walletdb.cpp:46
CKeyMetadata()
Definition: walletdb.h:51