1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 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.
5 #ifndef BITCOIN_WALLETDB_H
6 #define BITCOIN_WALLETDB_H
13 class CAccountingEntry;
15 /** Error statuses for the wallet database */
26 /** Access to the wallet database (wallet.dat) */
27 class CWalletDB : public CDB
30 CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
34 CWalletDB(const CWalletDB&);
35 void operator=(const CWalletDB&);
37 bool WriteName(const std::string& strAddress, const std::string& strName);
39 bool EraseName(const std::string& strAddress);
41 bool WriteTx(uint256 hash, const CWalletTx& wtx)
44 return Write(std::make_pair(std::string("tx"), hash), wtx);
47 bool EraseTx(uint256 hash)
50 return Erase(std::make_pair(std::string("tx"), hash));
53 bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey)
56 return Write(std::make_pair(std::string("key"), vchPubKey.Raw()), vchPrivKey, false);
59 bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, bool fEraseUnencryptedKey = true)
62 if (!Write(std::make_pair(std::string("ckey"), vchPubKey.Raw()), vchCryptedSecret, false))
64 if (fEraseUnencryptedKey)
66 Erase(std::make_pair(std::string("key"), vchPubKey.Raw()));
67 Erase(std::make_pair(std::string("wkey"), vchPubKey.Raw()));
72 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
75 return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
78 bool WriteCScript(const uint160& hash, const CScript& redeemScript)
81 return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
84 bool WriteBestBlock(const CBlockLocator& locator)
87 return Write(std::string("bestblock"), locator);
90 bool ReadBestBlock(CBlockLocator& locator)
92 return Read(std::string("bestblock"), locator);
95 bool WriteOrderPosNext(int64 nOrderPosNext)
98 return Write(std::string("orderposnext"), nOrderPosNext);
101 bool WriteDefaultKey(const CPubKey& vchPubKey)
104 return Write(std::string("defaultkey"), vchPubKey.Raw());
107 bool ReadPool(int64 nPool, CKeyPool& keypool)
109 return Read(std::make_pair(std::string("pool"), nPool), keypool);
112 bool WritePool(int64 nPool, const CKeyPool& keypool)
115 return Write(std::make_pair(std::string("pool"), nPool), keypool);
118 bool ErasePool(int64 nPool)
121 return Erase(std::make_pair(std::string("pool"), nPool));
124 // Settings are no longer stored in wallet.dat; these are
125 // used only for backwards compatibility:
127 bool ReadSetting(const std::string& strKey, T& value)
129 return Read(std::make_pair(std::string("setting"), strKey), value);
132 bool WriteSetting(const std::string& strKey, const T& value)
135 return Write(std::make_pair(std::string("setting"), strKey), value);
137 bool EraseSetting(const std::string& strKey)
140 return Erase(std::make_pair(std::string("setting"), strKey));
143 bool WriteMinVersion(int nVersion)
145 return Write(std::string("minversion"), nVersion);
148 bool ReadAccount(const std::string& strAccount, CAccount& account);
149 bool WriteAccount(const std::string& strAccount, const CAccount& account);
151 bool WriteAccountingEntry(const uint64 nAccEntryNum, const CAccountingEntry& acentry);
153 bool WriteAccountingEntry(const CAccountingEntry& acentry);
154 int64 GetAccountCreditDebit(const std::string& strAccount);
155 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
157 DBErrors ReorderTransactions(CWallet*);
158 DBErrors LoadWallet(CWallet* pwallet);
159 static bool Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys);
160 static bool Recover(CDBEnv& dbenv, std::string filename);
163 #endif // BITCOIN_WALLETDB_H