1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
6 #ifndef BITCOIN_SCRIPT_STANDARD_H
7 #define BITCOIN_SCRIPT_STANDARD_H
9 #include "script/interpreter.h"
12 #include <boost/variant.hpp>
16 static const bool DEFAULT_ACCEPT_DATACARRIER
= true;
21 /** A reference to a CScript: the Hash160 of its serialization (see script.h) */
22 class CScriptID
: public uint160
25 CScriptID() : uint160() {}
26 CScriptID(const CScript
& in
);
27 CScriptID(const uint160
& in
) : uint160(in
) {}
31 * Default setting for nMaxDatacarrierBytes. 80 bytes of data, +1 for OP_RETURN,
32 * +2 for the pushdata opcodes.
34 static const unsigned int MAX_OP_RETURN_RELAY
= 83;
37 * A data carrying output is an unspendable output containing data. The script
38 * type is designated as TX_NULL_DATA.
40 extern bool fAcceptDatacarrier
;
42 /** Maximum size of TX_NULL_DATA scripts that this node considers standard. */
43 extern unsigned nMaxDatacarrierBytes
;
46 * Mandatory script verification flags that all new blocks must comply with for
47 * them to be valid. (but old blocks may not comply with) Currently just P2SH,
48 * but in the future other flags may be added, such as a soft-fork to enforce
49 * strict DER encoding.
51 * Failing one of these tests may trigger a DoS ban - see CheckInputs() for
54 static const unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS
= SCRIPT_VERIFY_P2SH
;
59 // 'standard' transaction types:
64 TX_NULL_DATA
, //!< unspendable OP_RETURN script that carries data
65 TX_WITNESS_V0_SCRIPTHASH
,
66 TX_WITNESS_V0_KEYHASH
,
69 class CNoDestination
{
71 friend bool operator==(const CNoDestination
&a
, const CNoDestination
&b
) { return true; }
72 friend bool operator<(const CNoDestination
&a
, const CNoDestination
&b
) { return true; }
76 * A txout script template with a specific destination. It is either:
77 * * CNoDestination: no destination set
78 * * CKeyID: TX_PUBKEYHASH destination
79 * * CScriptID: TX_SCRIPTHASH destination
80 * A CTxDestination is the internal data type encoded in a bitcoin address
82 typedef boost::variant
<CNoDestination
, CKeyID
, CScriptID
> CTxDestination
;
84 /** Check whether a CTxDestination is a CNoDestination. */
85 bool IsValidDestination(const CTxDestination
& dest
);
87 /** Get the name of a txnouttype as a C string, or nullptr if unknown. */
88 const char* GetTxnOutputType(txnouttype t
);
91 * Parse a scriptPubKey and identify script type for standard scripts. If
92 * successful, returns script type and parsed pubkeys or hashes, depending on
93 * the type. For example, for a P2SH script, vSolutionsRet will contain the
94 * script hash, for P2PKH it will contain the key hash, etc.
96 * @param[in] scriptPubKey Script to parse
97 * @param[out] typeRet The script type
98 * @param[out] vSolutionsRet Vector of parsed pubkeys and hashes
99 * @return True if script matches standard template
101 bool Solver(const CScript
& scriptPubKey
, txnouttype
& typeRet
, std::vector
<std::vector
<unsigned char> >& vSolutionsRet
);
104 * Parse a standard scriptPubKey for the destination address. Assigns result to
105 * the addressRet parameter and returns true if successful. For multisig
106 * scripts, instead use ExtractDestinations. Currently only works for P2PK,
107 * P2PKH, and P2SH scripts.
109 bool ExtractDestination(const CScript
& scriptPubKey
, CTxDestination
& addressRet
);
112 * Parse a standard scriptPubKey with one or more destination addresses. For
113 * multisig scripts, this populates the addressRet vector with the pubkey IDs
114 * and nRequiredRet with the n required to spend. For other destinations,
115 * addressRet is populated with a single value and nRequiredRet is set to 1.
116 * Returns true if successful. Currently does not extract address from
117 * pay-to-witness scripts.
119 bool ExtractDestinations(const CScript
& scriptPubKey
, txnouttype
& typeRet
, std::vector
<CTxDestination
>& addressRet
, int& nRequiredRet
);
122 * Generate a Bitcoin scriptPubKey for the given CTxDestination. Returns a P2PKH
123 * script for a CKeyID destination, a P2SH script for a CScriptID, and an empty
124 * script for CNoDestination.
126 CScript
GetScriptForDestination(const CTxDestination
& dest
);
128 /** Generate a P2PK script for the given pubkey. */
129 CScript
GetScriptForRawPubKey(const CPubKey
& pubkey
);
131 /** Generate a multisig script. */
132 CScript
GetScriptForMultisig(int nRequired
, const std::vector
<CPubKey
>& keys
);
135 * Generate a pay-to-witness script for the given redeem script. If the redeem
136 * script is P2PK or P2PKH, this returns a P2WPKH script, otherwise it returns a
139 CScript
GetScriptForWitness(const CScript
& redeemscript
);
141 #endif // BITCOIN_SCRIPT_STANDARD_H