PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/qt/askpassphrasedialog.cpp

https://gitlab.com/Quetzalcoatl/VekitaCoin
C++ | 252 lines | 222 code | 14 blank | 16 comment | 35 complexity | a051067fd40d68e67f78166b2b40d3ea MD5 | raw file
  1. // Copyright (c) 2011-2013 The Bitcoin developers
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "askpassphrasedialog.h"
  5. #include "ui_askpassphrasedialog.h"
  6. #include "guiconstants.h"
  7. #include "walletmodel.h"
  8. #include <QMessageBox>
  9. #include <QPushButton>
  10. #include <QKeyEvent>
  11. AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
  12. QDialog(parent),
  13. ui(new Ui::AskPassphraseDialog),
  14. mode(mode),
  15. model(0),
  16. fCapsLock(false)
  17. {
  18. ui->setupUi(this);
  19. ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
  20. ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
  21. ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
  22. // Setup Caps Lock detection.
  23. ui->passEdit1->installEventFilter(this);
  24. ui->passEdit2->installEventFilter(this);
  25. ui->passEdit3->installEventFilter(this);
  26. switch(mode)
  27. {
  28. case Encrypt: // Ask passphrase x2
  29. ui->passLabel1->hide();
  30. ui->passEdit1->hide();
  31. 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>."));
  32. setWindowTitle(tr("Encrypt wallet"));
  33. break;
  34. case Unlock: // Ask passphrase
  35. ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
  36. ui->passLabel2->hide();
  37. ui->passEdit2->hide();
  38. ui->passLabel3->hide();
  39. ui->passEdit3->hide();
  40. setWindowTitle(tr("Unlock wallet"));
  41. break;
  42. case Decrypt: // Ask passphrase
  43. ui->warningLabel->setText(tr("This operation needs your wallet passphrase to decrypt the wallet."));
  44. ui->passLabel2->hide();
  45. ui->passEdit2->hide();
  46. ui->passLabel3->hide();
  47. ui->passEdit3->hide();
  48. setWindowTitle(tr("Decrypt wallet"));
  49. break;
  50. case ChangePass: // Ask old passphrase + new passphrase x2
  51. setWindowTitle(tr("Change passphrase"));
  52. ui->warningLabel->setText(tr("Enter the old and new passphrase to the wallet."));
  53. break;
  54. }
  55. textChanged();
  56. connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
  57. connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
  58. connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
  59. }
  60. AskPassphraseDialog::~AskPassphraseDialog()
  61. {
  62. // Attempt to overwrite text so that they do not linger around in memory
  63. ui->passEdit1->setText(QString(" ").repeated(ui->passEdit1->text().size()));
  64. ui->passEdit2->setText(QString(" ").repeated(ui->passEdit2->text().size()));
  65. ui->passEdit3->setText(QString(" ").repeated(ui->passEdit3->text().size()));
  66. delete ui;
  67. }
  68. void AskPassphraseDialog::setModel(WalletModel *model)
  69. {
  70. this->model = model;
  71. }
  72. void AskPassphraseDialog::accept()
  73. {
  74. SecureString oldpass, newpass1, newpass2;
  75. if(!model)
  76. return;
  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());
  85. switch(mode)
  86. {
  87. case Encrypt: {
  88. if(newpass1.empty() || newpass2.empty())
  89. {
  90. // Cannot encrypt with empty passphrase
  91. break;
  92. }
  93. QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
  94. tr("Warning: If you encrypt your wallet and lose your passphrase, you will <b>LOSE ALL OF YOUR LITECOINS</b>!") + "<br><br>" + tr("Are you sure you wish to encrypt your wallet?"),
  95. QMessageBox::Yes|QMessageBox::Cancel,
  96. QMessageBox::Cancel);
  97. if(retval == QMessageBox::Yes)
  98. {
  99. if(newpass1 == newpass2)
  100. {
  101. if(model->setWalletEncrypted(true, newpass1))
  102. {
  103. QMessageBox::warning(this, tr("Wallet encrypted"),
  104. "<qt>" +
  105. tr("Vekita will close now to finish the encryption process. "
  106. "Remember that encrypting your wallet cannot fully protect "
  107. "your vekitas from being stolen by malware infecting your computer.") +
  108. "<br><br><b>" +
  109. tr("IMPORTANT: Any previous backups you have made of your wallet file "
  110. "should be replaced with the newly generated, encrypted wallet file. "
  111. "For security reasons, previous backups of the unencrypted wallet file "
  112. "will become useless as soon as you start using the new, encrypted wallet.") +
  113. "</b></qt>");
  114. QApplication::quit();
  115. }
  116. else
  117. {
  118. QMessageBox::critical(this, tr("Wallet encryption failed"),
  119. tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
  120. }
  121. QDialog::accept(); // Success
  122. }
  123. else
  124. {
  125. QMessageBox::critical(this, tr("Wallet encryption failed"),
  126. tr("The supplied passphrases do not match."));
  127. }
  128. }
  129. else
  130. {
  131. QDialog::reject(); // Cancelled
  132. }
  133. } break;
  134. case Unlock:
  135. if(!model->setWalletLocked(false, oldpass))
  136. {
  137. QMessageBox::critical(this, tr("Wallet unlock failed"),
  138. tr("The passphrase entered for the wallet decryption was incorrect."));
  139. }
  140. else
  141. {
  142. QDialog::accept(); // Success
  143. }
  144. break;
  145. case Decrypt:
  146. if(!model->setWalletEncrypted(false, oldpass))
  147. {
  148. QMessageBox::critical(this, tr("Wallet decryption 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 ChangePass:
  157. if(newpass1 == newpass2)
  158. {
  159. if(model->changePassphrase(oldpass, newpass1))
  160. {
  161. QMessageBox::information(this, tr("Wallet encrypted"),
  162. tr("Wallet passphrase was successfully changed."));
  163. QDialog::accept(); // Success
  164. }
  165. else
  166. {
  167. QMessageBox::critical(this, tr("Wallet encryption failed"),
  168. tr("The passphrase entered for the wallet decryption was incorrect."));
  169. }
  170. }
  171. else
  172. {
  173. QMessageBox::critical(this, tr("Wallet encryption failed"),
  174. tr("The supplied passphrases do not match."));
  175. }
  176. break;
  177. }
  178. }
  179. void AskPassphraseDialog::textChanged()
  180. {
  181. // Validate input, set Ok button to enabled when acceptable
  182. bool acceptable = false;
  183. switch(mode)
  184. {
  185. case Encrypt: // New passphrase x2
  186. acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
  187. break;
  188. case Unlock: // Old passphrase x1
  189. case Decrypt:
  190. acceptable = !ui->passEdit1->text().isEmpty();
  191. break;
  192. case ChangePass: // Old passphrase x1, new passphrase x2
  193. acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
  194. break;
  195. }
  196. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
  197. }
  198. bool AskPassphraseDialog::event(QEvent *event)
  199. {
  200. // Detect Caps Lock key press.
  201. if (event->type() == QEvent::KeyPress) {
  202. QKeyEvent *ke = static_cast<QKeyEvent *>(event);
  203. if (ke->key() == Qt::Key_CapsLock) {
  204. fCapsLock = !fCapsLock;
  205. }
  206. if (fCapsLock) {
  207. ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
  208. } else {
  209. ui->capsLabel->clear();
  210. }
  211. }
  212. return QWidget::event(event);
  213. }
  214. bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
  215. {
  216. /* Detect Caps Lock.
  217. * There is no good OS-independent way to check a key state in Qt, but we
  218. * can detect Caps Lock by checking for the following condition:
  219. * Shift key is down and the result is a lower case character, or
  220. * Shift key is not down and the result is an upper case character.
  221. */
  222. if (event->type() == QEvent::KeyPress) {
  223. QKeyEvent *ke = static_cast<QKeyEvent *>(event);
  224. QString str = ke->text();
  225. if (str.length() != 0) {
  226. const QChar *psz = str.unicode();
  227. bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
  228. if ((fShift && *psz >= 'a' && *psz <= 'z') || (!fShift && *psz >= 'A' && *psz <= 'Z')) {
  229. fCapsLock = true;
  230. ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
  231. } else if (psz->isLetter()) {
  232. fCapsLock = false;
  233. ui->capsLabel->clear();
  234. }
  235. }
  236. }
  237. return QDialog::eventFilter(object, event);
  238. }