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
18static const bool DEFAULT_ACCEPT_DATACARRIER = true;
19
20class CKeyID;
21class CScript;
22struct ScriptHash;
23
25class CScriptID : public BaseHash<uint160>
26{
27public:
29 explicit CScriptID(const CScript& in);
30 explicit CScriptID(const uint160& in) : BaseHash(in) {}
31 explicit CScriptID(const ScriptHash& in);
32};
33
38static const unsigned int MAX_OP_RETURN_RELAY = 83;
39
44extern bool fAcceptDatacarrier;
45
47extern unsigned nMaxDatacarrierBytes;
48
58
59enum class TxoutType {
61 // 'standard' transaction types:
62 PUBKEY,
66 NULL_DATA,
71};
72
74public:
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
79struct PKHash : public BaseHash<uint160>
80{
82 explicit PKHash(const uint160& hash) : BaseHash(hash) {}
83 explicit PKHash(const CPubKey& pubkey);
84 explicit PKHash(const CKeyID& pubkey_id);
85};
86CKeyID ToKeyID(const PKHash& key_hash);
87
88struct WitnessV0KeyHash;
89struct 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
102struct WitnessV0ScriptHash : public BaseHash<uint256>
103{
105 explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}
106 explicit WitnessV0ScriptHash(const CScript& script);
107};
108
109struct 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};
116CKeyID 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
157using CTxDestination = std::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown>;
158
160bool IsValidDestination(const CTxDestination& dest);
161
163std::string GetTxnOutputType(TxoutType t);
164
175TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
176
182bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
183
190
193
195CScript 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{
227private:
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
294public:
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
323std::optional<std::vector<std::tuple<int, CScript, int>>> InferTaprootTree(const TaprootSpendData& spenddata, const XOnlyPubKey& output);
324
325#endif // BITCOIN_SCRIPT_STANDARD_H
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:23
friend bool operator<(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:76
friend bool operator==(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:75
An encapsulated public key.
Definition: pubkey.h:33
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:26
CScriptID()
Definition: standard.h:28
CScriptID(const uint160 &in)
Definition: standard.h:30
Utility class to construct Taproot outputs from internal key and script tree.
Definition: standard.h:226
WitnessV1Taproot GetOutput()
Compute scriptPubKey (after Finalize()).
Definition: standard.cpp:462
static NodeInfo Combine(NodeInfo &&a, NodeInfo &&b)
Combine information about a parent Merkle tree node from its child nodes.
Definition: standard.cpp:336
TaprootSpendData GetSpendData() const
Compute spending data (after Finalize()).
Definition: standard.cpp:464
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
bool IsComplete() const
Return whether there were either no leaves, or the leaves form a Huffman tree.
Definition: standard.h:308
static bool ValidDepths(const std::vector< int > &depths)
Check if a list of depths is legal (will lead to IsComplete()).
Definition: standard.cpp:405
void Insert(NodeInfo &&node, int depth)
Insert information about a node at a certain depth, and propagate information up.
Definition: standard.cpp:379
XOnlyPubKey m_internal_key
The internal key, set when finalizing.
Definition: standard.h:285
XOnlyPubKey m_output_key
The output key, computed when finalizing.
Definition: standard.h:286
bool IsValid() const
Return true if so far all input was valid.
Definition: standard.h:306
std::vector< std::optional< NodeInfo > > m_branch
The current state of the builder.
Definition: standard.h:283
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
TaprootBuilder & Finalize(const XOnlyPubKey &internal_key)
Finalize the construction.
Definition: standard.cpp:451
bool m_parity
The tweak parity, computed when finalizing.
Definition: standard.h:287
bool m_valid
Whether the builder is in a valid state so far.
Definition: standard.h:246
160-bit opaque blob.
Definition: uint256.h:113
256-bit opaque blob.
Definition: uint256.h:124
@ SCRIPT_VERIFY_P2SH
Definition: interpreter.h:46
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
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
static const unsigned int MAX_OP_RETURN_RELAY
Default setting for nMaxDatacarrierBytes.
Definition: standard.h:38
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
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
Definition: standard.cpp:19
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:320
CScript GetScriptForRawPubKey(const CPubKey &pubkey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:315
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:213
unsigned nMaxDatacarrierBytes
Maximum size of TxoutType::NULL_DATA scripts that this node considers standard.
Definition: standard.cpp:20
std::string GetTxnOutputType(TxoutType t)
Get the name of a TxoutType as a string.
Definition: standard.cpp:49
TxoutType
Definition: standard.h:59
@ WITNESS_V1_TAPROOT
@ WITNESS_UNKNOWN
Only for Witness versions not already defined above.
@ WITNESS_V0_SCRIPTHASH
@ NULL_DATA
unspendable OP_RETURN script that carries data
@ WITNESS_V0_KEYHASH
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
static const bool DEFAULT_ACCEPT_DATACARRIER
Definition: standard.h:18
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:332
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:310
CKeyID ToKeyID(const PKHash &key_hash)
Definition: standard.cpp:34
PKHash(const uint160 &hash)
Definition: standard.h:82
PKHash()
Definition: standard.h:81
ScriptHash(const WitnessV0KeyHash &hash)=delete
ScriptHash(const uint160 &hash)
Definition: standard.h:97
ScriptHash()
Definition: standard.h:91
ScriptHash(const PKHash &hash)=delete
bool operator()(const std::vector< unsigned char > &a, const std::vector< unsigned char > &b) const
Definition: standard.h:199
Information about a tracked leaf in the Merkle tree.
Definition: standard.h:230
std::vector< uint256 > merkle_branch
The hashing partners above this leaf.
Definition: standard.h:233
CScript script
The script.
Definition: standard.h:231
int leaf_version
The leaf version for that script.
Definition: standard.h:232
Information associated with a node in the Merkle tree.
Definition: standard.h:238
uint256 hash
Merkle hash of this node.
Definition: standard.h:240
std::vector< LeafInfo > leaves
Tracked leaves underneath this node (either from the node itself, or its children).
Definition: standard.h:243
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
Definition: standard.h:212
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
void Merge(TaprootSpendData other)
Merge other TaprootSpendData (for the same scriptPubKey) into this.
Definition: standard.cpp:358
XOnlyPubKey internal_key
The BIP341 internal key.
Definition: standard.h:210
CTxDestination subtype to encode any future Witness version.
Definition: standard.h:126
friend bool operator<(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:137
unsigned char program[40]
Definition: standard.h:129
unsigned int length
Definition: standard.h:128
unsigned int version
Definition: standard.h:127
friend bool operator==(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:131
WitnessV0KeyHash(const uint160 &hash)
Definition: standard.h:112
WitnessV0ScriptHash(const uint256 &hash)
Definition: standard.h:105
WitnessV1Taproot(const XOnlyPubKey &xpk)
Definition: standard.h:121