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