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