Bitcoin Core  22.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 <pubkey.h>
10 #include <script/interpreter.h>
11 #include <uint256.h>
12 #include <util/hash_type.h>
13 
14 #include <map>
15 #include <string>
16 #include <variant>
17 
18 static const bool DEFAULT_ACCEPT_DATACARRIER = true;
19 
20 class CKeyID;
21 class CScript;
22 struct ScriptHash;
23 
25 class CScriptID : public BaseHash<uint160>
26 {
27 public:
29  explicit CScriptID(const CScript& in);
30  explicit CScriptID(const uint160& in) : BaseHash(in) {}
31  explicit CScriptID(const ScriptHash& in);
32 };
33 
38 static const unsigned int MAX_OP_RETURN_RELAY = 83;
39 
44 extern bool fAcceptDatacarrier;
45 
47 extern unsigned nMaxDatacarrierBytes;
48 
58 
59 enum class TxoutType {
61  // 'standard' transaction types:
62  PUBKEY,
63  PUBKEYHASH,
64  SCRIPTHASH,
65  MULTISIG,
66  NULL_DATA,
71 };
72 
74 public:
75  friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
76  friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
77 };
78 
79 struct PKHash : public BaseHash<uint160>
80 {
81  PKHash() : BaseHash() {}
82  explicit PKHash(const uint160& hash) : BaseHash(hash) {}
83  explicit PKHash(const CPubKey& pubkey);
84  explicit PKHash(const CKeyID& pubkey_id);
85 };
86 CKeyID ToKeyID(const PKHash& key_hash);
87 
88 struct WitnessV0KeyHash;
89 struct ScriptHash : public BaseHash<uint160>
90 {
92  // These don't do what you'd expect.
93  // Use ScriptHash(GetScriptForDestination(...)) instead.
94  explicit ScriptHash(const WitnessV0KeyHash& hash) = delete;
95  explicit ScriptHash(const PKHash& hash) = delete;
96 
97  explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}
98  explicit ScriptHash(const CScript& script);
99  explicit ScriptHash(const CScriptID& script);
100 };
101 
102 struct WitnessV0ScriptHash : public BaseHash<uint256>
103 {
105  explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}
106  explicit WitnessV0ScriptHash(const CScript& script);
107 };
108 
109 struct WitnessV0KeyHash : public BaseHash<uint160>
110 {
112  explicit WitnessV0KeyHash(const uint160& hash) : BaseHash(hash) {}
113  explicit WitnessV0KeyHash(const CPubKey& pubkey);
114  explicit WitnessV0KeyHash(const PKHash& pubkey_hash);
115 };
116 CKeyID ToKeyID(const WitnessV0KeyHash& key_hash);
117 
119 {
121  explicit WitnessV1Taproot(const XOnlyPubKey& xpk) : XOnlyPubKey(xpk) {}
122 };
123 
126 {
127  unsigned int version;
128  unsigned int length;
129  unsigned char program[40];
130 
131  friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {
132  if (w1.version != w2.version) return false;
133  if (w1.length != w2.length) return false;
134  return std::equal(w1.program, w1.program + w1.length, w2.program);
135  }
136 
137  friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {
138  if (w1.version < w2.version) return true;
139  if (w1.version > w2.version) return false;
140  if (w1.length < w2.length) return true;
141  if (w1.length > w2.length) return false;
142  return std::lexicographical_compare(w1.program, w1.program + w1.length, w2.program, w2.program + w2.length);
143  }
144 };
145 
157 using CTxDestination = std::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown>;
158 
160 bool IsValidDestination(const CTxDestination& dest);
161 
163 std::string GetTxnOutputType(TxoutType t);
164 
175 TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
176 
182 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
183 
190 
192 CScript GetScriptForRawPubKey(const CPubKey& pubkey);
193 
195 CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
196 
198 {
199  bool operator()(const std::vector<unsigned char>& a, const std::vector<unsigned char>& b) const
200  {
201  if (a.size() < b.size()) return true;
202  if (a.size() > b.size()) return false;
203  return a < b;
204  }
205 };
206 
208 {
219  std::map<std::pair<CScript, int>, std::set<std::vector<unsigned char>, ShortestVectorFirstComparator>> scripts;
221  void Merge(TaprootSpendData other);
222 };
223 
226 {
227 private:
229  struct LeafInfo
230  {
233  std::vector<uint256> merkle_branch;
234  };
235 
237  struct NodeInfo
238  {
243  std::vector<LeafInfo> leaves;
244  };
246  bool m_valid = true;
247 
283  std::vector<std::optional<NodeInfo>> m_branch;
284 
287  bool m_parity;
288 
290  static NodeInfo Combine(NodeInfo&& a, NodeInfo&& b);
292  void Insert(NodeInfo&& node, int depth);
293 
294 public:
298  TaprootBuilder& Add(int depth, const CScript& script, int leaf_version, bool track = true);
300  TaprootBuilder& AddOmitted(int depth, const uint256& hash);
303  TaprootBuilder& Finalize(const XOnlyPubKey& internal_key);
304 
306  bool IsValid() const { return m_valid; }
308  bool IsComplete() const { return m_valid && (m_branch.size() == 0 || (m_branch.size() == 1 && m_branch[0].has_value())); }
312  static bool ValidDepths(const std::vector<int>& depths);
315 };
316 
323 std::optional<std::vector<std::tuple<int, CScript, int>>> InferTaprootTree(const TaprootSpendData& spenddata, const XOnlyPubKey& output);
324 
325 #endif // BITCOIN_SCRIPT_STANDARD_H
WitnessV0KeyHash::WitnessV0KeyHash
WitnessV0KeyHash(const uint160 &hash)
Definition: standard.h:112
InferTaprootTree
std::optional< std::vector< std::tuple< int, CScript, int > > > InferTaprootTree(const TaprootSpendData &spenddata, const XOnlyPubKey &output)
Given a TaprootSpendData and the output key, reconstruct its script tree.
Definition: standard.cpp:490
WitnessUnknown
CTxDestination subtype to encode any future Witness version.
Definition: standard.h:125
ScriptHash::ScriptHash
ScriptHash()
Definition: standard.h:91
WitnessV1Taproot::WitnessV1Taproot
WitnessV1Taproot(const XOnlyPubKey &xpk)
Definition: standard.h:121
TaprootSpendData::scripts
std::map< std::pair< CScript, int >, std::set< std::vector< unsigned char >, ShortestVectorFirstComparator > > scripts
Map from (script, leaf_version) to (sets of) control blocks.
Definition: standard.h:219
TxoutType
TxoutType
Definition: standard.h:59
TxoutType::NONSTANDARD
@ NONSTANDARD
TxoutType::WITNESS_UNKNOWN
@ WITNESS_UNKNOWN
Only for Witness versions not already defined above.
WitnessUnknown::version
unsigned int version
Definition: standard.h:127
uint256.h
hash_type.h
TaprootBuilder::GetSpendData
TaprootSpendData GetSpendData() const
Compute spending data (after Finalize()).
Definition: standard.cpp:464
WitnessUnknown::length
unsigned int length
Definition: standard.h:128
TaprootBuilder::Insert
void Insert(NodeInfo &&node, int depth)
Insert information about a node at a certain depth, and propagate information up.
Definition: standard.cpp:379
TaprootBuilder::NodeInfo
Information associated with a node in the Merkle tree.
Definition: standard.h:237
interpreter.h
XOnlyPubKey
Definition: pubkey.h:220
nMaxDatacarrierBytes
unsigned nMaxDatacarrierBytes
Maximum size of TxoutType::NULL_DATA scripts that this node considers standard.
Definition: standard.cpp:20
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
CNoDestination::operator==
friend bool operator==(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:75
pubkey.h
BaseHash
Definition: hash_type.h:9
GetTxnOutputType
std::string GetTxnOutputType(TxoutType t)
Get the name of a TxoutType as a string.
Definition: standard.cpp:49
TxoutType::WITNESS_V1_TAPROOT
@ WITNESS_V1_TAPROOT
WitnessV0KeyHash
Definition: standard.h:109
ToKeyID
CKeyID ToKeyID(const PKHash &key_hash)
Definition: standard.cpp:34
TaprootBuilder::LeafInfo::script
CScript script
The script.
Definition: standard.h:231
GetScriptForRawPubKey
CScript GetScriptForRawPubKey(const CPubKey &pubkey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:315
GetScriptForMultisig
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:320
TxoutType::WITNESS_V0_SCRIPTHASH
@ WITNESS_V0_SCRIPTHASH
TxoutType::PUBKEY
@ PUBKEY
CTxDestination
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
TaprootBuilder::GetOutput
WitnessV1Taproot GetOutput()
Compute scriptPubKey (after Finalize()).
Definition: standard.cpp:462
TaprootSpendData
Definition: standard.h:207
CNoDestination::operator<
friend bool operator<(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:76
TaprootBuilder::m_valid
bool m_valid
Whether the builder is in a valid state so far.
Definition: standard.h:246
TaprootBuilder::NodeInfo::leaves
std::vector< LeafInfo > leaves
Tracked leaves underneath this node (either from the node itself, or its children).
Definition: standard.h:243
ScriptHash::ScriptHash
ScriptHash(const uint160 &hash)
Definition: standard.h:97
Solver
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:144
TaprootSpendData::merkle_root
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
Definition: standard.h:212
TaprootBuilder::Add
TaprootBuilder & Add(int depth, const CScript &script, int leaf_version, bool track=true)
Add a new script at a certain depth in the tree.
Definition: standard.cpp:428
ShortestVectorFirstComparator::operator()
bool operator()(const std::vector< unsigned char > &a, const std::vector< unsigned char > &b) const
Definition: standard.h:199
TaprootBuilder::LeafInfo::leaf_version
int leaf_version
The leaf version for that script.
Definition: standard.h:232
TxoutType::SCRIPTHASH
@ SCRIPTHASH
uint256
256-bit opaque blob.
Definition: uint256.h:124
TaprootBuilder::NodeInfo::hash
uint256 hash
Merkle hash of this node.
Definition: standard.h:240
TaprootBuilder::m_branch
std::vector< std::optional< NodeInfo > > m_branch
The current state of the builder.
Definition: standard.h:283
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
TaprootBuilder::m_output_key
XOnlyPubKey m_output_key
The output key, computed when finalizing.
Definition: standard.h:286
TaprootBuilder::ValidDepths
static bool ValidDepths(const std::vector< int > &depths)
Check if a list of depths is legal (will lead to IsComplete()).
Definition: standard.cpp:405
TaprootBuilder::IsComplete
bool IsComplete() const
Return whether there were either no leaves, or the leaves form a Huffman tree.
Definition: standard.h:308
PKHash::PKHash
PKHash(const uint160 &hash)
Definition: standard.h:82
CNoDestination
Definition: standard.h:73
TaprootSpendData::internal_key
XOnlyPubKey internal_key
The BIP341 internal key.
Definition: standard.h:210
TxoutType::NULL_DATA
@ NULL_DATA
unspendable OP_RETURN script that carries data
TxoutType::PUBKEYHASH
@ PUBKEYHASH
PKHash
Definition: standard.h:79
WitnessUnknown::operator<
friend bool operator<(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:137
uint160
160-bit opaque blob.
Definition: uint256.h:113
CPubKey
An encapsulated public key.
Definition: pubkey.h:32
MAX_OP_RETURN_RELAY
static const unsigned int MAX_OP_RETURN_RELAY
Default setting for nMaxDatacarrierBytes.
Definition: standard.h:38
CScriptID::CScriptID
CScriptID(const uint160 &in)
Definition: standard.h:30
TaprootBuilder::LeafInfo::merkle_branch
std::vector< uint256 > merkle_branch
The hashing partners above this leaf.
Definition: standard.h:233
PKHash::PKHash
PKHash()
Definition: standard.h:81
ShortestVectorFirstComparator
Definition: standard.h:197
TaprootBuilder::m_internal_key
XOnlyPubKey m_internal_key
The internal key, set when finalizing.
Definition: standard.h:285
SCRIPT_VERIFY_P2SH
@ SCRIPT_VERIFY_P2SH
Definition: interpreter.h:46
WitnessV0ScriptHash::WitnessV0ScriptHash
WitnessV0ScriptHash(const uint256 &hash)
Definition: standard.h:105
WitnessV1Taproot::WitnessV1Taproot
WitnessV1Taproot()
Definition: standard.h:120
TaprootBuilder::LeafInfo
Information about a tracked leaf in the Merkle tree.
Definition: standard.h:229
TxoutType::MULTISIG
@ MULTISIG
TaprootBuilder::m_parity
bool m_parity
The tweak parity, computed when finalizing.
Definition: standard.h:287
TaprootBuilder::IsValid
bool IsValid() const
Return true if so far all input was valid.
Definition: standard.h:306
node
Definition: interfaces.cpp:68
IsValidDestination
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:332
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:310
TaprootBuilder::Finalize
TaprootBuilder & Finalize(const XOnlyPubKey &internal_key)
Finalize the construction.
Definition: standard.cpp:451
WitnessUnknown::operator==
friend bool operator==(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:131
TaprootBuilder
Utility class to construct Taproot outputs from internal key and script tree.
Definition: standard.h:225
TxoutType::WITNESS_V0_KEYHASH
@ WITNESS_V0_KEYHASH
WitnessV0ScriptHash
Definition: standard.h:102
CScriptID
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:25
WitnessV0KeyHash::WitnessV0KeyHash
WitnessV0KeyHash()
Definition: standard.h:111
CScriptID::CScriptID
CScriptID()
Definition: standard.h:28
WitnessV0ScriptHash::WitnessV0ScriptHash
WitnessV0ScriptHash()
Definition: standard.h:104
TaprootBuilder::AddOmitted
TaprootBuilder & AddOmitted(int depth, const uint256 &hash)
Like Add(), but for a Merkle node with a given hash to the tree.
Definition: standard.cpp:441
ExtractDestination
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:213
TaprootBuilder::Combine
static NodeInfo Combine(NodeInfo &&a, NodeInfo &&b)
Combine information about a parent Merkle tree node from its child nodes.
Definition: standard.cpp:336
ScriptHash
Definition: standard.h:89
MANDATORY_SCRIPT_VERIFY_FLAGS
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:57
fAcceptDatacarrier
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
Definition: standard.cpp:19
TaprootSpendData::Merge
void Merge(TaprootSpendData other)
Merge other TaprootSpendData (for the same scriptPubKey) into this.
Definition: standard.cpp:358
WitnessUnknown::program
unsigned char program[40]
Definition: standard.h:129
DEFAULT_ACCEPT_DATACARRIER
static const bool DEFAULT_ACCEPT_DATACARRIER
Definition: standard.h:18
WitnessV1Taproot
Definition: standard.h:118