From 39633ecd5c18ca29cb551f701e57e8cec3e609da Mon Sep 17 00:00:00 2001 From: John Newbery Date: Mon, 4 Dec 2017 11:42:15 -0500 Subject: [rpc] split wallet and non-wallet parts of DescribeAddressVisitor --- src/rpc/misc.cpp | 89 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 63 insertions(+), 26 deletions(-) diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 3f89996e61..a1e39be64e 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -33,13 +33,62 @@ #include -#ifdef ENABLE_WALLET class DescribeAddressVisitor : public boost::static_visitor { public: - CWallet * const pwallet; + explicit DescribeAddressVisitor() {} + + UniValue operator()(const CNoDestination &dest) const { return UniValue(UniValue::VOBJ); } + + UniValue operator()(const CKeyID &keyID) const { + UniValue obj(UniValue::VOBJ); + obj.pushKV("isscript", false); + obj.pushKV("iswitness", false); + return obj; + } + + UniValue operator()(const CScriptID &scriptID) const { + UniValue obj(UniValue::VOBJ); + obj.pushKV("isscript", true); + obj.pushKV("iswitness", false); + return obj; + } + + UniValue operator()(const WitnessV0KeyHash& id) const + { + UniValue obj(UniValue::VOBJ); + obj.pushKV("isscript", false); + obj.pushKV("iswitness", true); + obj.pushKV("witness_version", 0); + obj.pushKV("witness_program", HexStr(id.begin(), id.end())); + return obj; + } + + UniValue operator()(const WitnessV0ScriptHash& id) const + { + UniValue obj(UniValue::VOBJ); + obj.pushKV("isscript", true); + obj.pushKV("iswitness", true); + obj.pushKV("witness_version", 0); + obj.pushKV("witness_program", HexStr(id.begin(), id.end())); + return obj; + } - explicit DescribeAddressVisitor(CWallet *_pwallet) : pwallet(_pwallet) {} + UniValue operator()(const WitnessUnknown& id) const + { + UniValue obj(UniValue::VOBJ); + obj.pushKV("iswitness", true); + obj.pushKV("witness_version", (int)id.version); + obj.pushKV("witness_program", HexStr(id.program, id.program + id.length)); + return obj; + } +}; + +#ifdef ENABLE_WALLET +class DescribeWalletAddressVisitor : public boost::static_visitor +{ +public: + CWallet * const pwallet; void ProcessSubScript(const CScript& subscript, UniValue& obj, bool include_addresses = false) const { @@ -54,7 +103,11 @@ public: UniValue a(UniValue::VARR); if (ExtractDestination(subscript, embedded)) { // Only when the script corresponds to an address. - UniValue subobj = boost::apply_visitor(*this, embedded); + UniValue subobj(UniValue::VOBJ); + UniValue detail = boost::apply_visitor(DescribeAddressVisitor(), embedded); + subobj.pushKVs(detail); + UniValue wallet_detail = boost::apply_visitor(*this, embedded); + subobj.pushKVs(wallet_detail); subobj.pushKV("address", EncodeDestination(embedded)); subobj.pushKV("scriptPubKey", HexStr(subscript.begin(), subscript.end())); // Always report the pubkey at the top level, so that `getnewaddress()['pubkey']` always works. @@ -81,13 +134,13 @@ public: if (include_addresses) obj.pushKV("addresses", std::move(a)); } + explicit DescribeWalletAddressVisitor(CWallet *_pwallet) : pwallet(_pwallet) {} + UniValue operator()(const CNoDestination &dest) const { return UniValue(UniValue::VOBJ); } UniValue operator()(const CKeyID &keyID) const { UniValue obj(UniValue::VOBJ); CPubKey vchPubKey; - obj.pushKV("isscript", false); - obj.pushKV("iswitness", false); if (pwallet && pwallet->GetPubKey(keyID, vchPubKey)) { obj.pushKV("pubkey", HexStr(vchPubKey)); obj.pushKV("iscompressed", vchPubKey.IsCompressed()); @@ -98,8 +151,6 @@ public: UniValue operator()(const CScriptID &scriptID) const { UniValue obj(UniValue::VOBJ); CScript subscript; - obj.pushKV("isscript", true); - obj.pushKV("iswitness", false); if (pwallet && pwallet->GetCScript(scriptID, subscript)) { ProcessSubScript(subscript, obj, true); } @@ -110,10 +161,6 @@ public: { UniValue obj(UniValue::VOBJ); CPubKey pubkey; - obj.pushKV("isscript", false); - obj.pushKV("iswitness", true); - obj.pushKV("witness_version", 0); - obj.pushKV("witness_program", HexStr(id.begin(), id.end())); if (pwallet && pwallet->GetPubKey(CKeyID(id), pubkey)) { obj.pushKV("pubkey", HexStr(pubkey)); } @@ -124,10 +171,6 @@ public: { UniValue obj(UniValue::VOBJ); CScript subscript; - obj.pushKV("isscript", true); - obj.pushKV("iswitness", true); - obj.pushKV("witness_version", 0); - obj.pushKV("witness_program", HexStr(id.begin(), id.end())); CRIPEMD160 hasher; uint160 hash; hasher.Write(id.begin(), 32).Finalize(hash.begin()); @@ -137,15 +180,7 @@ public: return obj; } - UniValue operator()(const WitnessUnknown& id) const - { - UniValue obj(UniValue::VOBJ); - CScript subscript; - obj.pushKV("iswitness", true); - obj.pushKV("witness_version", (int)id.version); - obj.pushKV("witness_program", HexStr(id.program, id.program + id.length)); - return obj; - } + UniValue operator()(const WitnessUnknown& id) const { return UniValue(UniValue::VOBJ); } }; #endif @@ -219,8 +254,10 @@ UniValue validateaddress(const JSONRPCRequest& request) isminetype mine = pwallet ? IsMine(*pwallet, dest) : ISMINE_NO; ret.pushKV("ismine", bool(mine & ISMINE_SPENDABLE)); ret.pushKV("iswatchonly", bool(mine & ISMINE_WATCH_ONLY)); - UniValue detail = boost::apply_visitor(DescribeAddressVisitor(pwallet), dest); + UniValue detail = boost::apply_visitor(DescribeAddressVisitor(), dest); ret.pushKVs(detail); + UniValue wallet_detail = boost::apply_visitor(DescribeWalletAddressVisitor(pwallet), dest); + ret.pushKVs(wallet_detail); if (pwallet && pwallet->mapAddressBook.count(dest)) { ret.pushKV("account", pwallet->mapAddressBook[dest].name); } -- cgit v1.2.3