PRCYCoin  2.0.0.7rc1
P2P Digital Currency
encryptdialog.cpp
Go to the documentation of this file.
1 #include "encryptdialog.h"
2 #include "ui_encryptdialog.h"
3 #include "guiutil.h"
4 #include "guiconstants.h"
5 #include "bitcoingui.h"
6 #include "zxcvbn.h"
7 
8 #include <QMessageBox>
9 #include <QCloseEvent>
10 
11 EncryptDialog::EncryptDialog(QWidget *parent) :
12  QDialog(parent, Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
13  ui(new Ui::EncryptDialog)
14 {
15  ui->setupUi(this);
16 
17  connect(ui->linePwd, SIGNAL(textChanged(const QString &)), this, SLOT(validateNewPass()));
18  connect(ui->linePwdConfirm, SIGNAL(textChanged(const QString &)), this, SLOT(validateNewPassRepeat()));
19  connect(ui->btnOK, SIGNAL(clicked()), this, SLOT(on_acceptPassphrase()));
20  connect(ui->btnCancel, SIGNAL(clicked()), this, SLOT(on_btnCancel()));
21  connect(ui->showPassphraseCheckBox, SIGNAL(clicked()), this, SLOT(on_showPassphraseCheckBox_clicked()));
22 }
23 
25 {
26  delete ui;
27 }
28 
30 {
31  this->model = model;
32 }
33 
34 void EncryptDialog::closeEvent (QCloseEvent *event)
35 {
36  QMessageBox::StandardButton reply;
37  reply = QMessageBox::warning(this, "Wallet Encryption Required", "There was no passphrase entered for the wallet.\n\nWallet encryption is required for the security of your funds.\n\nWhat would you like to do?", QMessageBox::Retry|QMessageBox::Close);
38  if (reply == QMessageBox::Retry) {
39  event->ignore();
40  } else {
41  QApplication::quit();
42  }
43 }
44 
46 {
47  QMessageBox::StandardButton reply;
48  reply = QMessageBox::warning(this, "Wallet Encryption Required", "There was no passphrase entered for the wallet.\n\nWallet encryption is required for the security of your funds.\n\nWhat would you like to do?", QMessageBox::Retry|QMessageBox::Close);
49  if (reply == QMessageBox::Retry) {
50  return;
51  } else {
52  QApplication::quit();
53  }
54 }
55 
57  SecureString newPass = SecureString();
58  newPass.reserve(MAX_PASSPHRASE_SIZE);
59  newPass.assign( ui->linePwd->text().toStdString().c_str() );
60 
61  SecureString newPass2 = SecureString();
62  newPass2.reserve(MAX_PASSPHRASE_SIZE);
63  newPass2.assign(ui->linePwdConfirm->text().toStdString().c_str() );
64 
65  if ( (!ui->linePwd->text().length()) || (!ui->linePwdConfirm->text().length()) ) {
66  QMessageBox msgBox;
67  msgBox.setWindowTitle("Wallet Encryption Failed");
68  msgBox.setText("The passphrase entered for wallet encryption was empty. Please try again.");
69  msgBox.setStyleSheet(GUIUtil::loadStyleSheet());
70  msgBox.setIcon(QMessageBox::Critical);
71  msgBox.exec();
72  return;
73  }
74 
75  if (newPass == newPass2) {
76  if (newPass.length() < 10) {
77  QMessageBox msgBox;
78  msgBox.setWindowTitle("Wallet Encryption Failed");
79  msgBox.setText("The passphrase's length has to be more than 10. Please try again.");
80  msgBox.setStyleSheet(GUIUtil::loadStyleSheet());
81  msgBox.setIcon(QMessageBox::Critical);
82  msgBox.exec();
83  return;
84  }
85 
86  if (!pwalletMain->checkPassPhraseRule(newPass.c_str())) {
87  QMessageBox msgBox;
88  msgBox.setWindowTitle("Wallet Encryption Failed");
89  msgBox.setText("The passphrase must contain lower, upper, digit, symbol. Please try again.");
90  msgBox.setStyleSheet(GUIUtil::loadStyleSheet());
91  msgBox.setIcon(QMessageBox::Critical);
92  msgBox.exec();
93  return;
94  }
95 
96  double guesses;
97  int ret = zxcvbn_password_strength(newPass.c_str(), NULL, &guesses, NULL);
98  if (ret < 0 || guesses < 10000) {
99  QMessageBox msgBox;
100  msgBox.setWindowTitle("Wallet Encryption Failed");
101  msgBox.setText("The passphrases entered for wallet encryption is too weak. Please try again.");
102  msgBox.setStyleSheet(GUIUtil::loadStyleSheet());
103  msgBox.setIcon(QMessageBox::Critical);
104  msgBox.exec();
105  return;
106  }
107 
108  if (model->setWalletEncrypted(true, newPass)) {
110  model->setWalletLocked(false, newPass);
111  QMessageBox msgBox;
112  msgBox.setWindowTitle("Wallet Encryption Successful");
113  msgBox.setText("Wallet passphrase was successfully set.\nPlease remember your passphrase as there is no way to recover it.");
114  msgBox.setStyleSheet(GUIUtil::loadStyleSheet());
115  msgBox.setIcon(QMessageBox::Information);
116  msgBox.exec();
117  accept();
118  }
119  } else {
120  QMessageBox msgBox;
121  msgBox.setWindowTitle("Wallet Encryption Failed");
122  msgBox.setText("The passphrases entered for wallet encryption do not match. Please try again.");
123  msgBox.setStyleSheet(GUIUtil::loadStyleSheet());
124  msgBox.setIcon(QMessageBox::Critical);
125  msgBox.exec();
126  return;
127  }
128 }
129 
131 {
132  if (!ui->linePwd->text().length())
133  ui->linePwd->setStyleSheet("border-color: red");
134  else ui->linePwd->setStyleSheet(GUIUtil::loadStyleSheet());
136  ui->linePwd->repaint();
137 }
138 
140 {
142 }
143 
145 {
146  if (ui->linePwd->text()==ui->linePwdConfirm->text())
147  {
148  ui->linePwdConfirm->setStyleSheet(GUIUtil::loadStyleSheet());
149  ui->linePwdConfirm->repaint();
150  return true;
151  } else
152  {
153  ui->linePwdConfirm->setStyleSheet("border-color: red");
154  ui->linePwdConfirm->repaint();
155  return false;
156  }
157 }
158 
160 {
161  ui->linePwd->setEchoMode(ui->showPassphraseCheckBox->checkState() == Qt::Checked ? QLineEdit::Normal : QLineEdit::Password);
162  ui->linePwdConfirm->setEchoMode(ui->showPassphraseCheckBox->checkState() == Qt::Checked ? QLineEdit::Normal : QLineEdit::Password);
163 }
164 
EncryptDialog::on_showPassphraseCheckBox_clicked
void on_showPassphraseCheckBox_clicked()
Definition: encryptdialog.cpp:159
CWallet::checkPassPhraseRule
bool checkPassPhraseRule(const char *pass)
Definition: wallet.cpp:187
EncryptDialog::setModel
void setModel(WalletModel *model)
Definition: encryptdialog.cpp:29
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:102
zxcvbn_password_strength
int zxcvbn_password_strength(const char *password, const char *const *user_inputs, zxcvbn_guesses_t *guesses, zxcvbn_match_sequence_t *pmseq)
Definition: zxcvbn.cpp:13
EncryptDialog::on_btnCancel
void on_btnCancel()
Definition: encryptdialog.cpp:45
EncryptDialog::~EncryptDialog
~EncryptDialog()
Definition: encryptdialog.cpp:24
zxcvbn.h
EncryptDialog::validateNewPassRepeat
void validateNewPassRepeat()
Definition: encryptdialog.cpp:139
encryptdialog.h
EncryptDialog
Definition: encryptdialog.h:11
EncryptDialog::model
WalletModel * model
Definition: encryptdialog.h:31
SecureString
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: allocators.h:262
guiutil.h
EncryptDialog::on_acceptPassphrase
void on_acceptPassphrase()
Definition: encryptdialog.cpp:56
EncryptDialog::closeEvent
void closeEvent(QCloseEvent *event)
Definition: encryptdialog.cpp:34
Ui
Definition: 2faconfirmdialog.h:7
WalletModel::setWalletEncrypted
bool setWalletEncrypted(bool encrypted, const SecureString &passphrase)
Definition: walletmodel.cpp:386
guiconstants.h
EncryptDialog::validateNewPass
void validateNewPass()
Definition: encryptdialog.cpp:130
EncryptDialog::EncryptDialog
EncryptDialog(QWidget *parent=0)
Definition: encryptdialog.cpp:11
EncryptDialog::matchNewPasswords
bool matchNewPasswords()
Definition: encryptdialog.cpp:144
WalletModel::setWalletLocked
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString(), bool stakingOnly=false)
Definition: walletmodel.cpp:397
bitcoingui.h
GUIUtil::loadStyleSheet
QString loadStyleSheet()
Load global CSS theme.
Definition: guiutil.cpp:721
pwalletMain
CWallet * pwalletMain
Definition: wallet.cpp:49
EncryptDialog::ui
Ui::EncryptDialog * ui
Definition: encryptdialog.h:30
CWallet::nTimeFirstKey
int64_t nTimeFirstKey
Definition: wallet.h:363