bool CActiveMasternode::Register(CTxIn vin, CService service, CKey keyCollateralAddress, CPubKey pubKeyCollateralAddress, CKey keyMasternode, CPubKey pubKeyMasternode, std::string &errorMessage) { CMasternodeBroadcast mnb; CMasternodePing mnp(vin); if(!mnp.Sign(keyMasternode, pubKeyMasternode)){ errorMessage = strprintf("Failed to sign ping, vin: %s", vin.ToString()); LogPrintf("CActiveMasternode::Register() - %s\n", errorMessage); return false; } mnodeman.mapSeenMasternodePing.insert(make_pair(mnp.GetHash(), mnp)); LogPrintf("CActiveMasternode::Register() - Adding to Masternode list\n service: %s\n vin: %s\n", service.ToString(), vin.ToString()); mnb = CMasternodeBroadcast(service, vin, pubKeyCollateralAddress, pubKeyMasternode, PROTOCOL_VERSION); mnb.lastPing = mnp; if(!mnb.Sign(keyCollateralAddress)){ errorMessage = strprintf("Failed to sign broadcast, vin: %s", vin.ToString()); LogPrintf("CActiveMasternode::Register() - %s\n", errorMessage); return false; } mnodeman.mapSeenMasternodeBroadcast.insert(make_pair(mnb.GetHash(), mnb)); masternodeSync.AddedMasternodeList(mnb.GetHash()); CMasternode* pmn = mnodeman.Find(vin); if(pmn == NULL) { CMasternode mn(mnb); mnodeman.Add(mn); } else { pmn->UpdateFromNewBroadcast(mnb); } //send to all peers LogPrintf("CActiveMasternode::Register() - RelayElectionEntry vin = %s\n", vin.ToString()); mnb.Relay(); return true; }
bool CMasternodeBroadcast::CheckAndUpdate(int& nDos) { // make sure signature isn't in the future (past is OK) if (sigTime > GetAdjustedTime() + 60 * 60) { LogPrintf("mnb - Signature rejected, too far into the future %s\n", vin.ToString()); nDos = 1; return false; } std::string vchPubKey(pubkey.begin(), pubkey.end()); std::string vchPubKey2(pubkey2.begin(), pubkey2.end()); std::string strMessage = addr.ToString() + boost::lexical_cast<std::string>(sigTime) + vchPubKey + vchPubKey2 + boost::lexical_cast<std::string>(protocolVersion); if(protocolVersion < masternodePayments.GetMinMasternodePaymentsProto()) { LogPrintf("mnb - ignoring outdated Masternode %s protocol version %d\n", vin.ToString(), protocolVersion); return false; } CScript pubkeyScript; pubkeyScript = GetScriptForDestination(pubkey.GetID()); if(pubkeyScript.size() != 25) { LogPrintf("mnb - pubkey the wrong size\n"); nDos = 100; return false; } CScript pubkeyScript2; pubkeyScript2 = GetScriptForDestination(pubkey2.GetID()); if(pubkeyScript2.size() != 25) { LogPrintf("mnb - pubkey2 the wrong size\n"); nDos = 100; return false; } if(!vin.scriptSig.empty()) { LogPrintf("mnb - Ignore Not Empty ScriptSig %s\n",vin.ToString()); return false; } std::string errorMessage = ""; if(!darkSendSigner.VerifyMessage(pubkey, sig, strMessage, errorMessage)){ LogPrintf("mnb - Got bad Masternode address signature\n"); nDos = 100; return false; } if(Params().NetworkID() == CBaseChainParams::MAIN) { if(addr.GetPort() != 9999) return false; } else if(addr.GetPort() == 9999) return false; //search existing Masternode list, this is where we update existing Masternodes with new mnb broadcasts CMasternode* pmn = mnodeman.Find(vin); // no such masternode or it's not enabled already, nothing to update if(pmn == NULL || (pmn != NULL && !pmn->IsEnabled())) return true; // mn.pubkey = pubkey, IsVinAssociatedWithPubkey is validated once below, // after that they just need to match if(pmn->pubkey == pubkey && !pmn->IsBroadcastedWithin(MASTERNODE_MIN_MNB_SECONDS)) { //take the newest entry LogPrintf("mnb - Got updated entry for %s\n", addr.ToString()); if(pmn->UpdateFromNewBroadcast((*this))){ pmn->Check(); if(pmn->IsEnabled()) Relay(); } masternodeSync.AddedMasternodeList(GetHash()); } return true; }
bool CActiveMasternode::Register(CTxIn vin, CService service, CKey keyCollateralAddress, CPubKey pubKeyCollateralAddress, CKey keyMasternode, CPubKey pubKeyMasternode, std::string& errorMessage) { CMasternodeBroadcast mnb; CMasternodePing mnp(vin); if (!mnp.Sign(keyMasternode, pubKeyMasternode)) { errorMessage = strprintf("Failed to sign ping, vin: %s", vin.ToString()); LogPrintf("CActiveMasternode::Register() - %s\n", errorMessage); return false; } mnodeman.mapSeenMasternodePing.insert(make_pair(mnp.GetHash(), mnp)); LogPrintf("CActiveMasternode::Register() - Adding to Masternode list\n service: %s\n vin: %s\n", service.ToString(), vin.ToString()); mnb = CMasternodeBroadcast(service, vin, pubKeyCollateralAddress, pubKeyMasternode, PROTOCOL_VERSION); mnb.lastPing = mnp; if (!mnb.Sign(keyCollateralAddress)) { errorMessage = strprintf("Failed to sign broadcast, vin: %s", vin.ToString()); LogPrintf("CActiveMasternode::Register() - %s\n", errorMessage); return false; } mnodeman.mapSeenMasternodeBroadcast.insert(make_pair(mnb.GetHash(), mnb)); masternodeSync.AddedMasternodeList(mnb.GetHash()); CMasternode* pmn = mnodeman.Find(vin); if (pmn == NULL) { CMasternode mn(mnb); mnodeman.Add(mn); } else { pmn->UpdateFromNewBroadcast(mnb); } //send to all peers LogPrintf("CActiveMasternode::Register() - RelayElectionEntry vin = %s\n", vin.ToString()); mnb.Relay(); /* * IT'S SAFE TO REMOVE THIS IN FURTHER VERSIONS * AFTER MIGRATION TO V12 IS DONE */ if (IsSporkActive(SPORK_10_MASTERNODE_PAY_UPDATED_NODES)) return true; // for migration purposes inject our node in old masternodes' list too std::string retErrorMessage; std::vector<unsigned char> vchMasterNodeSignature; int64_t masterNodeSignatureTime = GetAdjustedTime(); std::string donationAddress = ""; int donationPercantage = 0; std::string vchPubKey(pubKeyCollateralAddress.begin(), pubKeyCollateralAddress.end()); std::string vchPubKey2(pubKeyMasternode.begin(), pubKeyMasternode.end()); std::string strMessage = service.ToString() + boost::lexical_cast<std::string>(masterNodeSignatureTime) + vchPubKey + vchPubKey2 + boost::lexical_cast<std::string>(PROTOCOL_VERSION) + donationAddress + boost::lexical_cast<std::string>(donationPercantage); if (!obfuScationSigner.SignMessage(strMessage, retErrorMessage, vchMasterNodeSignature, keyCollateralAddress)) { errorMessage = "dsee sign message failed: " + retErrorMessage; LogPrintf("CActiveMasternode::Register() - Error: %s\n", errorMessage.c_str()); return false; } if (!obfuScationSigner.VerifyMessage(pubKeyCollateralAddress, vchMasterNodeSignature, strMessage, retErrorMessage)) { errorMessage = "dsee verify message failed: " + retErrorMessage; LogPrintf("CActiveMasternode::Register() - Error: %s\n", errorMessage.c_str()); return false; } LOCK(cs_vNodes); BOOST_FOREACH (CNode* pnode, vNodes) pnode->PushMessage("dsee", vin, service, vchMasterNodeSignature, masterNodeSignatureTime, pubKeyCollateralAddress, pubKeyMasternode, -1, -1, masterNodeSignatureTime, PROTOCOL_VERSION, donationAddress, donationPercantage); /* * END OF "REMOVE" */ return true; }
bool CMasternodeBroadcast::CheckAndUpdate(int& nDos) { nDos = 0; // make sure signature isn't in the future (past is OK) if (sigTime > GetAdjustedTime() + 60 * 60) { LogPrintf("mnb - Signature rejected, too far into the future %s\n", vin.ToString()); nDos = 1; return false; } if(protocolVersion < masternodePayments.GetMinMasternodePaymentsProto()) { LogPrintf("mnb - ignoring outdated Masternode %s protocol version %d\n", vin.ToString(), protocolVersion); return false; } CScript pubkeyScript; pubkeyScript = GetScriptForDestination(pubkey.GetID()); if(pubkeyScript.size() != 25) { LogPrintf("mnb - pubkey the wrong size\n"); nDos = 100; return false; } CScript pubkeyScript2; pubkeyScript2 = GetScriptForDestination(pubkey2.GetID()); if(pubkeyScript2.size() != 25) { LogPrintf("mnb - pubkey2 the wrong size\n"); nDos = 100; return false; } if(!vin.scriptSig.empty()) { LogPrintf("mnb - Ignore Not Empty ScriptSig %s\n",vin.ToString()); return false; } std::string strMessage; std::string errorMessage = ""; if(protocolVersion < 70201) { std::string vchPubKey(pubkey.begin(), pubkey.end()); std::string vchPubKey2(pubkey2.begin(), pubkey2.end()); strMessage = addr.ToString(false) + boost::lexical_cast<std::string>(sigTime) + vchPubKey + vchPubKey2 + boost::lexical_cast<std::string>(protocolVersion); LogPrint("masternode", "mnb - sanitized strMessage: %s, pubkey address: %s, sig: %s\n", SanitizeString(strMessage), CBitcoinAddress(pubkey.GetID()).ToString(), EncodeBase64(&sig[0], sig.size())); if(!darkSendSigner.VerifyMessage(pubkey, sig, strMessage, errorMessage)){ if (addr.ToString() != addr.ToString(false)) { // maybe it's wrong format, try again with the old one strMessage = addr.ToString() + boost::lexical_cast<std::string>(sigTime) + vchPubKey + vchPubKey2 + boost::lexical_cast<std::string>(protocolVersion); LogPrint("masternode", "mnb - sanitized strMessage: %s, pubkey address: %s, sig: %s\n", SanitizeString(strMessage), CBitcoinAddress(pubkey.GetID()).ToString(), EncodeBase64(&sig[0], sig.size())); if(!darkSendSigner.VerifyMessage(pubkey, sig, strMessage, errorMessage)){ // didn't work either LogPrintf("mnb - Got bad Masternode address signature, sanitized error: %s\n", SanitizeString(errorMessage)); // there is a bug in old MN signatures, ignore such MN but do not ban the peer we got this from return false; } } else { // nope, sig is actually wrong LogPrintf("mnb - Got bad Masternode address signature, sanitized error: %s\n", SanitizeString(errorMessage)); // there is a bug in old MN signatures, ignore such MN but do not ban the peer we got this from return false; } } } else { strMessage = addr.ToString(false) + boost::lexical_cast<std::string>(sigTime) + pubkey.GetID().ToString() + pubkey2.GetID().ToString() + boost::lexical_cast<std::string>(protocolVersion); LogPrint("masternode", "mnb - strMessage: %s, pubkey address: %s, sig: %s\n", strMessage, CBitcoinAddress(pubkey.GetID()).ToString(), EncodeBase64(&sig[0], sig.size())); if(!darkSendSigner.VerifyMessage(pubkey, sig, strMessage, errorMessage)){ LogPrintf("mnb - Got bad Masternode address signature, error: %s\n", errorMessage); nDos = 100; return false; } } if(Params().NetworkID() == CBaseChainParams::MAIN) { if(addr.GetPort() != 9887) return false; } else if(addr.GetPort() == 9887) return false; //search existing Masternode list, this is where we update existing Masternodes with new mnb broadcasts CMasternode* pmn = mnodeman.Find(vin); // no such masternode, nothing to update if(pmn == NULL) return true ; else { // this broadcast older than we have, it's bad. if(pmn->sigTime > sigTime) { LogPrintf("mnb - Bad sigTime %d for Masternode %20s %105s (existing broadcast is at %d)\n", sigTime, addr.ToString(), vin.ToString(), pmn->sigTime); return false; } // masternode is not enabled yet/already, nothing to update if(!pmn->IsEnabled()) return true; } // mn.pubkey = pubkey, IsVinAssociatedWithPubkey is validated once below, // after that they just need to match if(pmn->pubkey == pubkey && !pmn->IsBroadcastedWithin(MASTERNODE_MIN_MNB_SECONDS)) { //take the newest entry LogPrintf("mnb - Got updated entry for %s\n", addr.ToString()); if(pmn->UpdateFromNewBroadcast((*this))){ pmn->Check(); if(pmn->IsEnabled()) Relay(); } masternodeSync.AddedMasternodeList(GetHash()); } return true; }