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 */
29 static const int CURRENT_VERSION=1;
31 int64_t nCreateTime; // 0 means unknown
37 CKeyMetadata(int64_t nCreateTime_)
39 nVersion = CKeyMetadata::CURRENT_VERSION;
40 nCreateTime = nCreateTime_;
45 READWRITE(this->nVersion);
46 nVersion = this->nVersion;
47 READWRITE(nCreateTime);
52 nVersion = CKeyMetadata::CURRENT_VERSION;
58 /** Access to the wallet database (wallet.dat) */
59 class CWalletDB : public CDB
62 CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
66 CWalletDB(const CWalletDB&);
67 void operator=(const CWalletDB&);
69 bool WriteName(const std::string& strAddress, const std::string& strName);
71 bool EraseName(const std::string& strAddress);
73 bool WriteTx(uint256 hash, const CWalletTx& wtx)
76 return Write(std::make_pair(std::string("tx"), hash), wtx);
79 bool EraseTx(uint256 hash)
82 return Erase(std::make_pair(std::string("tx"), hash));
85 bool WriteKey(const CPubKey& key, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta)
88 if(!Write(std::make_pair(std::string("keymeta"), key), keyMeta))
91 if(!Write(std::make_pair(std::string("key"), key), vchPrivKey, false))
97 bool WriteMalleableKey(const CMalleableKeyView& keyView, const CSecret& vchSecretH, const CKeyMetadata &keyMeta)
100 if(!Write(std::make_pair(std::string("malmeta"), keyView.ToString()), keyMeta))
103 if(!Write(std::make_pair(std::string("malpair"), keyView.ToString()), vchSecretH, false))
109 bool WriteCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char>& vchCryptedSecretH, const CKeyMetadata &keyMeta)
112 if(!Write(std::make_pair(std::string("malmeta"), keyView.ToString()), keyMeta))
115 if(!Write(std::make_pair(std::string("malcpair"), keyView.ToString()), vchCryptedSecretH, false))
118 Erase(std::make_pair(std::string("malpair"), keyView.ToString()));
124 bool WriteCryptedKey(const CPubKey& key, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta)
127 bool fEraseUnencryptedKey = true;
129 if(!Write(std::make_pair(std::string("keymeta"), key), keyMeta))
132 if (!Write(std::make_pair(std::string("ckey"), key), vchCryptedSecret, false))
134 if (fEraseUnencryptedKey)
136 Erase(std::make_pair(std::string("key"), key));
137 Erase(std::make_pair(std::string("wkey"), key));
142 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
145 return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
148 bool EraseMasterKey(unsigned int nID)
151 return Erase(std::make_pair(std::string("mkey"), nID));
154 bool EraseCryptedKey(const CPubKey& key)
156 return Erase(std::make_pair(std::string("ckey"), key));
159 bool EraseCryptedMalleableKey(const CMalleableKeyView& keyView)
161 return Erase(std::make_pair(std::string("malcpair"), keyView.ToString()));
164 bool WriteCScript(const uint160& hash, const CScript& redeemScript)
167 return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
170 bool WriteWatchOnly(const CScript &dest)
173 return Write(std::make_pair(std::string("watchs"), dest), '1');
176 bool EraseWatchOnly(const CScript &dest)
179 return Erase(std::make_pair(std::string("watchs"), dest));
182 bool WriteBestBlock(const CBlockLocator& locator)
185 return Write(std::string("bestblock"), locator);
188 bool ReadBestBlock(CBlockLocator& locator)
190 return Read(std::string("bestblock"), locator);
193 bool WriteOrderPosNext(int64_t nOrderPosNext)
196 return Write(std::string("orderposnext"), nOrderPosNext);
199 bool WriteDefaultKey(const CPubKey& key)
202 return Write(std::string("defaultkey"), key);
205 bool ReadPool(int64_t nPool, CKeyPool& keypool)
207 return Read(std::make_pair(std::string("pool"), nPool), keypool);
210 bool WritePool(int64_t nPool, const CKeyPool& keypool)
213 return Write(std::make_pair(std::string("pool"), nPool), keypool);
216 bool ErasePool(int64_t nPool)
219 return Erase(std::make_pair(std::string("pool"), nPool));
222 bool WriteMinVersion(int nVersion)
224 return Write(std::string("minversion"), nVersion);
227 bool ReadAccount(const std::string& strAccount, CAccount& account);
228 bool WriteAccount(const std::string& strAccount, const CAccount& account);
230 bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
232 bool WriteAccountingEntry(const CAccountingEntry& acentry);
233 int64_t GetAccountCreditDebit(const std::string& strAccount);
234 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
236 DBErrors ReorderTransactions(CWallet*);
237 DBErrors LoadWallet(CWallet* pwallet);
238 DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash);
239 DBErrors ZapWalletTx(CWallet* pwallet);
241 static bool Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys);
242 static bool Recover(CDBEnv& dbenv, std::string filename);
245 #endif // BITCOIN_WALLETDB_H