forked from ciphrex/mSIGNA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignatureactions.cpp
228 lines (195 loc) · 7.18 KB
/
signatureactions.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
///////////////////////////////////////////////////////////////////////////////
//
// mSIGNA
//
// signatureactions.h
//
// Copyright (c) 2013-2014 Eric Lombrozo
//
// All Rights Reserved.
#include "signatureactions.h"
#include "signaturemodel.h"
#include "signatureview.h"
#include "signaturedialog.h"
#include "passphrasedialog.h"
#include "entropysource.h"
#include <CoinDB/SynchedVault.h>
#include <CoinDB/Passphrase.h>
#include <QAction>
#include <QMenu>
#include <QMessageBox>
#include <QApplication>
#include <QClipboard>
#include <QFileDialog>
#include <logger/logger.h>
SignatureActions::SignatureActions(CoinDB::SynchedVault& synchedVault, SignatureDialog& dialog)
: m_synchedVault(synchedVault), m_dialog(dialog), m_currentRow(-1)
{
createActions();
createMenus();
connect(m_dialog.getView()->selectionModel(), &QItemSelectionModel::currentChanged, this, &SignatureActions::updateCurrentKeychain);
}
SignatureActions::~SignatureActions()
{
delete addSignatureAction;
delete unlockKeychainAction;
delete lockKeychainAction;
delete menu;
}
void SignatureActions::updateCurrentKeychain(const QModelIndex& current, const QModelIndex& /*previous*/)
{
m_currentRow = current.row();
refreshCurrentKeychain();
}
void SignatureActions::refreshCurrentKeychain()
{
if (m_currentRow != -1)
{
QStandardItem* keychainNameItem = m_dialog.getModel()->item(m_currentRow, 0);
m_currentKeychain = keychainNameItem->text();
if (m_synchedVault.isVaultOpen())
{
int keychainState = m_dialog.getModel()->getKeychainState(m_currentRow);
bool hasSigned = m_dialog.getModel()->getKeychainHasSigned(m_currentRow);
int sigsNeeded = m_dialog.getModel()->getSigsNeeded();
addSignatureAction->setEnabled(keychainState != SignatureModel::PUBLIC && !hasSigned && sigsNeeded > 0);
unlockKeychainAction->setEnabled(keychainState == SignatureModel::LOCKED);
lockKeychainAction->setEnabled(keychainState == SignatureModel::UNLOCKED);
return;
}
}
else
{
m_currentKeychain = "";
}
addSignatureAction->setEnabled(false);
unlockKeychainAction->setEnabled(false);
lockKeychainAction->setEnabled(false);
}
void SignatureActions::addSignature()
{
try
{
if (m_currentKeychain.isEmpty()) throw std::runtime_error("No keychain is selected.");
if (!m_synchedVault.isVaultOpen()) throw std::runtime_error("No vault is open.");
CoinDB::Vault* vault = m_synchedVault.getVault();
if (!vault->isKeychainPrivate(m_currentKeychain.toStdString()))
{
QMessageBox::critical(nullptr, tr("Error"), tr("Keychain is public."));
return;
}
QString currentKeychain = m_currentKeychain;
if (vault->isKeychainLocked(m_currentKeychain.toStdString()) && !unlockKeychain()) return;
std::vector<std::string> keychainNames;
keychainNames.push_back(currentKeychain.toStdString());
seedEntropySource(false, &m_dialog);
bytes_t txhash = m_dialog.getModel()->getTxHash();
vault->signTx(txhash, keychainNames, true);
if (keychainNames.empty())
{
QMessageBox::critical(nullptr, tr("Error"), tr("Signature could not be added."));
return;
}
m_dialog.updateTx();
if (m_synchedVault.isConnected() && m_dialog.getModel()->getSigsNeeded() == 0)
{
QMessageBox sendPrompt;
sendPrompt.setText(tr("Transaction is fully signed. Would you like to send?"));
sendPrompt.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
sendPrompt.setDefaultButton(QMessageBox::Yes);
if (sendPrompt.exec() == QMessageBox::Yes)
{
if (!m_synchedVault.isConnected())
{
QMessageBox::critical(nullptr, tr("Error"), tr("Connection was lost."));
return;
}
std::shared_ptr<CoinDB::Tx> tx = vault->getTx(txhash);
Coin::Transaction coin_tx = tx->toCoinCore();
m_synchedVault.sendTx(coin_tx);
}
}
}
catch (const std::exception& e)
{
emit error(e.what());
}
}
bool SignatureActions::unlockKeychain()
{
try
{
if (m_currentKeychain.isEmpty()) throw std::runtime_error("No keychain is selected.");
if (!m_synchedVault.isVaultOpen()) throw std::runtime_error("No vault is open.");
CoinDB::Vault* vault = m_synchedVault.getVault();
std::string keychainName = m_currentKeychain.toStdString();
if (!vault->isKeychainLocked(keychainName))
{
QMessageBox::critical(nullptr, tr("Error"), tr("Keychain is already unlocked."));
return true;
}
secure_bytes_t lockKey;
if (vault->isKeychainEncrypted(keychainName))
{
PassphraseDialog dlg(tr("Please enter the decryption passphrase for ") + m_currentKeychain + ":");
if (!dlg.exec()) return false;
lockKey = passphraseHash(dlg.getPassphrase().toStdString());
}
vault->unlockKeychain(keychainName, lockKey);
refreshCurrentKeychain();
m_dialog.updateKeychains();
}
catch (const CoinDB::KeychainPrivateKeyUnlockFailedException& e)
{
emit error(tr("Keychain decryption failed."));
return false;
}
catch (const std::exception& e)
{
emit error(e.what());
return false;
}
return true;
}
void SignatureActions::lockKeychain()
{
try
{
if (m_currentKeychain.isEmpty()) throw std::runtime_error("No keychain is selected.");
if (!m_synchedVault.isVaultOpen()) throw std::runtime_error("No vault is open.");
CoinDB::Vault* vault = m_synchedVault.getVault();
std::string keychainName = m_currentKeychain.toStdString();
if (vault->isKeychainLocked(keychainName))
{
QMessageBox::critical(nullptr, tr("Error"), tr("Keychain is already locked."));
return;
}
vault->lockKeychain(keychainName);
refreshCurrentKeychain();
m_dialog.updateKeychains();
}
catch (const std::exception& e)
{
emit error(e.what());
}
}
void SignatureActions::createActions()
{
addSignatureAction = new QAction(QIcon(":/icons/glossy-black-signature_32x32.png"), tr("Add Signature..."), this);
addSignatureAction->setEnabled(false);
connect(addSignatureAction, SIGNAL(triggered()), this, SLOT(addSignature()));
unlockKeychainAction = new QAction(QIcon(":/icons/unlocked.png"), tr("Unlock Keychain..."), this);
unlockKeychainAction->setEnabled(false);
connect(unlockKeychainAction, SIGNAL(triggered()), this, SLOT(unlockKeychain()));
lockKeychainAction = new QAction(QIcon(":/icons/locked.png"), tr("Lock Keychain"), this);
lockKeychainAction->setEnabled(false);
connect(lockKeychainAction, SIGNAL(triggered()), this, SLOT(lockKeychain()));
}
void SignatureActions::createMenus()
{
menu = new QMenu();
menu->addAction(addSignatureAction);
menu->addSeparator();
menu->addAction(unlockKeychainAction);
menu->addAction(lockKeychainAction);
}