Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2011-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <qt/askpassphrasedialog.h>6#include <qt/forms/ui_askpassphrasedialog.h>8#include <qt/guiconstants.h>9#include <qt/guiutil.h>10#include <qt/walletmodel.h>12#include <support/allocators/secure.h>14#include <QKeyEvent>15#include <QMessageBox>16#include <QPushButton>18AskPassphraseDialog::AskPassphraseDialog(Mode _mode, QWidget *parent, SecureString* passphrase_out) :19QDialog(parent, GUIUtil::dialog_flags),20ui(new Ui::AskPassphraseDialog),21mode(_mode),22m_passphrase_out(passphrase_out)23{24ui->setupUi(this);26ui->passEdit1->setMinimumSize(ui->passEdit1->sizeHint());27ui->passEdit2->setMinimumSize(ui->passEdit2->sizeHint());28ui->passEdit3->setMinimumSize(ui->passEdit3->sizeHint());30ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);31ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);32ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);34// Setup Caps Lock detection.35ui->passEdit1->installEventFilter(this);36ui->passEdit2->installEventFilter(this);37ui->passEdit3->installEventFilter(this);39switch(mode)40{41case Encrypt: // Ask passphrase x242ui->warningLabel->setText(tr("Enter the new passphrase for the wallet.<br/>Please use a passphrase of <b>ten or more random characters</b>, or <b>eight or more words</b>."));43ui->passLabel1->hide();44ui->passEdit1->hide();45setWindowTitle(tr("Encrypt wallet"));46break;47case UnlockMigration:48case Unlock: // Ask passphrase49ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));50ui->passLabel2->hide();51ui->passEdit2->hide();52ui->passLabel3->hide();53ui->passEdit3->hide();54setWindowTitle(tr("Unlock wallet"));55break;56case ChangePass: // Ask old passphrase + new passphrase x257setWindowTitle(tr("Change passphrase"));58ui->warningLabel->setText(tr("Enter the old passphrase and new passphrase for the wallet."));59break;60}61textChanged();62connect(ui->toggleShowPasswordButton, &QPushButton::toggled, this, &AskPassphraseDialog::toggleShowPassword);63connect(ui->passEdit1, &QLineEdit::textChanged, this, &AskPassphraseDialog::textChanged);64connect(ui->passEdit2, &QLineEdit::textChanged, this, &AskPassphraseDialog::textChanged);65connect(ui->passEdit3, &QLineEdit::textChanged, this, &AskPassphraseDialog::textChanged);67GUIUtil::handleCloseWindowShortcut(this);68}70AskPassphraseDialog::~AskPassphraseDialog()71{72secureClearPassFields();73delete ui;74}76void AskPassphraseDialog::setModel(WalletModel *_model)77{78this->model = _model;79}81void AskPassphraseDialog::accept()82{83SecureString oldpass, newpass1, newpass2;84if (!model && mode != Encrypt && mode != UnlockMigration)85return;86oldpass.reserve(MAX_PASSPHRASE_SIZE);87newpass1.reserve(MAX_PASSPHRASE_SIZE);88newpass2.reserve(MAX_PASSPHRASE_SIZE);90oldpass.assign(std::string_view{ui->passEdit1->text().toStdString()});91newpass1.assign(std::string_view{ui->passEdit2->text().toStdString()});92newpass2.assign(std::string_view{ui->passEdit3->text().toStdString()});94secureClearPassFields();96switch(mode)97{98case Encrypt: {99if(newpass1.empty() || newpass2.empty())100{101// Cannot encrypt with empty passphrase102break;103}104QMessageBox msgBoxConfirm(QMessageBox::Question,105tr("Confirm wallet encryption"),106tr("Warning: If you encrypt your wallet and lose your passphrase, you will <b>LOSE ALL OF YOUR BITCOINS</b>!") + "<br><br>" + tr("Are you sure you wish to encrypt your wallet?"),107QMessageBox::Cancel | QMessageBox::Yes, this);108msgBoxConfirm.button(QMessageBox::Yes)->setText(tr("Continue"));109msgBoxConfirm.button(QMessageBox::Cancel)->setText(tr("Back"));110msgBoxConfirm.setDefaultButton(QMessageBox::Cancel);111QMessageBox::StandardButton retval = (QMessageBox::StandardButton)msgBoxConfirm.exec();112if(retval == QMessageBox::Yes)113{114if(newpass1 == newpass2)115{116QString encryption_reminder = tr("Remember that encrypting your wallet cannot fully protect "117"your bitcoins from being stolen by malware infecting your computer.");118if (m_passphrase_out) {119m_passphrase_out->assign(newpass1);120QMessageBox msgBoxWarning(QMessageBox::Warning,121tr("Wallet to be encrypted"),122"<qt>" +123tr("Your wallet is about to be encrypted. ") + encryption_reminder + " " +124tr("Are you sure you wish to encrypt your wallet?") +125"</b></qt>",126QMessageBox::Cancel | QMessageBox::Yes, this);127msgBoxWarning.setDefaultButton(QMessageBox::Cancel);128QMessageBox::StandardButton retval = (QMessageBox::StandardButton)msgBoxWarning.exec();129if (retval == QMessageBox::Cancel) {130QDialog::reject();131return;132}133} else {134assert(model != nullptr);135if (model->setWalletEncrypted(newpass1)) {136QMessageBox::warning(this, tr("Wallet encrypted"),137"<qt>" +138tr("Your wallet is now encrypted. ") + encryption_reminder +139"<br><br><b>" +140tr("IMPORTANT: Any previous backups you have made of your wallet file "141"should be replaced with the newly generated, encrypted wallet file. "142"For security reasons, previous backups of the unencrypted wallet file "143"will become useless as soon as you start using the new, encrypted wallet.") +144"</b></qt>");145} else {146QMessageBox::critical(this, tr("Wallet encryption failed"),147tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));148}149}150QDialog::accept(); // Success151}152else153{154QMessageBox::critical(this, tr("Wallet encryption failed"),155tr("The supplied passphrases do not match."));156}157}158} break;159case Unlock:160try {161if (!model->setWalletLocked(false, oldpass)) {162// Check if the passphrase has a null character (see #27067 for details)163if (oldpass.find('\0') == std::string::npos) {164QMessageBox::critical(this, tr("Wallet unlock failed"),165tr("The passphrase entered for the wallet decryption was incorrect."));166} else {167QMessageBox::critical(this, tr("Wallet unlock failed"),168tr("The passphrase entered for the wallet decryption is incorrect. "169"It contains a null character (ie - a zero byte). "170"If the passphrase was set with a version of this software prior to 25.0, "171"please try again with only the characters up to — but not including — "172"the first null character. If this is successful, please set a new "173"passphrase to avoid this issue in the future."));174}175} else {176if (m_passphrase_out) {177m_passphrase_out->assign(oldpass);178}179QDialog::accept(); // Success180}181} catch (const std::runtime_error& e) {182QMessageBox::critical(this, tr("Wallet unlock failed"), e.what());183}184break;185case UnlockMigration:186Assume(m_passphrase_out)->assign(oldpass);187QDialog::accept();188break;189case ChangePass:190if(newpass1 == newpass2)191{192if(model->changePassphrase(oldpass, newpass1))193{194QMessageBox::information(this, tr("Wallet encrypted"),195tr("Wallet passphrase was successfully changed."));196QDialog::accept(); // Success197}198else199{200// Check if the old passphrase had a null character (see #27067 for details)201if (oldpass.find('\0') == std::string::npos) {202QMessageBox::critical(this, tr("Passphrase change failed"),203tr("The passphrase entered for the wallet decryption was incorrect."));204} else {205QMessageBox::critical(this, tr("Passphrase change failed"),206tr("The old passphrase entered for the wallet decryption is incorrect. "207"It contains a null character (ie - a zero byte). "208"If the passphrase was set with a version of this software prior to 25.0, "209"please try again with only the characters up to — but not including — "210"the first null character."));211}212}213}214else215{216QMessageBox::critical(this, tr("Wallet encryption failed"),217tr("The supplied passphrases do not match."));218}219break;220}221}223void AskPassphraseDialog::textChanged()224{225// Validate input, set Ok button to enabled when acceptable226bool acceptable = false;227switch(mode)228{229case Encrypt: // New passphrase x2230acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();231break;232case UnlockMigration:233case Unlock: // Old passphrase x1234acceptable = !ui->passEdit1->text().isEmpty();235break;236case ChangePass: // Old passphrase x1, new passphrase x2237acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();238break;239}240ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);241}243bool AskPassphraseDialog::event(QEvent *event)244{245// Detect Caps Lock key press.246if (event->type() == QEvent::KeyPress) {247QKeyEvent *ke = static_cast<QKeyEvent *>(event);248if (ke->key() == Qt::Key_CapsLock) {249fCapsLock = !fCapsLock;250}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.