BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
script.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 
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_SHA3 : return "OP_SHA3";
121  case OP_SHA256 : return "OP_SHA256";
122  case OP_HASH360 : return "OP_HASH360";
123  case OP_HASH3 : return "OP_HASH3";
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  case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
143 
144  default:
145  return "OP_UNKNOWN";
146  }
147 }
148 
149 unsigned int CScript::GetSigOpCount(bool fAccurate) const
150 {
151  unsigned int n = 0;
152  const_iterator pc = begin();
153  opcodetype lastOpcode = OP_INVALIDOPCODE;
154  while (pc < end())
155  {
156  opcodetype opcode;
157  if (!GetOp(pc, opcode))
158  break;
159  if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
160  n++;
161  else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
162  {
163  if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
164  n += DecodeOP_N(lastOpcode);
165  else
166  n += MAX_PUBKEYS_PER_MULTISIG;
167  }
168  lastOpcode = opcode;
169  }
170  return n;
171 }
172 
173 unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
174 {
175  if (!IsPayToScriptHash())
176  return GetSigOpCount(true);
177 
178  // This is a pay-to-script-hash scriptPubKey;
179  // get the last item that the scriptSig
180  // pushes onto the stack:
181  const_iterator pc = scriptSig.begin();
182  std::vector<unsigned char> vData;
183  while (pc < scriptSig.end())
184  {
185  opcodetype opcode;
186  if (!scriptSig.GetOp(pc, opcode, vData))
187  return 0;
188  if (opcode > OP_16)
189  return 0;
190  }
191 
193  CScript subscript(vData.begin(), vData.end());
194  return subscript.GetSigOpCount(true);
195 }
196 
198 {
199  // Extra-fast test for pay-to-script-hash CScripts:
200  return (this->size() == 23 &&
201  (*this)[0] == OP_HASH360 &&
202  (*this)[1] == 0x14 &&
203  (*this)[22] == OP_EQUAL);
204 }
205 
207 {
208  // Extra-fast test for pay-to-witness-script-hash CScripts:
209  return (this->size() == 34 &&
210  (*this)[0] == OP_0 &&
211  (*this)[1] == 0x20);
212 }
213 
214 // A witness program is any valid CScript that consists of a 1-byte push opcode
215 // followed by a data push between 2 and 40 bytes.
216 bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
217 {
218  if (this->size() < 4 || this->size() > 42) {
219  return false;
220  }
221  if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
222  return false;
223  }
224  if ((size_t)((*this)[1] + 2) == this->size()) {
225  version = DecodeOP_N((opcodetype)(*this)[0]);
226  program = std::vector<unsigned char>(this->begin() + 2, this->end());
227  return true;
228  }
229  return false;
230 }
231 
233 {
234  while (pc < end())
235  {
236  opcodetype opcode;
237  if (!GetOp(pc, opcode))
238  return false;
239  // Note that IsPushOnly() *does* consider OP_RESERVED to be a
240  // push-type opcode, however execution of OP_RESERVED fails, so
241  // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
242  // the P2SH special validation code being executed.
243  if (opcode > OP_16)
244  return false;
245  }
246  return true;
247 }
248 
250 {
251  return this->IsPushOnly(begin());
252 }
253 
254 std::string CScriptWitness::ToString() const
255 {
256  std::string ret = "CScriptWitness(";
257  for (unsigned int i = 0; i < stack.size(); i++) {
258  if (i) {
259  ret += ", ";
260  }
261  ret += HexStr(stack[i]);
262  }
263  return ret + ")";
264 }
265 
267 {
269  while (it < end()) {
270  opcodetype opcode;
271  std::vector<unsigned char> item;
272  if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
273  return false;
274  }
275  }
276  return true;
277 }
278 
279 bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
280 {
281  opcodeRet = OP_INVALIDOPCODE;
282  if (pvchRet)
283  pvchRet->clear();
284  if (pc >= end)
285  return false;
286 
287  // Read instruction
288  if (end - pc < 1)
289  return false;
290  unsigned int opcode = *pc++;
291 
292  // Immediate operand
293  if (opcode <= OP_PUSHDATA4)
294  {
295  unsigned int nSize = 0;
296  if (opcode < OP_PUSHDATA1)
297  {
298  nSize = opcode;
299  }
300  else if (opcode == OP_PUSHDATA1)
301  {
302  if (end - pc < 1)
303  return false;
304  nSize = *pc++;
305  }
306  else if (opcode == OP_PUSHDATA2)
307  {
308  if (end - pc < 2)
309  return false;
310  nSize = ReadLE16(&pc[0]);
311  pc += 2;
312  }
313  else if (opcode == OP_PUSHDATA4)
314  {
315  if (end - pc < 4)
316  return false;
317  nSize = ReadLE32(&pc[0]);
318  pc += 4;
319  }
320  if (end - pc < 0 || (unsigned int)(end - pc) < nSize)
321  return false;
322  if (pvchRet)
323  pvchRet->assign(pc, pc + nSize);
324  pc += nSize;
325  }
326 
327  opcodeRet = static_cast<opcodetype>(opcode);
328  return true;
329 }
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:149
Definition: script.h:101
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
Definition: script.h:503
Definition: script.h:154
Definition: script.h:93
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
Definition: script.h:491
bool IsPayToScriptHash() const
Definition: script.cpp:197
Definition: script.h:79
Definition: script.h:72
Definition: script.h:68
Definition: script.h:95
UniValue ret(UniValue::VARR)
Definition: rpcwallet.cpp:1140
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
bool IsPushOnly() const
Definition: script.cpp:249
std::vector< std::vector< unsigned char > > stack
Definition: script.h:566
bool HasValidOps() const
Check if the script contains valid OP_CODES.
Definition: script.cpp:266
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
Definition: script.cpp:216
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:303
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
bool GetScriptOp(CScriptBase::const_iterator &pc, CScriptBase::const_iterator end, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet)
Definition: script.cpp:279
auto end
Definition: rpcwallet.cpp:1068
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
Definition: script.h:67
Definition: script.h:99
Definition: script.h:92
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:384
Definition: script.h:64
iterator begin()
Definition: prevector.h:301
size_type size() const
Definition: prevector.h:293
Definition: script.h:71
Definition: script.h:120
Definition: script.h:69
Definition: script.h:78
Definition: script.h:51
std::string ToString() const
Definition: script.cpp:254
Definition: script.h:136
Definition: script.h:73
bool IsPayToWitnessScriptHash() const
Definition: script.cpp:206
Definition: script.h:100