Merge #11272: CKeystore/CCrypter: move relevant implementation out of the header

dd9bb25 Fix code style in keystore.cpp/crypter.cpp (Jonas Schnelli)
208fda6 CCrypter: move relevant implementation out of the header (Jonas Schnelli)
3155fd2 CKeystore: move relevant implementation out of the header (Jonas Schnelli)

Pull request description:

Tree-SHA512: 4ce73cca5609199b74b8ff2614ee2b6af949545a1332a3a0135c6453c98665d2b0da171c1e390c9a2aec6b12b7fad931ec90084bb7c2defe243786bfc70daf60
This commit is contained in:
Wladimir J. van der Laan 2017-11-09 21:00:19 +01:00
commit e6e3fc3951
No known key found for this signature in database
GPG key ID: 1E4AED62986CD25D
4 changed files with 119 additions and 129 deletions

View file

@ -36,6 +36,33 @@ bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
return true; return true;
} }
bool CBasicKeyStore::HaveKey(const CKeyID &address) const
{
LOCK(cs_KeyStore);
return mapKeys.count(address) > 0;
}
std::set<CKeyID> CBasicKeyStore::GetKeys() const
{
LOCK(cs_KeyStore);
std::set<CKeyID> set_address;
for (const auto& mi : mapKeys) {
set_address.insert(mi.first);
}
return set_address;
}
bool CBasicKeyStore::GetKey(const CKeyID &address, CKey &keyOut) const
{
LOCK(cs_KeyStore);
KeyMap::const_iterator mi = mapKeys.find(address);
if (mi != mapKeys.end()) {
keyOut = mi->second;
return true;
}
return false;
}
bool CBasicKeyStore::AddCScript(const CScript& redeemScript) bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
{ {
if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE) if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)

View file

@ -62,37 +62,9 @@ protected:
public: public:
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override; bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override; bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
bool HaveKey(const CKeyID &address) const override bool HaveKey(const CKeyID &address) const override;
{ std::set<CKeyID> GetKeys() const override;
bool result; bool GetKey(const CKeyID &address, CKey &keyOut) const override;
{
LOCK(cs_KeyStore);
result = (mapKeys.count(address) > 0);
}
return result;
}
std::set<CKeyID> GetKeys() const override
{
LOCK(cs_KeyStore);
std::set<CKeyID> set_address;
for (const auto& mi : mapKeys) {
set_address.insert(mi.first);
}
return set_address;
}
bool GetKey(const CKeyID &address, CKey &keyOut) const override
{
{
LOCK(cs_KeyStore);
KeyMap::const_iterator mi = mapKeys.find(address);
if (mi != mapKeys.end())
{
keyOut = mi->second;
return true;
}
}
return false;
}
bool AddCScript(const CScript& redeemScript) override; bool AddCScript(const CScript& redeemScript) override;
bool HaveCScript(const CScriptID &hash) const override; bool HaveCScript(const CScriptID &hash) const override;
bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override; bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override;

View file

@ -152,6 +152,15 @@ bool CCryptoKeyStore::SetCrypted()
return true; return true;
} }
bool CCryptoKeyStore::IsLocked() const
{
if (!IsCrypted()) {
return false;
}
LOCK(cs_KeyStore);
return vMasterKey.empty();
}
bool CCryptoKeyStore::Lock() bool CCryptoKeyStore::Lock()
{ {
if (!SetCrypted()) if (!SetCrypted())
@ -206,20 +215,22 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey) bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
{ {
{
LOCK(cs_KeyStore); LOCK(cs_KeyStore);
if (!IsCrypted()) if (!IsCrypted()) {
return CBasicKeyStore::AddKeyPubKey(key, pubkey); return CBasicKeyStore::AddKeyPubKey(key, pubkey);
}
if (IsLocked()) if (IsLocked()) {
return false; return false;
}
std::vector<unsigned char> vchCryptedSecret; std::vector<unsigned char> vchCryptedSecret;
CKeyingMaterial vchSecret(key.begin(), key.end()); CKeyingMaterial vchSecret(key.begin(), key.end());
if (!EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCryptedSecret)) if (!EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCryptedSecret)) {
return false; return false;
}
if (!AddCryptedKey(pubkey, vchCryptedSecret)) if (!AddCryptedKey(pubkey, vchCryptedSecret)) {
return false; return false;
} }
return true; return true;
@ -228,22 +239,30 @@ bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
{ {
{
LOCK(cs_KeyStore); LOCK(cs_KeyStore);
if (!SetCrypted()) if (!SetCrypted()) {
return false; return false;
}
mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret); mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
}
return true; return true;
} }
bool CCryptoKeyStore::HaveKey(const CKeyID &address) const
{
LOCK(cs_KeyStore);
if (!IsCrypted()) {
return CBasicKeyStore::HaveKey(address);
}
return mapCryptedKeys.count(address) > 0;
}
bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
{ {
{
LOCK(cs_KeyStore); LOCK(cs_KeyStore);
if (!IsCrypted()) if (!IsCrypted()) {
return CBasicKeyStore::GetKey(address, keyOut); return CBasicKeyStore::GetKey(address, keyOut);
}
CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
if (mi != mapCryptedKeys.end()) if (mi != mapCryptedKeys.end())
@ -252,13 +271,11 @@ bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second; const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
return DecryptKey(vMasterKey, vchCryptedSecret, vchPubKey, keyOut); return DecryptKey(vMasterKey, vchCryptedSecret, vchPubKey, keyOut);
} }
}
return false; return false;
} }
bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
{ {
{
LOCK(cs_KeyStore); LOCK(cs_KeyStore);
if (!IsCrypted()) if (!IsCrypted())
return CBasicKeyStore::GetPubKey(address, vchPubKeyOut); return CBasicKeyStore::GetPubKey(address, vchPubKeyOut);
@ -271,12 +288,23 @@ bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) co
} }
// Check for watch-only pubkeys // Check for watch-only pubkeys
return CBasicKeyStore::GetPubKey(address, vchPubKeyOut); return CBasicKeyStore::GetPubKey(address, vchPubKeyOut);
}
std::set<CKeyID> CCryptoKeyStore::GetKeys() const
{
LOCK(cs_KeyStore);
if (!IsCrypted()) {
return CBasicKeyStore::GetKeys();
} }
std::set<CKeyID> set_address;
for (const auto& mi : mapCryptedKeys) {
set_address.insert(mi.first);
}
return set_address;
} }
bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn) bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
{ {
{
LOCK(cs_KeyStore); LOCK(cs_KeyStore);
if (!mapCryptedKeys.empty() || IsCrypted()) if (!mapCryptedKeys.empty() || IsCrypted())
return false; return false;
@ -294,6 +322,5 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
return false; return false;
} }
mapKeys.clear(); mapKeys.clear();
}
return true; return true;
} }

View file

@ -139,52 +139,16 @@ public:
{ {
} }
bool IsCrypted() const bool IsCrypted() const { return fUseCrypto; }
{ bool IsLocked() const;
return fUseCrypto;
}
bool IsLocked() const
{
if (!IsCrypted())
return false;
bool result;
{
LOCK(cs_KeyStore);
result = vMasterKey.empty();
}
return result;
}
bool Lock(); bool Lock();
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret); virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override; bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
bool HaveKey(const CKeyID &address) const override bool HaveKey(const CKeyID &address) const override;
{
{
LOCK(cs_KeyStore);
if (!IsCrypted()) {
return CBasicKeyStore::HaveKey(address);
}
return mapCryptedKeys.count(address) > 0;
}
return false;
}
bool GetKey(const CKeyID &address, CKey& keyOut) const override; bool GetKey(const CKeyID &address, CKey& keyOut) const override;
bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override; bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
std::set<CKeyID> GetKeys() const override std::set<CKeyID> GetKeys() const override;
{
LOCK(cs_KeyStore);
if (!IsCrypted()) {
return CBasicKeyStore::GetKeys();
}
std::set<CKeyID> set_address;
for (const auto& mi : mapCryptedKeys) {
set_address.insert(mi.first);
}
return set_address;
}
/** /**
* Wallet status (encrypted, locked) changed. * Wallet status (encrypted, locked) changed.