Bitcoin Core  0.20.99
P2P Digital Currency
script.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 <util/strencodings.h>
9 
10 #include <string>
11 
12 std::string 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  // expansion
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  default:
146  return "OP_UNKNOWN";
147  }
148 }
149 
150 unsigned int CScript::GetSigOpCount(bool fAccurate) const
151 {
152  unsigned int n = 0;
153  const_iterator pc = begin();
154  opcodetype lastOpcode = OP_INVALIDOPCODE;
155  while (pc < end())
156  {
157  opcodetype opcode;
158  if (!GetOp(pc, opcode))
159  break;
160  if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
161  n++;
162  else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
163  {
164  if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
165  n += DecodeOP_N(lastOpcode);
166  else
168  }
169  lastOpcode = opcode;
170  }
171  return n;
172 }
173 
174 unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
175 {
176  if (!IsPayToScriptHash())
177  return GetSigOpCount(true);
178 
179  // This is a pay-to-script-hash scriptPubKey;
180  // get the last item that the scriptSig
181  // pushes onto the stack:
182  const_iterator pc = scriptSig.begin();
183  std::vector<unsigned char> vData;
184  while (pc < scriptSig.end())
185  {
186  opcodetype opcode;
187  if (!scriptSig.GetOp(pc, opcode, vData))
188  return 0;
189  if (opcode > OP_16)
190  return 0;
191  }
192 
194  CScript subscript(vData.begin(), vData.end());
195  return subscript.GetSigOpCount(true);
196 }
197 
199 {
200  // Extra-fast test for pay-to-script-hash CScripts:
201  return (this->size() == 23 &&
202  (*this)[0] == OP_HASH160 &&
203  (*this)[1] == 0x14 &&
204  (*this)[22] == OP_EQUAL);
205 }
206 
208 {
209  // Extra-fast test for pay-to-witness-script-hash CScripts:
210  return (this->size() == 34 &&
211  (*this)[0] == OP_0 &&
212  (*this)[1] == 0x20);
213 }
214 
215 // A witness program is any valid CScript that consists of a 1-byte push opcode
216 // followed by a data push between 2 and 40 bytes.
217 bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
218 {
219  if (this->size() < 4 || this->size() > 42) {
220  return false;
221  }
222  if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
223  return false;
224  }
225  if ((size_t)((*this)[1] + 2) == this->size()) {
226  version = DecodeOP_N((opcodetype)(*this)[0]);
227  program = std::vector<unsigned char>(this->begin() + 2, this->end());
228  return true;
229  }
230  return false;
231 }
232 
234 {
235  while (pc < end())
236  {
237  opcodetype opcode;
238  if (!GetOp(pc, opcode))
239  return false;
240  // Note that IsPushOnly() *does* consider OP_RESERVED to be a
241  // push-type opcode, however execution of OP_RESERVED fails, so
242  // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
243  // the P2SH special validation code being executed.
244  if (opcode > OP_16)
245  return false;
246  }
247  return true;
248 }
249 
251 {
252  return this->IsPushOnly(begin());
253 }
254 
255 std::string CScriptWitness::ToString() const
256 {
257  std::string ret = "CScriptWitness(";
258  for (unsigned int i = 0; i < stack.size(); i++) {
259  if (i) {
260  ret += ", ";
261  }
262  ret += HexStr(stack[i]);
263  }
264  return ret + ")";
265 }
266 
268 {
270  while (it < end()) {
271  opcodetype opcode;
272  std::vector<unsigned char> item;
273  if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
274  return false;
275  }
276  }
277  return true;
278 }
279 
280 bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
281 {
282  opcodeRet = OP_INVALIDOPCODE;
283  if (pvchRet)
284  pvchRet->clear();
285  if (pc >= end)
286  return false;
287 
288  // Read instruction
289  if (end - pc < 1)
290  return false;
291  unsigned int opcode = *pc++;
292 
293  // Immediate operand
294  if (opcode <= OP_PUSHDATA4)
295  {
296  unsigned int nSize = 0;
297  if (opcode < OP_PUSHDATA1)
298  {
299  nSize = opcode;
300  }
301  else if (opcode == OP_PUSHDATA1)
302  {
303  if (end - pc < 1)
304  return false;
305  nSize = *pc++;
306  }
307  else if (opcode == OP_PUSHDATA2)
308  {
309  if (end - pc < 2)
310  return false;
311  nSize = ReadLE16(&pc[0]);
312  pc += 2;
313  }
314  else if (opcode == OP_PUSHDATA4)
315  {
316  if (end - pc < 4)
317  return false;
318  nSize = ReadLE32(&pc[0]);
319  pc += 4;
320  }
321  if (end - pc < 0 || (unsigned int)(end - pc) < nSize)
322  return false;
323  if (pvchRet)
324  pvchRet->assign(pc, pc + nSize);
325  pc += nSize;
326  }
327 
328  opcodeRet = static_cast<opcodetype>(opcode);
329  return true;
330 }
Definition: script.h:139
Definition: script.h:68
Definition: script.h:124
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops.
Definition: script.cpp:150
static const int MAX_PUBKEYS_PER_MULTISIG
Definition: script.h:29
Definition: script.h:107
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
Definition: script.h:483
Definition: script.h:160
Definition: script.h:99
static const unsigned int MAX_OPCODE
Definition: script.h:194
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
Definition: script.h:472
bool IsPayToScriptHash() const
Definition: script.cpp:198
Definition: script.h:85
Definition: script.h:78
Definition: script.h:74
Definition: script.h:138
Definition: script.h:66
Definition: script.h:145
Definition: script.h:72
Definition: script.h:67
bool IsPushOnly() const
Definition: script.cpp:250
bool HasValidOps() const
Check if the script contains valid OP_CODES.
Definition: script.cpp:267
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
Definition: script.cpp:217
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:23
Definition: script.h:159
static uint16_t ReadLE16(const unsigned char *ptr)
Definition: common.h:17
Definition: script.h:80
Definition: script.h:76
Definition: script.h:125
Definition: script.h:69
iterator end()
Definition: prevector.h:292
Definition: script.h:146
opcodetype
Script opcodes.
Definition: script.h:54
Definition: script.h:116
Definition: script.h:71
Definition: script.h:64
Definition: script.h:83
bool GetScriptOp(CScriptBase::const_iterator &pc, CScriptBase::const_iterator end, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet)
Definition: script.cpp:280
Definition: script.h:143
Definition: script.h:111
Definition: script.h:144
Definition: script.h:89
static uint32_t ReadLE32(const unsigned char *ptr)
Definition: common.h:24
Definition: script.h:73
Definition: script.h:98
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
std::string GetOpName(opcodetype opcode)
Definition: script.cpp:12
std::string HexStr(const T itbegin, const T itend)
Definition: strencodings.h:123
Definition: script.h:70
iterator begin()
Definition: prevector.h:290
size_type size() const
Definition: prevector.h:282
Definition: script.h:77
Definition: script.h:126
Definition: script.h:75
Definition: script.h:84
auto it
Definition: validation.cpp:383
Definition: script.h:57
std::string ToString() const
Definition: script.cpp:255
Definition: script.h:142
Definition: script.h:79
bool IsPayToWitnessScriptHash() const
Definition: script.cpp:207
Definition: script.h:106