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 nCreateTime; // 0 means unknown
38 CKeyMetadata(int64 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)
90 if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta))
93 return Write(std::make_pair(std::string("key"), vchPubKey.Raw()), vchPrivKey, false);
96 bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta)
99 bool fEraseUnencryptedKey = true;
101 if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta))
104 if (!Write(std::make_pair(std::string("ckey"), vchPubKey.Raw()), vchCryptedSecret, false))
106 if (fEraseUnencryptedKey)
108 Erase(std::make_pair(std::string("key"), vchPubKey.Raw()));
109 Erase(std::make_pair(std::string("wkey"), vchPubKey.Raw()));
114 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
117 return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
120 bool WriteCScript(const uint160& hash, const CScript& redeemScript)
123 return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
126 bool WriteWatchOnly(const CTxDestination &dest)
129 return Write(std::make_pair(std::string("watch"), CBitcoinAddress(dest).ToString()), '1');
132 bool WriteBestBlock(const CBlockLocator& locator)
135 return Write(std::string("bestblock"), locator);
138 bool ReadBestBlock(CBlockLocator& locator)
140 return Read(std::string("bestblock"), locator);
143 bool WriteOrderPosNext(int64 nOrderPosNext)
146 return Write(std::string("orderposnext"), nOrderPosNext);
149 bool WriteDefaultKey(const CPubKey& vchPubKey)
152 return Write(std::string("defaultkey"), vchPubKey.Raw());
155 bool ReadPool(int64 nPool, CKeyPool& keypool)
157 return Read(std::make_pair(std::string("pool"), nPool), keypool);
160 bool WritePool(int64 nPool, const CKeyPool& keypool)
163 return Write(std::make_pair(std::string("pool"), nPool), keypool);
166 bool ErasePool(int64 nPool)
169 return Erase(std::make_pair(std::string("pool"), nPool));
172 // Settings are no longer stored in wallet.dat; these are
173 // used only for backwards compatibility:
175 bool ReadSetting(const std::string& strKey, T& value)
177 return Read(std::make_pair(std::string("setting"), strKey), value);
180 bool WriteSetting(const std::string& strKey, const T& value)
183 return Write(std::make_pair(std::string("setting"), strKey), value);
185 bool EraseSetting(const std::string& strKey)
188 return Erase(std::make_pair(std::string("setting"), strKey));
191 bool WriteMinVersion(int nVersion)
193 return Write(std::string("minversion"), nVersion);
196 bool ReadAccount(const std::string& strAccount, CAccount& account);
197 bool WriteAccount(const std::string& strAccount, const CAccount& account);
199 bool WriteAccountingEntry(const uint64 nAccEntryNum, const CAccountingEntry& acentry);
201 bool WriteAccountingEntry(const CAccountingEntry& acentry);
202 int64 GetAccountCreditDebit(const std::string& strAccount);
203 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
205 DBErrors ReorderTransactions(CWallet*);
206 DBErrors LoadWallet(CWallet* pwallet);
207 static bool Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys);
208 static bool Recover(CDBEnv& dbenv, std::string filename);
211 #endif // BITCOIN_WALLETDB_H