Raven Core  3.0.0
P2P Digital Currency
walletdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef RAVEN_WALLET_WALLETDB_H
8 #define RAVEN_WALLET_WALLETDB_H
9 
10 #include "amount.h"
11 #include "primitives/transaction.h"
12 #include "wallet/db.h"
13 #include "key.h"
14 
15 #include <list>
16 #include <stdint.h>
17 #include <string>
18 #include <utility>
19 #include <vector>
20 
36 static const bool DEFAULT_FLUSHWALLET = true;
37 
38 class CAccount;
39 class CAccountingEntry;
40 struct CBlockLocator;
41 class CKeyPool;
42 class CMasterKey;
43 class CScript;
44 class CWallet;
45 class CWalletTx;
46 class uint160;
47 class uint256;
48 
51 {
58 };
59 
60 /* simple HD chain data model */
61 class CHDChain
62 {
63 public:
67 
68  static const int VERSION_HD_BASE = 1;
69  static const int VERSION_HD_CHAIN_SPLIT = 2;
71  int nVersion;
72 
73  CHDChain() { SetNull(); }
75  template <typename Stream, typename Operation>
76  inline void SerializationOp(Stream& s, Operation ser_action)
77  {
78  READWRITE(this->nVersion);
79  READWRITE(nExternalChainCounter);
80  READWRITE(seed_id);
81  if (this->nVersion >= VERSION_HD_CHAIN_SPLIT)
82  READWRITE(nInternalChainCounter);
83  }
84 
85  void SetNull()
86  {
87  nVersion = CHDChain::CURRENT_VERSION;
88  nExternalChainCounter = 0;
89  nInternalChainCounter = 0;
90  seed_id.SetNull();
91  }
92 };
93 
95 {
96 public:
97  static const int VERSION_BASIC=1;
98  static const int VERSION_WITH_HDDATA=10;
99  static const int CURRENT_VERSION=VERSION_WITH_HDDATA;
100  int nVersion;
101  int64_t nCreateTime; // 0 means unknown
102  std::string hdKeypath; //optional HD/bip32 keypath
103  CKeyID hd_seed_id; //id of the HD seed used to derive this key
104 
106  {
107  SetNull();
108  }
109  explicit CKeyMetadata(int64_t nCreateTime_)
110  {
111  SetNull();
112  nCreateTime = nCreateTime_;
113  }
114 
116 
117  template <typename Stream, typename Operation>
118  inline void SerializationOp(Stream& s, Operation ser_action) {
119  READWRITE(this->nVersion);
120  READWRITE(nCreateTime);
121  if (this->nVersion >= VERSION_WITH_HDDATA)
122  {
123  READWRITE(hdKeypath);
124  READWRITE(hd_seed_id);
125  }
126  }
127 
128  void SetNull()
129  {
131  nCreateTime = 0;
132  hdKeypath.clear();
133  hd_seed_id.SetNull();
134  }
135 };
136 
143 {
144 private:
145  template <typename K, typename T>
146  bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
147  {
148  if (!batch.Write(key, value, fOverwrite)) {
149  return false;
150  }
151  m_dbw.IncrementUpdateCounter();
152  return true;
153  }
154 
155  template <typename K>
156  bool EraseIC(const K& key)
157  {
158  if (!batch.Erase(key)) {
159  return false;
160  }
161  m_dbw.IncrementUpdateCounter();
162  return true;
163  }
164 
165 public:
166  explicit CWalletDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool _fFlushOnClose = true) :
167  batch(dbw, pszMode, _fFlushOnClose),
168  m_dbw(dbw)
169  {
170  }
171  CWalletDB(const CWalletDB&) = delete;
172  CWalletDB& operator=(const CWalletDB&) = delete;
173 
174  bool WriteName(const std::string& strAddress, const std::string& strName);
175  bool EraseName(const std::string& strAddress);
176 
177  bool WritePurpose(const std::string& strAddress, const std::string& purpose);
178  bool ErasePurpose(const std::string& strAddress);
179 
180  bool WriteTx(const CWalletTx& wtx);
181  bool EraseTx(uint256 hash);
182 
183  bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
184  bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
185  bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
186 
187  bool WriteCScript(const uint160& hash, const CScript& redeemScript);
188 
189  bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
190  bool EraseWatchOnly(const CScript &script);
191 
192  bool WriteBestBlock(const CBlockLocator& locator);
193  bool ReadBestBlock(CBlockLocator& locator);
194 
195  bool WriteOrderPosNext(int64_t nOrderPosNext);
196 
197  bool ReadPool(int64_t nPool, CKeyPool& keypool);
198  bool WritePool(int64_t nPool, const CKeyPool& keypool);
199  bool ErasePool(int64_t nPool);
200 
201  bool WriteMinVersion(int nVersion);
202 
205  bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
206  bool ReadAccount(const std::string& strAccount, CAccount& account);
207  bool WriteAccount(const std::string& strAccount, const CAccount& account);
208 
210  bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
212  bool EraseDestData(const std::string &address, const std::string &key);
213 
214  CAmount GetAccountCreditDebit(const std::string& strAccount);
215  void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
216 
217  DBErrors LoadWallet(CWallet* pwallet);
218  DBErrors FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
219  DBErrors ZapWalletTx(std::vector<CWalletTx>& vWtx);
220  DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
221  /* Try to (very carefully!) recover wallet database (with a possible key type filter) */
222  static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
223  /* Recover convenience-function to bypass the key filter callback, called when verify fails, recovers everything */
224  static bool Recover(const std::string& filename, std::string& out_backup_filename);
225  /* Recover filter (used as callback), will only let keys (cryptographical keys) as KV/key-type pass through */
226  static bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue);
227  /* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
228  static bool IsKeyType(const std::string& strType);
229  /* verifies the database environment */
230  static bool VerifyEnvironment(const std::string& walletFile, const fs::path& dataDir, std::string& errorStr);
231  /* verifies the database file */
232  static bool VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr);
233 
235  bool WriteHDChain(const CHDChain& chain);
236 
238  bool TxnBegin();
240  bool TxnCommit();
242  bool TxnAbort();
244  bool ReadVersion(int& nVersion);
246  bool WriteVersion(int nVersion);
247 private:
250 };
251 
253 void MaybeCompactWalletDB();
254 
255 #endif // RAVEN_WALLET_WALLETDB_H
static const int CURRENT_VERSION
Definition: walletdb.h:70
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:109
Account information.
Definition: wallet.h:1228
std::string hdKeypath
Definition: walletdb.h:102
void SetNull()
Definition: uint256.h:41
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:132
#define READWRITE(obj)
Definition: serialize.h:163
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:146
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:35
bool EraseIC(const K &key)
Definition: walletdb.h:156
uint32_t nExternalChainCounter
Definition: walletdb.h:64
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:50
ADD_SERIALIZE_METHODS
Definition: walletdb.h:74
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletdb.h:76
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
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:33
An instance of this class represents one database.
Definition: db.h:94
CWalletDB(CWalletDBWrapper &dbw, const char *pszMode="r+", bool _fFlushOnClose=true)
Definition: walletdb.h:166
static const int CURRENT_VERSION
Definition: walletdb.h:99
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:757
An encapsulated public key.
Definition: pubkey.h:40
ADD_SERIALIZE_METHODS
Definition: walletdb.h:115
RAII class that provides access to a Berkeley database.
Definition: db.h:146
int64_t nCreateTime
Definition: walletdb.h:101
Access to the wallet database.
Definition: walletdb.h:142
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:285
int nVersion
Definition: walletdb.h:100
256-bit opaque blob.
Definition: uint256.h:123
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:30
Internal transfers.
Definition: wallet.h:590
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:673
160-bit opaque blob.
Definition: uint256.h:112
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletdb.h:118
static const int VERSION_HD_CHAIN_SPLIT
Definition: walletdb.h:69
CHDChain()
Definition: walletdb.h:73
void SetNull()
Definition: walletdb.h:85
CKeyID hd_seed_id
Definition: walletdb.h:103
CKeyID seed_id
seed hash160
Definition: walletdb.h:66
int nVersion
Definition: walletdb.h:71
void SetNull()
Definition: walletdb.h:128
CDB batch
Definition: walletdb.h:248
CWalletDBWrapper & m_dbw
Definition: walletdb.h:249
static const int VERSION_HD_BASE
Definition: walletdb.h:68
uint32_t nInternalChainCounter
Definition: walletdb.h:65
A key pool entry.
Definition: wallet.h:107