Add wallet dump functionality and key creation timestamps
[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 class CKeyMetadata
27 {
28 public:
29     static const int CURRENT_VERSION=1;
30     int nVersion;
31     int64 nCreateTime; // 0 means unknown
32
33     CKeyMetadata()
34     {
35         SetNull();
36     }
37     CKeyMetadata(int64 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& vchPubKey, const CPrivKey& vchPrivKey, int64 nCreateTime)
86     {
87         nWalletDBUpdated++;
88
89         CKeyMetadata keyMeta(nCreateTime);
90         if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta, false))
91             return false;
92
93         return Write(std::make_pair(std::string("key"), vchPubKey.Raw()), vchPrivKey, false);
94     }
95
96     bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, int64 nCreateTime)
97     {
98         nWalletDBUpdated++;
99         bool fEraseUnencryptedKey = true;
100
101         CKeyMetadata keyMeta(nCreateTime);
102         if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta, false))
103             return false;
104
105         if (!Write(std::make_pair(std::string("ckey"), vchPubKey.Raw()), vchCryptedSecret, false))
106             return false;
107         if (fEraseUnencryptedKey)
108         {
109             Erase(std::make_pair(std::string("key"), vchPubKey.Raw()));
110             Erase(std::make_pair(std::string("wkey"), vchPubKey.Raw()));
111         }
112         return true;
113     }
114
115     bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
116     {
117         nWalletDBUpdated++;
118         return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
119     }
120
121     bool WriteCScript(const uint160& hash, const CScript& redeemScript)
122     {
123         nWalletDBUpdated++;
124         return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
125     }
126
127     bool WriteBestBlock(const CBlockLocator& locator)
128     {
129         nWalletDBUpdated++;
130         return Write(std::string("bestblock"), locator);
131     }
132
133     bool ReadBestBlock(CBlockLocator& locator)
134     {
135         return Read(std::string("bestblock"), locator);
136     }
137
138     bool WriteOrderPosNext(int64 nOrderPosNext)
139     {
140         nWalletDBUpdated++;
141         return Write(std::string("orderposnext"), nOrderPosNext);
142     }
143
144     bool WriteDefaultKey(const CPubKey& vchPubKey)
145     {
146         nWalletDBUpdated++;
147         return Write(std::string("defaultkey"), vchPubKey.Raw());
148     }
149
150     bool ReadPool(int64 nPool, CKeyPool& keypool)
151     {
152         return Read(std::make_pair(std::string("pool"), nPool), keypool);
153     }
154
155     bool WritePool(int64 nPool, const CKeyPool& keypool)
156     {
157         nWalletDBUpdated++;
158         return Write(std::make_pair(std::string("pool"), nPool), keypool);
159     }
160
161     bool ErasePool(int64 nPool)
162     {
163         nWalletDBUpdated++;
164         return Erase(std::make_pair(std::string("pool"), nPool));
165     }
166
167     // Settings are no longer stored in wallet.dat; these are
168     // used only for backwards compatibility:
169     template<typename T>
170     bool ReadSetting(const std::string& strKey, T& value)
171     {
172         return Read(std::make_pair(std::string("setting"), strKey), value);
173     }
174     template<typename T>
175     bool WriteSetting(const std::string& strKey, const T& value)
176     {
177         nWalletDBUpdated++;
178         return Write(std::make_pair(std::string("setting"), strKey), value);
179     }
180     bool EraseSetting(const std::string& strKey)
181     {
182         nWalletDBUpdated++;
183         return Erase(std::make_pair(std::string("setting"), strKey));
184     }
185
186     bool WriteMinVersion(int nVersion)
187     {
188         return Write(std::string("minversion"), nVersion);
189     }
190
191     bool ReadAccount(const std::string& strAccount, CAccount& account);
192     bool WriteAccount(const std::string& strAccount, const CAccount& account);
193 private:
194     bool WriteAccountingEntry(const uint64 nAccEntryNum, const CAccountingEntry& acentry);
195 public:
196     bool WriteAccountingEntry(const CAccountingEntry& acentry);
197     int64 GetAccountCreditDebit(const std::string& strAccount);
198     void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
199
200     DBErrors ReorderTransactions(CWallet*);
201     DBErrors LoadWallet(CWallet* pwallet);
202     static bool Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys);
203     static bool Recover(CDBEnv& dbenv, std::string filename);
204 };
205
206 #endif // BITCOIN_WALLETDB_H