лишний #include
[novacoin.git] / src / qt / signverifymessagedialog.cpp
1 #include "signverifymessagedialog.h"
2 #include "ui_signverifymessagedialog.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
19 SignVerifyMessageDialog::SignVerifyMessageDialog(QWidget *parent) :
20     QDialog(parent, DIALOGWINDOWHINTS),
21     ui(new Ui::SignVerifyMessageDialog),
22     model(0)
23 {
24     ui->setupUi(this);
25
26 #if (QT_VERSION >= 0x040700)
27     /* Do not move this to the XML file, Qt before 4.7 will choke on it */
28     ui->addressIn_SM->setPlaceholderText(tr("Enter a NovaCoin address (e.g. 4Zo1ga6xuKuQ7JV7M9rGDoxdbYwV5zgQJ5)"));
29     ui->signatureOut_SM->setPlaceholderText(tr("Click \"Sign Message\" to generate signature"));
30
31     ui->addressIn_VM->setPlaceholderText(tr("Enter a NovaCoin address (e.g. 4Zo1ga6xuKuQ7JV7M9rGDoxdbYwV5zgQJ5)"));
32     ui->signatureIn_VM->setPlaceholderText(tr("Enter NovaCoin signature"));
33 #endif
34
35     GUIUtil::setupAddressWidget(ui->addressIn_SM, this);
36     GUIUtil::setupAddressWidget(ui->addressIn_VM, this);
37
38     ui->addressIn_SM->installEventFilter(this);
39     ui->messageIn_SM->installEventFilter(this);
40     ui->signatureOut_SM->installEventFilter(this);
41     ui->addressIn_VM->installEventFilter(this);
42     ui->messageIn_VM->installEventFilter(this);
43     ui->signatureIn_VM->installEventFilter(this);
44
45     ui->signatureOut_SM->setFont(GUIUtil::bitcoinAddressFont());
46     ui->signatureIn_VM->setFont(GUIUtil::bitcoinAddressFont());
47 }
48
49 SignVerifyMessageDialog::~SignVerifyMessageDialog()
50 {
51     delete ui;
52 }
53
54 void SignVerifyMessageDialog::setModel(WalletModel *model)
55 {
56     this->model = model;
57 }
58
59 void SignVerifyMessageDialog::setAddress_SM(QString address)
60 {
61     ui->addressIn_SM->setText(address);
62     ui->messageIn_SM->setFocus();
63 }
64
65 void SignVerifyMessageDialog::setAddress_VM(QString address)
66 {
67     ui->addressIn_VM->setText(address);
68     ui->messageIn_VM->setFocus();
69 }
70
71 void SignVerifyMessageDialog::showTab_SM(bool fShow)
72 {
73     ui->tabWidget->setCurrentIndex(0);
74
75     if (fShow)
76         this->show();
77 }
78
79 void SignVerifyMessageDialog::showTab_VM(bool fShow)
80 {
81     ui->tabWidget->setCurrentIndex(1);
82     if (fShow)
83         this->show();
84 }
85
86 void SignVerifyMessageDialog::on_addressBookButton_SM_clicked()
87 {
88     if (model && model->getAddressTableModel())
89     {
90         AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::ReceivingTab, this);
91         dlg.setModel(model->getAddressTableModel());
92         if (dlg.exec())
93         {
94             setAddress_SM(dlg.getReturnValue());
95         }
96     }
97 }
98
99 void SignVerifyMessageDialog::on_pasteButton_SM_clicked()
100 {
101     setAddress_SM(QApplication::clipboard()->text());
102 }
103
104 void SignVerifyMessageDialog::on_signMessageButton_SM_clicked()
105 {
106     /* Clear old signature to ensure users don't get confused on error with an old signature displayed */
107     ui->signatureOut_SM->clear();
108
109     CBitcoinAddress addr(ui->addressIn_SM->text().toStdString());
110     if (!addr.IsValid())
111     {
112         ui->addressIn_SM->setValid(false);
113         ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
114         ui->statusLabel_SM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
115         return;
116     }
117     CKeyID keyID;
118     if (!addr.GetKeyID(keyID))
119     {
120         ui->addressIn_SM->setValid(false);
121         ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
122         ui->statusLabel_SM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
123         return;
124     }
125
126     WalletModel::UnlockContext ctx(model->requestUnlock());
127     if (!ctx.isValid())
128     {
129         ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
130         ui->statusLabel_SM->setText(tr("Wallet unlock was cancelled."));
131         return;
132     }
133
134     CKey key;
135     if (!pwalletMain->GetKey(keyID, key))
136     {
137         ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
138         ui->statusLabel_SM->setText(tr("Private key for the entered address is not available."));
139         return;
140     }
141
142     CDataStream ss(SER_GETHASH, 0);
143     ss << strMessageMagic;
144     ss << ui->messageIn_SM->document()->toPlainText().toStdString();
145
146     std::vector<unsigned char> vchSig;
147     if (!key.SignCompact(Hash(ss.begin(), ss.end()), vchSig))
148     {
149         ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
150         ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signing failed.") + QString("</nobr>"));
151         return;
152     }
153
154     ui->statusLabel_SM->setStyleSheet("QLabel { color: green; }");
155     ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signed.") + QString("</nobr>"));
156
157     ui->signatureOut_SM->setText(QString::fromStdString(EncodeBase64(&vchSig[0], vchSig.size())));
158 }
159
160 void SignVerifyMessageDialog::on_copySignatureButton_SM_clicked()
161 {
162     QApplication::clipboard()->setText(ui->signatureOut_SM->text());
163 }
164
165 void SignVerifyMessageDialog::on_clearButton_SM_clicked()
166 {
167     ui->addressIn_SM->clear();
168     ui->messageIn_SM->clear();
169     ui->signatureOut_SM->clear();
170     ui->statusLabel_SM->clear();
171
172     ui->addressIn_SM->setFocus();
173 }
174
175 void SignVerifyMessageDialog::on_addressBookButton_VM_clicked()
176 {
177     if (model && model->getAddressTableModel())
178     {
179         AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this);
180         dlg.setModel(model->getAddressTableModel());
181         if (dlg.exec())
182         {
183             setAddress_VM(dlg.getReturnValue());
184         }
185     }
186 }
187
188 void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked()
189 {
190     CBitcoinAddress addr(ui->addressIn_VM->text().toStdString());
191     if (!addr.IsValid())
192     {
193         ui->addressIn_VM->setValid(false);
194         ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
195         ui->statusLabel_VM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
196         return;
197     }
198     CKeyID keyID;
199     if (!addr.GetKeyID(keyID))
200     {
201         ui->addressIn_VM->setValid(false);
202         ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
203         ui->statusLabel_VM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
204         return;
205     }
206
207     bool fInvalid = false;
208     std::vector<unsigned char> vchSig = DecodeBase64(ui->signatureIn_VM->text().toStdString().c_str(), &fInvalid);
209
210     if (fInvalid)
211     {
212         ui->signatureIn_VM->setValid(false);
213         ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
214         ui->statusLabel_VM->setText(tr("The signature could not be decoded.") + QString(" ") + tr("Please check the signature and try again."));
215         return;
216     }
217
218     CDataStream ss(SER_GETHASH, 0);
219     ss << strMessageMagic;
220     ss << ui->messageIn_VM->document()->toPlainText().toStdString();
221
222     CKey key;
223     if (!key.SetCompactSignature(Hash(ss.begin(), ss.end()), vchSig))
224     {
225         ui->signatureIn_VM->setValid(false);
226         ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
227         ui->statusLabel_VM->setText(tr("The signature did not match the message digest.") + QString(" ") + tr("Please check the signature and try again."));
228         return;
229     }
230
231     if (!(CBitcoinAddress(key.GetPubKey().GetID()) == addr))
232     {
233         ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
234         ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>"));
235         return;
236     }
237
238     ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }");
239     ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verified.") + QString("</nobr>"));
240 }
241
242 void SignVerifyMessageDialog::on_clearButton_VM_clicked()
243 {
244     ui->addressIn_VM->clear();
245     ui->signatureIn_VM->clear();
246     ui->messageIn_VM->clear();
247     ui->statusLabel_VM->clear();
248
249     ui->addressIn_VM->setFocus();
250 }
251
252 bool SignVerifyMessageDialog::eventFilter(QObject *object, QEvent *event)
253 {
254     if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::FocusIn)
255     {
256         if (ui->tabWidget->currentIndex() == 0)
257         {
258             /* Clear status message on focus change */
259             ui->statusLabel_SM->clear();
260
261             /* Select generated signature */
262             if (object == ui->signatureOut_SM)
263             {
264                 ui->signatureOut_SM->selectAll();
265                 return true;
266             }
267         }
268         else if (ui->tabWidget->currentIndex() == 1)
269         {
270             /* Clear status message on focus change */
271             ui->statusLabel_VM->clear();
272         }
273     }
274     return QDialog::eventFilter(object, event);
275 }