forked from ciphrex/mSIGNA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeychainmodel.cpp
319 lines (267 loc) · 7.8 KB
/
keychainmodel.cpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
///////////////////////////////////////////////////////////////////////////////
//
// mSIGNA
//
// keychainmodel.cpp
//
// Copyright (c) 2013 Eric Lombrozo
//
// All Rights Reserved.
#include "keychainmodel.h"
#include <QStandardItemModel>
using namespace CoinDB;
using namespace std;
KeychainModel::KeychainModel()
: vault(NULL)
{
QStringList columns;
columns << tr("Keychain") << tr("") << tr("") << tr("Hash");
setHorizontalHeaderLabels(columns);
connect(this, &KeychainModel::dataChanged, [this]() { emit keychainChanged(); });
}
void KeychainModel::setVault(CoinDB::Vault* vault)
{
this->vault = vault;
}
void KeychainModel::update()
{
removeRows(0, rowCount());
if (!vault) return;
std::vector<KeychainView> keychains = vault->getRootKeychainViews();
for (auto& keychain: keychains)
{
QList<QStandardItem*> row;
row.append(new QStandardItem(QString::fromStdString(keychain.name)));
QStandardItem* encryptedItem = new QStandardItem();
if (keychain.is_encrypted)
{
encryptedItem->setIcon(QIcon(":/icons/encrypted.png"));
encryptedItem->setData(true, Qt::UserRole);
}
else
{
encryptedItem->setData(false, Qt::UserRole);
}
row.append(encryptedItem);
QStandardItem* statusItem = new QStandardItem();
if (!keychain.is_private)
{
statusItem->setIcon(QIcon(":/icons/shared.png"));
statusItem->setData(PUBLIC, Qt::UserRole);
}
else if (keychain.is_locked)
{
statusItem->setIcon(QIcon(":/icons/locked.png"));
statusItem->setData(LOCKED, Qt::UserRole);
}
else
{
statusItem->setIcon(QIcon(":/icons/unlocked.png"));
statusItem->setData(UNLOCKED, Qt::UserRole);
}
row.append(statusItem);
row.append(new QStandardItem(QString::fromStdString(uchar_vector(keychain.hash).getHex())));
appendRow(row);
}
}
void KeychainModel::exportKeychain(const QString& keychainName, const QString& fileName, bool exportPrivate) const
{
if (!vault) {
throw std::runtime_error("No vault is loaded.");
}
vault->exportKeychain(keychainName.toStdString(), fileName.toStdString(), exportPrivate);
}
QString KeychainModel::importKeychain(const QString& fileName, bool& importPrivate)
{
if (!vault) {
throw std::runtime_error("No vault is loaded.");
}
std::shared_ptr<Keychain> keychain = vault->importKeychain(fileName.toStdString(), importPrivate);
update();
return QString::fromStdString(keychain->name());
}
bool KeychainModel::unlockKeychain(const QString& keychainName, const secure_bytes_t& unlockKey)
{
if (!vault) {
throw std::runtime_error("No vault is loaded.");
}
try
{
vault->unlockKeychain(keychainName.toStdString(), unlockKey);
update();
return true;
}
catch (const CoinDB::KeychainPrivateKeyUnlockFailedException& e)
{
emit error(tr("Keychain decryption failed."));
}
catch (const std::exception& e)
{
emit error(e.what());
}
return false;
}
void KeychainModel::lockKeychain(const QString& keychainName)
{
if (!vault) {
throw std::runtime_error("No vault is loaded.");
}
try
{
vault->lockKeychain(keychainName.toStdString());
update();
}
catch (const std::exception& e)
{
emit error(e.what());
}
}
void KeychainModel::lockAllKeychains()
{
if (!vault) {
throw std::runtime_error("No vault is loaded.");
}
try
{
vault->lockAllKeychains();
update();
}
catch (const std::exception& e)
{
emit error(e.what());
}
}
void KeychainModel::encryptKeychain(const QString& keychainName, const secure_bytes_t& lockKey)
{
if (!vault) {
throw std::runtime_error("No vault is loaded.");
}
try
{
vault->encryptKeychain(keychainName.toStdString(), lockKey);
update();
}
catch (const std::exception& e)
{
emit error(e.what());
}
}
void KeychainModel::decryptKeychain(const QString& keychainName)
{
if (!vault) {
throw std::runtime_error("No vault is loaded.");
}
try
{
vault->decryptKeychain(keychainName.toStdString());
update();
}
catch (const std::exception& e)
{
emit error(e.what());
}
}
bool KeychainModel::exists(const QString& keychainName) const
{
QList<QStandardItem*> items = findItems(keychainName, Qt::MatchExactly, 0);
return !items.isEmpty();
}
bool KeychainModel::isPrivate(const QString& keychainName) const
{
if (!vault) {
throw std::runtime_error("No vault is loaded.");
}
std::shared_ptr<Keychain> keychain = vault->getKeychain(keychainName.toStdString());
return keychain->isPrivate();
}
bool KeychainModel::isLocked(const QString& keychainName) const
{
if (!vault) {
throw std::runtime_error("No vault is loaded.");
}
std::shared_ptr<Keychain> keychain = vault->getKeychain(keychainName.toStdString());
return keychain->isLocked();
}
bool KeychainModel::isEncrypted(const QString& keychainName) const
{
if (!vault) {
throw std::runtime_error("No vault is loaded.");
}
std::shared_ptr<Keychain> keychain = vault->getKeychain(keychainName.toStdString());
return keychain->isEncrypted();
}
bool KeychainModel::hasSeed(const QString& keychainName) const
{
if (!vault) {
throw std::runtime_error("No vault is loaded.");
}
std::shared_ptr<Keychain> keychain = vault->getKeychain(keychainName.toStdString());
return keychain->hasSeed();
}
QString KeychainModel::getName(int row) const
{
if (row < 0 || row >= rowCount()) throw std::runtime_error("Invalid row.");
return item(row, 0)->text();
}
int KeychainModel::getStatus(int row) const
{
if (row < 0 || row >= rowCount()) throw std::runtime_error("Invalid row.");
return item(row, 2)->data(Qt::UserRole).toInt();
}
bool KeychainModel::isEncrypted(int row) const
{
if (row < 0 || row >= rowCount()) throw std::runtime_error("Invalid row.");
return item(row, 1)->data(Qt::UserRole).toBool();
}
bytes_t KeychainModel::getExtendedKeyBytes(const QString& keychainName, bool getPrivate, const bytes_t& /*decryptionKey*/) const
{
if (!vault) {
throw std::runtime_error("No vault is loaded.");
}
return vault->exportBIP32(keychainName.toStdString(), getPrivate);
}
QVariant KeychainModel::data(const QModelIndex& index, int role) const
{
/*
if (role == Qt::TextAlignmentRole && index.column() == 1) {
// Right-align numeric fields
return Qt::AlignRight;
}
*/
if (role == Qt::FontRole)
{
QFont font;
if (index.column() == 0)
{
font.setBold(true);
return font;
}
}
return QStandardItemModel::data(index, role);
}
bool KeychainModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (role == Qt::EditRole) {
if (index.column() == 0) {
// Keychain name edited.
if (!vault) return false;
try {
vault->renameKeychain(index.data().toString().toStdString(), value.toString().toStdString());
setItem(index.row(), index.column(), new QStandardItem(value.toString()));
return true;
}
catch (const std::exception& e) {
emit error(QString::fromStdString(e.what()));
}
}
return false;
}
return true;
}
Qt::ItemFlags KeychainModel::flags(const QModelIndex& index) const
{
if (index.column() == 0) {
return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
}
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}