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
14 class CAccountingEntry;
16 /** Error statuses for the wallet database */
30 static const int CURRENT_VERSION=1;
32 int64_t nCreateTime; // 0 means unknown
38 CKeyMetadata(int64_t nCreateTime_)
40 nVersion = CKeyMetadata::CURRENT_VERSION;
41 nCreateTime = nCreateTime_;
46 READWRITE(this->nVersion);
47 nVersion = this->nVersion;
48 READWRITE(nCreateTime);
53 nVersion = CKeyMetadata::CURRENT_VERSION;
59 /** Access to the wallet database (wallet.dat) */
60 class CWalletDB : public CDB
63 CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
67 CWalletDB(const CWalletDB&);
68 void operator=(const CWalletDB&);
70 bool WriteName(const std::string& strAddress, const std::string& strName);
72 bool EraseName(const std::string& strAddress);
74 bool WriteTx(uint256 hash, const CWalletTx& wtx)
77 return Write(std::make_pair(std::string("tx"), hash), wtx);
80 bool EraseTx(uint256 hash)
83 return Erase(std::make_pair(std::string("tx"), hash));
86 bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta)
89 if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta))
92 if(!Write(std::make_pair(std::string("key"), vchPubKey.Raw()), vchPrivKey, false))
98 bool WriteMalleableKey(const CMalleableKeyView& keyView, const CSecret& vchSecretH, const CKeyMetadata &keyMeta)
101 if(!Write(std::make_pair(std::string("malmeta"), keyView.ToString()), keyMeta))
104 if(!Write(std::make_pair(std::string("malpair"), keyView.ToString()), vchSecretH, false))
110 bool WriteCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char>& vchCryptedSecretH, const CKeyMetadata &keyMeta)
113 if(!Write(std::make_pair(std::string("malmeta"), keyView.ToString()), keyMeta))
116 if(!Write(std::make_pair(std::string("malcpair"), keyView.ToString()), vchCryptedSecretH, false))
119 Erase(std::make_pair(std::string("malpair"), keyView.ToString()));
125 bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta)
128 bool fEraseUnencryptedKey = true;
130 if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta))
133 if (!Write(std::make_pair(std::string("ckey"), vchPubKey.Raw()), vchCryptedSecret, false))
135 if (fEraseUnencryptedKey)
137 Erase(std::make_pair(std::string("key"), vchPubKey.Raw()));
138 Erase(std::make_pair(std::string("wkey"), vchPubKey.Raw()));
143 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
146 return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
149 bool EraseMasterKey(unsigned int nID)
152 return Erase(std::make_pair(std::string("mkey"), nID));
155 bool EraseCryptedKey(const CPubKey& vchPubKey)
157 return Erase(std::make_pair(std::string("ckey"), vchPubKey.Raw()));
160 bool EraseCryptedMalleableKey(const CMalleableKeyView& keyView)
162 return Erase(std::make_pair(std::string("malcpair"), keyView.ToString()));
165 bool WriteCScript(const uint160& hash, const CScript& redeemScript)
168 return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
171 bool WriteWatchOnly(const CScript &dest)
174 return Write(std::make_pair(std::string("watchs"), dest), '1');
177 bool EraseWatchOnly(const CScript &dest)
180 return Erase(std::make_pair(std::string("watchs"), dest));
183 bool WriteBestBlock(const CBlockLocator& locator)
186 return Write(std::string("bestblock"), locator);
189 bool ReadBestBlock(CBlockLocator& locator)
191 return Read(std::string("bestblock"), locator);
194 bool WriteOrderPosNext(int64_t nOrderPosNext)
197 return Write(std::string("orderposnext"), nOrderPosNext);
200 bool WriteDefaultKey(const CPubKey& vchPubKey)
203 return Write(std::string("defaultkey"), vchPubKey.Raw());
206 bool ReadPool(int64_t nPool, CKeyPool& keypool)
208 return Read(std::make_pair(std::string("pool"), nPool), keypool);
211 bool WritePool(int64_t nPool, const CKeyPool& keypool)
214 return Write(std::make_pair(std::string("pool"), nPool), keypool);
217 bool ErasePool(int64_t nPool)
220 return Erase(std::make_pair(std::string("pool"), nPool));
223 bool WriteMinVersion(int nVersion)
225 return Write(std::string("minversion"), nVersion);
228 bool ReadAccount(const std::string& strAccount, CAccount& account);
229 bool WriteAccount(const std::string& strAccount, const CAccount& account);
231 bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
233 bool WriteAccountingEntry(const CAccountingEntry& acentry);
234 int64_t GetAccountCreditDebit(const std::string& strAccount);
235 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
237 DBErrors ReorderTransactions(CWallet*);
238 DBErrors LoadWallet(CWallet* pwallet);
239 DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash);
240 DBErrors ZapWalletTx(CWallet* pwallet);
242 static bool Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys);
243 static bool Recover(CDBEnv& dbenv, std::string filename);
246 #endif // BITCOIN_WALLETDB_H