Skip to content
Permalink
Browse files

Fix Usage of CKeyMetadata , use CKeyMetadata not nCreateTime

  • Loading branch information
iamunick committed Jun 6, 2014
1 parent 6b67828 commit 643b047eccdedf273dd76a45ff8f494563fdf169
Showing with 20 additions and 21 deletions.
  1. +14 −13 src/wallet.cpp
  2. +2 −2 src/wallet.h
  3. +4 −6 src/walletdb.h
@@ -42,33 +42,34 @@ CPubKey CWallet::GenerateNewKey()
if (fCompressed)
SetMinVersion(FEATURE_COMPRPUBKEY);

CPubKey pubkey = key.GetPubKey();

// Create new metadata
int64 nCreationTime = GetTime();
mapKeyMetadata[pubkey.GetID()] = CKeyMetadata(nCreationTime);
if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
nTimeFirstKey = nCreationTime;

if (!AddKey(key))
throw std::runtime_error("CWallet::GenerateNewKey() : AddKey failed");
return key.GetPubKey();
}

bool CWallet::AddKey(const CKey& key, int64 nCreateTime)
bool CWallet::AddKey(const CKey& key)
{
if(!nCreateTime)
nCreateTime = GetTime();
if (!nTimeFirstKey || nCreateTime < nTimeFirstKey)
nTimeFirstKey = nCreateTime;
CPubKey pubkey = key.GetPubKey();

if (!CCryptoKeyStore::AddKey(key))
return false;
if (!fFileBacked)
return true;
if (!IsCrypted())
return CWalletDB(strWalletFile).WriteKey(key.GetPubKey(), key.GetPrivKey(), nCreateTime);
return CWalletDB(strWalletFile).WriteKey(pubkey, key.GetPrivKey(), mapKeyMetadata[pubkey.GetID()]);
return true;
}

bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const vector<unsigned char> &vchCryptedSecret, int64 nCreateTime)
bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const vector<unsigned char> &vchCryptedSecret)
{
if(!nCreateTime)
nCreateTime = GetTime();
if (!nTimeFirstKey || nCreateTime < nTimeFirstKey)
nTimeFirstKey = nCreateTime;

if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
return false;
@@ -77,9 +78,9 @@ bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const vector<unsigned char
{
LOCK(cs_wallet);
if (pwalletdbEncryption)
return pwalletdbEncryption->WriteCryptedKey(vchPubKey, vchCryptedSecret, nCreateTime);
return pwalletdbEncryption->WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]);
else
return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey, vchCryptedSecret, nCreateTime);
return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]);
}
return false;
}
@@ -132,7 +132,7 @@ class CWallet : public CCryptoKeyStore
// Generate a new key
CPubKey GenerateNewKey();
// Adds a key to the store, and saves it to disk.
bool AddKey(const CKey& key, int64 nCreateTime = 0);
bool AddKey(const CKey& key);
// Adds a key to the store, without saving it to disk (used by LoadWallet)
bool LoadKey(const CKey& key) { return CCryptoKeyStore::AddKey(key); }
// Load metadata (used by LoadWallet)
@@ -141,7 +141,7 @@ class CWallet : public CCryptoKeyStore
bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }

// Adds an encrypted key to the store, and saves it to disk.
bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret, int64 nCreateTime = 0);
bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
// Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) { SetMinVersion(FEATURE_WALLETCRYPT); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); }
bool AddCScript(const CScript& redeemScript);
@@ -81,24 +81,22 @@ class CWalletDB : public CDB
return Erase(std::make_pair(std::string("tx"), hash));
}

bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, int64 nCreateTime)
bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta)
{
nWalletDBUpdated++;

CKeyMetadata keyMeta(nCreateTime);
if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta, false))
if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta))
return false;

return Write(std::make_pair(std::string("key"), vchPubKey.Raw()), vchPrivKey, false);
}

bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, int64 nCreateTime)
bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta)
{
nWalletDBUpdated++;
bool fEraseUnencryptedKey = true;

CKeyMetadata keyMeta(nCreateTime);
if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta, false))
if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta))
return false;

if (!Write(std::make_pair(std::string("ckey"), vchPubKey.Raw()), vchCryptedSecret, false))

0 comments on commit 643b047

Please sign in to comment.
You can’t perform that action at this time.