diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -346,6 +346,7 @@ util/bip32.cpp util/bytevectorhash.cpp util/error.cpp + util/message.cpp util/moneystr.cpp util/settings.cpp util/spanparsing.cpp diff --git a/src/qt/signverifymessagedialog.cpp b/src/qt/signverifymessagedialog.cpp --- a/src/qt/signverifymessagedialog.cpp +++ b/src/qt/signverifymessagedialog.cpp @@ -10,7 +10,7 @@ #include #include #include -#include // For strMessageMagic +#include // For MessageSign(), MessageVerify() #include #include @@ -148,12 +148,11 @@ 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("QLabel { color: red; }"); ui->statusLabel_SM->setText(QString("") + tr("Message signing failed.") + @@ -165,8 +164,7 @@ ui->statusLabel_SM->setText(QString("") + tr("Message signed.") + QString("")); - ui->signatureOut_SM->setText( - QString::fromStdString(EncodeBase64(vchSig.data(), vchSig.size()))); + ui->signatureOut_SM->setText(QString::fromStdString(signature)); } void SignVerifyMessageDialog::on_copySignatureButton_SM_clicked() { @@ -194,62 +192,55 @@ } void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked() { - CTxDestination destination = DecodeDestination( - ui->addressIn_VM->text().toStdString(), model->getChainParams()); - if (!IsValidDestination(destination)) { - ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); - ui->statusLabel_VM->setText( - tr("The entered address is invalid.") + QString(" ") + - tr("Please check the address and try again.")); - return; - } - if (!boost::get(&destination)) { - ui->addressIn_VM->setValid(false); - ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); - ui->statusLabel_VM->setText( - tr("The entered address does not refer to a key.") + QString(" ") + - tr("Please check the address and try again.")); - return; - } + 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(); - bool fInvalid = false; - std::vector vchSig = DecodeBase64( - ui->signatureIn_VM->text().toStdString().c_str(), &fInvalid); + const auto result = + MessageVerify(model->getChainParams(), address, signature, message); - if (fInvalid) { - ui->signatureIn_VM->setValid(false); + if (result == MessageVerificationResult::OK) { + ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }"); + } else { ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); - 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)) { - ui->signatureIn_VM->setValid(false); - ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); - ui->statusLabel_VM->setText( - tr("The signature did not match the message digest.") + - QString(" ") + tr("Please check the signature and try again.")); - return; + 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->setText( + tr("The entered address does not refer to a key.") + + QString(" ") + tr("Please check the address and try again.")); + return; + case MessageVerificationResult::ERR_MALFORMED_SIGNATURE: + ui->signatureIn_VM->setValid(false); + ui->statusLabel_VM->setText( + tr("The signature could not be decoded.") + QString(" ") + + tr("Please check the signature and try again.")); + return; + case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED: + ui->signatureIn_VM->setValid(false); + 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(PKHash(pubkey)) == destination)) { - ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); - ui->statusLabel_VM->setText(QString("") + - tr("Message verification failed.") + - QString("")); - return; - } - - ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }"); - 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 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -17,10 +17,10 @@ #include #include