From 2c54217f913967703b404747133be67cf2f4feac Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Wed, 15 Jan 2020 13:40:14 -0800 Subject: [PATCH] Use explicit conversion from PKHash -> CKeyID These types are equivalent, in data etc, so they need only their data cast across. Note a function is used rather than a casting operator as CKeyID is defined at a lower level than script/standard --- src/qt/coincontroldialog.cpp | 2 +- src/script/signingprovider.cpp | 2 +- src/script/standard.cpp | 5 +++++ src/script/standard.h | 1 + src/wallet/rpcwallet.cpp | 2 +- src/wallet/scriptpubkeyman.cpp | 6 ++---- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/qt/coincontroldialog.cpp b/src/qt/coincontroldialog.cpp index f44a9f285ae10..7c72858501c59 100644 --- a/src/qt/coincontroldialog.cpp +++ b/src/qt/coincontroldialog.cpp @@ -456,7 +456,7 @@ void CoinControlDialog::updateLabels(CCoinControl& m_coin_control, WalletModel * { CPubKey pubkey; PKHash *pkhash = boost::get(&address); - if (pkhash && model->wallet().getPubKey(out.txout.scriptPubKey, CKeyID(*pkhash), pubkey)) + if (pkhash && model->wallet().getPubKey(out.txout.scriptPubKey, ToKeyID(*pkhash), pubkey)) { nBytesInputs += (pubkey.IsCompressed() ? 148 : 180); } diff --git a/src/script/signingprovider.cpp b/src/script/signingprovider.cpp index 01757e2f65537..a4eb0a83995d1 100644 --- a/src/script/signingprovider.cpp +++ b/src/script/signingprovider.cpp @@ -180,7 +180,7 @@ CKeyID GetKeyForDestination(const SigningProvider& store, const CTxDestination& // Only supports destinations which map to single public keys, i.e. P2PKH, // P2WPKH, and P2SH-P2WPKH. if (auto id = boost::get(&dest)) { - return CKeyID(*id); + return ToKeyID(*id); } if (auto witness_id = boost::get(&dest)) { return CKeyID(*witness_id); diff --git a/src/script/standard.cpp b/src/script/standard.cpp index 3e0a9e2304d50..4b408d82e3c7a 100644 --- a/src/script/standard.cpp +++ b/src/script/standard.cpp @@ -23,6 +23,11 @@ ScriptHash::ScriptHash(const CScript& in) : uint160(Hash160(in.begin(), in.end() PKHash::PKHash(const CPubKey& pubkey) : uint160(pubkey.GetID()) {} WitnessV0KeyHash::WitnessV0KeyHash(const CPubKey& pubkey) : uint160(pubkey.GetID()) {} +CKeyID ToKeyID(const PKHash& key_hash) +{ + return CKeyID{static_cast(key_hash)}; +} + WitnessV0ScriptHash::WitnessV0ScriptHash(const CScript& in) { CSHA256().Write(in.data(), in.size()).Finalize(begin()); diff --git a/src/script/standard.h b/src/script/standard.h index 382d8d002b5c2..1c630744beafa 100644 --- a/src/script/standard.h +++ b/src/script/standard.h @@ -79,6 +79,7 @@ struct PKHash : public uint160 explicit PKHash(const uint160& hash) : uint160(hash) {} explicit PKHash(const CPubKey& pubkey); }; +CKeyID ToKeyID(const PKHash& key_hash); struct WitnessV0KeyHash; struct ScriptHash : public uint160 diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c3a64cf46aaa6..43c1aa1a7d437 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -3517,7 +3517,7 @@ class DescribeWalletAddressVisitor : public boost::static_visitor UniValue operator()(const PKHash& pkhash) const { - CKeyID keyID(pkhash); + CKeyID keyID{ToKeyID(pkhash)}; UniValue obj(UniValue::VOBJ); CPubKey vchPubKey; if (provider && provider->GetPubKey(keyID, vchPubKey)) { diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index 8a2a79864417c..6ec34951ecf70 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -573,9 +573,8 @@ bool LegacyScriptPubKeyMan::SignTransaction(CMutableTransaction& tx, const std:: SigningResult LegacyScriptPubKeyMan::SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const { - CKeyID key_id(pkhash); CKey key; - if (!GetKey(key_id, key)) { + if (!GetKey(ToKeyID(pkhash), key)) { return SigningResult::PRIVATE_KEY_NOT_AVAILABLE; } @@ -2052,9 +2051,8 @@ SigningResult DescriptorScriptPubKeyMan::SignMessage(const std::string& message, return SigningResult::PRIVATE_KEY_NOT_AVAILABLE; } - CKeyID key_id(pkhash); CKey key; - if (!keys->GetKey(key_id, key)) { + if (!keys->GetKey(ToKeyID(pkhash), key)) { return SigningResult::PRIVATE_KEY_NOT_AVAILABLE; }