2019-01-29 15:53:14 +01:00
|
|
|
// Copyright (c) 2014-2018 The Dash Core developers
|
2017-04-12 09:04:06 +02:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "base58.h"
|
|
|
|
#include "hash.h"
|
2017-08-09 02:19:06 +02:00
|
|
|
#include "validation.h" // For strMessageMagic
|
2017-04-12 09:04:06 +02:00
|
|
|
#include "messagesigner.h"
|
|
|
|
#include "tinyformat.h"
|
|
|
|
#include "utilstrencodings.h"
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
bool CMessageSigner::GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CPubKey& pubkeyRet)
|
2017-04-12 09:04:06 +02:00
|
|
|
{
|
|
|
|
CBitcoinSecret vchSecret;
|
|
|
|
|
|
|
|
if(!vchSecret.SetString(strSecret)) return false;
|
|
|
|
|
|
|
|
keyRet = vchSecret.GetKey();
|
|
|
|
pubkeyRet = keyRet.GetPubKey();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
bool CMessageSigner::SignMessage(const std::string& strMessage, std::vector<unsigned char>& vchSigRet, const CKey& key)
|
2017-04-12 09:04:06 +02:00
|
|
|
{
|
|
|
|
CHashWriter ss(SER_GETHASH, 0);
|
|
|
|
ss << strMessageMagic;
|
|
|
|
ss << strMessage;
|
|
|
|
|
|
|
|
return CHashSigner::SignHash(ss.GetHash(), key, vchSigRet);
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
bool CMessageSigner::VerifyMessage(const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet)
|
2018-03-02 14:15:04 +01:00
|
|
|
{
|
|
|
|
return VerifyMessage(pubkey.GetID(), vchSig, strMessage, strErrorRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CMessageSigner::VerifyMessage(const CKeyID& keyID, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet)
|
2017-04-12 09:04:06 +02:00
|
|
|
{
|
|
|
|
CHashWriter ss(SER_GETHASH, 0);
|
|
|
|
ss << strMessageMagic;
|
|
|
|
ss << strMessage;
|
|
|
|
|
2018-03-02 14:15:04 +01:00
|
|
|
return CHashSigner::VerifyHash(ss.GetHash(), keyID, vchSig, strErrorRet);
|
2017-04-12 09:04:06 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
bool CHashSigner::SignHash(const uint256& hash, const CKey& key, std::vector<unsigned char>& vchSigRet)
|
2017-04-12 09:04:06 +02:00
|
|
|
{
|
|
|
|
return key.SignCompact(hash, vchSigRet);
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
bool CHashSigner::VerifyHash(const uint256& hash, const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, std::string& strErrorRet)
|
2018-03-02 14:15:04 +01:00
|
|
|
{
|
|
|
|
return VerifyHash(hash, pubkey.GetID(), vchSig, strErrorRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CHashSigner::VerifyHash(const uint256& hash, const CKeyID& keyID, const std::vector<unsigned char>& vchSig, std::string& strErrorRet)
|
2017-04-12 09:04:06 +02:00
|
|
|
{
|
|
|
|
CPubKey pubkeyFromSig;
|
|
|
|
if(!pubkeyFromSig.RecoverCompact(hash, vchSig)) {
|
|
|
|
strErrorRet = "Error recovering public key.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-02 14:15:04 +01:00
|
|
|
if(pubkeyFromSig.GetID() != keyID) {
|
2017-04-12 09:04:06 +02:00
|
|
|
strErrorRet = strprintf("Keys don't match: pubkey=%s, pubkeyFromSig=%s, hash=%s, vchSig=%s",
|
2018-03-02 14:15:04 +01:00
|
|
|
keyID.ToString(), pubkeyFromSig.GetID().ToString(), hash.ToString(),
|
2017-04-12 09:04:06 +02:00
|
|
|
EncodeBase64(&vchSig[0], vchSig.size()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|