bool CWalletDB::WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata& keyMeta) { nWalletDBUpdated++; if (!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta, false)) return false; // hash pubkey/privkey to accelerate wallet load std::vector<unsigned char> vchKey; vchKey.reserve(vchPubKey.size() + vchPrivKey.size()); vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end()); vchKey.insert(vchKey.end(), vchPrivKey.begin(), vchPrivKey.end()); return Write(std::make_pair(std::string("key"), vchPubKey), std::make_pair(vchPrivKey, Hash(vchKey.begin(), vchKey.end())), false); }
bool CKey::SetPrivKey(const CPrivKey& vchPrivKey) { const unsigned char* pbegin = &vchPrivKey[0]; if (!d2i_ECPrivateKey(&pkey, &pbegin, vchPrivKey.size())) return false; fSet = true; return true; }
void CECKey::GetPrivKey(CPrivKey &privkey, bool fCompressed) { EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED); int nSize = i2d_ECPrivateKey(pkey, NULL); assert(nSize); privkey.resize(nSize); unsigned char* pbegin = &privkey[0]; int nSize2 = i2d_ECPrivateKey(pkey, &pbegin); assert(nSize == nSize2); }
bool GenerateBeaconKeys(const std::string &cpid, std::string &sOutPubKey, std::string &sOutPrivKey) { // First Check the Index - if it already exists, use it sOutPrivKey = GetArgument("privatekey" + cpid + GetNetSuffix(), ""); sOutPubKey = GetArgument("publickey" + cpid + GetNetSuffix(), ""); // If current keypair is not empty, but is invalid, allow the new keys to be stored, otherwise return 1: (10-25-2016) if (!sOutPrivKey.empty() && !sOutPubKey.empty()) { uint256 hashBlock = GetRandHash(); std::string sSignature; std::string sError; bool fResult; fResult = SignBlockWithCPID(cpid, hashBlock.GetHex(), sSignature, sError, true); if (!fResult) LogPrintf("GenerateBeaconKeys::Failed to sign block with cpid with existing keys; generating new key pair -> %s", sError); else { fResult = VerifyCPIDSignature(cpid, hashBlock.GetHex(), sSignature); if (fResult) { LogPrintf("GenerateBeaconKeys::Current keypair is valid."); return true; } else LogPrintf("GenerateBeaconKeys::Signing block with CPID was successful; However Verifying CPID Sign was not; Key pair is not valid, generating new key pair"); } } // Generate the Keypair CKey key; key.MakeNewKey(false); CPrivKey vchPrivKey = key.GetPrivKey(); sOutPrivKey = HexStr<CPrivKey::iterator>(vchPrivKey.begin(), vchPrivKey.end()); sOutPubKey = HexStr(key.GetPubKey().Raw()); return true; }
bool CECKey::SetPrivKey(const CPrivKey &privkey, bool fSkipCheck) { const unsigned char* pbegin = &privkey[0]; if (d2i_ECPrivateKey(&pkey, &pbegin, privkey.size())) { if(fSkipCheck) return true; // d2i_ECPrivateKey returns true if parsing succeeds. // This doesn't necessarily mean the key is valid. if (EC_KEY_check_key(pkey)) return true; } return false; }
bool CKey::SetPrivKey(const CPrivKey& vchPrivKey) { const unsigned char* pbegin = &vchPrivKey[0]; if (d2i_ECPrivateKey(&pkey, &pbegin, vchPrivKey.size())) { // In testing, d2i_ECPrivateKey can return true // but fill in pkey with a key that fails // EC_KEY_check_key, so: if (EC_KEY_check_key(pkey)) { fSet = true; return true; } } // If vchPrivKey data is bad d2i_ECPrivateKey() can // leave pkey in a state where calling EC_KEY_free() // crashes. To avoid that, set pkey to NULL and // leak the memory (a leak is better than a crash) pkey = NULL; Reset(); return false; }