/ src / script / solver.h
solver.h
 1  // Copyright (c) 2009-2010 Satoshi Nakamoto
 2  // Copyright (c) 2009-2022 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 <string>
15  #include <optional>
16  #include <utility>
17  #include <vector>
18  
19  class CPubKey;
20  template <typename C> class Span;
21  
22  enum class TxoutType {
23      NONSTANDARD,
24      // 'standard' transaction types:
25      PUBKEY,
26      PUBKEYHASH,
27      SCRIPTHASH,
28      MULTISIG,
29      NULL_DATA, //!< unspendable OP_RETURN script that carries data
30      WITNESS_V0_SCRIPTHASH,
31      WITNESS_V0_KEYHASH,
32      WITNESS_V1_TAPROOT,
33      WITNESS_UNKNOWN, //!< Only for Witness versions not already defined above
34  };
35  
36  /** Get the name of a TxoutType as a string */
37  std::string GetTxnOutputType(TxoutType t);
38  
39  constexpr bool IsPushdataOp(opcodetype opcode)
40  {
41      return opcode > OP_FALSE && opcode <= OP_PUSHDATA4;
42  }
43  
44  /**
45   * Parse a scriptPubKey and identify script type for standard scripts. If
46   * successful, returns script type and parsed pubkeys or hashes, depending on
47   * the type. For example, for a P2SH script, vSolutionsRet will contain the
48   * script hash, for P2PKH it will contain the key hash, etc.
49   *
50   * @param[in]   scriptPubKey   Script to parse
51   * @param[out]  vSolutionsRet  Vector of parsed pubkeys and hashes
52   * @return                     The script type. TxoutType::NONSTANDARD represents a failed solve.
53   */
54  TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
55  
56  /** Generate a P2PK script for the given pubkey. */
57  CScript GetScriptForRawPubKey(const CPubKey& pubkey);
58  
59  /** Determine if script is a "multi_a" script. Returns (threshold, keyspans) if so, and nullopt otherwise.
60   *  The keyspans refer to bytes in the passed script. */
61  std::optional<std::pair<int, std::vector<Span<const unsigned char>>>> MatchMultiA(const CScript& script LIFETIMEBOUND);
62  
63  /** Generate a multisig script. */
64  CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
65  
66  #endif // BITCOIN_SCRIPT_SOLVER_H