Wallet decryption support
[novacoin.git] / src / qt / askpassphrasedialog.cpp
1 #include "askpassphrasedialog.h"
2 #include "ui_askpassphrasedialog.h"
3
4 #include "guiconstants.h"
5 #include "dialogwindowflags.h"
6 #include "walletmodel.h"
7
8 #include <QMessageBox>
9 #include <QPushButton>
10 #include <QKeyEvent>
11
12 extern bool fWalletUnlockMintOnly;
13
14 AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
15     QDialog(parent, DIALOGWINDOWHINTS),
16     ui(new Ui::AskPassphraseDialog),
17     mode(mode),
18     model(0),
19     fCapsLock(false)
20 {
21     ui->setupUi(this);
22     ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
23     ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
24     ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
25
26     // Setup Caps Lock detection.
27     ui->passEdit1->installEventFilter(this);
28     ui->passEdit2->installEventFilter(this);
29     ui->passEdit3->installEventFilter(this);
30
31     switch(mode)
32     {
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"));
38             break;
39         case Unlock: // Ask passphrase
40         case UnlockMining:
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"));
47             break;
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"));
55             break;
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."));
59             break;
60     }
61
62     textChanged();
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()));
66 }
67
68 AskPassphraseDialog::~AskPassphraseDialog()
69 {
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()));
74     delete ui;
75 }
76
77 void AskPassphraseDialog::setModel(WalletModel *model)
78 {
79     this->model = model;
80 }
81
82 void AskPassphraseDialog::accept()
83 {
84     SecureString oldpass, newpass1, newpass2;
85     if(!model)
86         return;
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());
95
96     switch(mode)
97     {
98     case Encrypt: {
99         if(newpass1.empty() || newpass2.empty())
100         {
101             // Cannot encrypt with empty passphrase
102             break;
103         }
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)
109         {
110             if(newpass1 == newpass2)
111             {
112                 if(model->setWalletEncrypted(true, newpass1))
113                 {
114                     QMessageBox::warning(this, tr("Wallet encrypted"),
115                                          "<qt>" + 
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.") + 
119                                          "<br><br><b>" + 
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.") + 
124                                          "</b></qt>");
125                     QApplication::quit();
126                 }
127                 else
128                 {
129                     QMessageBox::critical(this, tr("Wallet encryption failed"),
130                                          tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
131                 }
132                 QDialog::accept(); // Success
133             }
134             else
135             {
136                 QMessageBox::critical(this, tr("Wallet encryption failed"),
137                                      tr("The supplied passphrases do not match."));
138             }
139         }
140         else
141         {
142             QDialog::reject(); // Cancelled
143         }
144         } break;
145     case Unlock:
146         if(!model->setWalletLocked(false, oldpass))
147         {
148             QMessageBox::critical(this, tr("Wallet unlock failed"),
149                                   tr("The passphrase entered for the wallet decryption was incorrect."));
150         }
151         else
152         {
153             QDialog::accept(); // Success
154         }
155         break;
156     case UnlockMining:
157         if(!model->setWalletLocked(false, oldpass))
158         {
159             QMessageBox::critical(this, tr("Wallet unlock failed"),
160                                   tr("The passphrase entered for the wallet decryption was incorrect."));
161         }
162         else
163         {
164             QDialog::accept(); // Success
165             fWalletUnlockMintOnly = true;
166         }
167         break;
168     case Decrypt:
169         if(!model->setWalletEncrypted(false, oldpass))
170         {
171             QMessageBox::critical(this, tr("Wallet decryption failed"),
172                                   tr("The passphrase entered for the wallet decryption was incorrect."));
173         }
174         else
175         {
176             QMessageBox::warning(this, tr("Wallet decrypted"),
177                                      "<qt>" + 
178                                      tr("NovaCoin will close now to finish the decryption process. ") +
179                                      "</b></qt>");
180             QApplication::quit();
181         }
182         break;
183     case ChangePass:
184         if(newpass1 == newpass2)
185         {
186             if(model->changePassphrase(oldpass, newpass1))
187             {
188                 QMessageBox::information(this, tr("Wallet encrypted"),
189                                      tr("Wallet passphrase was successfully changed."));
190                 QDialog::accept(); // Success
191             }
192             else
193             {
194                 QMessageBox::critical(this, tr("Wallet encryption failed"),
195                                      tr("The passphrase entered for the wallet decryption was incorrect."));
196             }
197         }
198         else
199         {
200             QMessageBox::critical(this, tr("Wallet encryption failed"),
201                                  tr("The supplied passphrases do not match."));
202         }
203         break;
204     }
205 }
206
207 void AskPassphraseDialog::textChanged()
208 {
209     // Validate input, set Ok button to enabled when acceptable
210     bool acceptable = false;
211     switch(mode)
212     {
213     case Encrypt: // New passphrase x2
214         acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
215         break;
216     case Unlock: // Old passphrase x1
217     case UnlockMining:
218     case Decrypt:
219         acceptable = !ui->passEdit1->text().isEmpty();
220         break;
221     case ChangePass: // Old passphrase x1, new passphrase x2
222         acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
223         break;
224     }
225     ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
226 }
227
228 bool AskPassphraseDialog::event(QEvent *event)
229 {
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;
235         }
236         if (fCapsLock) {
237             ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
238         } else {
239             ui->capsLabel->clear();
240         }
241     }
242     return QWidget::event(event);
243 }
244
245 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
246 {
247     /* Detect Caps Lock.
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.
252      */
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())) {
260                 fCapsLock = true;
261                 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
262             } else if (psz->isLetter()) {
263                 fCapsLock = false;
264                 ui->capsLabel->clear();
265             }
266         }
267     }
268     return QDialog::eventFilter(object, event);
269 }