2019-12-30 06:39:22 -03:00
|
|
|
// Copyright (c) 2011-2019 The Bitcoin Core developers
|
2014-12-13 01:09:33 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-04 12:20:43 -03:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <qt/signverifymessagedialog.h>
|
2017-08-15 12:31:26 -03:00
|
|
|
#include <qt/forms/ui_signverifymessagedialog.h>
|
2012-06-15 03:48:26 -04:00
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <qt/addressbookpage.h>
|
|
|
|
#include <qt/guiutil.h>
|
|
|
|
#include <qt/platformstyle.h>
|
|
|
|
#include <qt/walletmodel.h>
|
2012-06-15 03:48:26 -04:00
|
|
|
|
2017-09-19 22:12:25 -03:00
|
|
|
#include <key_io.h>
|
2019-11-22 13:17:29 -03:00
|
|
|
#include <util/message.h> // For MessageSign(), MessageVerify()
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <wallet/wallet.h>
|
2013-01-23 17:51:02 -03:00
|
|
|
|
2012-06-15 03:48:26 -04:00
|
|
|
#include <vector>
|
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
#include <QClipboard>
|
|
|
|
|
2016-09-09 08:43:29 -03:00
|
|
|
SignVerifyMessageDialog::SignVerifyMessageDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
|
2012-06-15 03:48:26 -04:00
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::SignVerifyMessageDialog),
|
2018-07-30 06:37:09 -04:00
|
|
|
model(nullptr),
|
2016-09-09 08:43:29 -03:00
|
|
|
platformStyle(_platformStyle)
|
2012-06-15 03:48:26 -04:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2015-07-28 10:20:14 -03:00
|
|
|
ui->addressBookButton_SM->setIcon(platformStyle->SingleColorIcon(":/icons/address-book"));
|
|
|
|
ui->pasteButton_SM->setIcon(platformStyle->SingleColorIcon(":/icons/editpaste"));
|
|
|
|
ui->copySignatureButton_SM->setIcon(platformStyle->SingleColorIcon(":/icons/editcopy"));
|
|
|
|
ui->signMessageButton_SM->setIcon(platformStyle->SingleColorIcon(":/icons/edit"));
|
|
|
|
ui->clearButton_SM->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
|
|
|
|
ui->addressBookButton_VM->setIcon(platformStyle->SingleColorIcon(":/icons/address-book"));
|
|
|
|
ui->verifyMessageButton_VM->setIcon(platformStyle->SingleColorIcon(":/icons/transaction_0"));
|
|
|
|
ui->clearButton_VM->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
|
2014-11-06 16:55:52 -03:00
|
|
|
|
2012-06-15 03:48:26 -04:00
|
|
|
GUIUtil::setupAddressWidget(ui->addressIn_SM, this);
|
|
|
|
GUIUtil::setupAddressWidget(ui->addressIn_VM, this);
|
|
|
|
|
|
|
|
ui->addressIn_SM->installEventFilter(this);
|
|
|
|
ui->messageIn_SM->installEventFilter(this);
|
|
|
|
ui->signatureOut_SM->installEventFilter(this);
|
|
|
|
ui->addressIn_VM->installEventFilter(this);
|
|
|
|
ui->messageIn_VM->installEventFilter(this);
|
|
|
|
ui->signatureIn_VM->installEventFilter(this);
|
|
|
|
|
2015-10-22 08:33:58 -03:00
|
|
|
ui->signatureOut_SM->setFont(GUIUtil::fixedPitchFont());
|
|
|
|
ui->signatureIn_VM->setFont(GUIUtil::fixedPitchFont());
|
2012-06-15 03:48:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
SignVerifyMessageDialog::~SignVerifyMessageDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2016-09-09 08:43:29 -03:00
|
|
|
void SignVerifyMessageDialog::setModel(WalletModel *_model)
|
2012-06-15 03:48:26 -04:00
|
|
|
{
|
2016-09-09 08:43:29 -03:00
|
|
|
this->model = _model;
|
2012-06-15 03:48:26 -04:00
|
|
|
}
|
|
|
|
|
2013-01-10 10:04:32 -03:00
|
|
|
void SignVerifyMessageDialog::setAddress_SM(const QString &address)
|
2012-06-15 03:48:26 -04:00
|
|
|
{
|
|
|
|
ui->addressIn_SM->setText(address);
|
|
|
|
ui->messageIn_SM->setFocus();
|
|
|
|
}
|
|
|
|
|
2013-01-10 10:04:32 -03:00
|
|
|
void SignVerifyMessageDialog::setAddress_VM(const QString &address)
|
2012-06-15 03:48:26 -04:00
|
|
|
{
|
|
|
|
ui->addressIn_VM->setText(address);
|
|
|
|
ui->messageIn_VM->setFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::showTab_SM(bool fShow)
|
|
|
|
{
|
|
|
|
ui->tabWidget->setCurrentIndex(0);
|
|
|
|
if (fShow)
|
|
|
|
this->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::showTab_VM(bool fShow)
|
|
|
|
{
|
|
|
|
ui->tabWidget->setCurrentIndex(1);
|
|
|
|
if (fShow)
|
|
|
|
this->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::on_addressBookButton_SM_clicked()
|
|
|
|
{
|
|
|
|
if (model && model->getAddressTableModel())
|
|
|
|
{
|
2015-07-28 10:20:14 -03:00
|
|
|
AddressBookPage dlg(platformStyle, AddressBookPage::ForSelection, AddressBookPage::ReceivingTab, this);
|
2012-06-15 03:48:26 -04:00
|
|
|
dlg.setModel(model->getAddressTableModel());
|
|
|
|
if (dlg.exec())
|
|
|
|
{
|
|
|
|
setAddress_SM(dlg.getReturnValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::on_pasteButton_SM_clicked()
|
|
|
|
{
|
|
|
|
setAddress_SM(QApplication::clipboard()->text());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::on_signMessageButton_SM_clicked()
|
|
|
|
{
|
2013-08-23 07:07:20 -04:00
|
|
|
if (!model)
|
|
|
|
return;
|
|
|
|
|
2012-06-15 03:48:26 -04:00
|
|
|
/* Clear old signature to ensure users don't get confused on error with an old signature displayed */
|
|
|
|
ui->signatureOut_SM->clear();
|
|
|
|
|
2017-08-22 22:02:33 -03:00
|
|
|
CTxDestination destination = DecodeDestination(ui->addressIn_SM->text().toStdString());
|
|
|
|
if (!IsValidDestination(destination)) {
|
2012-06-15 03:48:26 -04:00
|
|
|
ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
|
|
|
|
ui->statusLabel_SM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
|
|
|
|
return;
|
|
|
|
}
|
2019-02-19 19:00:45 -03:00
|
|
|
const PKHash* pkhash = boost::get<PKHash>(&destination);
|
|
|
|
if (!pkhash) {
|
2012-06-15 03:48:26 -04:00
|
|
|
ui->addressIn_SM->setValid(false);
|
|
|
|
ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
|
|
|
|
ui->statusLabel_SM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WalletModel::UnlockContext ctx(model->requestUnlock());
|
|
|
|
if (!ctx.isValid())
|
|
|
|
{
|
|
|
|
ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
|
2012-07-25 20:48:39 -04:00
|
|
|
ui->statusLabel_SM->setText(tr("Wallet unlock was cancelled."));
|
2012-06-15 03:48:26 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CKey key;
|
2019-10-07 15:11:34 -03:00
|
|
|
if (!model->wallet().getPrivKey(GetScriptForDestination(destination), CKeyID(*pkhash), key))
|
2012-06-15 03:48:26 -04:00
|
|
|
{
|
|
|
|
ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
|
|
|
|
ui->statusLabel_SM->setText(tr("Private key for the entered address is not available."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-22 13:17:29 -03:00
|
|
|
const std::string& message = ui->messageIn_SM->document()->toPlainText().toStdString();
|
|
|
|
std::string signature;
|
2012-06-15 03:48:26 -04:00
|
|
|
|
2019-11-22 13:17:29 -03:00
|
|
|
if (!MessageSign(key, message, signature)) {
|
2012-06-15 03:48:26 -04:00
|
|
|
ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
|
|
|
|
ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signing failed.") + QString("</nobr>"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->statusLabel_SM->setStyleSheet("QLabel { color: green; }");
|
|
|
|
ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signed.") + QString("</nobr>"));
|
|
|
|
|
2019-11-22 13:17:29 -03:00
|
|
|
ui->signatureOut_SM->setText(QString::fromStdString(signature));
|
2012-06-15 03:48:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::on_copySignatureButton_SM_clicked()
|
|
|
|
{
|
2013-11-16 13:52:37 -03:00
|
|
|
GUIUtil::setClipboard(ui->signatureOut_SM->text());
|
2012-06-15 03:48:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::on_clearButton_SM_clicked()
|
|
|
|
{
|
|
|
|
ui->addressIn_SM->clear();
|
|
|
|
ui->messageIn_SM->clear();
|
|
|
|
ui->signatureOut_SM->clear();
|
|
|
|
ui->statusLabel_SM->clear();
|
|
|
|
|
|
|
|
ui->addressIn_SM->setFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::on_addressBookButton_VM_clicked()
|
|
|
|
{
|
|
|
|
if (model && model->getAddressTableModel())
|
|
|
|
{
|
2015-07-28 10:20:14 -03:00
|
|
|
AddressBookPage dlg(platformStyle, AddressBookPage::ForSelection, AddressBookPage::SendingTab, this);
|
2012-06-15 03:48:26 -04:00
|
|
|
dlg.setModel(model->getAddressTableModel());
|
|
|
|
if (dlg.exec())
|
|
|
|
{
|
|
|
|
setAddress_VM(dlg.getReturnValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked()
|
|
|
|
{
|
2019-11-19 11:49:35 -03:00
|
|
|
const std::string& address = ui->addressIn_VM->text().toStdString();
|
|
|
|
const std::string& signature = ui->signatureIn_VM->text().toStdString();
|
|
|
|
const std::string& message = ui->messageIn_VM->document()->toPlainText().toStdString();
|
|
|
|
|
|
|
|
const auto result = MessageVerify(address, signature, message);
|
|
|
|
|
|
|
|
if (result == MessageVerificationResult::OK) {
|
|
|
|
ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }");
|
|
|
|
} else {
|
2012-06-15 03:48:26 -04:00
|
|
|
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
|
|
|
|
}
|
2019-11-19 11:49:35 -03:00
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case MessageVerificationResult::OK:
|
|
|
|
ui->statusLabel_VM->setText(
|
|
|
|
QString("<nobr>") + tr("Message verified.") + QString("</nobr>")
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
case MessageVerificationResult::ERR_INVALID_ADDRESS:
|
|
|
|
ui->statusLabel_VM->setText(
|
|
|
|
tr("The entered address is invalid.") + QString(" ") +
|
|
|
|
tr("Please check the address and try again.")
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
case MessageVerificationResult::ERR_ADDRESS_NO_KEY:
|
2012-06-15 03:48:26 -04:00
|
|
|
ui->addressIn_VM->setValid(false);
|
2019-11-19 11:49:35 -03:00
|
|
|
ui->statusLabel_VM->setText(
|
|
|
|
tr("The entered address does not refer to a key.") + QString(" ") +
|
|
|
|
tr("Please check the address and try again.")
|
|
|
|
);
|
2012-06-15 03:48:26 -04:00
|
|
|
return;
|
2019-11-19 11:49:35 -03:00
|
|
|
case MessageVerificationResult::ERR_MALFORMED_SIGNATURE:
|
2012-06-15 03:48:26 -04:00
|
|
|
ui->signatureIn_VM->setValid(false);
|
2019-11-19 11:49:35 -03:00
|
|
|
ui->statusLabel_VM->setText(
|
|
|
|
tr("The signature could not be decoded.") + QString(" ") +
|
|
|
|
tr("Please check the signature and try again.")
|
|
|
|
);
|
2012-06-15 03:48:26 -04:00
|
|
|
return;
|
2019-11-19 11:49:35 -03:00
|
|
|
case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED:
|
2012-06-15 03:48:26 -04:00
|
|
|
ui->signatureIn_VM->setValid(false);
|
2019-11-19 11:49:35 -03:00
|
|
|
ui->statusLabel_VM->setText(
|
|
|
|
tr("The signature did not match the message digest.") + QString(" ") +
|
|
|
|
tr("Please check the signature and try again.")
|
|
|
|
);
|
2012-06-15 03:48:26 -04:00
|
|
|
return;
|
2019-11-19 11:49:35 -03:00
|
|
|
case MessageVerificationResult::ERR_NOT_SIGNED:
|
|
|
|
ui->statusLabel_VM->setText(
|
|
|
|
QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>")
|
|
|
|
);
|
2012-06-15 03:48:26 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::on_clearButton_VM_clicked()
|
|
|
|
{
|
|
|
|
ui->addressIn_VM->clear();
|
|
|
|
ui->signatureIn_VM->clear();
|
|
|
|
ui->messageIn_VM->clear();
|
|
|
|
ui->statusLabel_VM->clear();
|
|
|
|
|
|
|
|
ui->addressIn_VM->setFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SignVerifyMessageDialog::eventFilter(QObject *object, QEvent *event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::FocusIn)
|
|
|
|
{
|
|
|
|
if (ui->tabWidget->currentIndex() == 0)
|
|
|
|
{
|
|
|
|
/* Clear status message on focus change */
|
|
|
|
ui->statusLabel_SM->clear();
|
|
|
|
|
|
|
|
/* Select generated signature */
|
|
|
|
if (object == ui->signatureOut_SM)
|
|
|
|
{
|
|
|
|
ui->signatureOut_SM->selectAll();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ui->tabWidget->currentIndex() == 1)
|
|
|
|
{
|
|
|
|
/* Clear status message on focus change */
|
|
|
|
ui->statusLabel_VM->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QDialog::eventFilter(object, event);
|
|
|
|
}
|