1 #include "askpassphrasedialog.h"
2 #include "ui_askpassphrasedialog.h"
4 #include "guiconstants.h"
5 #include "walletmodel.h"
11 AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
13 ui(new Ui::AskPassphraseDialog),
19 ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
20 ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
21 ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
23 // Setup Caps Lock detection.
24 ui->passEdit1->installEventFilter(this);
25 ui->passEdit2->installEventFilter(this);
26 ui->passEdit3->installEventFilter(this);
27 ui->capsLabel->clear();
31 case Encrypt: // Ask passphrase x2
32 ui->passLabel1->hide();
33 ui->passEdit1->hide();
34 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>."));
35 setWindowTitle(tr("Encrypt wallet"));
37 case Unlock: // Ask passphrase
38 ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
39 ui->passLabel2->hide();
40 ui->passEdit2->hide();
41 ui->passLabel3->hide();
42 ui->passEdit3->hide();
43 setWindowTitle(tr("Unlock wallet"));
45 case Decrypt: // Ask passphrase
46 ui->warningLabel->setText(tr("This operation needs your wallet passphrase to decrypt the wallet."));
47 ui->passLabel2->hide();
48 ui->passEdit2->hide();
49 ui->passLabel3->hide();
50 ui->passEdit3->hide();
51 setWindowTitle(tr("Decrypt wallet"));
53 case ChangePass: // Ask old passphrase + new passphrase x2
54 setWindowTitle(tr("Change passphrase"));
55 ui->warningLabel->setText(tr("Enter the old and new passphrase to the wallet."));
60 connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
61 connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
62 connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
65 AskPassphraseDialog::~AskPassphraseDialog()
67 // Attempt to overwrite text so that they do not linger around in memory
68 ui->passEdit1->setText(QString(" ").repeated(ui->passEdit1->text().size()));
69 ui->passEdit2->setText(QString(" ").repeated(ui->passEdit2->text().size()));
70 ui->passEdit3->setText(QString(" ").repeated(ui->passEdit3->text().size()));
74 void AskPassphraseDialog::setModel(WalletModel *model)
79 void AskPassphraseDialog::accept()
81 SecureString oldpass, newpass1, newpass2;
84 oldpass.reserve(MAX_PASSPHRASE_SIZE);
85 newpass1.reserve(MAX_PASSPHRASE_SIZE);
86 newpass2.reserve(MAX_PASSPHRASE_SIZE);
87 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
88 // Alternately, find a way to make this input mlock()'d to begin with.
89 oldpass.assign(ui->passEdit1->text().toStdString().c_str());
90 newpass1.assign(ui->passEdit2->text().toStdString().c_str());
91 newpass2.assign(ui->passEdit3->text().toStdString().c_str());
96 if(newpass1.empty() || newpass2.empty())
98 // Cannot encrypt with empty passphrase
101 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
102 tr("WARNING: If you encrypt your wallet and lose your passphrase, you will <b>LOSE ALL OF YOUR BITCOINS</b>!\nAre you sure you wish to encrypt your wallet?"),
103 QMessageBox::Yes|QMessageBox::Cancel,
104 QMessageBox::Cancel);
105 if(retval == QMessageBox::Yes)
107 if(newpass1 == newpass2)
109 if(model->setWalletEncrypted(true, newpass1))
111 QMessageBox::warning(this, tr("Wallet encrypted"),
112 tr("Bitcoin will close now to finish the encryption process. Remember that encrypting your wallet cannot fully protect your bitcoins from being stolen by malware infecting your computer."));
113 QApplication::quit();
117 QMessageBox::critical(this, tr("Wallet encryption failed"),
118 tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
120 QDialog::accept(); // Success
124 QMessageBox::critical(this, tr("Wallet encryption failed"),
125 tr("The supplied passphrases do not match."));
130 QDialog::reject(); // Cancelled
134 if(!model->setWalletLocked(false, oldpass))
136 QMessageBox::critical(this, tr("Wallet unlock failed"),
137 tr("The passphrase entered for the wallet decryption was incorrect."));
141 QDialog::accept(); // Success
145 if(!model->setWalletEncrypted(false, oldpass))
147 QMessageBox::critical(this, tr("Wallet decryption failed"),
148 tr("The passphrase entered for the wallet decryption was incorrect."));
152 QDialog::accept(); // Success
156 if(newpass1 == newpass2)
158 if(model->changePassphrase(oldpass, newpass1))
160 QMessageBox::information(this, tr("Wallet encrypted"),
161 tr("Wallet passphrase was succesfully changed."));
162 QDialog::accept(); // Success
166 QMessageBox::critical(this, tr("Wallet encryption failed"),
167 tr("The passphrase entered for the wallet decryption was incorrect."));
172 QMessageBox::critical(this, tr("Wallet encryption failed"),
173 tr("The supplied passphrases do not match."));
179 void AskPassphraseDialog::textChanged()
181 // Validate input, set Ok button to enabled when accepable
182 bool acceptable = false;
185 case Encrypt: // New passphrase x2
186 acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
188 case Unlock: // Old passphrase x1
190 acceptable = !ui->passEdit1->text().isEmpty();
192 case ChangePass: // Old passphrase x1, new passphrase x2
193 acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
196 ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
199 bool AskPassphraseDialog::event(QEvent *event)
201 // Detect Caps Lock key press.
202 if (event->type() == QEvent::KeyPress) {
203 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
204 if (ke->key() == Qt::Key_CapsLock) {
205 fCapsLock = !fCapsLock;
208 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on."));
210 ui->capsLabel->clear();
213 return QWidget::event(event);
216 bool AskPassphraseDialog::eventFilter(QObject *, QEvent *event)
219 * There is no good OS-independent way to check a key state in Qt, but we
220 * can detect Caps Lock by checking for the following condition:
221 * Shift key is down and the result is a lower case character, or
222 * Shift key is not down and the result is an upper case character.
224 if (event->type() == QEvent::KeyPress) {
225 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
226 QString str = ke->text();
227 if (str.length() != 0) {
228 const QChar *psz = str.unicode();
229 bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
230 if ((fShift && psz->isLower()) || (!fShift && psz->isUpper())) {
232 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on."));
233 } else if (psz->isLetter()) {
235 ui->capsLabel->clear();