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 nCreateTime; // 0 means unknown
37 CKeyMetadata(int64 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& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta)
89 if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta))
92 return Write(std::make_pair(std::string("key"), vchPubKey.Raw()), vchPrivKey, false);
95 bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta)
98 bool fEraseUnencryptedKey = true;
100 if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta))
103 if (!Write(std::make_pair(std::string("ckey"), vchPubKey.Raw()), vchCryptedSecret, false))
105 if (fEraseUnencryptedKey)
107 Erase(std::make_pair(std::string("key"), vchPubKey.Raw()));
108 Erase(std::make_pair(std::string("wkey"), vchPubKey.Raw()));
113 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
116 return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
119 bool WriteCScript(const uint160& hash, const CScript& redeemScript)
122 return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
125 bool WriteBestBlock(const CBlockLocator& locator)
128 return Write(std::string("bestblock"), locator);
131 bool ReadBestBlock(CBlockLocator& locator)
133 return Read(std::string("bestblock"), locator);
136 bool WriteOrderPosNext(int64 nOrderPosNext)
139 return Write(std::string("orderposnext"), nOrderPosNext);
142 bool WriteDefaultKey(const CPubKey& vchPubKey)
145 return Write(std::string("defaultkey"), vchPubKey.Raw());
148 bool ReadPool(int64 nPool, CKeyPool& keypool)
150 return Read(std::make_pair(std::string("pool"), nPool), keypool);
153 bool WritePool(int64 nPool, const CKeyPool& keypool)
156 return Write(std::make_pair(std::string("pool"), nPool), keypool);
159 bool ErasePool(int64 nPool)
162 return Erase(std::make_pair(std::string("pool"), nPool));
165 // Settings are no longer stored in wallet.dat; these are
166 // used only for backwards compatibility:
168 bool ReadSetting(const std::string& strKey, T& value)
170 return Read(std::make_pair(std::string("setting"), strKey), value);
173 bool WriteSetting(const std::string& strKey, const T& value)
176 return Write(std::make_pair(std::string("setting"), strKey), value);
178 bool EraseSetting(const std::string& strKey)
181 return Erase(std::make_pair(std::string("setting"), strKey));
184 bool WriteMinVersion(int nVersion)
186 return Write(std::string("minversion"), nVersion);
189 bool ReadAccount(const std::string& strAccount, CAccount& account);
190 bool WriteAccount(const std::string& strAccount, const CAccount& account);
192 bool WriteAccountingEntry(const uint64 nAccEntryNum, const CAccountingEntry& acentry);
194 bool WriteAccountingEntry(const CAccountingEntry& acentry);
195 int64 GetAccountCreditDebit(const std::string& strAccount);
196 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
198 DBErrors ReorderTransactions(CWallet*);
199 DBErrors LoadWallet(CWallet* pwallet);
200 static bool Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys);
201 static bool Recover(CDBEnv& dbenv, std::string filename);
204 #endif // BITCOIN_WALLETDB_H