Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
signverifymessagedialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
7 
8 #include "addressbookpage.h"
9 #include "guiutil.h"
10 #include "walletmodel.h"
11 
12 #include "base58.h"
13 #include "init.h"
14 #include "wallet.h"
15 
16 #include <string>
17 #include <vector>
18 
19 #include <QClipboard>
20 
22  QDialog(parent),
23  ui(new Ui::SignVerifyMessageDialog),
24  model(0)
25 {
26  ui->setupUi(this);
27 
28 #if QT_VERSION >= 0x040700
29  ui->signatureOut_SM->setPlaceholderText(tr("Click \"Sign Message\" to generate signature"));
30  ui->addressIn_VM->setPlaceholderText(tr("Enter a Bitcoin address (e.g. 1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L)"));
31 #endif
32 
35 
36  ui->addressIn_SM->installEventFilter(this);
37  ui->messageIn_SM->installEventFilter(this);
38  ui->signatureOut_SM->installEventFilter(this);
39  ui->addressIn_VM->installEventFilter(this);
40  ui->messageIn_VM->installEventFilter(this);
41  ui->signatureIn_VM->installEventFilter(this);
42 
45 }
46 
48 {
49  delete ui;
50 }
51 
53 {
54  this->model = model;
55 }
56 
57 void SignVerifyMessageDialog::setAddress_SM(const QString &address)
58 {
59  ui->addressIn_SM->setText(address);
60  ui->messageIn_SM->setFocus();
61 }
62 
63 void SignVerifyMessageDialog::setAddress_VM(const QString &address)
64 {
65  ui->addressIn_VM->setText(address);
66  ui->messageIn_VM->setFocus();
67 }
68 
70 {
71  ui->tabWidget->setCurrentIndex(0);
72  if (fShow)
73  this->show();
74 }
75 
77 {
78  ui->tabWidget->setCurrentIndex(1);
79  if (fShow)
80  this->show();
81 }
82 
84 {
86  {
89  if (dlg.exec())
90  {
92  }
93  }
94 }
95 
97 {
98  setAddress_SM(QApplication::clipboard()->text());
99 }
100 
102 {
103  if (!model)
104  return;
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->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 {
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->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
193  ui->statusLabel_VM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
194  return;
195  }
196  CKeyID keyID;
197  if (!addr.GetKeyID(keyID))
198  {
199  ui->addressIn_VM->setValid(false);
200  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
201  ui->statusLabel_VM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
202  return;
203  }
204 
205  bool fInvalid = false;
206  std::vector<unsigned char> vchSig = DecodeBase64(ui->signatureIn_VM->text().toStdString().c_str(), &fInvalid);
207 
208  if (fInvalid)
209  {
210  ui->signatureIn_VM->setValid(false);
211  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
212  ui->statusLabel_VM->setText(tr("The signature could not be decoded.") + QString(" ") + tr("Please check the signature and try again."));
213  return;
214  }
215 
216  CDataStream ss(SER_GETHASH, 0);
217  ss << strMessageMagic;
218  ss << ui->messageIn_VM->document()->toPlainText().toStdString();
219 
220  CPubKey pubkey;
221  if (!pubkey.RecoverCompact(Hash(ss.begin(), ss.end()), vchSig))
222  {
223  ui->signatureIn_VM->setValid(false);
224  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
225  ui->statusLabel_VM->setText(tr("The signature did not match the message digest.") + QString(" ") + tr("Please check the signature and try again."));
226  return;
227  }
228 
229  if (!(CBitcoinAddress(pubkey.GetID()) == addr))
230  {
231  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
232  ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>"));
233  return;
234  }
235 
236  ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }");
237  ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verified.") + QString("</nobr>"));
238 }
239 
241 {
242  ui->addressIn_VM->clear();
243  ui->signatureIn_VM->clear();
244  ui->messageIn_VM->clear();
245  ui->statusLabel_VM->clear();
246 
247  ui->addressIn_VM->setFocus();
248 }
249 
250 bool SignVerifyMessageDialog::eventFilter(QObject *object, QEvent *event)
251 {
252  if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::FocusIn)
253  {
254  if (ui->tabWidget->currentIndex() == 0)
255  {
256  /* Clear status message on focus change */
257  ui->statusLabel_SM->clear();
258 
259  /* Select generated signature */
260  if (object == ui->signatureOut_SM)
261  {
262  ui->signatureOut_SM->selectAll();
263  return true;
264  }
265  }
266  else if (ui->tabWidget->currentIndex() == 1)
267  {
268  /* Clear status message on focus change */
269  ui->statusLabel_VM->clear();
270  }
271  }
272  return QDialog::eventFilter(object, event);
273 }
bool eventFilter(QObject *object, QEvent *event)
const_iterator begin() const
Definition: serialize.h:924
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:839
base58-encoded Bitcoin addresses.
Definition: base58.h:101
Open address book to pick address.
AddressTableModel * getAddressTableModel()
bool GetKey(const CKeyID &address, CKey &keyOut) const
Definition: crypter.cpp:211
const string strMessageMagic
Definition: main.cpp:82
Ui::SignVerifyMessageDialog * ui
string EncodeBase64(const unsigned char *pch, size_t len)
Definition: util.cpp:547
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:94
void setClipboard(const QString &str)
Definition: guiutil.cpp:755
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Definition: key.cpp:448
An encapsulated public key.
Definition: key.h:42
uint256 Hash(const T1 pbegin, const T1 pend)
Definition: hash.h:19
Widget that shows a list of sending or receiving addresses.
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Definition: key.cpp:405
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:96
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:26
SignVerifyMessageDialog(QWidget *parent)
An encapsulated private key.
Definition: key.h:179
vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
Definition: util.cpp:598
CKeyID GetID() const
Definition: key.h:131
void setModel(WalletModel *model)
void setValid(bool valid)
CWallet * pwalletMain
QFont bitcoinAddressFont()
Definition: guiutil.cpp:83
const_iterator end() const
Definition: serialize.h:926
void setAddress_SM(const QString &address)