LCOV - code coverage report
Current view: top level - src/wallet/rpc - addresses.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 17 490 3.5 %
Date: 2023-11-06 23:13:05 Functions: 0 41 0.0 %
Branches: 21 1786 1.2 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2011-2022 The Bitcoin Core developers
       2                 :            : // Distributed under the MIT software license, see the accompanying
       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :            : 
       5                 :            : #include <core_io.h>
       6                 :            : #include <key_io.h>
       7                 :            : #include <rpc/util.h>
       8                 :            : #include <script/script.h>
       9                 :            : #include <script/solver.h>
      10                 :            : #include <util/bip32.h>
      11                 :            : #include <util/translation.h>
      12                 :            : #include <wallet/receive.h>
      13                 :            : #include <wallet/rpc/util.h>
      14                 :            : #include <wallet/wallet.h>
      15                 :            : 
      16                 :            : #include <univalue.h>
      17         [ +  - ]:          2 : 
      18         [ +  - ]:          2 : namespace wallet {
      19                 :          0 : RPCHelpMan getnewaddress()
      20                 :            : {
      21 [ #  # ][ #  # ]:          0 :     return RPCHelpMan{"getnewaddress",
                 [ #  # ]
      22         [ #  # ]:          0 :                 "\nReturns a new Bitcoin address for receiving payments.\n"
      23                 :            :                 "If 'label' is specified, it is added to the address book \n"
      24                 :            :                 "so payments received with the address will be associated with 'label'.\n",
      25         [ #  # ]:          0 :                 {
      26 [ #  # ][ #  # ]:          0 :                     {"label", RPCArg::Type::STR, RPCArg::Default{""}, "The label name for the address to be linked to. It can also be set to the empty string \"\" to represent the default label. The label does not need to exist, it will be created if there is no label by the given name."},
         [ #  # ][ #  # ]
      27 [ #  # ][ #  # ]:          2 :                     {"address_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -addresstype"}, "The address type to use. Options are \"legacy\", \"p2sh-segwit\", \"bech32\", and \"bech32m\"."},
         [ #  # ][ #  # ]
      28 [ +  - ][ +  - ]:          6 :                 },
         [ +  - ][ -  + ]
                 [ #  # ]
      29 [ +  - ][ +  - ]:          2 :                 RPCResult{
         [ +  - ][ #  # ]
                 [ #  # ]
      30 [ +  - ][ +  - ]:          2 :                     RPCResult::Type::STR, "address", "The new bitcoin address"
         [ +  - ][ #  # ]
                 [ #  # ]
      31                 :            :                 },
      32         [ #  # ]:          0 :                 RPCExamples{
      33 [ #  # ][ #  # ]:          0 :                     HelpExampleCli("getnewaddress", "")
                 [ #  # ]
      34 [ #  # ][ #  # ]:          0 :             + HelpExampleRpc("getnewaddress", "")
         [ #  # ][ #  # ]
      35                 :            :                 },
      36                 :          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
      37                 :            : {
      38                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
      39 [ #  # ][ #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
      40                 :            : 
      41 [ #  # ][ #  # ]:          0 :     LOCK(pwallet->cs_wallet);
      42                 :            : 
      43 [ #  # ][ #  # ]:          0 :     if (!pwallet->CanGetAddresses()) {
      44 [ #  # ][ #  # ]:          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Error: This wallet has no available keys");
                 [ #  # ]
      45                 :            :     }
      46                 :            : 
      47                 :            :     // Parse the label first so we don't generate a key if there's an error
      48 [ #  # ][ #  # ]:          0 :     const std::string label{LabelFromValue(request.params[0])};
      49                 :            : 
      50                 :          0 :     OutputType output_type = pwallet->m_default_address_type;
      51 [ #  # ][ #  # ]:          0 :     if (!request.params[1].isNull()) {
                 [ #  # ]
      52 [ #  # ][ #  # ]:          0 :         std::optional<OutputType> parsed = ParseOutputType(request.params[1].get_str());
                 [ #  # ]
      53 [ #  # ][ #  # ]:          0 :         if (!parsed) {
      54 [ #  # ][ #  # ]:          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Unknown address type '%s'", request.params[1].get_str()));
         [ #  # ][ #  # ]
                 [ #  # ]
      55 [ #  # ][ #  # ]:          0 :         } else if (parsed.value() == OutputType::BECH32M && pwallet->GetLegacyScriptPubKeyMan()) {
         [ #  # ][ #  # ]
      56 [ #  # ][ #  # ]:          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Legacy wallets cannot provide bech32m addresses");
                 [ #  # ]
      57                 :            :         }
      58         [ #  # ]:          0 :         output_type = parsed.value();
      59                 :          0 :     }
      60                 :            : 
      61 [ #  # ][ #  # ]:          0 :     auto op_dest = pwallet->GetNewDestination(output_type, label);
      62         [ #  # ]:          0 :     if (!op_dest) {
      63 [ #  # ][ #  # ]:          0 :         throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, util::ErrorString(op_dest).original);
                 [ #  # ]
      64                 :            :     }
      65                 :            : 
      66 [ #  # ][ #  # ]:          0 :     return EncodeDestination(*op_dest);
                 [ #  # ]
      67                 :          0 : },
      68                 :            :     };
      69                 :          0 : }
      70                 :            : 
      71                 :          0 : RPCHelpMan getrawchangeaddress()
      72                 :            : {
      73 [ #  # ][ #  # ]:          0 :     return RPCHelpMan{"getrawchangeaddress",
                 [ #  # ]
      74         [ #  # ]:          0 :                 "\nReturns a new Bitcoin address, for receiving change.\n"
      75                 :            :                 "This is for use with raw transactions, NOT normal use.\n",
      76         [ #  # ]:          0 :                 {
      77 [ #  # ][ #  # ]:          0 :                     {"address_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -changetype"}, "The address type to use. Options are \"legacy\", \"p2sh-segwit\", \"bech32\", and \"bech32m\"."},
         [ #  # ][ #  # ]
      78                 :            :                 },
      79 [ #  # ][ #  # ]:          0 :                 RPCResult{
      80 [ #  # ][ #  # ]:          0 :                     RPCResult::Type::STR, "address", "The address"
      81                 :            :                 },
      82         [ #  # ]:          0 :                 RPCExamples{
      83 [ #  # ][ #  # ]:          0 :                     HelpExampleCli("getrawchangeaddress", "")
                 [ #  # ]
      84 [ #  # ][ #  # ]:          0 :             + HelpExampleRpc("getrawchangeaddress", "")
         [ #  # ][ #  # ]
      85                 :            :                 },
      86                 :          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
      87                 :            : {
      88                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
      89 [ #  # ][ #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
      90                 :            : 
      91         [ #  # ]:          2 :     LOCK(pwallet->cs_wallet);
      92                 :            : 
      93 [ #  # ][ #  # ]:          0 :     if (!pwallet->CanGetAddresses(true)) {
      94 [ #  # ][ #  # ]:          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Error: This wallet has no available keys");
                 [ #  # ]
      95                 :            :     }
      96                 :            : 
      97         [ #  # ]:          0 :     OutputType output_type = pwallet->m_default_change_type.value_or(pwallet->m_default_address_type);
      98 [ #  # ][ #  # ]:          0 :     if (!request.params[0].isNull()) {
      99 [ #  # ][ #  # ]:          2 :         std::optional<OutputType> parsed = ParseOutputType(request.params[0].get_str());
                 [ #  # ]
     100         [ #  # ]:          0 :         if (!parsed) {
     101 [ #  # ][ #  # ]:          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Unknown address type '%s'", request.params[0].get_str()));
         [ #  # ][ #  # ]
                 [ #  # ]
     102 [ #  # ][ #  # ]:          0 :         } else if (parsed.value() == OutputType::BECH32M && pwallet->GetLegacyScriptPubKeyMan()) {
         [ #  # ][ #  # ]
     103 [ #  # ][ #  # ]:          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Legacy wallets cannot provide bech32m addresses");
                 [ #  # ]
     104                 :            :         }
     105         [ #  # ]:          0 :         output_type = parsed.value();
     106                 :          0 :     }
     107                 :            : 
     108         [ #  # ]:          0 :     auto op_dest = pwallet->GetNewChangeDestination(output_type);
     109         [ #  # ]:          0 :     if (!op_dest) {
     110 [ #  # ][ #  # ]:          0 :         throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, util::ErrorString(op_dest).original);
                 [ #  # ]
     111                 :            :     }
     112 [ #  # ][ #  # ]:          0 :     return EncodeDestination(*op_dest);
                 [ #  # ]
     113                 :          0 : },
     114                 :            :     };
     115                 :          0 : }
     116                 :            : 
     117                 :            : 
     118                 :          0 : RPCHelpMan setlabel()
     119                 :            : {
     120 [ #  # ][ #  # ]:          0 :     return RPCHelpMan{"setlabel",
                 [ #  # ]
     121         [ #  # ]:          0 :                 "\nSets the label associated with the given address.\n",
     122         [ #  # ]:          0 :                 {
     123 [ #  # ][ #  # ]:          0 :                     {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to be associated with a label."},
                 [ #  # ]
     124 [ #  # ][ #  # ]:          0 :                     {"label", RPCArg::Type::STR, RPCArg::Optional::NO, "The label to assign to the address."},
                 [ #  # ]
     125                 :            :                 },
     126 [ #  # ][ #  # ]:          0 :                 RPCResult{RPCResult::Type::NONE, "", ""},
         [ #  # ][ #  # ]
     127         [ #  # ]:          0 :                 RPCExamples{
     128 [ #  # ][ #  # ]:          0 :                     HelpExampleCli("setlabel", "\"" + EXAMPLE_ADDRESS[0] + "\" \"tabby\"")
         [ #  # ][ #  # ]
     129 [ #  # ][ #  # ]:          0 :             + HelpExampleRpc("setlabel", "\"" + EXAMPLE_ADDRESS[0] + "\", \"tabby\"")
         [ #  # ][ #  # ]
                 [ #  # ]
     130                 :            :                 },
     131                 :          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     132                 :            : {
     133                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
     134 [ #  # ][ #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     135                 :            : 
     136         [ #  # ]:          0 :     LOCK(pwallet->cs_wallet);
     137                 :            : 
     138 [ #  # ][ #  # ]:          0 :     CTxDestination dest = DecodeDestination(request.params[0].get_str());
                 [ #  # ]
     139 [ #  # ][ #  # ]:          0 :     if (!IsValidDestination(dest)) {
     140 [ #  # ][ #  # ]:          0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
         [ #  # ][ #  # ]
     141                 :            :     }
     142                 :            : 
     143 [ #  # ][ #  # ]:          0 :     const std::string label{LabelFromValue(request.params[1])};
     144                 :            : 
     145 [ #  # ][ #  # ]:          0 :     if (pwallet->IsMine(dest)) {
     146         [ #  # ]:          0 :         pwallet->SetAddressBook(dest, label, AddressPurpose::RECEIVE);
     147                 :          0 :     } else {
     148         [ #  # ]:          0 :         pwallet->SetAddressBook(dest, label, AddressPurpose::SEND);
     149                 :            :     }
     150                 :            : 
     151         [ #  # ]:          0 :     return UniValue::VNULL;
     152                 :          0 : },
     153                 :            :     };
     154                 :          0 : }
     155                 :            : 
     156                 :          0 : RPCHelpMan listaddressgroupings()
     157                 :            : {
     158 [ #  # ][ #  # ]:          0 :     return RPCHelpMan{"listaddressgroupings",
         [ #  # ][ #  # ]
                 [ #  # ]
     159         [ #  # ]:          0 :                 "\nLists groups of addresses which have had their common ownership\n"
     160                 :            :                 "made public by common use as inputs or as the resulting change\n"
     161                 :            :                 "in past transactions\n",
     162                 :          0 :                 {},
     163 [ +  - ][ #  # ]:          2 :                 RPCResult{
                 [ #  # ]
     164 [ +  - ][ #  # ]:          2 :                     RPCResult::Type::ARR, "", "",
                 [ #  # ]
     165 [ +  - ][ #  # ]:          2 :                     {
     166 [ +  - ][ #  # ]:          2 :                         {RPCResult::Type::ARR, "", "",
         [ #  # ][ #  # ]
     167 [ +  - ][ #  # ]:          2 :                         {
     168 [ +  - ][ #  # ]:          2 :                             {RPCResult::Type::ARR_FIXED, "", "",
         [ #  # ][ #  # ]
     169 [ +  - ][ #  # ]:          2 :                             {
     170 [ +  - ][ #  # ]:          2 :                                 {RPCResult::Type::STR, "address", "The bitcoin address"},
         [ #  # ][ #  # ]
     171 [ #  # ][ #  # ]:          0 :                                 {RPCResult::Type::STR_AMOUNT, "amount", "The amount in " + CURRENCY_UNIT},
                 [ #  # ]
     172 [ #  # ][ #  # ]:          0 :                                 {RPCResult::Type::STR, "label", /*optional=*/true, "The label"},
                 [ #  # ]
     173                 :            :                             }},
     174                 :            :                         }},
     175                 :            :                     }
     176                 :            :                 },
     177         [ #  # ]:          0 :                 RPCExamples{
     178 [ #  # ][ #  # ]:          0 :                     HelpExampleCli("listaddressgroupings", "")
                 [ #  # ]
     179 [ #  # ][ #  # ]:          0 :             + HelpExampleRpc("listaddressgroupings", "")
         [ #  # ][ #  # ]
     180                 :            :                 },
     181                 :          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     182                 :            : {
     183                 :          0 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
     184 [ #  # ][ #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     185                 :            : 
     186                 :            :     // Make sure the results are valid at least up to the most recent block
     187                 :            :     // the user could have gotten from another RPC command prior to now
     188         [ #  # ]:          0 :     pwallet->BlockUntilSyncedToCurrentChain();
     189                 :            : 
     190         [ #  # ]:          0 :     LOCK(pwallet->cs_wallet);
     191                 :            : 
     192         [ #  # ]:          0 :     UniValue jsonGroupings(UniValue::VARR);
     193         [ #  # ]:          0 :     std::map<CTxDestination, CAmount> balances = GetAddressBalances(*pwallet);
     194 [ #  # ][ #  # ]:          0 :     for (const std::set<CTxDestination>& grouping : GetAddressGroupings(*pwallet)) {
     195         [ #  # ]:          0 :         UniValue jsonGrouping(UniValue::VARR);
     196         [ #  # ]:          0 :         for (const CTxDestination& address : grouping)
     197                 :            :         {
     198         [ #  # ]:          0 :             UniValue addressInfo(UniValue::VARR);
     199 [ #  # ][ #  # ]:          0 :             addressInfo.push_back(EncodeDestination(address));
                 [ #  # ]
     200 [ #  # ][ #  # ]:          0 :             addressInfo.push_back(ValueFromAmount(balances[address]));
                 [ #  # ]
     201                 :            :             {
     202         [ #  # ]:          0 :                 const auto* address_book_entry = pwallet->FindAddressBookEntry(address);
     203         [ #  # ]:          0 :                 if (address_book_entry) {
     204 [ #  # ][ #  # ]:          0 :                     addressInfo.push_back(address_book_entry->GetLabel());
                 [ #  # ]
     205                 :          0 :                 }
     206                 :            :             }
     207 [ #  # ][ #  # ]:          0 :             jsonGrouping.push_back(addressInfo);
     208                 :          0 :         }
     209 [ #  # ][ #  # ]:          0 :         jsonGroupings.push_back(jsonGrouping);
     210                 :          0 :     }
     211                 :          0 :     return jsonGroupings;
     212         [ #  # ]:          0 : },
     213                 :            :     };
     214                 :          0 : }
     215                 :            : 
     216                 :          0 : RPCHelpMan addmultisigaddress()
     217                 :            : {
     218 [ #  # ][ #  # ]:          0 :     return RPCHelpMan{"addmultisigaddress",
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     219         [ #  # ]:          0 :                 "\nAdd an nrequired-to-sign multisignature address to the wallet. Requires a new wallet backup.\n"
     220                 :            :                 "Each key is a Bitcoin address or hex-encoded public key.\n"
     221                 :            :                 "This functionality is only intended for use with non-watchonly addresses.\n"
     222                 :            :                 "See `importaddress` for watchonly p2sh address support.\n"
     223                 :            :                 "If 'label' is specified, assign address to that label.\n"
     224                 :            :                 "Note: This command is only compatible with legacy wallets.\n",
     225         [ #  # ]:          0 :                 {
     226 [ #  # ][ #  # ]:          0 :                     {"nrequired", RPCArg::Type::NUM, RPCArg::Optional::NO, "The number of required signatures out of the n keys or addresses."},
                 [ #  # ]
     227 [ #  # ][ #  # ]:          0 :                     {"keys", RPCArg::Type::ARR, RPCArg::Optional::NO, "The bitcoin addresses or hex-encoded public keys",
                 [ #  # ]
     228         [ #  # ]:          0 :                         {
     229 [ #  # ][ #  # ]:          0 :                             {"key", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "bitcoin address or hex-encoded public key"},
                 [ #  # ]
     230                 :            :                         },
     231                 :            :                         },
     232 [ #  # ][ #  # ]:          0 :                     {"label", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "A label to assign the addresses to."},
                 [ #  # ]
     233 [ #  # ][ #  # ]:          0 :                     {"address_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -addresstype"}, "The address type to use. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."},
         [ #  # ][ #  # ]
     234                 :            :                 },
     235 [ #  # ][ #  # ]:          0 :                 RPCResult{
     236 [ #  # ][ #  # ]:          0 :                     RPCResult::Type::OBJ, "", "",
     237         [ #  # ]:          0 :                     {
     238 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR, "address", "The value of the new multisig address"},
                 [ #  # ]
     239 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR_HEX, "redeemScript", "The string value of the hex-encoded redemption script"},
                 [ #  # ]
     240 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR, "descriptor", "The descriptor for this multisig"},
                 [ #  # ]
     241 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Any warnings resulting from the creation of this multisig",
                 [ #  # ]
     242         [ #  # ]:          0 :                         {
     243 [ #  # ][ #  # ]:          0 :                             {RPCResult::Type::STR, "", ""},
                 [ #  # ]
     244                 :            :                         }},
     245                 :            :                     }
     246                 :            :                 },
     247         [ #  # ]:          0 :                 RPCExamples{
     248                 :            :             "\nAdd a multisig address from 2 addresses\n"
     249 [ #  # ][ #  # ]:          0 :             + HelpExampleCli("addmultisigaddress", "2 \"[\\\"" + EXAMPLE_ADDRESS[0] + "\\\",\\\"" + EXAMPLE_ADDRESS[1] + "\\\"]\"") +
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     250                 :            :             "\nAs a JSON-RPC call\n"
     251 [ #  # ][ #  # ]:          0 :             + HelpExampleRpc("addmultisigaddress", "2, \"[\\\"" + EXAMPLE_ADDRESS[0] + "\\\",\\\"" + EXAMPLE_ADDRESS[1] + "\\\"]\"")
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     252                 :            :                 },
     253                 :          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     254                 :            : {
     255                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
     256 [ #  # ][ #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     257                 :            : 
     258         [ #  # ]:          0 :     LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet);
     259                 :            : 
     260 [ #  # ][ #  # ]:          0 :     LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
     261                 :            : 
     262 [ #  # ][ #  # ]:          0 :     const std::string label{LabelFromValue(request.params[2])};
     263                 :            : 
     264 [ +  - ][ #  # ]:          2 :     int required = request.params[0].getInt<int>();
                 [ #  # ]
     265                 :            : 
     266                 :            :     // Get the public keys
     267 [ #  # ][ #  # ]:          0 :     const UniValue& keys_or_addrs = request.params[1].get_array();
     268                 :          0 :     std::vector<CPubKey> pubkeys;
     269 [ #  # ][ #  # ]:          0 :     for (unsigned int i = 0; i < keys_or_addrs.size(); ++i) {
     270 [ #  # ][ #  # ]:          0 :         if (IsHex(keys_or_addrs[i].get_str()) && (keys_or_addrs[i].get_str().length() == 66 || keys_or_addrs[i].get_str().length() == 130)) {
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     271 [ #  # ][ #  # ]:          0 :             pubkeys.push_back(HexToPubKey(keys_or_addrs[i].get_str()));
         [ #  # ][ #  # ]
     272                 :          0 :         } else {
     273 [ #  # ][ #  # ]:          0 :             pubkeys.push_back(AddrToPubKey(spk_man, keys_or_addrs[i].get_str()));
         [ #  # ][ #  # ]
     274                 :            :         }
     275                 :          0 :     }
     276                 :            : 
     277                 :          0 :     OutputType output_type = pwallet->m_default_address_type;
     278 [ #  # ][ #  # ]:          0 :     if (!request.params[3].isNull()) {
     279 [ #  # ][ #  # ]:          0 :         std::optional<OutputType> parsed = ParseOutputType(request.params[3].get_str());
                 [ #  # ]
     280         [ #  # ]:          0 :         if (!parsed) {
     281 [ #  # ][ #  # ]:          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Unknown address type '%s'", request.params[3].get_str()));
         [ #  # ][ #  # ]
                 [ #  # ]
     282 [ #  # ][ #  # ]:          0 :         } else if (parsed.value() == OutputType::BECH32M) {
     283 [ #  # ][ #  # ]:          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Bech32m multisig addresses cannot be created with legacy wallets");
                 [ #  # ]
     284                 :            :         }
     285         [ #  # ]:          0 :         output_type = parsed.value();
     286                 :          0 :     }
     287                 :            : 
     288                 :            :     // Construct using pay-to-script-hash:
     289         [ #  # ]:          0 :     CScript inner;
     290         [ #  # ]:          0 :     CTxDestination dest = AddAndGetMultisigDestination(required, pubkeys, output_type, spk_man, inner);
     291         [ #  # ]:          0 :     pwallet->SetAddressBook(dest, label, AddressPurpose::SEND);
     292                 :            : 
     293                 :            :     // Make the descriptor
     294 [ #  # ][ #  # ]:          0 :     std::unique_ptr<Descriptor> descriptor = InferDescriptor(GetScriptForDestination(dest), spk_man);
     295                 :            : 
     296         [ #  # ]:          0 :     UniValue result(UniValue::VOBJ);
     297 [ #  # ][ #  # ]:          0 :     result.pushKV("address", EncodeDestination(dest));
         [ #  # ][ #  # ]
     298 [ #  # ][ #  # ]:          0 :     result.pushKV("redeemScript", HexStr(inner));
         [ #  # ][ #  # ]
                 [ #  # ]
     299 [ #  # ][ #  # ]:          0 :     result.pushKV("descriptor", descriptor->ToString());
         [ #  # ][ #  # ]
     300                 :            : 
     301         [ #  # ]:          0 :     UniValue warnings(UniValue::VARR);
     302 [ #  # ][ #  # ]:          0 :     if (descriptor->GetOutputType() != output_type) {
                 [ #  # ]
     303                 :            :         // Only warns if the user has explicitly chosen an address type we cannot generate
     304 [ #  # ][ #  # ]:          0 :         warnings.push_back("Unable to make chosen address type, please ensure no uncompressed public keys are present.");
     305                 :          0 :     }
     306         [ #  # ]:          0 :     PushWarnings(warnings, result);
     307                 :            : 
     308                 :          0 :     return result;
     309         [ #  # ]:          0 : },
     310                 :            :     };
     311                 :          0 : }
     312                 :            : 
     313                 :          0 : RPCHelpMan keypoolrefill()
     314                 :            : {
     315 [ #  # ][ #  # ]:          0 :     return RPCHelpMan{"keypoolrefill",
                 [ #  # ]
     316         [ #  # ]:          0 :                 "\nFills the keypool."+
     317                 :            :         HELP_REQUIRING_PASSPHRASE,
     318         [ #  # ]:          0 :                 {
     319 [ #  # ][ #  # ]:          0 :                     {"newsize", RPCArg::Type::NUM, RPCArg::DefaultHint{strprintf("%u, or as set by -keypool", DEFAULT_KEYPOOL_SIZE)}, "The new keypool size"},
         [ #  # ][ #  # ]
     320                 :            :                 },
     321 [ #  # ][ #  # ]:          0 :                 RPCResult{RPCResult::Type::NONE, "", ""},
         [ #  # ][ #  # ]
     322         [ #  # ]:          0 :                 RPCExamples{
     323 [ #  # ][ #  # ]:          0 :                     HelpExampleCli("keypoolrefill", "")
                 [ #  # ]
     324 [ #  # ][ #  # ]:          0 :             + HelpExampleRpc("keypoolrefill", "")
         [ #  # ][ #  # ]
     325                 :            :                 },
     326                 :          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     327                 :            : {
     328                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
     329 [ #  # ][ #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     330                 :            : 
     331 [ #  # ][ #  # ]:          0 :     if (pwallet->IsLegacy() && pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
         [ #  # ][ #  # ]
     332 [ #  # ][ #  # ]:          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Error: Private keys are disabled for this wallet");
                 [ #  # ]
     333                 :            :     }
     334                 :            : 
     335         [ #  # ]:          0 :     LOCK(pwallet->cs_wallet);
     336                 :            : 
     337                 :            :     // 0 is interpreted by TopUpKeyPool() as the default keypool size given by -keypool
     338                 :          0 :     unsigned int kpSize = 0;
     339 [ #  # ][ #  # ]:          0 :     if (!request.params[0].isNull()) {
     340 [ #  # ][ #  # ]:          0 :         if (request.params[0].getInt<int>() < 0)
                 [ #  # ]
     341 [ #  # ][ #  # ]:          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected valid size.");
                 [ #  # ]
     342 [ #  # ][ #  # ]:          0 :         kpSize = (unsigned int)request.params[0].getInt<int>();
     343                 :          0 :     }
     344                 :            : 
     345         [ #  # ]:          0 :     EnsureWalletIsUnlocked(*pwallet);
     346         [ #  # ]:          0 :     pwallet->TopUpKeyPool(kpSize);
     347                 :            : 
     348 [ #  # ][ #  # ]:          0 :     if (pwallet->GetKeyPoolSize() < kpSize) {
     349 [ #  # ][ #  # ]:          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Error refreshing keypool.");
                 [ #  # ]
     350                 :            :     }
     351                 :            : 
     352         [ #  # ]:          0 :     return UniValue::VNULL;
     353                 :          0 : },
     354                 :            :     };
     355                 :          0 : }
     356                 :            : 
     357                 :          0 : RPCHelpMan newkeypool()
     358                 :            : {
     359 [ #  # ][ #  # ]:          0 :     return RPCHelpMan{"newkeypool",
     360                 :            :                 "\nEntirely clears and refills the keypool.\n"
     361                 :            :                 "WARNING: On non-HD wallets, this will require a new backup immediately, to include the new keys.\n"
     362                 :            :                 "When restoring a backup of an HD wallet created before the newkeypool command is run, funds received to\n"
     363                 :            :                 "new addresses may not appear automatically. They have not been lost, but the wallet may not find them.\n"
     364                 :            :                 "This can be fixed by running the newkeypool command on the backup and then rescanning, so the wallet\n"
     365         [ #  # ]:          0 :                 "re-generates the required keys." +
     366                 :            :             HELP_REQUIRING_PASSPHRASE,
     367                 :          0 :                 {},
     368 [ #  # ][ #  # ]:          0 :                 RPCResult{RPCResult::Type::NONE, "", ""},
         [ #  # ][ #  # ]
     369         [ #  # ]:          0 :                 RPCExamples{
     370 [ #  # ][ #  # ]:          0 :             HelpExampleCli("newkeypool", "")
                 [ #  # ]
     371 [ #  # ][ #  # ]:          0 :             + HelpExampleRpc("newkeypool", "")
         [ #  # ][ #  # ]
     372                 :            :                 },
     373                 :          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     374                 :            : {
     375                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
     376 [ #  # ][ #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     377                 :            : 
     378         [ #  # ]:          0 :     LOCK(pwallet->cs_wallet);
     379                 :            : 
     380         [ #  # ]:          0 :     LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet, true);
     381         [ #  # ]:          0 :     spk_man.NewKeyPool();
     382                 :            : 
     383         [ #  # ]:          0 :     return UniValue::VNULL;
     384                 :          0 : },
     385                 :            :     };
     386                 :          0 : }
     387                 :            : 
     388                 :            : 
     389                 :            : class DescribeWalletAddressVisitor
     390                 :            : {
     391                 :            : public:
     392                 :            :     const SigningProvider * const provider;
     393                 :            : 
     394                 :          0 :     void ProcessSubScript(const CScript& subscript, UniValue& obj) const
     395                 :            :     {
     396                 :            :         // Always present: script type and redeemscript
     397                 :          0 :         std::vector<std::vector<unsigned char>> solutions_data;
     398         [ #  # ]:          0 :         TxoutType which_type = Solver(subscript, solutions_data);
     399 [ #  # ][ #  # ]:          0 :         obj.pushKV("script", GetTxnOutputType(which_type));
         [ #  # ][ #  # ]
     400 [ #  # ][ #  # ]:          0 :         obj.pushKV("hex", HexStr(subscript));
         [ #  # ][ #  # ]
                 [ #  # ]
     401                 :            : 
     402         [ #  # ]:          0 :         CTxDestination embedded;
     403 [ #  # ][ #  # ]:          0 :         if (ExtractDestination(subscript, embedded)) {
     404                 :            :             // Only when the script corresponds to an address.
     405         [ #  # ]:          0 :             UniValue subobj(UniValue::VOBJ);
     406         [ #  # ]:          0 :             UniValue detail = DescribeAddress(embedded);
     407 [ #  # ][ #  # ]:          0 :             subobj.pushKVs(detail);
     408         [ #  # ]:          0 :             UniValue wallet_detail = std::visit(*this, embedded);
     409 [ #  # ][ #  # ]:          0 :             subobj.pushKVs(wallet_detail);
     410 [ #  # ][ #  # ]:          0 :             subobj.pushKV("address", EncodeDestination(embedded));
         [ #  # ][ #  # ]
     411 [ #  # ][ #  # ]:          0 :             subobj.pushKV("scriptPubKey", HexStr(subscript));
         [ #  # ][ #  # ]
                 [ #  # ]
     412                 :            :             // Always report the pubkey at the top level, so that `getnewaddress()['pubkey']` always works.
     413 [ #  # ][ #  # ]:          0 :             if (subobj.exists("pubkey")) obj.pushKV("pubkey", subobj["pubkey"]);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     414 [ #  # ][ #  # ]:          0 :             obj.pushKV("embedded", std::move(subobj));
     415         [ #  # ]:          0 :         } else if (which_type == TxoutType::MULTISIG) {
     416                 :            :             // Also report some information on multisig scripts (which do not have a corresponding address).
     417 [ #  # ][ #  # ]:          0 :             obj.pushKV("sigsrequired", solutions_data[0][0]);
                 [ #  # ]
     418         [ #  # ]:          0 :             UniValue pubkeys(UniValue::VARR);
     419         [ #  # ]:          0 :             for (size_t i = 1; i < solutions_data.size() - 1; ++i) {
     420         [ #  # ]:          0 :                 CPubKey key(solutions_data[i].begin(), solutions_data[i].end());
     421 [ #  # ][ #  # ]:          0 :                 pubkeys.push_back(HexStr(key));
         [ #  # ][ #  # ]
     422                 :          0 :             }
     423 [ #  # ][ #  # ]:          0 :             obj.pushKV("pubkeys", std::move(pubkeys));
     424                 :          0 :         }
     425                 :          0 :     }
     426                 :            : 
     427                 :          0 :     explicit DescribeWalletAddressVisitor(const SigningProvider* _provider) : provider(_provider) {}
     428                 :            : 
     429         [ #  # ]:          0 :     UniValue operator()(const CNoDestination& dest) const { return UniValue(UniValue::VOBJ); }
     430         [ #  # ]:          0 :     UniValue operator()(const PubKeyDestination& dest) const { return UniValue(UniValue::VOBJ); }
     431                 :            : 
     432                 :          0 :     UniValue operator()(const PKHash& pkhash) const
     433                 :            :     {
     434                 :          0 :         CKeyID keyID{ToKeyID(pkhash)};
     435         [ #  # ]:          0 :         UniValue obj(UniValue::VOBJ);
     436         [ #  # ]:          0 :         CPubKey vchPubKey;
     437 [ #  # ][ #  # ]:          0 :         if (provider && provider->GetPubKey(keyID, vchPubKey)) {
                 [ #  # ]
     438 [ #  # ][ #  # ]:          0 :             obj.pushKV("pubkey", HexStr(vchPubKey));
         [ #  # ][ #  # ]
                 [ #  # ]
     439 [ #  # ][ #  # ]:          0 :             obj.pushKV("iscompressed", vchPubKey.IsCompressed());
         [ #  # ][ #  # ]
     440                 :          0 :         }
     441                 :          0 :         return obj;
     442         [ #  # ]:          0 :     }
     443                 :            : 
     444                 :          0 :     UniValue operator()(const ScriptHash& scripthash) const
     445                 :            :     {
     446         [ #  # ]:          0 :         UniValue obj(UniValue::VOBJ);
     447         [ #  # ]:          0 :         CScript subscript;
     448 [ #  # ][ #  # ]:          0 :         if (provider && provider->GetCScript(ToScriptID(scripthash), subscript)) {
         [ #  # ][ #  # ]
     449         [ #  # ]:          0 :             ProcessSubScript(subscript, obj);
     450                 :          0 :         }
     451                 :          0 :         return obj;
     452         [ #  # ]:          0 :     }
     453                 :            : 
     454                 :          0 :     UniValue operator()(const WitnessV0KeyHash& id) const
     455                 :            :     {
     456         [ #  # ]:          0 :         UniValue obj(UniValue::VOBJ);
     457         [ #  # ]:          0 :         CPubKey pubkey;
     458 [ #  # ][ #  # ]:          0 :         if (provider && provider->GetPubKey(ToKeyID(id), pubkey)) {
         [ #  # ][ #  # ]
     459 [ #  # ][ #  # ]:          0 :             obj.pushKV("pubkey", HexStr(pubkey));
         [ #  # ][ #  # ]
                 [ #  # ]
     460                 :          0 :         }
     461                 :          0 :         return obj;
     462         [ #  # ]:          0 :     }
     463                 :            : 
     464                 :          0 :     UniValue operator()(const WitnessV0ScriptHash& id) const
     465                 :            :     {
     466         [ #  # ]:          0 :         UniValue obj(UniValue::VOBJ);
     467         [ #  # ]:          0 :         CScript subscript;
     468         [ #  # ]:          0 :         CRIPEMD160 hasher;
     469         [ #  # ]:          0 :         uint160 hash;
     470 [ #  # ][ #  # ]:          0 :         hasher.Write(id.begin(), 32).Finalize(hash.begin());
         [ #  # ][ #  # ]
     471 [ #  # ][ #  # ]:          0 :         if (provider && provider->GetCScript(CScriptID(hash), subscript)) {
         [ #  # ][ #  # ]
     472         [ #  # ]:          0 :             ProcessSubScript(subscript, obj);
     473                 :          0 :         }
     474                 :          0 :         return obj;
     475         [ #  # ]:          0 :     }
     476                 :            : 
     477         [ #  # ]:          0 :     UniValue operator()(const WitnessV1Taproot& id) const { return UniValue(UniValue::VOBJ); }
     478         [ #  # ]:          0 :     UniValue operator()(const WitnessUnknown& id) const { return UniValue(UniValue::VOBJ); }
     479                 :            : };
     480                 :            : 
     481                 :          0 : static UniValue DescribeWalletAddress(const CWallet& wallet, const CTxDestination& dest)
     482                 :            : {
     483         [ #  # ]:          0 :     UniValue ret(UniValue::VOBJ);
     484         [ #  # ]:          0 :     UniValue detail = DescribeAddress(dest);
     485         [ #  # ]:          0 :     CScript script = GetScriptForDestination(dest);
     486                 :          0 :     std::unique_ptr<SigningProvider> provider = nullptr;
     487         [ #  # ]:          0 :     provider = wallet.GetSolvingProvider(script);
     488 [ #  # ][ #  # ]:          0 :     ret.pushKVs(detail);
     489 [ #  # ][ #  # ]:          0 :     ret.pushKVs(std::visit(DescribeWalletAddressVisitor(provider.get()), dest));
                 [ #  # ]
     490                 :          0 :     return ret;
     491         [ #  # ]:          0 : }
     492                 :            : 
     493                 :          0 : RPCHelpMan getaddressinfo()
     494                 :            : {
     495 [ #  # ][ #  # ]:          0 :     return RPCHelpMan{"getaddressinfo",
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     496         [ #  # ]:          0 :                 "\nReturn information about the given bitcoin address.\n"
     497                 :            :                 "Some of the information will only be present if the address is in the active wallet.\n",
     498         [ #  # ]:          0 :                 {
     499 [ #  # ][ #  # ]:          0 :                     {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address for which to get information."},
                 [ #  # ]
     500                 :            :                 },
     501 [ #  # ][ #  # ]:          0 :                 RPCResult{
     502 [ #  # ][ #  # ]:          0 :                     RPCResult::Type::OBJ, "", "",
     503         [ #  # ]:          0 :                     {
     504 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR, "address", "The bitcoin address validated."},
                 [ #  # ]
     505 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR_HEX, "scriptPubKey", "The hex-encoded scriptPubKey generated by the address."},
                 [ #  # ]
     506 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::BOOL, "ismine", "If the address is yours."},
                 [ #  # ]
     507 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::BOOL, "iswatchonly", "If the address is watchonly."},
                 [ #  # ]
     508 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::BOOL, "solvable", "If we know how to spend coins sent to this address, ignoring the possible lack of private keys."},
                 [ #  # ]
     509 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR, "desc", /*optional=*/true, "A descriptor for spending coins sent to this address (only when solvable)."},
                 [ #  # ]
     510 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR, "parent_desc", /*optional=*/true, "The descriptor used to derive this address if this is a descriptor wallet"},
                 [ #  # ]
     511 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::BOOL, "isscript", "If the key is a script."},
                 [ #  # ]
     512 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::BOOL, "ischange", "If the address was used for change output."},
                 [ #  # ]
     513 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::BOOL, "iswitness", "If the address is a witness address."},
                 [ #  # ]
     514 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::NUM, "witness_version", /*optional=*/true, "The version number of the witness program."},
                 [ #  # ]
     515 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR_HEX, "witness_program", /*optional=*/true, "The hex value of the witness program."},
                 [ #  # ]
     516 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR, "script", /*optional=*/true, "The output script type. Only if isscript is true and the redeemscript is known. Possible\n"
                 [ #  # ]
     517                 :            :                                                                      "types: nonstandard, pubkey, pubkeyhash, scripthash, multisig, nulldata, witness_v0_keyhash,\n"
     518                 :            :                             "witness_v0_scripthash, witness_unknown."},
     519 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR_HEX, "hex", /*optional=*/true, "The redeemscript for the p2sh address."},
                 [ #  # ]
     520 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::ARR, "pubkeys", /*optional=*/true, "Array of pubkeys associated with the known redeemscript (only if script is multisig).",
                 [ #  # ]
     521         [ #  # ]:          0 :                         {
     522 [ #  # ][ #  # ]:          0 :                             {RPCResult::Type::STR, "pubkey", ""},
                 [ #  # ]
     523                 :            :                         }},
     524 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::NUM, "sigsrequired", /*optional=*/true, "The number of signatures required to spend multisig output (only if script is multisig)."},
                 [ #  # ]
     525 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR_HEX, "pubkey", /*optional=*/true, "The hex value of the raw public key for single-key addresses (possibly embedded in P2SH or P2WSH)."},
                 [ #  # ]
     526 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::OBJ, "embedded", /*optional=*/true, "Information about the address embedded in P2SH or P2WSH, if relevant and known.",
                 [ #  # ]
     527         [ #  # ]:          0 :                         {
     528 [ #  # ][ #  # ]:          0 :                             {RPCResult::Type::ELISION, "", "Includes all getaddressinfo output fields for the embedded address, excluding metadata (timestamp, hdkeypath, hdseedid)\n"
                 [ #  # ]
     529                 :            :                             "and relation to the wallet (ismine, iswatchonly)."},
     530                 :            :                         }},
     531 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::BOOL, "iscompressed", /*optional=*/true, "If the pubkey is compressed."},
                 [ #  # ]
     532 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::NUM_TIME, "timestamp", /*optional=*/true, "The creation time of the key, if available, expressed in " + UNIX_EPOCH_TIME + "."},
         [ #  # ][ #  # ]
     533 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR, "hdkeypath", /*optional=*/true, "The HD keypath, if the key is HD and available."},
                 [ #  # ]
     534 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR_HEX, "hdseedid", /*optional=*/true, "The Hash160 of the HD seed."},
                 [ #  # ]
     535 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR_HEX, "hdmasterfingerprint", /*optional=*/true, "The fingerprint of the master key."},
                 [ #  # ]
     536 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::ARR, "labels", "Array of labels associated with the address. Currently limited to one label but returned\n"
                 [ #  # ]
     537                 :            :                             "as an array to keep the API stable if multiple labels are enabled in the future.",
     538         [ #  # ]:          0 :                         {
     539 [ #  # ][ #  # ]:          0 :                             {RPCResult::Type::STR, "label name", "Label name (defaults to \"\")."},
                 [ #  # ]
     540                 :            :                         }},
     541                 :            :                     }
     542                 :            :                 },
     543         [ #  # ]:          0 :                 RPCExamples{
     544 [ #  # ][ #  # ]:          0 :                     HelpExampleCli("getaddressinfo", "\"" + EXAMPLE_ADDRESS[0] + "\"") +
         [ #  # ][ #  # ]
                 [ #  # ]
     545 [ #  # ][ #  # ]:          0 :                     HelpExampleRpc("getaddressinfo", "\"" + EXAMPLE_ADDRESS[0] + "\"")
         [ #  # ][ #  # ]
     546                 :            :                 },
     547                 :          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     548                 :            : {
     549                 :          0 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
     550 [ #  # ][ #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     551                 :            : 
     552         [ #  # ]:          0 :     LOCK(pwallet->cs_wallet);
     553                 :            : 
     554                 :          0 :     std::string error_msg;
     555 [ #  # ][ #  # ]:          0 :     CTxDestination dest = DecodeDestination(request.params[0].get_str(), error_msg);
                 [ #  # ]
     556                 :            : 
     557                 :            :     // Make sure the destination is valid
     558 [ #  # ][ #  # ]:          0 :     if (!IsValidDestination(dest)) {
     559                 :            :         // Set generic error message in case 'DecodeDestination' didn't set it
     560 [ #  # ][ #  # ]:          0 :         if (error_msg.empty()) error_msg = "Invalid address";
     561                 :            : 
     562 [ #  # ][ #  # ]:          0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error_msg);
     563                 :            :     }
     564                 :            : 
     565         [ #  # ]:          0 :     UniValue ret(UniValue::VOBJ);
     566                 :            : 
     567         [ #  # ]:          0 :     std::string currentAddress = EncodeDestination(dest);
     568 [ #  # ][ #  # ]:          0 :     ret.pushKV("address", currentAddress);
                 [ #  # ]
     569                 :            : 
     570         [ #  # ]:          0 :     CScript scriptPubKey = GetScriptForDestination(dest);
     571 [ #  # ][ #  # ]:          0 :     ret.pushKV("scriptPubKey", HexStr(scriptPubKey));
         [ #  # ][ #  # ]
                 [ #  # ]
     572                 :            : 
     573         [ #  # ]:          0 :     std::unique_ptr<SigningProvider> provider = pwallet->GetSolvingProvider(scriptPubKey);
     574                 :            : 
     575         [ #  # ]:          0 :     isminetype mine = pwallet->IsMine(dest);
     576 [ #  # ][ #  # ]:          0 :     ret.pushKV("ismine", bool(mine & ISMINE_SPENDABLE));
                 [ #  # ]
     577                 :            : 
     578         [ #  # ]:          0 :     if (provider) {
     579         [ #  # ]:          0 :         auto inferred = InferDescriptor(scriptPubKey, *provider);
     580         [ #  # ]:          0 :         bool solvable = inferred->IsSolvable();
     581 [ #  # ][ #  # ]:          0 :         ret.pushKV("solvable", solvable);
                 [ #  # ]
     582         [ #  # ]:          0 :         if (solvable) {
     583 [ #  # ][ #  # ]:          0 :             ret.pushKV("desc", inferred->ToString());
         [ #  # ][ #  # ]
     584                 :          0 :         }
     585                 :          0 :     } else {
     586 [ #  # ][ #  # ]:          0 :         ret.pushKV("solvable", false);
                 [ #  # ]
     587                 :            :     }
     588                 :            : 
     589         [ #  # ]:          0 :     const auto& spk_mans = pwallet->GetScriptPubKeyMans(scriptPubKey);
     590                 :            :     // In most cases there is only one matching ScriptPubKey manager and we can't resolve ambiguity in a better way
     591                 :          0 :     ScriptPubKeyMan* spk_man{nullptr};
     592         [ #  # ]:          0 :     if (spk_mans.size()) spk_man = *spk_mans.begin();
     593                 :            : 
     594         [ #  # ]:          0 :     DescriptorScriptPubKeyMan* desc_spk_man = dynamic_cast<DescriptorScriptPubKeyMan*>(spk_man);
     595         [ #  # ]:          0 :     if (desc_spk_man) {
     596                 :          0 :         std::string desc_str;
     597 [ #  # ][ #  # ]:          0 :         if (desc_spk_man->GetDescriptorString(desc_str, /*priv=*/false)) {
     598 [ #  # ][ #  # ]:          0 :             ret.pushKV("parent_desc", desc_str);
                 [ #  # ]
     599                 :          0 :         }
     600                 :          0 :     }
     601                 :            : 
     602 [ #  # ][ #  # ]:          0 :     ret.pushKV("iswatchonly", bool(mine & ISMINE_WATCH_ONLY));
                 [ #  # ]
     603                 :            : 
     604         [ #  # ]:          0 :     UniValue detail = DescribeWalletAddress(*pwallet, dest);
     605 [ #  # ][ #  # ]:          0 :     ret.pushKVs(detail);
     606                 :            : 
     607 [ #  # ][ #  # ]:          0 :     ret.pushKV("ischange", ScriptIsChange(*pwallet, scriptPubKey));
         [ #  # ][ #  # ]
     608                 :            : 
     609         [ #  # ]:          0 :     if (spk_man) {
     610 [ #  # ][ #  # ]:          0 :         if (const std::unique_ptr<CKeyMetadata> meta = spk_man->GetMetadata(dest)) {
     611 [ #  # ][ #  # ]:          0 :             ret.pushKV("timestamp", meta->nCreateTime);
                 [ #  # ]
     612         [ #  # ]:          0 :             if (meta->has_key_origin) {
     613                 :            :                 // In legacy wallets hdkeypath has always used an apostrophe for
     614                 :            :                 // hardened derivation. Perhaps some external tool depends on that.
     615 [ #  # ][ #  # ]:          0 :                 ret.pushKV("hdkeypath", WriteHDKeypath(meta->key_origin.path, /*apostrophe=*/!desc_spk_man));
         [ #  # ][ #  # ]
     616 [ #  # ][ #  # ]:          0 :                 ret.pushKV("hdseedid", meta->hd_seed_id.GetHex());
         [ #  # ][ #  # ]
     617 [ #  # ][ #  # ]:          0 :                 ret.pushKV("hdmasterfingerprint", HexStr(meta->key_origin.fingerprint));
         [ #  # ][ #  # ]
     618                 :          0 :             }
     619                 :          0 :         }
     620                 :          0 :     }
     621                 :            : 
     622                 :            :     // Return a `labels` array containing the label associated with the address,
     623                 :            :     // equivalent to the `label` field above. Currently only one label can be
     624                 :            :     // associated with an address, but we return an array so the API remains
     625                 :            :     // stable if we allow multiple labels to be associated with an address in
     626                 :            :     // the future.
     627         [ #  # ]:          0 :     UniValue labels(UniValue::VARR);
     628         [ #  # ]:          0 :     const auto* address_book_entry = pwallet->FindAddressBookEntry(dest);
     629         [ #  # ]:          0 :     if (address_book_entry) {
     630 [ #  # ][ #  # ]:          0 :         labels.push_back(address_book_entry->GetLabel());
                 [ #  # ]
     631                 :          0 :     }
     632 [ #  # ][ #  # ]:          0 :     ret.pushKV("labels", std::move(labels));
     633                 :            : 
     634                 :          0 :     return ret;
     635         [ #  # ]:          0 : },
     636                 :            :     };
     637                 :          0 : }
     638                 :            : 
     639                 :          0 : RPCHelpMan getaddressesbylabel()
     640                 :            : {
     641 [ #  # ][ #  # ]:          0 :     return RPCHelpMan{"getaddressesbylabel",
         [ #  # ][ #  # ]
                 [ #  # ]
     642         [ #  # ]:          0 :                 "\nReturns the list of addresses assigned the specified label.\n",
     643         [ #  # ]:          0 :                 {
     644 [ #  # ][ #  # ]:          0 :                     {"label", RPCArg::Type::STR, RPCArg::Optional::NO, "The label."},
                 [ #  # ]
     645                 :            :                 },
     646 [ #  # ][ #  # ]:          0 :                 RPCResult{
     647 [ #  # ][ #  # ]:          0 :                     RPCResult::Type::OBJ_DYN, "", "json object with addresses as keys",
     648         [ #  # ]:          0 :                     {
     649 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::OBJ, "address", "json object with information about address",
                 [ #  # ]
     650         [ #  # ]:          0 :                         {
     651 [ #  # ][ #  # ]:          0 :                             {RPCResult::Type::STR, "purpose", "Purpose of address (\"send\" for sending address, \"receive\" for receiving address)"},
                 [ #  # ]
     652                 :            :                         }},
     653                 :            :                     }
     654                 :            :                 },
     655         [ #  # ]:          0 :                 RPCExamples{
     656 [ #  # ][ #  # ]:          0 :                     HelpExampleCli("getaddressesbylabel", "\"tabby\"")
                 [ #  # ]
     657 [ #  # ][ #  # ]:          0 :             + HelpExampleRpc("getaddressesbylabel", "\"tabby\"")
         [ #  # ][ #  # ]
     658                 :            :                 },
     659                 :          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     660                 :            : {
     661                 :          0 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
     662 [ #  # ][ #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     663                 :            : 
     664         [ #  # ]:          0 :     LOCK(pwallet->cs_wallet);
     665                 :            : 
     666 [ #  # ][ #  # ]:          0 :     const std::string label{LabelFromValue(request.params[0])};
     667                 :            : 
     668                 :            :     // Find all addresses that have the given label
     669         [ #  # ]:          0 :     UniValue ret(UniValue::VOBJ);
     670                 :          0 :     std::set<std::string> addresses;
     671 [ #  # ][ #  # ]:          0 :     pwallet->ForEachAddrBookEntry([&](const CTxDestination& _dest, const std::string& _label, bool _is_change, const std::optional<AddressPurpose>& _purpose) {
     672         [ #  # ]:          0 :         if (_is_change) return;
     673         [ #  # ]:          0 :         if (_label == label) {
     674                 :          0 :             std::string address = EncodeDestination(_dest);
     675                 :            :             // CWallet::m_address_book is not expected to contain duplicate
     676                 :            :             // address strings, but build a separate set as a precaution just in
     677                 :            :             // case it does.
     678         [ #  # ]:          0 :             bool unique = addresses.emplace(address).second;
     679         [ #  # ]:          0 :             CHECK_NONFATAL(unique);
     680                 :            :             // UniValue::pushKV checks if the key exists in O(N)
     681                 :            :             // and since duplicate addresses are unexpected (checked with
     682                 :            :             // std::set in O(log(N))), UniValue::pushKVEnd is used instead,
     683                 :            :             // which currently is O(1).
     684         [ #  # ]:          0 :             UniValue value(UniValue::VOBJ);
     685 [ #  # ][ #  # ]:          0 :             value.pushKV("purpose", _purpose ? PurposeToString(*_purpose) : "unknown");
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     686 [ #  # ][ #  # ]:          0 :             ret.pushKVEnd(address, value);
                 [ #  # ]
     687                 :          0 :         }
     688                 :          0 :     });
     689                 :            : 
     690 [ #  # ][ #  # ]:          0 :     if (ret.empty()) {
     691 [ #  # ][ #  # ]:          0 :         throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, std::string("No addresses with label " + label));
         [ #  # ][ #  # ]
     692                 :            :     }
     693                 :            : 
     694                 :          0 :     return ret;
     695         [ #  # ]:          0 : },
     696                 :            :     };
     697                 :          0 : }
     698                 :            : 
     699                 :          0 : RPCHelpMan listlabels()
     700                 :            : {
     701 [ #  # ][ #  # ]:          0 :     return RPCHelpMan{"listlabels",
         [ #  # ][ #  # ]
     702         [ #  # ]:          0 :                 "\nReturns the list of all labels, or labels that are assigned to addresses with a specific purpose.\n",
     703         [ #  # ]:          0 :                 {
     704 [ #  # ][ #  # ]:          0 :                     {"purpose", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Address purpose to list labels for ('send','receive'). An empty string is the same as not providing this argument."},
                 [ #  # ]
     705                 :            :                 },
     706 [ #  # ][ #  # ]:          0 :                 RPCResult{
     707 [ #  # ][ #  # ]:          0 :                     RPCResult::Type::ARR, "", "",
     708         [ #  # ]:          0 :                     {
     709 [ #  # ][ #  # ]:          0 :                         {RPCResult::Type::STR, "label", "Label name"},
                 [ #  # ]
     710                 :            :                     }
     711                 :            :                 },
     712         [ #  # ]:          0 :                 RPCExamples{
     713                 :            :             "\nList all labels\n"
     714 [ #  # ][ #  # ]:          0 :             + HelpExampleCli("listlabels", "") +
         [ #  # ][ #  # ]
                 [ #  # ]
     715                 :            :             "\nList labels that have receiving addresses\n"
     716 [ #  # ][ #  # ]:          0 :             + HelpExampleCli("listlabels", "receive") +
         [ #  # ][ #  # ]
                 [ #  # ]
     717                 :            :             "\nList labels that have sending addresses\n"
     718 [ #  # ][ #  # ]:          0 :             + HelpExampleCli("listlabels", "send") +
         [ #  # ][ #  # ]
                 [ #  # ]
     719                 :            :             "\nAs a JSON-RPC call\n"
     720 [ #  # ][ #  # ]:          0 :             + HelpExampleRpc("listlabels", "receive")
         [ #  # ][ #  # ]
     721                 :            :                 },
     722                 :          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     723                 :            : {
     724                 :          0 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
     725 [ #  # ][ #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     726                 :            : 
     727         [ #  # ]:          0 :     LOCK(pwallet->cs_wallet);
     728                 :            : 
     729                 :          0 :     std::optional<AddressPurpose> purpose;
     730 [ #  # ][ #  # ]:          0 :     if (!request.params[0].isNull()) {
     731 [ #  # ][ #  # ]:          0 :         std::string purpose_str = request.params[0].get_str();
                 [ #  # ]
     732         [ #  # ]:          0 :         if (!purpose_str.empty()) {
     733         [ #  # ]:          0 :             purpose = PurposeFromString(purpose_str);
     734         [ #  # ]:          0 :             if (!purpose) {
     735 [ #  # ][ #  # ]:          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid 'purpose' argument, must be a known purpose string, typically 'send', or 'receive'.");
         [ #  # ][ #  # ]
     736                 :            :             }
     737                 :          0 :         }
     738                 :          0 :     }
     739                 :            : 
     740                 :            :     // Add to a set to sort by label name, then insert into Univalue array
     741         [ #  # ]:          0 :     std::set<std::string> label_set = pwallet->ListAddrBookLabels(purpose);
     742                 :            : 
     743         [ #  # ]:          0 :     UniValue ret(UniValue::VARR);
     744         [ #  # ]:          0 :     for (const std::string& name : label_set) {
     745 [ #  # ][ #  # ]:          0 :         ret.push_back(name);
     746                 :            :     }
     747                 :            : 
     748                 :          0 :     return ret;
     749         [ #  # ]:          0 : },
     750                 :            :     };
     751                 :          0 : }
     752                 :            : 
     753                 :            : 
     754                 :            : #ifdef ENABLE_EXTERNAL_SIGNER
     755                 :            : RPCHelpMan walletdisplayaddress()
     756                 :            : {
     757                 :            :     return RPCHelpMan{
     758                 :            :         "walletdisplayaddress",
     759                 :            :         "Display address on an external signer for verification.",
     760                 :            :         {
     761                 :            :             {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "bitcoin address to display"},
     762                 :            :         },
     763                 :            :         RPCResult{
     764                 :            :             RPCResult::Type::OBJ,"","",
     765                 :            :             {
     766                 :            :                 {RPCResult::Type::STR, "address", "The address as confirmed by the signer"},
     767                 :            :             }
     768                 :            :         },
     769                 :            :         RPCExamples{""},
     770                 :            :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     771                 :            :         {
     772                 :            :             std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
     773                 :            :             if (!wallet) return UniValue::VNULL;
     774                 :            :             CWallet* const pwallet = wallet.get();
     775                 :            : 
     776                 :            :             LOCK(pwallet->cs_wallet);
     777                 :            : 
     778                 :            :             CTxDestination dest = DecodeDestination(request.params[0].get_str());
     779                 :            : 
     780                 :            :             // Make sure the destination is valid
     781                 :            :             if (!IsValidDestination(dest)) {
     782                 :            :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
     783                 :            :             }
     784                 :            : 
     785                 :            :             if (!pwallet->DisplayAddress(dest)) {
     786                 :            :                 throw JSONRPCError(RPC_MISC_ERROR, "Failed to display address");
     787                 :            :             }
     788                 :            : 
     789                 :            :             UniValue result(UniValue::VOBJ);
     790                 :            :             result.pushKV("address", request.params[0].get_str());
     791                 :            :             return result;
     792                 :            :         }
     793                 :            :     };
     794                 :            : }
     795                 :            : #endif // ENABLE_EXTERNAL_SIGNER
     796                 :            : } // namespace wallet

Generated by: LCOV version 1.14