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