util.h raw

   1  // Copyright (c) 2017-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  #ifndef BITCOIN_WALLET_RPC_UTIL_H
   6  #define BITCOIN_WALLET_RPC_UTIL_H
   7  
   8  #include <rpc/util.h>
   9  #include <script/script.h>
  10  #include <wallet/wallet.h>
  11  
  12  #include <any>
  13  #include <memory>
  14  #include <optional>
  15  #include <string>
  16  #include <string_view>
  17  #include <vector>
  18  
  19  class JSONRPCRequest;
  20  class UniValue;
  21  struct bilingual_str;
  22  
  23  namespace wallet {
  24  class LegacyScriptPubKeyMan;
  25  enum class DatabaseStatus;
  26  struct WalletContext;
  27  
  28  extern const std::string HELP_REQUIRING_PASSPHRASE;
  29  
  30  static const RPCResult RESULT_LAST_PROCESSED_BLOCK { RPCResult::Type::OBJ, "lastprocessedblock", "hash and height of the block this information was generated on",{
  31      {RPCResult::Type::STR_HEX, "hash", "hash of the block this information was generated on"},
  32      {RPCResult::Type::NUM, "height", "height of the block this information was generated on"}}
  33  };
  34  
  35  /**
  36   * Figures out what wallet, if any, to use for a JSONRPCRequest.
  37   *
  38   * @param[in] request JSONRPCRequest that wishes to access a wallet
  39   * @return nullptr if no wallet should be used, or a pointer to the CWallet
  40   */
  41  std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request);
  42  std::optional<std::string> GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request);
  43  /**
  44   * Ensures that a wallet name is specified across the endpoint and wallet_name.
  45   * Throws `RPC_INVALID_PARAMETER` if none or different wallet names are specified.
  46   */
  47  std::string EnsureUniqueWalletName(const JSONRPCRequest& request, std::optional<std::string_view> wallet_name);
  48  
  49  void EnsureWalletIsUnlocked(const CWallet&);
  50  WalletContext& EnsureWalletContext(const std::any& context);
  51  
  52  bool GetAvoidReuseFlag(const CWallet& wallet, const UniValue& param);
  53  std::string LabelFromValue(const UniValue& value);
  54  //! Fetch parent descriptors of this scriptPubKey.
  55  void PushParentDescriptors(const CWallet& wallet, const CScript& script_pubkey, UniValue& entry);
  56  
  57  void HandleWalletError(const std::shared_ptr<CWallet>& wallet, DatabaseStatus& status, bilingual_str& error);
  58  void AppendLastProcessedBlock(UniValue& entry, const CWallet& wallet) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
  59  } //  namespace wallet
  60  
  61  #endif // BITCOIN_WALLET_RPC_UTIL_H
  62