Line data Source code
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_SCRIPT_H
7 : #define BITCOIN_SCRIPT_SCRIPT_H
8 :
9 : #include <attributes.h>
10 : #include <crypto/common.h>
11 : #include <prevector.h>
12 : #include <serialize.h>
13 : #include <uint256.h>
14 : #include <util/hash_type.h>
15 :
16 : #include <assert.h>
17 : #include <climits>
18 : #include <limits>
19 : #include <stdexcept>
20 : #include <stdint.h>
21 : #include <string.h>
22 : #include <string>
23 : #include <vector>
24 :
25 : // Maximum number of bytes pushable to the stack
26 : static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520;
27 :
28 : // Maximum number of non-push operations per script
29 : static const int MAX_OPS_PER_SCRIPT = 201;
30 :
31 : // Maximum number of public keys per multisig
32 : static const int MAX_PUBKEYS_PER_MULTISIG = 20;
33 :
34 : /** The limit of keys in OP_CHECKSIGADD-based scripts. It is due to the stack limit in BIP342. */
35 : static constexpr unsigned int MAX_PUBKEYS_PER_MULTI_A = 999;
36 :
37 : // Maximum script length in bytes
38 : static const int MAX_SCRIPT_SIZE = 10000;
39 :
40 : // Maximum number of values on script interpreter stack
41 : static const int MAX_STACK_SIZE = 1000;
42 :
43 : // Threshold for nLockTime: below this value it is interpreted as block number,
44 : // otherwise as UNIX timestamp.
45 : static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov 5 00:53:20 1985 UTC
46 :
47 : // Maximum nLockTime. Since a lock time indicates the last invalid timestamp, a
48 : // transaction with this lock time will never be valid unless lock time
49 : // checking is disabled (by setting all input sequence numbers to
50 : // SEQUENCE_FINAL).
51 : static const uint32_t LOCKTIME_MAX = 0xFFFFFFFFU;
52 :
53 : // Tag for input annex. If there are at least two witness elements for a transaction input,
54 : // and the first byte of the last element is 0x50, this last element is called annex, and
55 : // has meanings independent of the script
56 : static constexpr unsigned int ANNEX_TAG = 0x50;
57 :
58 : // Validation weight per passing signature (Tapscript only, see BIP 342).
59 : static constexpr int64_t VALIDATION_WEIGHT_PER_SIGOP_PASSED{50};
60 :
61 : // How much weight budget is added to the witness size (Tapscript only, see BIP 342).
62 : static constexpr int64_t VALIDATION_WEIGHT_OFFSET{50};
63 :
64 : template <typename T>
65 16 : std::vector<unsigned char> ToByteVector(const T& in)
66 : {
67 16 : return std::vector<unsigned char>(in.begin(), in.end());
68 0 : }
69 :
70 : /** Script opcodes */
71 : enum opcodetype
72 : {
73 : // push value
74 : OP_0 = 0x00,
75 : OP_FALSE = OP_0,
76 : OP_PUSHDATA1 = 0x4c,
77 : OP_PUSHDATA2 = 0x4d,
78 : OP_PUSHDATA4 = 0x4e,
79 : OP_1NEGATE = 0x4f,
80 : OP_RESERVED = 0x50,
81 : OP_1 = 0x51,
82 : OP_TRUE=OP_1,
83 : OP_2 = 0x52,
84 : OP_3 = 0x53,
85 : OP_4 = 0x54,
86 : OP_5 = 0x55,
87 : OP_6 = 0x56,
88 : OP_7 = 0x57,
89 : OP_8 = 0x58,
90 : OP_9 = 0x59,
91 : OP_10 = 0x5a,
92 : OP_11 = 0x5b,
93 : OP_12 = 0x5c,
94 : OP_13 = 0x5d,
95 : OP_14 = 0x5e,
96 : OP_15 = 0x5f,
97 : OP_16 = 0x60,
98 :
99 : // control
100 : OP_NOP = 0x61,
101 : OP_VER = 0x62,
102 : OP_IF = 0x63,
103 : OP_NOTIF = 0x64,
104 : OP_VERIF = 0x65,
105 : OP_VERNOTIF = 0x66,
106 : OP_ELSE = 0x67,
107 : OP_ENDIF = 0x68,
108 : OP_VERIFY = 0x69,
109 : OP_RETURN = 0x6a,
110 :
111 : // stack ops
112 : OP_TOALTSTACK = 0x6b,
113 : OP_FROMALTSTACK = 0x6c,
114 : OP_2DROP = 0x6d,
115 : OP_2DUP = 0x6e,
116 : OP_3DUP = 0x6f,
117 : OP_2OVER = 0x70,
118 : OP_2ROT = 0x71,
119 : OP_2SWAP = 0x72,
120 : OP_IFDUP = 0x73,
121 : OP_DEPTH = 0x74,
122 : OP_DROP = 0x75,
123 : OP_DUP = 0x76,
124 : OP_NIP = 0x77,
125 : OP_OVER = 0x78,
126 : OP_PICK = 0x79,
127 : OP_ROLL = 0x7a,
128 : OP_ROT = 0x7b,
129 : OP_SWAP = 0x7c,
130 : OP_TUCK = 0x7d,
131 :
132 : // splice ops
133 : OP_CAT = 0x7e,
134 : OP_SUBSTR = 0x7f,
135 : OP_LEFT = 0x80,
136 : OP_RIGHT = 0x81,
137 : OP_SIZE = 0x82,
138 :
139 : // bit logic
140 : OP_INVERT = 0x83,
141 : OP_AND = 0x84,
142 : OP_OR = 0x85,
143 : OP_XOR = 0x86,
144 : OP_EQUAL = 0x87,
145 : OP_EQUALVERIFY = 0x88,
146 : OP_RESERVED1 = 0x89,
147 : OP_RESERVED2 = 0x8a,
148 :
149 : // numeric
150 : OP_1ADD = 0x8b,
151 : OP_1SUB = 0x8c,
152 : OP_2MUL = 0x8d,
153 : OP_2DIV = 0x8e,
154 : OP_NEGATE = 0x8f,
155 : OP_ABS = 0x90,
156 : OP_NOT = 0x91,
157 : OP_0NOTEQUAL = 0x92,
158 :
159 : OP_ADD = 0x93,
160 : OP_SUB = 0x94,
161 : OP_MUL = 0x95,
162 : OP_DIV = 0x96,
163 : OP_MOD = 0x97,
164 : OP_LSHIFT = 0x98,
165 : OP_RSHIFT = 0x99,
166 :
167 : OP_BOOLAND = 0x9a,
168 : OP_BOOLOR = 0x9b,
169 : OP_NUMEQUAL = 0x9c,
170 : OP_NUMEQUALVERIFY = 0x9d,
171 : OP_NUMNOTEQUAL = 0x9e,
172 : OP_LESSTHAN = 0x9f,
173 : OP_GREATERTHAN = 0xa0,
174 : OP_LESSTHANOREQUAL = 0xa1,
175 : OP_GREATERTHANOREQUAL = 0xa2,
176 : OP_MIN = 0xa3,
177 : OP_MAX = 0xa4,
178 :
179 : OP_WITHIN = 0xa5,
180 :
181 : // crypto
182 : OP_RIPEMD160 = 0xa6,
183 : OP_SHA1 = 0xa7,
184 : OP_SHA256 = 0xa8,
185 : OP_HASH160 = 0xa9,
186 : OP_HASH256 = 0xaa,
187 : OP_CODESEPARATOR = 0xab,
188 : OP_CHECKSIG = 0xac,
189 : OP_CHECKSIGVERIFY = 0xad,
190 : OP_CHECKMULTISIG = 0xae,
191 : OP_CHECKMULTISIGVERIFY = 0xaf,
192 :
193 : // expansion
194 : OP_NOP1 = 0xb0,
195 : OP_CHECKLOCKTIMEVERIFY = 0xb1,
196 : OP_NOP2 = OP_CHECKLOCKTIMEVERIFY,
197 : OP_CHECKSEQUENCEVERIFY = 0xb2,
198 : OP_NOP3 = OP_CHECKSEQUENCEVERIFY,
199 : OP_NOP4 = 0xb3,
200 : OP_NOP5 = 0xb4,
201 : OP_NOP6 = 0xb5,
202 : OP_NOP7 = 0xb6,
203 : OP_NOP8 = 0xb7,
204 : OP_NOP9 = 0xb8,
205 : OP_NOP10 = 0xb9,
206 :
207 : // Opcode added by BIP 342 (Tapscript)
208 : OP_CHECKSIGADD = 0xba,
209 :
210 : OP_INVALIDOPCODE = 0xff,
211 : };
212 :
213 : // Maximum value that an opcode can be
214 : static const unsigned int MAX_OPCODE = OP_NOP10;
215 :
216 : std::string GetOpName(opcodetype opcode);
217 :
218 : class scriptnum_error : public std::runtime_error
219 : {
220 : public:
221 0 : explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {}
222 : };
223 :
224 : class CScriptNum
225 : {
226 : /**
227 : * Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
228 : * The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],
229 : * but results may overflow (and are valid as long as they are not used in a subsequent
230 : * numeric operation). CScriptNum enforces those semantics by storing results as
231 : * an int64 and allowing out-of-range values to be returned as a vector of bytes but
232 : * throwing an exception if arithmetic is done or the result is interpreted as an integer.
233 : */
234 : public:
235 :
236 8917 : explicit CScriptNum(const int64_t& n)
237 : {
238 8917 : m_value = n;
239 8917 : }
240 :
241 : static const size_t nDefaultMaxNumSize = 4;
242 :
243 0 : explicit CScriptNum(const std::vector<unsigned char>& vch, bool fRequireMinimal,
244 : const size_t nMaxNumSize = nDefaultMaxNumSize)
245 : {
246 0 : if (vch.size() > nMaxNumSize) {
247 0 : throw scriptnum_error("script number overflow");
248 : }
249 0 : if (fRequireMinimal && vch.size() > 0) {
250 : // Check that the number is encoded with the minimum possible
251 : // number of bytes.
252 : //
253 : // If the most-significant-byte - excluding the sign bit - is zero
254 : // then we're not minimal. Note how this test also rejects the
255 : // negative-zero encoding, 0x80.
256 0 : if ((vch.back() & 0x7f) == 0) {
257 : // One exception: if there's more than one byte and the most
258 : // significant bit of the second-most-significant-byte is set
259 : // it would conflict with the sign bit. An example of this case
260 : // is +-255, which encode to 0xff00 and 0xff80 respectively.
261 : // (big-endian).
262 0 : if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
263 0 : throw scriptnum_error("non-minimally encoded script number");
264 : }
265 0 : }
266 0 : }
267 0 : m_value = set_vch(vch);
268 0 : }
269 :
270 0 : inline bool operator==(const int64_t& rhs) const { return m_value == rhs; }
271 0 : inline bool operator!=(const int64_t& rhs) const { return m_value != rhs; }
272 0 : inline bool operator<=(const int64_t& rhs) const { return m_value <= rhs; }
273 0 : inline bool operator< (const int64_t& rhs) const { return m_value < rhs; }
274 0 : inline bool operator>=(const int64_t& rhs) const { return m_value >= rhs; }
275 0 : inline bool operator> (const int64_t& rhs) const { return m_value > rhs; }
276 :
277 0 : inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); }
278 0 : inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); }
279 0 : inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); }
280 0 : inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); }
281 0 : inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); }
282 0 : inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); }
283 :
284 0 : inline CScriptNum operator+( const int64_t& rhs) const { return CScriptNum(m_value + rhs);}
285 0 : inline CScriptNum operator-( const int64_t& rhs) const { return CScriptNum(m_value - rhs);}
286 0 : inline CScriptNum operator+( const CScriptNum& rhs) const { return operator+(rhs.m_value); }
287 0 : inline CScriptNum operator-( const CScriptNum& rhs) const { return operator-(rhs.m_value); }
288 :
289 0 : inline CScriptNum& operator+=( const CScriptNum& rhs) { return operator+=(rhs.m_value); }
290 0 : inline CScriptNum& operator-=( const CScriptNum& rhs) { return operator-=(rhs.m_value); }
291 :
292 0 : inline CScriptNum operator&( const int64_t& rhs) const { return CScriptNum(m_value & rhs);}
293 0 : inline CScriptNum operator&( const CScriptNum& rhs) const { return operator&(rhs.m_value); }
294 :
295 0 : inline CScriptNum& operator&=( const CScriptNum& rhs) { return operator&=(rhs.m_value); }
296 :
297 0 : inline CScriptNum operator-() const
298 : {
299 0 : assert(m_value != std::numeric_limits<int64_t>::min());
300 0 : return CScriptNum(-m_value);
301 : }
302 :
303 0 : inline CScriptNum& operator=( const int64_t& rhs)
304 : {
305 0 : m_value = rhs;
306 0 : return *this;
307 : }
308 :
309 0 : inline CScriptNum& operator+=( const int64_t& rhs)
310 : {
311 0 : assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
312 : (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
313 0 : m_value += rhs;
314 0 : return *this;
315 : }
316 :
317 0 : inline CScriptNum& operator-=( const int64_t& rhs)
318 : {
319 0 : assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
320 : (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
321 0 : m_value -= rhs;
322 0 : return *this;
323 : }
324 :
325 0 : inline CScriptNum& operator&=( const int64_t& rhs)
326 : {
327 0 : m_value &= rhs;
328 0 : return *this;
329 : }
330 :
331 0 : int getint() const
332 : {
333 0 : if (m_value > std::numeric_limits<int>::max())
334 0 : return std::numeric_limits<int>::max();
335 0 : else if (m_value < std::numeric_limits<int>::min())
336 0 : return std::numeric_limits<int>::min();
337 0 : return m_value;
338 0 : }
339 :
340 0 : int64_t GetInt64() const { return m_value; }
341 :
342 8915 : std::vector<unsigned char> getvch() const
343 : {
344 8915 : return serialize(m_value);
345 : }
346 :
347 19190 : static std::vector<unsigned char> serialize(const int64_t& value)
348 : {
349 19190 : if(value == 0)
350 0 : return std::vector<unsigned char>();
351 :
352 19190 : std::vector<unsigned char> result;
353 19190 : const bool neg = value < 0;
354 19190 : uint64_t absvalue = neg ? ~static_cast<uint64_t>(value) + 1 : static_cast<uint64_t>(value);
355 :
356 38395 : while(absvalue)
357 : {
358 19205 : result.push_back(absvalue & 0xff);
359 19205 : absvalue >>= 8;
360 : }
361 :
362 : // - If the most significant byte is >= 0x80 and the value is positive, push a
363 : // new zero-byte to make the significant byte < 0x80 again.
364 :
365 : // - If the most significant byte is >= 0x80 and the value is negative, push a
366 : // new 0x80 byte that will be popped off when converting to an integral.
367 :
368 : // - If the most significant byte is < 0x80 and the value is negative, add
369 : // 0x80 to it, since it will be subtracted and interpreted as a negative when
370 : // converting to an integral.
371 :
372 19190 : if (result.back() & 0x80)
373 9937 : result.push_back(neg ? 0x80 : 0);
374 9253 : else if (neg)
375 0 : result.back() |= 0x80;
376 :
377 19190 : return result;
378 38380 : }
379 :
380 : private:
381 0 : static int64_t set_vch(const std::vector<unsigned char>& vch)
382 : {
383 0 : if (vch.empty())
384 0 : return 0;
385 :
386 0 : int64_t result = 0;
387 0 : for (size_t i = 0; i != vch.size(); ++i)
388 0 : result |= static_cast<int64_t>(vch[i]) << 8*i;
389 :
390 : // If the input vector's most significant byte is 0x80, remove it from
391 : // the result's msb and return a negative.
392 0 : if (vch.back() & 0x80)
393 0 : return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
394 :
395 0 : return result;
396 0 : }
397 :
398 : int64_t m_value;
399 : };
400 :
401 : /**
402 : * We use a prevector for the script to reduce the considerable memory overhead
403 : * of vectors in cases where they normally contain a small number of small elements.
404 : * Tests in October 2015 showed use of this reduced dbcache memory usage by 23%
405 : * and made an initial sync 13% faster.
406 : */
407 : typedef prevector<28, unsigned char> CScriptBase;
408 :
409 : bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet);
410 :
411 : /** Serialized script, used inside transaction inputs and outputs */
412 : class CScript : public CScriptBase
413 : {
414 : protected:
415 10323 : CScript& push_int64(int64_t n)
416 : {
417 10323 : if (n == -1 || (n >= 1 && n <= 16))
418 : {
419 48 : push_back(n + (OP_1 - 1));
420 48 : }
421 10275 : else if (n == 0)
422 : {
423 0 : push_back(OP_0);
424 0 : }
425 : else
426 : {
427 10275 : *this << CScriptNum::serialize(n);
428 : }
429 10323 : return *this;
430 0 : }
431 : public:
432 3216098 : CScript() { }
433 0 : CScript(const_iterator pbegin, const_iterator pend) : CScriptBase(pbegin, pend) { }
434 33392 : CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) : CScriptBase(pbegin, pend) { }
435 0 : CScript(const unsigned char* pbegin, const unsigned char* pend) : CScriptBase(pbegin, pend) { }
436 :
437 19083831 : SERIALIZE_METHODS(CScript, obj) { READWRITE(AsBase<CScriptBase>(obj)); }
438 :
439 : explicit CScript(int64_t b) { operator<<(b); }
440 0 : explicit CScript(opcodetype b) { operator<<(b); }
441 : explicit CScript(const CScriptNum& b) { operator<<(b); }
442 : // delete non-existent constructor to defend against future introduction
443 : // e.g. via prevector
444 : explicit CScript(const std::vector<unsigned char>& b) = delete;
445 :
446 : /** Delete non-existent operator to defend against future introduction */
447 : CScript& operator<<(const CScript& b) = delete;
448 :
449 10323 : CScript& operator<<(int64_t b) LIFETIMEBOUND { return push_int64(b); }
450 :
451 9939 : CScript& operator<<(opcodetype opcode) LIFETIMEBOUND
452 : {
453 9939 : if (opcode < 0 || opcode > 0xff)
454 0 : throw std::runtime_error("CScript::operator<<(): invalid opcode");
455 9939 : insert(end(), (unsigned char)opcode);
456 9939 : return *this;
457 0 : }
458 :
459 5 : CScript& operator<<(const CScriptNum& b) LIFETIMEBOUND
460 : {
461 5 : *this << b.getvch();
462 5 : return *this;
463 0 : }
464 :
465 10306 : CScript& operator<<(const std::vector<unsigned char>& b) LIFETIMEBOUND
466 : {
467 10306 : if (b.size() < OP_PUSHDATA1)
468 : {
469 10306 : insert(end(), (unsigned char)b.size());
470 10306 : }
471 0 : else if (b.size() <= 0xff)
472 : {
473 0 : insert(end(), OP_PUSHDATA1);
474 0 : insert(end(), (unsigned char)b.size());
475 0 : }
476 0 : else if (b.size() <= 0xffff)
477 : {
478 0 : insert(end(), OP_PUSHDATA2);
479 : uint8_t _data[2];
480 0 : WriteLE16(_data, b.size());
481 0 : insert(end(), _data, _data + sizeof(_data));
482 0 : }
483 : else
484 : {
485 0 : insert(end(), OP_PUSHDATA4);
486 : uint8_t _data[4];
487 0 : WriteLE32(_data, b.size());
488 0 : insert(end(), _data, _data + sizeof(_data));
489 : }
490 10306 : insert(end(), b.begin(), b.end());
491 10306 : return *this;
492 : }
493 :
494 26730 : bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
495 : {
496 26730 : return GetScriptOp(pc, end(), opcodeRet, &vchRet);
497 : }
498 :
499 322029 : bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
500 : {
501 322029 : return GetScriptOp(pc, end(), opcodeRet, nullptr);
502 : }
503 :
504 : /** Encode/decode small integers: */
505 752612 : static int DecodeOP_N(opcodetype opcode)
506 : {
507 752612 : if (opcode == OP_0)
508 752612 : return 0;
509 0 : assert(opcode >= OP_1 && opcode <= OP_16);
510 0 : return (int)opcode - (int)(OP_1 - 1);
511 752612 : }
512 0 : static opcodetype EncodeOP_N(int n)
513 : {
514 0 : assert(n >= 0 && n <= 16);
515 0 : if (n == 0)
516 0 : return OP_0;
517 0 : return (opcodetype)(OP_1+n-1);
518 0 : }
519 :
520 : /**
521 : * Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
522 : * as 20 sigops. With pay-to-script-hash, that changed:
523 : * CHECKMULTISIGs serialized in scriptSigs are
524 : * counted more accurately, assuming they are of the form
525 : * ... OP_N CHECKMULTISIG ...
526 : */
527 : unsigned int GetSigOpCount(bool fAccurate) const;
528 :
529 : /**
530 : * Accurately count sigOps, including sigOps in
531 : * pay-to-script-hash transactions:
532 : */
533 : unsigned int GetSigOpCount(const CScript& scriptSig) const;
534 :
535 : bool IsPayToScriptHash() const;
536 : bool IsPayToWitnessScriptHash() const;
537 : bool IsWitnessProgram(int& version, std::vector<unsigned char>& program) const;
538 :
539 : /** Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical). */
540 : bool IsPushOnly(const_iterator pc) const;
541 : bool IsPushOnly() const;
542 :
543 : /** Check if the script contains valid OP_CODES */
544 : bool HasValidOps() const;
545 :
546 : /**
547 : * Returns whether the script is guaranteed to fail at execution,
548 : * regardless of the initial stack. This allows outputs to be pruned
549 : * instantly when entering the UTXO set.
550 : */
551 389811 : bool IsUnspendable() const
552 : {
553 389811 : return (size() > 0 && *begin() == OP_RETURN) || (size() > MAX_SCRIPT_SIZE);
554 : }
555 :
556 530425 : void clear()
557 : {
558 : // The default prevector::clear() does not release memory
559 530425 : CScriptBase::clear();
560 530425 : shrink_to_fit();
561 530425 : }
562 : };
563 :
564 : struct CScriptWitness
565 : {
566 : // Note that this encodes the data elements being pushed, rather than
567 : // encoding them as a CScript that pushes them.
568 : std::vector<std::vector<unsigned char> > stack;
569 :
570 : // Some compilers complain without a default constructor
571 324979 : CScriptWitness() { }
572 :
573 170484 : bool IsNull() const { return stack.empty(); }
574 :
575 0 : void SetNull() { stack.clear(); stack.shrink_to_fit(); }
576 :
577 : std::string ToString() const;
578 : };
579 :
580 : /** A reference to a CScript: the Hash160 of its serialization */
581 : class CScriptID : public BaseHash<uint160>
582 : {
583 : public:
584 0 : CScriptID() : BaseHash() {}
585 : explicit CScriptID(const CScript& in);
586 0 : explicit CScriptID(const uint160& in) : BaseHash(in) {}
587 : };
588 :
589 : /** Test for OP_SUCCESSx opcodes as defined by BIP342. */
590 : bool IsOpSuccess(const opcodetype& opcode);
591 :
592 : bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode);
593 :
594 : /** Build a script by concatenating other scripts, or any argument accepted by CScript::operator<<. */
595 : template<typename... Ts>
596 0 : CScript BuildScript(Ts&&... inputs)
597 : {
598 0 : CScript ret;
599 0 : int cnt{0};
600 :
601 0 : ([&ret, &cnt] (Ts&& input) {
602 : if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<Ts>>, CScript>) {
603 : // If it is a CScript, extend ret with it. Move or copy the first element instead.
604 0 : if (cnt == 0) {
605 0 : ret = std::forward<Ts>(input);
606 0 : } else {
607 0 : ret.insert(ret.end(), input.begin(), input.end());
608 : }
609 : } else {
610 : // Otherwise invoke CScript::operator<<.
611 0 : ret << input;
612 : }
613 0 : cnt++;
614 0 : } (std::forward<Ts>(inputs)), ...);
615 :
616 0 : return ret;
617 0 : }
618 :
619 : #endif // BITCOIN_SCRIPT_SCRIPT_H
|