Bitcoin Core  25.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-2022 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 
50 
51 enum class TxoutType {
53  // 'standard' transaction types:
54  PUBKEY,
55  PUBKEYHASH,
56  SCRIPTHASH,
57  MULTISIG,
58  NULL_DATA,
63 };
64 
66 public:
67  friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
68  friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
69 };
70 
71 struct PKHash : public BaseHash<uint160>
72 {
73  PKHash() : BaseHash() {}
74  explicit PKHash(const uint160& hash) : BaseHash(hash) {}
75  explicit PKHash(const CPubKey& pubkey);
76  explicit PKHash(const CKeyID& pubkey_id);
77 };
78 CKeyID ToKeyID(const PKHash& key_hash);
79 
80 struct WitnessV0KeyHash;
81 struct ScriptHash : public BaseHash<uint160>
82 {
84  // These don't do what you'd expect.
85  // Use ScriptHash(GetScriptForDestination(...)) instead.
86  explicit ScriptHash(const WitnessV0KeyHash& hash) = delete;
87  explicit ScriptHash(const PKHash& hash) = delete;
88 
89  explicit ScriptHash(const uint160& hash) : BaseHash(hash) {}
90  explicit ScriptHash(const CScript& script);
91  explicit ScriptHash(const CScriptID& script);
92 };
93 
94 struct WitnessV0ScriptHash : public BaseHash<uint256>
95 {
97  explicit WitnessV0ScriptHash(const uint256& hash) : BaseHash(hash) {}
98  explicit WitnessV0ScriptHash(const CScript& script);
99 };
100 
101 struct WitnessV0KeyHash : public BaseHash<uint160>
102 {
104  explicit WitnessV0KeyHash(const uint160& hash) : BaseHash(hash) {}
105  explicit WitnessV0KeyHash(const CPubKey& pubkey);
106  explicit WitnessV0KeyHash(const PKHash& pubkey_hash);
107 };
108 CKeyID ToKeyID(const WitnessV0KeyHash& key_hash);
109 
111 {
113  explicit WitnessV1Taproot(const XOnlyPubKey& xpk) : XOnlyPubKey(xpk) {}
114 };
115 
118 {
119  unsigned int version;
120  unsigned int length;
121  unsigned char program[40];
122 
123  friend bool operator==(const WitnessUnknown& w1, const WitnessUnknown& w2) {
124  if (w1.version != w2.version) return false;
125  if (w1.length != w2.length) return false;
126  return std::equal(w1.program, w1.program + w1.length, w2.program);
127  }
128 
129  friend bool operator<(const WitnessUnknown& w1, const WitnessUnknown& w2) {
130  if (w1.version < w2.version) return true;
131  if (w1.version > w2.version) return false;
132  if (w1.length < w2.length) return true;
133  if (w1.length > w2.length) return false;
134  return std::lexicographical_compare(w1.program, w1.program + w1.length, w2.program, w2.program + w2.length);
135  }
136 };
137 
149 using CTxDestination = std::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown>;
150 
152 bool IsValidDestination(const CTxDestination& dest);
153 
155 std::string GetTxnOutputType(TxoutType t);
156 
157 constexpr bool IsPushdataOp(opcodetype opcode)
158 {
159  return opcode > OP_FALSE && opcode <= OP_PUSHDATA4;
160 }
161 
172 TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
173 
179 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
180 
187 
189 CScript GetScriptForRawPubKey(const CPubKey& pubkey);
190 
193 std::optional<std::pair<int, std::vector<Span<const unsigned char>>>> MatchMultiA(const CScript& script LIFETIMEBOUND);
194 
196 CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
197 
199 {
200  bool operator()(const std::vector<unsigned char>& a, const std::vector<unsigned char>& b) const
201  {
202  if (a.size() < b.size()) return true;
203  if (a.size() > b.size()) return false;
204  return a < b;
205  }
206 };
207 
209 {
220  std::map<std::pair<std::vector<unsigned char>, int>, std::set<std::vector<unsigned char>, ShortestVectorFirstComparator>> scripts;
222  void Merge(TaprootSpendData other);
223 };
224 
227 {
228 private:
230  struct LeafInfo
231  {
232  std::vector<unsigned char> script;
234  std::vector<uint256> merkle_branch;
235  };
236 
238  struct NodeInfo
239  {
244  std::vector<LeafInfo> leaves;
245  };
247  bool m_valid = true;
248 
284  std::vector<std::optional<NodeInfo>> m_branch;
285 
288  bool m_parity;
289 
291  static NodeInfo Combine(NodeInfo&& a, NodeInfo&& b);
293  void Insert(NodeInfo&& node, int depth);
294 
295 public:
299  TaprootBuilder& Add(int depth, Span<const unsigned char> script, int leaf_version, bool track = true);
301  TaprootBuilder& AddOmitted(int depth, const uint256& hash);
304  TaprootBuilder& Finalize(const XOnlyPubKey& internal_key);
305 
307  bool IsValid() const { return m_valid; }
309  bool IsComplete() const { return m_valid && (m_branch.size() == 0 || (m_branch.size() == 1 && m_branch[0].has_value())); }
313  static bool ValidDepths(const std::vector<int>& depths);
317  std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>> GetTreeTuples() const;
319  bool HasScripts() const { return !m_branch.empty(); }
320 };
321 
328 std::optional<std::vector<std::tuple<int, std::vector<unsigned char>, int>>> InferTaprootTree(const TaprootSpendData& spenddata, const XOnlyPubKey& output);
329 
330 #endif // BITCOIN_SCRIPT_STANDARD_H
#define LIFETIMEBOUND
Definition: attributes.h:16
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:24
friend bool operator<(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:68
friend bool operator==(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:67
An encapsulated public key.
Definition: pubkey.h:34
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:27
CScriptID()
Definition: standard.h:29
CScriptID(const uint160 &in)
Definition: standard.h:31
Utility class to construct Taproot outputs from internal key and script tree.
Definition: standard.h:227
WitnessV1Taproot GetOutput()
Compute scriptPubKey (after Finalize()).
Definition: standard.cpp:475
static NodeInfo Combine(NodeInfo &&a, NodeInfo &&b)
Combine information about a parent Merkle tree node from its child nodes.
Definition: standard.cpp:360
TaprootSpendData GetSpendData() const
Compute spending data (after Finalize()).
Definition: standard.cpp:477
bool IsComplete() const
Return whether there were either no leaves, or the leaves form a Huffman tree.
Definition: standard.h:309
bool HasScripts() const
Returns true if there are any tapscripts.
Definition: standard.h:319
static bool ValidDepths(const std::vector< int > &depths)
Check if a list of depths is legal (will lead to IsComplete()).
Definition: standard.cpp:418
void Insert(NodeInfo &&node, int depth)
Insert information about a node at a certain depth, and propagate information up.
Definition: standard.cpp:392
XOnlyPubKey m_internal_key
The internal key, set when finalizing.
Definition: standard.h:286
XOnlyPubKey m_output_key
The output key, computed when finalizing.
Definition: standard.h:287
bool IsValid() const
Return true if so far all input was valid.
Definition: standard.h:307
TaprootBuilder & Add(int depth, Span< const unsigned char > script, int leaf_version, bool track=true)
Add a new script at a certain depth in the tree.
Definition: standard.cpp:441
std::vector< std::optional< NodeInfo > > m_branch
The current state of the builder.
Definition: standard.h:284
TaprootBuilder & AddOmitted(int depth, const uint256 &hash)
Like Add(), but for a Merkle node with a given hash to the tree.
Definition: standard.cpp:454
TaprootBuilder & Finalize(const XOnlyPubKey &internal_key)
Finalize the construction.
Definition: standard.cpp:464
bool m_parity
The tweak parity, computed when finalizing.
Definition: standard.h:288
std::vector< std::tuple< uint8_t, uint8_t, std::vector< unsigned char > > > GetTreeTuples() const
Returns a vector of tuples representing the depth, leaf version, and script.
Definition: standard.cpp:639
bool m_valid
Whether the builder is in a valid state so far.
Definition: standard.h:247
160-bit opaque blob.
Definition: uint256.h:95
256-bit opaque blob.
Definition: uint256.h:106
@ SCRIPT_VERIFY_P2SH
Definition: interpreter.h:47
Definition: init.h:25
opcodetype
Script opcodes.
Definition: script.h:70
@ OP_PUSHDATA4
Definition: script.h:76
@ OP_FALSE
Definition: script.h:73
constexpr bool IsPushdataOp(opcodetype opcode)
Definition: standard.h:157
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:49
static const unsigned int MAX_OP_RETURN_RELAY
Default setting for -datacarriersize.
Definition: standard.h:39
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:168
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:344
CScript GetScriptForRawPubKey(const CPubKey &pubkey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:339
std::optional< std::pair< int, std::vector< Span< const unsigned char > > > > MatchMultiA(const CScript &script LIFETIMEBOUND)
Determine if script is a "multi_a" script.
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:237
std::string GetTxnOutputType(TxoutType t)
Get the name of a TxoutType as a string.
Definition: standard.cpp:46
std::optional< std::vector< std::tuple< int, std::vector< unsigned char >, int > > > InferTaprootTree(const TaprootSpendData &spenddata, const XOnlyPubKey &output)
Given a TaprootSpendData and the output key, reconstruct its script tree.
Definition: standard.cpp:504
TxoutType
Definition: standard.h:51
@ 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:149
static const bool DEFAULT_ACCEPT_DATACARRIER
Definition: standard.h:19
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:356
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:334
CKeyID ToKeyID(const PKHash &key_hash)
Definition: standard.cpp:31
PKHash(const uint160 &hash)
Definition: standard.h:74
PKHash()
Definition: standard.h:73
ScriptHash(const WitnessV0KeyHash &hash)=delete
ScriptHash(const uint160 &hash)
Definition: standard.h:89
ScriptHash()
Definition: standard.h:83
ScriptHash(const PKHash &hash)=delete
bool operator()(const std::vector< unsigned char > &a, const std::vector< unsigned char > &b) const
Definition: standard.h:200
Information about a tracked leaf in the Merkle tree.
Definition: standard.h:231
std::vector< uint256 > merkle_branch
The hashing partners above this leaf.
Definition: standard.h:234
int leaf_version
The leaf version for that script.
Definition: standard.h:233
std::vector< unsigned char > script
The script.
Definition: standard.h:232
Information associated with a node in the Merkle tree.
Definition: standard.h:239
uint256 hash
Merkle hash of this node.
Definition: standard.h:241
std::vector< LeafInfo > leaves
Tracked leaves underneath this node (either from the node itself, or its children).
Definition: standard.h:244
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
Definition: standard.h:213
std::map< std::pair< std::vector< unsigned char >, int >, std::set< std::vector< unsigned char >, ShortestVectorFirstComparator > > scripts
Map from (script, leaf_version) to (sets of) control blocks.
Definition: standard.h:220
void Merge(TaprootSpendData other)
Merge other TaprootSpendData (for the same scriptPubKey) into this.
Definition: standard.cpp:377
XOnlyPubKey internal_key
The BIP341 internal key.
Definition: standard.h:211
CTxDestination subtype to encode any future Witness version.
Definition: standard.h:118
friend bool operator<(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:129
unsigned char program[40]
Definition: standard.h:121
unsigned int length
Definition: standard.h:120
unsigned int version
Definition: standard.h:119
friend bool operator==(const WitnessUnknown &w1, const WitnessUnknown &w2)
Definition: standard.h:123
WitnessV0KeyHash(const uint160 &hash)
Definition: standard.h:104
WitnessV0ScriptHash(const uint256 &hash)
Definition: standard.h:97
WitnessV1Taproot(const XOnlyPubKey &xpk)
Definition: standard.h:113