LCOV - code coverage report
Current view: top level - src/wallet/rpc - addresses.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 85.1 % 451 384
Test Date: 2024-11-04 05:10:19 Functions: 88.2 % 34 30
Branches: 46.7 % 1814 847

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

Generated by: LCOV version 2.0-1