Fabcoin Core  0.16.2
P2P Digital Currency
signverifymessagedialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2017 The Bitcoin Core developers
2 // Distributed under the MIT 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 <platformstyle.h>
11 #include <walletmodel.h>
12 
13 #include <base58.h>
14 #include <init.h>
15 #include <validation.h> // For strMessageMagic
16 #include <wallet/wallet.h>
17 #include <styleSheet.h>
18 
19 #include <string>
20 #include <vector>
21 
22 #include <QClipboard>
23 
24 SignVerifyMessageDialog::SignVerifyMessageDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
25  QDialog(parent),
26  ui(new Ui::SignVerifyMessageDialog),
27  model(0),
28  platformStyle(_platformStyle)
29 {
30  ui->setupUi(this);
31 
36  ui->clearButton_SM->setIcon(platformStyle->MultiStatesIcon(":/icons/remove", PlatformStyle::PushButton, 0x5a5a5d));
39  ui->clearButton_VM->setIcon(platformStyle->MultiStatesIcon(":/icons/remove", PlatformStyle::PushButton, 0x5a5a5d));
40 
41  SetObjectStyleSheet(ui->clearButton_SM, StyleSheetNames::ButtonWhite);
42  SetObjectStyleSheet(ui->clearButton_VM, StyleSheetNames::ButtonWhite);
43  SetObjectStyleSheet(ui->signMessageButton_SM, StyleSheetNames::ButtonBlue);
44  SetObjectStyleSheet(ui->verifyMessageButton_VM, StyleSheetNames::ButtonBlue);
45  SetObjectStyleSheet(ui->addressBookButton_SM, StyleSheetNames::ButtonTransparent);
46  SetObjectStyleSheet(ui->pasteButton_SM, StyleSheetNames::ButtonTransparent);
47  SetObjectStyleSheet(ui->copySignatureButton_SM, StyleSheetNames::ButtonTransparent);
48  SetObjectStyleSheet(ui->addressBookButton_VM, StyleSheetNames::ButtonTransparent);
49 
50 #if QT_VERSION >= 0x040700
51  ui->signatureOut_SM->setPlaceholderText(tr("Click \"Sign Message\" to generate signature"));
52 #endif
53 
56 
57  ui->addressIn_SM->installEventFilter(this);
58  ui->messageIn_SM->installEventFilter(this);
59  ui->signatureOut_SM->installEventFilter(this);
60  ui->addressIn_VM->installEventFilter(this);
61  ui->messageIn_VM->installEventFilter(this);
62  ui->signatureIn_VM->installEventFilter(this);
63 
66 }
67 
69 {
70  delete ui;
71 }
72 
74 {
75  this->model = _model;
76 }
77 
79 {
80  ui->addressIn_SM->setText(address);
81  ui->messageIn_SM->setFocus();
82 }
83 
85 {
86  ui->addressIn_VM->setText(address);
87  ui->messageIn_VM->setFocus();
88 }
89 
91 {
92  ui->tabWidget->setCurrentIndex(0);
93  if (fShow)
94  this->show();
95 }
96 
98 {
99  ui->tabWidget->setCurrentIndex(1);
100  if (fShow)
101  this->show();
102 }
103 
105 {
106  if (model && model->getAddressTableModel())
107  {
110  if (dlg.exec())
111  {
113  }
114  }
115 }
116 
118 {
119  setAddress_SM(QApplication::clipboard()->text());
120 }
121 
123 {
124  if (!model)
125  return;
126 
127  /* Clear old signature to ensure users don't get confused on error with an old signature displayed */
128  ui->signatureOut_SM->clear();
129 
130  CFabcoinAddress addr(ui->addressIn_SM->text().toStdString());
131  if (!addr.IsValid())
132  {
133  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
134  ui->statusLabel_SM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
135  return;
136  }
137  CKeyID keyID;
138  if (!addr.GetKeyID(keyID))
139  {
140  ui->addressIn_SM->setValid(false);
141  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
142  ui->statusLabel_SM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
143  return;
144  }
145 
147  if (!ctx.isValid())
148  {
149  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
150  ui->statusLabel_SM->setText(tr("Wallet unlock was cancelled."));
151  return;
152  }
153 
154  CKey key;
155  if (!model->getPrivKey(keyID, key))
156  {
157  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
158  ui->statusLabel_SM->setText(tr("Private key for the entered address is not available."));
159  return;
160  }
161 
162  CHashWriter ss(SER_GETHASH, 0);
163  ss << strMessageMagic;
164  ss << ui->messageIn_SM->document()->toPlainText().toStdString();
165 
166  std::vector<unsigned char> vchSig;
167  if (!key.SignCompact(ss.GetHash(), vchSig))
168  {
169  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
170  ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signing failed.") + QString("</nobr>"));
171  return;
172  }
173 
174  ui->statusLabel_SM->setStyleSheet("QLabel { color: green; }");
175  ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signed.") + QString("</nobr>"));
176 
177  ui->signatureOut_SM->setText(QString::fromStdString(EncodeBase64(&vchSig[0], vchSig.size())));
178 }
179 
181 {
183 }
184 
186 {
187  ui->addressIn_SM->clear();
188  ui->messageIn_SM->clear();
189  ui->signatureOut_SM->clear();
190  ui->statusLabel_SM->clear();
191 
192  ui->addressIn_SM->setFocus();
193 }
194 
196 {
197  if (model && model->getAddressTableModel())
198  {
201  if (dlg.exec())
202  {
204  }
205  }
206 }
207 
209 {
210  CFabcoinAddress addr(ui->addressIn_VM->text().toStdString());
211  if (!addr.IsValid())
212  {
213  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
214  ui->statusLabel_VM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
215  return;
216  }
217  CKeyID keyID;
218  if (!addr.GetKeyID(keyID))
219  {
220  ui->addressIn_VM->setValid(false);
221  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
222  ui->statusLabel_VM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
223  return;
224  }
225 
226  bool fInvalid = false;
227  std::vector<unsigned char> vchSig = DecodeBase64(ui->signatureIn_VM->text().toStdString().c_str(), &fInvalid);
228 
229  if (fInvalid)
230  {
231  ui->signatureIn_VM->setValid(false);
232  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
233  ui->statusLabel_VM->setText(tr("The signature could not be decoded.") + QString(" ") + tr("Please check the signature and try again."));
234  return;
235  }
236 
237  CHashWriter ss(SER_GETHASH, 0);
238  ss << strMessageMagic;
239  ss << ui->messageIn_VM->document()->toPlainText().toStdString();
240 
241  CPubKey pubkey;
242  if (!pubkey.RecoverCompact(ss.GetHash(), vchSig))
243  {
244  ui->signatureIn_VM->setValid(false);
245  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
246  ui->statusLabel_VM->setText(tr("The signature did not match the message digest.") + QString(" ") + tr("Please check the signature and try again."));
247  return;
248  }
249 
250  if (!(CFabcoinAddress(pubkey.GetID()) == addr))
251  {
252  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
253  ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>"));
254  return;
255  }
256 
257  ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }");
258  ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verified.") + QString("</nobr>"));
259 }
260 
262 {
263  ui->addressIn_VM->clear();
264  ui->signatureIn_VM->clear();
265  ui->messageIn_VM->clear();
266  ui->statusLabel_VM->clear();
267 
268  ui->addressIn_VM->setFocus();
269 }
270 
271 bool SignVerifyMessageDialog::eventFilter(QObject *object, QEvent *event)
272 {
273  if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::FocusIn)
274  {
275  if (ui->tabWidget->currentIndex() == 0)
276  {
277  /* Clear status message on focus change */
278  ui->statusLabel_SM->clear();
279 
280  /* Select generated signature */
281  if (object == ui->signatureOut_SM)
282  {
283  ui->signatureOut_SM->selectAll();
284  return true;
285  }
286  }
287  else if (ui->tabWidget->currentIndex() == 1)
288  {
289  /* Clear status message on focus change */
290  ui->statusLabel_VM->clear();
291  }
292  }
293  return QDialog::eventFilter(object, event);
294 }
#define SetObjectStyleSheet(object, name)
Definition: styleSheet.h:10
bool eventFilter(QObject *object, QEvent *event)
QFont fixedPitchFont()
Definition: guiutil.cpp:90
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
void setAddress_VM(const QString &address)
void setModel(AddressTableModel *model)
void setupUi(QDialog *SignVerifyMessageDialog)
UnlockContext requestUnlock()
const QString & getReturnValue() const
base58-encoded Fabcoin addresses.
Definition: base58.h:104
Open address book to pick address.
AddressTableModel * getAddressTableModel()
bool getPrivKey(const CKeyID &address, CKey &vchPrivKeyOut) const
const std::string strMessageMagic
Definition: validation.cpp:115
Ui::SignVerifyMessageDialog * ui
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:122
void setClipboard(const QString &str)
Definition: guiutil.cpp:858
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:184
An encapsulated public key.
Definition: pubkey.h:39
Widget that shows a list of sending or receiving addresses.
SignVerifyMessageDialog(const PlatformStyle *platformStyle, QWidget *parent)
uint256 GetHash()
Definition: hash.h:149
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key...
Definition: key.cpp:189
QIcon MultiStatesIcon(const QString &resourcename, StateType type=NavBar, QColor color=Qt::white, QColor colorAlt=0x2d2d2d) const
Get multi-states icon.
Interface to Fabcoin wallet from Qt view code.
Definition: walletmodel.h:103
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:130
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
An encapsulated private key.
Definition: key.h:35
const PlatformStyle * platformStyle
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:146
void setModel(WalletModel *model)
void setValid(bool valid)
std::string EncodeBase64(const unsigned char *pch, size_t len)
void setAddress_SM(const QString &address)