/* Check the script signature to confirm it came from a valid prime node key * nTime is the time of the transaction/stake. * entry is the resulting database entry if the script signature is valid. */ bool CPrimeNodeDB::IsPrimeNodeKey(CScript scriptPubKeyType, unsigned int nTime, CPrimeNodeDBEntry &entry) { vector<CPrimeNodeDBEntry> primeNodeDBEntries; Dbc* pcursor = GetCursor(); if (!pcursor) throw runtime_error("IsPrimeNodeKey() : cannot create DB cursor"); unsigned int fFlags = DB_SET_RANGE; for (;;) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); if (fFlags == DB_SET_RANGE) ssKey << make_pair(string("primenode"), string("")); CDataStream ssValue(SER_DISK, CLIENT_VERSION); int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags); fFlags = DB_NEXT; if (ret == DB_NOTFOUND) break; else if (ret != 0) { pcursor->close(); throw runtime_error("IsPrimeNodeKey() : error scanning DB"); } // Unserialize string strType; ssKey >> strType; if (strType != "primenode") break; CPrimeNodeDBEntry dbentry; ssKey >> dbentry.key; ssValue >> dbentry; primeNodeDBEntries.push_back(dbentry); } pcursor->close(); BOOST_FOREACH(CPrimeNodeDBEntry dbentry, primeNodeDBEntries) { vector<unsigned char> vchPubKey = ParseHex(dbentry.key); CKey key; key.SetPubKey(vchPubKey); CScript scriptTime; scriptTime << nTime; uint256 hashScriptTime = Hash(scriptTime.begin(), scriptTime.end()); vector<unsigned char> vchSig; vchSig.insert(vchSig.end(), scriptPubKeyType.begin() + 2, scriptPubKeyType.end()); if(key.Verify(hashScriptTime, vchSig)) { entry = dbentry; return true; } }
bool CAlert::CheckSignature() const { CKey key; if (!key.SetPubKey(ParseHex(fTestNet ? pszTestKey : pszMainKey))) return error("CAlert::CheckSignature() : SetPubKey failed"); if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig)) return error("CAlert::CheckSignature() : verify signature failed"); // Now unserialize the data CDataStream sMsg(vchMsg, SER_NETWORK, PROTOCOL_VERSION); sMsg >> *(CUnsignedAlert*)this; return true; }
// ppcoin: verify signature of sync-checkpoint message bool CSyncCheckpoint::CheckSignature() { CKey key; std::string strMasterPubKey = fTestNet? CSyncCheckpoint::strTestPubKey : CSyncCheckpoint::strMainPubKey; if (!key.SetPubKey(ParseHex(strMasterPubKey))) return error("CSyncCheckpoint::CheckSignature() : SetPubKey failed"); if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig)) return error("CSyncCheckpoint::CheckSignature() : verify signature failed"); // Now unserialize the data CDataStream sMsg(vchMsg, SER_NETWORK, PROTOCOL_VERSION); sMsg >> *(CUnsignedSyncCheckpoint*)this; return true; }
bool RippleAddress::verifyNodePublic (uint256 const& hash, Blob const& vchSig) const { CKey pubkey = CKey (); bool bVerified; if (!pubkey.SetPubKey (getNodePublic ())) { // Failed to set public key. bVerified = false; } else { bVerified = pubkey.Verify (hash, vchSig); } return bVerified; }
bool RippleAddress::verifyNodePublic(const uint256& hash, const std::vector<unsigned char>& vchSig) const { CKey pubkey = CKey(); bool bVerified; if (!pubkey.SetPubKey(getNodePublic())) { // Failed to set public key. bVerified = false; } else { bVerified = pubkey.Verify(hash, vchSig); } return bVerified; }
bool RippleAddress::accountPrivateVerify(const uint256& uHash, const std::vector<unsigned char>& vucSig) const { CKey ckPrivate; bool bVerified; if (!ckPrivate.SetPrivateKeyU(getAccountPrivate())) { // Bad private key. cLog(lsWARNING) << "accountPrivateVerify: Bad private key."; bVerified = false; } else { bVerified = ckPrivate.Verify(uHash, vucSig); } return bVerified; }
bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType) { CKey key; if (!key.SetPubKey(vchPubKey)) return false; // Hash type is one byte tacked on to the end of the signature if (vchSig.empty()) return false; if (nHashType == 0) nHashType = vchSig.back(); else if (nHashType != vchSig.back()) return false; vchSig.pop_back(); return key.Verify(SignatureHash(scriptCode, txTo, nIn, nHashType), vchSig); }
bool RippleAddress::accountPrivateVerify (uint256 const& uHash, Blob const& vucSig) const { CKey ckPrivate; bool bVerified; if (!ckPrivate.SetPrivateKeyU (getAccountPrivate ())) { // Bad private key. WriteLog (lsWARNING, RippleAddress) << "accountPrivateVerify: Bad private key."; bVerified = false; } else { bVerified = ckPrivate.Verify (uHash, vucSig); } return bVerified; }
bool RippleAddress::accountPublicVerify (uint256 const& uHash, Blob const& vucSig, ECDSA fullyCanonical) const { CKey ckPublic; bool bVerified = isCanonicalECDSASig (vucSig, fullyCanonical); if (bVerified && !ckPublic.SetPubKey (getAccountPublic ())) { // Bad private key. WriteLog (lsWARNING, RippleAddress) << "accountPublicVerify: Bad private key."; bVerified = false; } else { bVerified = ckPublic.Verify (uHash, vucSig); } return bVerified; }
bool RippleAddress::verifyNodePublic (uint256 const& hash, Blob const& vchSig, ECDSA fullyCanonical) const { CKey pubkey = CKey (); bool bVerified; bVerified = isCanonicalECDSASig (vchSig, fullyCanonical); if (bVerified && !pubkey.SetPubKey (getNodePublic ())) { // Failed to set public key. bVerified = false; } else { bVerified = pubkey.Verify (hash, vchSig); } return bVerified; }
void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked() { CBitcoinAddress addr(ui->addressIn_VM->text().toStdString()); if (!addr.IsValid()) { ui->addressIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again.")); return; } CKeyID keyID; if (!addr.GetKeyID(keyID)) { ui->addressIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again.")); return; } bool fInvalid = false; std::vector<unsigned char> vchSig = DecodeBase64(ui->signatureIn_VM->text().toStdString().c_str(), &fInvalid); if (fInvalid) { ui->signatureIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The signature could not be decoded.") + QString(" ") + tr("Please check the signature and try again.")); return; } CDataStream ss(SER_GETHASH, 0); ss << strMessageMagic; ss << ui->messageIn_VM->document()->toPlainText().toStdString(); // get the public key from UI fInvalid = false; std::vector<unsigned char> vchPubKey = DecodeBase64(ui->pubkeyIn_VM->text().toStdString().c_str(), &fInvalid); if (fInvalid) { ui->pubkeyIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The public key could not be decoded.") + QString(" ") + tr("Please check it and try again.")); return; } CPubKey pubkey(vchPubKey); if (!pubkey.IsValid()) { ui->pubkeyIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The public key is not valid.") + QString(" ") + tr("Please check it and try again.")); return; } CKey key; if (!key.SetPubKey(pubkey)) { ui->pubkeyIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The public key cannot be added.") + QString(" ") + tr("Please check it and try again.")); return; } if (!key.Verify(HashKeccak(ss.begin(), ss.end()), vchSig)) { ui->signatureIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The signature did not match the message digest.") + QString(" ") + tr("Please check the signature and try again.")); return; } // TODO // add the public key //key.SetPubKey(); if (!(CBitcoinAddress(key.GetPubKey().GetID()) == addr)) { ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>")); return; } ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }"); ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verified.") + QString("</nobr>")); }