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
,
67 TX_WITNESS_UNKNOWN
, //!< Only for Witness versions not already defined above
70 class CNoDestination
{
72 friend bool operator==(const CNoDestination
&a
, const CNoDestination
&b
) { return true; }
73 friend bool operator<(const CNoDestination
&a
, const CNoDestination
&b
) { return true; }
76 struct WitnessV0ScriptHash
: public uint256
{};
77 struct WitnessV0KeyHash
: public uint160
{};
79 //! CTxDestination subtype to encode any future Witness version
84 unsigned char program
[40];
86 friend bool operator==(const WitnessUnknown
& w1
, const WitnessUnknown
& w2
) {
87 if (w1
.version
!= w2
.version
) return false;
88 if (w1
.length
!= w2
.length
) return false;
89 return std::equal(w1
.program
, w1
.program
+ w1
.length
, w2
.program
);
92 friend bool operator<(const WitnessUnknown
& w1
, const WitnessUnknown
& w2
) {
93 if (w1
.version
< w2
.version
) return true;
94 if (w1
.version
> w2
.version
) return false;
95 if (w1
.length
< w2
.length
) return true;
96 if (w1
.length
> w2
.length
) return false;
97 return std::lexicographical_compare(w1
.program
, w1
.program
+ w1
.length
, w2
.program
, w2
.program
+ w2
.length
);
102 * A txout script template with a specific destination. It is either:
103 * * CNoDestination: no destination set
104 * * CKeyID: TX_PUBKEYHASH destination (P2PKH)
105 * * CScriptID: TX_SCRIPTHASH destination (P2SH)
106 * * WitnessV0ScriptHash: TX_WITNESS_V0_SCRIPTHASH destination (P2WSH)
107 * * WitnessV0KeyHash: TX_WITNESS_V0_KEYHASH destination (P2WPKH)
108 * * WitnessUnknown: TX_WITNESS_UNKNOWN destination (P2W???)
109 * A CTxDestination is the internal data type encoded in a bitcoin address
111 typedef boost::variant
<CNoDestination
, CKeyID
, CScriptID
, WitnessV0ScriptHash
, WitnessV0KeyHash
, WitnessUnknown
> CTxDestination
;
113 /** Check whether a CTxDestination is a CNoDestination. */
114 bool IsValidDestination(const CTxDestination
& dest
);
116 /** Get the name of a txnouttype as a C string, or nullptr if unknown. */
117 const char* GetTxnOutputType(txnouttype t
);
120 * Parse a scriptPubKey and identify script type for standard scripts. If
121 * successful, returns script type and parsed pubkeys or hashes, depending on
122 * the type. For example, for a P2SH script, vSolutionsRet will contain the
123 * script hash, for P2PKH it will contain the key hash, etc.
125 * @param[in] scriptPubKey Script to parse
126 * @param[out] typeRet The script type
127 * @param[out] vSolutionsRet Vector of parsed pubkeys and hashes
128 * @return True if script matches standard template
130 bool Solver(const CScript
& scriptPubKey
, txnouttype
& typeRet
, std::vector
<std::vector
<unsigned char> >& vSolutionsRet
);
133 * Parse a standard scriptPubKey for the destination address. Assigns result to
134 * the addressRet parameter and returns true if successful. For multisig
135 * scripts, instead use ExtractDestinations. Currently only works for P2PK,
136 * P2PKH, P2SH, P2WPKH, and P2WSH scripts.
138 bool ExtractDestination(const CScript
& scriptPubKey
, CTxDestination
& addressRet
);
141 * Parse a standard scriptPubKey with one or more destination addresses. For
142 * multisig scripts, this populates the addressRet vector with the pubkey IDs
143 * and nRequiredRet with the n required to spend. For other destinations,
144 * addressRet is populated with a single value and nRequiredRet is set to 1.
145 * Returns true if successful. Currently does not extract address from
146 * pay-to-witness scripts.
148 bool ExtractDestinations(const CScript
& scriptPubKey
, txnouttype
& typeRet
, std::vector
<CTxDestination
>& addressRet
, int& nRequiredRet
);
151 * Generate a Bitcoin scriptPubKey for the given CTxDestination. Returns a P2PKH
152 * script for a CKeyID destination, a P2SH script for a CScriptID, and an empty
153 * script for CNoDestination.
155 CScript
GetScriptForDestination(const CTxDestination
& dest
);
157 /** Generate a P2PK script for the given pubkey. */
158 CScript
GetScriptForRawPubKey(const CPubKey
& pubkey
);
160 /** Generate a multisig script. */
161 CScript
GetScriptForMultisig(int nRequired
, const std::vector
<CPubKey
>& keys
);
164 * Generate a pay-to-witness script for the given redeem script. If the redeem
165 * script is P2PK or P2PKH, this returns a P2WPKH script, otherwise it returns a
168 CScript
GetScriptForWitness(const CScript
& redeemscript
);
170 #endif // BITCOIN_SCRIPT_STANDARD_H