solver.h raw

   1  // Copyright (c) 2009-2010 Satoshi Nakamoto
   2  // Copyright (c) 2009-present The Bitcoin Core developers
   3  // Distributed under the MIT software license, see the accompanying
   4  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   5  
   6  // The Solver functions are used by policy and the wallet, but not consensus.
   7  
   8  #ifndef BITCOIN_SCRIPT_SOLVER_H
   9  #define BITCOIN_SCRIPT_SOLVER_H
  10  
  11  #include <attributes.h>
  12  #include <script/script.h>
  13  
  14  #include <optional>
  15  #include <span>
  16  #include <string>
  17  #include <utility>
  18  #include <vector>
  19  
  20  class CPubKey;
  21  
  22  enum class TxoutType {
  23      NONSTANDARD,
  24      // 'standard' transaction types:
  25      ANCHOR, //!< anyone can spend script
  26      PUBKEY,
  27      PUBKEYHASH,
  28      SCRIPTHASH,
  29      MULTISIG,
  30      NULL_DATA, //!< unspendable OP_RETURN script that carries data
  31      WITNESS_V0_SCRIPTHASH,
  32      WITNESS_V0_KEYHASH,
  33      WITNESS_V1_TAPROOT,
  34      WITNESS_UNKNOWN, //!< Only for Witness versions not already defined above
  35  };
  36  
  37  /** Get the name of a TxoutType as a string */
  38  std::string GetTxnOutputType(TxoutType t);
  39  
  40  constexpr bool IsPushdataOp(opcodetype opcode)
  41  {
  42      return opcode > OP_FALSE && opcode <= OP_PUSHDATA4;
  43  }
  44  
  45  /**
  46   * Parse a scriptPubKey and identify script type for standard scripts. If
  47   * successful, returns script type and parsed pubkeys or hashes, depending on
  48   * the type. For example, for a P2SH script, vSolutionsRet will contain the
  49   * script hash, for P2PKH it will contain the key hash, etc.
  50   *
  51   * @param[in]   scriptPubKey   Script to parse
  52   * @param[out]  vSolutionsRet  Vector of parsed pubkeys and hashes
  53   * @return                     The script type. TxoutType::NONSTANDARD represents a failed solve.
  54   */
  55  TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
  56  
  57  /** Generate a P2PK script for the given pubkey. */
  58  CScript GetScriptForRawPubKey(const CPubKey& pubkey);
  59  
  60  /** Determine if script is a "multi_a" script. Returns (threshold, keyspans) if so, and nullopt otherwise.
  61   *  The keyspans refer to bytes in the passed script. */
  62  std::optional<std::pair<int, std::vector<std::span<const unsigned char>>>> MatchMultiA(const CScript& script LIFETIMEBOUND);
  63  
  64  /** Generate a multisig script. */
  65  CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
  66  
  67  #endif // BITCOIN_SCRIPT_SOLVER_H
  68