Dogecoin Core  1.14.2
P2P Digital Currency
askpassphrasedialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 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 
5 #if defined(HAVE_CONFIG_H)
6 #include "config/bitcoin-config.h"
7 #endif
8 
9 #include "askpassphrasedialog.h"
10 #include "ui_askpassphrasedialog.h"
11 
12 #include "guiconstants.h"
13 #include "walletmodel.h"
14 
16 
17 #include <QKeyEvent>
18 #include <QMessageBox>
19 #include <QPushButton>
20 
22  QDialog(parent),
23  ui(new Ui::AskPassphraseDialog),
24  mode(_mode),
25  model(0),
26  fCapsLock(false)
27 {
28  ui->setupUi(this);
29 
30  ui->passEdit1->setMinimumSize(ui->passEdit1->sizeHint());
31  ui->passEdit2->setMinimumSize(ui->passEdit2->sizeHint());
32  ui->passEdit3->setMinimumSize(ui->passEdit3->sizeHint());
33 
34  ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
35  ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
36  ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
37 
38  // Setup Caps Lock detection.
39  ui->passEdit1->installEventFilter(this);
40  ui->passEdit2->installEventFilter(this);
41  ui->passEdit3->installEventFilter(this);
42 
43  switch(mode)
44  {
45  case Encrypt: // Ask passphrase x2
46  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>."));
47  ui->passLabel1->hide();
48  ui->passEdit1->hide();
49  setWindowTitle(tr("Encrypt wallet"));
50  break;
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 DOGECOINS</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("%1 will close now to finish the encryption process. "
126  "Remember that encrypting your wallet cannot fully protect "
127  "your dogecoins from being stolen by malware infecting your computer.").arg(tr(PACKAGE_NAME)) +
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 Unlock:
155  if(!model->setWalletLocked(false, oldpass))
156  {
157  QMessageBox::critical(this, tr("Wallet unlock failed"),
158  tr("The passphrase entered for the wallet decryption was incorrect."));
159  }
160  else
161  {
162  QDialog::accept(); // Success
163  }
164  break;
165  case Decrypt:
166  if(!model->setWalletEncrypted(false, oldpass))
167  {
168  QMessageBox::critical(this, tr("Wallet decryption failed"),
169  tr("The passphrase entered for the wallet decryption was incorrect."));
170  }
171  else
172  {
173  QDialog::accept(); // Success
174  }
175  break;
176  case ChangePass:
177  if(newpass1 == newpass2)
178  {
179  if(model->changePassphrase(oldpass, newpass1))
180  {
181  QMessageBox::information(this, tr("Wallet encrypted"),
182  tr("Wallet passphrase was successfully changed."));
183  QDialog::accept(); // Success
184  }
185  else
186  {
187  QMessageBox::critical(this, tr("Wallet encryption failed"),
188  tr("The passphrase entered for the wallet decryption was incorrect."));
189  }
190  }
191  else
192  {
193  QMessageBox::critical(this, tr("Wallet encryption failed"),
194  tr("The supplied passphrases do not match."));
195  }
196  break;
197  }
198 }
199 
201 {
202  // Validate input, set Ok button to enabled when acceptable
203  bool acceptable = false;
204  switch(mode)
205  {
206  case Encrypt: // New passphrase x2
207  acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
208  break;
209  case Unlock: // Old passphrase x1
210  case Decrypt:
211  acceptable = !ui->passEdit1->text().isEmpty();
212  break;
213  case ChangePass: // Old passphrase x1, new passphrase x2
214  acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
215  break;
216  }
217  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
218 }
219 
220 bool AskPassphraseDialog::event(QEvent *event)
221 {
222  // Detect Caps Lock key press.
223  if (event->type() == QEvent::KeyPress) {
224  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
225  if (ke->key() == Qt::Key_CapsLock) {
226  fCapsLock = !fCapsLock;
227  }
228  if (fCapsLock) {
229  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
230  } else {
231  ui->capsLabel->clear();
232  }
233  }
234  return QWidget::event(event);
235 }
236 
237 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
238 {
239  /* Detect Caps Lock.
240  * There is no good OS-independent way to check a key state in Qt, but we
241  * can detect Caps Lock by checking for the following condition:
242  * Shift key is down and the result is a lower case character, or
243  * Shift key is not down and the result is an upper case character.
244  */
245  if (event->type() == QEvent::KeyPress) {
246  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
247  QString str = ke->text();
248  if (str.length() != 0) {
249  const QChar *psz = str.unicode();
250  bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
251  if ((fShift && *psz >= 'a' && *psz <= 'z') || (!fShift && *psz >= 'A' && *psz <= 'Z')) {
252  fCapsLock = true;
253  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
254  } else if (psz->isLetter()) {
255  fCapsLock = false;
256  ui->capsLabel->clear();
257  }
258  }
259  }
260  return QDialog::eventFilter(object, event);
261 }
262 
263 static void SecureClearQLineEdit(QLineEdit* edit)
264 {
265  // Attempt to overwrite text so that they do not linger around in memory
266  edit->setText(QString(" ").repeated(edit->text().size()));
267  edit->clear();
268 }
269 
271 {
272  SecureClearQLineEdit(ui->passEdit1);
273  SecureClearQLineEdit(ui->passEdit2);
274  SecureClearQLineEdit(ui->passEdit3);
275 }
Multifunctional dialog to ask for passphrases.
void setModel(WalletModel *model)
@ Unlock
Ask passphrase and unlock.
@ Encrypt
Ask passphrase twice and encrypt.
@ Decrypt
Ask passphrase and decrypt wallet.
@ ChangePass
Ask old passphrase + new passphrase twice.
AskPassphraseDialog(Mode mode, QWidget *parent)
bool event(QEvent *event)
Ui::AskPassphraseDialog * ui
bool eventFilter(QObject *object, QEvent *event)
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:99
bool setWalletEncrypted(bool encrypted, const SecureString &passphrase)
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:56