1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_WALLETDB_H
7 #define BITCOIN_WALLETDB_H
20 class CAccountingEntry;
30 /** Error statuses for the wallet database */
44 static const int CURRENT_VERSION=1;
46 int64_t nCreateTime; // 0 means unknown
52 CKeyMetadata(int64_t nCreateTime_)
54 nVersion = CKeyMetadata::CURRENT_VERSION;
55 nCreateTime = nCreateTime_;
58 ADD_SERIALIZE_METHODS;
60 template <typename Stream, typename Operation>
61 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
62 READWRITE(this->nVersion);
63 nVersion = this->nVersion;
64 READWRITE(nCreateTime);
69 nVersion = CKeyMetadata::CURRENT_VERSION;
74 /** Access to the wallet database (wallet.dat) */
75 class CWalletDB : public CDB
78 CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
82 CWalletDB(const CWalletDB&);
83 void operator=(const CWalletDB&);
85 bool WriteName(const std::string& strAddress, const std::string& strName);
86 bool EraseName(const std::string& strAddress);
88 bool WritePurpose(const std::string& strAddress, const std::string& purpose);
89 bool ErasePurpose(const std::string& strAddress);
91 bool WriteTx(uint256 hash, const CWalletTx& wtx);
92 bool EraseTx(uint256 hash);
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);
98 bool WriteCScript(const uint160& hash, const CScript& redeemScript);
100 bool WriteWatchOnly(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 ReadPool(int64_t nPool, CKeyPool& keypool);
110 bool WritePool(int64_t nPool, const CKeyPool& keypool);
111 bool ErasePool(int64_t nPool);
113 bool WriteMinVersion(int nVersion);
115 bool ReadAccount(const std::string& strAccount, CAccount& account);
116 bool WriteAccount(const std::string& strAccount, const CAccount& account);
118 /// Write destination data key,value tuple to database
119 bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
120 /// Erase destination data tuple from wallet database
121 bool EraseDestData(const std::string &address, const std::string &key);
123 bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
125 bool WriteAccountingEntry(const CAccountingEntry& acentry);
126 int64_t GetAccountCreditDebit(const std::string& strAccount);
127 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
129 DBErrors ReorderTransactions(CWallet*);
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, std::string filename, bool fOnlyKeys);
134 static bool Recover(CDBEnv& dbenv, std::string filename);
137 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
139 #endif // BITCOIN_WALLETDB_H