2020-04-16 19:14:08 +02:00
|
|
|
// Copyright (c) 2011-2020 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-04 16:20:43 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <qt/signverifymessagedialog.h>
|
2017-08-15 17:31:26 +02:00
|
|
|
#include <qt/forms/ui_signverifymessagedialog.h>
|
2012-06-15 09:48:26 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <qt/addressbookpage.h>
|
|
|
|
#include <qt/guiutil.h>
|
|
|
|
#include <qt/platformstyle.h>
|
|
|
|
#include <qt/walletmodel.h>
|
2012-06-15 09:48:26 +02:00
|
|
|
|
2017-09-20 03:12:25 +02:00
|
|
|
#include <key_io.h>
|
2019-11-22 17:17:29 +01:00
|
|
|
#include <util/message.h> // For MessageSign(), MessageVerify()
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <wallet/wallet.h>
|
2013-01-23 21:51:02 +01:00
|
|
|
|
2012-06-15 09:48:26 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <QClipboard>
|
|
|
|
|
2016-09-09 13:43:29 +02:00
|
|
|
SignVerifyMessageDialog::SignVerifyMessageDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
|
2012-06-15 09:48:26 +02:00
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::SignVerifyMessageDialog),
|
2018-07-30 12:37:09 +02:00
|
|
|
model(nullptr),
|
2016-09-09 13:43:29 +02:00
|
|
|
platformStyle(_platformStyle)
|
2012-06-15 09:48:26 +02:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2015-07-28 15:20:14 +02: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 20:55:52 +01:00
|
|
|
|
2012-06-15 09:48:26 +02: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 13:33:58 +02:00
|
|
|
ui->signatureOut_SM->setFont(GUIUtil::fixedPitchFont());
|
|
|
|
ui->signatureIn_VM->setFont(GUIUtil::fixedPitchFont());
|
2019-04-07 21:33:35 +02:00
|
|
|
|
|
|
|
GUIUtil::handleCloseWindowShortcut(this);
|
2012-06-15 09:48:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SignVerifyMessageDialog::~SignVerifyMessageDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2016-09-09 13:43:29 +02:00
|
|
|
void SignVerifyMessageDialog::setModel(WalletModel *_model)
|
2012-06-15 09:48:26 +02:00
|
|
|
{
|
2016-09-09 13:43:29 +02:00
|
|
|
this->model = _model;
|
2012-06-15 09:48:26 +02:00
|
|
|
}
|
|
|
|
|
2013-01-10 14:04:32 +01:00
|
|
|
void SignVerifyMessageDialog::setAddress_SM(const QString &address)
|
2012-06-15 09:48:26 +02:00
|
|
|
{
|
|
|
|
ui->addressIn_SM->setText(address);
|
|
|
|
ui->messageIn_SM->setFocus();
|
|
|
|
}
|
|
|
|
|
2013-01-10 14:04:32 +01:00
|
|
|
void SignVerifyMessageDialog::setAddress_VM(const QString &address)
|
2012-06-15 09:48:26 +02: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())
|
|
|
|
{
|
2020-01-13 19:50:44 +01:00
|
|
|
model->refresh(/* pk_hash_only */ true);
|
2015-07-28 15:20:14 +02:00
|
|
|
AddressBookPage dlg(platformStyle, AddressBookPage::ForSelection, AddressBookPage::ReceivingTab, this);
|
2012-06-15 09:48:26 +02: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 13:07:20 +02:00
|
|
|
if (!model)
|
|
|
|
return;
|
|
|
|
|
2012-06-15 09:48:26 +02:00
|
|
|
/* Clear old signature to ensure users don't get confused on error with an old signature displayed */
|
|
|
|
ui->signatureOut_SM->clear();
|
|
|
|
|
2017-08-23 03:02:33 +02:00
|
|
|
CTxDestination destination = DecodeDestination(ui->addressIn_SM->text().toStdString());
|
|
|
|
if (!IsValidDestination(destination)) {
|
2012-06-15 09:48:26 +02: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 23:00:45 +01:00
|
|
|
const PKHash* pkhash = boost::get<PKHash>(&destination);
|
|
|
|
if (!pkhash) {
|
2012-06-15 09:48:26 +02: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-26 02:48:39 +02:00
|
|
|
ui->statusLabel_SM->setText(tr("Wallet unlock was cancelled."));
|
2012-06-15 09:48:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:17:29 +01:00
|
|
|
const std::string& message = ui->messageIn_SM->document()->toPlainText().toStdString();
|
|
|
|
std::string signature;
|
2020-02-13 23:09:12 +01:00
|
|
|
SigningResult res = model->wallet().signMessage(message, *pkhash, signature);
|
|
|
|
|
|
|
|
QString error;
|
|
|
|
switch (res) {
|
|
|
|
case SigningResult::OK:
|
|
|
|
error = tr("No error");
|
|
|
|
break;
|
|
|
|
case SigningResult::PRIVATE_KEY_NOT_AVAILABLE:
|
|
|
|
error = tr("Private key for the entered address is not available.");
|
|
|
|
break;
|
|
|
|
case SigningResult::SIGNING_FAILED:
|
|
|
|
error = tr("Message signing failed.");
|
|
|
|
break;
|
|
|
|
// no default case, so the compiler can warn about missing cases
|
|
|
|
}
|
2012-06-15 09:48:26 +02:00
|
|
|
|
2020-02-13 23:09:12 +01:00
|
|
|
if (res != SigningResult::OK) {
|
2012-06-15 09:48:26 +02:00
|
|
|
ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
|
2020-02-13 23:09:12 +01:00
|
|
|
ui->statusLabel_SM->setText(QString("<nobr>") + error + QString("</nobr>"));
|
2012-06-15 09:48:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->statusLabel_SM->setStyleSheet("QLabel { color: green; }");
|
|
|
|
ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signed.") + QString("</nobr>"));
|
|
|
|
|
2019-11-22 17:17:29 +01:00
|
|
|
ui->signatureOut_SM->setText(QString::fromStdString(signature));
|
2012-06-15 09:48:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::on_copySignatureButton_SM_clicked()
|
|
|
|
{
|
2013-11-16 17:52:37 +01:00
|
|
|
GUIUtil::setClipboard(ui->signatureOut_SM->text());
|
2012-06-15 09:48:26 +02: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 15:20:14 +02:00
|
|
|
AddressBookPage dlg(platformStyle, AddressBookPage::ForSelection, AddressBookPage::SendingTab, this);
|
2012-06-15 09:48:26 +02:00
|
|
|
dlg.setModel(model->getAddressTableModel());
|
|
|
|
if (dlg.exec())
|
|
|
|
{
|
|
|
|
setAddress_VM(dlg.getReturnValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked()
|
|
|
|
{
|
2019-11-19 15:49:35 +01: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 09:48:26 +02:00
|
|
|
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
|
|
|
|
}
|
2019-11-19 15:49:35 +01: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 09:48:26 +02:00
|
|
|
ui->addressIn_VM->setValid(false);
|
2019-11-19 15:49:35 +01: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 09:48:26 +02:00
|
|
|
return;
|
2019-11-19 15:49:35 +01:00
|
|
|
case MessageVerificationResult::ERR_MALFORMED_SIGNATURE:
|
2012-06-15 09:48:26 +02:00
|
|
|
ui->signatureIn_VM->setValid(false);
|
2019-11-19 15:49:35 +01:00
|
|
|
ui->statusLabel_VM->setText(
|
|
|
|
tr("The signature could not be decoded.") + QString(" ") +
|
|
|
|
tr("Please check the signature and try again.")
|
|
|
|
);
|
2012-06-15 09:48:26 +02:00
|
|
|
return;
|
2019-11-19 15:49:35 +01:00
|
|
|
case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED:
|
2012-06-15 09:48:26 +02:00
|
|
|
ui->signatureIn_VM->setValid(false);
|
2019-11-19 15:49:35 +01: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 09:48:26 +02:00
|
|
|
return;
|
2019-11-19 15:49:35 +01:00
|
|
|
case MessageVerificationResult::ERR_NOT_SIGNED:
|
|
|
|
ui->statusLabel_VM->setText(
|
|
|
|
QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>")
|
|
|
|
);
|
2012-06-15 09:48:26 +02: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);
|
|
|
|
}
|