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
|
#include "Wallet.h"
#include <QFile>
#include <QDir>
#include <QDebug>
#include <QUrl>
namespace {
QString TEST_SEED = "bound class paint gasp task soul forgot past pleasure physical circle "
" appear shore bathroom glove women crap busy beauty bliss idea give needle burden";
namespace {
bool createFileWrapper(const QString &filename)
{
QFile file(filename);
// qDebug("%s: about to create file: %s", __FUNCTION__, qPrintable(filename));
bool result = file.open(QIODevice::WriteOnly);
if (!result ){
qWarning("%s: error creating file '%s' : '%s'",
__FUNCTION__,
qPrintable(filename),
qPrintable(file.errorString()));
}
return result;
}
}
}
struct WalletImpl
{
QString basename() const;
void setBasename(const QString &name);
QString keysName() const;
QString addressName() const;
// Bitmonero::Wallet * m_walletImpl;
QString m_basename;
QString m_seed;
QString m_password;
QString m_language;
static QString keysName(const QString &basename);
static QString addressName(const QString &basename);
};
QString WalletImpl::basename() const
{
return m_basename;
}
void WalletImpl::setBasename(const QString &name)
{
m_basename = name;
}
QString WalletImpl::keysName() const
{
return keysName(m_basename);
}
QString WalletImpl::addressName() const
{
return addressName(m_basename);
}
QString WalletImpl::keysName(const QString &basename)
{
return basename + ".keys";
}
QString WalletImpl::addressName(const QString &basename)
{
return basename + ".address.txt";
}
Wallet::Wallet(QObject *parent)
: QObject(parent)
{
}
QString Wallet::getSeed() const
{
return m_pimpl->m_seed;
}
QString Wallet::getSeedLanguage() const
{
return "English";
}
//void Wallet::setSeedLaguage(const QString &lang)
//{
// // TODO: call libwallet's appropriate method
//}
bool Wallet::setPassword(const QString &password)
{
// set/change password implies:
// recovery wallet with existing path, seed and lang
qDebug("%s: recovering wallet with path=%s, seed=%s, lang=%s and new password=%s",
__FUNCTION__,
qPrintable(this->getBasename()),
qPrintable(this->getSeed()),
qPrintable(this->getSeedLanguage()),
qPrintable(password));
return true;
}
QString Wallet::getPassword() const
{
return m_pimpl->m_password;
}
bool Wallet::rename(const QString &name)
{
QString dst = QUrl(name).toLocalFile();
if (dst.isEmpty())
dst = name;
qDebug("%s: renaming '%s' to '%s'",
__FUNCTION__,
qPrintable(m_pimpl->basename()),
qPrintable(dst));
QString walletKeysFile = m_pimpl->keysName();
QString walletAddressFile = m_pimpl->addressName();
QString dstWalletKeysFile = WalletImpl::keysName(dst);
QString dstWalletAddressFile = WalletImpl::addressName(dst);
QFile walletFile(this->getBasename());
if (!walletFile.rename(dst)) {
qWarning("Error renaming file: '%s' to '%s' : (%s)",
qPrintable(m_pimpl->basename()),
qPrintable(dst),
qPrintable(walletFile.errorString()));
return false;
}
QFile::rename(walletKeysFile, dstWalletKeysFile);
QFile::rename(walletAddressFile, dstWalletAddressFile);
bool result = QFile::exists(dst) && QFile::exists(dstWalletKeysFile)
&& QFile::exists(dstWalletAddressFile);
if (result) {
m_pimpl->m_basename = dst;
}
return result;
}
QString Wallet::getBasename() const
{
return m_pimpl->basename();
}
int Wallet::error() const
{
return 0;
}
QString Wallet::errorString() const
{
return m_pimpl->m_seed;
}
Wallet::Wallet(const QString &path, const QString &password, const QString &language)
{
m_pimpl = new WalletImpl;
m_pimpl->m_basename = path;
m_pimpl->m_password = password;
m_pimpl->m_language = language;
m_pimpl->m_seed = TEST_SEED;
// Create dummy files for testing
QFileInfo fi(path);
QDir tempDir;
tempDir.mkpath(fi.absolutePath());
createFileWrapper(m_pimpl->basename());
createFileWrapper(m_pimpl->keysName());
createFileWrapper(m_pimpl->addressName());
}
|