update to 0.4 preview
[novacoin.git] / src / walletdb.h
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
7
8 #include "db.h"
9 #include "base58.h"
10
11 class CKeyPool;
12 class CAccount;
13 class CAccountingEntry;
14
15 /** Error statuses for the wallet database */
16 enum DBErrors
17 {
18     DB_LOAD_OK,
19     DB_CORRUPT,
20     DB_NONCRITICAL_ERROR,
21     DB_TOO_NEW,
22     DB_LOAD_FAIL,
23     DB_NEED_REWRITE
24 };
25
26 /** Access to the wallet database (wallet.dat) */
27 class CWalletDB : public CDB
28 {
29 public:
30     CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
31     {
32     }
33 private:
34     CWalletDB(const CWalletDB&);
35     void operator=(const CWalletDB&);
36 public:
37     bool WriteName(const std::string& strAddress, const std::string& strName);
38
39     bool EraseName(const std::string& strAddress);
40
41     bool WriteTx(uint256 hash, const CWalletTx& wtx)
42     {
43         nWalletDBUpdated++;
44         return Write(std::make_pair(std::string("tx"), hash), wtx);
45     }
46
47     bool EraseTx(uint256 hash)
48     {
49         nWalletDBUpdated++;
50         return Erase(std::make_pair(std::string("tx"), hash));
51     }
52
53     bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey)
54     {
55         nWalletDBUpdated++;
56         return Write(std::make_pair(std::string("key"), vchPubKey.Raw()), vchPrivKey, false);
57     }
58
59     bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, bool fEraseUnencryptedKey = true)
60     {
61         nWalletDBUpdated++;
62         if (!Write(std::make_pair(std::string("ckey"), vchPubKey.Raw()), vchCryptedSecret, false))
63             return false;
64         if (fEraseUnencryptedKey)
65         {
66             Erase(std::make_pair(std::string("key"), vchPubKey.Raw()));
67             Erase(std::make_pair(std::string("wkey"), vchPubKey.Raw()));
68         }
69         return true;
70     }
71
72     bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
73     {
74         nWalletDBUpdated++;
75         return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
76     }
77
78     bool WriteCScript(const uint160& hash, const CScript& redeemScript)
79     {
80         nWalletDBUpdated++;
81         return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
82     }
83
84     bool WriteBestBlock(const CBlockLocator& locator)
85     {
86         nWalletDBUpdated++;
87         return Write(std::string("bestblock"), locator);
88     }
89
90     bool ReadBestBlock(CBlockLocator& locator)
91     {
92         return Read(std::string("bestblock"), locator);
93     }
94
95     bool WriteOrderPosNext(int64 nOrderPosNext)
96     {
97         nWalletDBUpdated++;
98         return Write(std::string("orderposnext"), nOrderPosNext);
99     }
100
101     bool WriteDefaultKey(const CPubKey& vchPubKey)
102     {
103         nWalletDBUpdated++;
104         return Write(std::string("defaultkey"), vchPubKey.Raw());
105     }
106
107     bool ReadPool(int64 nPool, CKeyPool& keypool)
108     {
109         return Read(std::make_pair(std::string("pool"), nPool), keypool);
110     }
111
112     bool WritePool(int64 nPool, const CKeyPool& keypool)
113     {
114         nWalletDBUpdated++;
115         return Write(std::make_pair(std::string("pool"), nPool), keypool);
116     }
117
118     bool ErasePool(int64 nPool)
119     {
120         nWalletDBUpdated++;
121         return Erase(std::make_pair(std::string("pool"), nPool));
122     }
123
124     // Settings are no longer stored in wallet.dat; these are
125     // used only for backwards compatibility:
126     template<typename T>
127     bool ReadSetting(const std::string& strKey, T& value)
128     {
129         return Read(std::make_pair(std::string("setting"), strKey), value);
130     }
131     template<typename T>
132     bool WriteSetting(const std::string& strKey, const T& value)
133     {
134         nWalletDBUpdated++;
135         return Write(std::make_pair(std::string("setting"), strKey), value);
136     }
137     bool EraseSetting(const std::string& strKey)
138     {
139         nWalletDBUpdated++;
140         return Erase(std::make_pair(std::string("setting"), strKey));
141     }
142
143     bool WriteMinVersion(int nVersion)
144     {
145         return Write(std::string("minversion"), nVersion);
146     }
147
148     bool ReadAccount(const std::string& strAccount, CAccount& account);
149     bool WriteAccount(const std::string& strAccount, const CAccount& account);
150 private:
151     bool WriteAccountingEntry(const uint64 nAccEntryNum, const CAccountingEntry& acentry);
152 public:
153     bool WriteAccountingEntry(const CAccountingEntry& acentry);
154     int64 GetAccountCreditDebit(const std::string& strAccount);
155     void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
156
157     DBErrors ReorderTransactions(CWallet*);
158     DBErrors LoadWallet(CWallet* pwallet);
159     static bool Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys);
160     static bool Recover(CDBEnv& dbenv, std::string filename);
161 };
162
163 #endif // BITCOIN_WALLETDB_H