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 QDialog::accept(); // Success
180 if(newpass1 == newpass2)
182 if(model->changePassphrase(oldpass, newpass1))
184 QMessageBox::information(this, tr("Wallet encrypted"),
185 tr("Wallet passphrase was successfully changed."));
186 QDialog::accept(); // Success
190 QMessageBox::critical(this, tr("Wallet encryption failed"),
191 tr("The passphrase entered for the wallet decryption was incorrect."));
196 QMessageBox::critical(this, tr("Wallet encryption failed"),
197 tr("The supplied passphrases do not match."));
203 void AskPassphraseDialog::textChanged()
205 // Validate input, set Ok button to enabled when acceptable
206 bool acceptable = false;
209 case Encrypt: // New passphrase x2
210 acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
212 case Unlock: // Old passphrase x1
215 acceptable = !ui->passEdit1->text().isEmpty();
217 case ChangePass: // Old passphrase x1, new passphrase x2
218 acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
221 ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
224 bool AskPassphraseDialog::event(QEvent *event)
226 // Detect Caps Lock key press.
227 if (event->type() == QEvent::KeyPress) {
228 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
229 if (ke->key() == Qt::Key_CapsLock) {
230 fCapsLock = !fCapsLock;
233 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
235 ui->capsLabel->clear();
238 return QWidget::event(event);
241 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
244 * There is no good OS-independent way to check a key state in Qt, but we
245 * can detect Caps Lock by checking for the following condition:
246 * Shift key is down and the result is a lower case character, or
247 * Shift key is not down and the result is an upper case character.
249 if (event->type() == QEvent::KeyPress) {
250 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
251 QString str = ke->text();
252 if (str.length() != 0) {
253 const QChar *psz = str.unicode();
254 bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
255 if ((fShift && psz->isLower()) || (!fShift && psz->isUpper())) {
257 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
258 } else if (psz->isLetter()) {
260 ui->capsLabel->clear();
264 return QDialog::eventFilter(object, event);