Skip to content

Commit 26d3fad

Browse files
committed
Add unmodified-but-with-checksum to getdescriptorinfo
1 parent 104b3a5 commit 26d3fad

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

src/rpc/misc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ UniValue getdescriptorinfo(const JSONRPCRequest& request)
136136
RPCResult{
137137
"{\n"
138138
" \"descriptor\" : \"desc\", (string) The descriptor in canonical form, without private keys\n"
139+
" \"checksum\" : \"chksum\", (string) The checksum for the input descriptor\n"
139140
" \"isrange\" : true|false, (boolean) Whether the descriptor is ranged\n"
140141
" \"issolvable\" : true|false, (boolean) Whether the descriptor is solvable\n"
141142
" \"hasprivatekeys\" : true|false, (boolean) Whether the input descriptor contained at least one private key\n"
@@ -156,6 +157,7 @@ UniValue getdescriptorinfo(const JSONRPCRequest& request)
156157

157158
UniValue result(UniValue::VOBJ);
158159
result.pushKV("descriptor", desc->ToString());
160+
result.pushKV("checksum", GetDescriptorChecksum(request.params[0].get_str()));
159161
result.pushKV("isrange", desc->IsRange());
160162
result.pushKV("issolvable", desc->IsSolvable());
161163
result.pushKV("hasprivatekeys", provider.keys.size() > 0);

src/script/descriptor.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,14 @@ std::unique_ptr<Descriptor> Parse(const std::string& descriptor, FlatSigningProv
938938
return nullptr;
939939
}
940940

941+
std::string GetDescriptorChecksum(const std::string& descriptor)
942+
{
943+
std::string ret;
944+
Span<const char> sp(descriptor.data(), descriptor.size());
945+
if (!CheckChecksum(sp, false, &ret)) return "";
946+
return ret;
947+
}
948+
941949
std::unique_ptr<Descriptor> InferDescriptor(const CScript& script, const SigningProvider& provider)
942950
{
943951
return InferScript(script, ParseScriptContext::TOP, provider);

src/script/descriptor.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ struct Descriptor {
8181
*/
8282
std::unique_ptr<Descriptor> Parse(const std::string& descriptor, FlatSigningProvider& out, bool require_checksum = false);
8383

84+
/** Get the checksum for a descriptor.
85+
*
86+
* If it already has one, and it is correct, return the checksum in the input.
87+
* If it already has one that is wrong, return "".
88+
* If it does not already have one, return the checksum that would need to be added.
89+
*/
90+
std::string GetDescriptorChecksum(const std::string& descriptor);
91+
8492
/** Find a descriptor for the specified script, using information from provider where possible.
8593
*
8694
* A non-ranged descriptor which only generates the specified script will be returned in all

test/functional/wallet_address_types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ def test_desc(self, node, address, multisig, typ, utxo):
175175
assert info['desc'] == descsum_create(info['desc'][:-9])
176176
# Verify that stripping the checksum and feeding it to getdescriptorinfo roundtrips
177177
assert info['desc'] == self.nodes[0].getdescriptorinfo(info['desc'][:-9])['descriptor']
178+
assert_equal(info['desc'][-8:], self.nodes[0].getdescriptorinfo(info['desc'][:-9])['checksum'])
179+
# Verify that keeping the checksum and feeding it to getdescriptorinfo roundtrips
180+
assert info['desc'] == self.nodes[0].getdescriptorinfo(info['desc'])['descriptor']
181+
assert_equal(info['desc'][-8:], self.nodes[0].getdescriptorinfo(info['desc'])['checksum'])
178182

179183
if not multisig and typ == 'legacy':
180184
# P2PKH

0 commit comments

Comments
 (0)