Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

askpassphrasedialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 The Bitcoin Core developers
2 // Copyright (c) 2014-2019 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 
6 #if defined(HAVE_CONFIG_H)
7 #include <config/dash-config.h>
8 #endif
9 
10 #include <qt/askpassphrasedialog.h>
11 #include <qt/forms/ui_askpassphrasedialog.h>
12 
13 #include <qt/guiconstants.h>
14 #include <qt/guiutil.h>
15 #include <qt/walletmodel.h>
16 
18 
19 #include <QKeyEvent>
20 #include <QMessageBox>
21 #include <QPushButton>
22 
24  QDialog(parent),
25  ui(new Ui::AskPassphraseDialog),
26  mode(_mode),
27  model(0),
28  fCapsLock(false)
29 {
30  ui->setupUi(this);
31 
33 
35 
37 
38  ui->passEdit1->setMinimumSize(ui->passEdit1->sizeHint());
39  ui->passEdit2->setMinimumSize(ui->passEdit2->sizeHint());
40  ui->passEdit3->setMinimumSize(ui->passEdit3->sizeHint());
41 
42  ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
43  ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
44  ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
45 
46  // Setup Caps Lock detection.
47  ui->passEdit1->installEventFilter(this);
48  ui->passEdit2->installEventFilter(this);
49  ui->passEdit3->installEventFilter(this);
50 
51  switch(mode)
52  {
53  case Encrypt: // Ask passphrase x2
54  ui->warningLabel->setText(tr("Enter the new passphrase to the wallet.<br/>Please use a passphrase of <b>ten or more random characters</b>, or <b>eight or more words</b>."));
55  ui->passLabel1->hide();
56  ui->passEdit1->hide();
57  setWindowTitle(tr("Encrypt wallet"));
58  break;
59  case UnlockMixing:
60  ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
61  ui->passLabel2->hide();
62  ui->passEdit2->hide();
63  ui->passLabel3->hide();
64  ui->passEdit3->hide();
65  setWindowTitle(tr("Unlock wallet for mixing only"));
66  break;
67  case Unlock: // Ask passphrase
68  ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
69  ui->passLabel2->hide();
70  ui->passEdit2->hide();
71  ui->passLabel3->hide();
72  ui->passEdit3->hide();
73  setWindowTitle(tr("Unlock wallet"));
74  break;
75  case Decrypt: // Ask passphrase
76  ui->warningLabel->setText(tr("This operation needs your wallet passphrase to decrypt the wallet."));
77  ui->passLabel2->hide();
78  ui->passEdit2->hide();
79  ui->passLabel3->hide();
80  ui->passEdit3->hide();
81  setWindowTitle(tr("Decrypt wallet"));
82  break;
83  case ChangePass: // Ask old passphrase + new passphrase x2
84  setWindowTitle(tr("Change passphrase"));
85  ui->warningLabel->setText(tr("Enter the old passphrase and new passphrase to the wallet."));
86  break;
87  }
88  textChanged();
89  connect(ui->toggleShowPasswordButton, SIGNAL(toggled(bool)), this, SLOT(toggleShowPassword(bool)));
90  connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
91  connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
92  connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
93 }
94 
96 {
98  delete ui;
99 }
100 
102 {
103  this->model = _model;
104 }
105 
107 {
108  SecureString oldpass, newpass1, newpass2;
109  if(!model)
110  return;
111  oldpass.reserve(MAX_PASSPHRASE_SIZE);
112  newpass1.reserve(MAX_PASSPHRASE_SIZE);
113  newpass2.reserve(MAX_PASSPHRASE_SIZE);
114  // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
115  // Alternately, find a way to make this input mlock()'d to begin with.
116  oldpass.assign(ui->passEdit1->text().toStdString().c_str());
117  newpass1.assign(ui->passEdit2->text().toStdString().c_str());
118  newpass2.assign(ui->passEdit3->text().toStdString().c_str());
119 
121 
122  switch(mode)
123  {
124  case Encrypt: {
125  if(newpass1.empty() || newpass2.empty())
126  {
127  // Cannot encrypt with empty passphrase
128  break;
129  }
130  QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
131  tr("Warning: If you encrypt your wallet and lose your passphrase, you will <b>LOSE ALL OF YOUR DASH</b>!") + "<br><br>" + tr("Are you sure you wish to encrypt your wallet?"),
132  QMessageBox::Yes|QMessageBox::Cancel,
133  QMessageBox::Cancel);
134  if(retval == QMessageBox::Yes)
135  {
136  if(newpass1 == newpass2)
137  {
138  if(model->setWalletEncrypted(true, newpass1))
139  {
140  if (model->hdEnabled()) {
141  QMessageBox::warning(this, tr("Wallet encrypted"),
142  "<qt>" +
143  tr("%1 will close now to finish the encryption process. "
144  "Remember that encrypting your wallet cannot fully protect "
145  "your funds from being stolen by malware infecting your computer.").arg(tr(PACKAGE_NAME)) +
146  "<br><br><b>" +
147  tr("IMPORTANT: Any previous backups you have made of your wallet file "
148  "should be replaced with the newly generated, encrypted wallet file. "
149  "Previous backups of the unencrypted wallet file contain the same HD seed and "
150  "still have full access to all your funds just like the new, encrypted wallet.") +
151  "</b></qt>");
152  } else {
153  QMessageBox::warning(this, tr("Wallet encrypted"),
154  "<qt>" +
155  tr("%1 will close now to finish the encryption process. "
156  "Remember that encrypting your wallet cannot fully protect "
157  "your funds from being stolen by malware infecting your computer.").arg(tr(PACKAGE_NAME)) +
158  "<br><br><b>" +
159  tr("IMPORTANT: Any previous backups you have made of your wallet file "
160  "should be replaced with the newly generated, encrypted wallet file. "
161  "For security reasons, previous backups of the unencrypted wallet file "
162  "will become useless as soon as you start using the new, encrypted wallet.") +
163  "</b></qt>");
164  }
165  QApplication::quit();
166  }
167  else
168  {
169  QMessageBox::critical(this, tr("Wallet encryption failed"),
170  tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
171  }
172  QDialog::accept(); // Success
173  }
174  else
175  {
176  QMessageBox::critical(this, tr("Wallet encryption failed"),
177  tr("The supplied passphrases do not match."));
178  }
179  }
180  else
181  {
182  QDialog::reject(); // Cancelled
183  }
184  } break;
185  case UnlockMixing:
186  case Unlock:
187  if(!model->setWalletLocked(false, oldpass, mode == UnlockMixing))
188  {
189  QMessageBox::critical(this, tr("Wallet unlock failed"),
190  tr("The passphrase entered for the wallet decryption was incorrect."));
191  }
192  else
193  {
194  QDialog::accept(); // Success
195  }
196  break;
197  case Decrypt:
198  if(!model->setWalletEncrypted(false, oldpass))
199  {
200  QMessageBox::critical(this, tr("Wallet decryption failed"),
201  tr("The passphrase entered for the wallet decryption was incorrect."));
202  }
203  else
204  {
205  QDialog::accept(); // Success
206  }
207  break;
208  case ChangePass:
209  if(newpass1 == newpass2)
210  {
211  if(model->changePassphrase(oldpass, newpass1))
212  {
213  QMessageBox::information(this, tr("Wallet encrypted"),
214  tr("Wallet passphrase was successfully changed."));
215  QDialog::accept(); // Success
216  }
217  else
218  {
219  QMessageBox::critical(this, tr("Wallet encryption failed"),
220  tr("The passphrase entered for the wallet decryption was incorrect."));
221  }
222  }
223  else
224  {
225  QMessageBox::critical(this, tr("Wallet encryption failed"),
226  tr("The supplied passphrases do not match."));
227  }
228  break;
229  }
230 }
231 
233 {
234  // Validate input, set Ok button to enabled when acceptable
235  bool acceptable = false;
236  switch(mode)
237  {
238  case Encrypt: // New passphrase x2
239  acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
240  break;
241  case UnlockMixing: // Old passphrase x1
242  case Unlock: // Old passphrase x1
243  case Decrypt:
244  acceptable = !ui->passEdit1->text().isEmpty();
245  break;
246  case ChangePass: // Old passphrase x1, new passphrase x2
247  acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
248  break;
249  }
250  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
251 }
252 
253 bool AskPassphraseDialog::event(QEvent *event)
254 {
255  // Detect Caps Lock key press.
256  if (event->type() == QEvent::KeyPress) {
257  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
258  if (ke->key() == Qt::Key_CapsLock) {
259  fCapsLock = !fCapsLock;
260  }
261  if (fCapsLock) {
262  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
263  } else {
264  ui->capsLabel->clear();
265  }
266  }
267  return QWidget::event(event);
268 }
269 
271 {
272  ui->toggleShowPasswordButton->setDown(show);
273  const auto mode = show ? QLineEdit::Normal : QLineEdit::Password;
274  ui->passEdit1->setEchoMode(mode);
275  ui->passEdit2->setEchoMode(mode);
276  ui->passEdit3->setEchoMode(mode);
277 }
278 
279 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
280 {
281  /* Detect Caps Lock.
282  * There is no good OS-independent way to check a key state in Qt, but we
283  * can detect Caps Lock by checking for the following condition:
284  * Shift key is down and the result is a lower case character, or
285  * Shift key is not down and the result is an upper case character.
286  */
287  if (event->type() == QEvent::KeyPress) {
288  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
289  QString str = ke->text();
290  if (str.length() != 0) {
291  const QChar *psz = str.unicode();
292  bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
293  if ((fShift && *psz >= 'a' && *psz <= 'z') || (!fShift && *psz >= 'A' && *psz <= 'Z')) {
294  fCapsLock = true;
295  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
296  } else if (psz->isLetter()) {
297  fCapsLock = false;
298  ui->capsLabel->clear();
299  }
300  }
301  }
302  return QDialog::eventFilter(object, event);
303 }
304 
305 static void SecureClearQLineEdit(QLineEdit* edit)
306 {
307  // Attempt to overwrite text so that they do not linger around in memory
308  edit->setText(QString(" ").repeated(edit->text().size()));
309  edit->clear();
310 }
311 
313 {
314  SecureClearQLineEdit(ui->passEdit1);
315  SecureClearQLineEdit(ui->passEdit2);
316  SecureClearQLineEdit(ui->passEdit3);
317 }
static const int MAX_PASSPHRASE_SIZE
Definition: guiconstants.h:13
bool event(QEvent *event)
Ask passphrase and unlock only for mixing.
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
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:57
Ask passphrase twice and encrypt.
static void SecureClearQLineEdit(QLineEdit *edit)
void updateFonts()
Update the font of all widgets where a custom font has been set with GUIUtil::setFont.
Definition: guiutil.cpp:1563
false
Definition: bls_dkg.cpp:168
Ask passphrase and unlock.
Ui::AskPassphraseDialog * ui
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
AskPassphraseDialog(Mode mode, QWidget *parent)
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
bool hdEnabled() const
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:100
Multifunctional dialog to ask for passphrases.
bool setWalletEncrypted(bool encrypted, const SecureString &passphrase)
Ask passphrase and decrypt wallet.
bool eventFilter(QObject *object, QEvent *event)
Ask old passphrase + new passphrase twice.
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString(), bool fMixing=false)
void setModel(WalletModel *model)
Released under the MIT license