dash/src/messagesigner.cpp
Alexander Block 611879aa6d Use spork addresses instead of raw keys and allow changing them on startup (#1969)
* Support passing CKeyID to CMessageSigner/CHashSigner

* Use Dash addresses instead of raw public keys for sporks

The spork addresses are identical to the previously used public keys.

Also use CKeyID/CKey directly inside CSporkManager instead of parsing the
addresses/keys over and over. The default spork key (from chainparams) is
initialized with InitDefaultSporkAddress(). SetPrivKey parses the private
key now and stores it in sporkPrivKey instead of parsing it in
CSporkMessage::Sign().

* Allow setting of spork address via command line

* Remove unused strMasternodePaymentsPubKey chainparam

Traces from the past...

* Review fixes

1. Remove the need for InitDefaultSporkAddress
2. Remove bogus checks for hex private keys
3. Alphabetical order for new include
4. Add . to help string

* Add regtest spork key

As this key is not meant to be private, the private key is also added in
the form of a comment (for later use in regtests)

* Review fixes
2018-03-02 16:15:04 +03:00

74 lines
2.4 KiB
C++

// Copyright (c) 2014-2017 The Dash Core developers
// 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"
#include "validation.h" // For strMessageMagic
#include "messagesigner.h"
#include "tinyformat.h"
#include "utilstrencodings.h"
bool CMessageSigner::GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CPubKey& pubkeyRet)
{
CBitcoinSecret vchSecret;
if(!vchSecret.SetString(strSecret)) return false;
keyRet = vchSecret.GetKey();
pubkeyRet = keyRet.GetPubKey();
return true;
}
bool CMessageSigner::SignMessage(const std::string& strMessage, std::vector<unsigned char>& vchSigRet, const CKey& key)
{
CHashWriter ss(SER_GETHASH, 0);
ss << strMessageMagic;
ss << strMessage;
return CHashSigner::SignHash(ss.GetHash(), key, vchSigRet);
}
bool CMessageSigner::VerifyMessage(const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet)
{
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)
{
CHashWriter ss(SER_GETHASH, 0);
ss << strMessageMagic;
ss << strMessage;
return CHashSigner::VerifyHash(ss.GetHash(), keyID, vchSig, strErrorRet);
}
bool CHashSigner::SignHash(const uint256& hash, const CKey& key, std::vector<unsigned char>& vchSigRet)
{
return key.SignCompact(hash, vchSigRet);
}
bool CHashSigner::VerifyHash(const uint256& hash, const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, std::string& strErrorRet)
{
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)
{
CPubKey pubkeyFromSig;
if(!pubkeyFromSig.RecoverCompact(hash, vchSig)) {
strErrorRet = "Error recovering public key.";
return false;
}
if(pubkeyFromSig.GetID() != keyID) {
strErrorRet = strprintf("Keys don't match: pubkey=%s, pubkeyFromSig=%s, hash=%s, vchSig=%s",
keyID.ToString(), pubkeyFromSig.GetID().ToString(), hash.ToString(),
EncodeBase64(&vchSig[0], vchSig.size()));
return false;
}
return true;
}