44
55#include " activemasternode.h"
66#include " base58.h"
7+ #include " clientversion.h"
78#include " init.h"
89#include " netbase.h"
910#include " masternode.h"
@@ -635,6 +636,23 @@ void CMasternodeBroadcast::Relay(CConnman& connman) const
635636 connman.RelayInv (inv);
636637}
637638
639+ uint256 CMasternodePing::GetHash () const
640+ {
641+ CHashWriter ss (SER_GETHASH, PROTOCOL_VERSION);
642+ if (sporkManager.IsSporkActive (SPORK_6_NEW_SIGS)) {
643+ ss << masternodeOutpoint;
644+ ss << blockHash;
645+ ss << sigTime;
646+ ss << fSentinelIsCurrent ;
647+ ss << nSentinelVersion;
648+ ss << nDaemonVersion;
649+ } else {
650+ ss << masternodeOutpoint << uint8_t {} << 0xffffffff ; // adding dummy values here to match old hashing format
651+ ss << sigTime;
652+ }
653+ return ss.GetHash ();
654+ }
655+
638656CMasternodePing::CMasternodePing (const COutPoint& outpoint)
639657{
640658 LOCK (cs_main);
@@ -643,42 +661,69 @@ CMasternodePing::CMasternodePing(const COutPoint& outpoint)
643661 masternodeOutpoint = outpoint;
644662 blockHash = chainActive[chainActive.Height () - 12 ]->GetBlockHash ();
645663 sigTime = GetAdjustedTime ();
664+ nDaemonVersion = CLIENT_VERSION;
646665}
647666
648667bool CMasternodePing::Sign (const CKey& keyMasternode, const CPubKey& pubKeyMasternode)
649668{
650669 std::string strError;
651- std::string strMasterNodeSignMessage;
652670
653- // TODO: add sentinel data
654671 sigTime = GetAdjustedTime ();
655- std::string strMessage = CTxIn (masternodeOutpoint).ToString () + blockHash.ToString () + boost::lexical_cast<std::string>(sigTime);
656672
657- if (!CMessageSigner::SignMessage (strMessage, vchSig, keyMasternode)) {
658- LogPrintf (" CMasternodePing::Sign -- SignMessage() failed\n " );
659- return false ;
660- }
673+ if (sporkManager.IsSporkActive (SPORK_6_NEW_SIGS)) {
674+ uint256 hash = GetHash ();
661675
662- if (!CMessageSigner::VerifyMessage (pubKeyMasternode, vchSig, strMessage, strError)) {
663- LogPrintf (" CMasternodePing::Sign -- VerifyMessage() failed, error: %s\n " , strError);
664- return false ;
676+ if (!CHashSigner::SignHash (hash, keyMasternode, vchSig)) {
677+ LogPrintf (" CMasternodePing::Sign -- SignHash() failed\n " );
678+ return false ;
679+ }
680+
681+ if (!CHashSigner::VerifyHash (hash, pubKeyMasternode, vchSig, strError)) {
682+ LogPrintf (" CMasternodePing::Sign -- VerifyHash() failed, error: %s\n " , strError);
683+ return false ;
684+ }
685+ } else {
686+ std::string strMessage = CTxIn (masternodeOutpoint).ToString () + blockHash.ToString () +
687+ boost::lexical_cast<std::string>(sigTime);
688+
689+ if (!CMessageSigner::SignMessage (strMessage, vchSig, keyMasternode)) {
690+ LogPrintf (" CMasternodePing::Sign -- SignMessage() failed\n " );
691+ return false ;
692+ }
693+
694+ if (!CMessageSigner::VerifyMessage (pubKeyMasternode, vchSig, strMessage, strError)) {
695+ LogPrintf (" CMasternodePing::Sign -- VerifyMessage() failed, error: %s\n " , strError);
696+ return false ;
697+ }
665698 }
666699
667700 return true ;
668701}
669702
670703bool CMasternodePing::CheckSignature (const CPubKey& pubKeyMasternode, int &nDos)
671704{
672- // TODO: add sentinel data
673- std::string strMessage = CTxIn (masternodeOutpoint).ToString () + blockHash.ToString () + boost::lexical_cast<std::string>(sigTime);
674705 std::string strError = " " ;
675706 nDos = 0 ;
676707
677- if (!CMessageSigner::VerifyMessage (pubKeyMasternode, vchSig, strMessage, strError)) {
678- LogPrintf (" CMasternodePing::CheckSignature -- Got bad Masternode ping signature, masternode=%s, error: %s\n " , masternodeOutpoint.ToStringShort (), strError);
679- nDos = 33 ;
680- return false ;
708+ if (sporkManager.IsSporkActive (SPORK_6_NEW_SIGS)) {
709+ uint256 hash = GetHash ();
710+
711+ if (!CHashSigner::VerifyHash (hash, pubKeyMasternode, vchSig, strError)) {
712+ LogPrintf (" CMasternodePing::CheckSignature -- Got bad Masternode ping signature, masternode=%s, error: %s\n " , masternodeOutpoint.ToStringShort (), strError);
713+ nDos = 33 ;
714+ return false ;
715+ }
716+ } else {
717+ std::string strMessage = CTxIn (masternodeOutpoint).ToString () + blockHash.ToString () +
718+ boost::lexical_cast<std::string>(sigTime);
719+
720+ if (!CMessageSigner::VerifyMessage (pubKeyMasternode, vchSig, strMessage, strError)) {
721+ LogPrintf (" CMasternodePing::CheckSignature -- Got bad Masternode ping signature, masternode=%s, error: %s\n " , masternodeOutpoint.ToStringShort (), strError);
722+ nDos = 33 ;
723+ return false ;
724+ }
681725 }
726+
682727 return true ;
683728}
684729
0 commit comments