Add getdescriptorinfo to compute checksum

This commit is contained in:
Pieter Wuille 2019-02-07 15:09:08 -08:00
parent 3b40bff988
commit b52cb63688
2 changed files with 43 additions and 0 deletions

View file

@ -143,6 +143,46 @@ static UniValue createmultisig(const JSONRPCRequest& request)
return result;
}
UniValue getdescriptorinfo(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 1) {
throw std::runtime_error(
RPCHelpMan{"getdescriptorinfo",
{"\nAnalyses a descriptor.\n"},
{
{"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor."},
},
RPCResult{
"{\n"
" \"descriptor\" : \"desc\", (string) The descriptor in canonical form, without private keys\n"
" \"isrange\" : true|false, (boolean) Whether the descriptor is ranged\n"
" \"issolvable\" : true|false, (boolean) Whether the descriptor is solvable\n"
" \"hasprivatekeys\" : true|false, (boolean) Whether the input descriptor contained at least one private key\n"
"}\n"
},
RPCExamples{
"Analyse a descriptor\n" +
HelpExampleCli("getdescriptorinfo", "\"wpkh([d34db33f/84h/0h/0h]0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2dce28d959f2815b16f81798)\"")
}}.ToString()
);
}
RPCTypeCheck(request.params, {UniValue::VSTR});
FlatSigningProvider provider;
auto desc = Parse(request.params[0].get_str(), provider);
if (!desc) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Invalid descriptor"));
}
UniValue result(UniValue::VOBJ);
result.pushKV("descriptor", desc->ToString());
result.pushKV("isrange", desc->IsRange());
result.pushKV("issolvable", desc->IsSolvable());
result.pushKV("hasprivatekeys", provider.keys.size() > 0);
return result;
}
UniValue deriveaddresses(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.empty() || request.params.size() > 3) {
@ -564,6 +604,7 @@ static const CRPCCommand commands[] =
{ "util", "validateaddress", &validateaddress, {"address"} },
{ "util", "createmultisig", &createmultisig, {"nrequired","keys","address_type"} },
{ "util", "deriveaddresses", &deriveaddresses, {"descriptor", "begin", "end"} },
{ "util", "getdescriptorinfo", &getdescriptorinfo, {"descriptor"} },
{ "util", "verifymessage", &verifymessage, {"address","signature","message"} },
{ "util", "signmessagewithprivkey", &signmessagewithprivkey, {"privkey","message"} },

View file

@ -175,6 +175,8 @@ class AddressTypeTest(BitcoinTestFramework):
assert(descsum_check(info['desc']))
# Verify that stripping the checksum and recreating it using Python roundtrips
assert(info['desc'] == descsum_create(info['desc'][:-9]))
# Verify that stripping the checksum and feeding it to getdescriptorinfo roundtrips
assert(info['desc'] == self.nodes[0].getdescriptorinfo(info['desc'][:-9])['descriptor'])
if not multisig and typ == 'legacy':
# P2PKH