1 #include "askpassphrasedialog.h"
2 #include "ui_askpassphrasedialog.h"
4 #include "guiconstants.h"
5 #include "walletmodel.h"
11 extern bool fWalletUnlockMintOnly;
13 AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
15 ui(new Ui::AskPassphraseDialog),
21 ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
22 ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
23 ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
25 // Setup Caps Lock detection.
26 ui->passEdit1->installEventFilter(this);
27 ui->passEdit2->installEventFilter(this);
28 ui->passEdit3->installEventFilter(this);
32 case Encrypt: // Ask passphrase x2
33 ui->passLabel1->hide();
34 ui->passEdit1->hide();
35 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>."));
36 setWindowTitle(tr("Encrypt wallet"));
38 case Unlock: // Ask passphrase
40 ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
41 ui->passLabel2->hide();
42 ui->passEdit2->hide();
43 ui->passLabel3->hide();
44 ui->passEdit3->hide();
45 setWindowTitle(tr("Unlock wallet"));
47 case Decrypt: // Ask passphrase
48 ui->warningLabel->setText(tr("This operation needs your wallet passphrase to decrypt the wallet."));
49 ui->passLabel2->hide();
50 ui->passEdit2->hide();
51 ui->passLabel3->hide();
52 ui->passEdit3->hide();
53 setWindowTitle(tr("Decrypt wallet"));
55 case ChangePass: // Ask old passphrase + new passphrase x2
56 setWindowTitle(tr("Change passphrase"));
57 ui->warningLabel->setText(tr("Enter the old and new passphrase to the wallet."));
62 connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
63 connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
64 connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
67 AskPassphraseDialog::~AskPassphraseDialog()
69 // Attempt to overwrite text so that they do not linger around in memory
70 ui->passEdit1->setText(QString(" ").repeated(ui->passEdit1->text().size()));
71 ui->passEdit2->setText(QString(" ").repeated(ui->passEdit2->text().size()));
72 ui->passEdit3->setText(QString(" ").repeated(ui->passEdit3->text().size()));
76 void AskPassphraseDialog::setModel(WalletModel *model)
81 void AskPassphraseDialog::accept()
83 SecureString oldpass, newpass1, newpass2;
86 oldpass.reserve(MAX_PASSPHRASE_SIZE);
87 newpass1.reserve(MAX_PASSPHRASE_SIZE);
88 newpass2.reserve(MAX_PASSPHRASE_SIZE);
89 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
90 // Alternately, find a way to make this input mlock()'d to begin with.
91 oldpass.assign(ui->passEdit1->text().toStdString().c_str());
92 newpass1.assign(ui->passEdit2->text().toStdString().c_str());
93 newpass2.assign(ui->passEdit3->text().toStdString().c_str());
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 QDialog::accept(); // Success
179 if(newpass1 == newpass2)
181 if(model->changePassphrase(oldpass, newpass1))
183 QMessageBox::information(this, tr("Wallet encrypted"),
184 tr("Wallet passphrase was successfully changed."));
185 QDialog::accept(); // Success
189 QMessageBox::critical(this, tr("Wallet encryption failed"),
190 tr("The passphrase entered for the wallet decryption was incorrect."));
195 QMessageBox::critical(this, tr("Wallet encryption failed"),
196 tr("The supplied passphrases do not match."));
202 void AskPassphraseDialog::textChanged()
204 // Validate input, set Ok button to enabled when acceptable
205 bool acceptable = false;
208 case Encrypt: // New passphrase x2
209 acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
211 case Unlock: // Old passphrase x1
214 acceptable = !ui->passEdit1->text().isEmpty();
216 case ChangePass: // Old passphrase x1, new passphrase x2
217 acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
220 ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
223 bool AskPassphraseDialog::event(QEvent *event)
225 // Detect Caps Lock key press.
226 if (event->type() == QEvent::KeyPress) {
227 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
228 if (ke->key() == Qt::Key_CapsLock) {
229 fCapsLock = !fCapsLock;
232 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
234 ui->capsLabel->clear();
237 return QWidget::event(event);
240 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
243 * There is no good OS-independent way to check a key state in Qt, but we
244 * can detect Caps Lock by checking for the following condition:
245 * Shift key is down and the result is a lower case character, or
246 * Shift key is not down and the result is an upper case character.
248 if (event->type() == QEvent::KeyPress) {
249 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
250 QString str = ke->text();
251 if (str.length() != 0) {
252 const QChar *psz = str.unicode();
253 bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
254 if ((fShift && psz->isLower()) || (!fShift && psz->isUpper())) {
256 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
257 } else if (psz->isLetter()) {
259 ui->capsLabel->clear();
263 return QDialog::eventFilter(object, event);