]> Git Repo - VerusCoin.git/blob - src/script/script.h
Enable future events to be registered via index
[VerusCoin.git] / src / script / script.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or https://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
12 #include <assert.h>
13 #include <climits>
14 #include <limits>
15 #include <stdexcept>
16 #include <stdint.h>
17 #include <string.h>
18 #include <string>
19 #include <vector>
20 #include "uint256.h"
21 #include "pubkey.h"
22 #include "pbaas/reserves.h"
23
24 #define OPRETTYPE_TIMELOCK 1
25 #define OPRETTYPE_STAKEPARAMS 2
26 #define OPRETTYPE_STAKECHEAT 3
27 #define OPRETTYPE_OBJECT 4
28 #define OPRETTYPE_OBJECTARR 5
29 #define OPRETTYPE_STAKEPARAMS2 6
30
31 class CCurrencyState;
32 class CIdentity;
33
34 static const unsigned int MAX_SCRIPT_ELEMENT_SIZE_V2 = 1024;
35 static const unsigned int MAX_SCRIPT_ELEMENT_SIZE_IDENTITY = 3073;    // fulfillment maximum size + 1, MAKE SURE TO KEEP MAX_BINARY_CC_SIZE IN SYNC WITH THIS-1, BUF_SIZE in crypto conditions, should be >=
36
37 // Maximum script length in bytes
38 static const int MAX_SCRIPT_SIZE = 10000;
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 template <typename T>
45 std::vector<unsigned char> ToByteVector(const T& in)
46 {
47     return std::vector<unsigned char>(in.begin(), in.end());
48 }
49
50 /** Script opcodes */
51 enum opcodetype
52 {
53     // push value
54     OP_0 = 0x00,
55     OP_FALSE = OP_0,
56     OP_PUSHDATA1 = 0x4c,
57     OP_PUSHDATA2 = 0x4d,
58     OP_PUSHDATA4 = 0x4e,
59     OP_1NEGATE = 0x4f,
60     OP_RESERVED = 0x50,
61     OP_1 = 0x51,
62     OP_TRUE=OP_1,
63     OP_2 = 0x52,
64     OP_3 = 0x53,
65     OP_4 = 0x54,
66     OP_5 = 0x55,
67     OP_6 = 0x56,
68     OP_7 = 0x57,
69     OP_8 = 0x58,
70     OP_9 = 0x59,
71     OP_10 = 0x5a,
72     OP_11 = 0x5b,
73     OP_12 = 0x5c,
74     OP_13 = 0x5d,
75     OP_14 = 0x5e,
76     OP_15 = 0x5f,
77     OP_16 = 0x60,
78
79     // control
80     OP_NOP = 0x61,
81     OP_VER = 0x62,
82     OP_IF = 0x63,
83     OP_NOTIF = 0x64,
84     OP_VERIF = 0x65,
85     OP_VERNOTIF = 0x66,
86     OP_ELSE = 0x67,
87     OP_ENDIF = 0x68,
88     OP_VERIFY = 0x69,
89     OP_RETURN = 0x6a,
90
91     // stack ops
92     OP_TOALTSTACK = 0x6b,
93     OP_FROMALTSTACK = 0x6c,
94     OP_2DROP = 0x6d,
95     OP_2DUP = 0x6e,
96     OP_3DUP = 0x6f,
97     OP_2OVER = 0x70,
98     OP_2ROT = 0x71,
99     OP_2SWAP = 0x72,
100     OP_IFDUP = 0x73,
101     OP_DEPTH = 0x74,
102     OP_DROP = 0x75,
103     OP_DUP = 0x76,
104     OP_NIP = 0x77,
105     OP_OVER = 0x78,
106     OP_PICK = 0x79,
107     OP_ROLL = 0x7a,
108     OP_ROT = 0x7b,
109     OP_SWAP = 0x7c,
110     OP_TUCK = 0x7d,
111
112     // splice ops
113     OP_CAT = 0x7e,
114     OP_SUBSTR = 0x7f,
115     OP_LEFT = 0x80,
116     OP_RIGHT = 0x81,
117     OP_SIZE = 0x82,
118
119     // bit logic
120     OP_INVERT = 0x83,
121     OP_AND = 0x84,
122     OP_OR = 0x85,
123     OP_XOR = 0x86,
124     OP_EQUAL = 0x87,
125     OP_EQUALVERIFY = 0x88,
126     OP_RESERVED1 = 0x89,
127     OP_RESERVED2 = 0x8a,
128
129     // numeric
130     OP_1ADD = 0x8b,
131     OP_1SUB = 0x8c,
132     OP_2MUL = 0x8d,
133     OP_2DIV = 0x8e,
134     OP_NEGATE = 0x8f,
135     OP_ABS = 0x90,
136     OP_NOT = 0x91,
137     OP_0NOTEQUAL = 0x92,
138
139     OP_ADD = 0x93,
140     OP_SUB = 0x94,
141     OP_MUL = 0x95,
142     OP_DIV = 0x96,
143     OP_MOD = 0x97,
144     OP_LSHIFT = 0x98,
145     OP_RSHIFT = 0x99,
146
147     OP_BOOLAND = 0x9a,
148     OP_BOOLOR = 0x9b,
149     OP_NUMEQUAL = 0x9c,
150     OP_NUMEQUALVERIFY = 0x9d,
151     OP_NUMNOTEQUAL = 0x9e,
152     OP_LESSTHAN = 0x9f,
153     OP_GREATERTHAN = 0xa0,
154     OP_LESSTHANOREQUAL = 0xa1,
155     OP_GREATERTHANOREQUAL = 0xa2,
156     OP_MIN = 0xa3,
157     OP_MAX = 0xa4,
158
159     OP_WITHIN = 0xa5,
160
161     // crypto
162     OP_RIPEMD160 = 0xa6,
163     OP_SHA1 = 0xa7,
164     OP_SHA256 = 0xa8,
165     OP_HASH160 = 0xa9,
166     OP_HASH256 = 0xaa,
167     OP_CODESEPARATOR = 0xab,
168     OP_CHECKSIG = 0xac,
169     OP_CHECKSIGVERIFY = 0xad,
170     OP_CHECKMULTISIG = 0xae,
171     OP_CHECKMULTISIGVERIFY = 0xaf,
172     OP_CHECKCRYPTOCONDITION = 0xcc,
173     OP_CHECKCRYPTOCONDITIONVERIFY = 0xcd,
174
175     // expansion
176     OP_NOP1 = 0xb0,
177     OP_NOP2 = 0xb1,
178     OP_CHECKLOCKTIMEVERIFY = OP_NOP2,
179     OP_NOP3 = 0xb2,
180     OP_NOP4 = 0xb3,
181     OP_NOP5 = 0xb4,
182     OP_NOP6 = 0xb5,
183     OP_NOP7 = 0xb6,
184     OP_NOP8 = 0xb7,
185     OP_NOP9 = 0xb8,
186     OP_NOP10 = 0xb9,
187
188     // template matching params
189     OP_SMALLDATA = 0xf9,
190     OP_SMALLINTEGER = 0xfa,
191     OP_PUBKEYS = 0xfb,
192     OP_PUBKEYHASH = 0xfd,
193     OP_PUBKEY = 0xfe,
194     OP_CRYPTOCONDITION = 0xfc,
195
196     OP_INVALIDOPCODE = 0xff,
197 };
198
199 const char* GetOpName(opcodetype opcode);
200
201 class scriptnum_error : public std::runtime_error
202 {
203 public:
204     explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {}
205 };
206
207 class CScriptNum
208 {
209 /**
210  * Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
211  * The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],
212  * but results may overflow (and are valid as long as they are not used in a subsequent
213  * numeric operation). CScriptNum enforces those semantics by storing results as
214  * an int64 and allowing out-of-range values to be returned as a vector of bytes but
215  * throwing an exception if arithmetic is done or the result is interpreted as an integer.
216  */
217 public:
218
219     explicit CScriptNum(const int64_t& n)
220     {
221         m_value = n;
222     }
223
224     static const size_t nDefaultMaxNumSize = 4;
225
226     explicit CScriptNum(const std::vector<unsigned char>& vch, bool fRequireMinimal,
227                         const size_t nMaxNumSize = nDefaultMaxNumSize)
228     {
229         if (vch.size() > nMaxNumSize) {
230             throw scriptnum_error("script number overflow");
231         }
232         if (fRequireMinimal && vch.size() > 0) {
233             // Check that the number is encoded with the minimum possible
234             // number of bytes.
235             //
236             // If the most-significant-byte - excluding the sign bit - is zero
237             // then we're not minimal. Note how this test also rejects the
238             // negative-zero encoding, 0x80.
239             if ((vch.back() & 0x7f) == 0) {
240                 // One exception: if there's more than one byte and the most
241                 // significant bit of the second-most-significant-byte is set
242                 // it would conflict with the sign bit. An example of this case
243                 // is +-255, which encode to 0xff00 and 0xff80 respectively.
244                 // (big-endian).
245                 if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
246                     throw scriptnum_error("non-minimally encoded script number");
247                 }
248             }
249         }
250         m_value = set_vch(vch);
251     }
252
253     inline bool operator==(const int64_t& rhs) const    { return m_value == rhs; }
254     inline bool operator!=(const int64_t& rhs) const    { return m_value != rhs; }
255     inline bool operator<=(const int64_t& rhs) const    { return m_value <= rhs; }
256     inline bool operator< (const int64_t& rhs) const    { return m_value <  rhs; }
257     inline bool operator>=(const int64_t& rhs) const    { return m_value >= rhs; }
258     inline bool operator> (const int64_t& rhs) const    { return m_value >  rhs; }
259
260     inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); }
261     inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); }
262     inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); }
263     inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); }
264     inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); }
265     inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); }
266
267     inline CScriptNum operator+(   const int64_t& rhs)    const { return CScriptNum(m_value + rhs);}
268     inline CScriptNum operator-(   const int64_t& rhs)    const { return CScriptNum(m_value - rhs);}
269     inline CScriptNum operator+(   const CScriptNum& rhs) const { return operator+(rhs.m_value);   }
270     inline CScriptNum operator-(   const CScriptNum& rhs) const { return operator-(rhs.m_value);   }
271
272     inline CScriptNum& operator+=( const CScriptNum& rhs)       { return operator+=(rhs.m_value);  }
273     inline CScriptNum& operator-=( const CScriptNum& rhs)       { return operator-=(rhs.m_value);  }
274
275     inline CScriptNum operator-()                         const
276     {
277         assert(m_value != std::numeric_limits<int64_t>::min());
278         return CScriptNum(-m_value);
279     }
280
281     inline CScriptNum& operator=( const int64_t& rhs)
282     {
283         m_value = rhs;
284         return *this;
285     }
286
287     inline CScriptNum& operator+=( const int64_t& rhs)
288     {
289         assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
290                            (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
291         m_value += rhs;
292         return *this;
293     }
294
295     inline CScriptNum& operator-=( const int64_t& rhs)
296     {
297         assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
298                            (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
299         m_value -= rhs;
300         return *this;
301     }
302
303     int getint() const
304     {
305         if (m_value > std::numeric_limits<int>::max())
306             return std::numeric_limits<int>::max();
307         else if (m_value < std::numeric_limits<int>::min())
308             return std::numeric_limits<int>::min();
309         return m_value;
310     }
311
312     std::vector<unsigned char> getvch() const
313     {
314         return serialize(m_value);
315     }
316
317     static std::vector<unsigned char> serialize(const int64_t& value)
318     {
319         if(value == 0)
320             return std::vector<unsigned char>();
321
322         std::vector<unsigned char> result;
323         const bool neg = value < 0;
324         uint64_t absvalue = neg ? -value : value;
325
326         while(absvalue)
327         {
328             result.push_back(absvalue & 0xff);
329             absvalue >>= 8;
330         }
331
332 //    - If the most significant byte is >= 0x80 and the value is positive, push a
333 //    new zero-byte to make the significant byte < 0x80 again.
334
335 //    - If the most significant byte is >= 0x80 and the value is negative, push a
336 //    new 0x80 byte that will be popped off when converting to an integral.
337
338 //    - If the most significant byte is < 0x80 and the value is negative, add
339 //    0x80 to it, since it will be subtracted and interpreted as a negative when
340 //    converting to an integral.
341
342         if (result.back() & 0x80)
343             result.push_back(neg ? 0x80 : 0);
344         else if (neg)
345             result.back() |= 0x80;
346
347         return result;
348     }
349
350 private:
351     static int64_t set_vch(const std::vector<unsigned char>& vch)
352     {
353       if (vch.empty())
354           return 0;
355
356       int64_t result = 0;
357       for (size_t i = 0; i != vch.size(); ++i)
358           result |= static_cast<int64_t>(vch[i]) << 8*i;
359
360       // If the input vector's most significant byte is 0x80, remove it from
361       // the result's msb and return a negative.
362       if (vch.back() & 0x80)
363           return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
364
365       return result;
366     }
367
368     int64_t m_value;
369 };
370
371 typedef prevector<28, unsigned char> CScriptBase;
372
373 class CKeyID;
374 class CScript;
375 class CKeyStore;
376
377 class CNoDestination {
378 public:
379     friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
380     friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
381 };
382
383 /** A reference to a CScript: the Hash160 of its serialization (see script.h) */
384 class CScriptID : public uint160
385 {
386 public:
387     CScriptID() : uint160() {}
388     CScriptID(const CScript& in);
389     CScriptID(const uint160& in) : uint160(in) {}
390 };
391
392 uint160 GetNameID(const std::string &Name, const uint160 &parent);
393
394 /** A reference to an identity: the Hash160 of a specific serialization if its name and parent chain (see script.h) */
395 class CIdentityID : public uint160
396 {
397 public:
398     CIdentityID() : uint160() {}
399     CIdentityID(const std::string& in, const uint160 &parent=uint160()) : uint160(GetNameID(in, parent)) {}
400     CIdentityID(const uint160& in) : uint160(in) {}
401 };
402
403 /** A reference to a quantum public key: the Hash160 of its serialization (see script.h), which it is indexed by in an output */
404 class CQuantumID : public uint160
405 {
406 public:
407     CQuantumID() : uint160() {}
408     CQuantumID(const std::string& in, const uint160 &parent=uint160()) : uint160(GetNameID(in, parent)) {}
409     CQuantumID(const uint160& in) : uint160(in) {}
410 };
411
412 /** A reference to an index only address type, not used in the API or externally, but
413  * reserved as an index for specific types of transaction outputs that can then be queried
414  * and assumed to be valid and checked if found.
415  * CQuantum public keys are indexed by a CIndexID that represents a hash of the quantum public key. */
416 class CIndexID : public uint160
417 {
418 public:
419     CIndexID() : uint160() {}
420     CIndexID(const uint160& in) : uint160(in) {}
421 };
422
423 /** 
424  * A txout script template with a specific destination. It is either:
425  *  * CNoDestination: no destination set
426  *  * CKeyID: TX_PUBKEYHASH destination
427  *  * CScriptID: TX_SCRIPTHASH destination
428  *  A CTxDestination is the internal data type encoded in a bitcoin address
429  */
430 typedef boost::variant<CNoDestination, CPubKey, CKeyID, CScriptID, CIdentityID, CIndexID, CQuantumID> CTxDestination;
431
432 CTxDestination TransferDestinationToDestination(const CTransferDestination &transferDest);
433 CTransferDestination DestinationToTransferDestination(const CTxDestination &dest);
434 CTransferDestination IdentityToTransferDestination(const CIdentity &identity);
435 CIdentity TransferDestinationToIdentity(const CTransferDestination &dest);
436 std::vector<CTxDestination> TransferDestinationsToDestinations(const std::vector<CTransferDestination> &transferDests);
437 std::vector<CTransferDestination> DestinationsToTransferDestinations(const std::vector<CTxDestination> &dests);
438
439 class COptCCParams
440 {
441 public:
442     static const uint8_t VERSION_V1 = 1;
443     static const uint8_t VERSION_V2 = 2;
444     static const uint8_t VERSION_V3 = 3;
445
446     static const uint8_t ADDRTYPE_INVALID = 0;
447     static const uint8_t ADDRTYPE_PK = 1;
448     static const uint8_t ADDRTYPE_PKH = 2;
449     static const uint8_t ADDRTYPE_SH = 3;
450     static const uint8_t ADDRTYPE_ID = 4;
451     static const uint8_t ADDRTYPE_INDEX = 5;
452     static const uint8_t ADDRTYPE_QUANTUM = 6;
453     static const uint8_t ADDRTYPE_LAST = 6;
454
455     uint8_t version;
456     uint8_t evalCode;
457     uint8_t m, n; // for m of n sigs required, n pub keys for sigs will follow
458     std::vector<CTxDestination> vKeys;
459     std::vector<std::vector<unsigned char>> vData; // extra parameters
460
461     COptCCParams() : version(0), evalCode(0), m(0), n(0) {}
462
463     COptCCParams(uint8_t ver, uint8_t code, uint8_t _m, uint8_t _n, const std::vector<CTxDestination> &vkeys, const std::vector<std::vector<unsigned char>> &vdata) : 
464         version(ver), evalCode(code), m(_m), n(_n), vKeys(vkeys), vData(vdata) {}
465
466     COptCCParams(const std::vector<unsigned char> &vch);
467
468     bool IsValid() const { return version == VERSION_V1 || version == VERSION_V2 || version == VERSION_V3; }
469
470     std::vector<unsigned char> AsVector() const;
471
472     std::set<CIndexID> GetIndexKeys() const;
473     std::map<uint160, uint32_t> GetIndexHeightOffsets(uint32_t height) const;
474     std::vector<CTxDestination> GetDestinations() const;
475 };
476
477 // This is for STAKEGUARD2, which is versioned and enables staking the reserve of a reserve currency
478 // chain, where this native currency is a reserve of a native currency that is being notarized into
479 // this chain.
480 class CStakeInfo
481 {
482 public:
483     enum
484     {
485         VERSION_INVALID = 0,
486         VERSION_FIRST = 1,
487         VERSION_CURRENT = 1,
488         VERSION_LAST = 1
489     };
490     uint32_t version;
491     uint32_t height;
492     uint32_t sourceHeight;
493     uint256 utxo;
494     uint256 prevHash;
495
496     CStakeInfo() : version(VERSION_INVALID), height(0), sourceHeight(0) {}
497     CStakeInfo(uint32_t Height, uint32_t SourceHeight, const uint256 &UTXO, const uint256 &PrevHash, uint32_t Version=VERSION_CURRENT) : 
498                 version(Version), height(0), sourceHeight(0), utxo(UTXO), prevHash(PrevHash) {}
499     CStakeInfo(std::vector<unsigned char> vch);
500
501     std::vector<unsigned char> AsVector() const;
502     
503     ADD_SERIALIZE_METHODS;
504
505     template <typename Stream, typename Operation>
506     inline void SerializationOp(Stream& s, Operation ser_action) {
507         READWRITE(VARINT(version));
508         READWRITE(VARINT(height));
509         READWRITE(VARINT(sourceHeight));
510         READWRITE(utxo);
511         READWRITE(prevHash);
512     }
513 };
514
515 // used when creating crypto condition outputs
516 template <typename T>
517 class CConditionObj
518 {
519 public:
520     uint8_t evalCode;
521     int m;
522     const std::vector<CTxDestination> dests;
523     T obj;
524     bool objectValid;
525     CConditionObj(uint8_t eCode, const std::vector<CTxDestination> &Dests, int M, const T *pO=nullptr) : evalCode(eCode), m(M), dests(Dests), objectValid(false)
526     {
527         if (pO)
528         {
529             obj = *pO;
530             objectValid = true;
531         }
532     }
533     bool HaveObject() const
534     {
535         return objectValid;
536     }
537     T *ObjectPtr() const
538     {
539         if (HaveObject())
540         {
541             return &obj;
542         }
543         else
544         {
545             return nullptr;
546         }
547     }
548 };
549
550 /** Serialized script, used inside transaction inputs and outputs */
551 class CScript : public CScriptBase
552 {
553 protected:
554     CScript& push_int64(int64_t n)
555     {
556         if (n == -1 || (n >= 1 && n <= 16))
557         {
558             push_back(n + (OP_1 - 1));
559         }
560         else if (n == 0)
561         {
562             push_back(OP_0);
563         }
564         else
565         {
566             *this << CScriptNum::serialize(n);
567         }
568         return *this;
569     }
570     bool GetBalancedData(const_iterator& pc, std::vector<std::vector<unsigned char>>& vSolutions) const;
571 public:
572     static unsigned int MAX_SCRIPT_ELEMENT_SIZE; // bytes
573
574     CScript() { }
575     CScript(const CScript& b) : CScriptBase(b.begin(), b.end()) { }
576     CScript(const_iterator pbegin, const_iterator pend) : CScriptBase(pbegin, pend) { }
577     CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) : CScriptBase(pbegin, pend) { }
578     CScript(const unsigned char* pbegin, const unsigned char* pend) : CScriptBase(pbegin, pend) { }
579
580     CScript& operator+=(const CScript& b)
581     {
582         insert(end(), b.begin(), b.end());
583         return *this;
584     }
585
586     friend CScript operator+(const CScript& a, const CScript& b)
587     {
588         CScript ret = a;
589         ret += b;
590         return ret;
591     }
592
593     CScript(int64_t b)        { operator<<(b); }
594     explicit CScript(opcodetype b)     { operator<<(b); }
595     explicit CScript(const CScriptNum& b) { operator<<(b); }
596     explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
597
598     CScript& operator<<(int64_t b) { return push_int64(b); }
599
600     CScript& operator<<(opcodetype opcode)
601     {
602         if (opcode < 0 || opcode > 0xff)
603             throw std::runtime_error("CScript::operator<<(): invalid opcode");
604         insert(end(), (unsigned char)opcode);
605         return *this;
606     }
607
608     CScript& operator<<(const CScriptNum& b)
609     {
610         *this << b.getvch();
611         return *this;
612     }
613
614     CScript& operator<<(const std::vector<unsigned char>& b)
615     {
616         if (b.size() < OP_PUSHDATA1)
617         {
618             insert(end(), (unsigned char)b.size());
619         }
620         else if (b.size() <= 0xff)
621         {
622             insert(end(), OP_PUSHDATA1);
623             insert(end(), (unsigned char)b.size());
624         }
625         else if (b.size() <= 0xffff)
626         {
627             insert(end(), OP_PUSHDATA2);
628             uint8_t data[2];
629             WriteLE16(data, b.size());
630             insert(end(), data, data + sizeof(data));
631         }
632         else
633         {
634             insert(end(), OP_PUSHDATA4);
635             uint8_t data[4];
636             WriteLE32(data, b.size());
637             insert(end(), data, data + sizeof(data));
638         }
639         insert(end(), b.begin(), b.end());
640         return *this;
641     }
642
643     CScript& operator<<(const CScript& b)
644     {
645         // I'm not sure if this should push the script or concatenate scripts.
646         // If there's ever a use for pushing a script onto a script, delete this member fn
647         assert(!"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
648         return *this;
649     }
650
651
652     bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
653     {
654          // Wrapper so it can be called with either iterator or const_iterator
655          const_iterator pc2 = pc;
656          bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
657          pc = begin() + (pc2 - begin());
658          return fRet;
659     }
660
661     bool GetOp(iterator& pc, opcodetype& opcodeRet)
662     {
663          const_iterator pc2 = pc;
664          bool fRet = GetOp2(pc2, opcodeRet, NULL);
665          pc = begin() + (pc2 - begin());
666          return fRet;
667     }
668
669     bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
670     {
671         return GetOp2(pc, opcodeRet, &vchRet);
672     }
673
674     bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
675     {
676         return GetOp2(pc, opcodeRet, NULL);
677     }
678
679     bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
680     {
681         opcodeRet = OP_INVALIDOPCODE;
682         if (pvchRet)
683             pvchRet->clear();
684         if (pc >= end())
685             return false;
686
687         // Read instruction
688         if (end() - pc < 1)
689             return false;
690         unsigned int opcode = *pc++;
691
692         // Immediate operand
693         if (opcode <= OP_PUSHDATA4)
694         {
695             unsigned int nSize = 0;
696             if (opcode < OP_PUSHDATA1)
697             {
698                 nSize = opcode;
699             }
700             else if (opcode == OP_PUSHDATA1)
701             {
702                 if (end() - pc < 1)
703                     return false;
704                 nSize = *pc++;
705             }
706             else if (opcode == OP_PUSHDATA2)
707             {
708                 if (end() - pc < 2)
709                     return false;
710                 nSize = ReadLE16(&pc[0]);
711                 pc += 2;
712             }
713             else if (opcode == OP_PUSHDATA4)
714             {
715                 if (end() - pc < 4)
716                     return false;
717                 nSize = ReadLE32(&pc[0]);
718                 pc += 4;
719             }
720             if (end() - pc < 0 || (unsigned int)(end() - pc) < nSize)
721                 return false;
722             if (pvchRet)
723                 pvchRet->assign(pc, pc + nSize);
724             pc += nSize;
725         }
726
727         opcodeRet = (opcodetype)opcode;
728         return true;
729     }
730
731     /** Encode/decode small integers: */
732     static int DecodeOP_N(opcodetype opcode)
733     {
734         if (opcode == OP_0)
735             return 0;
736         assert(opcode >= OP_1 && opcode <= OP_16);
737         return (int)opcode - (int)(OP_1 - 1);
738     }
739     static opcodetype EncodeOP_N(int n)
740     {
741         assert(n >= 0 && n <= 16);
742         if (n == 0)
743             return OP_0;
744         return (opcodetype)(OP_1+n-1);
745     }
746
747     /**
748      * Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
749      * as 20 sigops. With pay-to-script-hash, that changed:
750      * CHECKMULTISIGs serialized in scriptSigs are
751      * counted more accurately, assuming they are of the form
752      *  ... OP_N CHECKMULTISIG ...
753      */
754     unsigned int GetSigOpCount(bool fAccurate) const;
755
756     /**
757      * Accurately count sigOps, including sigOps in
758      * pay-to-script-hash transactions:
759      */
760     unsigned int GetSigOpCount(const CScript& scriptSig) const;
761
762     bool IsPayToPublicKeyHash() const;
763     bool IsPayToPublicKey() const;
764
765     bool IsPayToScriptHash() const;
766     bool GetPushedData(CScript::const_iterator pc, std::vector<std::vector<unsigned char>>& vData) const;
767     bool IsOpReturn() const { return size() > 0 && (*this)[0] == OP_RETURN; }
768     bool GetOpretData(std::vector<std::vector<unsigned char>>& vData) const;
769
770     bool IsPayToCryptoCondition(COptCCParams &ccParams) const;
771     bool IsPayToCryptoCondition(CScript *ccSubScript, std::vector<std::vector<unsigned char>> &vParams, COptCCParams &optParams) const;
772     bool IsPayToCryptoCondition(CScript *ccSubScript, std::vector<std::vector<unsigned char>> &vParams) const;
773     bool IsPayToCryptoCondition(CScript *ccSubScript) const;
774     bool IsPayToCryptoCondition(uint32_t *ecode) const;
775     bool IsPayToCryptoCondition() const;
776     CScript &ReplaceCCParams(const COptCCParams &params);
777
778     bool IsSpendableOutputType(const COptCCParams &p) const;
779     bool IsSpendableOutputType() const;
780     CCurrencyValueMap ReserveOutValue() const;
781     CCurrencyValueMap ReserveOutValue(COptCCParams &p, bool spendingOnly=false) const;
782     bool SetReserveOutValue(const CCurrencyValueMap &newValue);
783
784     bool IsCoinImport() const;
785     bool MayAcceptCryptoCondition() const;
786     bool MayAcceptCryptoCondition(int evalCode) const;
787     bool IsInstantSpend() const;
788
789     // insightexplorer, there may be more script types in the future
790     enum ScriptType : int {
791         UNKNOWN = 0,
792         P2PKH = 1,  // the same index value is used for all types that can have destinations represented by public key hash
793         P2PK = 1,
794         P2CC = 1,   // CCs are actually not an address type, but as a type of transaction, they are identified historicall as P2PKH. they now can also pay to IDs.
795         P2SH = 2,
796         P2ID = 3,
797         P2IDX = 4,
798         P2QRK = 5,
799     };
800
801     ScriptType GetType() const;
802     uint160 AddressHash() const;
803     std::vector<CTxDestination> GetDestinations() const;
804
805     /** Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical). */
806     bool IsPushOnly() const;
807
808     /** if the front of the script has check lock time verify. this is a fairly simple check.
809      * accepts NULL as parameter if unlockTime is not needed.
810      */
811     bool IsCheckLockTimeVerify(int64_t *unlockTime) const;
812
813     bool IsCheckLockTimeVerify() const;
814
815     /**
816      * Returns whether the script is guaranteed to fail at execution,
817      * regardless of the initial stack. This allows outputs to be pruned
818      * instantly when entering the UTXO set.
819      */
820     bool IsUnspendable() const
821     {
822         return (size() > 0 && *begin() == OP_RETURN) || (size() > MAX_SCRIPT_SIZE);
823     }
824
825     std::string ToString() const;
826
827     void clear()
828     {
829         // The default std::vector::clear() does not release memory.
830         CScriptBase().swap(*this);
831     }
832 };
833
834 class CReserveScript
835 {
836 public:
837     CScript reserveScript;
838     virtual void KeepScript() {}
839     CReserveScript() {}
840     virtual ~CReserveScript() {}
841 };
842
843 #endif // BITCOIN_SCRIPT_SCRIPT_H
This page took 0.071942 seconds and 4 git commands to generate.