Skip to content

Commit 3eaa003

Browse files
committed
Extend validateaddress information for P2SH-embedded witness
This adds new fields 'pubkeys' and 'embedded' to the RPC's output, and improves the documentation for previously added 'witness_version' and 'witness_program' fields.
1 parent 30a27dc commit 3eaa003

File tree

2 files changed

+66
-24
lines changed

2 files changed

+66
-24
lines changed

src/rpc/misc.cpp

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,46 @@ class DescribeAddressVisitor : public boost::static_visitor<UniValue>
4040

4141
explicit DescribeAddressVisitor(CWallet *_pwallet) : pwallet(_pwallet) {}
4242

43+
void ProcessSubScript(const CScript& subscript, UniValue& obj, bool include_addresses = false) const
44+
{
45+
// Always present: script type and redeemscript
46+
txnouttype which_type;
47+
std::vector<std::vector<unsigned char>> solutions_data;
48+
Solver(subscript, which_type, solutions_data);
49+
obj.pushKV("script", GetTxnOutputType(which_type));
50+
obj.pushKV("hex", HexStr(subscript.begin(), subscript.end()));
51+
52+
CTxDestination embedded;
53+
UniValue a(UniValue::VARR);
54+
if (ExtractDestination(subscript, embedded)) {
55+
// Only when the script corresponds to an address.
56+
UniValue subobj = boost::apply_visitor(*this, embedded);
57+
subobj.pushKV("address", EncodeDestination(embedded));
58+
subobj.pushKV("scriptPubKey", HexStr(subscript.begin(), subscript.end()));
59+
// Always report the pubkey at the top level, so that `getnewaddress()['pubkey']` always works.
60+
if (subobj.exists("pubkey")) obj.pushKV("pubkey", subobj["pubkey"]);
61+
obj.pushKV("embedded", std::move(subobj));
62+
if (include_addresses) a.push_back(EncodeDestination(embedded));
63+
} else if (which_type == TX_MULTISIG) {
64+
// Also report some information on multisig scripts (which do not have a corresponding address).
65+
// TODO: abstract out the common functionality between this logic and ExtractDestinations.
66+
obj.pushKV("sigsrequired", solutions_data[0][0]);
67+
UniValue pubkeys(UniValue::VARR);
68+
for (size_t i = 1; i < solutions_data.size() - 1; ++i) {
69+
CPubKey key(solutions_data[i].begin(), solutions_data[i].end());
70+
if (include_addresses) a.push_back(EncodeDestination(key.GetID()));
71+
pubkeys.push_back(HexStr(key.begin(), key.end()));
72+
}
73+
obj.pushKV("pubkeys", std::move(pubkeys));
74+
}
75+
76+
// The "addresses" field is confusing because it refers to public keys using their P2PKH address.
77+
// For that reason, only add the 'addresses' field when needed for backward compatibility. New applications
78+
// can use the 'embedded'->'address' field for P2SH or P2WSH wrapped addresses, and 'pubkeys' for
79+
// inspecting multisig participants.
80+
if (include_addresses) obj.pushKV("addresses", std::move(a));
81+
}
82+
4383
UniValue operator()(const CNoDestination &dest) const { return UniValue(UniValue::VOBJ); }
4484

4585
UniValue operator()(const CKeyID &keyID) const {
@@ -60,19 +100,7 @@ class DescribeAddressVisitor : public boost::static_visitor<UniValue>
60100
obj.push_back(Pair("isscript", true));
61101
obj.push_back(Pair("iswitness", false));
62102
if (pwallet && pwallet->GetCScript(scriptID, subscript)) {
63-
std::vector<CTxDestination> addresses;
64-
txnouttype whichType;
65-
int nRequired;
66-
ExtractDestinations(subscript, whichType, addresses, nRequired);
67-
obj.push_back(Pair("script", GetTxnOutputType(whichType)));
68-
obj.push_back(Pair("hex", HexStr(subscript.begin(), subscript.end())));
69-
UniValue a(UniValue::VARR);
70-
for (const CTxDestination& addr : addresses) {
71-
a.push_back(EncodeDestination(addr));
72-
}
73-
obj.push_back(Pair("addresses", a));
74-
if (whichType == TX_MULTISIG)
75-
obj.push_back(Pair("sigsrequired", nRequired));
103+
ProcessSubScript(subscript, obj, true);
76104
}
77105
return obj;
78106
}
@@ -103,7 +131,7 @@ class DescribeAddressVisitor : public boost::static_visitor<UniValue>
103131
uint160 hash;
104132
hasher.Write(id.begin(), 32).Finalize(hash.begin());
105133
if (pwallet && pwallet->GetCScript(CScriptID(hash), subscript)) {
106-
obj.push_back(Pair("hex", HexStr(subscript.begin(), subscript.end())));
134+
ProcessSubScript(subscript, obj);
107135
}
108136
return obj;
109137
}
@@ -131,23 +159,32 @@ UniValue validateaddress(const JSONRPCRequest& request)
131159
"\nResult:\n"
132160
"{\n"
133161
" \"isvalid\" : true|false, (boolean) If the address is valid or not. If not, this is the only property returned.\n"
134-
" \"address\" : \"address\", (string) The bitcoin address validated\n"
162+
" \"address\" : \"address\", (string) The bitcoin address validated\n"
135163
" \"scriptPubKey\" : \"hex\", (string) The hex encoded scriptPubKey generated by the address\n"
136164
" \"ismine\" : true|false, (boolean) If the address is yours or not\n"
137165
" \"iswatchonly\" : true|false, (boolean) If the address is watchonly\n"
138-
" \"isscript\" : true|false, (boolean) If the key is a script\n"
139-
" \"script\" : \"type\" (string, optional) The output script type. Possible types: nonstandard, pubkey, pubkeyhash, scripthash, multisig, nulldata, witness_v0_keyhash, witness_v0_scripthash\n"
140-
" \"hex\" : \"hex\", (string, optional) The redeemscript for the p2sh address\n"
141-
" \"addresses\" (string, optional) Array of addresses associated with the known redeemscript\n"
166+
" \"isscript\" : true|false, (boolean, optional) If the address is P2SH or P2WSH. Not included for unknown witness types.\n"
167+
" \"iswitness\" : true|false, (boolean) If the address is P2WPKH, P2WSH, or an unknown witness version\n"
168+
" \"witness_version\" : version (number, optional) For all witness output types, gives the version number.\n"
169+
" \"witness_program\" : \"hex\" (string, optional) For all witness output types, gives the script or key hash present in the address.\n"
170+
" \"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"
171+
" \"hex\" : \"hex\", (string, optional) The redeemscript for the P2SH or P2WSH address\n"
172+
" \"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"
142173
" [\n"
143174
" \"address\"\n"
144175
" ,...\n"
145176
" ]\n"
146-
" \"sigsrequired\" : xxxxx (numeric, optional) Number of signatures required to spend multisig output\n"
147-
" \"pubkey\" : \"publickeyhex\", (string) The hex value of the raw public key\n"
177+
" \"pubkeys\" (string, optional) Array of pubkeys associated with the known redeemscript (only if \"script\" is \"multisig\")\n"
178+
" [\n"
179+
" \"pubkey\"\n"
180+
" ,...\n"
181+
" ]\n"
182+
" \"sigsrequired\" : xxxxx (numeric, optional) Number of signatures required to spend multisig output (only if \"script\" is \"multisig\")\n"
183+
" \"pubkey\" : \"publickeyhex\", (string, optional) The hex value of the raw public key, for single-key addresses (possibly embedded in P2SH or P2WSH)\n"
184+
" \"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"
148185
" \"iscompressed\" : true|false, (boolean) If the address is compressed\n"
149186
" \"account\" : \"account\" (string) DEPRECATED. The account associated with the address, \"\" is the default account\n"
150-
" \"timestamp\" : timestamp, (number, optional) The creation time of the key if available in seconds since epoch (Jan 1 1970 GMT)\n"
187+
" \"timestamp\" : timestamp, (number, optional) The creation time of the key if available in seconds since epoch (Jan 1 1970 GMT)\n"
151188
" \"hdkeypath\" : \"keypath\" (string, optional) The HD keypath if the key is HD and available\n"
152189
" \"hdmasterkeyid\" : \"<hash160>\" (string, optional) The Hash160 of the HD master pubkey\n"
153190
"}\n"
@@ -188,8 +225,9 @@ UniValue validateaddress(const JSONRPCRequest& request)
188225
}
189226
if (pwallet) {
190227
const CKeyMetadata* meta = nullptr;
191-
if (const CKeyID* key_id = boost::get<CKeyID>(&dest)) {
192-
auto it = pwallet->mapKeyMetadata.find(*key_id);
228+
CKeyID key_id = GetKeyForDestination(*pwallet, dest);
229+
if (!key_id.IsNull()) {
230+
auto it = pwallet->mapKeyMetadata.find(key_id);
193231
if (it != pwallet->mapKeyMetadata.end()) {
194232
meta = &it->second;
195233
}

src/script/standard.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
155155
* addressRet is populated with a single value and nRequiredRet is set to 1.
156156
* Returns true if successful. Currently does not extract address from
157157
* pay-to-witness scripts.
158+
*
159+
* Note: this function confuses destinations (a subset of CScripts that are
160+
* encodable as an address) with key identifiers (of keys involved in a
161+
* CScript), and its use should be phased out.
158162
*/
159163
bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
160164

0 commit comments

Comments
 (0)