Bitcoin Core  23.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-2021 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 <attributes.h>
10 #include <pubkey.h>
11 #include <script/interpreter.h>
12 #include <uint256.h>
13 #include <util/hash_type.h>
14 
15 #include <map>
16 #include <string>
17 #include <variant>
18 
19 static const bool DEFAULT_ACCEPT_DATACARRIER = true;
20 
21 class CKeyID;
22 class CScript;
23 struct ScriptHash;
24 
26 class CScriptID : public BaseHash<uint160>
27 {
28 public:
30  explicit CScriptID(const CScript& in);
31  explicit CScriptID(const uint160& in) : BaseHash(in) {}
32  explicit CScriptID(const ScriptHash& in);
33 };
34 
39 static const unsigned int MAX_OP_RETURN_RELAY = 83;
40 
45 extern bool fAcceptDatacarrier;
46 
48 extern unsigned nMaxDatacarrierBytes;
49 
59 
60 enum class TxoutType {
62  // 'standard' transaction types:
63  PUBKEY,
64  PUBKEYHASH,
65  SCRIPTHASH,
66  MULTISIG,
67  NULL_DATA,
72 };
73 
75 public:
76  friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
77  friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
78 };
79 
80 struct PKHash : public BaseHash<uint160>
81 {
82  PKHash() : BaseHash() {}
83  explicit PKHash(const uint160& hash) : BaseHash(hash) {}
84  explicit PKHash(const CPubKey& pubkey);
85  explicit PKHash(const CKeyID& pubkey_id);
86 };
87 CKeyID ToKeyID(const PKHash& key_hash);
88 
89 struct WitnessV0KeyHash;
90 struct ScriptHash : public BaseHash<uint160>
91 {
93  // These don't do what you'd expect.
94  // Use ScriptHash(GetScriptForDestination(...)) instead.
95  explicit ScriptHash(const WitnessV0KeyHash& hash) = delete;
96  explicit ScriptHash(const PKHash& hash) = delete;
97 
98  explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}
99  explicit ScriptHash(const CScript& script);
100  explicit ScriptHash(const CScriptID& script);
101 };
102 
103 struct WitnessV0ScriptHash : public BaseHash<uint256>
104 {
106  explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}
107  explicit WitnessV0ScriptHash(const CScript& script);
108 };
109 
110 struct WitnessV0KeyHash : public BaseHash<uint160>
111 {
113  explicit WitnessV0KeyHash(const uint160& hash) : BaseHash(hash) {}
114  explicit WitnessV0KeyHash(const CPubKey& pubkey);
115  explicit WitnessV0KeyHash(const PKHash& pubkey_hash);
116 };
117 CKeyID ToKeyID(const WitnessV0KeyHash& key_hash);
118 
120 {
122  explicit WitnessV1Taproot(const XOnlyPubKey& xpk) : XOnlyPubKey(xpk) {}
123 };
124 
127 {
128  unsigned int version;
129  unsigned int length;
130  unsigned char program[40];
131 
132  friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {
133  if (w1.version != w2.version) return false;
134  if (w1.length != w2.length) return false;
135  return std::equal(w1.program, w1.program + w1.length, w2.program);
136  }
137 
138  friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {
139  if (w1.version < w2.version) return true;
140  if (w1.version > w2.version) return false;
141  if (w1.length < w2.length) return true;
142  if (w1.length > w2.length) return false;
143  return std::lexicographical_compare(w1.program, w1.program + w1.length, w2.program, w2.program + w2.length);
144  }
145 };
146 
158 using CTxDestination = std::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown>;
159 
161 bool IsValidDestination(const CTxDestination& dest);
162 
164 std::string GetTxnOutputType(TxoutType t);
165 
166 constexpr bool IsPushdataOp(opcodetype opcode)
167 {
168  return opcode > OP_FALSE && opcode <= OP_PUSHDATA4;
169 }
170 
181 TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
182 
188 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
189 
196 
198 CScript GetScriptForRawPubKey(const CPubKey& pubkey);
199 
202 std::optional<std::pair<int, std::vector<Span<const unsigned char>>>> MatchMultiA(const CScript& script LIFETIMEBOUND);
203 
205 CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
206 
208 {
209  bool operator()(const std::vector<unsigned char>& a, const std::vector<unsigned char>& b) const
210  {
211  if (a.size() < b.size()) return true;
212  if (a.size() > b.size()) return false;
213  return a < b;
214  }
215 };
216 
218 {
229  std::map<std::pair<CScript, int>, std::set<std::vector<unsigned char>, ShortestVectorFirstComparator>> scripts;
231  void Merge(TaprootSpendData other);
232 };
233 
236 {
237 private:
239  struct LeafInfo
240  {
243  std::vector<uint256> merkle_branch;
244  };
245 
247  struct NodeInfo
248  {
253  std::vector<LeafInfo> leaves;
254  };
256  bool m_valid = true;
257 
293  std::vector<std::optional<NodeInfo>> m_branch;
294 
297  bool m_parity;
298 
300  static NodeInfo Combine(NodeInfo&& a, NodeInfo&& b);
302  void Insert(NodeInfo&& node, int depth);
303 
304 public:
308  TaprootBuilder& Add(int depth, const CScript& script, int leaf_version, bool track = true);
310  TaprootBuilder& AddOmitted(int depth, const uint256& hash);
313  TaprootBuilder& Finalize(const XOnlyPubKey& internal_key);
314 
316  bool IsValid() const { return m_valid; }
318  bool IsComplete() const { return m_valid && (m_branch.size() == 0 || (m_branch.size() == 1 && m_branch[0].has_value())); }
322  static bool ValidDepths(const std::vector<int>& depths);
325 };
326 
333 std::optional<std::vector<std::tuple<int, CScript, int>>> InferTaprootTree(const TaprootSpendData& spenddata, const XOnlyPubKey& output);
334 
335 #endif // BITCOIN_SCRIPT_STANDARD_H
WitnessV0KeyHash::WitnessV0KeyHash
WitnessV0KeyHash(const uint160 &hash)
Definition: standard.h:113
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:511
WitnessUnknown
CTxDestination subtype to encode any future Witness version.
Definition: standard.h:126
ScriptHash::ScriptHash
ScriptHash()
Definition: standard.h:92
WitnessV1Taproot::WitnessV1Taproot
WitnessV1Taproot(const XOnlyPubKey &xpk)
Definition: standard.h:122
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:229
TxoutType
TxoutType
Definition: standard.h:60
TxoutType::NONSTANDARD
@ NONSTANDARD
TxoutType::WITNESS_UNKNOWN
@ WITNESS_UNKNOWN
Only for Witness versions not already defined above.
WitnessUnknown::version
unsigned int version
Definition: standard.h:128
MatchMultiA
std::optional< std::pair< int, std::vector< Span< const unsigned char > > > > MatchMultiA(const CScript &script LIFETIMEBOUND)
Determine if script is a "multi_a" script.
uint256.h
hash_type.h
TaprootBuilder::GetSpendData
TaprootSpendData GetSpendData() const
Compute spending data (after Finalize()).
Definition: standard.cpp:485
WitnessUnknown::length
unsigned int length
Definition: standard.h:129
TaprootBuilder::Insert
void Insert(NodeInfo &&node, int depth)
Insert information about a node at a certain depth, and propagate information up.
Definition: standard.cpp:400
TaprootBuilder::NodeInfo
Information associated with a node in the Merkle tree.
Definition: standard.h:247
interpreter.h
opcodetype
opcodetype
Script opcodes.
Definition: script.h:69
XOnlyPubKey
Definition: pubkey.h:224
IsPushdataOp
constexpr bool IsPushdataOp(opcodetype opcode)
Definition: standard.h:166
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:23
CNoDestination::operator==
friend bool operator==(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:76
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:110
OP_FALSE
@ OP_FALSE
Definition: script.h:73
ToKeyID
CKeyID ToKeyID(const PKHash &key_hash)
Definition: standard.cpp:34
TaprootBuilder::LeafInfo::script
CScript script
The script.
Definition: standard.h:241
GetScriptForRawPubKey
CScript GetScriptForRawPubKey(const CPubKey &pubkey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:342
GetScriptForMultisig
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:347
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:158
TaprootBuilder::GetOutput
WitnessV1Taproot GetOutput()
Compute scriptPubKey (after Finalize()).
Definition: standard.cpp:483
TaprootSpendData
Definition: standard.h:217
CNoDestination::operator<
friend bool operator<(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:77
TaprootBuilder::m_valid
bool m_valid
Whether the builder is in a valid state so far.
Definition: standard.h:256
TaprootBuilder::NodeInfo::leaves
std::vector< LeafInfo > leaves
Tracked leaves underneath this node (either from the node itself, or its children).
Definition: standard.h:253
ScriptHash::ScriptHash
ScriptHash(const uint160 &hash)
Definition: standard.h:98
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:171
TaprootSpendData::merkle_root
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
Definition: standard.h:222
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:449
ShortestVectorFirstComparator::operator()
bool operator()(const std::vector< unsigned char > &a, const std::vector< unsigned char > &b) const
Definition: standard.h:209
TaprootBuilder::LeafInfo::leaf_version
int leaf_version
The leaf version for that script.
Definition: standard.h:242
TxoutType::SCRIPTHASH
@ SCRIPTHASH
uint256
256-bit opaque blob.
Definition: uint256.h:126
TaprootBuilder::NodeInfo::hash
uint256 hash
Merkle hash of this node.
Definition: standard.h:250
TaprootBuilder::m_branch
std::vector< std::optional< NodeInfo > > m_branch
The current state of the builder.
Definition: standard.h:293
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:410
TaprootBuilder::m_output_key
XOnlyPubKey m_output_key
The output key, computed when finalizing.
Definition: standard.h:296
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:426
TaprootBuilder::IsComplete
bool IsComplete() const
Return whether there were either no leaves, or the leaves form a Huffman tree.
Definition: standard.h:318
PKHash::PKHash
PKHash(const uint160 &hash)
Definition: standard.h:83
CNoDestination
Definition: standard.h:74
TaprootSpendData::internal_key
XOnlyPubKey internal_key
The BIP341 internal key.
Definition: standard.h:220
TxoutType::NULL_DATA
@ NULL_DATA
unspendable OP_RETURN script that carries data
attributes.h
TxoutType::PUBKEYHASH
@ PUBKEYHASH
PKHash
Definition: standard.h:80
WitnessUnknown::operator<
friend bool operator<(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:138
uint160
160-bit opaque blob.
Definition: uint256.h:115
CPubKey
An encapsulated public key.
Definition: pubkey.h:33
MAX_OP_RETURN_RELAY
static const unsigned int MAX_OP_RETURN_RELAY
Default setting for nMaxDatacarrierBytes.
Definition: standard.h:39
CScriptID::CScriptID
CScriptID(const uint160 &in)
Definition: standard.h:31
LIFETIMEBOUND
#define LIFETIMEBOUND
Definition: attributes.h:16
TaprootBuilder::LeafInfo::merkle_branch
std::vector< uint256 > merkle_branch
The hashing partners above this leaf.
Definition: standard.h:243
PKHash::PKHash
PKHash()
Definition: standard.h:82
ShortestVectorFirstComparator
Definition: standard.h:207
TaprootBuilder::m_internal_key
XOnlyPubKey m_internal_key
The internal key, set when finalizing.
Definition: standard.h:295
SCRIPT_VERIFY_P2SH
@ SCRIPT_VERIFY_P2SH
Definition: interpreter.h:47
WitnessV0ScriptHash::WitnessV0ScriptHash
WitnessV0ScriptHash(const uint256 &hash)
Definition: standard.h:106
WitnessV1Taproot::WitnessV1Taproot
WitnessV1Taproot()
Definition: standard.h:121
TaprootBuilder::LeafInfo
Information about a tracked leaf in the Merkle tree.
Definition: standard.h:239
TxoutType::MULTISIG
@ MULTISIG
TaprootBuilder::m_parity
bool m_parity
The tweak parity, computed when finalizing.
Definition: standard.h:297
TaprootBuilder::IsValid
bool IsValid() const
Return true if so far all input was valid.
Definition: standard.h:316
node
Definition: init.h:22
IsValidDestination
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:359
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:337
TaprootBuilder::Finalize
TaprootBuilder & Finalize(const XOnlyPubKey &internal_key)
Finalize the construction.
Definition: standard.cpp:472
WitnessUnknown::operator==
friend bool operator==(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:132
TaprootBuilder
Utility class to construct Taproot outputs from internal key and script tree.
Definition: standard.h:235
TxoutType::WITNESS_V0_KEYHASH
@ WITNESS_V0_KEYHASH
WitnessV0ScriptHash
Definition: standard.h:103
CScriptID
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:26
WitnessV0KeyHash::WitnessV0KeyHash
WitnessV0KeyHash()
Definition: standard.h:112
CScriptID::CScriptID
CScriptID()
Definition: standard.h:29
OP_PUSHDATA4
@ OP_PUSHDATA4
Definition: script.h:76
WitnessV0ScriptHash::WitnessV0ScriptHash
WitnessV0ScriptHash()
Definition: standard.h:105
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:462
ExtractDestination
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:240
TaprootBuilder::Combine
static NodeInfo Combine(NodeInfo &&a, NodeInfo &&b)
Combine information about a parent Merkle tree node from its child nodes.
Definition: standard.cpp:363
ScriptHash
Definition: standard.h:90
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:58
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:385
WitnessUnknown::program
unsigned char program[40]
Definition: standard.h:130
DEFAULT_ACCEPT_DATACARRIER
static const bool DEFAULT_ACCEPT_DATACARRIER
Definition: standard.h:19
WitnessV1Taproot
Definition: standard.h:119