Update License in File Headers
[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
10 class CKeyPool;
11 class CAccount;
12 class CAccountingEntry;
13
14 /** Error statuses for the wallet database */
15 enum DBErrors
16 {
17     DB_LOAD_OK,
18     DB_CORRUPT,
19     DB_TOO_NEW,
20     DB_LOAD_FAIL,
21     DB_NEED_REWRITE
22 };
23
24 /** Access to the wallet database (wallet.dat) */
25 class CWalletDB : public CDB
26 {
27 public:
28     CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
29     {
30     }
31 private:
32     CWalletDB(const CWalletDB&);
33     void operator=(const CWalletDB&);
34 public:
35     bool ReadName(const std::string& strAddress, std::string& strName)
36     {
37         strName = "";
38         return Read(std::make_pair(std::string("name"), strAddress), strName);
39     }
40
41     bool WriteName(const std::string& strAddress, const std::string& strName);
42
43     bool EraseName(const std::string& strAddress);
44
45     bool ReadTx(uint256 hash, CWalletTx& wtx)
46     {
47         return Read(std::make_pair(std::string("tx"), hash), wtx);
48     }
49
50     bool WriteTx(uint256 hash, const CWalletTx& wtx)
51     {
52         nWalletDBUpdated++;
53         return Write(std::make_pair(std::string("tx"), hash), wtx);
54     }
55
56     bool EraseTx(uint256 hash)
57     {
58         nWalletDBUpdated++;
59         return Erase(std::make_pair(std::string("tx"), hash));
60     }
61
62     bool ReadKey(const std::vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)
63     {
64         vchPrivKey.clear();
65         return Read(std::make_pair(std::string("key"), vchPubKey), vchPrivKey);
66     }
67
68     bool WriteKey(const std::vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)
69     {
70         nWalletDBUpdated++;
71         return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false);
72     }
73
74     bool WriteCryptedKey(const std::vector<unsigned char>& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, bool fEraseUnencryptedKey = true)
75     {
76         nWalletDBUpdated++;
77         if (!Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, false))
78             return false;
79         if (fEraseUnencryptedKey)
80         {
81             Erase(std::make_pair(std::string("key"), vchPubKey));
82             Erase(std::make_pair(std::string("wkey"), vchPubKey));
83         }
84         return true;
85     }
86
87     bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
88     {
89         nWalletDBUpdated++;
90         return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
91     }
92
93     // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
94     bool ReadCScript(const uint160 &hash, CScript& redeemScript)
95     {
96         redeemScript.clear();
97         return Read(std::make_pair(std::string("cscript"), hash), redeemScript);
98     }
99
100     bool WriteCScript(const uint160& hash, const CScript& redeemScript)
101     {
102         nWalletDBUpdated++;
103         return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
104     }
105
106     bool WriteBestBlock(const CBlockLocator& locator)
107     {
108         nWalletDBUpdated++;
109         return Write(std::string("bestblock"), locator);
110     }
111
112     bool ReadBestBlock(CBlockLocator& locator)
113     {
114         return Read(std::string("bestblock"), locator);
115     }
116
117     bool ReadDefaultKey(std::vector<unsigned char>& vchPubKey)
118     {
119         vchPubKey.clear();
120         return Read(std::string("defaultkey"), vchPubKey);
121     }
122
123     bool WriteDefaultKey(const std::vector<unsigned char>& vchPubKey)
124     {
125         nWalletDBUpdated++;
126         return Write(std::string("defaultkey"), vchPubKey);
127     }
128
129     bool ReadPool(int64 nPool, CKeyPool& keypool)
130     {
131         return Read(std::make_pair(std::string("pool"), nPool), keypool);
132     }
133
134     bool WritePool(int64 nPool, const CKeyPool& keypool)
135     {
136         nWalletDBUpdated++;
137         return Write(std::make_pair(std::string("pool"), nPool), keypool);
138     }
139
140     bool ErasePool(int64 nPool)
141     {
142         nWalletDBUpdated++;
143         return Erase(std::make_pair(std::string("pool"), nPool));
144     }
145
146     // Settings are no longer stored in wallet.dat; these are
147     // used only for backwards compatibility:
148     template<typename T>
149     bool ReadSetting(const std::string& strKey, T& value)
150     {
151         return Read(std::make_pair(std::string("setting"), strKey), value);
152     }
153     template<typename T>
154     bool WriteSetting(const std::string& strKey, const T& value)
155     {
156         nWalletDBUpdated++;
157         return Write(std::make_pair(std::string("setting"), strKey), value);
158     }
159     bool EraseSetting(const std::string& strKey)
160     {
161         nWalletDBUpdated++;
162         return Erase(std::make_pair(std::string("setting"), strKey));
163     }
164
165     bool WriteMinVersion(int nVersion)
166     {
167         return Write(std::string("minversion"), nVersion);
168     }
169
170     bool ReadAccount(const std::string& strAccount, CAccount& account);
171     bool WriteAccount(const std::string& strAccount, const CAccount& account);
172     bool WriteAccountingEntry(const CAccountingEntry& acentry);
173     int64 GetAccountCreditDebit(const std::string& strAccount);
174     void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
175
176     int LoadWallet(CWallet* pwallet);
177 };
178
179 #endif // BITCOIN_WALLETDB_H