diff --git a/src/Makefile.am b/src/Makefile.am index 9b7e99861..b2071f49e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -103,7 +103,7 @@ BITCOIN_CORE_H = \ script/script.h \ script/sign.h \ script/standard.h \ - scriptutils.h \ + wallet_ismine.h \ serialize.h \ sync.h \ threadsafety.h \ @@ -173,6 +173,7 @@ libbitcoin_wallet_a_SOURCES = \ crypter.cpp \ rpcdump.cpp \ rpcwallet.cpp \ + wallet_ismine.cpp \ wallet.cpp \ walletdb.cpp \ $(BITCOIN_CORE_H) @@ -216,7 +217,6 @@ libbitcoin_common_a_SOURCES = \ script/script.cpp \ script/sign.cpp \ script/standard.cpp \ - scriptutils.cpp \ $(BITCOIN_CORE_H) # util: shared between all executables. diff --git a/src/test/multisig_tests.cpp b/src/test/multisig_tests.cpp index 6c5afa130..91dfa3950 100644 --- a/src/test/multisig_tests.cpp +++ b/src/test/multisig_tests.cpp @@ -8,9 +8,12 @@ #include "script/script.h" #include "script/interpreter.h" #include "script/sign.h" -#include "scriptutils.h" #include "uint256.h" +#ifdef ENABLE_WALLET +#include "wallet_ismine.h" +#endif + #include #include #include @@ -195,8 +198,10 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) CTxDestination addr; BOOST_CHECK(ExtractDestination(s, addr)); BOOST_CHECK(addr == keyaddr[0]); +#ifdef ENABLE_WALLET BOOST_CHECK(IsMine(keystore, s)); BOOST_CHECK(!IsMine(emptykeystore, s)); +#endif } { vector solutions; @@ -208,8 +213,10 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) CTxDestination addr; BOOST_CHECK(ExtractDestination(s, addr)); BOOST_CHECK(addr == keyaddr[0]); +#ifdef ENABLE_WALLET BOOST_CHECK(IsMine(keystore, s)); BOOST_CHECK(!IsMine(emptykeystore, s)); +#endif } { vector solutions; @@ -220,9 +227,11 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) BOOST_CHECK_EQUAL(solutions.size(), 4U); CTxDestination addr; BOOST_CHECK(!ExtractDestination(s, addr)); +#ifdef ENABLE_WALLET BOOST_CHECK(IsMine(keystore, s)); BOOST_CHECK(!IsMine(emptykeystore, s)); BOOST_CHECK(!IsMine(partialkeystore, s)); +#endif } { vector solutions; @@ -237,9 +246,11 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) BOOST_CHECK(addrs[0] == keyaddr[0]); BOOST_CHECK(addrs[1] == keyaddr[1]); BOOST_CHECK(nRequired == 1); +#ifdef ENABLE_WALLET BOOST_CHECK(IsMine(keystore, s)); BOOST_CHECK(!IsMine(emptykeystore, s)); BOOST_CHECK(!IsMine(partialkeystore, s)); +#endif } { vector solutions; diff --git a/src/test/script_P2SH_tests.cpp b/src/test/script_P2SH_tests.cpp index b7e7487bb..f99002017 100644 --- a/src/test/script_P2SH_tests.cpp +++ b/src/test/script_P2SH_tests.cpp @@ -7,7 +7,10 @@ #include "main.h" #include "script/script.h" #include "script/sign.h" -#include "scriptutils.h" + +#ifdef ENABLE_WALLET +#include "wallet_ismine.h" +#endif #include @@ -95,7 +98,9 @@ BOOST_AUTO_TEST_CASE(sign) txTo[i].vin[0].prevout.n = i; txTo[i].vin[0].prevout.hash = txFrom.GetHash(); txTo[i].vout[0].nValue = 1; +#ifdef ENABLE_WALLET BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i)); +#endif } for (int i = 0; i < 8; i++) { @@ -189,7 +194,9 @@ BOOST_AUTO_TEST_CASE(set) txTo[i].vin[0].prevout.hash = txFrom.GetHash(); txTo[i].vout[0].nValue = 1*CENT; txTo[i].vout[0].scriptPubKey = inner[i]; +#ifdef ENABLE_WALLET BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i)); +#endif } for (int i = 0; i < 4; i++) { diff --git a/src/wallet.cpp b/src/wallet.cpp index 218a13796..52660be9a 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -2086,6 +2086,39 @@ void CWallet::ListLockedCoins(std::vector& vOutpts) } } + +class CAffectedKeysVisitor : public boost::static_visitor { +private: + const CKeyStore &keystore; + std::vector &vKeys; + +public: + CAffectedKeysVisitor(const CKeyStore &keystoreIn, std::vector &vKeysIn) : keystore(keystoreIn), vKeys(vKeysIn) {} + + void Process(const CScript &script) { + txnouttype type; + std::vector vDest; + int nRequired; + if (ExtractDestinations(script, type, vDest, nRequired)) { + BOOST_FOREACH(const CTxDestination &dest, vDest) + boost::apply_visitor(*this, dest); + } + } + + void operator()(const CKeyID &keyId) { + if (keystore.HaveKey(keyId)) + vKeys.push_back(keyId); + } + + void operator()(const CScriptID &scriptId) { + CScript script; + if (keystore.GetCScript(scriptId, script)) + Process(script); + } + + void operator()(const CNoDestination &none) {} +}; + void CWallet::GetKeyBirthTimes(std::map &mapKeyBirth) const { AssertLockHeld(cs_wallet); // mapKeyMetadata mapKeyBirth.clear(); @@ -2121,7 +2154,7 @@ void CWallet::GetKeyBirthTimes(std::map &mapKeyBirth) const { int nHeight = blit->second->nHeight; BOOST_FOREACH(const CTxOut &txout, wtx.vout) { // iterate over all their outputs - ::ExtractAffectedKeys(*this, txout.scriptPubKey, vAffected); + CAffectedKeysVisitor(*this, vAffected).Process(txout.scriptPubKey); BOOST_FOREACH(const CKeyID &keyid, vAffected) { // ... and all their affected keys std::map::iterator rit = mapKeyFirstBlock.find(keyid); diff --git a/src/wallet.h b/src/wallet.h index 6788986f8..5c2618673 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -11,7 +11,7 @@ #include "key.h" #include "keystore.h" #include "main.h" -#include "scriptutils.h" +#include "wallet_ismine.h" #include "ui_interface.h" #include "walletdb.h" diff --git a/src/scriptutils.cpp b/src/wallet_ismine.cpp similarity index 69% rename from src/scriptutils.cpp rename to src/wallet_ismine.cpp index a636eeeda..1c2c117fa 100644 --- a/src/scriptutils.cpp +++ b/src/wallet_ismine.cpp @@ -3,7 +3,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "scriptutils.h" +#include "wallet_ismine.h" #include "key.h" #include "keystore.h" @@ -89,39 +89,3 @@ isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey) return ISMINE_WATCH_ONLY; return ISMINE_NO; } - -class CAffectedKeysVisitor : public boost::static_visitor { -private: - const CKeyStore &keystore; - std::vector &vKeys; - -public: - CAffectedKeysVisitor(const CKeyStore &keystoreIn, std::vector &vKeysIn) : keystore(keystoreIn), vKeys(vKeysIn) {} - - void Process(const CScript &script) { - txnouttype type; - std::vector vDest; - int nRequired; - if (ExtractDestinations(script, type, vDest, nRequired)) { - BOOST_FOREACH(const CTxDestination &dest, vDest) - boost::apply_visitor(*this, dest); - } - } - - void operator()(const CKeyID &keyId) { - if (keystore.HaveKey(keyId)) - vKeys.push_back(keyId); - } - - void operator()(const CScriptID &scriptId) { - CScript script; - if (keystore.GetCScript(scriptId, script)) - Process(script); - } - - void operator()(const CNoDestination &none) {} -}; - -void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector &vKeys) { - CAffectedKeysVisitor(keystore, vKeys).Process(scriptPubKey); -} diff --git a/src/scriptutils.h b/src/wallet_ismine.h similarity index 81% rename from src/scriptutils.h rename to src/wallet_ismine.h index 98080fc45..9915e9f7b 100644 --- a/src/scriptutils.h +++ b/src/wallet_ismine.h @@ -3,8 +3,8 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#ifndef H_BITCOIN_SCRIPTUTILS -#define H_BITCOIN_SCRIPTUTILS +#ifndef H_BITCOIN_WALLET_ISMINE +#define H_BITCOIN_WALLET_ISMINE #include "key.h" #include "script/script.h" @@ -24,6 +24,5 @@ typedef uint8_t isminefilter; isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey); isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest); -void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector &vKeys); #endif // H_BITCOIN_SCRIPT