Skip to content

Commit b65326b

Browse files
committed
Add matching descriptors to scantxoutset output + tests
1 parent 16203d5 commit b65326b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/rpc/blockchain.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,7 @@ UniValue scantxoutset(const JSONRPCRequest& request)
20742074
" \"txid\" : \"transactionid\", (string) The transaction id\n"
20752075
" \"vout\": n, (numeric) the vout value\n"
20762076
" \"scriptPubKey\" : \"script\", (string) the script key\n"
2077+
" \"desc\" : \"descriptor\", (string) A specialized descriptor for the matched scriptPubKey\n"
20772078
" \"amount\" : x.xxx, (numeric) The total amount in " + CURRENCY_UNIT + " of the unspent output\n"
20782079
" \"height\" : n, (numeric) Height of the unspent transaction output\n"
20792080
" }\n"
@@ -2108,6 +2109,7 @@ UniValue scantxoutset(const JSONRPCRequest& request)
21082109
throw JSONRPCError(RPC_INVALID_PARAMETER, "Scan already in progress, use action \"abort\" or \"status\"");
21092110
}
21102111
std::set<CScript> needles;
2112+
std::map<CScript, std::string> descriptors;
21112113
CAmount total_in = 0;
21122114

21132115
// loop through the scan objects
@@ -2140,7 +2142,11 @@ UniValue scantxoutset(const JSONRPCRequest& request)
21402142
if (!desc->Expand(i, provider, scripts, provider)) {
21412143
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys: '%s'", desc_str));
21422144
}
2143-
needles.insert(scripts.begin(), scripts.end());
2145+
for (const auto& script : scripts) {
2146+
std::string inferred = InferDescriptor(script, provider)->ToString();
2147+
needles.emplace(script);
2148+
descriptors.emplace(std::move(script), std::move(inferred));
2149+
}
21442150
}
21452151
}
21462152

@@ -2173,6 +2179,7 @@ UniValue scantxoutset(const JSONRPCRequest& request)
21732179
unspent.pushKV("txid", outpoint.hash.GetHex());
21742180
unspent.pushKV("vout", (int32_t)outpoint.n);
21752181
unspent.pushKV("scriptPubKey", HexStr(txo.scriptPubKey.begin(), txo.scriptPubKey.end()));
2182+
unspent.pushKV("desc", descriptors[txo.scriptPubKey]);
21762183
unspent.pushKV("amount", ValueFromAmount(txo.nValue));
21772184
unspent.pushKV("height", (int32_t)coin.nHeight);
21782185

test/functional/rpc_scantxoutset.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import shutil
1111
import os
1212

13+
def descriptors(out):
14+
return sorted(u['desc'] for u in out['unspents'])
15+
1316
class ScantxoutsetTest(BitcoinTestFramework):
1417
def set_test_params(self):
1518
self.num_nodes = 1
@@ -93,5 +96,10 @@ def run_test(self):
9396
assert_equal(self.nodes[0].scantxoutset("start", [ {"desc": "combo(tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/1/1/*)", "range": 1499}])['total_amount'], Decimal("12.288"))
9497
assert_equal(self.nodes[0].scantxoutset("start", [ {"desc": "combo(tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/1/1/*)", "range": 1500}])['total_amount'], Decimal("28.672"))
9598

99+
# Test the reported descriptors for a few matches
100+
assert_equal(descriptors(self.nodes[0].scantxoutset("start", [ {"desc": "combo(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/0h/0'/*)", "range": 1499}])), ["pkh([0c5f9a1e/0'/0'/0]026dbd8b2315f296d36e6b6920b1579ca75569464875c7ebe869b536a7d9503c8c)", "pkh([0c5f9a1e/0'/0'/1]033e6f25d76c00bedb3a8993c7d5739ee806397f0529b1b31dda31ef890f19a60c)"])
101+
assert_equal(descriptors(self.nodes[0].scantxoutset("start", [ "combo(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/1/0)"])), ["pkh([0c5f9a1e/1/1/0]03e1c5b6e650966971d7e71ef2674f80222752740fc1dfd63bbbd220d2da9bd0fb)"])
102+
assert_equal(descriptors(self.nodes[0].scantxoutset("start", [ {"desc": "combo(tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/1/1/*)", "range": 1500}])), ['pkh([0c5f9a1e/1/1/0]03e1c5b6e650966971d7e71ef2674f80222752740fc1dfd63bbbd220d2da9bd0fb)', 'pkh([0c5f9a1e/1/1/1500]03832901c250025da2aebae2bfb38d5c703a57ab66ad477f9c578bfbcd78abca6f)', 'pkh([0c5f9a1e/1/1/1]030d820fc9e8211c4169be8530efbc632775d8286167afd178caaf1089b77daba7)'])
103+
96104
if __name__ == '__main__':
97105
ScantxoutsetTest().main()

0 commit comments

Comments
 (0)