From 0b807a417f4a15f3e37ae35e70a72e6169f01c02 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sat, 18 Feb 2012 14:55:02 +0100 Subject: [PATCH 1/3] Add SetMinVersion to CWallet --- src/db.cpp | 1 + src/db.h | 5 +++++ src/wallet.cpp | 31 +++++++++++++++++++++++++++++-- src/wallet.h | 9 +++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/db.cpp b/src/db.cpp index 9a904ec2..ea6d46a6 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -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") { diff --git a/src/db.h b/src/db.h index 27479fef..2611faa4 100644 --- a/src/db.h +++ b/src/db.h @@ -486,6 +486,11 @@ public: 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); diff --git a/src/wallet.cpp b/src/wallet.cpp index 1e769d7e..42c49aa8 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -131,6 +131,32 @@ public: ) }; +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()) @@ -184,10 +210,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. diff --git a/src/wallet.h b/src/wallet.h index 3fdef50c..622ff974 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -25,6 +25,8 @@ private: CWalletDB *pwalletdbEncryption; + int nWalletVersion; + public: mutable CCriticalSection cs_wallet; @@ -33,18 +35,21 @@ public: std::set setKeyPool; + typedef std::map 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; @@ -66,6 +71,8 @@ public: // 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 &vchPubKey, const std::vector &vchCryptedSecret); // Adds an encrypted key to the store, without saving it to disk (used by LoadWallet) @@ -206,6 +213,8 @@ public: bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx); bool SetDefaultKey(const std::vector &vchPubKey); + + bool SetMinVersion(int nVersion, CWalletDB* pwalletdbIn = NULL); }; From 9976cf070fdda61afa30cd65ef5bcddad4f43e81 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sat, 18 Feb 2012 15:02:36 +0100 Subject: [PATCH 2/3] Move GenerateNewKey back to CWallet --- src/keystore.cpp | 10 ---------- src/keystore.h | 2 -- src/wallet.cpp | 10 ++++++++++ src/wallet.h | 2 ++ 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/keystore.cpp b/src/keystore.cpp index 18e5c377..e76f7790 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -8,16 +8,6 @@ #include "db.h" #include "script.h" -std::vector 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 &vchPubKeyOut) const { CKey key; diff --git a/src/keystore.h b/src/keystore.h index 7eea02de..6757834b 100644 --- a/src/keystore.h +++ b/src/keystore.h @@ -29,8 +29,6 @@ public: 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 GenerateNewKey(); virtual bool GetSecret(const CBitcoinAddress &address, CSecret& vchSecret, bool &fCompressed) const { CKey key; diff --git a/src/wallet.cpp b/src/wallet.cpp index 42c49aa8..da64aa51 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -15,6 +15,16 @@ using namespace std; // mapWallet // +std::vector CWallet::GenerateNewKey() +{ + RandAddSeedPerfmon(); + CKey key; + key.MakeNewKey(); + if (!AddKey(key)) + throw std::runtime_error("CWallet::GenerateNewKey() : AddKey failed"); + return key.GetPubKey(); +} + bool CWallet::AddKey(const CKey& key) { if (!CCryptoKeyStore::AddKey(key)) diff --git a/src/wallet.h b/src/wallet.h index 622ff974..fea32977 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -66,6 +66,8 @@ public: std::vector vchDefaultKey; // keystore implementation + // Generate a new key + std::vector 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) From 38067c18f8b54c7121643fa3291ffe81b6eefef1 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sat, 18 Feb 2012 15:06:32 +0100 Subject: [PATCH 3/3] Make compressed pubkeys require 0.6.0 --- src/key.h | 2 +- src/wallet.cpp | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/key.h b/src/key.h index 9e92897e..43c8d845 100644 --- a/src/key.h +++ b/src/key.h @@ -114,7 +114,7 @@ public: 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"); diff --git a/src/wallet.cpp b/src/wallet.cpp index da64aa51..8a33041a 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -17,9 +17,16 @@ using namespace std; std::vector CWallet::GenerateNewKey() { + bool fCompressed = true; // default to compressed public keys + RandAddSeedPerfmon(); CKey key; - key.MakeNewKey(); + 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();