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, int64 nCreateTime)
89 CKeyMetadata keyMeta(nCreateTime);
90 if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta, false))
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, int64 nCreateTime)
99 bool fEraseUnencryptedKey = true;
101 CKeyMetadata keyMeta(nCreateTime);
102 if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta, false))
105 if (!Write(std::make_pair(std::string("ckey"), vchPubKey.Raw()), vchCryptedSecret, false))
107 if (fEraseUnencryptedKey)
109 Erase(std::make_pair(std::string("key"), vchPubKey.Raw()));
110 Erase(std::make_pair(std::string("wkey"), vchPubKey.Raw()));
115 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
118 return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
121 bool WriteCScript(const uint160& hash, const CScript& redeemScript)
124 return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
127 bool WriteBestBlock(const CBlockLocator& locator)
130 return Write(std::string("bestblock"), locator);
133 bool ReadBestBlock(CBlockLocator& locator)
135 return Read(std::string("bestblock"), locator);
138 bool WriteOrderPosNext(int64 nOrderPosNext)
141 return Write(std::string("orderposnext"), nOrderPosNext);
144 bool WriteDefaultKey(const CPubKey& vchPubKey)
147 return Write(std::string("defaultkey"), vchPubKey.Raw());
150 bool ReadPool(int64 nPool, CKeyPool& keypool)
152 return Read(std::make_pair(std::string("pool"), nPool), keypool);
155 bool WritePool(int64 nPool, const CKeyPool& keypool)
158 return Write(std::make_pair(std::string("pool"), nPool), keypool);
161 bool ErasePool(int64 nPool)
164 return Erase(std::make_pair(std::string("pool"), nPool));
167 // Settings are no longer stored in wallet.dat; these are
168 // used only for backwards compatibility:
170 bool ReadSetting(const std::string& strKey, T& value)
172 return Read(std::make_pair(std::string("setting"), strKey), value);
175 bool WriteSetting(const std::string& strKey, const T& value)
178 return Write(std::make_pair(std::string("setting"), strKey), value);
180 bool EraseSetting(const std::string& strKey)
183 return Erase(std::make_pair(std::string("setting"), strKey));
186 bool WriteMinVersion(int nVersion)
188 return Write(std::string("minversion"), nVersion);
191 bool ReadAccount(const std::string& strAccount, CAccount& account);
192 bool WriteAccount(const std::string& strAccount, const CAccount& account);
194 bool WriteAccountingEntry(const uint64 nAccEntryNum, const CAccountingEntry& acentry);
196 bool WriteAccountingEntry(const CAccountingEntry& acentry);
197 int64 GetAccountCreditDebit(const std::string& strAccount);
198 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
200 DBErrors ReorderTransactions(CWallet*);
201 DBErrors LoadWallet(CWallet* pwallet);
202 static bool Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys);
203 static bool Recover(CDBEnv& dbenv, std::string filename);
206 #endif // BITCOIN_WALLETDB_H