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 #include <string>
15 
16 
17 static const bool DEFAULT_ACCEPT_DATACARRIER = true;
18 
19 class CKeyID;
20 class CScript;
21 struct ScriptHash;
22 
23 template<typename HashType>
24 class BaseHash
25 {
26 protected:
27  HashType m_hash;
28 
29 public:
30  BaseHash() : m_hash() {}
31  BaseHash(const HashType& in) : m_hash(in) {}
32 
33  unsigned char* begin()
34  {
35  return m_hash.begin();
36  }
37 
38  const unsigned char* begin() const
39  {
40  return m_hash.begin();
41  }
42 
43  unsigned char* end()
44  {
45  return m_hash.end();
46  }
47 
48  const unsigned char* end() const
49  {
50  return m_hash.end();
51  }
52 
53  operator std::vector<unsigned char>() const
54  {
55  return std::vector<unsigned char>{m_hash.begin(), m_hash.end()};
56  }
57 
58  std::string ToString() const
59  {
60  return m_hash.ToString();
61  }
62 
63  bool operator==(const BaseHash<HashType>& other) const noexcept
64  {
65  return m_hash == other.m_hash;
66  }
67 
68  bool operator!=(const BaseHash<HashType>& other) const noexcept
69  {
70  return !(m_hash == other.m_hash);
71  }
72 
73  bool operator<(const BaseHash<HashType>& other) const noexcept
74  {
75  return m_hash < other.m_hash;
76  }
77 
78  size_t size() const
79  {
80  return m_hash.size();
81  }
82 
83  unsigned char* data() { return m_hash.data(); }
84  const unsigned char* data() const { return m_hash.data(); }
85 };
86 
88 class CScriptID : public BaseHash<uint160>
89 {
90 public:
92  explicit CScriptID(const CScript& in);
93  explicit CScriptID(const uint160& in) : BaseHash(in) {}
94  explicit CScriptID(const ScriptHash& in);
95 };
96 
101 static const unsigned int MAX_OP_RETURN_RELAY = 83;
102 
107 extern bool fAcceptDatacarrier;
108 
110 extern unsigned nMaxDatacarrierBytes;
111 
121 
122 enum class TxoutType {
123  NONSTANDARD,
124  // 'standard' transaction types:
125  PUBKEY,
126  PUBKEYHASH,
127  SCRIPTHASH,
128  MULTISIG,
129  NULL_DATA,
133 };
134 
136 public:
137  friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
138  friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
139 };
140 
141 struct PKHash : public BaseHash<uint160>
142 {
143  PKHash() : BaseHash() {}
144  explicit PKHash(const uint160& hash) : BaseHash(hash) {}
145  explicit PKHash(const CPubKey& pubkey);
146  explicit PKHash(const CKeyID& pubkey_id);
147 };
148 CKeyID ToKeyID(const PKHash& key_hash);
149 
150 struct WitnessV0KeyHash;
151 struct ScriptHash : public BaseHash<uint160>
152 {
154  // These don't do what you'd expect.
155  // Use ScriptHash(GetScriptForDestination(...)) instead.
156  explicit ScriptHash(const WitnessV0KeyHash& hash) = delete;
157  explicit ScriptHash(const PKHash& hash) = delete;
158 
159  explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}
160  explicit ScriptHash(const CScript& script);
161  explicit ScriptHash(const CScriptID& script);
162 };
163 
164 struct WitnessV0ScriptHash : public BaseHash<uint256>
165 {
167  explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}
168  explicit WitnessV0ScriptHash(const CScript& script);
169 };
170 
171 struct WitnessV0KeyHash : public BaseHash<uint160>
172 {
174  explicit WitnessV0KeyHash(const uint160& hash) : BaseHash(hash) {}
175  explicit WitnessV0KeyHash(const CPubKey& pubkey);
176  explicit WitnessV0KeyHash(const PKHash& pubkey_hash);
177 };
178 CKeyID ToKeyID(const WitnessV0KeyHash& key_hash);
179 
182 {
183  unsigned int version;
184  unsigned int length;
185  unsigned char program[40];
186 
187  friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {
188  if (w1.version != w2.version) return false;
189  if (w1.length != w2.length) return false;
190  return std::equal(w1.program, w1.program + w1.length, w2.program);
191  }
192 
193  friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {
194  if (w1.version < w2.version) return true;
195  if (w1.version > w2.version) return false;
196  if (w1.length < w2.length) return true;
197  if (w1.length > w2.length) return false;
198  return std::lexicographical_compare(w1.program, w1.program + w1.length, w2.program, w2.program + w2.length);
199  }
200 };
201 
212 typedef boost::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown> CTxDestination;
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 
274 CScript GetScriptForWitness(const CScript& redeemscript);
275 
276 #endif // BITCOIN_SCRIPT_STANDARD_H
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:328
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:218
unsigned char * begin()
Definition: standard.h:33
const unsigned char * data() const
Definition: standard.h:84
WitnessV0KeyHash(const uint160 &hash)
Definition: standard.h:174
CScript GetScriptForRawPubKey(const CPubKey &pubkey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:300
unsigned char * data()
Definition: standard.h:83
unsigned char program[40]
Definition: standard.h:185
static const unsigned int MAX_OP_RETURN_RELAY
Default setting for nMaxDatacarrierBytes.
Definition: standard.h:101
ScriptHash()
Definition: standard.h:153
static const bool DEFAULT_ACCEPT_DATACARRIER
Definition: standard.h:17
HashType m_hash
Definition: standard.h:27
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:174
friend bool operator==(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:137
unsigned int version
Definition: standard.h:183
CScriptID()
Definition: standard.h:91
unsigned int length
Definition: standard.h:184
const unsigned char * begin() const
Definition: standard.h:38
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:48
friend bool operator<(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:138
CScript GetScriptForWitness(const CScript &redeemscript)
Generate a pay-to-witness script for the given redeem script.
Definition: standard.cpp:316
CTxDestination subtype to encode any future Witness version.
Definition: standard.h:181
TxoutType
Definition: standard.h:122
An encapsulated public key.
Definition: pubkey.h:30
bool operator!=(const BaseHash< HashType > &other) const noexcept
Definition: standard.h:68
PKHash(const uint160 &hash)
Definition: standard.h:144
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:305
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
Definition: standard.cpp:16
CScriptID(const uint160 &in)
Definition: standard.h:93
BaseHash()
Definition: standard.h:30
WitnessV0ScriptHash(const uint256 &hash)
Definition: standard.h:167
256-bit opaque blob.
Definition: uint256.h:123
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:295
friend bool operator==(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:187
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
std::string ToString() const
Definition: standard.h:58
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
Only for Witness versions not already defined above.
size_t size() const
Definition: standard.h:78
160-bit opaque blob.
Definition: uint256.h:112
friend bool operator<(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:193
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:88
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:109
BaseHash(const HashType &in)
Definition: standard.h:31
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:63
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:212
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:120
ScriptHash(const uint160 &hash)
Definition: standard.h:159
PKHash()
Definition: standard.h:143
unsigned char * end()
Definition: standard.h:43