Branch data 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 : : #include <util/hash_type.h>
# 12 : :
# 13 : : #include <string>
# 14 : : #include <variant>
# 15 : :
# 16 : : static const bool DEFAULT_ACCEPT_DATACARRIER = true;
# 17 : :
# 18 : : class CKeyID;
# 19 : : class CScript;
# 20 : : struct ScriptHash;
# 21 : :
# 22 : : /** A reference to a CScript: the Hash160 of its serialization (see script.h) */
# 23 : : class CScriptID : public BaseHash<uint160>
# 24 : : {
# 25 : : public:
# 26 : 536649 : CScriptID() : BaseHash() {}
# 27 : : explicit CScriptID(const CScript& in);
# 28 : 169705 : explicit CScriptID(const uint160& in) : BaseHash(in) {}
# 29 : : explicit CScriptID(const ScriptHash& in);
# 30 : : };
# 31 : :
# 32 : : /**
# 33 : : * Default setting for nMaxDatacarrierBytes. 80 bytes of data, +1 for OP_RETURN,
# 34 : : * +2 for the pushdata opcodes.
# 35 : : */
# 36 : : static const unsigned int MAX_OP_RETURN_RELAY = 83;
# 37 : :
# 38 : : /**
# 39 : : * A data carrying output is an unspendable output containing data. The script
# 40 : : * type is designated as TxoutType::NULL_DATA.
# 41 : : */
# 42 : : extern bool fAcceptDatacarrier;
# 43 : :
# 44 : : /** Maximum size of TxoutType::NULL_DATA scripts that this node considers standard. */
# 45 : : extern unsigned nMaxDatacarrierBytes;
# 46 : :
# 47 : : /**
# 48 : : * Mandatory script verification flags that all new blocks must comply with for
# 49 : : * them to be valid. (but old blocks may not comply with) Currently just P2SH,
# 50 : : * but in the future other flags may be added.
# 51 : : *
# 52 : : * Failing one of these tests may trigger a DoS ban - see CheckInputScripts() for
# 53 : : * details.
# 54 : : */
# 55 : : static const unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH;
# 56 : :
# 57 : : enum class TxoutType {
# 58 : : NONSTANDARD,
# 59 : : // 'standard' transaction types:
# 60 : : PUBKEY,
# 61 : : PUBKEYHASH,
# 62 : : SCRIPTHASH,
# 63 : : MULTISIG,
# 64 : : NULL_DATA, //!< unspendable OP_RETURN script that carries data
# 65 : : WITNESS_V0_SCRIPTHASH,
# 66 : : WITNESS_V0_KEYHASH,
# 67 : : WITNESS_V1_TAPROOT,
# 68 : : WITNESS_UNKNOWN, //!< Only for Witness versions not already defined above
# 69 : : };
# 70 : :
# 71 : : class CNoDestination {
# 72 : : public:
# 73 : 0 : friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
# 74 : 0 : friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
# 75 : : };
# 76 : :
# 77 : : struct PKHash : public BaseHash<uint160>
# 78 : : {
# 79 : 1 : PKHash() : BaseHash() {}
# 80 : 685003 : explicit PKHash(const uint160& hash) : BaseHash(hash) {}
# 81 : : explicit PKHash(const CPubKey& pubkey);
# 82 : : explicit PKHash(const CKeyID& pubkey_id);
# 83 : : };
# 84 : : CKeyID ToKeyID(const PKHash& key_hash);
# 85 : :
# 86 : : struct WitnessV0KeyHash;
# 87 : : struct ScriptHash : public BaseHash<uint160>
# 88 : : {
# 89 : 0 : ScriptHash() : BaseHash() {}
# 90 : : // These don't do what you'd expect.
# 91 : : // Use ScriptHash(GetScriptForDestination(...)) instead.
# 92 : : explicit ScriptHash(const WitnessV0KeyHash& hash) = delete;
# 93 : : explicit ScriptHash(const PKHash& hash) = delete;
# 94 : :
# 95 : 13773 : explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}
# 96 : : explicit ScriptHash(const CScript& script);
# 97 : : explicit ScriptHash(const CScriptID& script);
# 98 : : };
# 99 : :
# 100 : : struct WitnessV0ScriptHash : public BaseHash<uint256>
# 101 : : {
# 102 : 2339 : WitnessV0ScriptHash() : BaseHash() {}
# 103 : 0 : explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}
# 104 : : explicit WitnessV0ScriptHash(const CScript& script);
# 105 : : };
# 106 : :
# 107 : : struct WitnessV0KeyHash : public BaseHash<uint160>
# 108 : : {
# 109 : 381897 : WitnessV0KeyHash() : BaseHash() {}
# 110 : 414615 : explicit WitnessV0KeyHash(const uint160& hash) : BaseHash(hash) {}
# 111 : : explicit WitnessV0KeyHash(const CPubKey& pubkey);
# 112 : : explicit WitnessV0KeyHash(const PKHash& pubkey_hash);
# 113 : : };
# 114 : : CKeyID ToKeyID(const WitnessV0KeyHash& key_hash);
# 115 : :
# 116 : : //! CTxDestination subtype to encode any future Witness version
# 117 : : struct WitnessUnknown
# 118 : : {
# 119 : : unsigned int version;
# 120 : : unsigned int length;
# 121 : : unsigned char program[40];
# 122 : :
# 123 : 2 : friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {
# 124 [ - + ]: 2 : if (w1.version != w2.version) return false;
# 125 [ - + ]: 2 : if (w1.length != w2.length) return false;
# 126 : 2 : return std::equal(w1.program, w1.program + w1.length, w2.program);
# 127 : 2 : }
# 128 : :
# 129 : 34 : friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {
# 130 [ + + ]: 34 : if (w1.version < w2.version) return true;
# 131 [ + + ]: 24 : if (w1.version > w2.version) return false;
# 132 [ + + ]: 18 : if (w1.length < w2.length) return true;
# 133 [ + + ]: 14 : if (w1.length > w2.length) return false;
# 134 : 12 : return std::lexicographical_compare(w1.program, w1.program + w1.length, w2.program, w2.program + w2.length);
# 135 : 12 : }
# 136 : : };
# 137 : :
# 138 : : /**
# 139 : : * A txout script template with a specific destination. It is either:
# 140 : : * * CNoDestination: no destination set
# 141 : : * * PKHash: TxoutType::PUBKEYHASH destination (P2PKH)
# 142 : : * * ScriptHash: TxoutType::SCRIPTHASH destination (P2SH)
# 143 : : * * WitnessV0ScriptHash: TxoutType::WITNESS_V0_SCRIPTHASH destination (P2WSH)
# 144 : : * * WitnessV0KeyHash: TxoutType::WITNESS_V0_KEYHASH destination (P2WPKH)
# 145 : : * * WitnessUnknown: TxoutType::WITNESS_UNKNOWN/WITNESS_V1_TAPROOT destination (P2W???)
# 146 : : * (taproot outputs do not require their own type as long as no wallet support exists)
# 147 : : * A CTxDestination is the internal data type encoded in a bitcoin address
# 148 : : */
# 149 : : using CTxDestination = std::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown>;
# 150 : :
# 151 : : /** Check whether a CTxDestination is a CNoDestination. */
# 152 : : bool IsValidDestination(const CTxDestination& dest);
# 153 : :
# 154 : : /** Get the name of a TxoutType as a string */
# 155 : : std::string GetTxnOutputType(TxoutType t);
# 156 : :
# 157 : : /**
# 158 : : * Parse a scriptPubKey and identify script type for standard scripts. If
# 159 : : * successful, returns script type and parsed pubkeys or hashes, depending on
# 160 : : * the type. For example, for a P2SH script, vSolutionsRet will contain the
# 161 : : * script hash, for P2PKH it will contain the key hash, etc.
# 162 : : *
# 163 : : * @param[in] scriptPubKey Script to parse
# 164 : : * @param[out] vSolutionsRet Vector of parsed pubkeys and hashes
# 165 : : * @return The script type. TxoutType::NONSTANDARD represents a failed solve.
# 166 : : */
# 167 : : TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
# 168 : :
# 169 : : /**
# 170 : : * Parse a standard scriptPubKey for the destination address. Assigns result to
# 171 : : * the addressRet parameter and returns true if successful. For multisig
# 172 : : * scripts, instead use ExtractDestinations. Currently only works for P2PK,
# 173 : : * P2PKH, P2SH, P2WPKH, and P2WSH scripts.
# 174 : : */
# 175 : : bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
# 176 : :
# 177 : : /**
# 178 : : * Parse a standard scriptPubKey with one or more destination addresses. For
# 179 : : * multisig scripts, this populates the addressRet vector with the pubkey IDs
# 180 : : * and nRequiredRet with the n required to spend. For other destinations,
# 181 : : * addressRet is populated with a single value and nRequiredRet is set to 1.
# 182 : : * Returns true if successful.
# 183 : : *
# 184 : : * Note: this function confuses destinations (a subset of CScripts that are
# 185 : : * encodable as an address) with key identifiers (of keys involved in a
# 186 : : * CScript), and its use should be phased out.
# 187 : : *
# 188 : : * TODO: from v23 ("addresses" and "reqSigs" deprecated) "ExtractDestinations" should be removed
# 189 : : */
# 190 : : bool ExtractDestinations(const CScript& scriptPubKey, TxoutType& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
# 191 : :
# 192 : : /**
# 193 : : * Generate a Bitcoin scriptPubKey for the given CTxDestination. Returns a P2PKH
# 194 : : * script for a CKeyID destination, a P2SH script for a CScriptID, and an empty
# 195 : : * script for CNoDestination.
# 196 : : */
# 197 : : CScript GetScriptForDestination(const CTxDestination& dest);
# 198 : :
# 199 : : /** Generate a P2PK script for the given pubkey. */
# 200 : : CScript GetScriptForRawPubKey(const CPubKey& pubkey);
# 201 : :
# 202 : : /** Generate a multisig script. */
# 203 : : CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
# 204 : :
# 205 : : #endif // BITCOIN_SCRIPT_STANDARD_H
|