Changeset View
Changeset View
Standalone View
Standalone View
src/qt/signverifymessagedialog.cpp
// Copyright (c) 2011-2016 The Bitcoin Core developers | // Copyright (c) 2011-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <qt/forms/ui_signverifymessagedialog.h> | #include <qt/forms/ui_signverifymessagedialog.h> | ||||
#include <qt/signverifymessagedialog.h> | #include <qt/signverifymessagedialog.h> | ||||
#include <key_io.h> | #include <key_io.h> | ||||
#include <qt/addressbookpage.h> | #include <qt/addressbookpage.h> | ||||
#include <qt/guiutil.h> | #include <qt/guiutil.h> | ||||
#include <qt/platformstyle.h> | #include <qt/platformstyle.h> | ||||
#include <qt/walletmodel.h> | #include <qt/walletmodel.h> | ||||
#include <util/validation.h> // For strMessageMagic | #include <util/message.h> // For MessageSign(), MessageVerify() | ||||
#include <wallet/wallet.h> | #include <wallet/wallet.h> | ||||
#include <QClipboard> | #include <QClipboard> | ||||
#include <vector> | #include <vector> | ||||
SignVerifyMessageDialog::SignVerifyMessageDialog( | SignVerifyMessageDialog::SignVerifyMessageDialog( | ||||
const PlatformStyle *_platformStyle, QWidget *parent) | const PlatformStyle *_platformStyle, QWidget *parent) | ||||
▲ Show 20 Lines • Show All 121 Lines • ▼ Show 20 Lines | void SignVerifyMessageDialog::on_signMessageButton_SM_clicked() { | ||||
if (!model->wallet().getPrivKey(GetScriptForDestination(destination), | if (!model->wallet().getPrivKey(GetScriptForDestination(destination), | ||||
CKeyID(*pkhash), key)) { | CKeyID(*pkhash), key)) { | ||||
ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }"); | ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }"); | ||||
ui->statusLabel_SM->setText( | ui->statusLabel_SM->setText( | ||||
tr("Private key for the entered address is not available.")); | tr("Private key for the entered address is not available.")); | ||||
return; | return; | ||||
} | } | ||||
CHashWriter ss(SER_GETHASH, 0); | const std::string &message = | ||||
ss << strMessageMagic; | ui->messageIn_SM->document()->toPlainText().toStdString(); | ||||
ss << ui->messageIn_SM->document()->toPlainText().toStdString(); | std::string signature; | ||||
std::vector<uint8_t> vchSig; | if (!MessageSign(key, message, signature)) { | ||||
if (!key.SignCompact(ss.GetHash(), vchSig)) { | |||||
ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }"); | ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }"); | ||||
ui->statusLabel_SM->setText(QString("<nobr>") + | ui->statusLabel_SM->setText(QString("<nobr>") + | ||||
tr("Message signing failed.") + | tr("Message signing failed.") + | ||||
QString("</nobr>")); | QString("</nobr>")); | ||||
return; | return; | ||||
} | } | ||||
ui->statusLabel_SM->setStyleSheet("QLabel { color: green; }"); | ui->statusLabel_SM->setStyleSheet("QLabel { color: green; }"); | ||||
ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signed.") + | ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signed.") + | ||||
QString("</nobr>")); | QString("</nobr>")); | ||||
ui->signatureOut_SM->setText( | ui->signatureOut_SM->setText(QString::fromStdString(signature)); | ||||
QString::fromStdString(EncodeBase64(vchSig.data(), vchSig.size()))); | |||||
} | } | ||||
void SignVerifyMessageDialog::on_copySignatureButton_SM_clicked() { | void SignVerifyMessageDialog::on_copySignatureButton_SM_clicked() { | ||||
GUIUtil::setClipboard(ui->signatureOut_SM->text()); | GUIUtil::setClipboard(ui->signatureOut_SM->text()); | ||||
} | } | ||||
void SignVerifyMessageDialog::on_clearButton_SM_clicked() { | void SignVerifyMessageDialog::on_clearButton_SM_clicked() { | ||||
ui->addressIn_SM->clear(); | ui->addressIn_SM->clear(); | ||||
Show All 11 Lines | if (model && model->getAddressTableModel()) { | ||||
dlg.setModel(model->getAddressTableModel()); | dlg.setModel(model->getAddressTableModel()); | ||||
if (dlg.exec()) { | if (dlg.exec()) { | ||||
setAddress_VM(dlg.getReturnValue()); | setAddress_VM(dlg.getReturnValue()); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked() { | void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked() { | ||||
CTxDestination destination = DecodeDestination( | const std::string &address = ui->addressIn_VM->text().toStdString(); | ||||
ui->addressIn_VM->text().toStdString(), model->getChainParams()); | const std::string &signature = ui->signatureIn_VM->text().toStdString(); | ||||
if (!IsValidDestination(destination)) { | const std::string &message = | ||||
ui->messageIn_VM->document()->toPlainText().toStdString(); | |||||
const auto result = | |||||
MessageVerify(model->getChainParams(), address, signature, message); | |||||
if (result == MessageVerificationResult::OK) { | |||||
ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }"); | |||||
} else { | |||||
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); | ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); | ||||
} | |||||
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( | ui->statusLabel_VM->setText( | ||||
tr("The entered address is invalid.") + QString(" ") + | tr("The entered address is invalid.") + QString(" ") + | ||||
tr("Please check the address and try again.")); | tr("Please check the address and try again.")); | ||||
return; | return; | ||||
} | case MessageVerificationResult::ERR_ADDRESS_NO_KEY: | ||||
if (!boost::get<PKHash>(&destination)) { | |||||
ui->addressIn_VM->setValid(false); | ui->addressIn_VM->setValid(false); | ||||
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); | |||||
ui->statusLabel_VM->setText( | ui->statusLabel_VM->setText( | ||||
tr("The entered address does not refer to a key.") + QString(" ") + | tr("The entered address does not refer to a key.") + | ||||
tr("Please check the address and try again.")); | QString(" ") + tr("Please check the address and try again.")); | ||||
return; | return; | ||||
} | case MessageVerificationResult::ERR_MALFORMED_SIGNATURE: | ||||
bool fInvalid = false; | |||||
std::vector<uint8_t> vchSig = DecodeBase64( | |||||
ui->signatureIn_VM->text().toStdString().c_str(), &fInvalid); | |||||
if (fInvalid) { | |||||
ui->signatureIn_VM->setValid(false); | ui->signatureIn_VM->setValid(false); | ||||
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); | |||||
ui->statusLabel_VM->setText( | ui->statusLabel_VM->setText( | ||||
tr("The signature could not be decoded.") + QString(" ") + | tr("The signature could not be decoded.") + QString(" ") + | ||||
tr("Please check the signature and try again.")); | tr("Please check the signature and try again.")); | ||||
return; | return; | ||||
} | case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED: | ||||
CHashWriter ss(SER_GETHASH, 0); | |||||
ss << strMessageMagic; | |||||
ss << ui->messageIn_VM->document()->toPlainText().toStdString(); | |||||
CPubKey pubkey; | |||||
if (!pubkey.RecoverCompact(ss.GetHash(), vchSig)) { | |||||
ui->signatureIn_VM->setValid(false); | ui->signatureIn_VM->setValid(false); | ||||
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); | |||||
ui->statusLabel_VM->setText( | ui->statusLabel_VM->setText( | ||||
tr("The signature did not match the message digest.") + | tr("The signature did not match the message digest.") + | ||||
QString(" ") + tr("Please check the signature and try again.")); | QString(" ") + tr("Please check the signature and try again.")); | ||||
return; | return; | ||||
} | case MessageVerificationResult::ERR_NOT_SIGNED: | ||||
if (!(CTxDestination(PKHash(pubkey)) == destination)) { | |||||
ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); | |||||
ui->statusLabel_VM->setText(QString("<nobr>") + | ui->statusLabel_VM->setText(QString("<nobr>") + | ||||
tr("Message verification failed.") + | tr("Message verification failed.") + | ||||
QString("</nobr>")); | QString("</nobr>")); | ||||
return; | 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() { | void SignVerifyMessageDialog::on_clearButton_VM_clicked() { | ||||
ui->addressIn_VM->clear(); | ui->addressIn_VM->clear(); | ||||
ui->signatureIn_VM->clear(); | ui->signatureIn_VM->clear(); | ||||
ui->messageIn_VM->clear(); | ui->messageIn_VM->clear(); | ||||
ui->statusLabel_VM->clear(); | ui->statusLabel_VM->clear(); | ||||
Show All 22 Lines |