X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fkeystore.cpp;h=33b8eb18cea77c71d56277465a02c41841772960;hb=9e58e0a8ca28b15a4bfa677f5b23891972db40fd;hp=5f07bb11e2c1b8b97b6ad9529d15857d5f2d274a;hpb=48564f02c4769f26295bedfc04b796246ad0d0bc;p=novacoin.git diff --git a/src/keystore.cpp b/src/keystore.cpp index 5f07bb1..33b8eb1 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -29,8 +29,20 @@ bool CBasicKeyStore::AddKey(const CKey& key) return true; } +bool CBasicKeyStore::AddMalleableKey(const CMalleableKeyView& keyView, const CSecret& vchSecretH) +{ + { + LOCK(cs_KeyStore); + mapMalleableKeys[CMalleableKeyView(keyView)] = vchSecretH; + } + return true; +} + bool CBasicKeyStore::AddCScript(const CScript& redeemScript) { + if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE) + return error("CBasicKeyStore::AddCScript() : redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE); + { LOCK(cs_KeyStore); mapScripts[redeemScript.GetID()] = redeemScript; @@ -145,12 +157,14 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn) if (vchSecret.size() != 32) return false; CKey key; - key.SetPubKey(vchPubKey); key.SetSecret(vchSecret); + if (vchPubKey.size() == 33) + key.SetCompressedPubKey(); if (key.GetPubKey() == vchPubKey) break; return false; } + vMasterKey = vMasterKeyIn; } NotifyStatusChanged(this); @@ -186,6 +200,28 @@ bool CCryptoKeyStore::AddKey(const CKey& key) return true; } +bool CCryptoKeyStore::AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH) +{ + { + LOCK(cs_KeyStore); + if (!SetCrypted()) + return CBasicKeyStore::AddMalleableKey(keyView, vchSecretH); + + if (IsLocked()) + return false; + + CKey keyH; + keyH.SetSecret(vchSecretH, true); + + std::vector vchCryptedSecretH; + if (!EncryptSecret(vMasterKey, vchSecretH, keyH.GetPubKey().GetHash(), vchCryptedSecretH)) + return false; + + if (!AddCryptedMalleableKey(keyView, vchCryptedSecretH)) + return false; + } + return true; +} bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret) { @@ -199,6 +235,71 @@ bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector< return true; } +bool CCryptoKeyStore::AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector &vchCryptedSecretH) +{ + { + LOCK(cs_KeyStore); + if (!SetCrypted()) + return false; + + mapCryptedMalleableKeys[CMalleableKeyView(keyView)] = vchCryptedSecretH; + } + return true; +} + +bool CCryptoKeyStore::CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const +{ + { + LOCK(cs_KeyStore); + if (!IsCrypted()) + return CBasicKeyStore::CreatePrivKey(pubKeyVariant, R, privKey); + + for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++) + { + if (mi->first.CheckKeyVariant(R, pubKeyVariant)) + { + const CPubKey H = mi->first.GetMalleablePubKey().GetH(); + + CSecret vchSecretH; + if (!DecryptSecret(vMasterKey, mi->second, H.GetHash(), vchSecretH)) + return false; + if (vchSecretH.size() != 32) + return false; + + CMalleableKey mKey = mi->first.GetMalleableKey(vchSecretH); + return mKey.CheckKeyVariant(R, pubKeyVariant, privKey);; + } + } + + } + return true; +} + +bool CCryptoKeyStore::GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const +{ + { + LOCK(cs_KeyStore); + if (!IsCrypted()) + return CBasicKeyStore::GetMalleableKey(keyView, mKey); + CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.find(keyView); + if (mi != mapCryptedMalleableKeys.end()) + { + const CPubKey H = keyView.GetMalleablePubKey().GetH(); + + CSecret vchSecretH; + if (!DecryptSecret(vMasterKey, mi->second, H.GetHash(), vchSecretH)) + return false; + + if (vchSecretH.size() != 32) + return false; + mKey = mi->first.GetMalleableKey(vchSecretH); + + return true; + } + } + return false; +} + bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const { { @@ -216,8 +317,9 @@ bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const return false; if (vchSecret.size() != 32) return false; - keyOut.SetPubKey(vchPubKey); keyOut.SetSecret(vchSecret); + if (vchPubKey.size() == 33) + keyOut.SetCompressedPubKey(); return true; } } @@ -263,6 +365,64 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn) return false; } mapKeys.clear(); + + BOOST_FOREACH(MalleableKeyMap::value_type& mKey, mapMalleableKeys) + { + const CPubKey vchPubKeyH = mKey.first.GetMalleablePubKey().GetH(); + std::vector vchCryptedSecretH; + if (!EncryptSecret(vMasterKeyIn, mKey.second, vchPubKeyH.GetHash(), vchCryptedSecretH)) + return false; + if (!AddCryptedMalleableKey(mKey.first, vchCryptedSecretH)) + return false; + } + mapMalleableKeys.clear(); + } + return true; +} + +bool CCryptoKeyStore::DecryptKeys(const CKeyingMaterial& vMasterKeyIn) +{ + { + LOCK(cs_KeyStore); + if (!IsCrypted()) + return false; + + CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin(); + for (; mi != mapCryptedKeys.end(); ++mi) + { + const CPubKey &vchPubKey = (*mi).second.first; + const std::vector &vchCryptedSecret = (*mi).second.second; + CSecret vchSecret; + if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) + return false; + if (vchSecret.size() != 32) + return false; + CKey key; + key.SetSecret(vchSecret); + if (vchPubKey.size() == 33) + key.SetCompressedPubKey(); + if (!CBasicKeyStore::AddKey(key)) + return false; + } + + mapCryptedKeys.clear(); + + CryptedMalleableKeyMap::const_iterator mi2 = mapCryptedMalleableKeys.begin(); + for(; mi2 != mapCryptedMalleableKeys.end(); ++mi2) + { + const CPubKey vchPubKeyH = mi2->first.GetMalleablePubKey().GetH(); + + CSecret vchSecretH; + if(!DecryptSecret(vMasterKeyIn, mi2->second, vchPubKeyH.GetHash(), vchSecretH)) + return false; + if (vchSecretH.size() != 32) + return false; + + if (!CBasicKeyStore::AddMalleableKey(mi2->first, vchSecretH)) + return false; + } + mapCryptedMalleableKeys.clear(); } + return true; }