Update CMakeLists.txt - play with openssl
[novacoin.git] / src / qt / secondauthdialog.cpp
1 #include "secondauthdialog.h"
2 #include "ui_secondauthdialog.h"
3
4 #include "addressbookpage.h"
5 #include "base58.h"
6 #include "guiutil.h"
7 #include "dialogwindowflags.h"
8 #include "init.h"
9 #include "main.h"
10 #include "optionsmodel.h"
11 #include "walletmodel.h"
12 #include "wallet.h"
13
14 #include <string>
15 #include <vector>
16
17 #include <QClipboard>
18 #include <QKeyEvent>
19
20 SecondAuthDialog::SecondAuthDialog(QWidget *parent) :
21     QWidget(parent, DIALOGWINDOWHINTS),
22     ui(new Ui::SecondAuthDialog),
23     model(0)
24 {
25     ui->setupUi(this);
26
27 #if (QT_VERSION >= 0x040700)
28     /* Do not move this to the XML file, Qt before 4.7 will choke on it */
29     ui->addressIn->setPlaceholderText(tr("Enter a NovaCoin address (e.g. 4Zo1ga6xuKuQ7JV7M9rGDoxdbYwV5zgQJ5)"));
30     ui->signatureOut->setPlaceholderText(tr("Click \"Sign data\" to generate signature"));
31 #endif
32
33     GUIUtil::setupAddressWidget(ui->addressIn, this);
34
35     ui->addressIn->installEventFilter(this);
36     ui->messageIn->installEventFilter(this);
37     ui->signatureOut->installEventFilter(this);
38
39     ui->signatureOut->setFont(GUIUtil::bitcoinAddressFont());
40 }
41
42 SecondAuthDialog::~SecondAuthDialog()
43 {
44     delete ui;
45 }
46
47 void SecondAuthDialog::setModel(WalletModel *model)
48 {
49     this->model = model;
50 }
51
52 void SecondAuthDialog::on_addressBookButton_clicked()
53 {
54     if (model && model->getAddressTableModel())
55     {
56         AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::ReceivingTab, this);
57         dlg.setModel(model->getAddressTableModel());
58         if (dlg.exec())
59         {
60             ui->addressIn->setText(dlg.getReturnValue());
61         }
62     }
63 }
64
65 void SecondAuthDialog::on_pasteButton_clicked()
66 {
67     ui->messageIn->setText(QApplication::clipboard()->text());
68 }
69
70 void SecondAuthDialog::on_signMessageButton_clicked()
71 {
72     /* Clear old signature to ensure users don't get confused on error with an old signature displayed */
73     ui->signatureOut->clear();
74
75     CBitcoinAddress addr(ui->addressIn->text().toStdString());
76     if (!addr.IsValid())
77     {
78         ui->addressIn->setValid(false);
79         ui->statusLabel->setStyleSheet("QLabel { color: red; }");
80         ui->statusLabel->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
81         return;
82     }
83
84     CKeyID keyID;
85     if (!addr.GetKeyID(keyID))
86     {
87         ui->addressIn->setValid(false);
88         ui->statusLabel->setStyleSheet("QLabel { color: red; }");
89         ui->statusLabel->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
90         return;
91     }
92
93     WalletModel::UnlockContext ctx(model->requestUnlock());
94     if (!ctx.isValid())
95     {
96         ui->statusLabel->setStyleSheet("QLabel { color: red; }");
97         ui->statusLabel->setText(tr("Wallet unlock was cancelled."));
98         return;
99     }
100
101     CKey key;
102     if (!pwalletMain->GetKey(keyID, key))
103     {
104         ui->statusLabel->setStyleSheet("QLabel { color: red; }");
105         ui->statusLabel->setText(tr("Private key for the entered address is not available."));
106         return;
107     }
108
109     uint256 hash;
110     hash.SetHex(ui->messageIn->text().toStdString());
111     CTransaction tx;
112     uint256 hashBlock = 0;
113     if (!GetTransaction(hash, tx, hashBlock) || !hashBlock) {
114         ui->statusLabel->setStyleSheet("QLabel { color: red; }");
115         ui->statusLabel->setText(tr("No information available about transaction."));
116         return;
117     }
118
119     CDataStream ss(SER_GETHASH, 0);
120     ss << strMessageMagic;
121     ss << ui->messageIn->text().toStdString();
122
123     std::vector<unsigned char> vchSig;
124     if (!key.SignCompact(Hash(ss.begin(), ss.end()), vchSig))
125     {
126         ui->statusLabel->setStyleSheet("QLabel { color: red; }");
127         ui->statusLabel->setText(QString("<nobr>") + tr("Message signing failed.") + QString("</nobr>"));
128         return;
129     }
130
131     ui->statusLabel->setStyleSheet("QLabel { color: green; }");
132     ui->statusLabel->setText(QString("<nobr>") + tr("Message signed.") + QString("</nobr>"));
133
134     ui->signatureOut->setText(QString::fromStdString(EncodeBase64(&vchSig[0], vchSig.size())));
135 }
136
137 void SecondAuthDialog::on_copySignatureButton_clicked()
138 {
139     QApplication::clipboard()->setText(ui->signatureOut->text());
140 }
141
142 void SecondAuthDialog::on_clearButton_clicked()
143 {
144     ui->addressIn->clear();
145     ui->messageIn->clear();
146     ui->signatureOut->clear();
147     ui->statusLabel->clear();
148
149     ui->addressIn->setFocus();
150 }
151
152 bool SecondAuthDialog::eventFilter(QObject *object, QEvent *event)
153 {
154     return QWidget::eventFilter(object, event);
155 }
156
157 void SecondAuthDialog::keyPressEvent(QKeyEvent *event)
158 {
159 #ifdef ANDROID
160     if(event->key() == Qt::Key_Back)
161     {
162         close();
163     }
164 #else
165     if(event->key() == Qt::Key_Escape)
166     {
167         close();
168     }
169 #endif
170 }