[rpc] Move DescribeAddressVisitor to rpc/util

This commit is contained in:
John Newbery 2017-12-04 12:49:20 -05:00 committed by Andrew Chow
parent 39633ecd5c
commit 1598f32304
4 changed files with 83 additions and 54 deletions

View File

@ -215,6 +215,7 @@ libbitcoin_server_a_SOURCES = \
rpc/rawtransaction.cpp \ rpc/rawtransaction.cpp \
rpc/safemode.cpp \ rpc/safemode.cpp \
rpc/server.cpp \ rpc/server.cpp \
rpc/util.cpp \
script/sigcache.cpp \ script/sigcache.cpp \
script/ismine.cpp \ script/ismine.cpp \
timedata.cpp \ timedata.cpp \

View File

@ -33,57 +33,6 @@
#include <univalue.h> #include <univalue.h>
class DescribeAddressVisitor : public boost::static_visitor<UniValue>
{
public:
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;
}
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 #ifdef ENABLE_WALLET
class DescribeWalletAddressVisitor : public boost::static_visitor<UniValue> class DescribeWalletAddressVisitor : public boost::static_visitor<UniValue>
{ {
@ -182,6 +131,15 @@ public:
UniValue operator()(const WitnessUnknown& id) const { return UniValue(UniValue::VOBJ); } UniValue operator()(const WitnessUnknown& id) const { return UniValue(UniValue::VOBJ); }
}; };
UniValue DescribeWalletAddress(CWallet* pwallet, const CTxDestination& dest)
{
UniValue ret(UniValue::VOBJ);
UniValue detail = DescribeAddress(dest);
ret.pushKVs(detail);
ret.pushKVs(boost::apply_visitor(DescribeWalletAddressVisitor(pwallet), dest));
return ret;
}
#endif #endif
UniValue validateaddress(const JSONRPCRequest& request) UniValue validateaddress(const JSONRPCRequest& request)
@ -254,10 +212,8 @@ UniValue validateaddress(const JSONRPCRequest& request)
isminetype mine = pwallet ? IsMine(*pwallet, dest) : ISMINE_NO; isminetype mine = pwallet ? IsMine(*pwallet, dest) : ISMINE_NO;
ret.pushKV("ismine", bool(mine & ISMINE_SPENDABLE)); ret.pushKV("ismine", bool(mine & ISMINE_SPENDABLE));
ret.pushKV("iswatchonly", bool(mine & ISMINE_WATCH_ONLY)); ret.pushKV("iswatchonly", bool(mine & ISMINE_WATCH_ONLY));
UniValue detail = boost::apply_visitor(DescribeAddressVisitor(), dest); UniValue detail = DescribeWalletAddress(pwallet, dest);
ret.pushKVs(detail); ret.pushKVs(detail);
UniValue wallet_detail = boost::apply_visitor(DescribeWalletAddressVisitor(pwallet), dest);
ret.pushKVs(wallet_detail);
if (pwallet && pwallet->mapAddressBook.count(dest)) { if (pwallet && pwallet->mapAddressBook.count(dest)) {
ret.pushKV("account", pwallet->mapAddressBook[dest].name); ret.pushKV("account", pwallet->mapAddressBook[dest].name);
} }
@ -284,6 +240,8 @@ UniValue validateaddress(const JSONRPCRequest& request)
} }
} }
} }
#else
ret.pushKvs = DescribeAddress(dest);
#endif #endif
} }
return ret; return ret;

View File

@ -66,3 +66,64 @@ CScript CreateMultisigRedeemscript(const int required, const std::vector<CPubKey
return result; return result;
} }
class DescribeAddressVisitor : public boost::static_visitor<UniValue>
{
public:
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;
}
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;
}
};
UniValue DescribeAddress(const CTxDestination& dest)
{
return boost::apply_visitor(DescribeAddressVisitor(), dest);
}

View File

@ -5,6 +5,13 @@
#ifndef BITCOIN_RPC_UTIL_H #ifndef BITCOIN_RPC_UTIL_H
#define BITCOIN_RPC_UTIL_H #define BITCOIN_RPC_UTIL_H
#include <pubkey.h>
#include <script/standard.h>
#include <univalue.h>
#include <utilstrencodings.h>
#include <boost/variant/static_visitor.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
@ -16,4 +23,6 @@ CPubKey HexToPubKey(const std::string& hex_in);
CPubKey AddrToPubKey(CKeyStore* const keystore, const std::string& addr_in); CPubKey AddrToPubKey(CKeyStore* const keystore, const std::string& addr_in);
CScript CreateMultisigRedeemscript(const int required, const std::vector<CPubKey>& pubkeys); CScript CreateMultisigRedeemscript(const int required, const std::vector<CPubKey>& pubkeys);
UniValue DescribeAddress(const CTxDestination& dest);
#endif // BITCOIN_RPC_UTIL_H #endif // BITCOIN_RPC_UTIL_H