1 #include "askpassphrasedialog.h"
2 #include "ui_askpassphrasedialog.h"
4 #include "guiconstants.h"
5 #include "dialogwindowflags.h"
6 #include "walletmodel.h"
12 extern bool fWalletUnlockMintOnly;
14 AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
15 QDialog(parent, DIALOGWINDOWHINTS),
16 ui(new Ui::AskPassphraseDialog),
22 ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
23 ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
24 ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
26 // Setup Caps Lock detection.
27 ui->passEdit1->installEventFilter(this);
28 ui->passEdit2->installEventFilter(this);
29 ui->passEdit3->installEventFilter(this);
33 case Encrypt: // Ask passphrase x2
34 ui->passLabel1->hide();
35 ui->passEdit1->hide();
36 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>."));
37 setWindowTitle(tr("Encrypt wallet"));
39 case Unlock: // Ask passphrase
41 ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
42 ui->passLabel2->hide();
43 ui->passEdit2->hide();
44 ui->passLabel3->hide();
45 ui->passEdit3->hide();
46 setWindowTitle(tr("Unlock wallet"));
48 case Decrypt: // Ask passphrase
49 ui->warningLabel->setText(tr("This operation needs your wallet passphrase to decrypt the wallet."));
50 ui->passLabel2->hide();
51 ui->passEdit2->hide();
52 ui->passLabel3->hide();
53 ui->passEdit3->hide();
54 setWindowTitle(tr("Decrypt wallet"));
56 case ChangePass: // Ask old passphrase + new passphrase x2
57 setWindowTitle(tr("Change passphrase"));
58 ui->warningLabel->setText(tr("Enter the old and new passphrase to the wallet."));
63 connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
64 connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
65 connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
68 AskPassphraseDialog::~AskPassphraseDialog()
70 secureClearPassFields();
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());
93 secureClearPassFields();
98 if(newpass1.empty() || newpass2.empty())
100 // Cannot encrypt with empty passphrase
103 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
104 tr("Warning: If you encrypt your wallet and lose your passphrase, you will <b>LOSE ALL OF YOUR COINS</b>!") + "<br><br>" + tr("Are you sure you wish to encrypt your wallet?"),
105 QMessageBox::Yes|QMessageBox::Cancel,
106 QMessageBox::Cancel);
107 if(retval == QMessageBox::Yes)
109 if(newpass1 == newpass2)
111 if(model->setWalletEncrypted(true, newpass1))
113 QMessageBox::warning(this, tr("Wallet encrypted"),
115 tr("NovaCoin will close now to finish the encryption process. "
116 "Remember that encrypting your wallet cannot fully protect "
117 "your coins from being stolen by malware infecting your computer.") +
119 tr("IMPORTANT: Any previous backups you have made of your wallet file "
120 "should be replaced with the newly generated, encrypted wallet file. "
121 "For security reasons, previous backups of the unencrypted wallet file "
122 "will become useless as soon as you start using the new, encrypted wallet.") +
124 QApplication::quit();
128 QMessageBox::critical(this, tr("Wallet encryption failed"),
129 tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
131 QDialog::accept(); // Success
135 QMessageBox::critical(this, tr("Wallet encryption failed"),
136 tr("The supplied passphrases do not match."));
141 QDialog::reject(); // Cancelled
145 if(!model->setWalletLocked(false, oldpass))
147 QMessageBox::critical(this, tr("Wallet unlock failed"),
148 tr("The passphrase entered for the wallet decryption was incorrect."));
152 QDialog::accept(); // Success
156 if(!model->setWalletLocked(false, oldpass))
158 QMessageBox::critical(this, tr("Wallet unlock failed"),
159 tr("The passphrase entered for the wallet decryption was incorrect."));
163 QDialog::accept(); // Success
164 fWalletUnlockMintOnly = true;
168 if(!model->setWalletEncrypted(false, oldpass))
170 QMessageBox::critical(this, tr("Wallet decryption failed"),
171 tr("The passphrase entered for the wallet decryption was incorrect."));
175 QMessageBox::warning(this, tr("Wallet decrypted"),
177 tr("NovaCoin will close now to finish the decryption process. ") +
179 QApplication::quit();
183 if(newpass1 == newpass2)
185 if(model->changePassphrase(oldpass, newpass1))
187 QMessageBox::information(this, tr("Wallet encrypted"),
188 tr("Wallet passphrase was successfully changed."));
189 QDialog::accept(); // Success
193 QMessageBox::critical(this, tr("Wallet encryption failed"),
194 tr("The passphrase entered for the wallet decryption was incorrect."));
199 QMessageBox::critical(this, tr("Wallet encryption failed"),
200 tr("The supplied passphrases do not match."));
206 void AskPassphraseDialog::textChanged()
208 // Validate input, set Ok button to enabled when acceptable
209 bool acceptable = false;
212 case Encrypt: // New passphrase x2
213 acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
215 case Unlock: // Old passphrase x1
218 acceptable = !ui->passEdit1->text().isEmpty();
220 case ChangePass: // Old passphrase x1, new passphrase x2
221 acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
224 ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
227 bool AskPassphraseDialog::event(QEvent *event)
229 // Detect Caps Lock key press.
230 if (event->type() == QEvent::KeyPress) {
231 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
232 if (ke->key() == Qt::Key_CapsLock) {
233 fCapsLock = !fCapsLock;
236 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
238 ui->capsLabel->clear();
241 return QWidget::event(event);
244 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
247 * There is no good OS-independent way to check a key state in Qt, but we
248 * can detect Caps Lock by checking for the following condition:
249 * Shift key is down and the result is a lower case character, or
250 * Shift key is not down and the result is an upper case character.
252 if (event->type() == QEvent::KeyPress) {
253 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
254 QString str = ke->text();
255 if (str.length() != 0) {
256 const QChar *psz = str.unicode();
257 bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
258 if ((fShift && psz->isLower()) || (!fShift && psz->isUpper())) {
260 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
261 } else if (psz->isLetter()) {
263 ui->capsLabel->clear();
267 return QDialog::eventFilter(object, event);
270 static void SecureClearQLineEdit(QLineEdit* edit)
272 // Attempt to overwrite text so that they do not linger around in memory
273 edit->setText(QString(" ").repeated(edit->text().size()));
277 void AskPassphraseDialog::secureClearPassFields()
279 SecureClearQLineEdit(ui->passEdit1);
280 SecureClearQLineEdit(ui->passEdit2);
281 SecureClearQLineEdit(ui->passEdit3);