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