Skip to content

Commit 4000b6e

Browse files
committed
[RPC] Define listcoldutxos function
to list all P2CS utxos belonging to the wallet (either as cold-staker or coin-owner)
1 parent f771f86 commit 4000b6e

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
4848
{"settxfee", 0},
4949
{"getreceivedbyaddress", 1},
5050
{"getreceivedbyaccount", 1},
51+
{"listcoldutxos", 0},
5152
{"listreceivedbyaddress", 0},
5253
{"listreceivedbyaddress", 1},
5354
{"listreceivedbyaddress", 2},

src/rpc/server.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ static const CRPCCommand vRPCCommands[] =
427427
{"wallet", "keypoolrefill", &keypoolrefill, true, false, true},
428428
{"wallet", "listaccounts", &listaccounts, false, false, true},
429429
{"wallet", "listaddressgroupings", &listaddressgroupings, false, false, true},
430+
{"wallet", "listcoldutxos", &listcoldutxos, false, false, true},
430431
{"wallet", "listlockunspent", &listlockunspent, false, false, true},
431432
{"wallet", "listreceivedbyaccount", &listreceivedbyaccount, false, false, true},
432433
{"wallet", "listreceivedbyaddress", &listreceivedbyaddress, false, false, true},

src/rpc/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ extern UniValue movecmd(const UniValue& params, bool fHelp);
235235
extern UniValue sendfrom(const UniValue& params, bool fHelp);
236236
extern UniValue sendmany(const UniValue& params, bool fHelp);
237237
extern UniValue addmultisigaddress(const UniValue& params, bool fHelp);
238+
extern UniValue listcoldutxos(const UniValue& params, bool fHelp);
238239
extern UniValue listreceivedbyaddress(const UniValue& params, bool fHelp);
239240
extern UniValue listreceivedbyaccount(const UniValue& params, bool fHelp);
240241
extern UniValue listtransactions(const UniValue& params, bool fHelp);

src/wallet/rpcwallet.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,73 @@ UniValue listreceivedbyaccount(const UniValue& params, bool fHelp)
16091609
return ListReceived(params, true);
16101610
}
16111611

1612+
UniValue listcoldutxos(const UniValue& params, bool fHelp)
1613+
{
1614+
if (fHelp || params.size() > 1)
1615+
throw std::runtime_error(
1616+
"listcoldutxos ( nonWhitelistedOnly )\n"
1617+
"\nList P2CS unspent outputs received by this wallet as cold-staker-\n"
1618+
1619+
"\nArguments:\n"
1620+
"1. nonWhitelistedOnly (boolean, optional, default=false) Whether to exclude P2CS from whitelisted delegators.\n"
1621+
1622+
"\nResult:\n"
1623+
"[\n"
1624+
" {\n"
1625+
" \"txid\" : \"true\", (string) The transaction id of the P2CS utxo\n"
1626+
" \"txidn\" : \"accountname\", (string) The output number of the P2CS utxo\n"
1627+
" \"amount\" : x.xxx, (numeric) The amount of the P2CS utxo\n"
1628+
" \"confirmations\" : n (numeric) The number of confirmations of the P2CS utxo\n"
1629+
" \"cold-staker\" : n (string) The cold-staker address of the P2CS utxo\n"
1630+
" \"coin-owner\" : n (string) The coin-owner address of the P2CS utxo\n"
1631+
" \"whitelisted\" : n (string) \"true\"/\"false\" coin-owner in delegator whitelist\n"
1632+
" }\n"
1633+
" ,...\n"
1634+
"]\n"
1635+
1636+
"\nExamples:\n" +
1637+
HelpExampleCli("listcoldutxos", "") + HelpExampleCli("listcoldutxos", "true"));
1638+
1639+
LOCK2(cs_main, pwalletMain->cs_wallet);
1640+
1641+
bool fExcludeWhitelisted = false;
1642+
if (params.size() > 0)
1643+
fExcludeWhitelisted = params[0].get_bool();
1644+
UniValue results(UniValue::VARR);
1645+
1646+
for (std::map<uint256, CWalletTx>::const_iterator it =
1647+
pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) {
1648+
const uint256& wtxid = it->first;
1649+
const CWalletTx* pcoin = &(*it).second;
1650+
1651+
for (unsigned int i = 0; i < pcoin->vout.size(); i++) {
1652+
const CTxOut& out = pcoin->vout[i];
1653+
isminetype mine = pwalletMain->IsMine(out);
1654+
if (!bool(mine & ISMINE_COLD) && !bool(mine & ISMINE_SPENDABLE_DELEGATED))
1655+
continue;
1656+
txnouttype type;
1657+
std::vector<CTxDestination> addresses;
1658+
int nRequired;
1659+
if (!ExtractDestinations(out.scriptPubKey, type, addresses, nRequired))
1660+
continue;
1661+
const bool fWhitelisted = pwalletMain->mapAddressBook.count(addresses[1]) > 0;
1662+
if (fExcludeWhitelisted && fWhitelisted)
1663+
continue;
1664+
UniValue entry(UniValue::VOBJ);
1665+
entry.push_back(Pair("txid", wtxid.GetHex()));
1666+
entry.push_back(Pair("txidn", (int)i));
1667+
entry.push_back(Pair("amount", ValueFromAmount(out.nValue)));
1668+
entry.push_back(Pair("confirmations", pcoin->GetDepthInMainChain(false)));
1669+
entry.push_back(Pair("cold-staker", CBitcoinAddress(addresses[0]).ToString()));
1670+
entry.push_back(Pair("coin-owner", CBitcoinAddress(addresses[1]).ToString()));
1671+
entry.push_back(Pair("whitelisted", fWhitelisted ? "true" : "false"));
1672+
results.push_back(entry);
1673+
}
1674+
}
1675+
1676+
return results;
1677+
}
1678+
16121679
static void MaybePushAddress(UniValue & entry, const CTxDestination &dest)
16131680
{
16141681
CBitcoinAddress addr;

0 commit comments

Comments
 (0)