aboutsummaryrefslogtreecommitdiff
path: root/src/qt/verifymessagedialog.cpp
blob: 92f58328a47569bb5cb2e79dacc98ba1035f9573 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "verifymessagedialog.h"
#include "ui_verifymessagedialog.h"

#include <string>
#include <vector>

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QPushButton>

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

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

#if (QT_VERSION >= 0x040700)
    /* Do not move this to the XML file, Qt before 4.7 will choke on it */
    ui->lnAddress->setPlaceholderText(tr("Enter a Bitcoin address (e.g. 1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L)"));
    ui->lnSig->setPlaceholderText(tr("Enter Bitcoin signature"));
#endif

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

    ui->lnSig->setFont(GUIUtil::bitcoinAddressFont());

    ui->lnAddress->setFocus();
}

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

void VerifyMessageDialog::on_verifyMessage_clicked()
{
    CBitcoinAddress addr(ui->lnAddress->text().toStdString());
    if (!addr.IsValid())
    {
        ui->lnAddress->setValid(false);
        ui->lblStatus->setStyleSheet("QLabel { color: red; }");
        ui->lblStatus->setText(tr("\"%1\" is not a valid address.").arg(ui->lnAddress->text()) + QString(" ") + tr("Please check the address and try again."));
        return;
    }
    CKeyID keyID;
    if (!addr.GetKeyID(keyID))
    {
        ui->lnAddress->setValid(false);
        ui->lblStatus->setStyleSheet("QLabel { color: red; }");
        ui->lblStatus->setText(tr("\"%1\" does not refer to a key.").arg(ui->lnAddress->text()) + QString(" ") + tr("Please check the address and try again."));
        return;
    }

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

    if (fInvalid)
    {
        ui->lnSig->setValid(false);
        ui->lblStatus->setStyleSheet("QLabel { color: red; }");
        ui->lblStatus->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->edMessage->document()->toPlainText().toStdString();

    CKey key;
    if (!key.SetCompactSignature(Hash(ss.begin(), ss.end()), vchSig))
    {
        ui->lnSig->setValid(false);
        ui->lblStatus->setStyleSheet("QLabel { color: red; }");
        ui->lblStatus->setText(tr("The signature did not match the message digest.")+ QString(" ") + tr("Please check the signature and try again."));
        return;
    }

    if (!(CBitcoinAddress(key.GetPubKey().GetID()) == addr))
    {
        ui->lblStatus->setStyleSheet("QLabel { color: red; }");
        ui->lblStatus->setText(QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>"));
        return;
    }

    ui->lblStatus->setStyleSheet("QLabel { color: green; }");
    ui->lblStatus->setText(QString("<nobr>") + tr("Message verified.") + QString("</nobr>"));
}

void VerifyMessageDialog::on_clearButton_clicked()
{
    ui->lnAddress->clear();
    ui->lnSig->clear();
    ui->edMessage->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))
    {
        // set lnAddress to valid, as QEvent::FocusIn would not reach QValidatedLineEdit::focusInEvent
        ui->lnAddress->setValid(true);
        ui->lnAddress->selectAll();
        return true;
    }
    return QDialog::eventFilter(object, event);
}