2013-11-04 16:20:43 +01:00
|
|
|
// Copyright (c) 2011-2013 The Bitcoin developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2012-06-15 09:48:26 +02:00
|
|
|
#include "signverifymessagedialog.h"
|
|
|
|
#include "ui_signverifymessagedialog.h"
|
|
|
|
|
|
|
|
#include "addressbookpage.h"
|
|
|
|
#include "base58.h"
|
|
|
|
#include "guiutil.h"
|
|
|
|
#include "init.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "optionsmodel.h"
|
|
|
|
#include "walletmodel.h"
|
|
|
|
#include "wallet.h"
|
|
|
|
|
2013-01-23 21:51:02 +01:00
|
|
|
#include <QClipboard>
|
|
|
|
|
2012-06-15 09:48:26 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
SignVerifyMessageDialog::SignVerifyMessageDialog(QWidget *parent) :
|
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::SignVerifyMessageDialog),
|
|
|
|
model(0)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
#if (QT_VERSION >= 0x040700)
|
|
|
|
/* Do not move this to the XML file, Qt before 4.7 will choke on it */
|
|
|
|
ui->addressIn_SM->setPlaceholderText(tr("Enter a Bitcoin address (e.g. 1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L)"));
|
|
|
|
ui->signatureOut_SM->setPlaceholderText(tr("Click \"Sign Message\" to generate signature"));
|
|
|
|
|
|
|
|
ui->addressIn_VM->setPlaceholderText(tr("Enter a Bitcoin address (e.g. 1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L)"));
|
|
|
|
ui->signatureIn_VM->setPlaceholderText(tr("Enter Bitcoin signature"));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
ui->signatureOut_SM->setFont(GUIUtil::bitcoinAddressFont());
|
|
|
|
ui->signatureIn_VM->setFont(GUIUtil::bitcoinAddressFont());
|
|
|
|
}
|
|
|
|
|
|
|
|
SignVerifyMessageDialog::~SignVerifyMessageDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::setModel(WalletModel *model)
|
|
|
|
{
|
|
|
|
this->model = model;
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
{
|
2013-10-16 15:14:26 +02:00
|
|
|
AddressBookPage dlg(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();
|
|
|
|
|
|
|
|
CBitcoinAddress addr(ui->addressIn_SM->text().toStdString());
|
|
|
|
if (!addr.IsValid())
|
|
|
|
{
|
|
|
|
ui->addressIn_SM->setValid(false);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
CKeyID keyID;
|
|
|
|
if (!addr.GetKeyID(keyID))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
CKey key;
|
|
|
|
if (!pwalletMain->GetKey(keyID, key))
|
|
|
|
{
|
|
|
|
ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
|
|
|
|
ui->statusLabel_SM->setText(tr("Private key for the entered address is not available."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CDataStream ss(SER_GETHASH, 0);
|
|
|
|
ss << strMessageMagic;
|
|
|
|
ss << ui->messageIn_SM->document()->toPlainText().toStdString();
|
|
|
|
|
|
|
|
std::vector<unsigned char> vchSig;
|
|
|
|
if (!key.SignCompact(Hash(ss.begin(), ss.end()), vchSig))
|
|
|
|
{
|
|
|
|
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>"));
|
|
|
|
|
|
|
|
ui->signatureOut_SM->setText(QString::fromStdString(EncodeBase64(&vchSig[0], vchSig.size())));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignVerifyMessageDialog::on_copySignatureButton_SM_clicked()
|
|
|
|
{
|
|
|
|
QApplication::clipboard()->setText(ui->signatureOut_SM->text());
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
2013-10-16 15:14:26 +02:00
|
|
|
AddressBookPage dlg(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()
|
|
|
|
{
|
|
|
|
CBitcoinAddress addr(ui->addressIn_VM->text().toStdString());
|
|
|
|
if (!addr.IsValid())
|
|
|
|
{
|
|
|
|
ui->addressIn_VM->setValid(false);
|
|
|
|
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
|
|
|
|
ui->statusLabel_VM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CKeyID keyID;
|
|
|
|
if (!addr.GetKeyID(keyID))
|
|
|
|
{
|
|
|
|
ui->addressIn_VM->setValid(false);
|
|
|
|
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
|
|
|
|
ui->statusLabel_VM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fInvalid = false;
|
|
|
|
std::vector<unsigned char> vchSig = DecodeBase64(ui->signatureIn_VM->text().toStdString().c_str(), &fInvalid);
|
|
|
|
|
|
|
|
if (fInvalid)
|
|
|
|
{
|
|
|
|
ui->signatureIn_VM->setValid(false);
|
|
|
|
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
|
|
|
|
ui->statusLabel_VM->setText(tr("The signature could not be decoded.") + QString(" ") + tr("Please check the signature and try again."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CDataStream ss(SER_GETHASH, 0);
|
|
|
|
ss << strMessageMagic;
|
|
|
|
ss << ui->messageIn_VM->document()->toPlainText().toStdString();
|
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
CPubKey pubkey;
|
|
|
|
if (!pubkey.RecoverCompact(Hash(ss.begin(), ss.end()), vchSig))
|
2012-06-15 09:48:26 +02:00
|
|
|
{
|
|
|
|
ui->signatureIn_VM->setValid(false);
|
|
|
|
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
|
|
|
|
ui->statusLabel_VM->setText(tr("The signature did not match the message digest.") + QString(" ") + tr("Please check the signature and try again."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
if (!(CBitcoinAddress(pubkey.GetID()) == addr))
|
2012-06-15 09:48:26 +02:00
|
|
|
{
|
|
|
|
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
|
|
|
|
ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }");
|
|
|
|
ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verified.") + QString("</nobr>"));
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|