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/signverifymessagedialog.h>6#include <qt/forms/ui_signverifymessagedialog.h>8#include <qt/addressbookpage.h>9#include <qt/guiutil.h>10#include <qt/platformstyle.h>11#include <qt/walletmodel.h>13#include <common/signmessage.h>14#include <bitcoin-build-config.h> // IWYU pragma: keep15#include <key_io.h>16#include <wallet/wallet.h>18#include <string>19#include <variant>20#include <vector>22#include <QClipboard>24SignVerifyMessageDialog::SignVerifyMessageDialog(const PlatformStyle *_platformStyle, QWidget *parent) :25QDialog(parent, GUIUtil::dialog_flags),26ui(new Ui::SignVerifyMessageDialog),27platformStyle(_platformStyle)28{29ui->setupUi(this);31ui->addressBookButton_SM->setIcon(platformStyle->SingleColorIcon(":/icons/address-book"));32ui->pasteButton_SM->setIcon(platformStyle->SingleColorIcon(":/icons/editpaste"));33ui->copySignatureButton_SM->setIcon(platformStyle->SingleColorIcon(":/icons/editcopy"));34ui->signMessageButton_SM->setIcon(platformStyle->SingleColorIcon(":/icons/edit"));35ui->clearButton_SM->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));36ui->addressBookButton_VM->setIcon(platformStyle->SingleColorIcon(":/icons/address-book"));37ui->verifyMessageButton_VM->setIcon(platformStyle->SingleColorIcon(":/icons/transaction_0"));38ui->clearButton_VM->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));40GUIUtil::setupAddressWidget(ui->addressIn_SM, this);41GUIUtil::setupAddressWidget(ui->addressIn_VM, this);43ui->addressIn_SM->installEventFilter(this);44ui->messageIn_SM->installEventFilter(this);45ui->signatureOut_SM->installEventFilter(this);46ui->addressIn_VM->installEventFilter(this);47ui->messageIn_VM->installEventFilter(this);48ui->signatureIn_VM->installEventFilter(this);50ui->signatureOut_SM->setFont(GUIUtil::fixedPitchFont());51ui->signatureIn_VM->setFont(GUIUtil::fixedPitchFont());53GUIUtil::handleCloseWindowShortcut(this);54}56SignVerifyMessageDialog::~SignVerifyMessageDialog()57{58delete ui;59}61void SignVerifyMessageDialog::setModel(WalletModel *_model)62{63this->model = _model;64}66void SignVerifyMessageDialog::setAddress_SM(const QString &address)67{68ui->addressIn_SM->setText(address);69ui->messageIn_SM->setFocus();70}72void SignVerifyMessageDialog::setAddress_VM(const QString &address)73{74ui->addressIn_VM->setText(address);75ui->messageIn_VM->setFocus();76}78void SignVerifyMessageDialog::showTab_SM(bool fShow)79{80ui->tabWidget->setCurrentIndex(0);81if (fShow)82this->show();83}85void SignVerifyMessageDialog::showTab_VM(bool fShow)86{87ui->tabWidget->setCurrentIndex(1);88if (fShow)89this->show();90}92void SignVerifyMessageDialog::on_addressBookButton_SM_clicked()93{94if (model && model->getAddressTableModel())95{96model->refresh(/*pk_hash_only=*/true);97AddressBookPage dlg(platformStyle, AddressBookPage::ForSelection, AddressBookPage::ReceivingTab, this);98dlg.setModel(model->getAddressTableModel());99if (dlg.exec())100{101setAddress_SM(dlg.getReturnValue());102}103}104}106void SignVerifyMessageDialog::on_pasteButton_SM_clicked()107{108setAddress_SM(QApplication::clipboard()->text());109}111void SignVerifyMessageDialog::on_signMessageButton_SM_clicked()112{113if (!model)114return;116/* Clear old signature to ensure users don't get confused on error with an old signature displayed */117ui->signatureOut_SM->clear();119CTxDestination destination = DecodeDestination(ui->addressIn_SM->text().toStdString());120if (!IsValidDestination(destination)) {121ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");122ui->statusLabel_SM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));123return;124}125const PKHash* pkhash = std::get_if<PKHash>(&destination);126if (!pkhash) {127ui->addressIn_SM->setValid(false);128ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");129ui->statusLabel_SM->setText(tr("The entered address does not refer to a legacy (P2PKH) key. Message signing for SegWit and other non-P2PKH address types is not supported in this version of %1. Please check the address and try again.").arg(CLIENT_NAME));130return;131}133WalletModel::UnlockContext ctx(model->requestUnlock());134if (!ctx.isValid())135{136ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");137ui->statusLabel_SM->setText(tr("Wallet unlock was cancelled."));138return;139}141const std::string& message = ui->messageIn_SM->document()->toPlainText().toStdString();142std::string signature;143SigningResult res = model->wallet().signMessage(message, *pkhash, signature);145QString error;146switch (res) {147case SigningResult::OK:148error = tr("No error");149break;150case SigningResult::PRIVATE_KEY_NOT_AVAILABLE:151error = tr("Private key for the entered address is not available.");152break;153case SigningResult::SIGNING_FAILED:154error = tr("Message signing failed.");155break;156} // no default case, so the compiler can warn about missing cases158if (res != SigningResult::OK) {159ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");160ui->statusLabel_SM->setText(QString("<nobr>") + error + QString("</nobr>"));161return;162}164ui->statusLabel_SM->setStyleSheet("QLabel { color: green; }");165ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signed.") + QString("</nobr>"));167ui->signatureOut_SM->setText(QString::fromStdString(signature));168}170void SignVerifyMessageDialog::on_copySignatureButton_SM_clicked()171{172GUIUtil::setClipboard(ui->signatureOut_SM->text());173}175void SignVerifyMessageDialog::on_clearButton_SM_clicked()176{177ui->addressIn_SM->clear();178ui->messageIn_SM->clear();179ui->signatureOut_SM->clear();180ui->statusLabel_SM->clear();182ui->addressIn_SM->setFocus();183}185void SignVerifyMessageDialog::on_addressBookButton_VM_clicked()186{187if (model && model->getAddressTableModel())188{189AddressBookPage dlg(platformStyle, AddressBookPage::ForSelection, AddressBookPage::SendingTab, this);190dlg.setModel(model->getAddressTableModel());191if (dlg.exec())192{193setAddress_VM(dlg.getReturnValue());194}195}196}198void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked()199{200const std::string& address = ui->addressIn_VM->text().toStdString();201const std::string& signature = ui->signatureIn_VM->text().toStdString();202const std::string& message = ui->messageIn_VM->document()->toPlainText().toStdString();204const auto result = MessageVerify(address, signature, message);206if (result == MessageVerificationResult::OK) {207ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }");208} else {209ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }");210}212switch (result) {213case MessageVerificationResult::OK:214ui->statusLabel_VM->setText(215QString("<nobr>") + tr("Message verified.") + QString("</nobr>")216);217return;218case MessageVerificationResult::ERR_INVALID_ADDRESS:219ui->statusLabel_VM->setText(220tr("The entered address is invalid.") + QString(" ") +221tr("Please check the address and try again.")222);223return;224case MessageVerificationResult::ERR_ADDRESS_NO_KEY:225ui->addressIn_VM->setValid(false);226ui->statusLabel_VM->setText(tr("The entered address does not refer to a legacy (P2PKH) key. Message signing for SegWit and other non-P2PKH address types is not supported in this version of %1. Please check the address and try again.").arg(CLIENT_NAME));227return;228case MessageVerificationResult::ERR_MALFORMED_SIGNATURE:229ui->signatureIn_VM->setValid(false);230ui->statusLabel_VM->setText(231tr("The signature could not be decoded.") + QString(" ") +232tr("Please check the signature and try again.")233);234return;235case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED:236ui->signatureIn_VM->setValid(false);237ui->statusLabel_VM->setText(238tr("The signature did not match the message digest.") + QString(" ") +239tr("Please check the signature and try again.")240);241return;242case MessageVerificationResult::ERR_NOT_SIGNED:243ui->statusLabel_VM->setText(244QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>")245);246return;247}248}250void SignVerifyMessageDialog::on_clearButton_VM_clicked()
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.