Anoncoin  0.9.4
P2P Digital Currency
signverifymessagedialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
8 
9 #include "addressbookpage.h"
10 #include "guiutil.h"
11 #include "walletmodel.h"
12 
13 #include "base58.h"
14 #include "init.h"
15 #include "wallet.h"
16 
17 #include <string>
18 #include <vector>
19 
20 #include <QClipboard>
21 
23  QDialog(parent),
24  ui(new Ui::SignVerifyMessageDialog),
25  model(0)
26 {
27  ui->setupUi(this);
28 
29 #if QT_VERSION >= 0x040700
30  ui->signatureOut_SM->setPlaceholderText(tr("Click \"Sign Message\" to generate signature"));
31  // ToDo: ...make the e.g. look right
32  ui->addressIn_VM->setPlaceholderText(tr("Enter an Anoncoin address (e.g. 1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L)"));
33 #endif
34 
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 
47 }
48 
50 {
51  delete ui;
52 }
53 
55 {
56  this->model = model;
57 }
58 
59 void SignVerifyMessageDialog::setAddress_SM(const QString &address)
60 {
61  ui->addressIn_SM->setText(address);
62  ui->messageIn_SM->setFocus();
63 }
64 
65 void SignVerifyMessageDialog::setAddress_VM(const QString &address)
66 {
67  ui->addressIn_VM->setText(address);
68  ui->messageIn_VM->setFocus();
69 }
70 
72 {
73  ui->tabWidget->setCurrentIndex(0);
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  if (!model)
106  return;
107 
108  /* Clear old signature to ensure users don't get confused on error with an old signature displayed */
109  ui->signatureOut_SM->clear();
110 
111  CAnoncoinAddress addr(ui->addressIn_SM->text().toStdString());
112  if (!addr.IsValid())
113  {
114  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
115  ui->statusLabel_SM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
116  return;
117  }
118  CKeyID keyID;
119  if (!addr.GetKeyID(keyID))
120  {
121  ui->addressIn_SM->setValid(false);
122  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
123  ui->statusLabel_SM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
124  return;
125  }
126 
128  if (!ctx.isValid())
129  {
130  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
131  ui->statusLabel_SM->setText(tr("Wallet unlock was cancelled."));
132  return;
133  }
134 
135  CKey key;
136  if (!pwalletMain->GetKey(keyID, key))
137  {
138  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
139  ui->statusLabel_SM->setText(tr("Private key for the entered address is not available."));
140  return;
141  }
142 
143  CDataStream ss(SER_GETHASH, 0);
144  ss << strMessageMagic;
145  ss << ui->messageIn_SM->document()->toPlainText().toStdString();
146 
147  std::vector<unsigned char> vchSig;
148  if (!key.SignCompact(Hash(ss.begin(), ss.end()), vchSig))
149  {
150  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
151  ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signing failed.") + QString("</nobr>"));
152  return;
153  }
154 
155  ui->statusLabel_SM->setStyleSheet("QLabel { color: green; }");
156  ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signed.") + QString("</nobr>"));
157 
158  ui->signatureOut_SM->setText(QString::fromStdString(EncodeBase64(&vchSig[0], vchSig.size())));
159 }
160 
162 {
164 }
165 
167 {
168  ui->addressIn_SM->clear();
169  ui->messageIn_SM->clear();
170  ui->signatureOut_SM->clear();
171  ui->statusLabel_SM->clear();
172 
173  ui->addressIn_SM->setFocus();
174 }
175 
177 {
178  if (model && model->getAddressTableModel())
179  {
182  if (dlg.exec())
183  {
185  }
186  }
187 }
188 
190 {
191  CAnoncoinAddress addr(ui->addressIn_VM->text().toStdString());
192  if (!addr.IsValid())
193  {
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  CPubKey pubkey;
223  if (!pubkey.RecoverCompact(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 (!(CAnoncoinAddress(pubkey.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 
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 }
bool eventFilter(QObject *object, QEvent *event)
const_iterator begin() const
Definition: serialize.h:933
void setAddress_VM(const QString &address)
void setModel(AddressTableModel *model)
void setupUi(QDialog *SignVerifyMessageDialog)
UnlockContext requestUnlock()
const QString & getReturnValue() const
Double ended buffer combining vector and stream-like interfaces.
Definition: serialize.h:848
Open address book to pick address.
AddressTableModel * getAddressTableModel()
bool GetKey(const CKeyID &address, CKey &keyOut) const
Definition: crypter.cpp:212
const string strMessageMagic
Definition: main.cpp:77
Ui::SignVerifyMessageDialog * ui
string EncodeBase64(const unsigned char *pch, size_t len)
Definition: util.cpp:548
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:96
void setClipboard(const QString &str)
Definition: guiutil.cpp:761
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Definition: key.cpp:457
An encapsulated public key.
Definition: key.h:43
base58-encoded Anoncoin addresses.
Definition: base58.h:102
uint256 Hash(const T1 pbegin, const T1 pend)
Definition: hash.h:20
Widget that shows a list of sending or receiving addresses.
QFont anoncoinAddressFont()
Definition: guiutil.cpp:85
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Definition: key.cpp:414
Interface to Anoncoin wallet from Qt view code.
Definition: walletmodel.h:97
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:27
SignVerifyMessageDialog(QWidget *parent)
An encapsulated private key.
Definition: key.h:180
vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
Definition: util.cpp:599
CKeyID GetID() const
Definition: key.h:132
void setModel(WalletModel *model)
void setValid(bool valid)
CWallet * pwalletMain
const_iterator end() const
Definition: serialize.h:935
void setAddress_SM(const QString &address)