BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
askpassphrasedialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2018 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 
10 #include <qt/forms/ui_askpassphrasedialog.h>
11 
12 #include <qt/guiconstants.h>
13 #include <qt/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->toggleShowPasswordButton, &QPushButton::toggled, this, &AskPassphraseDialog::toggleShowPassword);
74  connect(ui->passEdit1, &QLineEdit::textChanged, this, &AskPassphraseDialog::textChanged);
75  connect(ui->passEdit2, &QLineEdit::textChanged, this, &AskPassphraseDialog::textChanged);
76  connect(ui->passEdit3, &QLineEdit::textChanged, this, &AskPassphraseDialog::textChanged);
77 }
78 
80 {
82  delete ui;
83 }
84 
86 {
87  this->model = _model;
88 }
89 
91 {
92  SecureString oldpass, newpass1, newpass2;
93  if(!model)
94  return;
95  oldpass.reserve(MAX_PASSPHRASE_SIZE);
96  newpass1.reserve(MAX_PASSPHRASE_SIZE);
97  newpass2.reserve(MAX_PASSPHRASE_SIZE);
98  // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
99  // Alternately, find a way to make this input mlock()'d to begin with.
100  oldpass.assign(ui->passEdit1->text().toStdString().c_str());
101  newpass1.assign(ui->passEdit2->text().toStdString().c_str());
102  newpass2.assign(ui->passEdit3->text().toStdString().c_str());
103 
105 
106  switch(mode)
107  {
108  case Encrypt: {
109  if(newpass1.empty() || newpass2.empty())
110  {
111  // Cannot encrypt with empty passphrase
112  break;
113  }
114  QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
115  tr("Warning: If you encrypt your wallet and lose your passphrase, you will <b>LOSE ALL OF YOUR BITCOINS</b>!") + "<br><br>" + tr("Are you sure you wish to encrypt your wallet?"),
116  QMessageBox::Yes|QMessageBox::Cancel,
117  QMessageBox::Cancel);
118  if(retval == QMessageBox::Yes)
119  {
120  if(newpass1 == newpass2)
121  {
122  if(model->setWalletEncrypted(true, newpass1))
123  {
124  QMessageBox::warning(this, tr("Wallet encrypted"),
125  "<qt>" +
126  tr("Your wallet is now encrypted. "
127  "Remember that encrypting your wallet cannot fully protect "
128  "your bitcoins from being stolen by malware infecting your computer.") +
129  "<br><br><b>" +
130  tr("IMPORTANT: Any previous backups you have made of your wallet file "
131  "should be replaced with the newly generated, encrypted wallet file. "
132  "For security reasons, previous backups of the unencrypted wallet file "
133  "will become useless as soon as you start using the new, encrypted wallet.") +
134  "</b></qt>");
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 
238 {
239  ui->toggleShowPasswordButton->setDown(show);
240  const auto mode = show ? QLineEdit::Normal : QLineEdit::Password;
241  ui->passEdit1->setEchoMode(mode);
242  ui->passEdit2->setEchoMode(mode);
243  ui->passEdit3->setEchoMode(mode);
244 }
245 
246 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
247 {
248  /* Detect Caps Lock.
249  * There is no good OS-independent way to check a key state in Qt, but we
250  * can detect Caps Lock by checking for the following condition:
251  * Shift key is down and the result is a lower case character, or
252  * Shift key is not down and the result is an upper case character.
253  */
254  if (event->type() == QEvent::KeyPress) {
255  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
256  QString str = ke->text();
257  if (str.length() != 0) {
258  const QChar *psz = str.unicode();
259  bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
260  if ((fShift && *psz >= 'a' && *psz <= 'z') || (!fShift && *psz >= 'A' && *psz <= 'Z')) {
261  fCapsLock = true;
262  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
263  } else if (psz->isLetter()) {
264  fCapsLock = false;
265  ui->capsLabel->clear();
266  }
267  }
268  }
269  return QDialog::eventFilter(object, event);
270 }
271 
272 static void SecureClearQLineEdit(QLineEdit* edit)
273 {
274  // Attempt to overwrite text so that they do not linger around in memory
275  edit->setText(QString(" ").repeated(edit->text().size()));
276  edit->clear();
277 }
278 
280 {
281  SecureClearQLineEdit(ui->passEdit1);
282  SecureClearQLineEdit(ui->passEdit2);
283  SecureClearQLineEdit(ui->passEdit3);
284 }
bool event(QEvent *event)
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:56
Ask passphrase twice and encrypt.
Ask passphrase and unlock.
Ui::AskPassphraseDialog * ui
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
AddressTableModel * parent
AskPassphraseDialog(Mode mode, QWidget *parent)
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:125
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)