|
| 1 | +#if defined(HAVE_CONFIG_H) |
| 2 | +#include "config/bitcoin-config.h" |
| 3 | +#endif |
| 4 | + |
| 5 | +#include "passwordsettingwidget.h" |
| 6 | +#include "forms/ui_passwordsettingwidget.h" |
| 7 | +#include "guiconstants.h" |
| 8 | +#include "walletmodel.h" |
| 9 | +#include "support/allocators/secure.h" |
| 10 | +#include <QKeyEvent> |
| 11 | +#include <QMessageBox> |
| 12 | +#include <QPushButton> |
| 13 | +#include "cmessagebox.h" |
| 14 | +#include "dpoc/TimeService.h" |
| 15 | +#include <QSettings> |
| 16 | +#include "log/log.h" |
| 17 | +PasswordSettingWidget::PasswordSettingWidget(Mode _mode,QWidget *parent) : |
| 18 | + QWidget(parent), |
| 19 | + ui(new Ui::PasswordSettingWidget) |
| 20 | +{ |
| 21 | + ui->setupUi(this); |
| 22 | + ui->passEdit1->setMinimumSize(ui->passEdit1->sizeHint()); |
| 23 | + ui->passEdit2->setMinimumSize(ui->passEdit2->sizeHint()); |
| 24 | + ui->passEdit3->setMinimumSize(ui->passEdit3->sizeHint()); |
| 25 | + |
| 26 | + ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE); |
| 27 | + ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE); |
| 28 | + ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE); |
| 29 | + |
| 30 | + // Setup Caps Lock detection. |
| 31 | + ui->passEdit1->installEventFilter(this); |
| 32 | + ui->passEdit2->installEventFilter(this); |
| 33 | + ui->passEdit3->installEventFilter(this); |
| 34 | + |
| 35 | + LOG_WRITE(LOG_INFO,"passwordsettingwidget::passwordsettingwidget",QString::number(_mode).toStdString().c_str()); |
| 36 | + switch(_mode) |
| 37 | + { |
| 38 | + case Encrypt: // Ask passphrase x2 |
| 39 | + ui->passEdit3->hide(); |
| 40 | + ui->passEdit3->setVisible(false); |
| 41 | + ui->passEdit1->setPlaceholderText(tr("6-20 characters, suggesting English numerals are mixed")); |
| 42 | + ui->passEdit2->setPlaceholderText(tr("Confirm password")); |
| 43 | + break; |
| 44 | + case Unlock: // Ask passphrase |
| 45 | + ui->passEdit2->hide(); |
| 46 | + ui->passEdit3->hide(); |
| 47 | + break; |
| 48 | + case Decrypt: // Ask passphrase |
| 49 | + ui->passEdit2->hide(); |
| 50 | + ui->passEdit3->hide(); |
| 51 | + break; |
| 52 | + case ChangePass: // Ask old passphrase + new passphrase x2 |
| 53 | + ui->passEdit1->setPlaceholderText(tr("old password")); |
| 54 | + ui->passEdit2->setPlaceholderText(tr("6-20 characters, suggesting English numerals are mixed")); |
| 55 | + ui->passEdit3->setPlaceholderText(tr("Confirm password")); |
| 56 | + break; |
| 57 | + } |
| 58 | + textChanged(); |
| 59 | + connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged())); |
| 60 | + connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged())); |
| 61 | + connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged())); |
| 62 | + ui->label_error->setText(""); |
| 63 | +} |
| 64 | + |
| 65 | +PasswordSettingWidget::~PasswordSettingWidget() |
| 66 | +{ |
| 67 | + delete ui; |
| 68 | +} |
| 69 | +void PasswordSettingWidget::on_pushButton_2_pressed() |
| 70 | +{ |
| 71 | + Q_EMIT back(); |
| 72 | +} |
| 73 | +void PasswordSettingWidget::setMode(Mode _mode) |
| 74 | +{ |
| 75 | + this->mode = _mode; |
| 76 | +} |
| 77 | +void PasswordSettingWidget::setModel(WalletModel *_model) |
| 78 | +{ |
| 79 | + this->model = _model; |
| 80 | +} |
| 81 | +void PasswordSettingWidget::on_pushButton_pressed() |
| 82 | +{ |
| 83 | + LOG_WRITE(LOG_INFO,"passwordsettingwidget::on_pushBUtton_pressed",QString::number(mode).toStdString().c_str()); |
| 84 | + |
| 85 | + if(mode == Encrypt){ |
| 86 | + if(ui->passEdit1->text().isEmpty()){ |
| 87 | + ui->label_error->setText(tr("please set password")); |
| 88 | + return; |
| 89 | + }else if(ui->passEdit2->text().isEmpty()){ |
| 90 | + ui->label_error->setText(tr("please set confirm password")); |
| 91 | + return; |
| 92 | + } |
| 93 | + int size = ui->passEdit1->text().size(); |
| 94 | + std::string temp = ui->passEdit1->text().toStdString(); |
| 95 | + if(size<6||size>20||temp.length()!=size){ |
| 96 | + //ui->label_error->setText(tr("please check password")); |
| 97 | + ui->label_error->setText(tr("The input length is between 6-20,please check password")); |
| 98 | + return; |
| 99 | + } |
| 100 | + }else if(mode == ChangePass){ |
| 101 | + if(ui->passEdit1->text().isEmpty()){ |
| 102 | + ui->label_error->setText(tr("please set old password")); |
| 103 | + return; |
| 104 | + }else if(ui->passEdit2->text().isEmpty()){ |
| 105 | + ui->label_error->setText(tr("please set new password")); |
| 106 | + return; |
| 107 | + }else if(ui->passEdit3->text().isEmpty()){ |
| 108 | + ui->label_error->setText(tr("please set confirm password")); |
| 109 | + return; |
| 110 | + } |
| 111 | + int size = ui->passEdit2->text().size(); |
| 112 | + std::string temp = ui->passEdit2->text().toStdString(); |
| 113 | + if(size<6||size>20){ |
| 114 | + ui->label_error->setText(tr("The input length is between 6-20,please check password")); |
| 115 | + return; |
| 116 | + } |
| 117 | + if(temp.length()!=size){ |
| 118 | + ui->label_error->setText(tr("Inconsistency of input passwords,please check password")); |
| 119 | + return; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + ui->label_error->setText(tr("please wait...")); |
| 124 | + SecureString oldpass, newpass1, newpass2; |
| 125 | + if(!model) |
| 126 | + return; |
| 127 | + oldpass.reserve(MAX_PASSPHRASE_SIZE); |
| 128 | + newpass1.reserve(MAX_PASSPHRASE_SIZE); |
| 129 | + newpass2.reserve(MAX_PASSPHRASE_SIZE); |
| 130 | + |
| 131 | + switch(mode) |
| 132 | + { |
| 133 | + case Encrypt: { |
| 134 | + newpass1.assign(ui->passEdit1->text().toStdString().c_str()); |
| 135 | + newpass2.assign(ui->passEdit2->text().toStdString().c_str()); |
| 136 | + |
| 137 | + if(newpass1.empty() || newpass2.empty()) |
| 138 | + { |
| 139 | + // Cannot encrypt with empty passphrase |
| 140 | + break; |
| 141 | + } |
| 142 | + if(newpass1 == newpass2) |
| 143 | + { |
| 144 | + if(model->setWalletEncrypted(true, newpass1)) |
| 145 | + { |
| 146 | + LOG_WRITE(LOG_INFO,"passwordsettingwidget::Encrypted",QString::number(getTag()).toStdString().c_str()); |
| 147 | + if(2 == getTag()) |
| 148 | + { |
| 149 | + Q_EMIT openSendCoinsAffrimwidget(); |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + ui->label_error->setText(tr("passwordset success")); |
| 154 | + Q_EMIT ChangePasswordSuccess(); |
| 155 | + } |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + ui->label_error->setText(tr("Password mismatch")); |
| 160 | + } |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + ui->label_error->setText(tr("Wallet encryption failed")); |
| 165 | + } |
| 166 | + |
| 167 | + } break; |
| 168 | + case Unlock: |
| 169 | + if(!model->setWalletLocked(false, oldpass)) |
| 170 | + { |
| 171 | + ui->label_error->setText(tr("Wallet unlock failed")); |
| 172 | + } |
| 173 | + break; |
| 174 | + case Decrypt: |
| 175 | + if(!model->setWalletEncrypted(false, oldpass)) |
| 176 | + { |
| 177 | + ui->label_error->setText(tr("Wallet decryption failed")); |
| 178 | + } |
| 179 | + break; |
| 180 | + case ChangePass: |
| 181 | + oldpass.assign(ui->passEdit1->text().toStdString().c_str()); |
| 182 | + newpass1.assign(ui->passEdit2->text().toStdString().c_str()); |
| 183 | + newpass2.assign(ui->passEdit3->text().toStdString().c_str()); |
| 184 | + if(newpass1 == newpass2) |
| 185 | + { |
| 186 | + int64_t timenum = 0; |
| 187 | + int iPasswordErrorNum = 0; |
| 188 | + bool was_locked = (model->getEncryptionStatus() == WalletModel::Locked)?1:0; |
| 189 | + if(was_locked) |
| 190 | + { |
| 191 | + QSettings settings; |
| 192 | + iPasswordErrorNum = settings.value("PasswordErrorNum").toInt(); |
| 193 | + LOG_WRITE(LOG_INFO,"passwordsettingwidget::iPasswordErrorNum",QString::number(iPasswordErrorNum).toStdString().c_str()); |
| 194 | + timenum = timeService.GetCurrentTimeSeconds(); |
| 195 | + if(iPasswordErrorNum >= 5){ |
| 196 | + QString locktime = settings.value("locktime").toString(); |
| 197 | + LOG_WRITE(LOG_INFO,"passwordsettingwidget::timeService::GetCurrentTimeSeconds",QString::number(timenum).toStdString().c_str(),"locktime",locktime.toStdString().c_str()); |
| 198 | + QString strtimenum = QString::number(timenum); |
| 199 | + if(locktime > strtimenum){ |
| 200 | + CMessageBox msg; |
| 201 | + msg.setGeometry(this->x()+(this->width()-msg.width())/2, |
| 202 | + this->y()+(this->height()-msg.height())/2,msg.width(),msg.height()); |
| 203 | + |
| 204 | + msg.setIsClose(false); |
| 205 | + msg.setMessage(2); |
| 206 | + msg.exec(); |
| 207 | + ui->label_error->setText(tr("Wallet unlock failed")); |
| 208 | + return; |
| 209 | + } |
| 210 | + } |
| 211 | + if(model->changePassphrase(oldpass, newpass1)) |
| 212 | + { |
| 213 | + LOG_WRITE(LOG_INFO,"passwordsettingwidget::changePassphrase",QString::number(getTag()).toStdString().c_str()); |
| 214 | + |
| 215 | + settings.setValue("PasswordErrorNum", 0); |
| 216 | + ui->label_error->setText(tr("Wallet passphrase was successfully changed.")); |
| 217 | + if(2 == getTag()) |
| 218 | + { |
| 219 | + Q_EMIT openSendCoinsAffrimwidget(); |
| 220 | + } |
| 221 | + else |
| 222 | + { |
| 223 | + ui->label_error->setText(tr("passwordset success")); |
| 224 | + Q_EMIT ChangePasswordSuccess(); |
| 225 | + } |
| 226 | + } |
| 227 | + else |
| 228 | + { |
| 229 | + timenum = timenum + (int64_t)60*60*24; |
| 230 | + settings.setValue("PasswordErrorNum", iPasswordErrorNum +1); |
| 231 | + settings.setValue("locktime", QString::number(timenum)); |
| 232 | + ui->label_error->setText(tr("Wallet encryption failed,The passphrase entered for the wallet decryption was incorrect.")); |
| 233 | + } |
| 234 | + }else{ |
| 235 | + LOG_WRITE(LOG_INFO,"passwordsettingwidget::no lock"); |
| 236 | + } |
| 237 | + } |
| 238 | + else |
| 239 | + { |
| 240 | + ui->label_error->setText(tr("Wallet encryption failed")); |
| 241 | + secureClearPassFields(); |
| 242 | + } |
| 243 | + break; |
| 244 | + } |
| 245 | +} |
| 246 | +int PasswordSettingWidget::getTag() |
| 247 | +{ |
| 248 | + return m_tag; |
| 249 | +} |
| 250 | +void PasswordSettingWidget::setTag(int tag) |
| 251 | +{ |
| 252 | + m_tag = tag; |
| 253 | +} |
| 254 | +void PasswordSettingWidget::textChanged() |
| 255 | +{ |
| 256 | + // Validate input, set Ok button to enabled when acceptable |
| 257 | + bool acceptable = false; |
| 258 | + switch(mode) |
| 259 | + { |
| 260 | + case Encrypt: |
| 261 | + acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty(); |
| 262 | + break; |
| 263 | + case Unlock: |
| 264 | + case Decrypt: |
| 265 | + break; |
| 266 | + case ChangePass: |
| 267 | + break; |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 271 | +bool PasswordSettingWidget::event(QEvent *event) |
| 272 | +{ |
| 273 | + // Detect Caps Lock key press. |
| 274 | + if (event->type() == QEvent::KeyPress) { |
| 275 | + QKeyEvent *ke = static_cast<QKeyEvent *>(event); |
| 276 | + if (ke->key() == Qt::Key_CapsLock) { |
| 277 | + fCapsLock = !fCapsLock; |
| 278 | + } |
| 279 | + } |
| 280 | + return QWidget::event(event); |
| 281 | +} |
| 282 | + |
| 283 | +static void SecureClearQLineEdit(QLineEdit* edit) |
| 284 | +{ |
| 285 | + // Attempt to overwrite text so that they do not linger around in memory |
| 286 | + edit->setText(QString(" ").repeated(edit->text().size())); |
| 287 | + edit->clear(); |
| 288 | +} |
| 289 | + |
| 290 | +void PasswordSettingWidget::secureClearPassFields() |
| 291 | +{ |
| 292 | + SecureClearQLineEdit(ui->passEdit2); |
| 293 | + SecureClearQLineEdit(ui->passEdit3); |
| 294 | +} |
| 295 | + |
0 commit comments