PRCYCoin  2.0.0.7rc1
P2P Digital Currency
signverifymessagedialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2014 The Bitcoin developers
2 // Copyright (c) 2014-2015 The Dash developers
3 // Copyright (c) 2015-2018 The PIVX developers
4 // Copyright (c) 2018-2020 The DAPS Project developers
5 // Distributed under the MIT/X11 software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
9 #include "ui_signverifymessagedialog.h"
10 
11 #include "addressbookpage.h"
12 #include "guiutil.h"
13 #include "walletmodel.h"
14 
15 #include "base58.h"
16 #include "init.h"
17 #include "wallet/wallet.h"
18 #include "askpassphrasedialog.h"
19 
20 #include <string>
21 #include <vector>
22 
23 #include <QClipboard>
24 
25 SignVerifyMessageDialog::SignVerifyMessageDialog(QWidget* parent) : QDialog(parent, Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
26  ui(new Ui::SignVerifyMessageDialog),
27  model(0)
28 {
29  ui->setupUi(this);
30 
31  ui->signatureOut_SM->setPlaceholderText(tr("Click \"Sign Message\" to generate signature"));
32 
33  GUIUtil::setupAddressWidget(ui->addressIn_SM, this);
34  GUIUtil::setupAddressWidget(ui->addressIn_VM, this);
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 
43  ui->signatureOut_SM->setFont(GUIUtil::bitcoinAddressFont());
44  ui->signatureIn_VM->setFont(GUIUtil::bitcoinAddressFont());
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 {
85  if (model && model->getAddressTableModel()) {
88  if (dlg.exec()) {
90  }
91  }
92 }
93 
95 {
96  setAddress_SM(QApplication::clipboard()->text());
97 }
98 
100 {
101  if (!model)
102  return;
103 
104  /* Clear old signature to ensure users don't get confused on error with an old signature displayed */
105  ui->signatureOut_SM->clear();
106 
107  CBitcoinAddress addr(ui->addressIn_SM->text().toStdString());
108  if (!addr.IsValid()) {
109  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
110  ui->statusLabel_SM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
111  return;
112  }
113  CKeyID keyID;
114  if (!addr.GetKeyID(keyID)) {
115  ui->addressIn_SM->setValid(false);
116  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
117  ui->statusLabel_SM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
118  return;
119  }
120 
122  if (!ctx.isValid()) {
123  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
124  ui->statusLabel_SM->setText(tr("Wallet unlock was cancelled."));
125  return;
126  }
127 
128  CKey key;
129  if (pwalletMain && !pwalletMain->GetKey(keyID, key)) {
130  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
131  ui->statusLabel_SM->setText(tr("Private key for the entered address is not available."));
132  return;
133  }
134 
135  CDataStream ss(SER_GETHASH, 0);
136  ss << strMessageMagic;
137  ss << ui->messageIn_SM->document()->toPlainText().toStdString();
138 
139  std::vector<unsigned char> vchSig;
140  if (!key.SignCompact(Hash(ss.begin(), ss.end()), vchSig)) {
141  ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
142  ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signing failed.") + QString("</nobr>"));
143  return;
144  }
145 
146  ui->statusLabel_SM->setStyleSheet("QLabel { color: green; }");
147  ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signed.") + QString("</nobr>"));
148 
149  ui->signatureOut_SM->setText(QString::fromStdString(EncodeBase64(&vchSig[0], vchSig.size())));
150 }
151 
153 {
154  GUIUtil::setClipboard(ui->signatureOut_SM->text());
155 }
156 
158 {
159  ui->addressIn_SM->clear();
160  ui->messageIn_SM->clear();
161  ui->signatureOut_SM->clear();
162  ui->statusLabel_SM->clear();
163 
164  ui->addressIn_SM->setFocus();
165 }
166 
168 {
169  if (model && model->getAddressTableModel()) {
172  if (dlg.exec()) {
174  }
175  }
176 }
177 
179 {
180  CBitcoinAddress addr(ui->addressIn_VM->text().toStdString());
181  if (!addr.IsValid()) {
182  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
183  ui->statusLabel_VM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
184  return;
185  }
186  CKeyID keyID;
187  if (!addr.GetKeyID(keyID)) {
188  ui->addressIn_VM->setValid(false);
189  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
190  ui->statusLabel_VM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
191  return;
192  }
193 
194  bool fInvalid = false;
195  std::vector<unsigned char> vchSig = DecodeBase64(ui->signatureIn_VM->text().toStdString().c_str(), &fInvalid);
196 
197  if (fInvalid) {
198  ui->signatureIn_VM->setValid(false);
199  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
200  ui->statusLabel_VM->setText(tr("The signature could not be decoded.") + QString(" ") + tr("Please check the signature and try again."));
201  return;
202  }
203 
204  CDataStream ss(SER_GETHASH, 0);
205  ss << strMessageMagic;
206  ss << ui->messageIn_VM->document()->toPlainText().toStdString();
207 
208  CPubKey pubkey;
209  if (!pubkey.RecoverCompact(Hash(ss.begin(), ss.end()), vchSig)) {
210  ui->signatureIn_VM->setValid(false);
211  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
212  ui->statusLabel_VM->setText(tr("The signature did not match the message digest.") + QString(" ") + tr("Please check the signature and try again."));
213  return;
214  }
215 
216  if (!(CBitcoinAddress(pubkey.GetID()) == addr)) {
217  ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");
218  ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>"));
219  return;
220  }
221 
222  ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }");
223  ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verified.") + QString("</nobr>"));
224 }
225 
227 {
228  ui->addressIn_VM->clear();
229  ui->signatureIn_VM->clear();
230  ui->messageIn_VM->clear();
231  ui->statusLabel_VM->clear();
232 
233  ui->addressIn_VM->setFocus();
234 }
235 
236 bool SignVerifyMessageDialog::eventFilter(QObject* object, QEvent* event)
237 {
238  if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::FocusIn) {
239  if (ui->tabWidget->currentIndex() == 0) {
240  /* Clear status message on focus change */
241  ui->statusLabel_SM->clear();
242 
243  /* Select generated signature */
244  if (object == ui->signatureOut_SM) {
245  ui->signatureOut_SM->selectAll();
246  return true;
247  }
248  } else if (ui->tabWidget->currentIndex() == 1) {
249  /* Clear status message on focus change */
250  ui->statusLabel_VM->clear();
251  }
252  }
253  return QDialog::eventFilter(object, event);
254 }
signverifymessagedialog.h
GUIUtil::bitcoinAddressFont
QFont bitcoinAddressFont()
Definition: guiutil.cpp:81
AddressBookPage::setModel
void setModel(AddressTableModel *model)
Definition: addressbookpage.cpp:110
AddressBookPage::SendingTab
@ SendingTab
Definition: addressbookpage.h:34
SignVerifyMessageDialog::on_signMessageButton_SM_clicked
void on_signMessageButton_SM_clicked()
Definition: signverifymessagedialog.cpp:99
WalletModel::UnlockContext::isValid
bool isValid() const
Definition: walletmodel.h:195
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:161
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:102
SignVerifyMessageDialog::setAddress_VM
void setAddress_VM(const QString &address)
Definition: signverifymessagedialog.cpp:63
CBitcoinAddress::GetKeyID
bool GetKeyID(CKeyID &keyID) const
Definition: base58.cpp:281
SignVerifyMessageDialog::eventFilter
bool eventFilter(QObject *object, QEvent *event)
Definition: signverifymessagedialog.cpp:236
CDataStream::begin
const_iterator begin() const
Definition: streams.h:116
addressbookpage.h
SignVerifyMessageDialog::showTab_VM
void showTab_VM(bool fShow)
Definition: signverifymessagedialog.cpp:76
walletmodel.h
CBitcoinAddress
base58-encoded PRCY addresses.
Definition: base58.h:109
WalletModel::getAddressTableModel
AddressTableModel * getAddressTableModel()
Definition: walletmodel.cpp:363
wallet.h
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
SignVerifyMessageDialog::setModel
void setModel(WalletModel *model)
Definition: signverifymessagedialog.cpp:52
SignVerifyMessageDialog
Definition: signverifymessagedialog.h:17
GUIUtil::setClipboard
void setClipboard(const QString &str)
Definition: guiutil.cpp:785
DecodeBase64
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
Definition: utilstrencodings.cpp:150
SignVerifyMessageDialog::model
WalletModel * model
Definition: signverifymessagedialog.h:37
SignVerifyMessageDialog::on_addressBookButton_VM_clicked
void on_addressBookButton_VM_clicked()
Definition: signverifymessagedialog.cpp:167
init.h
SignVerifyMessageDialog::ui
Ui::SignVerifyMessageDialog * ui
Definition: signverifymessagedialog.h:36
SignVerifyMessageDialog::SignVerifyMessageDialog
SignVerifyMessageDialog(QWidget *parent)
Definition: signverifymessagedialog.cpp:25
CKey::SignCompact
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:126
CDataStream::end
const_iterator end() const
Definition: streams.h:118
AddressBookPage
Widget that shows a list of sending or receiving addresses.
Definition: addressbookpage.h:28
guiutil.h
SignVerifyMessageDialog::~SignVerifyMessageDialog
~SignVerifyMessageDialog()
Definition: signverifymessagedialog.cpp:47
SignVerifyMessageDialog::on_copySignatureButton_SM_clicked
void on_copySignatureButton_SM_clicked()
Definition: signverifymessagedialog.cpp:152
CPubKey::RecoverCompact
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:32
EncodeBase64
std::string EncodeBase64(const unsigned char *pch, size_t len)
Definition: utilstrencodings.cpp:102
SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked
void on_verifyMessageButton_VM_clicked()
Definition: signverifymessagedialog.cpp:178
AskPassphraseDialog::Context::Sign_Message
@ Sign_Message
Multi-Signature dialog.
SignVerifyMessageDialog::setAddress_SM
void setAddress_SM(const QString &address)
Definition: signverifymessagedialog.cpp:57
CPubKey
An encapsulated public key.
Definition: pubkey.h:37
Ui
Definition: 2faconfirmdialog.h:7
SignVerifyMessageDialog::on_pasteButton_SM_clicked
void on_pasteButton_SM_clicked()
Definition: signverifymessagedialog.cpp:94
CKey
An encapsulated private key.
Definition: key.h:39
WalletModel::UnlockContext
Definition: walletmodel.h:189
SignVerifyMessageDialog::on_addressBookButton_SM_clicked
void on_addressBookButton_SM_clicked()
Definition: signverifymessagedialog.cpp:83
key
CKey key
Definition: bip38tooldialog.cpp:173
strMessageMagic
const std::string strMessageMagic
Definition: main.cpp:119
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:34
base58.h
SignVerifyMessageDialog::on_clearButton_VM_clicked
void on_clearButton_VM_clicked()
Definition: signverifymessagedialog.cpp:226
AddressBookPage::getReturnValue
const QString & getReturnValue() const
Definition: addressbookpage.h:47
Hash
std::string Hash(std::string input)
Compute the 256-bit hash of a std::string.
Definition: hash.h:122
CBitcoinAddress::IsValid
bool IsValid() const
Definition: base58.cpp:254
GUIUtil::setupAddressWidget
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:86
pwalletMain
CWallet * pwalletMain
Definition: wallet.cpp:49
CWallet::GetKey
bool GetKey(const CKeyID &address, CKey &keyOut) const
GetKey implementation that can derive a HD private key on the fly.
Definition: wallet.cpp:6906
SignVerifyMessageDialog::showTab_SM
void showTab_SM(bool fShow)
Definition: signverifymessagedialog.cpp:69
WalletModel::requestUnlock
UnlockContext requestUnlock(AskPassphraseDialog::Context context, bool relock=false)
Definition: walletmodel.cpp:546
askpassphrasedialog.h
SignVerifyMessageDialog::on_clearButton_SM_clicked
void on_clearButton_SM_clicked()
Definition: signverifymessagedialog.cpp:157
CPubKey::GetID
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:143
AddressBookPage::ForSelection
@ ForSelection
Open address book to pick address.
Definition: addressbookpage.h:39
AddressBookPage::ReceivingTab
@ ReceivingTab
Definition: addressbookpage.h:35