Dash Core  0.12.2.1
P2P Digital Currency
askpassphrasedialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 The Bitcoin Core developers
2 // Copyright (c) 2014-2017 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 #include "askpassphrasedialog.h"
8 
9 #include "guiconstants.h"
10 #include "walletmodel.h"
11 
13 
14 #include <QKeyEvent>
15 #include <QMessageBox>
16 #include <QPushButton>
17 
19  QDialog(parent),
20  ui(new Ui::AskPassphraseDialog),
21  mode(mode),
22  model(0),
23  fCapsLock(false)
24 {
25  ui->setupUi(this);
26 
27  ui->passEdit1->setMinimumSize(ui->passEdit1->sizeHint());
28  ui->passEdit2->setMinimumSize(ui->passEdit2->sizeHint());
29  ui->passEdit3->setMinimumSize(ui->passEdit3->sizeHint());
30 
31  ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
32  ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
33  ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
34 
35  // Setup Caps Lock detection.
36  ui->passEdit1->installEventFilter(this);
37  ui->passEdit2->installEventFilter(this);
38  ui->passEdit3->installEventFilter(this);
39 
40  switch(mode)
41  {
42  case Encrypt: // Ask passphrase x2
43  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>."));
44  ui->passLabel1->hide();
45  ui->passEdit1->hide();
46  setWindowTitle(tr("Encrypt wallet"));
47  break;
48  case UnlockMixing:
49  ui->mixingOnlyCheckBox->show();
50  ui->mixingOnlyCheckBox->setChecked(true);
51  case Unlock: // Ask passphrase
52  ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
53  ui->passLabel2->hide();
54  ui->passEdit2->hide();
55  ui->passLabel3->hide();
56  ui->passEdit3->hide();
57  setWindowTitle(tr("Unlock wallet"));
58  break;
59  case Decrypt: // Ask passphrase
60  ui->warningLabel->setText(tr("This operation needs your wallet passphrase to decrypt the wallet."));
61  ui->passLabel2->hide();
62  ui->passEdit2->hide();
63  ui->passLabel3->hide();
64  ui->passEdit3->hide();
65  setWindowTitle(tr("Decrypt wallet"));
66  break;
67  case ChangePass: // Ask old passphrase + new passphrase x2
68  setWindowTitle(tr("Change passphrase"));
69  ui->warningLabel->setText(tr("Enter the old passphrase and new passphrase to the wallet."));
70  break;
71  }
72  textChanged();
73  connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
74  connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
75  connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
76 }
77 
79 {
81  delete ui;
82 }
83 
85 {
86  this->model = model;
87 }
88 
90 {
91  SecureString oldpass, newpass1, newpass2;
92  if(!model)
93  return;
94  oldpass.reserve(MAX_PASSPHRASE_SIZE);
95  newpass1.reserve(MAX_PASSPHRASE_SIZE);
96  newpass2.reserve(MAX_PASSPHRASE_SIZE);
97  // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
98  // Alternately, find a way to make this input mlock()'d to begin with.
99  oldpass.assign(ui->passEdit1->text().toStdString().c_str());
100  newpass1.assign(ui->passEdit2->text().toStdString().c_str());
101  newpass2.assign(ui->passEdit3->text().toStdString().c_str());
102 
104 
105  switch(mode)
106  {
107  case Encrypt: {
108  if(newpass1.empty() || newpass2.empty())
109  {
110  // Cannot encrypt with empty passphrase
111  break;
112  }
113  QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
114  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?"),
115  QMessageBox::Yes|QMessageBox::Cancel,
116  QMessageBox::Cancel);
117  if(retval == QMessageBox::Yes)
118  {
119  if(newpass1 == newpass2)
120  {
121  if(model->setWalletEncrypted(true, newpass1))
122  {
123  QMessageBox::warning(this, tr("Wallet encrypted"),
124  "<qt>" +
125  tr("Dash Core will close now to finish the encryption process. "
126  "Remember that encrypting your wallet cannot fully protect "
127  "your dashs from being stolen by malware infecting your computer.") +
128  "<br><br><b>" +
129  tr("IMPORTANT: Any previous backups you have made of your wallet file "
130  "should be replaced with the newly generated, encrypted wallet file. "
131  "For security reasons, previous backups of the unencrypted wallet file "
132  "will become useless as soon as you start using the new, encrypted wallet.") +
133  "</b></qt>");
134  QApplication::quit();
135  }
136  else
137  {
138  QMessageBox::critical(this, tr("Wallet encryption failed"),
139  tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
140  }
141  QDialog::accept(); // Success
142  }
143  else
144  {
145  QMessageBox::critical(this, tr("Wallet encryption failed"),
146  tr("The supplied passphrases do not match."));
147  }
148  }
149  else
150  {
151  QDialog::reject(); // Cancelled
152  }
153  } break;
154  case UnlockMixing:
155  case Unlock:
156  if(!model->setWalletLocked(false, oldpass, ui->mixingOnlyCheckBox->isChecked()))
157  {
158  QMessageBox::critical(this, tr("Wallet unlock failed"),
159  tr("The passphrase entered for the wallet decryption was incorrect."));
160  }
161  else
162  {
163  QDialog::accept(); // Success
164  }
165  break;
166  case Decrypt:
167  if(!model->setWalletEncrypted(false, oldpass))
168  {
169  QMessageBox::critical(this, tr("Wallet decryption failed"),
170  tr("The passphrase entered for the wallet decryption was incorrect."));
171  }
172  else
173  {
174  QDialog::accept(); // Success
175  }
176  break;
177  case ChangePass:
178  if(newpass1 == newpass2)
179  {
180  if(model->changePassphrase(oldpass, newpass1))
181  {
182  QMessageBox::information(this, tr("Wallet encrypted"),
183  tr("Wallet passphrase was successfully changed."));
184  QDialog::accept(); // Success
185  }
186  else
187  {
188  QMessageBox::critical(this, tr("Wallet encryption failed"),
189  tr("The passphrase entered for the wallet decryption was incorrect."));
190  }
191  }
192  else
193  {
194  QMessageBox::critical(this, tr("Wallet encryption failed"),
195  tr("The supplied passphrases do not match."));
196  }
197  break;
198  }
199 }
200 
202 {
203  // Validate input, set Ok button to enabled when acceptable
204  bool acceptable = false;
205  switch(mode)
206  {
207  case Encrypt: // New passphrase x2
208  acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
209  break;
210  case UnlockMixing: // Old passphrase x1
211  case Unlock: // Old passphrase x1
212  case Decrypt:
213  acceptable = !ui->passEdit1->text().isEmpty();
214  break;
215  case ChangePass: // Old passphrase x1, new passphrase x2
216  acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
217  break;
218  }
219  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
220 }
221 
222 bool AskPassphraseDialog::event(QEvent *event)
223 {
224  // Detect Caps Lock key press.
225  if (event->type() == QEvent::KeyPress) {
226  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
227  if (ke->key() == Qt::Key_CapsLock) {
228  fCapsLock = !fCapsLock;
229  }
230  if (fCapsLock) {
231  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
232  } else {
233  ui->capsLabel->clear();
234  }
235  }
236  return QWidget::event(event);
237 }
238 
239 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
240 {
241  /* Detect Caps Lock.
242  * There is no good OS-independent way to check a key state in Qt, but we
243  * can detect Caps Lock by checking for the following condition:
244  * Shift key is down and the result is a lower case character, or
245  * Shift key is not down and the result is an upper case character.
246  */
247  if (event->type() == QEvent::KeyPress) {
248  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
249  QString str = ke->text();
250  if (str.length() != 0) {
251  const QChar *psz = str.unicode();
252  bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
253  if ((fShift && *psz >= 'a' && *psz <= 'z') || (!fShift && *psz >= 'A' && *psz <= 'Z')) {
254  fCapsLock = true;
255  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
256  } else if (psz->isLetter()) {
257  fCapsLock = false;
258  ui->capsLabel->clear();
259  }
260  }
261  }
262  return QDialog::eventFilter(object, event);
263 }
264 
265 static void SecureClearQLineEdit(QLineEdit* edit)
266 {
267  // Attempt to overwrite text so that they do not linger around in memory
268  edit->setText(QString(" ").repeated(edit->text().size()));
269  edit->clear();
270 }
271 
273 {
277 }
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString(), bool fMixing=false)
static const int MAX_PASSPHRASE_SIZE
Definition: guiconstants.h:13
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:61
static void SecureClearQLineEdit(QLineEdit *edit)
Ui::AskPassphraseDialog * ui
QDialogButtonBox * buttonBox
bool setWalletEncrypted(bool encrypted, const SecureString &passphrase)
AddressTableModel * parent
AskPassphraseDialog(Mode mode, QWidget *parent)
bool eventFilter(QObject *object, QEvent *event)
void setupUi(QDialog *AskPassphraseDialog)
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
void setModel(WalletModel *model)
bool event(QEvent *event)