Dash Core  0.12.2.1
P2P Digital Currency
script.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 #include "tinyformat.h"
8 #include "utilstrencodings.h"
9 
10 using namespace std;
11 
12 const char* GetOpName(opcodetype opcode)
13 {
14  switch (opcode)
15  {
16  // push value
17  case OP_0 : return "0";
18  case OP_PUSHDATA1 : return "OP_PUSHDATA1";
19  case OP_PUSHDATA2 : return "OP_PUSHDATA2";
20  case OP_PUSHDATA4 : return "OP_PUSHDATA4";
21  case OP_1NEGATE : return "-1";
22  case OP_RESERVED : return "OP_RESERVED";
23  case OP_1 : return "1";
24  case OP_2 : return "2";
25  case OP_3 : return "3";
26  case OP_4 : return "4";
27  case OP_5 : return "5";
28  case OP_6 : return "6";
29  case OP_7 : return "7";
30  case OP_8 : return "8";
31  case OP_9 : return "9";
32  case OP_10 : return "10";
33  case OP_11 : return "11";
34  case OP_12 : return "12";
35  case OP_13 : return "13";
36  case OP_14 : return "14";
37  case OP_15 : return "15";
38  case OP_16 : return "16";
39 
40  // control
41  case OP_NOP : return "OP_NOP";
42  case OP_VER : return "OP_VER";
43  case OP_IF : return "OP_IF";
44  case OP_NOTIF : return "OP_NOTIF";
45  case OP_VERIF : return "OP_VERIF";
46  case OP_VERNOTIF : return "OP_VERNOTIF";
47  case OP_ELSE : return "OP_ELSE";
48  case OP_ENDIF : return "OP_ENDIF";
49  case OP_VERIFY : return "OP_VERIFY";
50  case OP_RETURN : return "OP_RETURN";
51 
52  // stack ops
53  case OP_TOALTSTACK : return "OP_TOALTSTACK";
54  case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
55  case OP_2DROP : return "OP_2DROP";
56  case OP_2DUP : return "OP_2DUP";
57  case OP_3DUP : return "OP_3DUP";
58  case OP_2OVER : return "OP_2OVER";
59  case OP_2ROT : return "OP_2ROT";
60  case OP_2SWAP : return "OP_2SWAP";
61  case OP_IFDUP : return "OP_IFDUP";
62  case OP_DEPTH : return "OP_DEPTH";
63  case OP_DROP : return "OP_DROP";
64  case OP_DUP : return "OP_DUP";
65  case OP_NIP : return "OP_NIP";
66  case OP_OVER : return "OP_OVER";
67  case OP_PICK : return "OP_PICK";
68  case OP_ROLL : return "OP_ROLL";
69  case OP_ROT : return "OP_ROT";
70  case OP_SWAP : return "OP_SWAP";
71  case OP_TUCK : return "OP_TUCK";
72 
73  // splice ops
74  case OP_CAT : return "OP_CAT";
75  case OP_SUBSTR : return "OP_SUBSTR";
76  case OP_LEFT : return "OP_LEFT";
77  case OP_RIGHT : return "OP_RIGHT";
78  case OP_SIZE : return "OP_SIZE";
79 
80  // bit logic
81  case OP_INVERT : return "OP_INVERT";
82  case OP_AND : return "OP_AND";
83  case OP_OR : return "OP_OR";
84  case OP_XOR : return "OP_XOR";
85  case OP_EQUAL : return "OP_EQUAL";
86  case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
87  case OP_RESERVED1 : return "OP_RESERVED1";
88  case OP_RESERVED2 : return "OP_RESERVED2";
89 
90  // numeric
91  case OP_1ADD : return "OP_1ADD";
92  case OP_1SUB : return "OP_1SUB";
93  case OP_2MUL : return "OP_2MUL";
94  case OP_2DIV : return "OP_2DIV";
95  case OP_NEGATE : return "OP_NEGATE";
96  case OP_ABS : return "OP_ABS";
97  case OP_NOT : return "OP_NOT";
98  case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
99  case OP_ADD : return "OP_ADD";
100  case OP_SUB : return "OP_SUB";
101  case OP_MUL : return "OP_MUL";
102  case OP_DIV : return "OP_DIV";
103  case OP_MOD : return "OP_MOD";
104  case OP_LSHIFT : return "OP_LSHIFT";
105  case OP_RSHIFT : return "OP_RSHIFT";
106  case OP_BOOLAND : return "OP_BOOLAND";
107  case OP_BOOLOR : return "OP_BOOLOR";
108  case OP_NUMEQUAL : return "OP_NUMEQUAL";
109  case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
110  case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
111  case OP_LESSTHAN : return "OP_LESSTHAN";
112  case OP_GREATERTHAN : return "OP_GREATERTHAN";
113  case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
114  case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
115  case OP_MIN : return "OP_MIN";
116  case OP_MAX : return "OP_MAX";
117  case OP_WITHIN : return "OP_WITHIN";
118 
119  // crypto
120  case OP_RIPEMD160 : return "OP_RIPEMD160";
121  case OP_SHA1 : return "OP_SHA1";
122  case OP_SHA256 : return "OP_SHA256";
123  case OP_HASH160 : return "OP_HASH160";
124  case OP_HASH256 : return "OP_HASH256";
125  case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
126  case OP_CHECKSIG : return "OP_CHECKSIG";
127  case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
128  case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
129  case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
130 
131  // expanson
132  case OP_NOP1 : return "OP_NOP1";
133  case OP_CHECKLOCKTIMEVERIFY : return "OP_CHECKLOCKTIMEVERIFY";
134  case OP_CHECKSEQUENCEVERIFY : return "OP_CHECKSEQUENCEVERIFY";
135  case OP_NOP4 : return "OP_NOP4";
136  case OP_NOP5 : return "OP_NOP5";
137  case OP_NOP6 : return "OP_NOP6";
138  case OP_NOP7 : return "OP_NOP7";
139  case OP_NOP8 : return "OP_NOP8";
140  case OP_NOP9 : return "OP_NOP9";
141  case OP_NOP10 : return "OP_NOP10";
142 
143  case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
144 
145  // Note:
146  // The template matching params OP_SMALLINTEGER/etc are defined in opcodetype enum
147  // as kind of implementation hack, they are *NOT* real opcodes. If found in real
148  // Script, just let the default: case deal with them.
149 
150  default:
151  return "OP_UNKNOWN";
152  }
153 }
154 
155 unsigned int CScript::GetSigOpCount(bool fAccurate) const
156 {
157  unsigned int n = 0;
158  const_iterator pc = begin();
159  opcodetype lastOpcode = OP_INVALIDOPCODE;
160  while (pc < end())
161  {
162  opcodetype opcode;
163  if (!GetOp(pc, opcode))
164  break;
165  if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
166  n++;
167  else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
168  {
169  if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
170  n += DecodeOP_N(lastOpcode);
171  else
173  }
174  lastOpcode = opcode;
175  }
176  return n;
177 }
178 
179 unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
180 {
181  if (!IsPayToScriptHash())
182  return GetSigOpCount(true);
183 
184  // This is a pay-to-script-hash scriptPubKey;
185  // get the last item that the scriptSig
186  // pushes onto the stack:
187  const_iterator pc = scriptSig.begin();
188  vector<unsigned char> data;
189  while (pc < scriptSig.end())
190  {
191  opcodetype opcode;
192  if (!scriptSig.GetOp(pc, opcode, data))
193  return 0;
194  if (opcode > OP_16)
195  return 0;
196  }
197 
199  CScript subscript(data.begin(), data.end());
200  return subscript.GetSigOpCount(true);
201 }
202 
204 {
205  if(this->size() != 25) return false;
206 
207  std::string str;
208  opcodetype opcode;
209  const_iterator pc = begin();
210  int i = 0;
211  while (pc < end())
212  {
213  GetOp(pc, opcode);
214 
215  if( i == 0 && opcode != OP_DUP) return false;
216  else if(i == 1 && opcode != OP_HASH160) return false;
217  else if(i == 3 && opcode != OP_EQUALVERIFY) return false;
218  else if(i == 4 && opcode != OP_CHECKSIG) return false;
219  else if(i == 5) return false;
220 
221  i++;
222  }
223 
224  return true;
225 }
226 
228 {
229  // Extra-fast test for pay-to-pubkey-hash CScripts:
230  return (this->size() == 25 &&
231  (*this)[0] == OP_DUP &&
232  (*this)[1] == OP_HASH160 &&
233  (*this)[2] == 0x14 &&
234  (*this)[23] == OP_EQUALVERIFY &&
235  (*this)[24] == OP_CHECKSIG);
236 }
237 
239 {
240  // Extra-fast test for pay-to-script-hash CScripts:
241  return (this->size() == 23 &&
242  (*this)[0] == OP_HASH160 &&
243  (*this)[1] == 0x14 &&
244  (*this)[22] == OP_EQUAL);
245 }
246 
248 {
249  while (pc < end())
250  {
251  opcodetype opcode;
252  if (!GetOp(pc, opcode))
253  return false;
254  // Note that IsPushOnly() *does* consider OP_RESERVED to be a
255  // push-type opcode, however execution of OP_RESERVED fails, so
256  // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
257  // the P2SH special validation code being executed.
258  if (opcode > OP_16)
259  return false;
260  }
261  return true;
262 }
263 
265 {
266  return this->IsPushOnly(begin());
267 }
Definition: script.h:126
Definition: script.h:55
Definition: script.h:111
unsigned int GetSigOpCount(bool fAccurate) const
Definition: script.cpp:155
static const int MAX_PUBKEYS_PER_MULTISIG
Definition: script.h:28
bool IsPayToPublicKeyHash() const
Definition: script.cpp:227
Definition: script.h:94
Definition: script.h:147
Definition: script.h:86
Definition: script.h:72
Definition: script.h:65
Definition: script.h:61
Definition: script.h:88
Definition: script.h:125
Definition: script.h:99
Definition: script.h:53
Definition: script.h:132
Definition: script.h:59
Definition: script.h:54
bool IsPushOnly() const
Definition: script.cpp:264
Definition: script.h:146
Definition: script.h:67
Definition: script.h:63
Definition: script.h:112
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
Definition: script.h:473
Definition: script.h:56
iterator end()
Definition: prevector.h:272
Definition: script.h:133
opcodetype
Definition: script.h:41
Definition: script.h:103
Definition: script.h:58
Definition: script.h:51
Definition: script.h:70
Definition: script.h:96
Definition: script.h:95
bool IsNormalPaymentScript() const
Definition: script.cpp:203
Definition: script.h:130
bool IsPayToScriptHash() const
Definition: script.cpp:238
Definition: script.h:98
Definition: script.h:131
Definition: script.h:76
const char * GetOpName(opcodetype opcode)
Definition: script.cpp:12
Definition: script.h:60
Definition: script.h:92
Definition: script.h:85
Definition: script.h:57
iterator begin()
Definition: prevector.h:270
Definition: script.h:64
Definition: script.h:97
Definition: script.h:113
Definition: script.h:62
Definition: script.h:71
Definition: script.h:44
Definition: script.h:129
Definition: script.h:66
Definition: script.h:93