Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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