Skip to content

Commit b98bfc5

Browse files
committed
Create getaddressinfo RPC and deprecate parts of validateaddress
Moves the parts of validateaddress which require the wallet into getaddressinfo which is part of the wallet RPCs. Mark those parts of validateaddress which require the wallet as deprecated. Validateaddress will call getaddressinfo for the data that both share for right now. Moves IsMine functions to libbitcoin_common and then links libbitcoin_wallet before libbitcoin_common in order to prevent linker errors since IsMine is no longer used in libbitcoin_server.
1 parent 1598f32 commit b98bfc5

File tree

8 files changed

+253
-194
lines changed

8 files changed

+253
-194
lines changed

src/Makefile.am

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ BITCOIN_CORE_H = \
135135
rpc/register.h \
136136
rpc/util.h \
137137
scheduler.h \
138+
script/ismine.h \
138139
script/sigcache.h \
139140
script/sign.h \
140141
script/standard.h \
141-
script/ismine.h \
142142
streams.h \
143143
support/allocators/secure.h \
144144
support/allocators/zeroafterfree.h \
@@ -215,9 +215,7 @@ libbitcoin_server_a_SOURCES = \
215215
rpc/rawtransaction.cpp \
216216
rpc/safemode.cpp \
217217
rpc/server.cpp \
218-
rpc/util.cpp \
219218
script/sigcache.cpp \
220-
script/ismine.cpp \
221219
timedata.cpp \
222220
torcontrol.cpp \
223221
txdb.cpp \
@@ -334,6 +332,7 @@ libbitcoin_common_a_SOURCES = \
334332
policy/feerate.cpp \
335333
protocol.cpp \
336334
scheduler.cpp \
335+
script/ismine.cpp \
337336
script/sign.cpp \
338337
script/standard.cpp \
339338
warnings.cpp \
@@ -390,10 +389,10 @@ endif
390389

391390
bitcoind_LDADD = \
392391
$(LIBBITCOIN_SERVER) \
392+
$(LIBBITCOIN_WALLET) \
393393
$(LIBBITCOIN_COMMON) \
394394
$(LIBUNIVALUE) \
395395
$(LIBBITCOIN_UTIL) \
396-
$(LIBBITCOIN_WALLET) \
397396
$(LIBBITCOIN_ZMQ) \
398397
$(LIBBITCOIN_CONSENSUS) \
399398
$(LIBBITCOIN_CRYPTO) \

src/Makefile.bench.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ bench_bench_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(EVENT_CLFAGS
3535
bench_bench_bitcoin_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
3636
bench_bench_bitcoin_LDADD = \
3737
$(LIBBITCOIN_SERVER) \
38+
$(LIBBITCOIN_WALLET) \
3839
$(LIBBITCOIN_COMMON) \
3940
$(LIBBITCOIN_UTIL) \
4041
$(LIBBITCOIN_CONSENSUS) \
@@ -51,7 +52,6 @@ endif
5152

5253
if ENABLE_WALLET
5354
bench_bench_bitcoin_SOURCES += bench/coin_selection.cpp
54-
bench_bench_bitcoin_LDADD += $(LIBBITCOIN_WALLET) $(LIBBITCOIN_CRYPTO)
5555
endif
5656

5757
bench_bench_bitcoin_LDADD += $(BOOST_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS)

src/rpc/misc.cpp

Lines changed: 21 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -33,216 +33,56 @@
3333

3434
#include <univalue.h>
3535

36-
#ifdef ENABLE_WALLET
37-
class DescribeWalletAddressVisitor : public boost::static_visitor<UniValue>
38-
{
39-
public:
40-
CWallet * const pwallet;
41-
42-
void ProcessSubScript(const CScript& subscript, UniValue& obj, bool include_addresses = false) const
43-
{
44-
// Always present: script type and redeemscript
45-
txnouttype which_type;
46-
std::vector<std::vector<unsigned char>> solutions_data;
47-
Solver(subscript, which_type, solutions_data);
48-
obj.pushKV("script", GetTxnOutputType(which_type));
49-
obj.pushKV("hex", HexStr(subscript.begin(), subscript.end()));
50-
51-
CTxDestination embedded;
52-
UniValue a(UniValue::VARR);
53-
if (ExtractDestination(subscript, embedded)) {
54-
// Only when the script corresponds to an address.
55-
UniValue subobj(UniValue::VOBJ);
56-
UniValue detail = boost::apply_visitor(DescribeAddressVisitor(), embedded);
57-
subobj.pushKVs(detail);
58-
UniValue wallet_detail = boost::apply_visitor(*this, embedded);
59-
subobj.pushKVs(wallet_detail);
60-
subobj.pushKV("address", EncodeDestination(embedded));
61-
subobj.pushKV("scriptPubKey", HexStr(subscript.begin(), subscript.end()));
62-
// Always report the pubkey at the top level, so that `getnewaddress()['pubkey']` always works.
63-
if (subobj.exists("pubkey")) obj.pushKV("pubkey", subobj["pubkey"]);
64-
obj.pushKV("embedded", std::move(subobj));
65-
if (include_addresses) a.push_back(EncodeDestination(embedded));
66-
} else if (which_type == TX_MULTISIG) {
67-
// Also report some information on multisig scripts (which do not have a corresponding address).
68-
// TODO: abstract out the common functionality between this logic and ExtractDestinations.
69-
obj.pushKV("sigsrequired", solutions_data[0][0]);
70-
UniValue pubkeys(UniValue::VARR);
71-
for (size_t i = 1; i < solutions_data.size() - 1; ++i) {
72-
CPubKey key(solutions_data[i].begin(), solutions_data[i].end());
73-
if (include_addresses) a.push_back(EncodeDestination(key.GetID()));
74-
pubkeys.push_back(HexStr(key.begin(), key.end()));
75-
}
76-
obj.pushKV("pubkeys", std::move(pubkeys));
77-
}
78-
79-
// The "addresses" field is confusing because it refers to public keys using their P2PKH address.
80-
// For that reason, only add the 'addresses' field when needed for backward compatibility. New applications
81-
// can use the 'embedded'->'address' field for P2SH or P2WSH wrapped addresses, and 'pubkeys' for
82-
// inspecting multisig participants.
83-
if (include_addresses) obj.pushKV("addresses", std::move(a));
84-
}
85-
86-
explicit DescribeWalletAddressVisitor(CWallet *_pwallet) : pwallet(_pwallet) {}
87-
88-
UniValue operator()(const CNoDestination &dest) const { return UniValue(UniValue::VOBJ); }
89-
90-
UniValue operator()(const CKeyID &keyID) const {
91-
UniValue obj(UniValue::VOBJ);
92-
CPubKey vchPubKey;
93-
if (pwallet && pwallet->GetPubKey(keyID, vchPubKey)) {
94-
obj.pushKV("pubkey", HexStr(vchPubKey));
95-
obj.pushKV("iscompressed", vchPubKey.IsCompressed());
96-
}
97-
return obj;
98-
}
99-
100-
UniValue operator()(const CScriptID &scriptID) const {
101-
UniValue obj(UniValue::VOBJ);
102-
CScript subscript;
103-
if (pwallet && pwallet->GetCScript(scriptID, subscript)) {
104-
ProcessSubScript(subscript, obj, true);
105-
}
106-
return obj;
107-
}
108-
109-
UniValue operator()(const WitnessV0KeyHash& id) const
110-
{
111-
UniValue obj(UniValue::VOBJ);
112-
CPubKey pubkey;
113-
if (pwallet && pwallet->GetPubKey(CKeyID(id), pubkey)) {
114-
obj.pushKV("pubkey", HexStr(pubkey));
115-
}
116-
return obj;
117-
}
118-
119-
UniValue operator()(const WitnessV0ScriptHash& id) const
120-
{
121-
UniValue obj(UniValue::VOBJ);
122-
CScript subscript;
123-
CRIPEMD160 hasher;
124-
uint160 hash;
125-
hasher.Write(id.begin(), 32).Finalize(hash.begin());
126-
if (pwallet && pwallet->GetCScript(CScriptID(hash), subscript)) {
127-
ProcessSubScript(subscript, obj);
128-
}
129-
return obj;
130-
}
131-
132-
UniValue operator()(const WitnessUnknown& id) const { return UniValue(UniValue::VOBJ); }
133-
};
134-
135-
UniValue DescribeWalletAddress(CWallet* pwallet, const CTxDestination& dest)
136-
{
137-
UniValue ret(UniValue::VOBJ);
138-
UniValue detail = DescribeAddress(dest);
139-
ret.pushKVs(detail);
140-
ret.pushKVs(boost::apply_visitor(DescribeWalletAddressVisitor(pwallet), dest));
141-
return ret;
142-
}
143-
#endif
144-
14536
UniValue validateaddress(const JSONRPCRequest& request)
14637
{
14738
if (request.fHelp || request.params.size() != 1)
14839
throw std::runtime_error(
14940
"validateaddress \"address\"\n"
15041
"\nReturn information about the given bitcoin address.\n"
42+
"DEPRECATION WARNING: Parts of this command have been deprecated and moved to getaddressinfo. Clients must\n"
43+
"transition to using getaddressinfo to access this information before upgrading to v0.18. The following deprecated\n"
44+
"fields have moved to getaddressinfo and will only be shown here with -deprecatedrpc=validateaddress: ismine, iswatchonly,\n"
45+
"script, hex, pubkeys, sigsrequired, pubkey, addresses, embedded, iscompressed, account, timestamp, hdkeypath, kdmasterkeyid.\n"
15146
"\nArguments:\n"
152-
"1. \"address\" (string, required) The bitcoin address to validate\n"
47+
"1. \"address\" (string, required) The bitcoin address to validate\n"
15348
"\nResult:\n"
15449
"{\n"
15550
" \"isvalid\" : true|false, (boolean) If the address is valid or not. If not, this is the only property returned.\n"
15651
" \"address\" : \"address\", (string) The bitcoin address validated\n"
15752
" \"scriptPubKey\" : \"hex\", (string) The hex encoded scriptPubKey generated by the address\n"
158-
" \"ismine\" : true|false, (boolean) If the address is yours or not\n"
159-
" \"iswatchonly\" : true|false, (boolean) If the address is watchonly\n"
160-
" \"isscript\" : true|false, (boolean, optional) If the address is P2SH or P2WSH. Not included for unknown witness types.\n"
161-
" \"iswitness\" : true|false, (boolean) If the address is P2WPKH, P2WSH, or an unknown witness version\n"
162-
" \"witness_version\" : version (number, optional) For all witness output types, gives the version number.\n"
163-
" \"witness_program\" : \"hex\" (string, optional) For all witness output types, gives the script or key hash present in the address.\n"
164-
" \"script\" : \"type\" (string, optional) The output script type. Only if \"isscript\" is true and the redeemscript is known. Possible types: nonstandard, pubkey, pubkeyhash, scripthash, multisig, nulldata, witness_v0_keyhash, witness_v0_scripthash, witness_unknown\n"
165-
" \"hex\" : \"hex\", (string, optional) The redeemscript for the P2SH or P2WSH address\n"
166-
" \"addresses\" (string, optional) Array of addresses associated with the known redeemscript (only if \"iswitness\" is false). This field is superseded by the \"pubkeys\" field and the address inside \"embedded\".\n"
167-
" [\n"
168-
" \"address\"\n"
169-
" ,...\n"
170-
" ]\n"
171-
" \"pubkeys\" (string, optional) Array of pubkeys associated with the known redeemscript (only if \"script\" is \"multisig\")\n"
172-
" [\n"
173-
" \"pubkey\"\n"
174-
" ,...\n"
175-
" ]\n"
176-
" \"sigsrequired\" : xxxxx (numeric, optional) Number of signatures required to spend multisig output (only if \"script\" is \"multisig\")\n"
177-
" \"pubkey\" : \"publickeyhex\", (string, optional) The hex value of the raw public key, for single-key addresses (possibly embedded in P2SH or P2WSH)\n"
178-
" \"embedded\" : {...}, (object, optional) information about the address embedded in P2SH or P2WSH, if relevant and known. It includes all validateaddress output fields for the embedded address, excluding \"isvalid\", metadata (\"timestamp\", \"hdkeypath\", \"hdmasterkeyid\") and relation to the wallet (\"ismine\", \"iswatchonly\", \"account\").\n"
179-
" \"iscompressed\" : true|false, (boolean) If the address is compressed\n"
180-
" \"account\" : \"account\" (string) DEPRECATED. The account associated with the address, \"\" is the default account\n"
181-
" \"timestamp\" : timestamp, (number, optional) The creation time of the key if available in seconds since epoch (Jan 1 1970 GMT)\n"
182-
" \"hdkeypath\" : \"keypath\" (string, optional) The HD keypath if the key is HD and available\n"
183-
" \"hdmasterkeyid\" : \"<hash160>\" (string, optional) The Hash160 of the HD master pubkey\n"
53+
" \"isscript\" : true|false, (boolean) If the key is a script\n"
54+
" \"iswitness\" : true|false, (boolean) If the address is a witness address\n"
55+
" \"witness_version\" : version (numeric, optional) The version number of the witness program\n"
56+
" \"witness_program\" : \"hex\" (string, optional) The hex value of the witness program\n"
18457
"}\n"
18558
"\nExamples:\n"
18659
+ HelpExampleCli("validateaddress", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"")
18760
+ HelpExampleRpc("validateaddress", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"")
18861
);
18962

190-
#ifdef ENABLE_WALLET
191-
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
192-
193-
LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : nullptr);
194-
#else
195-
LOCK(cs_main);
196-
#endif
197-
19863
CTxDestination dest = DecodeDestination(request.params[0].get_str());
19964
bool isValid = IsValidDestination(dest);
20065

20166
UniValue ret(UniValue::VOBJ);
20267
ret.pushKV("isvalid", isValid);
20368
if (isValid)
20469
{
205-
std::string currentAddress = EncodeDestination(dest);
206-
ret.pushKV("address", currentAddress);
207-
208-
CScript scriptPubKey = GetScriptForDestination(dest);
209-
ret.pushKV("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
21070

21171
#ifdef ENABLE_WALLET
212-
isminetype mine = pwallet ? IsMine(*pwallet, dest) : ISMINE_NO;
213-
ret.pushKV("ismine", bool(mine & ISMINE_SPENDABLE));
214-
ret.pushKV("iswatchonly", bool(mine & ISMINE_WATCH_ONLY));
215-
UniValue detail = DescribeWalletAddress(pwallet, dest);
216-
ret.pushKVs(detail);
217-
if (pwallet && pwallet->mapAddressBook.count(dest)) {
218-
ret.pushKV("account", pwallet->mapAddressBook[dest].name);
219-
}
220-
if (pwallet) {
221-
const CKeyMetadata* meta = nullptr;
222-
CKeyID key_id = GetKeyForDestination(*pwallet, dest);
223-
if (!key_id.IsNull()) {
224-
auto it = pwallet->mapKeyMetadata.find(key_id);
225-
if (it != pwallet->mapKeyMetadata.end()) {
226-
meta = &it->second;
227-
}
228-
}
229-
if (!meta) {
230-
auto it = pwallet->m_script_metadata.find(CScriptID(scriptPubKey));
231-
if (it != pwallet->m_script_metadata.end()) {
232-
meta = &it->second;
233-
}
234-
}
235-
if (meta) {
236-
ret.pushKV("timestamp", meta->nCreateTime);
237-
if (!meta->hdKeypath.empty()) {
238-
ret.pushKV("hdkeypath", meta->hdKeypath);
239-
ret.pushKV("hdmasterkeyid", meta->hdMasterKeyID.GetHex());
240-
}
241-
}
72+
if (!::vpwallets.empty() && IsDeprecatedRPCEnabled("validateaddress")) {
73+
ret.pushKVs(getaddressinfo(request));
24274
}
243-
#else
244-
ret.pushKvs = DescribeAddress(dest);
24575
#endif
76+
if (ret["address"].isNull()) {
77+
std::string currentAddress = EncodeDestination(dest);
78+
ret.pushKV("address", currentAddress);
79+
80+
CScript scriptPubKey = GetScriptForDestination(dest);
81+
ret.pushKV("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end()));;
82+
83+
UniValue detail = DescribeAddress(dest);
84+
ret.pushKVs(detail);
85+
}
24686
}
24787
return ret;
24888
}

0 commit comments

Comments
 (0)