From 29fa24f09debcbb8778cb9e1072098f809c21a9c Mon Sep 17 00:00:00 2001 From: MarkLTZ Date: Tue, 10 Sep 2024 10:15:27 +0000 Subject: [PATCH] Add listaddresses RPC call --- src/wallet/rpcwallet.cpp | 31 ++++++++++++++++++++++++++++ src/wallet/test/rpc_wallet_tests.cpp | 6 ++++++ 2 files changed, 37 insertions(+) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index ae81aeb43..56daa4dd8 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -203,6 +203,36 @@ UniValue getrawchangeaddress(const UniValue& params, bool fHelp) return EncodeDestination(keyID); } +UniValue listaddresses(const UniValue& params, bool fHelp) +{ + if (!EnsureWalletIsAvailable(fHelp)) + return NullUniValue; + + if (fHelp || params.size() != 0) + throw runtime_error( + "listaddresses\n" + "\nReturns the list of transparent addresses belonging to the wallet.\n" + "\nResult:\n" + "[ (json array of string)\n" + " \"address\" (string) a transparent address belonging to the wallet\n" + " ,...\n" + "]\n" + "\nExamples:\n" + + HelpExampleCli("listaddresses", "") + + HelpExampleRpc("listaddresses", "") + ); + + LOCK2(cs_main, pwalletMain->cs_wallet); + + // Find all addresses + UniValue ret(UniValue::VARR); + for (const std::pair& item : pwalletMain->mapAddressBook) { + const CTxDestination& dest = item.first; + ret.push_back(EncodeDestination(dest)); + } + return ret; +} + static void SendMoney(const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, CWalletTx& wtxNew) { CAmount curBalance = pwalletMain->GetBalance(); @@ -4387,6 +4417,7 @@ static const CRPCCommand commands[] = { "wallet", "importaddress", &importaddress, true }, { "wallet", "importpubkey", &importpubkey, true }, { "wallet", "keypoolrefill", &keypoolrefill, true }, + { "wallet", "listaddresses", &listaddresses, true }, { "wallet", "listaddressgroupings", &listaddressgroupings, false }, { "wallet", "listlockunspent", &listlockunspent, false }, { "wallet", "listreceivedbyaddress", &listreceivedbyaddress, false }, diff --git a/src/wallet/test/rpc_wallet_tests.cpp b/src/wallet/test/rpc_wallet_tests.cpp index 5610d1b80..b457f0182 100644 --- a/src/wallet/test/rpc_wallet_tests.cpp +++ b/src/wallet/test/rpc_wallet_tests.cpp @@ -198,6 +198,12 @@ BOOST_AUTO_TEST_CASE(rpc_wallet) /* Correct address, message and signature*/ BOOST_CHECK(CallRPC("verifymessage " + EncodeDestination(demoAddress) + " " + retValue.get_str() + " mymessage").get_bool() == true); + /********************************* + * listaddresses + *********************************/ + BOOST_CHECK_THROW(CallRPC("listaddresses true"), runtime_error); + BOOST_CHECK_NO_THROW(retValue = CallRPC("listaddresses")); + /********************************* * fundrawtransaction *********************************/