forked from syscoin/syscoin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deduplicate the message verifying code
The logic of verifying a message was duplicated in 2 places: src/qt/signverifymessagedialog.cpp SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked() src/rpc/misc.cpp verifymessage() with the only difference being the result handling. Move the logic into a dedicated src/util/message.cpp MessageVerify() which returns a set of result codes, call it from the 2 places and just handle the results differently in the callers.
- Loading branch information
Showing
9 changed files
with
211 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) 2009-2010 Satoshi Nakamoto | ||
// Copyright (c) 2009-2020 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <hash.h> // For CHashWriter | ||
#include <key_io.h> // For DecodeDestination() | ||
#include <pubkey.h> // For CPubKey | ||
#include <script/standard.h> // For CTxDestination, IsValidDestination(), PKHash | ||
#include <serialize.h> // For SER_GETHASH | ||
#include <util/message.h> | ||
#include <util/strencodings.h> // For DecodeBase64() | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
const std::string strMessageMagic = "Bitcoin Signed Message:\n"; | ||
|
||
MessageVerificationResult MessageVerify( | ||
const std::string& address, | ||
const std::string& signature, | ||
const std::string& message) | ||
{ | ||
CTxDestination destination = DecodeDestination(address); | ||
if (!IsValidDestination(destination)) { | ||
return MessageVerificationResult::ERR_INVALID_ADDRESS; | ||
} | ||
|
||
if (boost::get<PKHash>(&destination) == nullptr) { | ||
return MessageVerificationResult::ERR_ADDRESS_NO_KEY; | ||
} | ||
|
||
bool invalid = false; | ||
std::vector<unsigned char> signature_bytes = DecodeBase64(signature.c_str(), &invalid); | ||
if (invalid) { | ||
return MessageVerificationResult::ERR_MALFORMED_SIGNATURE; | ||
} | ||
|
||
CHashWriter ss(SER_GETHASH, 0); | ||
ss << strMessageMagic; | ||
ss << message; | ||
|
||
CPubKey pubkey; | ||
if (!pubkey.RecoverCompact(ss.GetHash(), signature_bytes)) { | ||
return MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED; | ||
} | ||
|
||
if (!(CTxDestination(PKHash(pubkey)) == destination)) { | ||
return MessageVerificationResult::ERR_NOT_SIGNED; | ||
} | ||
|
||
return MessageVerificationResult::OK; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2009-2010 Satoshi Nakamoto | ||
// Copyright (c) 2009-2020 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BITCOIN_UTIL_MESSAGE_H | ||
#define BITCOIN_UTIL_MESSAGE_H | ||
|
||
#include <string> | ||
|
||
extern const std::string strMessageMagic; | ||
|
||
/** The result of a signed message verification. | ||
* Message verification takes as an input: | ||
* - address (with whose private key the message is supposed to have been signed) | ||
* - signature | ||
* - message | ||
*/ | ||
enum class MessageVerificationResult { | ||
//! The provided address is invalid. | ||
ERR_INVALID_ADDRESS, | ||
|
||
//! The provided address is valid but does not refer to a public key. | ||
ERR_ADDRESS_NO_KEY, | ||
|
||
//! The provided signature couldn't be parsed (maybe invalid base64). | ||
ERR_MALFORMED_SIGNATURE, | ||
|
||
//! A public key could not be recovered from the provided signature and message. | ||
ERR_PUBKEY_NOT_RECOVERED, | ||
|
||
//! The message was not signed with the private key of the provided address. | ||
ERR_NOT_SIGNED, | ||
|
||
//! The message verification was successful. | ||
OK | ||
}; | ||
|
||
/** Verify a signed message. | ||
* @param[in] address Signer's bitcoin address, it must refer to a public key. | ||
* @param[in] signature The signature in base64 format. | ||
* @param[in] message The message that was signed. | ||
* @return result code */ | ||
MessageVerificationResult MessageVerify( | ||
const std::string& address, | ||
const std::string& signature, | ||
const std::string& message); | ||
|
||
#endif // BITCOIN_UTIL_MESSAGE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters