Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
signverifymessagedialog.cpp
Go to the documentation of this file.
2 #include "ui_signverifymessagedialog.h"
3 
4 #include "addressbookpage.h"
5 #include "base58.h"
6 #include "guiutil.h"
7 #include "init.h"
8 #include "main.h"
9 #include "optionsmodel.h"
10 #include "walletmodel.h"
11 #include "wallet.h"
12 
13 #include <QClipboard>
14 
15 #include <string>
16 #include <vector>
17 
19  QDialog(parent),
20  ui(new Ui::SignVerifyMessageDialog),
21  model(0)
22 {
23  ui->setupUi(this);
24 
25 #if (QT_VERSION >= 0x040700)
26  /* Do not move this to the XML file, Qt before 4.7 will choke on it */
27  ui->addressIn_SM->setPlaceholderText(tr("Enter a Feathercoin address (e.g. 6nqmPL9tX4Uz3uQhbz8GrgLfXQNQEXstVu)"));
28  ui->signatureOut_SM->setPlaceholderText(tr("Click \"Sign Message\" to generate signature"));
29 
30  ui->addressIn_VM->setPlaceholderText(tr("Enter a Feathercoin address (e.g. 6nqmPL9tX4Uz3uQhbz8GrgLfXQNQEXstVu)"));
31  ui->signatureIn_VM->setPlaceholderText(tr("Enter Feathercoin signature"));
32 #endif
33 
34  GUIUtil::setupAddressWidget(ui->addressIn_SM, this);
35  GUIUtil::setupAddressWidget(ui->addressIn_VM, this);
36 
37  ui->addressIn_SM->installEventFilter(this);
38  ui->messageIn_SM->installEventFilter(this);
39  ui->signatureOut_SM->installEventFilter(this);
40  ui->addressIn_VM->installEventFilter(this);
41  ui->messageIn_VM->installEventFilter(this);
42  ui->signatureIn_VM->installEventFilter(this);
43 
44  ui->signatureOut_SM->setFont(GUIUtil::bitcoinAddressFont());
45  ui->signatureIn_VM->setFont(GUIUtil::bitcoinAddressFont());
46 }
47 
49 {
50  delete ui;
51 }
52 
54 {
55  this->model = model;
56 }
57 
58 void SignVerifyMessageDialog::setAddress_SM(const QString &address)
59 {
60  ui->addressIn_SM->setText(address);
61  ui->messageIn_SM->setFocus();
62 }
63 
64 void SignVerifyMessageDialog::setAddress_VM(const QString &address)
65 {
66  ui->addressIn_VM->setText(address);
67  ui->messageIn_VM->setFocus();
68 }
69 
71 {
72  ui->tabWidget->setCurrentIndex(0);
73 
74  if (fShow)
75  this->show();
76 }
77 
79 {
80  ui->tabWidget->setCurrentIndex(1);
81  if (fShow)
82  this->show();
83 }
84 
86 {
88  {
91  if (dlg.exec())
92  {
94  }
95  }
96 }
97 
99 {
100  setAddress_SM(QApplication::clipboard()->text());
101 }
102 
104 {
105  /* Clear old signature to ensure users don't get confused on error with an old signature displayed */
106  ui->signatureOut_SM->clear();
107 
108  CBitcoinAddress addr(ui->addressIn_SM->text().toStdString());
109  if (!addr.IsValid())
110  {
111  ui->addressIn_SM->setValid(false);
112  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
113  ui->statusLabel_SM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
114  return;
115  }
116  CKeyID keyID;
117  if (!addr.GetKeyID(keyID))
118  {
119  ui->addressIn_SM->setValid(false);
120  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
121  ui->statusLabel_SM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
122  return;
123  }
124 
126  if (!ctx.isValid())
127  {
128  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
129  ui->statusLabel_SM->setText(tr("Wallet unlock was cancelled."));
130  return;
131  }
132 
133  CKey key;
134  if (!pwalletMain->GetKey(keyID, key))
135  {
136  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
137  ui->statusLabel_SM->setText(tr("Private key for the entered address is not available."));
138  return;
139  }
140 
141  CDataStream ss(SER_GETHASH, 0);
142  ss << strMessageMagic;
143  ss << ui->messageIn_SM->document()->toPlainText().toStdString();
144 
145  std::vector<unsigned char> vchSig;
146  if (!key.SignCompact(Hash(ss.begin(), ss.end()), vchSig))
147  {
148  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
149  ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signing failed.") + QString("</nobr>"));
150  return;
151  }
152 
153  ui->statusLabel_SM->setStyleSheet("QLabel { color: green; }");
154  ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signed.") + QString("</nobr>"));
155 
156  ui->signatureOut_SM->setText(QString::fromStdString(EncodeBase64(&vchSig[0], vchSig.size())));
157 }
158 
160 {
161  QApplication::clipboard()->setText(ui->signatureOut_SM->text());
162 }
163 
165 {
166  ui->addressIn_SM->clear();
167  ui->messageIn_SM->clear();
168  ui->signatureOut_SM->clear();
169  ui->statusLabel_SM->clear();
170 
171  ui->addressIn_SM->setFocus();
172 }
173 
175 {
176  if (model && model->getAddressTableModel())
177  {
180  if (dlg.exec())
181  {
183  }
184  }
185 }
186 
188 {
189  CBitcoinAddress addr(ui->addressIn_VM->text().toStdString());
190  if (!addr.IsValid())
191  {
192  ui->addressIn_VM->setValid(false);
193  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
194  ui->statusLabel_VM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
195  return;
196  }
197  CKeyID keyID;
198  if (!addr.GetKeyID(keyID))
199  {
200  ui->addressIn_VM->setValid(false);
201  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
202  ui->statusLabel_VM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
203  return;
204  }
205 
206  bool fInvalid = false;
207  std::vector<unsigned char> vchSig = DecodeBase64(ui->signatureIn_VM->text().toStdString().c_str(), &fInvalid);
208 
209  if (fInvalid)
210  {
211  ui->signatureIn_VM->setValid(false);
212  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
213  ui->statusLabel_VM->setText(tr("The signature could not be decoded.") + QString(" ") + tr("Please check the signature and try again."));
214  return;
215  }
216 
217  CDataStream ss(SER_GETHASH, 0);
218  ss << strMessageMagic;
219  ss << ui->messageIn_VM->document()->toPlainText().toStdString();
220 
221  CPubKey pubkey;
222  if (!pubkey.RecoverCompact(Hash(ss.begin(), ss.end()), vchSig))
223  {
224  ui->signatureIn_VM->setValid(false);
225  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
226  ui->statusLabel_VM->setText(tr("The signature did not match the message digest.") + QString(" ") + tr("Please check the signature and try again."));
227  return;
228  }
229 
230  if (!(CBitcoinAddress(pubkey.GetID()) == addr))
231  {
232  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
233  ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>"));
234  return;
235  }
236 
237  ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }");
238  ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verified.") + QString("</nobr>"));
239 }
240 
242 {
243  ui->addressIn_VM->clear();
244  ui->signatureIn_VM->clear();
245  ui->messageIn_VM->clear();
246  ui->statusLabel_VM->clear();
247 
248  ui->addressIn_VM->setFocus();
249 }
250 
251 bool SignVerifyMessageDialog::eventFilter(QObject *object, QEvent *event)
252 {
253  if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::FocusIn)
254  {
255  if (ui->tabWidget->currentIndex() == 0)
256  {
257  /* Clear status message on focus change */
258  ui->statusLabel_SM->clear();
259 
260  /* Select generated signature */
261  if (object == ui->signatureOut_SM)
262  {
263  ui->signatureOut_SM->selectAll();
264  return true;
265  }
266  }
267  else if (ui->tabWidget->currentIndex() == 1)
268  {
269  /* Clear status message on focus change */
270  ui->statusLabel_VM->clear();
271  }
272  }
273  return QDialog::eventFilter(object, event);
274 }
bool eventFilter(QObject *object, QEvent *event)
const_iterator begin() const
Definition: serialize.h:884
void setAddress_VM(const QString &address)
void setModel(AddressTableModel *model)
UnlockContext requestUnlock()
Definition: aboutdialog.h:6
const QString & getReturnValue() const
Double ended buffer combining vector and stream-like interfaces.
Definition: serialize.h:799
AddressTableModel * getAddressTableModel()
void setupAddressWidget(QLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:68
bool GetKey(const CKeyID &address, CKey &keyOut) const
Definition: keystore.cpp:142
const string strMessageMagic
Definition: main.cpp:78
Ui::SignVerifyMessageDialog * ui
Open address book to pick address for sending.
string EncodeBase64(const unsigned char *pch, size_t len)
Definition: util.cpp:628
SignVerifyMessageDialog(QWidget *parent=0)
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Definition: key.cpp:354
An encapsulated public key.
Definition: key.h:40
CWallet * pwalletMain
Definition: init.cpp:31
uint256 Hash(const T1 pbegin, const T1 pend)
Definition: hash.h:16
Widget that shows a list of sending or receiving addresses.
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Definition: key.cpp:329
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:36
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:24
An encapsulated private key.
Definition: key.h:172
vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
Definition: util.cpp:679
CKeyID GetID() const
Definition: key.h:129
void setModel(WalletModel *model)
QFont bitcoinAddressFont()
Definition: guiutil.cpp:61
const_iterator end() const
Definition: serialize.h:886
void setAddress_SM(const QString &address)