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