Created
September 2, 2014 14:41
-
-
Save lukem512/9b272bd35e2cdefbf386 to your computer and use it in GitHub Desktop.
Generate Alert Tests in Bitcoin Core (SignAndSave alternative)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sign CAlert with stored private key | |
bool SignAlert(CAlert &alert) | |
{ | |
// key in WIF format | |
const char* pszPrivKey = ""; | |
// serialize alert data | |
CDataStream sMsg(SER_NETWORK, PROTOCOL_VERSION); | |
sMsg << *(CUnsignedAlert*)&alert; | |
alert.vchMsg = std::vector<unsigned char>(sMsg.begin(), sMsg.end()); | |
// sign alert | |
CBitcoinSecret vchSecret; | |
if (!vchSecret.SetString(pszPrivKey)) | |
{ | |
LogPrintf("SignAlert() : vchSecret.SetString failed\n"); | |
return false; | |
} | |
CKey key = vchSecret.GetKey(); | |
if (!key.Sign(Hash(alert.vchMsg.begin(), alert.vchMsg.end()), alert.vchSig)) | |
{ | |
LogPrintf("SignAlert() : key.Sign failed\n"); | |
return false; | |
} | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sign a CAlert and serialize it | |
bool SignAndSerialize(CAlert &alert, CDataStream &buffer) | |
{ | |
// Sign | |
if(!SignAlert(alert)) | |
{ | |
LogPrintf("SignAndSerialize() : could not sign alert\n"); | |
return false; | |
} | |
// ...and save! | |
buffer << alert; | |
return true; | |
} | |
void GenerateAlertTests() | |
{ | |
CDataStream sBuffer(SER_DISK, CLIENT_VERSION); | |
CAlert alert; | |
alert.nRelayUntil = 60; | |
alert.nExpiration = 24 * 60 * 60; | |
alert.nID = 1; | |
alert.nCancel = 0; // cancels previous messages up to this ID number | |
alert.nMinVer = 0; // These versions are protocol versions | |
alert.nMaxVer = 999001; | |
alert.nPriority = 1; | |
alert.strComment = "Alert comment"; | |
alert.strStatusBar = "Alert 1"; | |
// Replace SignAndSave with SignAndSerialize | |
SignAndSerialize(alert, sBuffer); | |
// More tests go here ... | |
// Print the hex array, which will become the contents of alertTest.raw.h | |
std::vector<unsigned char> vch = std::vector<unsigned char>(sBuffer.begin(), sBuffer.end()); | |
printf("%s\n", HexStrArray(vch, 8).c_str()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code to output a C-style array of values | |
template<typename T> | |
std::string HexStrArray(const T itbegin, const T itend, int lineLength) | |
{ | |
std::string rv; | |
static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', | |
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; | |
rv.reserve((itend-itbegin)*3); | |
int i = 0; | |
for(T it = itbegin; it < itend; ++it) | |
{ | |
unsigned char val = (unsigned char)(*it); | |
if(it != itbegin) | |
{ | |
if (i % lineLength == 0) | |
rv.push_back('\n'); | |
else | |
rv.push_back(' '); | |
} | |
rv.push_back('0'); | |
rv.push_back('x'); | |
rv.push_back(hexmap[val>>4]); | |
rv.push_back(hexmap[val&15]); | |
rv.push_back(','); | |
i++; | |
} | |
return rv; | |
} | |
template<typename T> | |
inline std::string HexStrArray(const T& vch, int lineLength) | |
{ | |
return HexStrArray(vch.begin(), vch.end(), lineLength); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment