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
12 class CAccountingEntry;
14 /** Error statuses for the wallet database */
24 /** Access to the wallet database (wallet.dat) */
25 class CWalletDB : public CDB
28 CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
32 CWalletDB(const CWalletDB&);
33 void operator=(const CWalletDB&);
35 bool ReadName(const std::string& strAddress, std::string& strName)
38 return Read(std::make_pair(std::string("name"), strAddress), strName);
41 bool WriteName(const std::string& strAddress, const std::string& strName);
43 bool EraseName(const std::string& strAddress);
45 bool ReadTx(uint256 hash, CWalletTx& wtx)
47 return Read(std::make_pair(std::string("tx"), hash), wtx);
50 bool WriteTx(uint256 hash, const CWalletTx& wtx)
53 return Write(std::make_pair(std::string("tx"), hash), wtx);
56 bool EraseTx(uint256 hash)
59 return Erase(std::make_pair(std::string("tx"), hash));
62 bool ReadKey(const std::vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)
65 return Read(std::make_pair(std::string("key"), vchPubKey), vchPrivKey);
68 bool WriteKey(const std::vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)
71 return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false);
74 bool WriteCryptedKey(const std::vector<unsigned char>& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, bool fEraseUnencryptedKey = true)
77 if (!Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, false))
79 if (fEraseUnencryptedKey)
81 Erase(std::make_pair(std::string("key"), vchPubKey));
82 Erase(std::make_pair(std::string("wkey"), vchPubKey));
87 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
90 return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
93 // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
94 bool ReadCScript(const uint160 &hash, CScript& redeemScript)
97 return Read(std::make_pair(std::string("cscript"), hash), redeemScript);
100 bool WriteCScript(const uint160& hash, const CScript& redeemScript)
103 return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
106 bool WriteBestBlock(const CBlockLocator& locator)
109 return Write(std::string("bestblock"), locator);
112 bool ReadBestBlock(CBlockLocator& locator)
114 return Read(std::string("bestblock"), locator);
117 bool ReadDefaultKey(std::vector<unsigned char>& vchPubKey)
120 return Read(std::string("defaultkey"), vchPubKey);
123 bool WriteDefaultKey(const std::vector<unsigned char>& vchPubKey)
126 return Write(std::string("defaultkey"), vchPubKey);
129 bool ReadPool(int64 nPool, CKeyPool& keypool)
131 return Read(std::make_pair(std::string("pool"), nPool), keypool);
134 bool WritePool(int64 nPool, const CKeyPool& keypool)
137 return Write(std::make_pair(std::string("pool"), nPool), keypool);
140 bool ErasePool(int64 nPool)
143 return Erase(std::make_pair(std::string("pool"), nPool));
146 // Settings are no longer stored in wallet.dat; these are
147 // used only for backwards compatibility:
149 bool ReadSetting(const std::string& strKey, T& value)
151 return Read(std::make_pair(std::string("setting"), strKey), value);
154 bool WriteSetting(const std::string& strKey, const T& value)
157 return Write(std::make_pair(std::string("setting"), strKey), value);
159 bool EraseSetting(const std::string& strKey)
162 return Erase(std::make_pair(std::string("setting"), strKey));
165 bool WriteMinVersion(int nVersion)
167 return Write(std::string("minversion"), nVersion);
170 bool ReadAccount(const std::string& strAccount, CAccount& account);
171 bool WriteAccount(const std::string& strAccount, const CAccount& account);
172 bool WriteAccountingEntry(const CAccountingEntry& acentry);
173 int64 GetAccountCreditDebit(const std::string& strAccount);
174 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
176 int LoadWallet(CWallet* pwallet);
179 #endif // BITCOIN_WALLETDB_H