aboutsummaryrefslogtreecommitdiff
path: root/src/qt/verifymessagedialog.cpp
blob: 0bac24820c62e4ebf2b76adf2f573231c51d1f69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "verifymessagedialog.h"
#include "ui_verifymessagedialog.h"

#include <string>
#include <vector>

#include <QDialogButtonBox>
#include <QAbstractButton>
#include <QClipboard>
#include <QMessageBox>

#include "main.h"
#include "wallet.h"
#include "walletmodel.h"
#include "addresstablemodel.h"
#include "guiutil.h"

VerifyMessageDialog::VerifyMessageDialog(AddressTableModel *addressModel, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::VerifyMessageDialog),
    model(addressModel)
{
    ui->setupUi(this);

#if (QT_VERSION >= 0x040700)
    /* Do not move this to the XML file, Qt before 4.7 will choke on it */
    ui->lnSig->setPlaceholderText(tr("Enter Bitcoin signature"));
    ui->lnAddress->setPlaceholderText(tr("Click \"Verify Message\" to obtain address"));
#endif

    GUIUtil::setupAddressWidget(ui->lnAddress, this);
    ui->lnAddress->installEventFilter(this);

    ui->edMessage->setFocus();
}

VerifyMessageDialog::~VerifyMessageDialog()
{
    delete ui;
}

bool VerifyMessageDialog::checkAddress()
{
    CDataStream ss(SER_GETHASH, 0);
    ss << strMessageMagic;
    ss << ui->edMessage->document()->toPlainText().toStdString();
    uint256 hash = Hash(ss.begin(), ss.end());

    bool invalid = true;
    std::vector<unsigned char> vchSig = DecodeBase64(ui->lnSig->text().toStdString().c_str(), &invalid);

    if(invalid)
    {
        QMessageBox::warning(this, tr("Invalid Signature"), tr("The signature could not be decoded. Please check the signature and try again."));
        return false;
    }

    CKey key;
    if(!key.SetCompactSignature(hash, vchSig))
    {
        QMessageBox::warning(this, tr("Invalid Signature"), tr("The signature did not match the message digest. Please check the signature and try again."));
        return false;
    }

    CBitcoinAddress address(key.GetPubKey());
    QString qStringAddress = QString::fromStdString(address.ToString());
    ui->lnAddress->setText(qStringAddress);
    ui->copyToClipboard->setEnabled(true);

    QString label = model->labelForAddress(qStringAddress);
    ui->lblStatus->setText(label.isEmpty() ? tr("Address not found in address book.") : tr("Address found in address book: %1").arg(label));
    return true;
}

void VerifyMessageDialog::on_verifyMessage_clicked()
{
    checkAddress();
}

void VerifyMessageDialog::on_copyToClipboard_clicked()
{
    QApplication::clipboard()->setText(ui->lnAddress->text());
}

void VerifyMessageDialog::on_clearButton_clicked()
{
    ui->edMessage->clear();
    ui->lnSig->clear();
    ui->lnAddress->clear();
    ui->lblStatus->clear();

    ui->edMessage->setFocus();
}

bool VerifyMessageDialog::eventFilter(QObject *object, QEvent *event)
{
    if(object == ui->lnAddress && (event->type() == QEvent::MouseButtonPress ||
                                   event->type() == QEvent::FocusIn))
    {
        ui->lnAddress->selectAll();
        return true;
    }
    return QDialog::eventFilter(object, event);
}