@ -152,6 +152,15 @@ bool CCryptoKeyStore::SetCrypted()
@@ -152,6 +152,15 @@ bool CCryptoKeyStore::SetCrypted()
return true ;
}
bool CCryptoKeyStore : : IsLocked ( ) const
{
if ( ! IsCrypted ( ) ) {
return false ;
}
LOCK ( cs_KeyStore ) ;
return vMasterKey . empty ( ) ;
}
bool CCryptoKeyStore : : Lock ( )
{
if ( ! SetCrypted ( ) )
@ -205,21 +214,23 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
@@ -205,21 +214,23 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
}
bool CCryptoKeyStore : : AddKeyPubKey ( const CKey & key , const CPubKey & pubkey )
{
{
LOCK ( cs_KeyStore ) ;
if ( ! IsCrypted ( ) )
if ( ! IsCrypted ( ) ) {
return CBasicKeyStore : : AddKeyPubKey ( key , pubkey ) ;
}
if ( IsLocked ( ) )
if ( IsLocked ( ) ) {
return false ;
}
std : : vector < unsigned char > vchCryptedSecret ;
CKeyingMaterial vchSecret ( key . begin ( ) , key . end ( ) ) ;
if ( ! EncryptSecret ( vMasterKey , vchSecret , pubkey . GetHash ( ) , vchCryptedSecret ) )
if ( ! EncryptSecret ( vMasterKey , vchSecret , pubkey . GetHash ( ) , vchCryptedSecret ) ) {
return false ;
}
if ( ! AddCryptedKey ( pubkey , vchCryptedSecret ) )
if ( ! AddCryptedKey ( pubkey , vchCryptedSecret ) ) {
return false ;
}
return true ;
@ -227,23 +238,31 @@ bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
@@ -227,23 +238,31 @@ bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
bool CCryptoKeyStore : : AddCryptedKey ( const CPubKey & vchPubKey , const std : : vector < unsigned char > & vchCryptedSecret )
{
{
LOCK ( cs_KeyStore ) ;
if ( ! SetCrypted ( ) )
if ( ! SetCrypted ( ) ) {
return false ;
}
mapCryptedKeys [ vchPubKey . GetID ( ) ] = make_pair ( vchPubKey , vchCryptedSecret ) ;
}
return true ;
}
bool CCryptoKeyStore : : Get Key( const CKeyID & address , CKey & keyOut ) const
bool CCryptoKeyStore : : Have Key( 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
{
LOCK ( cs_KeyStore ) ;
if ( ! IsCrypted ( ) )
if ( ! IsCrypted ( ) ) {
return CBasicKeyStore : : GetKey ( address , keyOut ) ;
}
CryptedKeyMap : : const_iterator mi = mapCryptedKeys . find ( address ) ;
if ( mi ! = mapCryptedKeys . end ( ) )
@ -252,12 +271,10 @@ bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
@@ -252,12 +271,10 @@ bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
const std : : vector < unsigned char > & vchCryptedSecret = ( * mi ) . second . second ;
return DecryptKey ( vMasterKey , vchCryptedSecret , vchPubKey , keyOut ) ;
}
}
return false ;
}
bool CCryptoKeyStore : : GetPubKey ( const CKeyID & address , CPubKey & vchPubKeyOut ) const
{
{
LOCK ( cs_KeyStore ) ;
if ( ! IsCrypted ( ) )
@ -272,10 +289,21 @@ bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) co
@@ -272,10 +289,21 @@ bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) co
// Check for watch-only pubkeys
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 )
{
{
LOCK ( cs_KeyStore ) ;
if ( ! mapCryptedKeys . empty ( ) | | IsCrypted ( ) )
@ -294,6 +322,5 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
@@ -294,6 +322,5 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
return false ;
}
mapKeys . clear ( ) ;
}
return true ;
}