Wallet.h cleanup
[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 "keystore.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 class CKeyMetadata
27 {
28 public:
29     static const int CURRENT_VERSION=1;
30     int nVersion;
31     int64_t nCreateTime; // 0 means unknown
32
33     CKeyMetadata()
34     {
35         SetNull();
36     }
37     CKeyMetadata(int64_t nCreateTime_)
38     {
39         nVersion = CKeyMetadata::CURRENT_VERSION;
40         nCreateTime = nCreateTime_;
41     }
42
43     IMPLEMENT_SERIALIZE
44     (
45         READWRITE(this->nVersion);
46         nVersion = this->nVersion;
47         READWRITE(nCreateTime);
48     )
49
50     void SetNull()
51     {
52         nVersion = CKeyMetadata::CURRENT_VERSION;
53         nCreateTime = 0;
54     }
55 };
56
57
58 /** Access to the wallet database (wallet.dat) */
59 class CWalletDB : public CDB
60 {
61 public:
62     CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
63     {
64     }
65 private:
66     CWalletDB(const CWalletDB&);
67     void operator=(const CWalletDB&);
68 public:
69     bool WriteName(const std::string& strAddress, const std::string& strName);
70
71     bool EraseName(const std::string& strAddress);
72
73     bool WriteTx(uint256 hash, const CWalletTx& wtx)
74     {
75         nWalletDBUpdated++;
76         return Write(std::make_pair(std::string("tx"), hash), wtx);
77     }
78
79     bool EraseTx(uint256 hash)
80     {
81         nWalletDBUpdated++;
82         return Erase(std::make_pair(std::string("tx"), hash));
83     }
84
85     bool WriteKey(const CPubKey& key, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta)
86     {
87         nWalletDBUpdated++;
88         if(!Write(std::make_pair(std::string("keymeta"), key), keyMeta))
89             return false;
90
91         if(!Write(std::make_pair(std::string("key"), key), vchPrivKey, false))
92             return false;
93
94         return true;
95     }
96
97     bool WriteMalleableKey(const CMalleableKeyView& keyView, const CSecret& vchSecretH, const CKeyMetadata &keyMeta)
98     {
99         nWalletDBUpdated++;
100         if(!Write(std::make_pair(std::string("malmeta"), keyView.ToString()), keyMeta))
101             return false;
102
103         if(!Write(std::make_pair(std::string("malpair"), keyView.ToString()), vchSecretH, false))
104             return false;
105
106         return true;
107     }
108
109     bool WriteCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char>& vchCryptedSecretH, const CKeyMetadata &keyMeta)
110     {
111         nWalletDBUpdated++;
112         if(!Write(std::make_pair(std::string("malmeta"), keyView.ToString()), keyMeta))
113             return false;
114
115         if(!Write(std::make_pair(std::string("malcpair"), keyView.ToString()), vchCryptedSecretH, false))
116             return false;
117
118         Erase(std::make_pair(std::string("malpair"), keyView.ToString()));
119
120         return true;
121     }
122
123
124     bool WriteCryptedKey(const CPubKey& key, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta)
125     {
126         nWalletDBUpdated++;
127         bool fEraseUnencryptedKey = true;
128
129         if(!Write(std::make_pair(std::string("keymeta"), key), keyMeta))
130             return false;
131
132         if (!Write(std::make_pair(std::string("ckey"), key), vchCryptedSecret, false))
133             return false;
134         if (fEraseUnencryptedKey)
135         {
136             Erase(std::make_pair(std::string("key"), key));
137             Erase(std::make_pair(std::string("wkey"), key));
138         }
139         return true;
140     }
141
142     bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
143     {
144         nWalletDBUpdated++;
145         return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
146     }
147
148     bool EraseMasterKey(unsigned int nID)
149     {
150         nWalletDBUpdated++;
151         return Erase(std::make_pair(std::string("mkey"), nID));
152     }
153
154     bool EraseCryptedKey(const CPubKey& key)
155     {
156         return Erase(std::make_pair(std::string("ckey"), key));
157     }
158
159     bool EraseCryptedMalleableKey(const CMalleableKeyView& keyView)
160     {
161         return Erase(std::make_pair(std::string("malcpair"), keyView.ToString()));
162     }
163
164     bool WriteCScript(const uint160& hash, const CScript& redeemScript)
165     {
166         nWalletDBUpdated++;
167         return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
168     }
169
170     bool WriteWatchOnly(const CScript &dest)
171     {
172         nWalletDBUpdated++;
173         return Write(std::make_pair(std::string("watchs"), dest), '1');
174     }
175
176     bool EraseWatchOnly(const CScript &dest)
177     {
178         nWalletDBUpdated++;
179         return Erase(std::make_pair(std::string("watchs"), dest));
180     }
181
182     bool WriteBestBlock(const CBlockLocator& locator)
183     {
184         nWalletDBUpdated++;
185         return Write(std::string("bestblock"), locator);
186     }
187
188     bool ReadBestBlock(CBlockLocator& locator)
189     {
190         return Read(std::string("bestblock"), locator);
191     }
192
193     bool WriteOrderPosNext(int64_t nOrderPosNext)
194     {
195         nWalletDBUpdated++;
196         return Write(std::string("orderposnext"), nOrderPosNext);
197     }
198
199     bool WriteDefaultKey(const CPubKey& key)
200     {
201         nWalletDBUpdated++;
202         return Write(std::string("defaultkey"), key);
203     }
204
205     bool ReadPool(int64_t nPool, CKeyPool& keypool)
206     {
207         return Read(std::make_pair(std::string("pool"), nPool), keypool);
208     }
209
210     bool WritePool(int64_t nPool, const CKeyPool& keypool)
211     {
212         nWalletDBUpdated++;
213         return Write(std::make_pair(std::string("pool"), nPool), keypool);
214     }
215
216     bool ErasePool(int64_t nPool)
217     {
218         nWalletDBUpdated++;
219         return Erase(std::make_pair(std::string("pool"), nPool));
220     }
221
222     bool WriteMinVersion(int nVersion)
223     {
224         return Write(std::string("minversion"), nVersion);
225     }
226
227     bool ReadAccount(const std::string& strAccount, CAccount& account);
228     bool WriteAccount(const std::string& strAccount, const CAccount& account);
229 private:
230     bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
231 public:
232     bool WriteAccountingEntry(const CAccountingEntry& acentry);
233     int64_t GetAccountCreditDebit(const std::string& strAccount);
234     void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
235
236     DBErrors ReorderTransactions(CWallet*);
237     DBErrors LoadWallet(CWallet* pwallet);
238     DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash);
239     DBErrors ZapWalletTx(CWallet* pwallet);
240
241     static bool Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys);
242     static bool Recover(CDBEnv& dbenv, std::string filename);
243 };
244
245 #endif // BITCOIN_WALLETDB_H