Fabcoin Core  0.16.2
P2P Digital Currency
walletdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 FABCOIN_WALLET_WALLETDB_H
7 #define FABCOIN_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 
35 static const bool DEFAULT_FLUSHWALLET = true;
36 
37 class CAccount;
38 class CAccountingEntry;
39 struct CBlockLocator;
40 class CKeyPool;
41 class CMasterKey;
42 class CScript;
43 class CWallet;
44 class CWalletTx;
45 class CTokenInfo;
46 class CTokenTx;
47 class uint160;
48 class uint256;
49 
52 {
59 };
60 
61 /* simple HD chain data model */
62 class CHDChain
63 {
64 public:
68 
69  static const int VERSION_HD_BASE = 1;
70  static const int VERSION_HD_CHAIN_SPLIT = 2;
72  int nVersion;
73 
74  CHDChain() { SetNull(); }
76  template <typename Stream, typename Operation>
77  inline void SerializationOp(Stream& s, Operation ser_action)
78  {
79  READWRITE(this->nVersion);
80  READWRITE(nExternalChainCounter);
81  READWRITE(masterKeyID);
82  if (this->nVersion >= VERSION_HD_CHAIN_SPLIT)
83  READWRITE(nInternalChainCounter);
84  }
85 
86  void SetNull()
87  {
88  nVersion = CHDChain::CURRENT_VERSION;
89  nExternalChainCounter = 0;
90  nInternalChainCounter = 0;
91  masterKeyID.SetNull();
92  }
93 };
94 
96 {
97 public:
98  static const int VERSION_BASIC=1;
99  static const int VERSION_WITH_HDDATA=10;
100  static const int CURRENT_VERSION=VERSION_WITH_HDDATA;
101  int nVersion;
102  int64_t nCreateTime; // 0 means unknown
103  std::string hdKeypath; //optional HD/bip32 keypath
104  CKeyID hdMasterKeyID; //id of the HD masterkey used to derive this key
105 
107  {
108  SetNull();
109  }
110  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(hdMasterKeyID);
126  }
127  }
128 
129  void SetNull()
130  {
132  nCreateTime = 0;
133  hdKeypath.clear();
134  hdMasterKeyID.SetNull();
135  }
136 };
137 
144 {
145 private:
146  template <typename K, typename T>
147  bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
148  {
149  if (!batch.Write(key, value, fOverwrite)) {
150  return false;
151  }
152  m_dbw.IncrementUpdateCounter();
153  return true;
154  }
155 
156  template <typename K>
157  bool EraseIC(const K& key)
158  {
159  if (!batch.Erase(key)) {
160  return false;
161  }
162  m_dbw.IncrementUpdateCounter();
163  return true;
164  }
165 
166 public:
167  CWalletDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool _fFlushOnClose = true) :
168  batch(dbw, pszMode, _fFlushOnClose),
169  m_dbw(dbw)
170  {
171  }
172 
173  bool WriteName(const std::string& strAddress, const std::string& strName);
174  bool EraseName(const std::string& strAddress);
175 
176  bool WritePurpose(const std::string& strAddress, const std::string& purpose);
177  bool ErasePurpose(const std::string& strAddress);
178 
179  bool WriteTx(const CWalletTx& wtx);
180  bool EraseTx(uint256 hash);
181 
182  bool WriteToken(const CTokenInfo& wtoken);
183  bool EraseToken(uint256 hash);
184 
185  bool WriteTokenTx(const CTokenTx& wTokenTx);
186  bool EraseTokenTx(uint256 hash);
187 
188  bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
189  bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
190  bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
191 
192  bool WriteCScript(const uint160& hash, const CScript& redeemScript);
193 
194  bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
195  bool EraseWatchOnly(const CScript &script);
196 
197  bool WriteBestBlock(const CBlockLocator& locator);
198  bool ReadBestBlock(CBlockLocator& locator);
199 
200  bool WriteOrderPosNext(int64_t nOrderPosNext);
201 
202  bool WriteDefaultKey(const CPubKey& vchPubKey);
203 
204  bool ReadPool(int64_t nPool, CKeyPool& keypool);
205  bool WritePool(int64_t nPool, const CKeyPool& keypool);
206  bool ErasePool(int64_t nPool);
207 
208  bool WriteMinVersion(int nVersion);
209 
212  bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
213  bool ReadAccount(const std::string& strAccount, CAccount& account);
214  bool WriteAccount(const std::string& strAccount, const CAccount& account);
215 
217  bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
219  bool EraseDestData(const std::string &address, const std::string &key);
220 
222  bool WriteContractData(const std::string &address, const std::string &key, const std::string &value);
224  bool EraseContractData(const std::string &address, const std::string &key);
225 
226 
227  CAmount GetAccountCreditDebit(const std::string& strAccount);
228  void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
229 
230  DBErrors LoadWallet(CWallet* pwallet);
231  DBErrors FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
232  DBErrors ZapWalletTx(std::vector<CWalletTx>& vWtx);
233  DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
234  /* Try to (very carefully!) recover wallet database (with a possible key type filter) */
235  static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
236  /* Recover convenience-function to bypass the key filter callback, called when verify fails, recovers everything */
237  static bool Recover(const std::string& filename, std::string& out_backup_filename);
238  /* Recover filter (used as callback), will only let keys (cryptographical keys) as KV/key-type pass through */
239  static bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue);
240  /* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
241  static bool IsKeyType(const std::string& strType);
242  /* verifies the database environment */
243  static bool VerifyEnvironment(const std::string& walletFile, const fs::path& dataDir, std::string& errorStr);
244  /* verifies the database file */
245  static bool VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr);
246 
248  bool WriteHDChain(const CHDChain& chain);
249 
251  bool TxnBegin();
253  bool TxnCommit();
255  bool TxnAbort();
257  bool ReadVersion(int& nVersion);
259  bool WriteVersion(int nVersion);
260 private:
263 
264  CWalletDB(const CWalletDB&);
265  void operator=(const CWalletDB&);
266 };
267 
269 void MaybeCompactWalletDB();
270 
271 #endif // FABCOIN_WALLET_WALLETDB_H
static const int CURRENT_VERSION
Definition: walletdb.h:71
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:110
CKeyID masterKeyID
master key hash160
Definition: walletdb.h:67
Account information.
Definition: wallet.h:1244
std::string hdKeypath
Definition: walletdb.h:103
void SetNull()
Definition: uint256.h:46
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:251
#define READWRITE(obj)
Definition: serialize.h:179
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:147
#define T(i, x)
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:32
bool EraseIC(const K &key)
Definition: walletdb.h:157
CKeyID hdMasterKeyID
Definition: walletdb.h:104
uint32_t nExternalChainCounter
Definition: walletdb.h:65
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:146
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:51
ADD_SERIALIZE_METHODS
Definition: walletdb.h:75
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletdb.h:77
int64_t CAmount
Amount in lius (Can be negative)
Definition: amount.h:15
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:32
An instance of this class represents one database.
Definition: db.h:93
CWalletDB(CWalletDBWrapper &dbw, const char *pszMode="r+", bool _fFlushOnClose=true)
Definition: walletdb.h:167
static const int CURRENT_VERSION
Definition: walletdb.h:100
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:788
An encapsulated public key.
Definition: pubkey.h:39
ADD_SERIALIZE_METHODS
Definition: walletdb.h:116
RAII class that provides access to a Berkeley database.
Definition: db.h:145
int64_t nCreateTime
Definition: walletdb.h:102
Access to the wallet database.
Definition: walletdb.h:143
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:287
int nVersion
Definition: walletdb.h:101
256-bit opaque blob.
Definition: uint256.h:132
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:417
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
Internal transfers.
Definition: wallet.h:589
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:672
160-bit opaque blob.
Definition: uint256.h:120
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletdb.h:119
static const int VERSION_HD_CHAIN_SPLIT
Definition: walletdb.h:70
CHDChain()
Definition: walletdb.h:74
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
void SetNull()
Definition: walletdb.h:86
int nVersion
Definition: walletdb.h:72
void SetNull()
Definition: walletdb.h:129
CDB batch
Definition: walletdb.h:261
CWalletDBWrapper & m_dbw
Definition: walletdb.h:262
static const int VERSION_HD_BASE
Definition: walletdb.h:69
uint32_t nInternalChainCounter
Definition: walletdb.h:66
A key pool entry.
Definition: wallet.h:120