From fd61d6f5068cf92d34569862b4225f177049a4f0 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Mon, 14 May 2012 19:07:52 +0200 Subject: [PATCH] Encapsulate public keys in CPubKey --- src/base58.h | 6 +++--- src/bitcoinrpc.cpp | 20 +++++++++--------- src/init.cpp | 2 +- src/key.cpp | 12 +++++------ src/key.h | 39 ++++++++++++++++++++++++++++++++++-- src/keystore.cpp | 22 ++++++++++---------- src/keystore.h | 8 ++++---- src/main.cpp | 2 +- src/qt/addresstablemodel.cpp | 2 +- src/script.cpp | 2 +- src/script.h | 6 ++++++ src/test/multisig_tests.cpp | 14 ++++++------- src/wallet.cpp | 32 ++++++++++++++--------------- src/wallet.h | 24 +++++++++++----------- src/walletdb.cpp | 2 +- src/walletdb.h | 20 +++++++++--------- 16 files changed, 127 insertions(+), 86 deletions(-) diff --git a/src/base58.h b/src/base58.h index a4ff35c4..26d8c5f9 100644 --- a/src/base58.h +++ b/src/base58.h @@ -275,9 +275,9 @@ public: return true; } - void SetPubKey(const std::vector& vchPubKey) + void SetPubKey(const CPubKey& vchPubKey) { - SetHash160(Hash160(vchPubKey)); + SetHash160(vchPubKey.GetID()); } bool SetScriptHash160(const uint160& hash160) @@ -333,7 +333,7 @@ public: SetHash160(hash160In); } - CBitcoinAddress(const std::vector& vchPubKey) + CBitcoinAddress(const CPubKey& vchPubKey) { SetPubKey(vchPubKey); } diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 8f4fb93a..f227e363 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -590,7 +590,7 @@ Value getnewaddress(const Array& params, bool fHelp) pwalletMain->TopUpKeyPool(); // Generate a new key that is added to wallet - std::vector newKey; + CPubKey newKey; if (!pwalletMain->GetKeyFromPool(newKey, false)) throw JSONRPCError(-12, "Error: Keypool ran out, please call keypoolrefill first"); CBitcoinAddress address(newKey); @@ -611,12 +611,12 @@ CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false) bool bKeyUsed = false; // Check if the current key has been used - if (!account.vchPubKey.empty()) + if (account.vchPubKey.IsValid()) { CScript scriptPubKey; scriptPubKey.SetBitcoinAddress(account.vchPubKey); for (map::iterator it = pwalletMain->mapWallet.begin(); - it != pwalletMain->mapWallet.end() && !account.vchPubKey.empty(); + it != pwalletMain->mapWallet.end() && account.vchPubKey.IsValid(); ++it) { const CWalletTx& wtx = (*it).second; @@ -627,7 +627,7 @@ CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false) } // Generate a new key - if (account.vchPubKey.empty() || bForceNew || bKeyUsed) + if (!account.vchPubKey.IsValid() || bForceNew || bKeyUsed) { if (!pwalletMain->GetKeyFromPool(account.vchPubKey, false)) throw JSONRPCError(-12, "Error: Keypool ran out, please call keypoolrefill first"); @@ -1203,19 +1203,19 @@ Value addmultisigaddress(const Array& params, bool fHelp) if (address.IsScript()) throw runtime_error( strprintf("%s is a pay-to-script address",ks.c_str())); - std::vector vchPubKey; + CPubKey vchPubKey; if (!pwalletMain->GetPubKey(address, vchPubKey)) throw runtime_error( strprintf("no full public key for address %s",ks.c_str())); - if (vchPubKey.empty() || !pubkeys[i].SetPubKey(vchPubKey)) + if (!vchPubKey.IsValid() || !pubkeys[i].SetPubKey(vchPubKey)) throw runtime_error(" Invalid public key: "+ks); } // Case 2: hex public key else if (IsHex(ks)) { - vector vchPubKey = ParseHex(ks); - if (vchPubKey.empty() || !pubkeys[i].SetPubKey(vchPubKey)) + CPubKey vchPubKey(ParseHex(ks)); + if (!vchPubKey.IsValid() || !pubkeys[i].SetPubKey(vchPubKey)) throw runtime_error(" Invalid public key: "+ks); } else @@ -1954,9 +1954,9 @@ Value validateaddress(const Array& params, bool fHelp) if (pwalletMain->HaveKey(address)) { ret.push_back(Pair("ismine", true)); - std::vector vchPubKey; + CPubKey vchPubKey; pwalletMain->GetPubKey(address, vchPubKey); - ret.push_back(Pair("pubkey", HexStr(vchPubKey))); + ret.push_back(Pair("pubkey", HexStr(vchPubKey.Raw()))); CKey key; key.SetPubKey(vchPubKey); ret.push_back(Pair("iscompressed", key.IsCompressed())); diff --git a/src/init.cpp b/src/init.cpp index c01dc270..8295534e 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -604,7 +604,7 @@ bool AppInit2() // Create new keyUser and set as default key RandAddSeedPerfmon(); - std::vector newDefaultKey; + CPubKey newDefaultKey; if (!pwalletMain->GetKeyFromPool(newDefaultKey, false)) strErrors << _("Cannot initialize keypool") << "\n"; pwalletMain->SetDefaultKey(newDefaultKey); diff --git a/src/key.cpp b/src/key.cpp index 9485b477..57ab842b 100644 --- a/src/key.cpp +++ b/src/key.cpp @@ -239,18 +239,18 @@ CPrivKey CKey::GetPrivKey() const return vchPrivKey; } -bool CKey::SetPubKey(const std::vector& vchPubKey) +bool CKey::SetPubKey(const CPubKey& vchPubKey) { - const unsigned char* pbegin = &vchPubKey[0]; - if (!o2i_ECPublicKey(&pkey, &pbegin, vchPubKey.size())) + const unsigned char* pbegin = &vchPubKey.vchPubKey[0]; + if (!o2i_ECPublicKey(&pkey, &pbegin, vchPubKey.vchPubKey.size())) return false; fSet = true; - if (vchPubKey.size() == 33) + if (vchPubKey.vchPubKey.size() == 33) SetCompressedPubKey(); return true; } -std::vector CKey::GetPubKey() const +CPubKey CKey::GetPubKey() const { int nSize = i2o_ECPublicKey(pkey, NULL); if (!nSize) @@ -259,7 +259,7 @@ std::vector CKey::GetPubKey() const unsigned char* pbegin = &vchPubKey[0]; if (i2o_ECPublicKey(pkey, &pbegin) != nSize) throw key_error("CKey::GetPubKey() : i2o_ECPublicKey returned unexpected size"); - return vchPubKey; + return CPubKey(vchPubKey); } bool CKey::Sign(uint256 hash, std::vector& vchSig) diff --git a/src/key.h b/src/key.h index bd58c843..7c3e9d44 100644 --- a/src/key.h +++ b/src/key.h @@ -9,7 +9,9 @@ #include #include "allocators.h" +#include "serialize.h" #include "uint256.h" +#include "util.h" #include // for EC_KEY definition @@ -42,6 +44,39 @@ public: explicit key_error(const std::string& str) : std::runtime_error(str) {} }; +class CPubKey { +private: + std::vector vchPubKey; + friend class CKey; + +public: + CPubKey() { } + CPubKey(const std::vector &vchPubKeyIn) : vchPubKey(vchPubKeyIn) { } + friend bool operator==(const CPubKey &a, const CPubKey &b) { return a.vchPubKey == b.vchPubKey; } + friend bool operator!=(const CPubKey &a, const CPubKey &b) { return a.vchPubKey != b.vchPubKey; } + friend bool operator<(const CPubKey &a, const CPubKey &b) { return a.vchPubKey < b.vchPubKey; } + + IMPLEMENT_SERIALIZE( + READWRITE(vchPubKey); + ) + + uint160 GetID() const { + return Hash160(vchPubKey); + } + + uint256 GetHash() const { + return Hash(vchPubKey.begin(), vchPubKey.end()); + } + + bool IsValid() const { + return vchPubKey.size() == 33 || vchPubKey.size() == 65; + } + + std::vector Raw() const { + return vchPubKey; + } +}; + // secure_allocator is defined in serialize.h // CPrivKey is a serialized private key, with all parameters included (279 bytes) @@ -78,8 +113,8 @@ public: bool SetSecret(const CSecret& vchSecret, bool fCompressed = false); CSecret GetSecret(bool &fCompressed) const; CPrivKey GetPrivKey() const; - bool SetPubKey(const std::vector& vchPubKey); - std::vector GetPubKey() const; + bool SetPubKey(const CPubKey& vchPubKey); + CPubKey GetPubKey() const; bool Sign(uint256 hash, std::vector& vchSig); diff --git a/src/keystore.cpp b/src/keystore.cpp index c56e820e..12ed3027 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -6,7 +6,7 @@ #include "keystore.h" #include "script.h" -bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector &vchPubKeyOut) const +bool CKeyStore::GetPubKey(const CBitcoinAddress &address, CPubKey &vchPubKeyOut) const { CKey key; if (!GetKey(address, key)) @@ -97,10 +97,10 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn) CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin(); for (; mi != mapCryptedKeys.end(); ++mi) { - const std::vector &vchPubKey = (*mi).second.first; + const CPubKey &vchPubKey = (*mi).second.first; const std::vector &vchCryptedSecret = (*mi).second.second; CSecret vchSecret; - if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret)) + if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) return false; if (vchSecret.size() != 32) return false; @@ -128,9 +128,9 @@ bool CCryptoKeyStore::AddKey(const CKey& key) return false; std::vector vchCryptedSecret; - std::vector vchPubKey = key.GetPubKey(); + CPubKey vchPubKey = key.GetPubKey(); bool fCompressed; - if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret)) + if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret)) return false; if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret)) @@ -140,7 +140,7 @@ bool CCryptoKeyStore::AddKey(const CKey& key) } -bool CCryptoKeyStore::AddCryptedKey(const std::vector &vchPubKey, const std::vector &vchCryptedSecret) +bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret) { { LOCK(cs_KeyStore); @@ -162,10 +162,10 @@ bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); if (mi != mapCryptedKeys.end()) { - const std::vector &vchPubKey = (*mi).second.first; + const CPubKey &vchPubKey = (*mi).second.first; const std::vector &vchCryptedSecret = (*mi).second.second; CSecret vchSecret; - if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret)) + if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) return false; if (vchSecret.size() != 32) return false; @@ -177,7 +177,7 @@ bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const return false; } -bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector& vchPubKeyOut) const +bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, CPubKey& vchPubKeyOut) const { { LOCK(cs_KeyStore); @@ -207,10 +207,10 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn) CKey key; if (!key.SetSecret(mKey.second.first, mKey.second.second)) return false; - const std::vector vchPubKey = key.GetPubKey(); + const CPubKey vchPubKey = key.GetPubKey(); std::vector vchCryptedSecret; bool fCompressed; - if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret)) + if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret)) return false; if (!AddCryptedKey(vchPubKey, vchCryptedSecret)) return false; diff --git a/src/keystore.h b/src/keystore.h index 479d6c5a..cd72606d 100644 --- a/src/keystore.h +++ b/src/keystore.h @@ -28,7 +28,7 @@ public: virtual bool HaveKey(const CBitcoinAddress &address) const =0; virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0; virtual void GetKeys(std::set &setAddress) const =0; - virtual bool GetPubKey(const CBitcoinAddress &address, std::vector& vchPubKeyOut) const; + virtual bool GetPubKey(const CBitcoinAddress &address, CPubKey& vchPubKeyOut) const; // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013 virtual bool AddCScript(const CScript& redeemScript) =0; @@ -98,7 +98,7 @@ public: virtual bool GetCScript(const uint160 &hash, CScript& redeemScriptOut) const; }; -typedef std::map, std::vector > > CryptedKeyMap; +typedef std::map > > CryptedKeyMap; /** Keystore which keeps the private keys encrypted. * It derives from the basic key store, which is used if no encryption is active. @@ -146,7 +146,7 @@ public: bool Lock(); - virtual bool AddCryptedKey(const std::vector &vchPubKey, const std::vector &vchCryptedSecret); + virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret); bool AddKey(const CKey& key); bool HaveKey(const CBitcoinAddress &address) const { @@ -159,7 +159,7 @@ public: return false; } bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const; - bool GetPubKey(const CBitcoinAddress &address, std::vector& vchPubKeyOut) const; + bool GetPubKey(const CBitcoinAddress &address, CPubKey& vchPubKeyOut) const; void GetKeys(std::set &setAddress) const { if (!IsCrypted()) diff --git a/src/main.cpp b/src/main.cpp index 96718cf1..9913ae33 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2304,7 +2304,7 @@ unsigned char pchMessageStart[4] = { 0xf9, 0xbe, 0xb4, 0xd9 }; bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) { - static map > mapReuseKey; + static map mapReuseKey; RandAddSeedPerfmon(); if (fDebug) printf("received: %s (%d bytes)\n", strCommand.c_str(), vRecv.size()); diff --git a/src/qt/addresstablemodel.cpp b/src/qt/addresstablemodel.cpp index 75ea2c12..11dfc3e0 100644 --- a/src/qt/addresstablemodel.cpp +++ b/src/qt/addresstablemodel.cpp @@ -331,7 +331,7 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con editStatus = WALLET_UNLOCK_FAILURE; return QString(); } - std::vector newKey; + CPubKey newKey; if(!wallet->GetKeyFromPool(newKey, true)) { editStatus = KEY_GENERATION_FAILURE; diff --git a/src/script.cpp b/src/script.cpp index 0620fba2..20013af1 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -1374,7 +1374,7 @@ bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash return false; else { - valtype vch; + CPubKey vch; keystore.GetPubKey(address, vch); scriptSigRet << vch; } diff --git a/src/script.h b/src/script.h index 5397a197..163ce057 100644 --- a/src/script.h +++ b/src/script.h @@ -320,6 +320,12 @@ public: return *this; } + CScript& operator<<(const CPubKey& key) + { + std::vector vchKey = key.Raw(); + return (*this) << vchKey; + } + CScript& operator<<(const CBigNum& b) { *this << b.getvch(); diff --git a/src/test/multisig_tests.cpp b/src/test/multisig_tests.cpp index 8ae9290f..aa0050ca 100644 --- a/src/test/multisig_tests.cpp +++ b/src/test/multisig_tests.cpp @@ -201,7 +201,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) vector solutions; txnouttype whichType; CScript s; - s << OP_DUP << OP_HASH160 << Hash160(key[0].GetPubKey()) << OP_EQUALVERIFY << OP_CHECKSIG; + s << OP_DUP << OP_HASH160 << Hash160(key[0].GetPubKey().Raw()) << OP_EQUALVERIFY << OP_CHECKSIG; BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK(solutions.size() == 1); CBitcoinAddress addr; @@ -214,7 +214,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) vector solutions; txnouttype whichType; CScript s; - s << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; + s << OP_2 << key[0].GetPubKey() << key[1].GetPubKey().Raw() << OP_2 << OP_CHECKMULTISIG; BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK_EQUAL(solutions.size(), 4); CBitcoinAddress addr; @@ -227,7 +227,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) vector solutions; txnouttype whichType; CScript s; - s << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; + s << OP_1 << key[0].GetPubKey().Raw() << key[1].GetPubKey().Raw() << OP_2 << OP_CHECKMULTISIG; BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK_EQUAL(solutions.size(), 4); vector addrs; @@ -244,7 +244,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) vector solutions; txnouttype whichType; CScript s; - s << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG; + s << OP_2 << key[0].GetPubKey().Raw() << key[1].GetPubKey().Raw() << key[2].GetPubKey().Raw() << OP_3 << OP_CHECKMULTISIG; BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK(solutions.size() == 5); } @@ -262,13 +262,13 @@ BOOST_AUTO_TEST_CASE(multisig_Sign) } CScript a_and_b; - a_and_b << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; + a_and_b << OP_2 << key[0].GetPubKey().Raw() << key[1].GetPubKey().Raw() << OP_2 << OP_CHECKMULTISIG; CScript a_or_b; - a_or_b << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; + a_or_b << OP_1 << key[0].GetPubKey().Raw() << key[1].GetPubKey().Raw() << OP_2 << OP_CHECKMULTISIG; CScript escrow; - escrow << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG; + escrow << OP_2 << key[0].GetPubKey().Raw() << key[1].GetPubKey().Raw() << key[2].GetPubKey().Raw() << OP_3 << OP_CHECKMULTISIG; CTransaction txFrom; // Funding transaction txFrom.vout.resize(3); diff --git a/src/wallet.cpp b/src/wallet.cpp index 62f663c0..2f8d7c05 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -16,7 +16,7 @@ using namespace std; // mapWallet // -std::vector CWallet::GenerateNewKey() +CPubKey CWallet::GenerateNewKey() { bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets @@ -44,7 +44,7 @@ bool CWallet::AddKey(const CKey& key) return true; } -bool CWallet::AddCryptedKey(const vector &vchPubKey, const vector &vchCryptedSecret) +bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const vector &vchCryptedSecret) { if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret)) return false; @@ -366,7 +366,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn) { if (txout.scriptPubKey == scriptDefaultKey) { - std::vector newDefaultKey; + CPubKey newDefaultKey; if (GetKeyFromPool(newDefaultKey, false)) { SetDefaultKey(newDefaultKey); @@ -1095,7 +1095,7 @@ bool CWallet::CreateTransaction(const vector >& vecSend, CW // post-backup change. // Reserve a new key pair from key pool - vector vchPubKey = reservekey.GetReservedKey(); + CPubKey vchPubKey = reservekey.GetReservedKey(); // assert(mapKeys.count(vchPubKey)); // Fill a vout to ourself @@ -1278,7 +1278,7 @@ int CWallet::LoadWallet(bool& fFirstRunRet) if (nLoadWalletRet != DB_LOAD_OK) return nLoadWalletRet; - fFirstRunRet = vchDefaultKey.empty(); + fFirstRunRet = !vchDefaultKey.IsValid(); CreateThread(ThreadFlushWalletDB, &strWalletFile); return DB_LOAD_OK; @@ -1332,7 +1332,7 @@ bool CWallet::GetTransaction(const uint256 &hashTx, CWalletTx& wtx) return false; } -bool CWallet::SetDefaultKey(const std::vector &vchPubKey) +bool CWallet::SetDefaultKey(const CPubKey &vchPubKey) { if (fFileBacked) { @@ -1408,7 +1408,7 @@ bool CWallet::TopUpKeyPool() void CWallet::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool) { nIndex = -1; - keypool.vchPubKey.clear(); + keypool.vchPubKey = CPubKey(); { LOCK(cs_wallet); @@ -1425,9 +1425,9 @@ void CWallet::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool) setKeyPool.erase(setKeyPool.begin()); if (!walletdb.ReadPool(nIndex, keypool)) throw runtime_error("ReserveKeyFromKeyPool() : read failed"); - if (!HaveKey(Hash160(keypool.vchPubKey))) + if (!HaveKey(keypool.vchPubKey.GetID())) throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool"); - assert(!keypool.vchPubKey.empty()); + assert(keypool.vchPubKey.IsValid()); printf("keypool reserve %"PRI64d"\n", nIndex); } } @@ -1468,7 +1468,7 @@ void CWallet::ReturnKey(int64 nIndex) printf("keypool return %"PRI64d"\n", nIndex); } -bool CWallet::GetKeyFromPool(vector& result, bool fAllowReuse) +bool CWallet::GetKeyFromPool(CPubKey& result, bool fAllowReuse) { int64 nIndex = 0; CKeyPool keypool; @@ -1477,7 +1477,7 @@ bool CWallet::GetKeyFromPool(vector& result, bool fAllowReuse) ReserveKeyFromKeyPool(nIndex, keypool); if (nIndex == -1) { - if (fAllowReuse && !vchDefaultKey.empty()) + if (fAllowReuse && vchDefaultKey.IsValid()) { result = vchDefaultKey; return true; @@ -1503,7 +1503,7 @@ int64 CWallet::GetOldestKeyPoolTime() return keypool.nTime; } -vector CReserveKey::GetReservedKey() +CPubKey CReserveKey::GetReservedKey() { if (nIndex == -1) { @@ -1517,7 +1517,7 @@ vector CReserveKey::GetReservedKey() vchPubKey = pwallet->vchDefaultKey; } } - assert(!vchPubKey.empty()); + assert(vchPubKey.IsValid()); return vchPubKey; } @@ -1526,7 +1526,7 @@ void CReserveKey::KeepKey() if (nIndex != -1) pwallet->KeepKey(nIndex); nIndex = -1; - vchPubKey.clear(); + vchPubKey = CPubKey(); } void CReserveKey::ReturnKey() @@ -1534,7 +1534,7 @@ void CReserveKey::ReturnKey() if (nIndex != -1) pwallet->ReturnKey(nIndex); nIndex = -1; - vchPubKey.clear(); + vchPubKey = CPubKey(); } void CWallet::GetAllReserveAddresses(set& setAddress) @@ -1550,7 +1550,7 @@ void CWallet::GetAllReserveAddresses(set& setAddress) if (!walletdb.ReadPool(id, keypool)) throw runtime_error("GetAllReserveKeyHashes() : read failed"); CBitcoinAddress address(keypool.vchPubKey); - assert(!keypool.vchPubKey.empty()); + assert(keypool.vchPubKey.IsValid()); if (!HaveKey(address)) throw runtime_error("GetAllReserveKeyHashes() : unknown key in key pool"); setAddress.insert(address); diff --git a/src/wallet.h b/src/wallet.h index 57633c4a..6c99b3c4 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -32,14 +32,14 @@ class CKeyPool { public: int64 nTime; - std::vector vchPubKey; + CPubKey vchPubKey; CKeyPool() { nTime = GetTime(); } - CKeyPool(const std::vector& vchPubKeyIn) + CKeyPool(const CPubKey& vchPubKeyIn) { nTime = GetTime(); vchPubKey = vchPubKeyIn; @@ -107,14 +107,14 @@ public: std::map mapAddressBook; - std::vector vchDefaultKey; + CPubKey vchDefaultKey; // check whether we are allowed to upgrade (or already support) to the named feature bool CanSupportFeature(enum WalletFeature wf) { return nWalletMaxVersion >= wf; } // keystore implementation // Generate a new key - std::vector GenerateNewKey(); + CPubKey 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) @@ -123,9 +123,9 @@ public: 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 std::vector &vchPubKey, const std::vector &vchCryptedSecret); + bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret); // Adds an encrypted key to the store, without saving it to disk (used by LoadWallet) - bool LoadCryptedKey(const std::vector &vchPubKey, const std::vector &vchCryptedSecret) { SetMinVersion(FEATURE_WALLETCRYPT); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); } + bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret) { SetMinVersion(FEATURE_WALLETCRYPT); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); } bool AddCScript(const CScript& redeemScript); bool LoadCScript(const CScript& redeemScript) { return CCryptoKeyStore::AddCScript(redeemScript); } @@ -156,7 +156,7 @@ public: void ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool); void KeepKey(int64 nIndex); void ReturnKey(int64 nIndex); - bool GetKeyFromPool(std::vector &key, bool fAllowReuse=true); + bool GetKeyFromPool(CPubKey &key, bool fAllowReuse=true); int64 GetOldestKeyPoolTime(); void GetAllReserveAddresses(std::set& setAddress); @@ -252,7 +252,7 @@ public: bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx); - bool SetDefaultKey(const std::vector &vchPubKey); + bool SetDefaultKey(const CPubKey &vchPubKey); // signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false); @@ -280,7 +280,7 @@ class CReserveKey protected: CWallet* pwallet; int64 nIndex; - std::vector vchPubKey; + CPubKey vchPubKey; public: CReserveKey(CWallet* pwalletIn) { @@ -295,7 +295,7 @@ public: } void ReturnKey(); - std::vector GetReservedKey(); + CPubKey GetReservedKey(); void KeepKey(); }; @@ -640,7 +640,7 @@ public: class CAccount { public: - std::vector vchPubKey; + CPubKey vchPubKey; CAccount() { @@ -649,7 +649,7 @@ public: void SetNull() { - vchPubKey.clear(); + vchPubKey = CPubKey(); } IMPLEMENT_SERIALIZE diff --git a/src/walletdb.cpp b/src/walletdb.cpp index 2d44e498..34e0b805 100644 --- a/src/walletdb.cpp +++ b/src/walletdb.cpp @@ -104,7 +104,7 @@ void CWalletDB::ListAccountCreditDebit(const string& strAccount, listvchDefaultKey.clear(); + pwallet->vchDefaultKey = CPubKey(); int nFileVersion = 0; vector vWalletUpgrade; bool fIsEncrypted = false; diff --git a/src/walletdb.h b/src/walletdb.h index dee17502..de2b7f3a 100644 --- a/src/walletdb.h +++ b/src/walletdb.h @@ -59,27 +59,27 @@ public: return Erase(std::make_pair(std::string("tx"), hash)); } - bool ReadKey(const std::vector& vchPubKey, CPrivKey& vchPrivKey) + bool ReadKey(const CPubKey& vchPubKey, CPrivKey& vchPrivKey) { vchPrivKey.clear(); - return Read(std::make_pair(std::string("key"), vchPubKey), vchPrivKey); + return Read(std::make_pair(std::string("key"), vchPubKey.Raw()), vchPrivKey); } - bool WriteKey(const std::vector& vchPubKey, const CPrivKey& vchPrivKey) + bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey) { nWalletDBUpdated++; - return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false); + return Write(std::make_pair(std::string("key"), vchPubKey.Raw()), vchPrivKey, false); } - bool WriteCryptedKey(const std::vector& vchPubKey, const std::vector& vchCryptedSecret, bool fEraseUnencryptedKey = true) + bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector& vchCryptedSecret, bool fEraseUnencryptedKey = true) { nWalletDBUpdated++; - if (!Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, false)) + if (!Write(std::make_pair(std::string("ckey"), vchPubKey.Raw()), vchCryptedSecret, false)) return false; if (fEraseUnencryptedKey) { - Erase(std::make_pair(std::string("key"), vchPubKey)); - Erase(std::make_pair(std::string("wkey"), vchPubKey)); + Erase(std::make_pair(std::string("key"), vchPubKey.Raw())); + Erase(std::make_pair(std::string("wkey"), vchPubKey.Raw())); } return true; } @@ -120,10 +120,10 @@ public: return Read(std::string("defaultkey"), vchPubKey); } - bool WriteDefaultKey(const std::vector& vchPubKey) + bool WriteDefaultKey(const CPubKey& vchPubKey) { nWalletDBUpdated++; - return Write(std::string("defaultkey"), vchPubKey); + return Write(std::string("defaultkey"), vchPubKey.Raw()); } bool ReadPool(int64 nPool, CKeyPool& keypool)