1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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.
6 #ifndef BITCOIN_WALLET_WALLETDB_H
7 #define BITCOIN_WALLET_WALLETDB_H
10 #include "wallet/db.h"
13 #include "zcash/Address.hpp"
22 class CAccountingEntry;
32 /** Error statuses for the wallet database */
46 static const int CURRENT_VERSION=1;
48 int64_t nCreateTime; // 0 means unknown
54 CKeyMetadata(int64_t nCreateTime_)
56 nVersion = CKeyMetadata::CURRENT_VERSION;
57 nCreateTime = nCreateTime_;
60 ADD_SERIALIZE_METHODS;
62 template <typename Stream, typename Operation>
63 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
64 READWRITE(this->nVersion);
65 nVersion = this->nVersion;
66 READWRITE(nCreateTime);
71 nVersion = CKeyMetadata::CURRENT_VERSION;
76 /** Access to the wallet database (wallet.dat) */
77 class CWalletDB : public CDB
80 CWalletDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnClose = true) : CDB(strFilename, pszMode, fFlushOnClose)
84 bool WriteName(const std::string& strAddress, const std::string& strName);
85 bool EraseName(const std::string& strAddress);
87 bool WritePurpose(const std::string& strAddress, const std::string& purpose);
88 bool ErasePurpose(const std::string& strAddress);
90 bool WriteTx(uint256 hash, const CWalletTx& wtx);
91 bool EraseTx(uint256 hash);
93 bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
94 bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
95 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
97 bool WriteCScript(const uint160& hash, const CScript& redeemScript);
99 bool WriteWatchOnly(const CScript &script);
100 bool EraseWatchOnly(const CScript &script);
102 bool WriteBestBlock(const CBlockLocator& locator);
103 bool ReadBestBlock(CBlockLocator& locator);
105 bool WriteOrderPosNext(int64_t nOrderPosNext);
107 bool WriteDefaultKey(const CPubKey& vchPubKey);
109 bool WriteWitnessCacheSize(int64_t nWitnessCacheSize);
111 bool ReadPool(int64_t nPool, CKeyPool& keypool);
112 bool WritePool(int64_t nPool, const CKeyPool& keypool);
113 bool ErasePool(int64_t nPool);
115 bool WriteMinVersion(int nVersion);
117 bool ReadAccount(const std::string& strAccount, CAccount& account);
118 bool WriteAccount(const std::string& strAccount, const CAccount& account);
120 /// Write destination data key,value tuple to database
121 bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
122 /// Erase destination data tuple from wallet database
123 bool EraseDestData(const std::string &address, const std::string &key);
125 bool WriteAccountingEntry(const CAccountingEntry& acentry);
126 CAmount GetAccountCreditDebit(const std::string& strAccount);
127 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
129 DBErrors ReorderTransactions(CWallet* pwallet);
130 DBErrors LoadWallet(CWallet* pwallet);
131 DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
132 DBErrors ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx);
133 static bool Recover(CDBEnv& dbenv, const std::string& filename, bool fOnlyKeys);
134 static bool Recover(CDBEnv& dbenv, const std::string& filename);
136 /// Write spending key to wallet database, where key is payment address and value is spending key.
137 bool WriteZKey(const libzcash::PaymentAddress& addr, const libzcash::SpendingKey& key, const CKeyMetadata &keyMeta);
138 bool WriteCryptedZKey(const libzcash::PaymentAddress & addr,
139 const libzcash::ReceivingKey & rk,
140 const std::vector<unsigned char>& vchCryptedSecret,
141 const CKeyMetadata &keyMeta);
144 CWalletDB(const CWalletDB&);
145 void operator=(const CWalletDB&);
147 bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
150 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
151 void ThreadFlushWalletDB(const std::string& strFile);
153 #endif // BITCOIN_WALLET_WALLETDB_H