Bitcoin Core  0.20.99
P2P Digital Currency
standard.h
Go to the documentation of this file.
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 
15 static const bool DEFAULT_ACCEPT_DATACARRIER = true;
16 
17 class CKeyID;
18 class CScript;
19 
21 class CScriptID : public uint160
22 {
23 public:
24  CScriptID() : uint160() {}
25  explicit CScriptID(const CScript& in);
26  CScriptID(const uint160& in) : uint160(in) {}
27 };
28 
33 static const unsigned int MAX_OP_RETURN_RELAY = 83;
34 
39 extern bool fAcceptDatacarrier;
40 
42 extern unsigned nMaxDatacarrierBytes;
43 
54 
56 {
58  // 'standard' transaction types:
67 };
68 
70 public:
71  friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
72  friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
73 };
74 
75 struct PKHash : public uint160
76 {
77  PKHash() : uint160() {}
78  explicit PKHash(const uint160& hash) : uint160(hash) {}
79  explicit PKHash(const CPubKey& pubkey);
80  using uint160::uint160;
81 };
82 
83 struct WitnessV0KeyHash;
84 struct ScriptHash : public uint160
85 {
87  // These don't do what you'd expect.
88  // Use ScriptHash(GetScriptForDestination(...)) instead.
89  explicit ScriptHash(const WitnessV0KeyHash& hash) = delete;
90  explicit ScriptHash(const PKHash& hash) = delete;
91  explicit ScriptHash(const uint160& hash) : uint160(hash) {}
92  explicit ScriptHash(const CScript& script);
93  using uint160::uint160;
94 };
95 
97 {
99  explicit WitnessV0ScriptHash(const uint256& hash) : uint256(hash) {}
100  explicit WitnessV0ScriptHash(const CScript& script);
101  using uint256::uint256;
102 };
103 
104 struct WitnessV0KeyHash : public uint160
105 {
107  explicit WitnessV0KeyHash(const uint160& hash) : uint160(hash) {}
108  using uint160::uint160;
109 };
110 
113 {
114  unsigned int version;
115  unsigned int length;
116  unsigned char program[40];
117 
118  friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {
119  if (w1.version != w2.version) return false;
120  if (w1.length != w2.length) return false;
121  return std::equal(w1.program, w1.program + w1.length, w2.program);
122  }
123 
124  friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {
125  if (w1.version < w2.version) return true;
126  if (w1.version > w2.version) return false;
127  if (w1.length < w2.length) return true;
128  if (w1.length > w2.length) return false;
129  return std::lexicographical_compare(w1.program, w1.program + w1.length, w2.program, w2.program + w2.length);
130  }
131 };
132 
143 typedef boost::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown> CTxDestination;
144 
146 bool IsValidDestination(const CTxDestination& dest);
147 
149 const char* GetTxnOutputType(txnouttype t);
150 
161 txnouttype Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
162 
169 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
170 
182 bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
183 
190 
192 CScript GetScriptForRawPubKey(const CPubKey& pubkey);
193 
195 CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
196 
205 CScript GetScriptForWitness(const CScript& redeemscript);
206 
207 #endif // BITCOIN_SCRIPT_STANDARD_H
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:325
WitnessV0KeyHash(const uint160 &hash)
Definition: standard.h:107
unspendable OP_RETURN script that carries data
Definition: standard.h:63
CScript GetScriptForRawPubKey(const CPubKey &pubkey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:297
unsigned char program[40]
Definition: standard.h:116
static const unsigned int MAX_OP_RETURN_RELAY
Default setting for nMaxDatacarrierBytes.
Definition: standard.h:33
ScriptHash()
Definition: standard.h:86
static const bool DEFAULT_ACCEPT_DATACARRIER
Definition: standard.h:15
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:156
friend bool operator==(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:71
unsigned int version
Definition: standard.h:114
CScriptID()
Definition: standard.h:24
unsigned int length
Definition: standard.h:115
uint160()
Definition: uint256.h:111
friend bool operator<(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:72
CScript GetScriptForWitness(const CScript &redeemscript)
Generate a pay-to-witness script for the given redeem script.
Definition: standard.cpp:313
Only for Witness versions not already defined above.
Definition: standard.h:66
CTxDestination subtype to encode any future Witness version.
Definition: standard.h:112
An encapsulated public key.
Definition: pubkey.h:30
PKHash(const uint160 &hash)
Definition: standard.h:78
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:302
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
Definition: standard.cpp:14
uint256()
Definition: uint256.h:122
CScriptID(const uint160 &in)
Definition: standard.h:26
WitnessV0ScriptHash(const uint256 &hash)
Definition: standard.h:99
txnouttype
Definition: standard.h:55
256-bit opaque blob.
Definition: uint256.h:120
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:289
friend bool operator==(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:118
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
160-bit opaque blob.
Definition: uint256.h:109
friend bool operator<(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:124
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:21
const char * GetTxnOutputType(txnouttype t)
Get the name of a txnouttype as a C string, or nullptr if unknown.
Definition: standard.cpp:28
unsigned nMaxDatacarrierBytes
Maximum size of TX_NULL_DATA scripts that this node considers standard.
Definition: standard.cpp:15
bool ExtractDestinations(const CScript &scriptPubKey, txnouttype &typeRet, std::vector< CTxDestination > &addressRet, int &nRequiredRet)
Parse a standard scriptPubKey with one or more destination addresses.
Definition: standard.cpp:200
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:143
static const unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS
Mandatory script verification flags that all new blocks must comply with for them to be valid...
Definition: standard.h:53
txnouttype Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:91
ScriptHash(const uint160 &hash)
Definition: standard.h:91
PKHash()
Definition: standard.h:77