1 #include "askpassphrasedialog.h"
2 #include "ui_askpassphrasedialog.h"
4 #include "guiconstants.h"
5 #include "walletmodel.h"
10 AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
12 ui(new Ui::AskPassphraseDialog),
17 ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
18 ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
19 ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
23 case Encrypt: // Ask passphrase x2
24 ui->passLabel1->hide();
25 ui->passEdit1->hide();
26 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>."));
27 setWindowTitle(tr("Encrypt wallet"));
29 case Unlock: // Ask passphrase
30 ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
31 ui->passLabel2->hide();
32 ui->passEdit2->hide();
33 ui->passLabel3->hide();
34 ui->passEdit3->hide();
35 setWindowTitle(tr("Unlock wallet"));
37 case Decrypt: // Ask passphrase
38 ui->warningLabel->setText(tr("This operation needs your wallet passphrase to decrypt the wallet."));
39 ui->passLabel2->hide();
40 ui->passEdit2->hide();
41 ui->passLabel3->hide();
42 ui->passEdit3->hide();
43 setWindowTitle(tr("Decrypt wallet"));
45 case ChangePass: // Ask old passphrase + new passphrase x2
46 setWindowTitle(tr("Change passphrase"));
47 ui->warningLabel->setText(tr("Enter the old and new passphrase to the wallet."));
50 resize(minimumSize()); // Get rid of extra space in dialog
53 connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
54 connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
55 connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
58 AskPassphraseDialog::~AskPassphraseDialog()
60 // Attempt to overwrite text so that they do not linger around in memory
61 ui->passEdit1->setText(QString(" ").repeated(ui->passEdit1->text().size()));
62 ui->passEdit2->setText(QString(" ").repeated(ui->passEdit2->text().size()));
63 ui->passEdit3->setText(QString(" ").repeated(ui->passEdit3->text().size()));
67 void AskPassphraseDialog::setModel(WalletModel *model)
72 void AskPassphraseDialog::accept()
74 SecureString oldpass, newpass1, newpass2;
77 oldpass.reserve(MAX_PASSPHRASE_SIZE);
78 newpass1.reserve(MAX_PASSPHRASE_SIZE);
79 newpass2.reserve(MAX_PASSPHRASE_SIZE);
80 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
81 // Alternately, find a way to make this input mlock()'d to begin with.
82 oldpass.assign(ui->passEdit1->text().toStdString().c_str());
83 newpass1.assign(ui->passEdit2->text().toStdString().c_str());
84 newpass2.assign(ui->passEdit3->text().toStdString().c_str());
89 if(newpass1.empty() || newpass2.empty())
91 // Cannot encrypt with empty passphrase
94 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
95 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?"),
96 QMessageBox::Yes|QMessageBox::Cancel,
98 if(retval == QMessageBox::Yes)
100 if(newpass1 == newpass2)
102 if(model->setWalletEncrypted(true, newpass1))
104 QMessageBox::warning(this, tr("Wallet encrypted"),
105 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."));
106 QApplication::quit();
110 QMessageBox::critical(this, tr("Wallet encryption failed"),
111 tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
113 QDialog::accept(); // Success
117 QMessageBox::critical(this, tr("Wallet encryption failed"),
118 tr("The supplied passphrases do not match."));
123 QDialog::reject(); // Cancelled
127 if(!model->setWalletLocked(false, oldpass))
129 QMessageBox::critical(this, tr("Wallet unlock failed"),
130 tr("The passphrase entered for the wallet decryption was incorrect."));
134 QDialog::accept(); // Success
138 if(!model->setWalletEncrypted(false, oldpass))
140 QMessageBox::critical(this, tr("Wallet decryption failed"),
141 tr("The passphrase entered for the wallet decryption was incorrect."));
145 QDialog::accept(); // Success
149 if(newpass1 == newpass2)
151 if(model->changePassphrase(oldpass, newpass1))
153 QMessageBox::information(this, tr("Wallet encrypted"),
154 tr("Wallet passphrase was succesfully changed."));
155 QDialog::accept(); // Success
159 QMessageBox::critical(this, tr("Wallet encryption failed"),
160 tr("The passphrase entered for the wallet decryption was incorrect."));
165 QMessageBox::critical(this, tr("Wallet encryption failed"),
166 tr("The supplied passphrases do not match."));
172 void AskPassphraseDialog::textChanged()
174 // Validate input, set Ok button to enabled when accepable
175 bool acceptable = false;
178 case Encrypt: // New passphrase x2
179 acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
181 case Unlock: // Old passphrase x1
183 acceptable = !ui->passEdit1->text().isEmpty();
185 case ChangePass: // Old passphrase x1, new passphrase x2
186 acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
189 ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);