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