]> Git Repo - VerusCoin.git/blob - src/script/script.cpp
Enable time locked coin bases to be used as normal coinbase transactions with longer...
[VerusCoin.git] / src / script / script.cpp
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 http://www.opensource.org/licenses/mit-license.php.
5
6 #include "script.h"
7
8 #include "tinyformat.h"
9 #include "utilstrencodings.h"
10 #include "script/cc.h"
11 #include "cryptoconditions/include/cryptoconditions.h"
12
13 namespace {
14 inline std::string ValueString(const std::vector<unsigned char>& vch)
15 {
16     if (vch.size() <= 4)
17         return strprintf("%d", CScriptNum(vch, false).getint());
18     else
19         return HexStr(vch);
20 }
21 } // anon namespace
22
23 using namespace std;
24
25 const char* GetOpName(opcodetype opcode)
26 {
27     switch (opcode)
28     {
29     // push value
30     case OP_0                      : return "0";
31     case OP_PUSHDATA1              : return "OP_PUSHDATA1";
32     case OP_PUSHDATA2              : return "OP_PUSHDATA2";
33     case OP_PUSHDATA4              : return "OP_PUSHDATA4";
34     case OP_1NEGATE                : return "-1";
35     case OP_RESERVED               : return "OP_RESERVED";
36     case OP_1                      : return "1";
37     case OP_2                      : return "2";
38     case OP_3                      : return "3";
39     case OP_4                      : return "4";
40     case OP_5                      : return "5";
41     case OP_6                      : return "6";
42     case OP_7                      : return "7";
43     case OP_8                      : return "8";
44     case OP_9                      : return "9";
45     case OP_10                     : return "10";
46     case OP_11                     : return "11";
47     case OP_12                     : return "12";
48     case OP_13                     : return "13";
49     case OP_14                     : return "14";
50     case OP_15                     : return "15";
51     case OP_16                     : return "16";
52
53     // control
54     case OP_NOP                    : return "OP_NOP";
55     case OP_VER                    : return "OP_VER";
56     case OP_IF                     : return "OP_IF";
57     case OP_NOTIF                  : return "OP_NOTIF";
58     case OP_VERIF                  : return "OP_VERIF";
59     case OP_VERNOTIF               : return "OP_VERNOTIF";
60     case OP_ELSE                   : return "OP_ELSE";
61     case OP_ENDIF                  : return "OP_ENDIF";
62     case OP_VERIFY                 : return "OP_VERIFY";
63     case OP_RETURN                 : return "OP_RETURN";
64
65     // stack ops
66     case OP_TOALTSTACK             : return "OP_TOALTSTACK";
67     case OP_FROMALTSTACK           : return "OP_FROMALTSTACK";
68     case OP_2DROP                  : return "OP_2DROP";
69     case OP_2DUP                   : return "OP_2DUP";
70     case OP_3DUP                   : return "OP_3DUP";
71     case OP_2OVER                  : return "OP_2OVER";
72     case OP_2ROT                   : return "OP_2ROT";
73     case OP_2SWAP                  : return "OP_2SWAP";
74     case OP_IFDUP                  : return "OP_IFDUP";
75     case OP_DEPTH                  : return "OP_DEPTH";
76     case OP_DROP                   : return "OP_DROP";
77     case OP_DUP                    : return "OP_DUP";
78     case OP_NIP                    : return "OP_NIP";
79     case OP_OVER                   : return "OP_OVER";
80     case OP_PICK                   : return "OP_PICK";
81     case OP_ROLL                   : return "OP_ROLL";
82     case OP_ROT                    : return "OP_ROT";
83     case OP_SWAP                   : return "OP_SWAP";
84     case OP_TUCK                   : return "OP_TUCK";
85
86     // splice ops
87     case OP_CAT                    : return "OP_CAT";
88     case OP_SUBSTR                 : return "OP_SUBSTR";
89     case OP_LEFT                   : return "OP_LEFT";
90     case OP_RIGHT                  : return "OP_RIGHT";
91     case OP_SIZE                   : return "OP_SIZE";
92
93     // bit logic
94     case OP_INVERT                 : return "OP_INVERT";
95     case OP_AND                    : return "OP_AND";
96     case OP_OR                     : return "OP_OR";
97     case OP_XOR                    : return "OP_XOR";
98     case OP_EQUAL                  : return "OP_EQUAL";
99     case OP_EQUALVERIFY            : return "OP_EQUALVERIFY";
100     case OP_RESERVED1              : return "OP_RESERVED1";
101     case OP_RESERVED2              : return "OP_RESERVED2";
102
103     // numeric
104     case OP_1ADD                   : return "OP_1ADD";
105     case OP_1SUB                   : return "OP_1SUB";
106     case OP_2MUL                   : return "OP_2MUL";
107     case OP_2DIV                   : return "OP_2DIV";
108     case OP_NEGATE                 : return "OP_NEGATE";
109     case OP_ABS                    : return "OP_ABS";
110     case OP_NOT                    : return "OP_NOT";
111     case OP_0NOTEQUAL              : return "OP_0NOTEQUAL";
112     case OP_ADD                    : return "OP_ADD";
113     case OP_SUB                    : return "OP_SUB";
114     case OP_MUL                    : return "OP_MUL";
115     case OP_DIV                    : return "OP_DIV";
116     case OP_MOD                    : return "OP_MOD";
117     case OP_LSHIFT                 : return "OP_LSHIFT";
118     case OP_RSHIFT                 : return "OP_RSHIFT";
119     case OP_BOOLAND                : return "OP_BOOLAND";
120     case OP_BOOLOR                 : return "OP_BOOLOR";
121     case OP_NUMEQUAL               : return "OP_NUMEQUAL";
122     case OP_NUMEQUALVERIFY         : return "OP_NUMEQUALVERIFY";
123     case OP_NUMNOTEQUAL            : return "OP_NUMNOTEQUAL";
124     case OP_LESSTHAN               : return "OP_LESSTHAN";
125     case OP_GREATERTHAN            : return "OP_GREATERTHAN";
126     case OP_LESSTHANOREQUAL        : return "OP_LESSTHANOREQUAL";
127     case OP_GREATERTHANOREQUAL     : return "OP_GREATERTHANOREQUAL";
128     case OP_MIN                    : return "OP_MIN";
129     case OP_MAX                    : return "OP_MAX";
130     case OP_WITHIN                 : return "OP_WITHIN";
131
132     // crypto
133     case OP_RIPEMD160              : return "OP_RIPEMD160";
134     case OP_SHA1                   : return "OP_SHA1";
135     case OP_SHA256                 : return "OP_SHA256";
136     case OP_HASH160                : return "OP_HASH160";
137     case OP_HASH256                : return "OP_HASH256";
138     case OP_CODESEPARATOR          : return "OP_CODESEPARATOR";
139     case OP_CHECKSIG               : return "OP_CHECKSIG";
140     case OP_CHECKSIGVERIFY         : return "OP_CHECKSIGVERIFY";
141     case OP_CHECKMULTISIG          : return "OP_CHECKMULTISIG";
142     case OP_CHECKMULTISIGVERIFY    : return "OP_CHECKMULTISIGVERIFY";
143     case OP_CHECKCRYPTOCONDITION   : return "OP_CHECKCRYPTOCONDITION";
144     case OP_CHECKCRYPTOCONDITIONVERIFY
145                                    : return "OP_CHECKCRYPTOCONDITIONVERIFY";
146
147     // expansion
148     case OP_NOP1                   : return "OP_NOP1";
149     case OP_NOP2                   : return "OP_NOP2";
150     case OP_NOP3                   : return "OP_NOP3";
151     case OP_NOP4                   : return "OP_NOP4";
152     case OP_NOP5                   : return "OP_NOP5";
153     case OP_NOP6                   : return "OP_NOP6";
154     case OP_NOP7                   : return "OP_NOP7";
155     case OP_NOP8                   : return "OP_NOP8";
156     case OP_NOP9                   : return "OP_NOP9";
157     case OP_NOP10                  : return "OP_NOP10";
158
159     case OP_INVALIDOPCODE          : return "OP_INVALIDOPCODE";
160
161     // Note:
162     //  The template matching params OP_SMALLDATA/etc are defined in opcodetype enum
163     //  as kind of implementation hack, they are *NOT* real opcodes.  If found in real
164     //  Script, just let the default: case deal with them.
165
166     default:
167         return "OP_UNKNOWN";
168     }
169 }
170
171 unsigned int CScript::GetSigOpCount(bool fAccurate) const
172 {
173     unsigned int n = 0;
174     const_iterator pc = begin();
175     opcodetype lastOpcode = OP_INVALIDOPCODE;
176     while (pc < end())
177     {
178         opcodetype opcode;
179         if (!GetOp(pc, opcode))
180             break;
181         if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
182             n++;
183         else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
184         {
185             if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
186                 n += DecodeOP_N(lastOpcode);
187             else
188                 n += 20;
189         }
190         lastOpcode = opcode;
191     }
192     return n;
193 }
194
195 unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
196 {
197     if (!IsPayToScriptHash())
198         return GetSigOpCount(true);
199
200     // This is a pay-to-script-hash scriptPubKey;
201     // get the last item that the scriptSig
202     // pushes onto the stack:
203     const_iterator pc = scriptSig.begin();
204     vector<unsigned char> data;
205     while (pc < scriptSig.end())
206     {
207         opcodetype opcode;
208         if (!scriptSig.GetOp(pc, opcode, data))
209             return 0;
210         if (opcode > OP_16)
211             return 0;
212     }
213
214     /// ... and return its opcount:
215     CScript subscript(data.begin(), data.end());
216     return subscript.GetSigOpCount(true);
217 }
218
219 bool CScript::IsPayToPublicKeyHash() const
220 {
221     // Extra-fast test for pay-to-pubkey-hash CScripts:
222     return (this->size() == 25 &&
223             (*this)[0] == OP_DUP &&
224             (*this)[1] == OP_HASH160 &&
225             (*this)[2] == 0x14 &&
226             (*this)[23] == OP_EQUALVERIFY &&
227             (*this)[24] == OP_CHECKSIG);
228 }
229
230 bool CScript::IsPayToScriptHash() const
231 {
232     // Extra-fast test for pay-to-script-hash CScripts:
233     return (this->size() == 23 &&
234             this->at(0) == OP_HASH160 &&
235             this->at(1) == 0x14 &&
236             this->at(22) == OP_EQUAL);
237 }
238
239 bool CScript::IsPayToCryptoCondition() const
240 {
241     const_iterator pc = this->begin();
242     vector<unsigned char> data;
243     opcodetype opcode;
244     if (this->GetOp(pc, opcode, data))
245         // Sha256 conditions are <76 bytes
246         if (opcode > OP_0 && opcode < OP_PUSHDATA1)
247             if (this->GetOp(pc, opcode, data))
248                 if (opcode == OP_CHECKCRYPTOCONDITION)
249                     if (pc == this->end())
250                         return 1;
251     return 0;
252 }
253
254 bool CScript::MayAcceptCryptoCondition() const
255 {
256     // Get the type mask of the condition
257     const_iterator pc = this->begin();
258     vector<unsigned char> data;
259     opcodetype opcode;
260     if (!this->GetOp(pc, opcode, data)) return false;
261     if (!(opcode > OP_0 && opcode < OP_PUSHDATA1)) return false;
262     CC *cond = cc_readConditionBinary(data.data(), data.size());
263     if (!cond) return false;
264     bool out = IsSupportedCryptoCondition(cond);
265     cc_free(cond);
266     return out;
267 }
268
269 bool CScript::IsPushOnly() const
270 {
271     const_iterator pc = begin();
272     while (pc < end())
273     {
274         opcodetype opcode;
275         if (!GetOp(pc, opcode))
276             return false;
277         // Note that IsPushOnly() *does* consider OP_RESERVED to be a
278         // push-type opcode, however execution of OP_RESERVED fails, so
279         // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
280         // the P2SH special validation code being executed.
281         if (opcode > OP_16)
282             return false;
283     }
284     return true;
285 }
286
287 // if the front of the script has check lock time verify. this is a fairly simple check.
288 // accepts NULL as parameter if unlockTime is not needed.
289 bool CScript::IsCheckLockTimeVerify(int64_t *unlockTime) const
290 {
291     opcodetype op;
292     std::vector<unsigned char> unlockTimeParam = std::vector<unsigned char>();
293     CScript::const_iterator it = this->begin();
294
295     if (this->GetOp2(it, op, &unlockTimeParam))
296     {
297         if (unlockTimeParam.size() >= 0 && unlockTimeParam.size() < 6 &&
298             *(this->data() + unlockTimeParam.size() + 1) == OP_CHECKLOCKTIMEVERIFY)
299         {
300             int i = unlockTimeParam.size() - 1;
301             for (*unlockTime = 0; i >= 0; i--)
302             {
303                 *unlockTime <<= 8;
304                 *unlockTime |= *((unsigned char *)unlockTimeParam.data() + i);
305             }
306             return true;
307         }
308     }
309     return false;
310 }
311
312 bool CScript::IsCheckLockTimeVerify() const
313 {
314     int64_t ult;
315     return this->IsCheckLockTimeVerify(&ult);
316 }
317
318 std::string CScript::ToString() const
319 {
320     std::string str;
321     opcodetype opcode;
322     std::vector<unsigned char> vch;
323     const_iterator pc = begin();
324     while (pc < end())
325     {
326         if (!str.empty())
327             str += " ";
328         if (!GetOp(pc, opcode, vch))
329         {
330             str += "[error]";
331             return str;
332         }
333         if (0 <= opcode && opcode <= OP_PUSHDATA4)
334             str += ValueString(vch);
335         else
336             str += GetOpName(opcode);
337     }
338     return str;
339 }
This page took 0.046048 seconds and 4 git commands to generate.