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
15 class CAccountingEntry;
17 /** Error statuses for the wallet database */
31 static const int CURRENT_VERSION=1;
33 int64_t nCreateTime; // 0 means unknown
39 CKeyMetadata(int64_t nCreateTime_)
41 nVersion = CKeyMetadata::CURRENT_VERSION;
42 nCreateTime = nCreateTime_;
47 READWRITE(this->nVersion);
48 nVersion = this->nVersion;
49 READWRITE(nCreateTime);
54 nVersion = CKeyMetadata::CURRENT_VERSION;
60 /** Access to the wallet database (wallet.dat) */
61 class CWalletDB : public CDB
64 CWalletDB(string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
68 CWalletDB(const CWalletDB&);
69 void operator=(const CWalletDB&);
71 bool WriteName(const string& strAddress, const string& strName);
73 bool EraseName(const string& strAddress);
75 bool WriteTx(uint256 hash, const CWalletTx& wtx)
78 return Write(make_pair(string("tx"), hash), wtx);
81 bool EraseTx(uint256 hash)
84 return Erase(make_pair(string("tx"), hash));
87 bool WriteKey(const CPubKey& key, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta)
90 if(!Write(make_pair(string("keymeta"), key), keyMeta))
93 if(!Write(make_pair(string("key"), key), vchPrivKey, false))
99 bool WriteMalleableKey(const CMalleableKeyView& keyView, const CSecret& vchSecretH, const CKeyMetadata &keyMeta)
102 if(!Write(make_pair(string("malmeta"), keyView.ToString()), keyMeta))
105 if(!Write(make_pair(string("malpair"), keyView.ToString()), vchSecretH, false))
111 bool WriteCryptedMalleableKey(const CMalleableKeyView& keyView, const vector<unsigned char>& vchCryptedSecretH, const CKeyMetadata &keyMeta)
114 if(!Write(make_pair(string("malmeta"), keyView.ToString()), keyMeta))
117 if(!Write(make_pair(string("malcpair"), keyView.ToString()), vchCryptedSecretH, false))
120 Erase(make_pair(string("malpair"), keyView.ToString()));
126 bool WriteCryptedKey(const CPubKey& key, const vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta)
129 bool fEraseUnencryptedKey = true;
131 if(!Write(make_pair(string("keymeta"), key), keyMeta))
134 if (!Write(make_pair(string("ckey"), key), vchCryptedSecret, false))
136 if (fEraseUnencryptedKey)
138 Erase(make_pair(string("key"), key));
139 Erase(make_pair(string("wkey"), key));
144 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
147 return Write(make_pair(string("mkey"), nID), kMasterKey, true);
150 bool EraseMasterKey(unsigned int nID)
153 return Erase(make_pair(string("mkey"), nID));
156 bool EraseCryptedKey(const CPubKey& key)
158 return Erase(make_pair(string("ckey"), key));
161 bool EraseCryptedMalleableKey(const CMalleableKeyView& keyView)
163 return Erase(make_pair(string("malcpair"), keyView.ToString()));
166 bool WriteCScript(const uint160& hash, const CScript& redeemScript)
169 return Write(make_pair(string("cscript"), hash), redeemScript, false);
172 bool WriteWatchOnly(const CScript &dest)
175 return Write(make_pair(string("watchs"), dest), '1');
178 bool EraseWatchOnly(const CScript &dest)
181 return Erase(make_pair(string("watchs"), dest));
184 bool WriteBestBlock(const CBlockLocator& locator)
187 return Write(string("bestblock"), locator);
190 bool ReadBestBlock(CBlockLocator& locator)
192 return Read(string("bestblock"), locator);
195 bool WriteOrderPosNext(int64_t nOrderPosNext)
198 return Write(string("orderposnext"), nOrderPosNext);
201 bool WriteDefaultKey(const CPubKey& key)
204 return Write(string("defaultkey"), key);
207 bool ReadPool(int64_t nPool, CKeyPool& keypool)
209 return Read(make_pair(string("pool"), nPool), keypool);
212 bool WritePool(int64_t nPool, const CKeyPool& keypool)
215 return Write(make_pair(string("pool"), nPool), keypool);
218 bool ErasePool(int64_t nPool)
221 return Erase(make_pair(string("pool"), nPool));
224 bool WriteMinVersion(int nVersion)
226 return Write(string("minversion"), nVersion);
229 bool ReadAccount(const string& strAccount, CAccount& account);
230 bool WriteAccount(const string& strAccount, const CAccount& account);
232 bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
234 bool WriteAccountingEntry(const CAccountingEntry& acentry);
235 int64_t GetAccountCreditDebit(const string& strAccount);
236 void ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& acentries);
238 DBErrors ReorderTransactions(CWallet*);
239 DBErrors LoadWallet(CWallet* pwallet);
240 DBErrors FindWalletTx(CWallet* pwallet, vector<uint256>& vTxHash);
241 DBErrors ZapWalletTx(CWallet* pwallet);
243 static bool Recover(CDBEnv& dbenv, string filename, bool fOnlyKeys);
244 static bool Recover(CDBEnv& dbenv, string filename);
247 #endif // BITCOIN_WALLETDB_H