// // Sign a CAlert and serialize it // bool SignAndSave(CAlert &alert) { // Sign if(!alert.Sign()) { printf("SignAndSave() : could not sign alert:\n%s", alert.ToString().c_str()); return false; } std::string strFilePath = "src/test/data/alertTests.raw"; // open output file and associate it with CAutoFile FILE *file = fopen(strFilePath.c_str(), "ab+"); CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); if (fileout.IsNull()) return error("%s: Failed to open file %s", __func__, strFilePath); try { fileout << alert; } catch (std::exception &e) { return error("%s: Serialize or I/O error - %s", __func__, e.what()); } fileout.fclose(); return true; }
void SignAndSave(CAlert alert, const std::string filename) { //serialze alert message CDataStream sMsg(SER_NETWORK, PROTOCOL_VERSION); sMsg << (CUnsignedAlert)alert; alert.vchMsg.reserve(sMsg.size()); for(size_t i=0; i<sMsg.size(); i++) { alert.vchMsg.push_back(sMsg[i]); } //a dummy secret key with the public key //0469204F0E1800E16C1F85176BDC27A245F09987DB71A1EF5C4BD48A42F9AFD1D74F21469488DB552B594AC29CE667AD60DAAD0FFBCE03FB0C2AC49FFB07B36DC5 //set to match the mainnet vAlertPubKey from chainparams.cpp const std::vector<unsigned char> secretKey = ParseHex("1A4976EE6174D80B8B3FF5E9B9C6E638CABFA9A5975557FEA967BC089A5584EE"); CKey secret; secret.Set(secretKey.begin(), secretKey.end(), false); assert(secret.IsValid()); //sign alert secret.Sign(alert.GetHash(), alert.vchSig); assert(alert.CheckSignature()); //serialize alert CDataStream ss(SER_DISK, CLIENT_VERSION); ss << alert; //write alert std::ofstream fs; fs.open(filename.c_str(), std::ios::out | std::ios::app | std::ios::binary); fs.write((char*)&ss[0], ss.size()); fs.close(); }
// ppcoin: send alert. // There is a known deadlock situation with ThreadMessageHandler // ThreadMessageHandler: holds cs_vSend and acquiring cs_main in SendMessages() // ThreadRPCServer: holds cs_main and acquiring cs_vSend in alert.RelayTo()/PushMessage()/BeginMessage() UniValue sendalert(const UniValue& params, bool fHelp) { if (fHelp || params.size() < 6) throw runtime_error( "sendalert <message> <privatekey> <minver> <maxver> <priority> <id> [cancelupto]\n" "\n" "<message> ----> is the alert text message\n" "<privatekey> -> is hex string of alert master private key\n" "<minver> -----> is the minimum applicable internal client version\n" "<maxver> -----> is the maximum applicable internal client version\n" "<priority> ---> is integer priority number\n" "<id> ---------> is the alert id\n" "[cancelupto] -> cancels all alert id's up to this number\n" "\n" "Returns true or false\n"); CAlert alert; CKey key; alert.strStatusBar = params[0].get_str(); alert.nMinVer = params[2].get_int(); alert.nMaxVer = params[3].get_int(); alert.nPriority = params[4].get_int(); alert.nID = params[5].get_int(); if (params.size() > 6) alert.nCancel = params[6].get_int(); alert.nVersion = PROTOCOL_VERSION; alert.nRelayUntil = GetAdjustedTime() + 365*24*60*60; alert.nExpiration = GetAdjustedTime() + 365*24*60*60; CDataStream sMsg(SER_NETWORK, PROTOCOL_VERSION); sMsg << (CUnsignedAlert)alert; alert.vchMsg = vector<unsigned char>(sMsg.begin(), sMsg.end()); vector<unsigned char> vchPrivKey = ParseHex(params[1].get_str()); key.SetPrivKey(CPrivKey(vchPrivKey.begin(), vchPrivKey.end())); // if key is not correct openssl may crash if (!key.Sign(Hash(alert.vchMsg.begin(), alert.vchMsg.end()), alert.vchSig)) throw runtime_error( "Unable to sign alert, check private key?\n"); if(!alert.ProcessAlert()) throw runtime_error( "Failed to process alert.\n"); // Relay alert { LOCK(cs_vNodes); for (auto const& pnode : vNodes) alert.RelayTo(pnode); } UniValue result(UniValue::VOBJ); result.pushKV("strStatusBar", alert.strStatusBar); result.pushKV("nVersion", alert.nVersion); result.pushKV("nMinVer", alert.nMinVer); result.pushKV("nMaxVer", alert.nMaxVer); result.pushKV("nPriority", alert.nPriority); result.pushKV("nID", alert.nID); if (alert.nCancel > 0) result.pushKV("nCancel", alert.nCancel); return result; }
void ClientModel::updateAlert(const QString& hash, int status) { // Show error message notification for new alert if (status == CT_NEW) { uint256 hash_256; hash_256.SetHex(hash.toStdString()); CAlert alert = CAlert::getAlertByHash(hash_256); if (!alert.IsNull()) { emit message(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), CClientUIInterface::ICON_ERROR); } } emit alertsChanged(getStatusBarWarnings()); }
bool CAlert::Cancels(const CAlert& alert) const { if (!alert.IsInEffect()) return false; // this was a no-op before 31403 return (alert.setCancel.count(alert.nID) == 0); }
void ClientModel::updateAlert(const QString &hash, int status) { // Show error message notification for new alert if(status == CT_NEW) { uint256 hash_256; hash_256.SetHex(hash.toStdString()); CAlert alert = CAlert::getAlertByHash(hash_256); if(!alert.IsNull()) { emit error(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), false); } } // Emit a numBlocksChanged when the status message changes, // so that the view recomputes and updates the status bar. emit numBlocksChanged(getNumBlocks(), getNumBlocksOfPeers()); }
void ThreadSendAlert(CConnman& connman) { if (!mapArgs.count("-sendalert") && !mapArgs.count("-printalert")) return; // Wait one minute so we get well connected. If we only need to print // but not to broadcast - do this right away. if (mapArgs.count("-sendalert")) MilliSleep(60*1000); // // Alerts are relayed around the network until nRelayUntil, flood // filling to every node. // After the relay time is past, new nodes are told about alerts // when they connect to peers, until either nExpiration or // the alert is cancelled by a newer alert. // Nodes never save alerts to disk, they are in-memory-only. // CAlert alert; alert.nRelayUntil = GetAdjustedTime() + 15 * 60; alert.nExpiration = GetAdjustedTime() + 30 * 60 * 60; alert.nID = 1; // keep track of alert IDs somewhere alert.nCancel = 0; // cancels previous messages up to this ID number // These versions are protocol versions alert.nMinVer = 70000; alert.nMaxVer = 70103; // // 1000 for Misc warnings like out of disk space and clock is wrong // 2000 for longer invalid proof-of-work chain // Higher numbers mean higher priority alert.nPriority = 5000; alert.strComment = ""; alert.strStatusBar = "URGENT: Upgrade required: see https://www.terracoin.io"; // Set specific client version/versions here. If setSubVer is empty, no filtering on subver is done: // alert.setSubVer.insert(std::string("/Terracoin Core:0.12.0.58/")); // Sign if(!alert.Sign()) { LogPrintf("ThreadSendAlert() : could not sign alert\n"); return; } // Test CDataStream sBuffer(SER_NETWORK, CLIENT_VERSION); sBuffer << alert; CAlert alert2; sBuffer >> alert2; if (!alert2.CheckSignature(Params().AlertKey())) { printf("ThreadSendAlert() : CheckSignature failed\n"); return; } assert(alert2.vchMsg == alert.vchMsg); assert(alert2.vchSig == alert.vchSig); alert.SetNull(); printf("\nThreadSendAlert:\n"); printf("hash=%s\n", alert2.GetHash().ToString().c_str()); printf("%s", alert2.ToString().c_str()); printf("vchMsg=%s\n", HexStr(alert2.vchMsg).c_str()); printf("vchSig=%s\n", HexStr(alert2.vchSig).c_str()); // Confirm if (!mapArgs.count("-sendalert")) return; while (connman.GetNodeCount(CConnman::CONNECTIONS_ALL) == 0 && !ShutdownRequested()) MilliSleep(500); if (ShutdownRequested()) return; // Send printf("ThreadSendAlert() : Sending alert\n"); int nSent = 0; { connman.ForEachNode([&alert2, &connman, &nSent](CNode* pnode) { if (alert2.RelayTo(pnode, connman)) { printf("ThreadSendAlert() : Sent alert to %s\n", pnode->addr.ToString().c_str()); nSent++; } }); } printf("ThreadSendAlert() : Alert sent to %d nodes\n", nSent); }
void ThreadSendAlert() { if (!mapArgs.count("-sendalert") && !mapArgs.count("-printalert")) return; MilliSleep(60*1000); // Wait a minute so we get connected // // Alerts are relayed around the network until nRelayUntil, flood // filling to every node. // After the relay time is past, new nodes are told about alerts // when they connect to peers, until either nExpiration or // the alert is cancelled by a newer alert. // Nodes never save alerts to disk, they are in-memory-only. // CAlert alert; alert.nRelayUntil = GetTime() + 15 * 60; alert.nExpiration = GetTime() + 365 * 60 * 60; alert.nID = 1000; // use https://github.com/zcash/zcash/wiki/specification#assigned-numbers to keep track of alert IDs alert.nCancel = 0; // cancels previous messages up to this ID number // These versions are protocol versions // 170002 : 1.0.0 alert.nMinVer = 170002; alert.nMaxVer = 170002; // // main.cpp: // 1000 for Misc warnings like out of disk space and clock is wrong // 2000 for longer invalid proof-of-work chain // Higher numbers mean higher priority // 4000 or higher will put the RPC into safe mode alert.nPriority = 5000; alert.strComment = ""; alert.strStatusBar = "URGENT: Upgrade required: see https://z.cash"; alert.strRPCError = "URGENT: Upgrade required: see https://z.cash"; // Set specific client version/versions here. If setSubVer is empty, no filtering on subver is done: // alert.setSubVer.insert(std::string("/MagicBean:0.7.2/")); // Sign const CChainParams& chainparams = Params(); std::string networkID = chainparams.NetworkIDString(); bool fIsTestNet = networkID.compare("test") == 0; std::vector<unsigned char> vchTmp(ParseHex(fIsTestNet ? pszTestNetPrivKey : pszPrivKey)); CPrivKey vchPrivKey(vchTmp.begin(), vchTmp.end()); CDataStream sMsg(SER_NETWORK, CLIENT_VERSION); sMsg << *(CUnsignedAlert*)&alert; alert.vchMsg = std::vector<unsigned char>(sMsg.begin(), sMsg.end()); CKey key; if (!key.SetPrivKey(vchPrivKey, false)) { printf("ThreadSendAlert() : key.SetPrivKey failed\n"); return; } if (!key.Sign(Hash(alert.vchMsg.begin(), alert.vchMsg.end()), alert.vchSig)) { printf("ThreadSendAlert() : key.Sign failed\n"); return; } // Test CDataStream sBuffer(SER_NETWORK, CLIENT_VERSION); sBuffer << alert; CAlert alert2; sBuffer >> alert2; if (!alert2.CheckSignature(chainparams.AlertKey())) { printf("ThreadSendAlert() : CheckSignature failed\n"); return; } assert(alert2.vchMsg == alert.vchMsg); assert(alert2.vchSig == alert.vchSig); alert.SetNull(); printf("\nThreadSendAlert:\n"); printf("hash=%s\n", alert2.GetHash().ToString().c_str()); printf("%s\n", alert2.ToString().c_str()); printf("vchMsg=%s\n", HexStr(alert2.vchMsg).c_str()); printf("vchSig=%s\n", HexStr(alert2.vchSig).c_str()); // Confirm if (!mapArgs.count("-sendalert")) return; while (vNodes.size() < 1 && !ShutdownRequested()) MilliSleep(500); if (ShutdownRequested()) return; // Send printf("ThreadSendAlert() : Sending alert\n"); int nSent = 0; { LOCK(cs_vNodes); BOOST_FOREACH(CNode* pnode, vNodes) { if (alert2.RelayTo(pnode)) { printf("ThreadSendAlert() : Sent alert to %s\n", pnode->addr.ToString().c_str()); nSent++; } } } printf("ThreadSendAlert() : Alert sent to %d nodes\n", nSent); }
UniValue sendalert2(const UniValue& params, bool fHelp) { if (fHelp || params.size() != 7) throw runtime_error( // 0 1 2 3 4 5 6 "sendalert2 <privatekey> <id> <subverlist> <cancellist> <expire> <priority> <message>\n" "\n" "<privatekey> -> is hex string of alert master private key\n" "<id> ---------> is the unique alert number\n" "<subverlist> -> comma separated list of versions warning applies to\n" "<cancellist> -> comma separated ids of alerts to cancel\n" "<expire> -----> alert expiration in days\n" "<priority> ---> integer, >1000->visible\n" "<message> ---->is the alert text message\n" "\n" "Returns summary of what was done."); CAlert alert; CKey key; alert.strStatusBar = params[6].get_str(); alert.nMinVer = PROTOCOL_VERSION; alert.nMaxVer = PROTOCOL_VERSION; alert.nPriority = params[5].get_int(); alert.nID = params[1].get_int(); alert.nVersion = PROTOCOL_VERSION; alert.nRelayUntil = alert.nExpiration = GetAdjustedTime() + 24*60*60*params[4].get_int(); if(params[2].get_str().length()) { std::vector<std::string> split_subver = split(params[2].get_str(), ","); alert.setSubVer.insert(split_subver.begin(),split_subver.end()); } if(params[3].get_str().length()) { for(std::string &s : split(params[3].get_str(), ",")) { int aver = RoundFromString(s, 0); alert.setCancel.insert(aver); } } CDataStream sMsg(SER_NETWORK, PROTOCOL_VERSION); sMsg << (CUnsignedAlert)alert; alert.vchMsg = vector<unsigned char>(sMsg.begin(), sMsg.end()); vector<unsigned char> vchPrivKey = ParseHex(params[0].get_str()); key.SetPrivKey(CPrivKey(vchPrivKey.begin(), vchPrivKey.end())); // if key is not correct openssl may crash if (!key.Sign(Hash(alert.vchMsg.begin(), alert.vchMsg.end()), alert.vchSig)) throw runtime_error( "Unable to sign alert, check private key?\n"); if(!alert.ProcessAlert()) throw runtime_error( "Failed to process alert.\n"); // Relay alert { LOCK(cs_vNodes); for (auto const& pnode : vNodes) alert.RelayTo(pnode); } UniValue result(UniValue::VOBJ); result.pushKV("Content", alert.ToString()); result.pushKV("Success", true); return result; }