Bitcoin Core  21.99.0
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 <string>
13 #include <variant>
14 
15 static const bool DEFAULT_ACCEPT_DATACARRIER = true;
16 
17 class CKeyID;
18 class CScript;
19 struct ScriptHash;
20 
21 template<typename HashType>
22 class BaseHash
23 {
24 protected:
25  HashType m_hash;
26 
27 public:
28  BaseHash() : m_hash() {}
29  explicit BaseHash(const HashType& in) : m_hash(in) {}
30 
31  unsigned char* begin()
32  {
33  return m_hash.begin();
34  }
35 
36  const unsigned char* begin() const
37  {
38  return m_hash.begin();
39  }
40 
41  unsigned char* end()
42  {
43  return m_hash.end();
44  }
45 
46  const unsigned char* end() const
47  {
48  return m_hash.end();
49  }
50 
51  operator std::vector<unsigned char>() const
52  {
53  return std::vector<unsigned char>{m_hash.begin(), m_hash.end()};
54  }
55 
56  std::string ToString() const
57  {
58  return m_hash.ToString();
59  }
60 
61  bool operator==(const BaseHash<HashType>& other) const noexcept
62  {
63  return m_hash == other.m_hash;
64  }
65 
66  bool operator!=(const BaseHash<HashType>& other) const noexcept
67  {
68  return !(m_hash == other.m_hash);
69  }
70 
71  bool operator<(const BaseHash<HashType>& other) const noexcept
72  {
73  return m_hash < other.m_hash;
74  }
75 
76  size_t size() const
77  {
78  return m_hash.size();
79  }
80 
81  unsigned char* data() { return m_hash.data(); }
82  const unsigned char* data() const { return m_hash.data(); }
83 };
84 
86 class CScriptID : public BaseHash<uint160>
87 {
88 public:
90  explicit CScriptID(const CScript& in);
91  explicit CScriptID(const uint160& in) : BaseHash(in) {}
92  explicit CScriptID(const ScriptHash& in);
93 };
94 
99 static const unsigned int MAX_OP_RETURN_RELAY = 83;
100 
105 extern bool fAcceptDatacarrier;
106 
108 extern unsigned nMaxDatacarrierBytes;
109 
119 
120 enum class TxoutType {
121  NONSTANDARD,
122  // 'standard' transaction types:
123  PUBKEY,
124  PUBKEYHASH,
125  SCRIPTHASH,
126  MULTISIG,
127  NULL_DATA,
132 };
133 
135 public:
136  friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
137  friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
138 };
139 
140 struct PKHash : public BaseHash<uint160>
141 {
142  PKHash() : BaseHash() {}
143  explicit PKHash(const uint160& hash) : BaseHash(hash) {}
144  explicit PKHash(const CPubKey& pubkey);
145  explicit PKHash(const CKeyID& pubkey_id);
146 };
147 CKeyID ToKeyID(const PKHash& key_hash);
148 
149 struct WitnessV0KeyHash;
150 struct ScriptHash : public BaseHash<uint160>
151 {
153  // These don't do what you'd expect.
154  // Use ScriptHash(GetScriptForDestination(...)) instead.
155  explicit ScriptHash(const WitnessV0KeyHash& hash) = delete;
156  explicit ScriptHash(const PKHash& hash) = delete;
157 
158  explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}
159  explicit ScriptHash(const CScript& script);
160  explicit ScriptHash(const CScriptID& script);
161 };
162 
163 struct WitnessV0ScriptHash : public BaseHash<uint256>
164 {
166  explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}
167  explicit WitnessV0ScriptHash(const CScript& script);
168 };
169 
170 struct WitnessV0KeyHash : public BaseHash<uint160>
171 {
173  explicit WitnessV0KeyHash(const uint160& hash) : BaseHash(hash) {}
174  explicit WitnessV0KeyHash(const CPubKey& pubkey);
175  explicit WitnessV0KeyHash(const PKHash& pubkey_hash);
176 };
177 CKeyID ToKeyID(const WitnessV0KeyHash& key_hash);
178 
181 {
182  unsigned int version;
183  unsigned int length;
184  unsigned char program[40];
185 
186  friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {
187  if (w1.version != w2.version) return false;
188  if (w1.length != w2.length) return false;
189  return std::equal(w1.program, w1.program + w1.length, w2.program);
190  }
191 
192  friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {
193  if (w1.version < w2.version) return true;
194  if (w1.version > w2.version) return false;
195  if (w1.length < w2.length) return true;
196  if (w1.length > w2.length) return false;
197  return std::lexicographical_compare(w1.program, w1.program + w1.length, w2.program, w2.program + w2.length);
198  }
199 };
200 
212 using CTxDestination = std::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown>;
213 
215 bool IsValidDestination(const CTxDestination& dest);
216 
218 std::string GetTxnOutputType(TxoutType t);
219 
230 TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
231 
238 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
239 
251 bool ExtractDestinations(const CScript& scriptPubKey, TxoutType& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
252 
259 
261 CScript GetScriptForRawPubKey(const CPubKey& pubkey);
262 
264 CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
265 
266 #endif // BITCOIN_SCRIPT_STANDARD_H
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:321
bool ExtractDestinations(const CScript &scriptPubKey, TxoutType &typeRet, std::vector< CTxDestination > &addressRet, int &nRequiredRet)
Parse a standard scriptPubKey with one or more destination addresses.
Definition: standard.cpp:224
unsigned char * begin()
Definition: standard.h:31
const unsigned char * data() const
Definition: standard.h:82
WitnessV0KeyHash(const uint160 &hash)
Definition: standard.h:173
CScript GetScriptForRawPubKey(const CPubKey &pubkey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:305
unsigned char * data()
Definition: standard.h:81
unsigned char program[40]
Definition: standard.h:184
static const unsigned int MAX_OP_RETURN_RELAY
Default setting for nMaxDatacarrierBytes.
Definition: standard.h:99
ScriptHash()
Definition: standard.h:152
static const bool DEFAULT_ACCEPT_DATACARRIER
Definition: standard.h:15
HashType m_hash
Definition: standard.h:25
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:180
friend bool operator==(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:136
unsigned int version
Definition: standard.h:182
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:212
CScriptID()
Definition: standard.h:89
unsigned int length
Definition: standard.h:183
const unsigned char * begin() const
Definition: standard.h:36
unspendable OP_RETURN script that carries data
CKeyID ToKeyID(const PKHash &key_hash)
Definition: standard.cpp:31
std::string GetTxnOutputType(TxoutType t)
Get the name of a TxoutType as a string.
Definition: standard.cpp:46
const unsigned char * end() const
Definition: standard.h:46
friend bool operator<(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:137
CTxDestination subtype to encode any future Witness version.
Definition: standard.h:180
TxoutType
Definition: standard.h:120
An encapsulated public key.
Definition: pubkey.h:31
bool operator!=(const BaseHash< HashType > &other) const noexcept
Definition: standard.h:66
PKHash(const uint160 &hash)
Definition: standard.h:143
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:310
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
Definition: standard.cpp:16
CScriptID(const uint160 &in)
Definition: standard.h:91
BaseHash()
Definition: standard.h:28
WitnessV0ScriptHash(const uint256 &hash)
Definition: standard.h:166
256-bit opaque blob.
Definition: uint256.h:124
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:300
friend bool operator==(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:186
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
std::string ToString() const
Definition: standard.h:56
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
Only for Witness versions not already defined above.
size_t size() const
Definition: standard.h:76
160-bit opaque blob.
Definition: uint256.h:113
friend bool operator<(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:192
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:86
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:110
BaseHash(const HashType &in)
Definition: standard.h:29
unsigned nMaxDatacarrierBytes
Maximum size of TxoutType::NULL_DATA scripts that this node considers standard.
Definition: standard.cpp:17
bool operator==(const BaseHash< HashType > &other) const noexcept
Definition: standard.h:61
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:118
ScriptHash(const uint160 &hash)
Definition: standard.h:158
PKHash()
Definition: standard.h:142
unsigned char * end()
Definition: standard.h:41