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 // Attempt to overwrite text so that they do not linger around in memory
71 ui->passEdit1->setText(QString(" ").repeated(ui->passEdit1->text().size()));
72 ui->passEdit2->setText(QString(" ").repeated(ui->passEdit2->text().size()));
73 ui->passEdit3->setText(QString(" ").repeated(ui->passEdit3->text().size()));
77 void AskPassphraseDialog::setModel(WalletModel *model)
82 void AskPassphraseDialog::accept()
84 SecureString oldpass, newpass1, newpass2;
87 oldpass.reserve(MAX_PASSPHRASE_SIZE);
88 newpass1.reserve(MAX_PASSPHRASE_SIZE);
89 newpass2.reserve(MAX_PASSPHRASE_SIZE);
90 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
91 // Alternately, find a way to make this input mlock()'d to begin with.
92 oldpass.assign(ui->passEdit1->text().toStdString().c_str());
93 newpass1.assign(ui->passEdit2->text().toStdString().c_str());
94 newpass2.assign(ui->passEdit3->text().toStdString().c_str());
99 if(newpass1.empty() || newpass2.empty())
101 // Cannot encrypt with empty passphrase
104 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
105 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?"),
106 QMessageBox::Yes|QMessageBox::Cancel,
107 QMessageBox::Cancel);
108 if(retval == QMessageBox::Yes)
110 if(newpass1 == newpass2)
112 if(model->setWalletEncrypted(true, newpass1))
114 QMessageBox::warning(this, tr("Wallet encrypted"),
116 tr("NovaCoin will close now to finish the encryption process. "
117 "Remember that encrypting your wallet cannot fully protect "
118 "your coins from being stolen by malware infecting your computer.") +
120 tr("IMPORTANT: Any previous backups you have made of your wallet file "
121 "should be replaced with the newly generated, encrypted wallet file. "
122 "For security reasons, previous backups of the unencrypted wallet file "
123 "will become useless as soon as you start using the new, encrypted wallet.") +
125 QApplication::quit();
129 QMessageBox::critical(this, tr("Wallet encryption failed"),
130 tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
132 QDialog::accept(); // Success
136 QMessageBox::critical(this, tr("Wallet encryption failed"),
137 tr("The supplied passphrases do not match."));
142 QDialog::reject(); // Cancelled
146 if(!model->setWalletLocked(false, oldpass))
148 QMessageBox::critical(this, tr("Wallet unlock failed"),
149 tr("The passphrase entered for the wallet decryption was incorrect."));
153 QDialog::accept(); // Success
157 if(!model->setWalletLocked(false, oldpass))
159 QMessageBox::critical(this, tr("Wallet unlock failed"),
160 tr("The passphrase entered for the wallet decryption was incorrect."));
164 QDialog::accept(); // Success
165 fWalletUnlockMintOnly = true;
169 if(!model->setWalletEncrypted(false, oldpass))
171 QMessageBox::critical(this, tr("Wallet decryption failed"),
172 tr("The passphrase entered for the wallet decryption was incorrect."));
176 QMessageBox::warning(this, tr("Wallet decrypted"),
178 tr("NovaCoin will close now to finish the decryption process. ") +
180 QApplication::quit();
184 if(newpass1 == newpass2)
186 if(model->changePassphrase(oldpass, newpass1))
188 QMessageBox::information(this, tr("Wallet encrypted"),
189 tr("Wallet passphrase was successfully changed."));
190 QDialog::accept(); // Success
194 QMessageBox::critical(this, tr("Wallet encryption failed"),
195 tr("The passphrase entered for the wallet decryption was incorrect."));
200 QMessageBox::critical(this, tr("Wallet encryption failed"),
201 tr("The supplied passphrases do not match."));
207 void AskPassphraseDialog::textChanged()
209 // Validate input, set Ok button to enabled when acceptable
210 bool acceptable = false;
213 case Encrypt: // New passphrase x2
214 acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
216 case Unlock: // Old passphrase x1
219 acceptable = !ui->passEdit1->text().isEmpty();
221 case ChangePass: // Old passphrase x1, new passphrase x2
222 acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
225 ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
228 bool AskPassphraseDialog::event(QEvent *event)
230 // Detect Caps Lock key press.
231 if (event->type() == QEvent::KeyPress) {
232 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
233 if (ke->key() == Qt::Key_CapsLock) {
234 fCapsLock = !fCapsLock;
237 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
239 ui->capsLabel->clear();
242 return QWidget::event(event);
245 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
248 * There is no good OS-independent way to check a key state in Qt, but we
249 * can detect Caps Lock by checking for the following condition:
250 * Shift key is down and the result is a lower case character, or
251 * Shift key is not down and the result is an upper case character.
253 if (event->type() == QEvent::KeyPress) {
254 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
255 QString str = ke->text();
256 if (str.length() != 0) {
257 const QChar *psz = str.unicode();
258 bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
259 if ((fShift && psz->isLower()) || (!fShift && psz->isUpper())) {
261 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
262 } else if (psz->isLetter()) {
264 ui->capsLabel->clear();
268 return QDialog::eventFilter(object, event);