Skip to content

Commit

Permalink
Fix Usage of CKeyMetadata , use CKeyMetadata not nCreateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
Tranz5 committed May 21, 2014
1 parent 489f996 commit 8701510
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
28 changes: 14 additions & 14 deletions src/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,44 +41,44 @@ 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;
if (!fFileBacked)
return true;
{
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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down
10 changes: 4 additions & 6 deletions src/walletdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 8701510

Please sign in to comment.