From 1b699a5083b435c2b79f3951f94ac9f967d24f6c Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Thu, 6 Jun 2019 09:53:01 +0200 Subject: Add HaveKey and HaveCScript to SigningProvider --- src/keystore.h | 2 -- src/script/sign.h | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/keystore.h b/src/keystore.h index 4bd99e255d..fd2212afcb 100644 --- a/src/keystore.h +++ b/src/keystore.h @@ -23,12 +23,10 @@ public: virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0; //! Check whether a key corresponding to a given address is present in the store. - virtual bool HaveKey(const CKeyID &address) const =0; virtual std::set GetKeys() const =0; //! Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki virtual bool AddCScript(const CScript& redeemScript) =0; - virtual bool HaveCScript(const CScriptID &hash) const =0; virtual std::set GetCScripts() const =0; //! Support for Watch-only addresses diff --git a/src/script/sign.h b/src/script/sign.h index e5c0329a61..9df4dc98fa 100644 --- a/src/script/sign.h +++ b/src/script/sign.h @@ -51,8 +51,10 @@ class SigningProvider public: virtual ~SigningProvider() {} virtual bool GetCScript(const CScriptID &scriptid, CScript& script) const { return false; } + virtual bool HaveCScript(const CScriptID &scriptid) const { return false; } virtual bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const { return false; } virtual bool GetKey(const CKeyID &address, CKey& key) const { return false; } + virtual bool HaveKey(const CKeyID &address) const { return false; } virtual bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const { return false; } }; -- cgit v1.2.3 From c7797ec65544bd23a2e571b2892e1bf512f2a485 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Thu, 6 Jun 2019 16:31:31 +0200 Subject: Remove CKeyStore and squash into CBasicKeyStore --- src/bitcoin-tx.cpp | 2 +- src/keystore.cpp | 4 +-- src/keystore.h | 59 ++++++++++++++---------------------------- src/outputtype.cpp | 2 +- src/outputtype.h | 2 +- src/rpc/util.cpp | 6 ++--- src/rpc/util.h | 6 ++--- src/test/transaction_tests.cpp | 2 +- 8 files changed, 31 insertions(+), 52 deletions(-) diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp index 933b34744d..75776727a6 100644 --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -631,7 +631,7 @@ static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr) } } - const CKeyStore& keystore = tempKeystore; + const CBasicKeyStore& keystore = tempKeystore; bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE); diff --git a/src/keystore.cpp b/src/keystore.cpp index f6d19416ce..8c149a9250 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -174,7 +174,7 @@ bool CBasicKeyStore::HaveWatchOnly() const return (!setWatchOnly.empty()); } -CKeyID GetKeyForDestination(const CKeyStore& store, const CTxDestination& dest) +CKeyID GetKeyForDestination(const CBasicKeyStore& store, const CTxDestination& dest) { // Only supports destinations which map to single public keys, i.e. P2PKH, // P2WPKH, and P2SH-P2WPKH. @@ -197,7 +197,7 @@ CKeyID GetKeyForDestination(const CKeyStore& store, const CTxDestination& dest) return CKeyID(); } -bool HaveKey(const CKeyStore& store, const CKey& key) +bool HaveKey(const CBasicKeyStore& store, const CKey& key) { CKey key2; key2.Set(key.begin(), key.end(), !key.IsCompressed()); diff --git a/src/keystore.h b/src/keystore.h index fd2212afcb..b8f5a430eb 100644 --- a/src/keystore.h +++ b/src/keystore.h @@ -15,29 +15,8 @@ #include -/** A virtual base class for key stores */ -class CKeyStore : public SigningProvider -{ -public: - //! Add a key to the store. - virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0; - - //! Check whether a key corresponding to a given address is present in the store. - virtual std::set GetKeys() const =0; - - //! Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki - virtual bool AddCScript(const CScript& redeemScript) =0; - virtual std::set GetCScripts() const =0; - - //! Support for Watch-only addresses - virtual bool AddWatchOnly(const CScript &dest) =0; - virtual bool RemoveWatchOnly(const CScript &dest) =0; - virtual bool HaveWatchOnly(const CScript &dest) const =0; - virtual bool HaveWatchOnly() const =0; -}; - /** Basic key store, that keeps keys in an address->secret map */ -class CBasicKeyStore : public CKeyStore +class CBasicKeyStore : public SigningProvider { protected: mutable CCriticalSection cs_KeyStore; @@ -55,27 +34,27 @@ protected: void ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore); public: - bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override; - bool AddKey(const CKey &key) { return AddKeyPubKey(key, key.GetPubKey()); } - bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override; - bool HaveKey(const CKeyID &address) const override; - std::set GetKeys() const override; - bool GetKey(const CKeyID &address, CKey &keyOut) const override; - bool AddCScript(const CScript& redeemScript) override; - bool HaveCScript(const CScriptID &hash) const override; - std::set GetCScripts() const override; - bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override; - - bool AddWatchOnly(const CScript &dest) override; - bool RemoveWatchOnly(const CScript &dest) override; - bool HaveWatchOnly(const CScript &dest) const override; - bool HaveWatchOnly() const override; + virtual bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey); + virtual bool AddKey(const CKey &key) { return AddKeyPubKey(key, key.GetPubKey()); } + virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override; + virtual bool HaveKey(const CKeyID &address) const override; + virtual std::set GetKeys() const; + virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override; + virtual bool AddCScript(const CScript& redeemScript); + virtual bool HaveCScript(const CScriptID &hash) const override; + virtual std::set GetCScripts() const; + virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override; + + virtual bool AddWatchOnly(const CScript &dest); + virtual bool RemoveWatchOnly(const CScript &dest); + virtual bool HaveWatchOnly(const CScript &dest) const; + virtual bool HaveWatchOnly() const; }; /** Return the CKeyID of the key involved in a script (if there is a unique one). */ -CKeyID GetKeyForDestination(const CKeyStore& store, const CTxDestination& dest); +CKeyID GetKeyForDestination(const CBasicKeyStore& store, const CTxDestination& dest); -/** Checks if a CKey is in the given CKeyStore compressed or otherwise*/ -bool HaveKey(const CKeyStore& store, const CKey& key); +/** Checks if a CKey is in the given CBasicKeyStore compressed or otherwise*/ +bool HaveKey(const CBasicKeyStore& store, const CKey& key); #endif // BITCOIN_KEYSTORE_H diff --git a/src/outputtype.cpp b/src/outputtype.cpp index 73ffb801f2..13ce1e29a2 100644 --- a/src/outputtype.cpp +++ b/src/outputtype.cpp @@ -73,7 +73,7 @@ std::vector GetAllDestinationsForKey(const CPubKey& key) } } -CTxDestination AddAndGetDestinationForScript(CKeyStore& keystore, const CScript& script, OutputType type) +CTxDestination AddAndGetDestinationForScript(CBasicKeyStore& keystore, const CScript& script, OutputType type) { // Add script to keystore keystore.AddCScript(script); diff --git a/src/outputtype.h b/src/outputtype.h index 6c30fd1950..d07e6ba08c 100644 --- a/src/outputtype.h +++ b/src/outputtype.h @@ -44,7 +44,7 @@ std::vector GetAllDestinationsForKey(const CPubKey& key); * This function will automatically add the script (and any other * necessary scripts) to the keystore. */ -CTxDestination AddAndGetDestinationForScript(CKeyStore& keystore, const CScript& script, OutputType); +CTxDestination AddAndGetDestinationForScript(CBasicKeyStore& keystore, const CScript& script, OutputType); #endif // BITCOIN_OUTPUTTYPE_H diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index 67ccb225b5..65a1b39dd0 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -131,8 +131,8 @@ CPubKey HexToPubKey(const std::string& hex_in) return vchPubKey; } -// Retrieves a public key for an address from the given CKeyStore -CPubKey AddrToPubKey(CKeyStore* const keystore, const std::string& addr_in) +// Retrieves a public key for an address from the given CBasicKeyStore +CPubKey AddrToPubKey(CBasicKeyStore* const keystore, const std::string& addr_in) { CTxDestination dest = DecodeDestination(addr_in); if (!IsValidDestination(dest)) { @@ -153,7 +153,7 @@ CPubKey AddrToPubKey(CKeyStore* const keystore, const std::string& addr_in) } // Creates a multisig address from a given list of public keys, number of signatures required, and the address type -CTxDestination AddAndGetMultisigDestination(const int required, const std::vector& pubkeys, OutputType type, CKeyStore& keystore, CScript& script_out) +CTxDestination AddAndGetMultisigDestination(const int required, const std::vector& pubkeys, OutputType type, CBasicKeyStore& keystore, CScript& script_out) { // Gather public keys if (required < 1) { diff --git a/src/rpc/util.h b/src/rpc/util.h index 8762281d73..b22b4dd54e 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -19,7 +19,7 @@ #include -class CKeyStore; +class CBasicKeyStore; class CPubKey; class CScript; struct InitInterfaces; @@ -72,8 +72,8 @@ extern std::string HelpExampleCli(const std::string& methodname, const std::stri extern std::string HelpExampleRpc(const std::string& methodname, const std::string& args); CPubKey HexToPubKey(const std::string& hex_in); -CPubKey AddrToPubKey(CKeyStore* const keystore, const std::string& addr_in); -CTxDestination AddAndGetMultisigDestination(const int required, const std::vector& pubkeys, OutputType type, CKeyStore& keystore, CScript& script_out); +CPubKey AddrToPubKey(CBasicKeyStore* const keystore, const std::string& addr_in); +CTxDestination AddAndGetMultisigDestination(const int required, const std::vector& pubkeys, OutputType type, CBasicKeyStore& keystore, CScript& script_out); UniValue DescribeAddress(const CTxDestination& dest); diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp index f77b77a972..8c1b56ccf2 100644 --- a/src/test/transaction_tests.cpp +++ b/src/test/transaction_tests.cpp @@ -346,7 +346,7 @@ BOOST_AUTO_TEST_CASE(test_Get) BOOST_CHECK_EQUAL(coins.GetValueIn(CTransaction(t1)), (50+21+22)*CENT); } -static void CreateCreditAndSpend(const CKeyStore& keystore, const CScript& outscript, CTransactionRef& output, CMutableTransaction& input, bool success = true) +static void CreateCreditAndSpend(const CBasicKeyStore& keystore, const CScript& outscript, CTransactionRef& output, CMutableTransaction& input, bool success = true) { CMutableTransaction outputm; outputm.nVersion = 1; -- cgit v1.2.3 From a913e3f2fbeb1352fc66f334d4f5f7332ea89ad7 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Thu, 6 Jun 2019 16:49:52 +0200 Subject: Move HaveKey static function from keystore to rpcwallet where it is used --- src/keystore.cpp | 7 ------- src/keystore.h | 3 --- src/wallet/rpcwallet.cpp | 8 ++++++++ 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/keystore.cpp b/src/keystore.cpp index 8c149a9250..0319c469fc 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -196,10 +196,3 @@ CKeyID GetKeyForDestination(const CBasicKeyStore& store, const CTxDestination& d } return CKeyID(); } - -bool HaveKey(const CBasicKeyStore& store, const CKey& key) -{ - CKey key2; - key2.Set(key.begin(), key.end(), !key.IsCompressed()); - return store.HaveKey(key.GetPubKey().GetID()) || store.HaveKey(key2.GetPubKey().GetID()); -} diff --git a/src/keystore.h b/src/keystore.h index b8f5a430eb..3e46acda1d 100644 --- a/src/keystore.h +++ b/src/keystore.h @@ -54,7 +54,4 @@ public: /** Return the CKeyID of the key involved in a script (if there is a unique one). */ CKeyID GetKeyForDestination(const CBasicKeyStore& store, const CTxDestination& dest); -/** Checks if a CKey is in the given CBasicKeyStore compressed or otherwise*/ -bool HaveKey(const CBasicKeyStore& store, const CKey& key); - #endif // BITCOIN_KEYSTORE_H diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index eae5f876ea..bc86665782 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -52,6 +52,14 @@ static inline bool GetAvoidReuseFlag(CWallet * const pwallet, const UniValue& pa return avoid_reuse; } +/** Checks if a CKey is in the given CWallet compressed or otherwise*/ +bool HaveKey(const CWallet& wallet, const CKey& key) +{ + CKey key2; + key2.Set(key.begin(), key.end(), !key.IsCompressed()); + return wallet.HaveKey(key.GetPubKey().GetID()) || wallet.HaveKey(key2.GetPubKey().GetID()); +} + bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& wallet_name) { if (request.URI.substr(0, WALLET_ENDPOINT_BASE.size()) == WALLET_ENDPOINT_BASE) { -- cgit v1.2.3 From d9becff4e13da8e182631baa79b9794c03d44434 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Thu, 6 Jun 2019 16:33:23 +0200 Subject: scripted-diff: rename CBasicKeyStore to FillableSigningProvider -BEGIN VERIFY SCRIPT- git grep -l "CBasicKeyStore" | xargs sed -i -e 's/CBasicKeyStore/FillableSigningProvider/g' -END VERIFY SCRIPT- --- doc/developer-notes.md | 2 +- src/bench/ccoins_caching.cpp | 4 ++-- src/bitcoin-tx.cpp | 4 ++-- src/keystore.cpp | 32 ++++++++++++++++---------------- src/keystore.h | 4 ++-- src/outputtype.cpp | 2 +- src/outputtype.h | 2 +- src/rpc/misc.cpp | 2 +- src/rpc/rawtransaction.cpp | 2 +- src/rpc/rawtransaction_util.cpp | 2 +- src/rpc/rawtransaction_util.h | 4 ++-- src/rpc/util.cpp | 6 +++--- src/rpc/util.h | 6 +++--- src/test/denialofservice_tests.cpp | 2 +- src/test/multisig_tests.cpp | 2 +- src/test/script_p2sh_tests.cpp | 6 +++--- src/test/script_tests.cpp | 2 +- src/test/transaction_tests.cpp | 12 ++++++------ src/test/txvalidationcache_tests.cpp | 2 +- src/wallet/crypter.cpp | 12 ++++++------ src/wallet/crypter.h | 2 +- src/wallet/test/wallet_tests.cpp | 2 +- src/wallet/wallet.h | 2 +- 23 files changed, 58 insertions(+), 58 deletions(-) diff --git a/doc/developer-notes.md b/doc/developer-notes.md index ecd720539e..39463dc6f8 100644 --- a/doc/developer-notes.md +++ b/doc/developer-notes.md @@ -375,7 +375,7 @@ reported in the debug.log file. Re-architecting the core code so there are better-defined interfaces between the various components is a goal, with any necessary locking -done by the components (e.g. see the self-contained `CBasicKeyStore` class +done by the components (e.g. see the self-contained `FillableSigningProvider` class and its `cs_KeyStore` lock for example). Threads diff --git a/src/bench/ccoins_caching.cpp b/src/bench/ccoins_caching.cpp index 1041a22303..57a8e164cb 100644 --- a/src/bench/ccoins_caching.cpp +++ b/src/bench/ccoins_caching.cpp @@ -17,7 +17,7 @@ // paid to a TX_PUBKEYHASH. // static std::vector -SetupDummyInputs(CBasicKeyStore& keystoreRet, CCoinsViewCache& coinsRet) +SetupDummyInputs(FillableSigningProvider& keystoreRet, CCoinsViewCache& coinsRet) { std::vector dummyTransactions; dummyTransactions.resize(2); @@ -55,7 +55,7 @@ SetupDummyInputs(CBasicKeyStore& keystoreRet, CCoinsViewCache& coinsRet) // (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484) static void CCoinsCaching(benchmark::State& state) { - CBasicKeyStore keystore; + FillableSigningProvider keystore; CCoinsView coinsDummy; CCoinsViewCache coins(&coinsDummy); std::vector dummyTransactions = SetupDummyInputs(keystore, coins); diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp index 75776727a6..271f064690 100644 --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -557,7 +557,7 @@ static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr) if (!registers.count("privatekeys")) throw std::runtime_error("privatekeys register variable must be set."); - CBasicKeyStore tempKeystore; + FillableSigningProvider tempKeystore; UniValue keysObj = registers["privatekeys"]; for (unsigned int kidx = 0; kidx < keysObj.size(); kidx++) { @@ -631,7 +631,7 @@ static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr) } } - const CBasicKeyStore& keystore = tempKeystore; + const FillableSigningProvider& keystore = tempKeystore; bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE); diff --git a/src/keystore.cpp b/src/keystore.cpp index 0319c469fc..6f331ffa5e 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -7,7 +7,7 @@ #include -void CBasicKeyStore::ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey) +void FillableSigningProvider::ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey) { AssertLockHeld(cs_KeyStore); CKeyID key_id = pubkey.GetID(); @@ -32,7 +32,7 @@ void CBasicKeyStore::ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey) } } -bool CBasicKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const +bool FillableSigningProvider::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const { CKey key; if (!GetKey(address, key)) { @@ -48,7 +48,7 @@ bool CBasicKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) con return true; } -bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey) +bool FillableSigningProvider::AddKeyPubKey(const CKey& key, const CPubKey &pubkey) { LOCK(cs_KeyStore); mapKeys[pubkey.GetID()] = key; @@ -56,13 +56,13 @@ bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey) return true; } -bool CBasicKeyStore::HaveKey(const CKeyID &address) const +bool FillableSigningProvider::HaveKey(const CKeyID &address) const { LOCK(cs_KeyStore); return mapKeys.count(address) > 0; } -std::set CBasicKeyStore::GetKeys() const +std::set FillableSigningProvider::GetKeys() const { LOCK(cs_KeyStore); std::set set_address; @@ -72,7 +72,7 @@ std::set CBasicKeyStore::GetKeys() const return set_address; } -bool CBasicKeyStore::GetKey(const CKeyID &address, CKey &keyOut) const +bool FillableSigningProvider::GetKey(const CKeyID &address, CKey &keyOut) const { LOCK(cs_KeyStore); KeyMap::const_iterator mi = mapKeys.find(address); @@ -83,23 +83,23 @@ bool CBasicKeyStore::GetKey(const CKeyID &address, CKey &keyOut) const return false; } -bool CBasicKeyStore::AddCScript(const CScript& redeemScript) +bool FillableSigningProvider::AddCScript(const CScript& redeemScript) { if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE) - return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE); + return error("FillableSigningProvider::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE); LOCK(cs_KeyStore); mapScripts[CScriptID(redeemScript)] = redeemScript; return true; } -bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const +bool FillableSigningProvider::HaveCScript(const CScriptID& hash) const { LOCK(cs_KeyStore); return mapScripts.count(hash) > 0; } -std::set CBasicKeyStore::GetCScripts() const +std::set FillableSigningProvider::GetCScripts() const { LOCK(cs_KeyStore); std::set set_script; @@ -109,7 +109,7 @@ std::set CBasicKeyStore::GetCScripts() const return set_script; } -bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const +bool FillableSigningProvider::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const { LOCK(cs_KeyStore); ScriptMap::const_iterator mi = mapScripts.find(hash); @@ -137,7 +137,7 @@ static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut) return true; } -bool CBasicKeyStore::AddWatchOnly(const CScript &dest) +bool FillableSigningProvider::AddWatchOnly(const CScript &dest) { LOCK(cs_KeyStore); setWatchOnly.insert(dest); @@ -149,7 +149,7 @@ bool CBasicKeyStore::AddWatchOnly(const CScript &dest) return true; } -bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest) +bool FillableSigningProvider::RemoveWatchOnly(const CScript &dest) { LOCK(cs_KeyStore); setWatchOnly.erase(dest); @@ -162,19 +162,19 @@ bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest) return true; } -bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const +bool FillableSigningProvider::HaveWatchOnly(const CScript &dest) const { LOCK(cs_KeyStore); return setWatchOnly.count(dest) > 0; } -bool CBasicKeyStore::HaveWatchOnly() const +bool FillableSigningProvider::HaveWatchOnly() const { LOCK(cs_KeyStore); return (!setWatchOnly.empty()); } -CKeyID GetKeyForDestination(const CBasicKeyStore& store, const CTxDestination& dest) +CKeyID GetKeyForDestination(const FillableSigningProvider& store, const CTxDestination& dest) { // Only supports destinations which map to single public keys, i.e. P2PKH, // P2WPKH, and P2SH-P2WPKH. diff --git a/src/keystore.h b/src/keystore.h index 3e46acda1d..98209d4a43 100644 --- a/src/keystore.h +++ b/src/keystore.h @@ -16,7 +16,7 @@ #include /** Basic key store, that keeps keys in an address->secret map */ -class CBasicKeyStore : public SigningProvider +class FillableSigningProvider : public SigningProvider { protected: mutable CCriticalSection cs_KeyStore; @@ -52,6 +52,6 @@ public: }; /** Return the CKeyID of the key involved in a script (if there is a unique one). */ -CKeyID GetKeyForDestination(const CBasicKeyStore& store, const CTxDestination& dest); +CKeyID GetKeyForDestination(const FillableSigningProvider& store, const CTxDestination& dest); #endif // BITCOIN_KEYSTORE_H diff --git a/src/outputtype.cpp b/src/outputtype.cpp index 13ce1e29a2..fce5a1783a 100644 --- a/src/outputtype.cpp +++ b/src/outputtype.cpp @@ -73,7 +73,7 @@ std::vector GetAllDestinationsForKey(const CPubKey& key) } } -CTxDestination AddAndGetDestinationForScript(CBasicKeyStore& keystore, const CScript& script, OutputType type) +CTxDestination AddAndGetDestinationForScript(FillableSigningProvider& keystore, const CScript& script, OutputType type) { // Add script to keystore keystore.AddCScript(script); diff --git a/src/outputtype.h b/src/outputtype.h index d07e6ba08c..dbaf0ee380 100644 --- a/src/outputtype.h +++ b/src/outputtype.h @@ -44,7 +44,7 @@ std::vector GetAllDestinationsForKey(const CPubKey& key); * This function will automatically add the script (and any other * necessary scripts) to the keystore. */ -CTxDestination AddAndGetDestinationForScript(CBasicKeyStore& keystore, const CScript& script, OutputType); +CTxDestination AddAndGetDestinationForScript(FillableSigningProvider& keystore, const CScript& script, OutputType); #endif // BITCOIN_OUTPUTTYPE_H diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 7a1bdec7b9..103ae62834 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -122,7 +122,7 @@ static UniValue createmultisig(const JSONRPCRequest& request) } // Construct using pay-to-script-hash: - CBasicKeyStore keystore; + FillableSigningProvider keystore; CScript inner; const CTxDestination dest = AddAndGetMultisigDestination(required, pubkeys, output_type, keystore, inner); diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 966ff3fedc..b8cc4abbd7 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -761,7 +761,7 @@ static UniValue signrawtransactionwithkey(const JSONRPCRequest& request) throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed"); } - CBasicKeyStore keystore; + FillableSigningProvider keystore; const UniValue& keys = request.params[1].get_array(); for (unsigned int idx = 0; idx < keys.size(); ++idx) { UniValue k = keys[idx]; diff --git a/src/rpc/rawtransaction_util.cpp b/src/rpc/rawtransaction_util.cpp index 14fcb628eb..9da48da0ab 100644 --- a/src/rpc/rawtransaction_util.cpp +++ b/src/rpc/rawtransaction_util.cpp @@ -148,7 +148,7 @@ static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std:: vErrorsRet.push_back(entry); } -UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxsUnival, CBasicKeyStore* keystore, std::map& coins, bool is_temp_keystore, const UniValue& hashType) +UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxsUnival, FillableSigningProvider* keystore, std::map& coins, bool is_temp_keystore, const UniValue& hashType) { // Add previous txouts given in the RPC call: if (!prevTxsUnival.isNull()) { diff --git a/src/rpc/rawtransaction_util.h b/src/rpc/rawtransaction_util.h index c115d33a77..d198887b93 100644 --- a/src/rpc/rawtransaction_util.h +++ b/src/rpc/rawtransaction_util.h @@ -7,7 +7,7 @@ #include -class CBasicKeyStore; +class FillableSigningProvider; class UniValue; struct CMutableTransaction; class Coin; @@ -24,7 +24,7 @@ class COutPoint; * @param hashType The signature hash type * @returns JSON object with details of signed transaction */ -UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxs, CBasicKeyStore* keystore, std::map& coins, bool tempKeystore, const UniValue& hashType); +UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxs, FillableSigningProvider* keystore, std::map& coins, bool tempKeystore, const UniValue& hashType); /** Create a transaction from univalue parameters */ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime, const UniValue& rbf); diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index 65a1b39dd0..86f0dd24bb 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -131,8 +131,8 @@ CPubKey HexToPubKey(const std::string& hex_in) return vchPubKey; } -// Retrieves a public key for an address from the given CBasicKeyStore -CPubKey AddrToPubKey(CBasicKeyStore* const keystore, const std::string& addr_in) +// Retrieves a public key for an address from the given FillableSigningProvider +CPubKey AddrToPubKey(FillableSigningProvider* const keystore, const std::string& addr_in) { CTxDestination dest = DecodeDestination(addr_in); if (!IsValidDestination(dest)) { @@ -153,7 +153,7 @@ CPubKey AddrToPubKey(CBasicKeyStore* const keystore, const std::string& addr_in) } // Creates a multisig address from a given list of public keys, number of signatures required, and the address type -CTxDestination AddAndGetMultisigDestination(const int required, const std::vector& pubkeys, OutputType type, CBasicKeyStore& keystore, CScript& script_out) +CTxDestination AddAndGetMultisigDestination(const int required, const std::vector& pubkeys, OutputType type, FillableSigningProvider& keystore, CScript& script_out) { // Gather public keys if (required < 1) { diff --git a/src/rpc/util.h b/src/rpc/util.h index b22b4dd54e..c7d2c4dad6 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -19,7 +19,7 @@ #include -class CBasicKeyStore; +class FillableSigningProvider; class CPubKey; class CScript; struct InitInterfaces; @@ -72,8 +72,8 @@ extern std::string HelpExampleCli(const std::string& methodname, const std::stri extern std::string HelpExampleRpc(const std::string& methodname, const std::string& args); CPubKey HexToPubKey(const std::string& hex_in); -CPubKey AddrToPubKey(CBasicKeyStore* const keystore, const std::string& addr_in); -CTxDestination AddAndGetMultisigDestination(const int required, const std::vector& pubkeys, OutputType type, CBasicKeyStore& keystore, CScript& script_out); +CPubKey AddrToPubKey(FillableSigningProvider* const keystore, const std::string& addr_in); +CTxDestination AddAndGetMultisigDestination(const int required, const std::vector& pubkeys, OutputType type, FillableSigningProvider& keystore, CScript& script_out); UniValue DescribeAddress(const CTxDestination& dest); diff --git a/src/test/denialofservice_tests.cpp b/src/test/denialofservice_tests.cpp index 93883d1d98..0fc682f764 100644 --- a/src/test/denialofservice_tests.cpp +++ b/src/test/denialofservice_tests.cpp @@ -369,7 +369,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans) { CKey key; key.MakeNewKey(true); - CBasicKeyStore keystore; + FillableSigningProvider keystore; BOOST_CHECK(keystore.AddKey(key)); // 50 orphan transactions: diff --git a/src/test/multisig_tests.cpp b/src/test/multisig_tests.cpp index 11e79937be..8fc36ebc8b 100644 --- a/src/test/multisig_tests.cpp +++ b/src/test/multisig_tests.cpp @@ -174,7 +174,7 @@ BOOST_AUTO_TEST_CASE(multisig_IsStandard) BOOST_AUTO_TEST_CASE(multisig_Sign) { // Test SignSignature() (and therefore the version of Solver() that signs transactions) - CBasicKeyStore keystore; + FillableSigningProvider keystore; CKey key[4]; for (int i = 0; i < 4; i++) { diff --git a/src/test/script_p2sh_tests.cpp b/src/test/script_p2sh_tests.cpp index 735b67c06e..0c701a4130 100644 --- a/src/test/script_p2sh_tests.cpp +++ b/src/test/script_p2sh_tests.cpp @@ -55,7 +55,7 @@ BOOST_AUTO_TEST_CASE(sign) // scriptPubKey: HASH160 EQUAL // Test SignSignature() (and therefore the version of Solver() that signs transactions) - CBasicKeyStore keystore; + FillableSigningProvider keystore; CKey key[4]; for (int i = 0; i < 4; i++) { @@ -151,7 +151,7 @@ BOOST_AUTO_TEST_CASE(set) { LOCK(cs_main); // Test the CScript::Set* methods - CBasicKeyStore keystore; + FillableSigningProvider keystore; CKey key[4]; std::vector keys; for (int i = 0; i < 4; i++) @@ -265,7 +265,7 @@ BOOST_AUTO_TEST_CASE(AreInputsStandard) LOCK(cs_main); CCoinsView coinsDummy; CCoinsViewCache coins(&coinsDummy); - CBasicKeyStore keystore; + FillableSigningProvider keystore; CKey key[6]; std::vector keys; for (int i = 0; i < 6; i++) diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp index ae903df0ad..c8c2f7783d 100644 --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -1199,7 +1199,7 @@ SignatureData CombineSignatures(const CTxOut& txout, const CMutableTransaction& BOOST_AUTO_TEST_CASE(script_combineSigs) { // Test the ProduceSignature's ability to combine signatures function - CBasicKeyStore keystore; + FillableSigningProvider keystore; std::vector keys; std::vector pubkeys; for (int i = 0; i < 3; i++) diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp index 8c1b56ccf2..634985abc4 100644 --- a/src/test/transaction_tests.cpp +++ b/src/test/transaction_tests.cpp @@ -289,7 +289,7 @@ BOOST_AUTO_TEST_CASE(basic_transaction_tests) // paid to a TX_PUBKEYHASH. // static std::vector -SetupDummyInputs(CBasicKeyStore& keystoreRet, CCoinsViewCache& coinsRet) +SetupDummyInputs(FillableSigningProvider& keystoreRet, CCoinsViewCache& coinsRet) { std::vector dummyTransactions; dummyTransactions.resize(2); @@ -322,7 +322,7 @@ SetupDummyInputs(CBasicKeyStore& keystoreRet, CCoinsViewCache& coinsRet) BOOST_AUTO_TEST_CASE(test_Get) { - CBasicKeyStore keystore; + FillableSigningProvider keystore; CCoinsView coinsDummy; CCoinsViewCache coins(&coinsDummy); std::vector dummyTransactions = SetupDummyInputs(keystore, coins); @@ -346,7 +346,7 @@ BOOST_AUTO_TEST_CASE(test_Get) BOOST_CHECK_EQUAL(coins.GetValueIn(CTransaction(t1)), (50+21+22)*CENT); } -static void CreateCreditAndSpend(const CBasicKeyStore& keystore, const CScript& outscript, CTransactionRef& output, CMutableTransaction& input, bool success = true) +static void CreateCreditAndSpend(const FillableSigningProvider& keystore, const CScript& outscript, CTransactionRef& output, CMutableTransaction& input, bool success = true) { CMutableTransaction outputm; outputm.nVersion = 1; @@ -423,7 +423,7 @@ BOOST_AUTO_TEST_CASE(test_big_witness_transaction) CKey key; key.MakeNewKey(true); // Need to use compressed keys in segwit or the signing will fail - CBasicKeyStore keystore; + FillableSigningProvider keystore; BOOST_CHECK(keystore.AddKeyPubKey(key, key.GetPubKey())); CKeyID hash = key.GetPubKey().GetID(); CScript scriptPubKey = CScript() << OP_0 << std::vector(hash.begin(), hash.end()); @@ -507,7 +507,7 @@ SignatureData CombineSignatures(const CMutableTransaction& input1, const CMutabl BOOST_AUTO_TEST_CASE(test_witness) { - CBasicKeyStore keystore, keystore2; + FillableSigningProvider keystore, keystore2; CKey key1, key2, key3, key1L, key2L; CPubKey pubkey1, pubkey2, pubkey3, pubkey1L, pubkey2L; key1.MakeNewKey(true); @@ -682,7 +682,7 @@ BOOST_AUTO_TEST_CASE(test_witness) BOOST_AUTO_TEST_CASE(test_IsStandard) { LOCK(cs_main); - CBasicKeyStore keystore; + FillableSigningProvider keystore; CCoinsView coinsDummy; CCoinsViewCache coins(&coinsDummy); std::vector dummyTransactions = SetupDummyInputs(keystore, coins); diff --git a/src/test/txvalidationcache_tests.cpp b/src/test/txvalidationcache_tests.cpp index 45c97fa2aa..379c84e33e 100644 --- a/src/test/txvalidationcache_tests.cpp +++ b/src/test/txvalidationcache_tests.cpp @@ -161,7 +161,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup) CScript p2pkh_scriptPubKey = GetScriptForDestination(PKHash(coinbaseKey.GetPubKey())); CScript p2wpkh_scriptPubKey = GetScriptForWitness(p2pkh_scriptPubKey); - CBasicKeyStore keystore; + FillableSigningProvider keystore; BOOST_CHECK(keystore.AddKey(coinbaseKey)); BOOST_CHECK(keystore.AddCScript(p2pk_scriptPubKey)); diff --git a/src/wallet/crypter.cpp b/src/wallet/crypter.cpp index dd56ea10ab..3070454b1b 100644 --- a/src/wallet/crypter.cpp +++ b/src/wallet/crypter.cpp @@ -217,7 +217,7 @@ bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey) { LOCK(cs_KeyStore); if (!IsCrypted()) { - return CBasicKeyStore::AddKeyPubKey(key, pubkey); + return FillableSigningProvider::AddKeyPubKey(key, pubkey); } if (IsLocked()) { @@ -253,7 +253,7 @@ bool CCryptoKeyStore::HaveKey(const CKeyID &address) const { LOCK(cs_KeyStore); if (!IsCrypted()) { - return CBasicKeyStore::HaveKey(address); + return FillableSigningProvider::HaveKey(address); } return mapCryptedKeys.count(address) > 0; } @@ -262,7 +262,7 @@ bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const { LOCK(cs_KeyStore); if (!IsCrypted()) { - return CBasicKeyStore::GetKey(address, keyOut); + return FillableSigningProvider::GetKey(address, keyOut); } CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); @@ -279,7 +279,7 @@ bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) co { LOCK(cs_KeyStore); if (!IsCrypted()) - return CBasicKeyStore::GetPubKey(address, vchPubKeyOut); + return FillableSigningProvider::GetPubKey(address, vchPubKeyOut); CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); if (mi != mapCryptedKeys.end()) @@ -288,14 +288,14 @@ bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) co return true; } // Check for watch-only pubkeys - return CBasicKeyStore::GetPubKey(address, vchPubKeyOut); + return FillableSigningProvider::GetPubKey(address, vchPubKeyOut); } std::set CCryptoKeyStore::GetKeys() const { LOCK(cs_KeyStore); if (!IsCrypted()) { - return CBasicKeyStore::GetKeys(); + return FillableSigningProvider::GetKeys(); } std::set set_address; for (const auto& mi : mapCryptedKeys) { diff --git a/src/wallet/crypter.h b/src/wallet/crypter.h index 8e195ca8fa..95235903f4 100644 --- a/src/wallet/crypter.h +++ b/src/wallet/crypter.h @@ -112,7 +112,7 @@ public: /** Keystore which keeps the private keys encrypted. * It derives from the basic key store, which is used if no encryption is active. */ -class CCryptoKeyStore : public CBasicKeyStore +class CCryptoKeyStore : public FillableSigningProvider { private: diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index ef95d0544f..cbb05596b8 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -488,7 +488,7 @@ static size_t CalculateNestedKeyhashInputSize(bool use_max_sig) CScript script_pubkey = CScript() << OP_HASH160 << std::vector(script_id.begin(), script_id.end()) << OP_EQUAL; // Add inner-script to key store and key to watchonly - CBasicKeyStore keystore; + FillableSigningProvider keystore; keystore.AddCScript(inner_script); keystore.AddKeyPubKey(key, pubkey); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 7b5465c219..3c1d998d19 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1296,7 +1296,7 @@ public: /** * Explicitly make the wallet learn the related scripts for outputs to the * given key. This is purely to make the wallet file compatible with older - * software, as CBasicKeyStore automatically does this implicitly for all + * software, as FillableSigningProvider automatically does this implicitly for all * keys now. */ void LearnRelatedScripts(const CPubKey& key, OutputType); -- cgit v1.2.3 From 16f8096e911e4d59292240a17e2d4004f0500b9e Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Mon, 17 Jun 2019 15:47:12 -0400 Subject: Move KeyOriginInfo to its own header file --- src/Makefile.am | 1 + src/script/keyorigin.h | 37 +++++++++++++++++++++++++++++++++++++ src/script/sign.h | 26 +------------------------- 3 files changed, 39 insertions(+), 25 deletions(-) create mode 100644 src/script/keyorigin.h diff --git a/src/Makefile.am b/src/Makefile.am index 39e8d3d689..30058d9541 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -181,6 +181,7 @@ BITCOIN_CORE_H = \ rpc/util.h \ scheduler.h \ script/descriptor.h \ + script/keyorigin.h \ script/sigcache.h \ script/sign.h \ script/standard.h \ diff --git a/src/script/keyorigin.h b/src/script/keyorigin.h new file mode 100644 index 0000000000..610f233500 --- /dev/null +++ b/src/script/keyorigin.h @@ -0,0 +1,37 @@ +// Copyright (c) 2019 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_SCRIPT_KEYORIGIN_H +#define BITCOIN_SCRIPT_KEYORIGIN_H + +#include +#include +#include + +struct KeyOriginInfo +{ + unsigned char fingerprint[4]; //!< First 32 bits of the Hash160 of the public key at the root of the path + std::vector path; + + friend bool operator==(const KeyOriginInfo& a, const KeyOriginInfo& b) + { + return std::equal(std::begin(a.fingerprint), std::end(a.fingerprint), std::begin(b.fingerprint)) && a.path == b.path; + } + + ADD_SERIALIZE_METHODS; + template + inline void SerializationOp(Stream& s, Operation ser_action) + { + READWRITE(fingerprint); + READWRITE(path); + } + + void clear() + { + memset(fingerprint, 0, 4); + path.clear(); + } +}; + +#endif // BITCOIN_SCRIPT_KEYORIGIN_H diff --git a/src/script/sign.h b/src/script/sign.h index 9df4dc98fa..7b4627cca2 100644 --- a/src/script/sign.h +++ b/src/script/sign.h @@ -10,6 +10,7 @@ #include #include #include