Skip to content

Instantly share code, notes, and snippets.

@gavinandresen
Created December 15, 2011 16:30
Show Gist options
  • Save gavinandresen/1481736 to your computer and use it in GitHub Desktop.
Save gavinandresen/1481736 to your computer and use it in GitHub Desktop.
Bitcoin CAlert code (without private key, of course)
#include "headers.h"
#include "net.h"
static const int64 DAYS = 24 * 60 * 60;
void ThreadSendAlert();
class CSendAlert
{
public:
CSendAlert()
{
boost::thread(boost::bind(ThreadSendAlert));
}
~CSendAlert()
{
}
}
instance_of_csendalert;
void ThreadSendAlert()
{
Sleep(2000);
if (!mapArgs.count("-sendalert") && !mapArgs.count("-printalert"))
return;
// Create alert -- set your parameters here
CAlert alert;
alert.nRelayUntil = GetTime() + 15 * 60;
alert.nExpiration = GetTime() + 1 * DAYS;
alert.nID = 1003;
alert.nCancel = 1001; // cancels previous messages up to this ID number
alert.nMinVer = 50000;
alert.nMaxVer = 51000;
alert.nPriority = 5000;
alert.strComment = "";
alert.strStatusBar = "CAlert system test: ver.0.5.1 available";
// alert.strStatusBar = "WALLET ENCRYPTION INSECURE: ver.0.5.1 available";
// Sign
const char* pszPrivKey = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
std::vector<unsigned char> vchTmp(ParseHex(pszPrivKey));
CPrivKey vchPrivKey(vchTmp.begin(), vchTmp.end());
CDataStream sMsg;
sMsg << *(CUnsignedAlert*)&alert;
alert.vchMsg = std::vector<unsigned char>(sMsg.begin(), sMsg.end());
CKey key;
if (!key.SetPrivKey(vchPrivKey))
{
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;
sBuffer << alert;
CAlert alert2;
sBuffer >> alert2;
if (!alert2.CheckSignature())
{
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());
alert2.print();
printf("vchMsg.size()=%d, vchSig.size()=%d\n", alert2.vchMsg.size(), alert2.vchSig.size());
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 && !fShutdown)
Sleep(500);
if (fShutdown)
return;
#ifdef QT_GUI
if (ThreadSafeMessageBox("Send alert?", "ThreadSendAlert", wxYES_NO | wxNO_DEFAULT) != wxYES)
return;
if (ThreadSafeMessageBox("Send alert, are you sure?", "ThreadSendAlert", wxYES_NO | wxNO_DEFAULT) != wxYES)
{
ThreadSafeMessageBox("Nothing sent", "ThreadSendAlert", wxOK);
return;
}
#endif
// Send
printf("ThreadSendAlert() : Sending alert\n");
int nSent = 0;
CRITICAL_BLOCK(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);
ThreadSafeMessageBox(strprintf("Alert sent to %d nodes", nSent), "ThreadSendAlert", wxOK);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment