LCOV - code coverage report
Current view: top level - src/script - standard.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 42 44 95.5 %
Date: 2020-09-26 01:30:44 Functions: 39 41 95.1 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2020 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 <script/interpreter.h>
      10             : #include <uint256.h>
      11             : 
      12             : #include <boost/variant.hpp>
      13             : 
      14             : #include <string>
      15             : 
      16             : 
      17             : static const bool DEFAULT_ACCEPT_DATACARRIER = true;
      18             : 
      19             : class CKeyID;
      20             : class CScript;
      21             : struct ScriptHash;
      22             : 
      23             : template<typename HashType>
      24             : class BaseHash
      25             : {
      26             : protected:
      27             :     HashType m_hash;
      28             : 
      29             : public:
      30      582336 :     BaseHash() : m_hash() {}
      31     1585764 :     BaseHash(const HashType& in) : m_hash(in) {}
      32             : 
      33      191389 :     unsigned char* begin()
      34             :     {
      35      191389 :         return m_hash.begin();
      36             :     }
      37             : 
      38      798612 :     const unsigned char* begin() const
      39             :     {
      40      798612 :         return m_hash.begin();
      41             :     }
      42             : 
      43             :     unsigned char* end()
      44             :     {
      45             :         return m_hash.end();
      46             :     }
      47             : 
      48      797690 :     const unsigned char* end() const
      49             :     {
      50      797690 :         return m_hash.end();
      51             :     }
      52             : 
      53        6306 :     operator std::vector<unsigned char>() const
      54             :     {
      55        6306 :         return std::vector<unsigned char>{m_hash.begin(), m_hash.end()};
      56             :     }
      57             : 
      58           3 :     std::string ToString() const
      59             :     {
      60           3 :         return m_hash.ToString();
      61             :     }
      62             : 
      63        7389 :     bool operator==(const BaseHash<HashType>& other) const noexcept
      64             :     {
      65        7389 :         return m_hash == other.m_hash;
      66             :     }
      67             : 
      68          32 :     bool operator!=(const BaseHash<HashType>& other) const noexcept
      69             :     {
      70          32 :         return !(m_hash == other.m_hash);
      71             :     }
      72             : 
      73     7212128 :     bool operator<(const BaseHash<HashType>& other) const noexcept
      74             :     {
      75     7212128 :         return m_hash < other.m_hash;
      76             :     }
      77             : 
      78       19372 :     size_t size() const
      79             :     {
      80       19372 :         return m_hash.size();
      81             :     }
      82             : 
      83           1 :     unsigned char* data() { return m_hash.data(); }
      84         744 :     const unsigned char* data() const { return m_hash.data(); }
      85             : };
      86             : 
      87             : /** A reference to a CScript: the Hash160 of its serialization (see script.h) */
      88             : class CScriptID : public BaseHash<uint160>
      89             : {
      90             : public:
      91      788970 :     CScriptID() : BaseHash() {}
      92             :     explicit CScriptID(const CScript& in);
      93      339778 :     explicit CScriptID(const uint160& in) : BaseHash(in) {}
      94             :     explicit CScriptID(const ScriptHash& in);
      95             : };
      96             : 
      97             : /**
      98             :  * Default setting for nMaxDatacarrierBytes. 80 bytes of data, +1 for OP_RETURN,
      99             :  * +2 for the pushdata opcodes.
     100             :  */
     101             : static const unsigned int MAX_OP_RETURN_RELAY = 83;
     102             : 
     103             : /**
     104             :  * A data carrying output is an unspendable output containing data. The script
     105             :  * type is designated as TxoutType::NULL_DATA.
     106             :  */
     107             : extern bool fAcceptDatacarrier;
     108             : 
     109             : /** Maximum size of TxoutType::NULL_DATA scripts that this node considers standard. */
     110             : extern unsigned nMaxDatacarrierBytes;
     111             : 
     112             : /**
     113             :  * Mandatory script verification flags that all new blocks must comply with for
     114             :  * them to be valid. (but old blocks may not comply with) Currently just P2SH,
     115             :  * but in the future other flags may be added.
     116             :  *
     117             :  * Failing one of these tests may trigger a DoS ban - see CheckInputScripts() for
     118             :  * details.
     119             :  */
     120             : static const unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH;
     121             : 
     122             : enum class TxoutType {
     123             :     NONSTANDARD,
     124             :     // 'standard' transaction types:
     125             :     PUBKEY,
     126             :     PUBKEYHASH,
     127             :     SCRIPTHASH,
     128             :     MULTISIG,
     129             :     NULL_DATA, //!< unspendable OP_RETURN script that carries data
     130             :     WITNESS_V0_SCRIPTHASH,
     131             :     WITNESS_V0_KEYHASH,
     132             :     WITNESS_UNKNOWN, //!< Only for Witness versions not already defined above
     133             : };
     134             : 
     135             : class CNoDestination {
     136             : public:
     137           0 :     friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
     138           0 :     friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
     139             : };
     140             : 
     141             : struct PKHash : public BaseHash<uint160>
     142             : {
     143           2 :     PKHash() : BaseHash() {}
     144     1013028 :     explicit PKHash(const uint160& hash) : BaseHash(hash) {}
     145             :     explicit PKHash(const CPubKey& pubkey);
     146             :     explicit PKHash(const CKeyID& pubkey_id);
     147             : };
     148             : CKeyID ToKeyID(const PKHash& key_hash);
     149             : 
     150             : struct WitnessV0KeyHash;
     151             : struct ScriptHash : public BaseHash<uint160>
     152             : {
     153             :     ScriptHash() : BaseHash() {}
     154             :     // These don't do what you'd expect.
     155             :     // Use ScriptHash(GetScriptForDestination(...)) instead.
     156             :     explicit ScriptHash(const WitnessV0KeyHash& hash) = delete;
     157             :     explicit ScriptHash(const PKHash& hash) = delete;
     158             : 
     159       39590 :     explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}
     160             :     explicit ScriptHash(const CScript& script);
     161             :     explicit ScriptHash(const CScriptID& script);
     162             : };
     163             : 
     164             : struct WitnessV0ScriptHash : public BaseHash<uint256>
     165             : {
     166        7486 :     WitnessV0ScriptHash() : BaseHash() {}
     167             :     explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}
     168             :     explicit WitnessV0ScriptHash(const CScript& script);
     169             : };
     170             : 
     171             : struct WitnessV0KeyHash : public BaseHash<uint160>
     172             : {
     173      343446 :     WitnessV0KeyHash() : BaseHash() {}
     174      285080 :     explicit WitnessV0KeyHash(const uint160& hash) : BaseHash(hash) {}
     175             :     explicit WitnessV0KeyHash(const CPubKey& pubkey);
     176             :     explicit WitnessV0KeyHash(const PKHash& pubkey_hash);
     177             : };
     178             : CKeyID ToKeyID(const WitnessV0KeyHash& key_hash);
     179             : 
     180             : //! CTxDestination subtype to encode any future Witness version
     181             : struct WitnessUnknown
     182             : {
     183             :     unsigned int version;
     184             :     unsigned int length;
     185             :     unsigned char program[40];
     186             : 
     187           1 :     friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {
     188           1 :         if (w1.version != w2.version) return false;
     189           1 :         if (w1.length != w2.length) return false;
     190           1 :         return std::equal(w1.program, w1.program + w1.length, w2.program);
     191           1 :     }
     192             : 
     193          34 :     friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {
     194          34 :         if (w1.version < w2.version) return true;
     195          24 :         if (w1.version > w2.version) return false;
     196          18 :         if (w1.length < w2.length) return true;
     197          14 :         if (w1.length > w2.length) return false;
     198          12 :         return std::lexicographical_compare(w1.program, w1.program + w1.length, w2.program, w2.program + w2.length);
     199          34 :     }
     200             : };
     201             : 
     202             : /**
     203             :  * A txout script template with a specific destination. It is either:
     204             :  *  * CNoDestination: no destination set
     205             :  *  * PKHash: TxoutType::PUBKEYHASH destination (P2PKH)
     206             :  *  * ScriptHash: TxoutType::SCRIPTHASH destination (P2SH)
     207             :  *  * WitnessV0ScriptHash: TxoutType::WITNESS_V0_SCRIPTHASH destination (P2WSH)
     208             :  *  * WitnessV0KeyHash: TxoutType::WITNESS_V0_KEYHASH destination (P2WPKH)
     209             :  *  * WitnessUnknown: TxoutType::WITNESS_UNKNOWN destination (P2W???)
     210             :  *  A CTxDestination is the internal data type encoded in a bitcoin address
     211             :  */
     212             : typedef boost::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown> CTxDestination;
     213             : 
     214             : /** Check whether a CTxDestination is a CNoDestination. */
     215             : bool IsValidDestination(const CTxDestination& dest);
     216             : 
     217             : /** Get the name of a TxoutType as a string */
     218             : std::string GetTxnOutputType(TxoutType t);
     219             : 
     220             : /**
     221             :  * Parse a scriptPubKey and identify script type for standard scripts. If
     222             :  * successful, returns script type and parsed pubkeys or hashes, depending on
     223             :  * the type. For example, for a P2SH script, vSolutionsRet will contain the
     224             :  * script hash, for P2PKH it will contain the key hash, etc.
     225             :  *
     226             :  * @param[in]   scriptPubKey   Script to parse
     227             :  * @param[out]  vSolutionsRet  Vector of parsed pubkeys and hashes
     228             :  * @return                     The script type. TxoutType::NONSTANDARD represents a failed solve.
     229             :  */
     230             : TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
     231             : 
     232             : /**
     233             :  * Parse a standard scriptPubKey for the destination address. Assigns result to
     234             :  * the addressRet parameter and returns true if successful. For multisig
     235             :  * scripts, instead use ExtractDestinations. Currently only works for P2PK,
     236             :  * P2PKH, P2SH, P2WPKH, and P2WSH scripts.
     237             :  */
     238             : bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
     239             : 
     240             : /**
     241             :  * Parse a standard scriptPubKey with one or more destination addresses. For
     242             :  * multisig scripts, this populates the addressRet vector with the pubkey IDs
     243             :  * and nRequiredRet with the n required to spend. For other destinations,
     244             :  * addressRet is populated with a single value and nRequiredRet is set to 1.
     245             :  * Returns true if successful.
     246             :  *
     247             :  * Note: this function confuses destinations (a subset of CScripts that are
     248             :  * encodable as an address) with key identifiers (of keys involved in a
     249             :  * CScript), and its use should be phased out.
     250             :  */
     251             : bool ExtractDestinations(const CScript& scriptPubKey, TxoutType& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
     252             : 
     253             : /**
     254             :  * Generate a Bitcoin scriptPubKey for the given CTxDestination. Returns a P2PKH
     255             :  * script for a CKeyID destination, a P2SH script for a CScriptID, and an empty
     256             :  * script for CNoDestination.
     257             :  */
     258             : CScript GetScriptForDestination(const CTxDestination& dest);
     259             : 
     260             : /** Generate a P2PK script for the given pubkey. */
     261             : CScript GetScriptForRawPubKey(const CPubKey& pubkey);
     262             : 
     263             : /** Generate a multisig script. */
     264             : CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
     265             : 
     266             : #endif // BITCOIN_SCRIPT_STANDARD_H

Generated by: LCOV version 1.15