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,
134 };
135 
137 public:
138  friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
139  friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
140 };
141 
142 struct PKHash : public BaseHash<uint160>
143 {
144  PKHash() : BaseHash() {}
145  explicit PKHash(const uint160& hash) : BaseHash(hash) {}
146  explicit PKHash(const CPubKey& pubkey);
147  explicit PKHash(const CKeyID& pubkey_id);
148 };
149 CKeyID ToKeyID(const PKHash& key_hash);
150 
151 struct WitnessV0KeyHash;
152 struct ScriptHash : public BaseHash<uint160>
153 {
155  // These don't do what you'd expect.
156  // Use ScriptHash(GetScriptForDestination(...)) instead.
157  explicit ScriptHash(const WitnessV0KeyHash& hash) = delete;
158  explicit ScriptHash(const PKHash& hash) = delete;
159 
160  explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}
161  explicit ScriptHash(const CScript& script);
162  explicit ScriptHash(const CScriptID& script);
163 };
164 
165 struct WitnessV0ScriptHash : public BaseHash<uint256>
166 {
168  explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}
169  explicit WitnessV0ScriptHash(const CScript& script);
170 };
171 
172 struct WitnessV0KeyHash : public BaseHash<uint160>
173 {
175  explicit WitnessV0KeyHash(const uint160& hash) : BaseHash(hash) {}
176  explicit WitnessV0KeyHash(const CPubKey& pubkey);
177  explicit WitnessV0KeyHash(const PKHash& pubkey_hash);
178 };
179 CKeyID ToKeyID(const WitnessV0KeyHash& key_hash);
180 
183 {
184  unsigned int version;
185  unsigned int length;
186  unsigned char program[40];
187 
188  friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {
189  if (w1.version != w2.version) return false;
190  if (w1.length != w2.length) return false;
191  return std::equal(w1.program, w1.program + w1.length, w2.program);
192  }
193 
194  friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {
195  if (w1.version < w2.version) return true;
196  if (w1.version > w2.version) return false;
197  if (w1.length < w2.length) return true;
198  if (w1.length > w2.length) return false;
199  return std::lexicographical_compare(w1.program, w1.program + w1.length, w2.program, w2.program + w2.length);
200  }
201 };
202 
214 typedef boost::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown> CTxDestination;
215 
217 bool IsValidDestination(const CTxDestination& dest);
218 
220 std::string GetTxnOutputType(TxoutType t);
221 
232 TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
233 
240 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
241 
253 bool ExtractDestinations(const CScript& scriptPubKey, TxoutType& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
254 
261 
263 CScript GetScriptForRawPubKey(const CPubKey& pubkey);
264 
266 CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
267 
268 #endif // BITCOIN_SCRIPT_STANDARD_H
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:322
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:33
const unsigned char * data() const
Definition: standard.h:84
WitnessV0KeyHash(const uint160 &hash)
Definition: standard.h:175
CScript GetScriptForRawPubKey(const CPubKey &pubkey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:306
unsigned char * data()
Definition: standard.h:83
unsigned char program[40]
Definition: standard.h:186
static const unsigned int MAX_OP_RETURN_RELAY
Default setting for nMaxDatacarrierBytes.
Definition: standard.h:101
ScriptHash()
Definition: standard.h:154
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:180
friend bool operator==(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:138
unsigned int version
Definition: standard.h:184
CScriptID()
Definition: standard.h:91
unsigned int length
Definition: standard.h:185
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:139
CTxDestination subtype to encode any future Witness version.
Definition: standard.h:182
TxoutType
Definition: standard.h:122
An encapsulated public key.
Definition: pubkey.h:31
bool operator!=(const BaseHash< HashType > &other) const noexcept
Definition: standard.h:68
PKHash(const uint160 &hash)
Definition: standard.h:145
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:311
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:168
256-bit opaque blob.
Definition: uint256.h:124
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:301
friend bool operator==(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:188
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
std::string ToString() const
Definition: standard.h:58
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:78
160-bit opaque blob.
Definition: uint256.h:113
friend bool operator<(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:194
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:110
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:214
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:160
PKHash()
Definition: standard.h:144
unsigned char * end()
Definition: standard.h:43