1 #include "signverifymessagedialog.h"
2 #include "ui_signverifymessagedialog.h"
4 #include "addressbookpage.h"
7 #include "dialogwindowflags.h"
10 #include "optionsmodel.h"
11 #include "walletmodel.h"
19 SignVerifyMessageDialog::SignVerifyMessageDialog(QWidget *parent) :
20 QDialog(parent, DIALOGWINDOWHINTS),
21 ui(new Ui::SignVerifyMessageDialog),
26 #if (QT_VERSION >= 0x040700)
27 /* Do not move this to the XML file, Qt before 4.7 will choke on it */
28 ui->addressIn_SM->setPlaceholderText(tr("Enter a NovaCoin address (e.g. 4Zo1ga6xuKuQ7JV7M9rGDoxdbYwV5zgQJ5)"));
29 ui->signatureOut_SM->setPlaceholderText(tr("Click \"Sign Message\" to generate signature"));
31 ui->addressIn_VM->setPlaceholderText(tr("Enter a NovaCoin address (e.g. 4Zo1ga6xuKuQ7JV7M9rGDoxdbYwV5zgQJ5)"));
32 ui->signatureIn_VM->setPlaceholderText(tr("Enter NovaCoin signature"));
35 GUIUtil::setupAddressWidget(ui->addressIn_SM, this);
36 GUIUtil::setupAddressWidget(ui->addressIn_VM, this);
38 ui->addressIn_SM->installEventFilter(this);
39 ui->messageIn_SM->installEventFilter(this);
40 ui->signatureOut_SM->installEventFilter(this);
41 ui->addressIn_VM->installEventFilter(this);
42 ui->messageIn_VM->installEventFilter(this);
43 ui->signatureIn_VM->installEventFilter(this);
45 ui->signatureOut_SM->setFont(GUIUtil::bitcoinAddressFont());
46 ui->signatureIn_VM->setFont(GUIUtil::bitcoinAddressFont());
49 SignVerifyMessageDialog::~SignVerifyMessageDialog()
54 void SignVerifyMessageDialog::setModel(WalletModel *model)
59 void SignVerifyMessageDialog::setAddress_SM(QString address)
61 ui->addressIn_SM->setText(address);
62 ui->messageIn_SM->setFocus();
65 void SignVerifyMessageDialog::setAddress_VM(QString address)
67 ui->addressIn_VM->setText(address);
68 ui->messageIn_VM->setFocus();
71 void SignVerifyMessageDialog::showTab_SM(bool fShow)
73 ui->tabWidget->setCurrentIndex(0);
79 void SignVerifyMessageDialog::showTab_VM(bool fShow)
81 ui->tabWidget->setCurrentIndex(1);
86 void SignVerifyMessageDialog::on_addressBookButton_SM_clicked()
88 if (model && model->getAddressTableModel())
90 AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::ReceivingTab, this);
91 dlg.setModel(model->getAddressTableModel());
94 setAddress_SM(dlg.getReturnValue());
99 void SignVerifyMessageDialog::on_pasteButton_SM_clicked()
101 setAddress_SM(QApplication::clipboard()->text());
104 void SignVerifyMessageDialog::on_signMessageButton_SM_clicked()
106 /* Clear old signature to ensure users don't get confused on error with an old signature displayed */
107 ui->signatureOut_SM->clear();
109 CBitcoinAddress addr(ui->addressIn_SM->text().toStdString());
112 ui->addressIn_SM->setValid(false);
113 ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
114 ui->statusLabel_SM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
118 if (!addr.GetKeyID(keyID))
120 ui->addressIn_SM->setValid(false);
121 ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
122 ui->statusLabel_SM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
126 WalletModel::UnlockContext ctx(model->requestUnlock());
129 ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
130 ui->statusLabel_SM->setText(tr("Wallet unlock was cancelled."));
135 if (!pwalletMain->GetKey(keyID, key))
137 ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
138 ui->statusLabel_SM->setText(tr("Private key for the entered address is not available."));
142 CDataStream ss(SER_GETHASH, 0);
143 ss << strMessageMagic;
144 ss << ui->messageIn_SM->document()->toPlainText().toStdString();
146 std::vector<unsigned char> vchSig;
147 if (!key.SignCompact(Hash(ss.begin(), ss.end()), vchSig))
149 ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
150 ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signing failed.") + QString("</nobr>"));
154 ui->statusLabel_SM->setStyleSheet("QLabel { color: green; }");
155 ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signed.") + QString("</nobr>"));
157 ui->signatureOut_SM->setText(QString::fromStdString(EncodeBase64(&vchSig[0], vchSig.size())));
160 void SignVerifyMessageDialog::on_copySignatureButton_SM_clicked()
162 QApplication::clipboard()->setText(ui->signatureOut_SM->text());
165 void SignVerifyMessageDialog::on_clearButton_SM_clicked()
167 ui->addressIn_SM->clear();
168 ui->messageIn_SM->clear();
169 ui->signatureOut_SM->clear();
170 ui->statusLabel_SM->clear();
172 ui->addressIn_SM->setFocus();
175 void SignVerifyMessageDialog::on_addressBookButton_VM_clicked()
177 if (model && model->getAddressTableModel())
179 AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this);
180 dlg.setModel(model->getAddressTableModel());
183 setAddress_VM(dlg.getReturnValue());
188 void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked()
190 CBitcoinAddress addr(ui->addressIn_VM->text().toStdString());
193 ui->addressIn_VM->setValid(false);
194 ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
195 ui->statusLabel_VM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
199 if (!addr.GetKeyID(keyID))
201 ui->addressIn_VM->setValid(false);
202 ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
203 ui->statusLabel_VM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
207 bool fInvalid = false;
208 std::vector<unsigned char> vchSig = DecodeBase64(ui->signatureIn_VM->text().toStdString().c_str(), &fInvalid);
212 ui->signatureIn_VM->setValid(false);
213 ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
214 ui->statusLabel_VM->setText(tr("The signature could not be decoded.") + QString(" ") + tr("Please check the signature and try again."));
218 CDataStream ss(SER_GETHASH, 0);
219 ss << strMessageMagic;
220 ss << ui->messageIn_VM->document()->toPlainText().toStdString();
223 if (!key.SetCompactSignature(Hash(ss.begin(), ss.end()), vchSig))
225 ui->signatureIn_VM->setValid(false);
226 ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
227 ui->statusLabel_VM->setText(tr("The signature did not match the message digest.") + QString(" ") + tr("Please check the signature and try again."));
231 if (!(CBitcoinAddress(key.GetPubKey().GetID()) == addr))
233 ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
234 ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>"));
238 ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }");
239 ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verified.") + QString("</nobr>"));
242 void SignVerifyMessageDialog::on_clearButton_VM_clicked()
244 ui->addressIn_VM->clear();
245 ui->signatureIn_VM->clear();
246 ui->messageIn_VM->clear();
247 ui->statusLabel_VM->clear();
249 ui->addressIn_VM->setFocus();
252 bool SignVerifyMessageDialog::eventFilter(QObject *object, QEvent *event)
254 if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::FocusIn)
256 if (ui->tabWidget->currentIndex() == 0)
258 /* Clear status message on focus change */
259 ui->statusLabel_SM->clear();
261 /* Select generated signature */
262 if (object == ui->signatureOut_SM)
264 ui->signatureOut_SM->selectAll();
268 else if (ui->tabWidget->currentIndex() == 1)
270 /* Clear status message on focus change */
271 ui->statusLabel_VM->clear();
274 return QDialog::eventFilter(object, event);