mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-12 02:07:39 +01:00
54 lines
1.7 KiB
C++
54 lines
1.7 KiB
C++
|
// 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;
|
||
|
}
|