Skip to content

Commit

Permalink
Merge pull request #864 from sipa/fix_856
Browse files Browse the repository at this point in the history
Make compressed pubkeys require client >=0.5.99
  • Loading branch information
sipa committed Feb 20, 2012
2 parents 49355d9 + 38067c1 commit 3a4d817
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ int CWalletDB::LoadWallet(CWallet* pwallet)
ssValue >> nMinVersion;
if (nMinVersion > CLIENT_VERSION)
return DB_TOO_NEW;
pwallet->LoadMinVersion(nMinVersion);
}
else if (strType == "cscript")
{
Expand Down
5 changes: 5 additions & 0 deletions src/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,11 @@ class CWalletDB : public CDB
return Write(std::make_pair(std::string("setting"), strKey), value);
}

bool WriteMinVersion(int nVersion)
{
return Write(std::string("minversion"), nVersion);
}

bool ReadAccount(const std::string& strAccount, CAccount& account);
bool WriteAccount(const std::string& strAccount, const CAccount& account);
bool WriteAccountingEntry(const CAccountingEntry& acentry);
Expand Down
2 changes: 1 addition & 1 deletion src/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class CKey
return fCompressedPubKey;
}

void MakeNewKey(bool fCompressed = true)
void MakeNewKey(bool fCompressed)
{
if (!EC_KEY_generate_key(pkey))
throw key_error("CKey::MakeNewKey() : EC_KEY_generate_key failed");
Expand Down
10 changes: 0 additions & 10 deletions src/keystore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@
#include "db.h"
#include "script.h"

std::vector<unsigned char> CKeyStore::GenerateNewKey()
{
RandAddSeedPerfmon();
CKey key;
key.MakeNewKey();
if (!AddKey(key))
throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
return key.GetPubKey();
}

bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
{
CKey key;
Expand Down
2 changes: 0 additions & 2 deletions src/keystore.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class CKeyStore
virtual bool HaveCScript(const uint160 &hash) const =0;
virtual bool GetCScript(const uint160 &hash, CScript& redeemScriptOut) const =0;

// Generate a new key, and add it to the store
virtual std::vector<unsigned char> GenerateNewKey();
virtual bool GetSecret(const CBitcoinAddress &address, CSecret& vchSecret, bool &fCompressed) const
{
CKey key;
Expand Down
48 changes: 46 additions & 2 deletions src/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ using namespace std;
// mapWallet
//

std::vector<unsigned char> CWallet::GenerateNewKey()
{
bool fCompressed = true; // default to compressed public keys

RandAddSeedPerfmon();
CKey key;
key.MakeNewKey(fCompressed);

// Compressed public keys were introduced in version 0.6.0
if (fCompressed)
SetMinVersion(59900);

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

bool CWallet::AddKey(const CKey& key)
{
if (!CCryptoKeyStore::AddKey(key))
Expand Down Expand Up @@ -131,6 +148,32 @@ class CCorruptAddress
)
};

bool CWallet::SetMinVersion(int nVersion, CWalletDB* pwalletdbIn)
{
if (nWalletVersion >= nVersion)
return true;

nWalletVersion = nVersion;

if (fFileBacked)
{
CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile);
if (nWalletVersion >= 40000)
{
// Versions prior to 0.4.0 did not support the "minversion" record.
// Use a CCorruptAddress to make them crash instead.
CCorruptAddress corruptAddress;
pwalletdb->WriteSetting("addrIncoming", corruptAddress);
}
if (nWalletVersion > 40000)
pwalletdb->WriteMinVersion(nWalletVersion);
if (!pwalletdbIn)
delete pwalletdb;
}

return true;
}

bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
{
if (IsCrypted())
Expand Down Expand Up @@ -184,10 +227,11 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
exit(1); //We now probably have half of our keys encrypted in memory, and half not...die and let the user reload their unencrypted wallet.
}

// Encryption was introduced in version 0.4.0
SetMinVersion(40000, pwalletdbEncryption);

if (fFileBacked)
{
CCorruptAddress corruptAddress;
pwalletdbEncryption->WriteSetting("addrIncoming", corruptAddress);
if (!pwalletdbEncryption->TxnCommit())
exit(1); //We now have keys encrypted in memory, but no on disk...die to avoid confusion and let the user reload their unencrypted wallet.

Expand Down
11 changes: 11 additions & 0 deletions src/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class CWallet : public CCryptoKeyStore

CWalletDB *pwalletdbEncryption;

int nWalletVersion;

public:
mutable CCriticalSection cs_wallet;

Expand All @@ -33,18 +35,21 @@ class CWallet : public CCryptoKeyStore

std::set<int64> setKeyPool;


typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
MasterKeyMap mapMasterKeys;
unsigned int nMasterKeyMaxID;

CWallet()
{
nWalletVersion = 0;
fFileBacked = false;
nMasterKeyMaxID = 0;
pwalletdbEncryption = NULL;
}
CWallet(std::string strWalletFileIn)
{
nWalletVersion = 0;
strWalletFile = strWalletFileIn;
fFileBacked = true;
nMasterKeyMaxID = 0;
Expand All @@ -61,11 +66,15 @@ class CWallet : public CCryptoKeyStore
std::vector<unsigned char> vchDefaultKey;

// keystore implementation
// Generate a new key
std::vector<unsigned char> GenerateNewKey();
// Adds a key to the store, and saves it to disk.
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); }

bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; return true; }

// Adds an encrypted key to the store, and saves it to disk.
bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
// Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
Expand Down Expand Up @@ -206,6 +215,8 @@ class CWallet : public CCryptoKeyStore
bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx);

bool SetDefaultKey(const std::vector<unsigned char> &vchPubKey);

bool SetMinVersion(int nVersion, CWalletDB* pwalletdbIn = NULL);
};


Expand Down

0 comments on commit 3a4d817

Please sign in to comment.