Skip to content

Commit 5099624

Browse files
committed
rpc: sort listdescriptors result
1 parent ba2edca commit 5099624

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

src/wallet/rpc/backup.cpp

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,34 +1784,59 @@ RPCHelpMan listdescriptors()
17841784

17851785
LOCK(wallet->cs_wallet);
17861786

1787-
UniValue descriptors(UniValue::VARR);
17881787
const auto active_spk_mans = wallet->GetActiveScriptPubKeyMans();
1788+
1789+
struct WalletDescInfo {
1790+
std::string descriptor;
1791+
uint64_t creation_time;
1792+
bool active;
1793+
std::optional<bool> internal;
1794+
std::optional<std::pair<int64_t,int64_t>> range;
1795+
int64_t next_index;
1796+
};
1797+
1798+
std::vector<WalletDescInfo> wallet_descriptors;
17891799
for (const auto& spk_man : wallet->GetAllScriptPubKeyMans()) {
17901800
const auto desc_spk_man = dynamic_cast<DescriptorScriptPubKeyMan*>(spk_man);
17911801
if (!desc_spk_man) {
17921802
throw JSONRPCError(RPC_WALLET_ERROR, "Unexpected ScriptPubKey manager type.");
17931803
}
1794-
UniValue spk(UniValue::VOBJ);
17951804
LOCK(desc_spk_man->cs_desc_man);
17961805
const auto& wallet_descriptor = desc_spk_man->GetWalletDescriptor();
17971806
std::string descriptor;
1798-
17991807
if (!desc_spk_man->GetDescriptorString(descriptor, priv)) {
18001808
throw JSONRPCError(RPC_WALLET_ERROR, "Can't get descriptor string.");
18011809
}
1802-
spk.pushKV("desc", descriptor);
1803-
spk.pushKV("timestamp", wallet_descriptor.creation_time);
1804-
spk.pushKV("active", active_spk_mans.count(desc_spk_man) != 0);
1805-
const auto internal = wallet->IsInternalScriptPubKeyMan(desc_spk_man);
1806-
if (internal.has_value()) {
1807-
spk.pushKV("internal", *internal);
1810+
const bool is_range = wallet_descriptor.descriptor->IsRange();
1811+
wallet_descriptors.push_back({
1812+
descriptor,
1813+
wallet_descriptor.creation_time,
1814+
active_spk_mans.count(desc_spk_man) != 0,
1815+
wallet->IsInternalScriptPubKeyMan(desc_spk_man),
1816+
is_range ? std::optional(std::make_pair(wallet_descriptor.range_start, wallet_descriptor.range_end)) : std::nullopt,
1817+
wallet_descriptor.next_index
1818+
});
1819+
}
1820+
1821+
std::sort(wallet_descriptors.begin(), wallet_descriptors.end(), [](const auto& a, const auto& b) {
1822+
return a.descriptor < b.descriptor;
1823+
});
1824+
1825+
UniValue descriptors(UniValue::VARR);
1826+
for (const WalletDescInfo& info : wallet_descriptors) {
1827+
UniValue spk(UniValue::VOBJ);
1828+
spk.pushKV("desc", info.descriptor);
1829+
spk.pushKV("timestamp", info.creation_time);
1830+
spk.pushKV("active", info.active);
1831+
if (info.internal.has_value()) {
1832+
spk.pushKV("internal", info.internal.value());
18081833
}
1809-
if (wallet_descriptor.descriptor->IsRange()) {
1834+
if (info.range.has_value()) {
18101835
UniValue range(UniValue::VARR);
1811-
range.push_back(wallet_descriptor.range_start);
1812-
range.push_back(wallet_descriptor.range_end - 1);
1836+
range.push_back(info.range->first);
1837+
range.push_back(info.range->second - 1);
18131838
spk.pushKV("range", range);
1814-
spk.pushKV("next", wallet_descriptor.next_index);
1839+
spk.pushKV("next", info.next_index);
18151840
}
18161841
descriptors.push_back(spk);
18171842
}

0 commit comments

Comments
 (0)