LCOV - code coverage report
Current view: top level - src/script - standard.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 8 10 80.0 %
Date: 2026-06-25 07:23:43 Functions: 13 15 86.7 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2021 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             : #ifndef BITCOIN_SCRIPT_STANDARD_H
       7             : #define BITCOIN_SCRIPT_STANDARD_H
       8             : 
       9             : #include <pubkey.h>
      10             : #include <script/interpreter.h>
      11             : #include <uint256.h>
      12             : #include <util/hash_type.h>
      13             : 
      14             : #include <variant>
      15             : #include <string>
      16             : 
      17             : 
      18             : static const bool DEFAULT_ACCEPT_DATACARRIER = true;
      19             : 
      20             : class CKeyID;
      21             : class CScript;
      22             : struct ScriptHash;
      23             : 
      24             : /** A reference to a CScript: the Hash160 of its serialization (see script.h) */
      25             : class CScriptID : public BaseHash<uint160>
      26             : {
      27             : public:
      28      941022 :     CScriptID() : BaseHash() {}
      29             :     explicit CScriptID(const CScript& in);
      30      140976 :     explicit CScriptID(const uint160& in) : BaseHash(in) {}
      31             :     explicit CScriptID(const ScriptHash& in);
      32             : };
      33             : 
      34             : /**
      35             :  * Default setting for nMaxDatacarrierBytes. 80 bytes of data, +1 for OP_RETURN,
      36             :  * +2 for the pushdata opcodes.
      37             :  */
      38             : static const unsigned int MAX_OP_RETURN_RELAY = 83;
      39             : 
      40             : /**
      41             :  * A data carrying output is an unspendable output containing data. The script
      42             :  * type is designated as TxoutType::NULL_DATA.
      43             :  */
      44             : extern bool fAcceptDatacarrier;
      45             : 
      46             : /** Maximum size of TxoutType::NULL_DATA scripts that this node considers standard. */
      47             : extern unsigned nMaxDatacarrierBytes;
      48             : 
      49             : /**
      50             :  * Mandatory script verification flags that all new blocks must comply with for
      51             :  * them to be valid. (but old blocks may not comply with) Currently just P2SH,
      52             :  * but in the future other flags may be added.
      53             :  *
      54             :  * Failing one of these tests may trigger a DoS ban - see CheckInputScripts() for
      55             :  * details.
      56             :  */
      57             : static const unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH;
      58             : 
      59             : enum class TxoutType
      60             : {
      61             :     NONSTANDARD,
      62             :     // 'standard' transaction types:
      63             :     PUBKEY,
      64             :     PUBKEYHASH,
      65             :     SCRIPTHASH,
      66             :     MULTISIG,
      67             :     NULL_DATA, //!< unspendable OP_RETURN script that carries data
      68             : };
      69             : 
      70             : class CNoDestination {
      71             : public:
      72           0 :     friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
      73           0 :     friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
      74             : };
      75             : 
      76             : struct PKHash : public BaseHash<uint160>
      77             : {
      78          36 :     PKHash() : BaseHash() {}
      79      705684 :     explicit PKHash(const uint160& hash) : BaseHash(hash) {}
      80             :     explicit PKHash(const CPubKey& pubkey);
      81             :     explicit PKHash(const CKeyID& pubkey_id);
      82             : };
      83             : CKeyID ToKeyID(const PKHash& key_hash);
      84             : 
      85             : struct ScriptHash : public BaseHash<uint160>
      86             : {
      87          20 :     ScriptHash() : BaseHash() {}
      88             :     // These don't do what you'd expect.
      89             :     // Use ScriptHash(GetScriptForDestination(...)) instead.
      90             :     explicit ScriptHash(const PKHash& hash) = delete;
      91             : 
      92       53614 :     explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}
      93             :     explicit ScriptHash(const CScript& script);
      94             :     explicit ScriptHash(const CScriptID& script);
      95             : };
      96             : 
      97             : /**
      98             :  * A txout script template with a specific destination. It is either:
      99             :  *  * CNoDestination: no destination set
     100             :  *  * CKeyID: TxoutType::PUBKEYHASH destination
     101             :  *  * CScriptID: TxoutType::SCRIPTHASH destination
     102             :  *  A CTxDestination is the internal data type encoded in a bitcoin address
     103             :  */
     104             : using CTxDestination = std::variant<CNoDestination, PKHash, ScriptHash>;
     105             : 
     106             : /** Check whether a CTxDestination is a CNoDestination. */
     107             : bool IsValidDestination(const CTxDestination& dest);
     108             : 
     109             : /** Get the name of a TxoutType as a C string, or nullptr if unknown. */
     110             : std::string GetTxnOutputType(TxoutType t);
     111             : 
     112         870 : constexpr bool IsPushdataOp(opcodetype opcode)
     113             : {
     114         870 :     return opcode > OP_FALSE && opcode <= OP_PUSHDATA4;
     115             : }
     116             : 
     117             : /**
     118             :  * Parse a scriptPubKey and identify script type for standard scripts. If
     119             :  * successful, returns script type and parsed pubkeys or hashes, depending on
     120             :  * the type. For example, for a P2SH script, vSolutionsRet will contain the
     121             :  * script hash, for P2PKH it will contain the key hash, etc.
     122             :  *
     123             :  * @param[in]   scriptPubKey   Script to parse
     124             :  * @param[out]  vSolutionsRet  Vector of parsed pubkeys and hashes
     125             :  * @return                     The script type. TxoutType::NONSTANDARD represents a failed solve.
     126             :  */
     127             : TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
     128             : 
     129             : /**
     130             :  * Parse a standard scriptPubKey for the destination address. Assigns result to
     131             :  * the addressRet parameter and returns true if successful. Currently only works for P2PK,
     132             :  * P2PKH, and P2SH scripts.
     133             :  */
     134             : bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
     135             : 
     136             : /**
     137             :  * Generate a Bitcoin scriptPubKey for the given CTxDestination. Returns a P2PKH
     138             :  * script for a CKeyID destination, a P2SH script for a CScriptID, and an empty
     139             :  * script for CNoDestination.
     140             :  */
     141             : CScript GetScriptForDestination(const CTxDestination& dest);
     142             : 
     143             : /** Generate a P2PK script for the given pubkey. */
     144             : CScript GetScriptForRawPubKey(const CPubKey& pubkey);
     145             : 
     146             : /** Generate a multisig script. */
     147             : CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
     148             : 
     149             : #endif // BITCOIN_SCRIPT_STANDARD_H

Generated by: LCOV version 1.16