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