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