From d155f13c76dfb792b963469a8c3b2581c038c81d Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Mon, 19 Sep 2022 02:19:22 +0530 Subject: [PATCH] merge bitcoin#17577: deduplicate the message sign/verify code --- src/Makefile.am | 4 +- src/messagesigner.cpp | 6 +- src/qt/signverifymessagedialog.cpp | 91 ++++++++++++------------ src/rpc/misc.cpp | 44 +++++------- src/spork.cpp | 4 +- src/test/util_tests.cpp | 110 +++++++++++++++++++++++++++++ src/util/message.cpp | 78 ++++++++++++++++++++ src/util/message.h | 68 ++++++++++++++++++ src/util/validation.cpp | 2 - src/util/validation.h | 2 - src/wallet/rpcwallet.cpp | 12 ++-- 11 files changed, 332 insertions(+), 89 deletions(-) create mode 100644 src/util/message.cpp create mode 100644 src/util/message.h diff --git a/src/Makefile.am b/src/Makefile.am index d3d48766d8..b71e8a3685 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -303,6 +303,7 @@ BITCOIN_CORE_H = \ util/getuniquepath.h \ util/macros.h \ util/memory.h \ + util/message.h \ util/moneystr.h \ util/ranges.h \ util/ref.h \ @@ -702,13 +703,14 @@ libbitcoin_util_a_SOURCES = \ support/cleanse.cpp \ sync.cpp \ threadinterrupt.cpp \ + util/asmap.cpp \ util/bip32.cpp \ util/bytevectorhash.cpp \ util/error.cpp \ util/fees.cpp \ util/getuniquepath.cpp \ util/system.cpp \ - util/asmap.cpp \ + util/message.cpp \ util/moneystr.cpp \ util/settings.cpp \ util/spanparsing.cpp \ diff --git a/src/messagesigner.cpp b/src/messagesigner.cpp index 37cba1997d..cf0740ec12 100644 --- a/src/messagesigner.cpp +++ b/src/messagesigner.cpp @@ -4,7 +4,7 @@ #include #include -#include // For strMessageMagic +#include // For MESSAGE_MAGIC #include #include #include @@ -23,7 +23,7 @@ bool CMessageSigner::GetKeysFromSecret(const std::string& strSecret, CKey& keyRe bool CMessageSigner::SignMessage(const std::string& strMessage, std::vector& vchSigRet, const CKey& key) { CHashWriter ss(SER_GETHASH, 0); - ss << strMessageMagic; + ss << MESSAGE_MAGIC; ss << strMessage; return CHashSigner::SignHash(ss.GetHash(), key, vchSigRet); @@ -37,7 +37,7 @@ bool CMessageSigner::VerifyMessage(const CPubKey& pubkey, const std::vector& vchSig, const std::string& strMessage, std::string& strErrorRet) { CHashWriter ss(SER_GETHASH, 0); - ss << strMessageMagic; + ss << MESSAGE_MAGIC; ss << strMessage; return CHashSigner::VerifyHash(ss.GetHash(), keyID, vchSig, strErrorRet); diff --git a/src/qt/signverifymessagedialog.cpp b/src/qt/signverifymessagedialog.cpp index 3c29566ad0..cc07ff6399 100644 --- a/src/qt/signverifymessagedialog.cpp +++ b/src/qt/signverifymessagedialog.cpp @@ -12,7 +12,7 @@ #include #include -#include // For strMessageMagic +#include // For MessageSign(), MessageVerify() #include #include @@ -169,13 +169,10 @@ void SignVerifyMessageDialog::on_signMessageButton_SM_clicked() return; } - CHashWriter ss(SER_GETHASH, 0); - ss << strMessageMagic; - ss << ui->messageIn_SM->document()->toPlainText().toStdString(); + const std::string& message = ui->messageIn_SM->document()->toPlainText().toStdString(); + std::string signature; - std::vector vchSig; - if (!key.SignCompact(ss.GetHash(), vchSig)) - { + if (!MessageSign(key, message, signature)) { ui->statusLabel_SM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR)); ui->statusLabel_SM->setText(QString("") + tr("Message signing failed.") + QString("")); return; @@ -184,7 +181,7 @@ void SignVerifyMessageDialog::on_signMessageButton_SM_clicked() ui->statusLabel_SM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_SUCCESS)); ui->statusLabel_SM->setText(QString("") + tr("Message signed.") + QString("")); - ui->signatureOut_SM->setText(QString::fromStdString(EncodeBase64(vchSig))); + ui->signatureOut_SM->setText(QString::fromStdString(signature)); } void SignVerifyMessageDialog::on_copySignatureButton_SM_clicked() @@ -217,51 +214,57 @@ void SignVerifyMessageDialog::on_addressBookButton_VM_clicked() void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked() { - CTxDestination destination = DecodeDestination(ui->addressIn_VM->text().toStdString()); - if (!IsValidDestination(destination)) { + const std::string& address = ui->addressIn_VM->text().toStdString(); + const std::string& signature = ui->signatureIn_VM->text().toStdString(); + const std::string& message = ui->messageIn_VM->document()->toPlainText().toStdString(); + + const auto result = MessageVerify(address, signature, message); + + if (result == MessageVerificationResult::OK) { + ui->statusLabel_VM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_SUCCESS)); + } else { ui->statusLabel_VM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR)); - ui->statusLabel_VM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again.")); - return; } - if (!boost::get(&destination)) { + + switch (result) { + case MessageVerificationResult::OK: + ui->statusLabel_VM->setText( + QString("") + tr("Message verified.") + QString("") + ); + return; + case MessageVerificationResult::ERR_INVALID_ADDRESS: + ui->statusLabel_VM->setText( + tr("The entered address is invalid.") + QString(" ") + + tr("Please check the address and try again.") + ); + return; + case MessageVerificationResult::ERR_ADDRESS_NO_KEY: ui->addressIn_VM->setValid(false); - ui->statusLabel_VM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR)); - ui->statusLabel_VM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again.")); + ui->statusLabel_VM->setText( + tr("The entered address does not refer to a key.") + QString(" ") + + tr("Please check the address and try again.") + ); return; - } - - bool fInvalid = false; - std::vector vchSig = DecodeBase64(ui->signatureIn_VM->text().toStdString().c_str(), &fInvalid); - - if (fInvalid) - { + case MessageVerificationResult::ERR_MALFORMED_SIGNATURE: ui->signatureIn_VM->setValid(false); - ui->statusLabel_VM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR)); - ui->statusLabel_VM->setText(tr("The signature could not be decoded.") + QString(" ") + tr("Please check the signature and try again.")); + ui->statusLabel_VM->setText( + tr("The signature could not be decoded.") + QString(" ") + + tr("Please check the signature and try again.") + ); return; - } - - CHashWriter ss(SER_GETHASH, 0); - ss << strMessageMagic; - ss << ui->messageIn_VM->document()->toPlainText().toStdString(); - - CPubKey pubkey; - if (!pubkey.RecoverCompact(ss.GetHash(), vchSig)) - { + case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED: ui->signatureIn_VM->setValid(false); - ui->statusLabel_VM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR)); - ui->statusLabel_VM->setText(tr("The signature did not match the message digest.") + QString(" ") + tr("Please check the signature and try again.")); + ui->statusLabel_VM->setText( + tr("The signature did not match the message digest.") + QString(" ") + + tr("Please check the signature and try again.") + ); + return; + case MessageVerificationResult::ERR_NOT_SIGNED: + ui->statusLabel_VM->setText( + QString("") + tr("Message verification failed.") + QString("") + ); return; } - - if (!(CTxDestination(pubkey.GetID()) == destination)) { - ui->statusLabel_VM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR)); - ui->statusLabel_VM->setText(QString("") + tr("Message verification failed.") + QString("")); - return; - } - - ui->statusLabel_VM->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_SUCCESS)); - ui->statusLabel_VM->setText(QString("") + tr("Message verified.") + QString("")); } void SignVerifyMessageDialog::on_clearButton_VM_clicked() diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index b5c984f9ff..2b903b754e 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -20,11 +20,11 @@ #include