RPC: Add new methods suitable for malleable key pairs management;
[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& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta)
87     {
88         nWalletDBUpdated++;
89         if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta))
90             return false;
91
92         if(!Write(std::make_pair(std::string("key"), vchPubKey.Raw()), vchPrivKey, false))
93             return false;
94
95         return true;
96     }
97
98     bool WriteMalleableKey(const CMalleableKeyView& keyView, const CMalleableKey& malleableKey, 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()), malleableKey.ToString(), false))
105             return false;
106
107         return true;
108     }
109
110
111     bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta)
112     {
113         nWalletDBUpdated++;
114         bool fEraseUnencryptedKey = true;
115
116         if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta))
117             return false;
118
119         if (!Write(std::make_pair(std::string("ckey"), vchPubKey.Raw()), vchCryptedSecret, false))
120             return false;
121         if (fEraseUnencryptedKey)
122         {
123             Erase(std::make_pair(std::string("key"), vchPubKey.Raw()));
124             Erase(std::make_pair(std::string("wkey"), vchPubKey.Raw()));
125         }
126         return true;
127     }
128
129     bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
130     {
131         nWalletDBUpdated++;
132         return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
133     }
134
135     bool EraseMasterKey(unsigned int nID)
136     {
137         nWalletDBUpdated++;
138         return Erase(std::make_pair(std::string("mkey"), nID));
139     }
140
141     bool EraseCryptedKey(const CPubKey& vchPubKey)
142     {
143         return Erase(std::make_pair(std::string("ckey"), vchPubKey.Raw()));
144     }
145
146     bool WriteCScript(const uint160& hash, const CScript& redeemScript)
147     {
148         nWalletDBUpdated++;
149         return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
150     }
151
152     bool WriteWatchOnly(const CScript &dest)
153     {
154         nWalletDBUpdated++;
155         return Write(std::make_pair(std::string("watchs"), dest), '1');
156     }
157
158     bool EraseWatchOnly(const CScript &dest)
159     {
160         nWalletDBUpdated++;
161         return Erase(std::make_pair(std::string("watchs"), dest));
162     }
163
164     bool WriteBestBlock(const CBlockLocator& locator)
165     {
166         nWalletDBUpdated++;
167         return Write(std::string("bestblock"), locator);
168     }
169
170     bool ReadBestBlock(CBlockLocator& locator)
171     {
172         return Read(std::string("bestblock"), locator);
173     }
174
175     bool WriteOrderPosNext(int64_t nOrderPosNext)
176     {
177         nWalletDBUpdated++;
178         return Write(std::string("orderposnext"), nOrderPosNext);
179     }
180
181     bool WriteDefaultKey(const CPubKey& vchPubKey)
182     {
183         nWalletDBUpdated++;
184         return Write(std::string("defaultkey"), vchPubKey.Raw());
185     }
186
187     bool ReadPool(int64_t nPool, CKeyPool& keypool)
188     {
189         return Read(std::make_pair(std::string("pool"), nPool), keypool);
190     }
191
192     bool WritePool(int64_t nPool, const CKeyPool& keypool)
193     {
194         nWalletDBUpdated++;
195         return Write(std::make_pair(std::string("pool"), nPool), keypool);
196     }
197
198     bool ErasePool(int64_t nPool)
199     {
200         nWalletDBUpdated++;
201         return Erase(std::make_pair(std::string("pool"), nPool));
202     }
203
204     bool WriteMinVersion(int nVersion)
205     {
206         return Write(std::string("minversion"), nVersion);
207     }
208
209     bool ReadAccount(const std::string& strAccount, CAccount& account);
210     bool WriteAccount(const std::string& strAccount, const CAccount& account);
211 private:
212     bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
213 public:
214     bool WriteAccountingEntry(const CAccountingEntry& acentry);
215     int64_t GetAccountCreditDebit(const std::string& strAccount);
216     void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
217
218     DBErrors ReorderTransactions(CWallet*);
219     DBErrors LoadWallet(CWallet* pwallet);
220     DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash);
221     DBErrors ZapWalletTx(CWallet* pwallet);
222
223     static bool Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys);
224     static bool Recover(CDBEnv& dbenv, std::string filename);
225 };
226
227 #endif // BITCOIN_WALLETDB_H