1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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.
6 #ifndef BITCOIN_WALLETDB_H
7 #define BITCOIN_WALLETDB_H
21 class CAccountingEntry;
31 /** Error statuses for the wallet database */
45 static const int CURRENT_VERSION=1;
47 int64_t nCreateTime; // 0 means unknown
53 CKeyMetadata(int64_t nCreateTime_)
55 nVersion = CKeyMetadata::CURRENT_VERSION;
56 nCreateTime = nCreateTime_;
59 ADD_SERIALIZE_METHODS;
61 template <typename Stream, typename Operation>
62 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
63 READWRITE(this->nVersion);
64 nVersion = this->nVersion;
65 READWRITE(nCreateTime);
70 nVersion = CKeyMetadata::CURRENT_VERSION;
75 /** Access to the wallet database (wallet.dat) */
76 class CWalletDB : public CDB
79 CWalletDB(const std::string& strFilename, const char* pszMode = "r+") : CDB(strFilename, pszMode)
83 bool WriteName(const std::string& strAddress, const std::string& strName);
84 bool EraseName(const std::string& strAddress);
86 bool WritePurpose(const std::string& strAddress, const std::string& purpose);
87 bool ErasePurpose(const std::string& strAddress);
89 bool WriteTx(uint256 hash, const CWalletTx& wtx);
90 bool EraseTx(uint256 hash);
92 bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
93 bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
94 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
96 bool WriteCScript(const uint160& hash, const CScript& redeemScript);
98 bool WriteWatchOnly(const CScript &script);
99 bool EraseWatchOnly(const CScript &script);
101 bool WriteBestBlock(const CBlockLocator& locator);
102 bool ReadBestBlock(CBlockLocator& locator);
104 bool WriteOrderPosNext(int64_t nOrderPosNext);
106 bool WriteDefaultKey(const CPubKey& vchPubKey);
108 bool ReadPool(int64_t nPool, CKeyPool& keypool);
109 bool WritePool(int64_t nPool, const CKeyPool& keypool);
110 bool ErasePool(int64_t nPool);
112 bool WriteMinVersion(int nVersion);
114 bool ReadAccount(const std::string& strAccount, CAccount& account);
115 bool WriteAccount(const std::string& strAccount, const CAccount& account);
117 /// Write destination data key,value tuple to database
118 bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
119 /// Erase destination data tuple from wallet database
120 bool EraseDestData(const std::string &address, const std::string &key);
122 bool WriteAccountingEntry(const CAccountingEntry& acentry);
123 CAmount GetAccountCreditDebit(const std::string& strAccount);
124 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
126 DBErrors ReorderTransactions(CWallet* pwallet);
127 DBErrors LoadWallet(CWallet* pwallet);
128 DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
129 DBErrors ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx);
130 static bool Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys);
131 static bool Recover(CDBEnv& dbenv, std::string filename);
134 CWalletDB(const CWalletDB&);
135 void operator=(const CWalletDB&);
137 bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
140 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
142 #endif // BITCOIN_WALLETDB_H