Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

signverifymessagedialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 The Bitcoin Core developers
2 // Copyright (c) 2014-2020 The Dash Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
7 #include <qt/forms/ui_signverifymessagedialog.h>
8 
9 #include <qt/addressbookpage.h>
10 #include <qt/guiutil.h>
11 #include <qt/walletmodel.h>
12 
13 #include <base58.h>
14 #include <init.h>
15 #include <validation.h> // For strMessageMagic
16 #include <wallet/wallet.h>
17 
18 #include <string>
19 #include <vector>
20 
21 #include <QClipboard>
22 
24  QDialog(parent),
25  ui(new Ui::SignVerifyMessageDialog),
26  model(0)
27 {
28  ui->setupUi(this);
29  pageButtons.addButton(ui->btnSignMessage, pageButtons.buttons().size());
30  pageButtons.addButton(ui->btnVerifyMessage, pageButtons.buttons().size());
31  connect(&pageButtons, SIGNAL(buttonClicked(int)), this, SLOT(showPage(int)));
32 #if QT_VERSION >= 0x040700
33  ui->messageIn_SM->setPlaceholderText(tr("Enter a message to be signed"));
34  ui->signatureOut_SM->setPlaceholderText(tr("Click \"Sign Message\" to generate signature"));
35 
36  ui->messageIn_VM->setPlaceholderText(tr("Enter a message to be verified"));
37  ui->signatureIn_VM->setPlaceholderText(tr("Enter a signature for the message to be verified"));
38 #endif
39 
40  GUIUtil::setIcon(ui->addressBookButton_SM, "address-book");
41  GUIUtil::setIcon(ui->pasteButton_SM, "editpaste");
42  GUIUtil::setIcon(ui->copySignatureButton_SM, "editcopy");
43  GUIUtil::setIcon(ui->addressBookButton_VM, "address-book");
44 
45  GUIUtil::setupAddressWidget(ui->addressIn_SM, this);
46  GUIUtil::setupAddressWidget(ui->addressIn_VM, this);
47 
48  ui->addressIn_SM->installEventFilter(this);
49  ui->messageIn_SM->installEventFilter(this);
50  ui->signatureOut_SM->installEventFilter(this);
51  ui->addressIn_VM->installEventFilter(this);
52  ui->messageIn_VM->installEventFilter(this);
53  ui->signatureIn_VM->installEventFilter(this);
54 
55  GUIUtil::setFont({ui->signatureOut_SM, ui->signatureIn_VM}, GUIUtil::FontWeight::Normal, 11, true);
56  GUIUtil::setFont({ui->signatureLabel_SM}, GUIUtil::FontWeight::Bold, 16);
57  GUIUtil::setFont({ui->statusLabel_SM, ui->statusLabel_VM}, GUIUtil::FontWeight::Bold);
58 
60 
62 }
63 
65 {
66  delete ui;
67 }
68 
70 {
71  this->model = _model;
72 }
73 
74 void SignVerifyMessageDialog::setAddress_SM(const QString &address)
75 {
76  ui->addressIn_SM->setText(address);
77  ui->messageIn_SM->setFocus();
78 }
79 
80 void SignVerifyMessageDialog::setAddress_VM(const QString &address)
81 {
82  ui->addressIn_VM->setText(address);
83  ui->messageIn_VM->setFocus();
84 }
85 
87 {
88  showPage(0);
89  if (fShow)
90  this->show();
91 }
92 
94 {
95  showPage(1);
96  if (fShow)
97  this->show();
98 }
99 
101 {
102  std::vector<QWidget*> vecNormal;
103  QAbstractButton* btnActive = pageButtons.button(index);
104  for (QAbstractButton* button : pageButtons.buttons()) {
105  if (button != btnActive) {
106  vecNormal.push_back(button);
107  }
108  }
109 
113 
114  ui->stackedWidgetSig->setCurrentIndex(index);
115  btnActive->setChecked(true);
116 }
117 
119 {
120  if (model && model->getAddressTableModel())
121  {
124  if (dlg.exec())
125  {
127  }
128  }
129 }
130 
132 {
133  setAddress_SM(QApplication::clipboard()->text());
134 }
135 
137 {
138  if (!model)
139  return;
140 
141  /* Clear old signature to ensure users don't get confused on error with an old signature displayed */
142  ui->signatureOut_SM->clear();
143 
144  CTxDestination destination = DecodeDestination(ui->addressIn_SM->text().toStdString());
145  if (!IsValidDestination(destination)) {
146  ui->statusLabel_SM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR));
147  ui->statusLabel_SM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
148  return;
149  }
150  const CKeyID* keyID = boost::get<CKeyID>(&destination);
151  if (!keyID) {
152  ui->addressIn_SM->setValid(false);
153  ui->statusLabel_SM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR));
154  ui->statusLabel_SM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
155  return;
156  }
157 
159  if (!ctx.isValid())
160  {
161  ui->statusLabel_SM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR));
162  ui->statusLabel_SM->setText(tr("Wallet unlock was cancelled."));
163  return;
164  }
165 
166  CKey key;
167  if (!model->getPrivKey(*keyID, key))
168  {
169  ui->statusLabel_SM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR));
170  ui->statusLabel_SM->setText(tr("Private key for the entered address is not available."));
171  return;
172  }
173 
174  CHashWriter ss(SER_GETHASH, 0);
175  ss << strMessageMagic;
176  ss << ui->messageIn_SM->document()->toPlainText().toStdString();
177 
178  std::vector<unsigned char> vchSig;
179  if (!key.SignCompact(ss.GetHash(), vchSig))
180  {
181  ui->statusLabel_SM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR));
182  ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signing failed.") + QString("</nobr>"));
183  return;
184  }
185 
186  ui->statusLabel_SM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_SUCCESS));
187  ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signed.") + QString("</nobr>"));
188 
189  ui->signatureOut_SM->setText(QString::fromStdString(EncodeBase64(vchSig.data(), vchSig.size())));
190 }
191 
193 {
194  GUIUtil::setClipboard(ui->signatureOut_SM->text());
195 }
196 
198 {
199  ui->addressIn_SM->clear();
200  ui->messageIn_SM->clear();
201  ui->signatureOut_SM->clear();
202  ui->statusLabel_SM->clear();
203 
204  ui->addressIn_SM->setFocus();
205 }
206 
208 {
209  if (model && model->getAddressTableModel())
210  {
213  if (dlg.exec())
214  {
216  }
217  }
218 }
219 
221 {
222  CTxDestination destination = DecodeDestination(ui->addressIn_VM->text().toStdString());
223  if (!IsValidDestination(destination)) {
224  ui->statusLabel_VM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR));
225  ui->statusLabel_VM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
226  return;
227  }
228  if (!boost::get<CKeyID>(&destination)) {
229  ui->addressIn_VM->setValid(false);
230  ui->statusLabel_VM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR));
231  ui->statusLabel_VM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
232  return;
233  }
234 
235  bool fInvalid = false;
236  std::vector<unsigned char> vchSig = DecodeBase64(ui->signatureIn_VM->text().toStdString().c_str(), &fInvalid);
237 
238  if (fInvalid)
239  {
240  ui->signatureIn_VM->setValid(false);
241  ui->statusLabel_VM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR));
242  ui->statusLabel_VM->setText(tr("The signature could not be decoded.") + QString(" ") + tr("Please check the signature and try again."));
243  return;
244  }
245 
246  CHashWriter ss(SER_GETHASH, 0);
247  ss << strMessageMagic;
248  ss << ui->messageIn_VM->document()->toPlainText().toStdString();
249 
250  CPubKey pubkey;
251  if (!pubkey.RecoverCompact(ss.GetHash(), vchSig))
252  {
253  ui->signatureIn_VM->setValid(false);
254  ui->statusLabel_VM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR));
255  ui->statusLabel_VM->setText(tr("The signature did not match the message digest.") + QString(" ") + tr("Please check the signature and try again."));
256  return;
257  }
258 
259  if (!(CTxDestination(pubkey.GetID()) == destination)) {
260  ui->statusLabel_VM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR));
261  ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>"));
262  return;
263  }
264 
265  ui->statusLabel_VM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_SUCCESS));
266  ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verified.") + QString("</nobr>"));
267 }
268 
270 {
271  ui->addressIn_VM->clear();
272  ui->signatureIn_VM->clear();
273  ui->messageIn_VM->clear();
274  ui->statusLabel_VM->clear();
275 
276  ui->addressIn_VM->setFocus();
277 }
278 
279 bool SignVerifyMessageDialog::eventFilter(QObject *object, QEvent *event)
280 {
281  if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::FocusIn)
282  {
283  if (ui->stackedWidgetSig->currentIndex() == 0) {
284  /* Clear status message on focus change */
285  ui->statusLabel_SM->clear();
286 
287  /* Select generated signature */
288  if (object == ui->signatureOut_SM)
289  {
290  ui->signatureOut_SM->selectAll();
291  return true;
292  }
293  } else if (ui->stackedWidgetSig->currentIndex() == 1) {
294  /* Clear status message on focus change */
295  ui->statusLabel_VM->clear();
296  }
297  }
298  return QDialog::eventFilter(object, event);
299 }
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:80
bool eventFilter(QObject *object, QEvent *event)
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
void setFont(const std::vector< QWidget *> &vecWidgets, FontWeight weight, int nPointSize, bool fItalic)
Workaround to set correct font styles in all themes since there is a bug in macOS which leads to issu...
Definition: guiutil.cpp:1552
void setAddress_VM(const QString &address)
void setModel(AddressTableModel *model)
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:281
CTxDestination DecodeDestination(const std::string &str)
Definition: base58.cpp:336
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent, bool fAllowURI)
Definition: guiutil.cpp:286
Open address book to pick address.
AddressTableModel * getAddressTableModel()
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:149
const std::string strMessageMagic
Definition: validation.cpp:257
Ui::SignVerifyMessageDialog * ui
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:221
void updateFonts()
Update the font of all widgets where a custom font has been set with GUIUtil::setFont.
Definition: guiutil.cpp:1563
bool getPrivKey(const CKeyID &address, CKey &vchPrivKeyOut) const
void showPage(int index)
custom tab buttons clicked
void setClipboard(const QString &str)
Definition: guiutil.cpp:1817
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:186
static secp256k1_context * ctx
Definition: tests.c:46
An encapsulated public key.
Definition: pubkey.h:30
Widget that shows a list of sending or receiving addresses.
void disableMacFocusRect(const QWidget *w)
Disable the OS default focus rect for macOS because we have custom focus rects set in the css files...
Definition: guiutil.cpp:1789
uint256 GetHash()
Definition: hash.h:203
UnlockContext requestUnlock(bool fForMixingOnly=false)
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:100
QString getThemedStyleQString(ThemedStyle style)
Helper to get css style strings which are injected into rich text through qt.
Definition: guiutil.cpp:210
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
SignVerifyMessageDialog(QWidget *parent)
void setIcon(QAbstractButton *button, const QString &strIcon, const ThemedColor color, const ThemedColor colorAlternative, const QSize &size)
Helper to set an icon for a button with the given color (replaces black) and colorAlternative (replac...
Definition: guiutil.cpp:247
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:184
An encapsulated private key.
Definition: key.h:27
void setModel(WalletModel *model)
std::string EncodeBase64(const unsigned char *pch, size_t len)
const QString & getReturnValue() const
void setAddress_SM(const QString &address)
Released under the MIT license