Browse Source

Merge pull request #2781 from sipa/keytimemem

Refactor keytime/metadata and wallet encryption bugfix
miguelfreitas
Pieter Wuille 12 years ago
parent
commit
905738a15a
  1. 36
      src/wallet.cpp
  2. 8
      src/wallet.h
  3. 6
      src/walletdb.cpp
  4. 14
      src/walletdb.h

36
src/wallet.cpp

@ -40,18 +40,20 @@ CPubKey CWallet::GenerateNewKey() @@ -40,18 +40,20 @@ CPubKey CWallet::GenerateNewKey()
SetMinVersion(FEATURE_COMPRPUBKEY);
CPubKey pubkey = secret.GetPubKey();
// Create new metadata
int64 nCreationTime = GetTime();
mapKeyMetadata[pubkey.GetID()] = CKeyMetadata(nCreationTime);
if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
nTimeFirstKey = nCreationTime;
if (!AddKeyPubKey(secret, pubkey))
throw std::runtime_error("CWallet::GenerateNewKey() : AddKey failed");
return pubkey;
}
bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey,
int64 nCreateTime)
bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey)
{
if (!nCreateTime)
nCreateTime = GetTime();
if (!nTimeFirstKey || (nCreateTime < nTimeFirstKey))
nTimeFirstKey = nCreateTime;
if (!CCryptoKeyStore::AddKeyPubKey(secret, pubkey))
return false;
if (!fFileBacked)
@ -59,19 +61,14 @@ bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey, @@ -59,19 +61,14 @@ bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey,
if (!IsCrypted()) {
return CWalletDB(strWalletFile).WriteKey(pubkey,
secret.GetPrivKey(),
nCreateTime);
mapKeyMetadata[pubkey.GetID()]);
}
return true;
}
bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
const vector<unsigned char> &vchCryptedSecret,
int64 nCreateTime)
const vector<unsigned char> &vchCryptedSecret)
{
if (!nCreateTime)
nCreateTime = GetTime();
if (!nTimeFirstKey || (nCreateTime < nTimeFirstKey))
nTimeFirstKey = nCreateTime;
if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
return false;
if (!fFileBacked)
@ -81,15 +78,24 @@ bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, @@ -81,15 +78,24 @@ bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
if (pwalletdbEncryption)
return pwalletdbEncryption->WriteCryptedKey(vchPubKey,
vchCryptedSecret,
nCreateTime);
mapKeyMetadata[vchPubKey.GetID()]);
else
return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey,
vchCryptedSecret,
nCreateTime);
mapKeyMetadata[vchPubKey.GetID()]);
}
return false;
}
bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta)
{
if (meta.nCreateTime && (!nTimeFirstKey || meta.nCreateTime < nTimeFirstKey))
nTimeFirstKey = meta.nCreateTime;
mapKeyMetadata[pubkey.GetID()] = meta;
return true;
}
bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
{
return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);

8
src/wallet.h

@ -87,7 +87,7 @@ public: @@ -87,7 +87,7 @@ public:
std::string strWalletFile;
std::set<int64> setKeyPool;
std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
MasterKeyMap mapMasterKeys;
@ -140,14 +140,16 @@ public: @@ -140,14 +140,16 @@ public:
// Generate a new key
CPubKey GenerateNewKey();
// Adds a key to the store, and saves it to disk.
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey, int64 nCreateTime = 0);
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
// Adds a key to the store, without saving it to disk (used by LoadWallet)
bool LoadKey(const CKey& key, const CPubKey &pubkey) { return CCryptoKeyStore::AddKeyPubKey(key, pubkey); }
// Load metadata (used by LoadWallet)
bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
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);
bool AddCScript(const CScript& redeemScript);

6
src/walletdb.cpp

@ -344,12 +344,14 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, @@ -344,12 +344,14 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
}
else if (strType == "keymeta")
{
vector<unsigned char> vchPubKey;
CPubKey vchPubKey;
ssKey >> vchPubKey;
CKeyMetadata keyMeta;
ssValue >> keyMeta;
wss.nKeyMeta++;
pwallet->LoadKeyMetadata(vchPubKey, keyMeta);
// find earliest key creation time, as wallet birthday
if (!pwallet->nTimeFirstKey ||
(keyMeta.nCreateTime < pwallet->nTimeFirstKey))
@ -483,7 +485,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet) @@ -483,7 +485,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
// nTimeFirstKey is only reliable if all keys have metadata
if ((wss.nKeys + wss.nCKeys) != wss.nKeyMeta)
pwallet->nTimeFirstKey = 0;
pwallet->nTimeFirstKey = 1; // 0 would be considered 'no value'
BOOST_FOREACH(uint256 hash, wss.vWalletUpgrade)
WriteTx(hash, pwallet->mapWallet[hash]);

14
src/walletdb.h

@ -30,7 +30,7 @@ class CKeyMetadata @@ -30,7 +30,7 @@ class CKeyMetadata
public:
static const int CURRENT_VERSION=1;
int nVersion;
int64 nCreateTime;
int64 nCreateTime; // 0 means unknown
CKeyMetadata()
{
@ -52,7 +52,7 @@ public: @@ -52,7 +52,7 @@ public:
void SetNull()
{
nVersion = CKeyMetadata::CURRENT_VERSION;
nCreateTime = GetTime();
nCreateTime = 0;
}
};
@ -84,13 +84,12 @@ public: @@ -84,13 +84,12 @@ public:
}
bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey,
int64 nCreateTime)
const CKeyMetadata &keyMeta)
{
nWalletDBUpdated++;
CKeyMetadata keyMeta(nCreateTime);
if (!Write(std::make_pair(std::string("keymeta"), vchPubKey),
keyMeta, false))
keyMeta))
return false;
return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false);
@ -98,14 +97,13 @@ public: @@ -98,14 +97,13 @@ public:
bool WriteCryptedKey(const CPubKey& vchPubKey,
const std::vector<unsigned char>& vchCryptedSecret,
int64 nCreateTime)
const CKeyMetadata &keyMeta)
{
const bool fEraseUnencryptedKey = true;
nWalletDBUpdated++;
CKeyMetadata keyMeta(nCreateTime);
if (!Write(std::make_pair(std::string("keymeta"), vchPubKey),
keyMeta, false))
keyMeta))
return false;
if (!Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, false))

Loading…
Cancel
Save