-
Notifications
You must be signed in to change notification settings - Fork 17
/
WalletDB.h
223 lines (187 loc) · 6.36 KB
/
WalletDB.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2011 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_WALLETDB_H
#define BITCOIN_WALLETDB_H
#include <coinWallet/Export.h>
#include <coinWallet/Wallet.h>
#include <coinWallet/BerkeleyDB.h>
#include <coinWallet/WalletTx.h>
extern unsigned int nWalletDBUpdated;
class CAccount;
class CAccountingEntry;
class Wallet;
class CWalletTx;
// helpers to enable reading and writing of uint256
inline CDataStream& operator<<(CDataStream& os, const std::pair<std::string, uint256>& obj) {
std::ostringstream oss;
oss << const_varstr(obj.first) << obj.second;
std::string s = oss.str();
return os.write(&s[0], s.size());
}
inline CDataStream& operator>>(CDataStream& is, std::pair<std::string, uint256>& obj) {
std::istringstream iss(is.str());
const std::streampos before = iss.tellg();
iss >> varstr(obj.first) >> obj.second;
const std::streampos after = iss.tellg();
is.ignore(after - before);
return is;
}
inline CDataStream& operator>>(CDataStream& is, uint256& n) {
std::istringstream iss(is.str());
const std::streampos before = iss.tellg();
iss >> n;
const std::streampos after = iss.tellg();
is.ignore(after - before);
return is;
}
class COINWALLET_EXPORT CKeyPool
{
public:
int64_t nTime;
PubKey vchPubKey;
CKeyPool()
{
nTime = UnixTime::s();
}
CKeyPool(const PubKey& vchPubKeyIn)
{
nTime = UnixTime::s();
vchPubKey = vchPubKeyIn;
}
IMPLEMENT_SERIALIZE
(
if (!(nType & SER_GETHASH))
READWRITE(nVersion);
READWRITE(nTime);
READWRITE(vchPubKey);
)
};
class CMasterKey;
// Changed the WalletDB to be thread safe - keep a static mutex that is locked on write
class COINWALLET_EXPORT CWalletDB : public CDB
{
public:
CWalletDB(const std::string dataDir, std::string strFilename, const char* pszMode="r+") : CDB(dataDir, strFilename.c_str(), pszMode)
{
}
private:
CWalletDB(const CWalletDB&);
void operator=(const CWalletDB&);
private:
static boost::mutex _write;
public:
bool ReadName(const std::string& strAddress, std::string& strName)
{
strName = "";
return Read(std::make_pair(std::string("name"), strAddress), strName);
}
bool WriteName(const std::string& strAddress, const std::string& strName);
bool EraseName(const std::string& strAddress);
bool ReadTx(uint256 hash, CWalletTx& wtx)
{
return Read(std::make_pair(std::string("tx"), hash), wtx);
}
bool WriteTx(uint256 hash, const CWalletTx& wtx) {
boost::mutex::scoped_lock lock(_write);
nWalletDBUpdated++;
return Write(std::make_pair(std::string("tx"), hash), wtx);
}
bool EraseTx(uint256 hash)
{
boost::mutex::scoped_lock lock(_write);
nWalletDBUpdated++;
return Erase(std::make_pair(std::string("tx"), hash));
}
bool ReadKey(const PubKey& vchPubKey, CPrivKey& vchPrivKey)
{
vchPrivKey.clear();
return Read(std::make_pair(std::string("key"), vchPubKey), vchPrivKey);
}
bool WriteKey(const PubKey& vchPubKey, const CPrivKey& vchPrivKey)
{
boost::mutex::scoped_lock lock(_write);
nWalletDBUpdated++;
return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false);
}
bool WriteCryptedKey(const PubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, bool fEraseUnencryptedKey = true)
{
boost::mutex::scoped_lock lock(_write);
nWalletDBUpdated++;
if (!Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, false))
return false;
if (fEraseUnencryptedKey)
{
Erase(std::make_pair(std::string("key"), vchPubKey));
Erase(std::make_pair(std::string("wkey"), vchPubKey));
}
return true;
}
bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
{
boost::mutex::scoped_lock lock(_write);
nWalletDBUpdated++;
return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
}
/*
bool WriteBestBlock(const CBlockLocator& locator)
{
boost::mutex::scoped_lock lock(_write);
nWalletDBUpdated++;
return Write(std::string("bestblock"), locator);
}
bool ReadBestBlock(CBlockLocator& locator)
{
return Read(std::string("bestblock"), locator);
}
*/
bool ReadDefaultKey(PubKey& vchPubKey)
{
vchPubKey.clear();
return Read(std::string("defaultkey"), vchPubKey);
}
bool WriteDefaultKey(const PubKey& vchPubKey)
{
boost::mutex::scoped_lock lock(_write);
nWalletDBUpdated++;
return Write(std::string("defaultkey"), vchPubKey);
}
bool ReadPool(int64_t nPool, CKeyPool& keypool)
{
return Read(std::make_pair(std::string("pool"), nPool), keypool);
}
bool WritePool(int64_t nPool, const CKeyPool& keypool)
{
boost::mutex::scoped_lock lock(_write);
nWalletDBUpdated++;
return Write(std::make_pair(std::string("pool"), nPool), keypool);
}
bool ErasePool(int64_t nPool)
{
boost::mutex::scoped_lock lock(_write);
nWalletDBUpdated++;
return Erase(std::make_pair(std::string("pool"), nPool));
}
template<typename T>
bool ReadSetting(const std::string& strKey, T& value)
{
return Read(std::make_pair(std::string("setting"), strKey), value);
}
template<typename T>
bool WriteSetting(const std::string& strKey, const T& value)
{
boost::mutex::scoped_lock lock(_write);
nWalletDBUpdated++;
return Write(std::make_pair(std::string("setting"), strKey), value);
}
bool ReadAccount(const std::string& strAccount, CAccount& account);
bool WriteAccount(const std::string& strAccount, const CAccount& account);
bool WriteAccountingEntry(const CAccountingEntry& acentry);
int64_t GetAccountCreditDebit(const std::string& strAccount);
void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
int LoadWallet(Wallet* pwallet);
};
//void ThreadFlushWalletDB(void* parg);
bool BackupWallet(const Wallet& wallet, const std::string& strDest);
#endif