From d53d4d880a2b625a43cdd73df79efb58894c35ef Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Tue, 18 Feb 2020 13:20:43 -0300 Subject: [PATCH] add destination wrappers --- src/chainparams.cpp | 2 +- src/rpc/misc.cpp | 4 ++-- src/script/standard.cpp | 8 ++++++++ src/script/standard.h | 6 ++++++ src/wallet/test/rpc_wallet_tests.cpp | 6 +++--- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index acfe62dce..b37fa36bb 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -616,7 +616,7 @@ CScript CChainParams::GetFoundersRewardScriptAtHeight(int nHeight) const { CTxDestination address = DecodeDestination(GetFoundersRewardAddressAtHeight(nHeight).c_str()); assert(IsValidDestination(address)); - assert(boost::get(&address) != nullptr); + assert(IsScriptDestination(address)); CScriptID scriptID = boost::get(address); // address is a boost variant CScript script = CScript() << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL; return script; diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 430623dec..fa2dfb70f 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -506,13 +506,13 @@ static bool getIndexKey( if (!IsValidDestination(dest)) { return false; } - if (dest.type() == typeid(CKeyID)) { + if (IsKeyDestination(dest)) { auto x = boost::get(&dest); memcpy(&hashBytes, x->begin(), 20); type = CScript::P2PKH; return true; } - if (dest.type() == typeid(CScriptID)) { + if (IsScriptDestination(dest)) { auto x = boost::get(&dest); memcpy(&hashBytes, x->begin(), 20); type = CScript::P2SH; diff --git a/src/script/standard.cpp b/src/script/standard.cpp index f84406075..47f7ea5b2 100644 --- a/src/script/standard.cpp +++ b/src/script/standard.cpp @@ -280,6 +280,14 @@ bool IsValidDestination(const CTxDestination& dest) { return dest.which() != 0; } +bool IsKeyDestination(const CTxDestination& dest) { + return dest.which() == 1; +} + +bool IsScriptDestination(const CTxDestination& dest) { + return dest.which() == 2; +} + // insightexplorer CTxDestination DestFromAddressHash(int scriptType, uint160& addressHash) { diff --git a/src/script/standard.h b/src/script/standard.h index 876f5bbe6..97df161f5 100644 --- a/src/script/standard.h +++ b/src/script/standard.h @@ -81,6 +81,12 @@ typedef boost::variant CTxDestination; /** Check whether a CTxDestination is a CNoDestination. */ bool IsValidDestination(const CTxDestination& dest); +/** Check whether a CTxDestination is a CKeyID. */ +bool IsKeyDestination(const CTxDestination& dest); + +/** Check whether a CTxDestination is a CScriptID. */ +bool IsScriptDestination(const CTxDestination& dest); + /** Get the name of a txnouttype as a C string, or nullptr if unknown. */ const char* GetTxnOutputType(txnouttype t); diff --git a/src/wallet/test/rpc_wallet_tests.cpp b/src/wallet/test/rpc_wallet_tests.cpp index a0221b241..e749e2c91 100644 --- a/src/wallet/test/rpc_wallet_tests.cpp +++ b/src/wallet/test/rpc_wallet_tests.cpp @@ -72,15 +72,15 @@ BOOST_AUTO_TEST_CASE(rpc_addmultisig) CTxDestination address; BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex), false)); address = DecodeDestination(v.get_str()); - BOOST_CHECK(IsValidDestination(address) && boost::get(&address) != nullptr); + BOOST_CHECK(IsValidDestination(address) && IsScriptDestination(address)); BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex, address2Hex), false)); address = DecodeDestination(v.get_str()); - BOOST_CHECK(IsValidDestination(address) && boost::get(&address) != nullptr); + BOOST_CHECK(IsValidDestination(address) && IsScriptDestination(address)); BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(2, address1Hex, address2Hex), false)); address = DecodeDestination(v.get_str()); - BOOST_CHECK(IsValidDestination(address) && boost::get(&address) != nullptr); + BOOST_CHECK(IsValidDestination(address) && IsScriptDestination(address)); BOOST_CHECK_THROW(addmultisig(createArgs(0), false), runtime_error); BOOST_CHECK_THROW(addmultisig(createArgs(1), false), runtime_error);