Bitcoin Core  26.1.0
P2P Digital Currency
script.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 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 <hash.h>
9 #include <util/strencodings.h>
10 
11 #include <string>
12 
14 
15 std::string GetOpName(opcodetype opcode)
16 {
17  switch (opcode)
18  {
19  // push value
20  case OP_0 : return "0";
21  case OP_PUSHDATA1 : return "OP_PUSHDATA1";
22  case OP_PUSHDATA2 : return "OP_PUSHDATA2";
23  case OP_PUSHDATA4 : return "OP_PUSHDATA4";
24  case OP_1NEGATE : return "-1";
25  case OP_RESERVED : return "OP_RESERVED";
26  case OP_1 : return "1";
27  case OP_2 : return "2";
28  case OP_3 : return "3";
29  case OP_4 : return "4";
30  case OP_5 : return "5";
31  case OP_6 : return "6";
32  case OP_7 : return "7";
33  case OP_8 : return "8";
34  case OP_9 : return "9";
35  case OP_10 : return "10";
36  case OP_11 : return "11";
37  case OP_12 : return "12";
38  case OP_13 : return "13";
39  case OP_14 : return "14";
40  case OP_15 : return "15";
41  case OP_16 : return "16";
42 
43  // control
44  case OP_NOP : return "OP_NOP";
45  case OP_VER : return "OP_VER";
46  case OP_IF : return "OP_IF";
47  case OP_NOTIF : return "OP_NOTIF";
48  case OP_VERIF : return "OP_VERIF";
49  case OP_VERNOTIF : return "OP_VERNOTIF";
50  case OP_ELSE : return "OP_ELSE";
51  case OP_ENDIF : return "OP_ENDIF";
52  case OP_VERIFY : return "OP_VERIFY";
53  case OP_RETURN : return "OP_RETURN";
54 
55  // stack ops
56  case OP_TOALTSTACK : return "OP_TOALTSTACK";
57  case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
58  case OP_2DROP : return "OP_2DROP";
59  case OP_2DUP : return "OP_2DUP";
60  case OP_3DUP : return "OP_3DUP";
61  case OP_2OVER : return "OP_2OVER";
62  case OP_2ROT : return "OP_2ROT";
63  case OP_2SWAP : return "OP_2SWAP";
64  case OP_IFDUP : return "OP_IFDUP";
65  case OP_DEPTH : return "OP_DEPTH";
66  case OP_DROP : return "OP_DROP";
67  case OP_DUP : return "OP_DUP";
68  case OP_NIP : return "OP_NIP";
69  case OP_OVER : return "OP_OVER";
70  case OP_PICK : return "OP_PICK";
71  case OP_ROLL : return "OP_ROLL";
72  case OP_ROT : return "OP_ROT";
73  case OP_SWAP : return "OP_SWAP";
74  case OP_TUCK : return "OP_TUCK";
75 
76  // splice ops
77  case OP_CAT : return "OP_CAT";
78  case OP_SUBSTR : return "OP_SUBSTR";
79  case OP_LEFT : return "OP_LEFT";
80  case OP_RIGHT : return "OP_RIGHT";
81  case OP_SIZE : return "OP_SIZE";
82 
83  // bit logic
84  case OP_INVERT : return "OP_INVERT";
85  case OP_AND : return "OP_AND";
86  case OP_OR : return "OP_OR";
87  case OP_XOR : return "OP_XOR";
88  case OP_EQUAL : return "OP_EQUAL";
89  case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
90  case OP_RESERVED1 : return "OP_RESERVED1";
91  case OP_RESERVED2 : return "OP_RESERVED2";
92 
93  // numeric
94  case OP_1ADD : return "OP_1ADD";
95  case OP_1SUB : return "OP_1SUB";
96  case OP_2MUL : return "OP_2MUL";
97  case OP_2DIV : return "OP_2DIV";
98  case OP_NEGATE : return "OP_NEGATE";
99  case OP_ABS : return "OP_ABS";
100  case OP_NOT : return "OP_NOT";
101  case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
102  case OP_ADD : return "OP_ADD";
103  case OP_SUB : return "OP_SUB";
104  case OP_MUL : return "OP_MUL";
105  case OP_DIV : return "OP_DIV";
106  case OP_MOD : return "OP_MOD";
107  case OP_LSHIFT : return "OP_LSHIFT";
108  case OP_RSHIFT : return "OP_RSHIFT";
109  case OP_BOOLAND : return "OP_BOOLAND";
110  case OP_BOOLOR : return "OP_BOOLOR";
111  case OP_NUMEQUAL : return "OP_NUMEQUAL";
112  case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
113  case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
114  case OP_LESSTHAN : return "OP_LESSTHAN";
115  case OP_GREATERTHAN : return "OP_GREATERTHAN";
116  case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
117  case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
118  case OP_MIN : return "OP_MIN";
119  case OP_MAX : return "OP_MAX";
120  case OP_WITHIN : return "OP_WITHIN";
121 
122  // crypto
123  case OP_RIPEMD160 : return "OP_RIPEMD160";
124  case OP_SHA1 : return "OP_SHA1";
125  case OP_SHA256 : return "OP_SHA256";
126  case OP_HASH160 : return "OP_HASH160";
127  case OP_HASH256 : return "OP_HASH256";
128  case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
129  case OP_CHECKSIG : return "OP_CHECKSIG";
130  case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
131  case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
132  case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
133 
134  // expansion
135  case OP_NOP1 : return "OP_NOP1";
136  case OP_CHECKLOCKTIMEVERIFY : return "OP_CHECKLOCKTIMEVERIFY";
137  case OP_CHECKSEQUENCEVERIFY : return "OP_CHECKSEQUENCEVERIFY";
138  case OP_NOP4 : return "OP_NOP4";
139  case OP_NOP5 : return "OP_NOP5";
140  case OP_NOP6 : return "OP_NOP6";
141  case OP_NOP7 : return "OP_NOP7";
142  case OP_NOP8 : return "OP_NOP8";
143  case OP_NOP9 : return "OP_NOP9";
144  case OP_NOP10 : return "OP_NOP10";
145 
146  // Opcode added by BIP 342 (Tapscript)
147  case OP_CHECKSIGADD : return "OP_CHECKSIGADD";
148 
149  case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
150 
151  default:
152  return "OP_UNKNOWN";
153  }
154 }
155 
156 unsigned int CScript::GetSigOpCount(bool fAccurate) const
157 {
158  unsigned int n = 0;
159  const_iterator pc = begin();
160  opcodetype lastOpcode = OP_INVALIDOPCODE;
161  while (pc < end())
162  {
163  opcodetype opcode;
164  if (!GetOp(pc, opcode))
165  break;
166  if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
167  n++;
168  else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
169  {
170  if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
171  n += DecodeOP_N(lastOpcode);
172  else
174  }
175  lastOpcode = opcode;
176  }
177  return n;
178 }
179 
180 unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
181 {
182  if (!IsPayToScriptHash())
183  return GetSigOpCount(true);
184 
185  // This is a pay-to-script-hash scriptPubKey;
186  // get the last item that the scriptSig
187  // pushes onto the stack:
188  const_iterator pc = scriptSig.begin();
189  std::vector<unsigned char> vData;
190  while (pc < scriptSig.end())
191  {
192  opcodetype opcode;
193  if (!scriptSig.GetOp(pc, opcode, vData))
194  return 0;
195  if (opcode > OP_16)
196  return 0;
197  }
198 
200  CScript subscript(vData.begin(), vData.end());
201  return subscript.GetSigOpCount(true);
202 }
203 
205 {
206  // Extra-fast test for pay-to-script-hash CScripts:
207  return (this->size() == 23 &&
208  (*this)[0] == OP_HASH160 &&
209  (*this)[1] == 0x14 &&
210  (*this)[22] == OP_EQUAL);
211 }
212 
214 {
215  // Extra-fast test for pay-to-witness-script-hash CScripts:
216  return (this->size() == 34 &&
217  (*this)[0] == OP_0 &&
218  (*this)[1] == 0x20);
219 }
220 
221 // A witness program is any valid CScript that consists of a 1-byte push opcode
222 // followed by a data push between 2 and 40 bytes.
223 bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
224 {
225  if (this->size() < 4 || this->size() > 42) {
226  return false;
227  }
228  if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
229  return false;
230  }
231  if ((size_t)((*this)[1] + 2) == this->size()) {
232  version = DecodeOP_N((opcodetype)(*this)[0]);
233  program = std::vector<unsigned char>(this->begin() + 2, this->end());
234  return true;
235  }
236  return false;
237 }
238 
240 {
241  while (pc < end())
242  {
243  opcodetype opcode;
244  if (!GetOp(pc, opcode))
245  return false;
246  // Note that IsPushOnly() *does* consider OP_RESERVED to be a
247  // push-type opcode, however execution of OP_RESERVED fails, so
248  // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
249  // the P2SH special validation code being executed.
250  if (opcode > OP_16)
251  return false;
252  }
253  return true;
254 }
255 
257 {
258  return this->IsPushOnly(begin());
259 }
260 
261 std::string CScriptWitness::ToString() const
262 {
263  std::string ret = "CScriptWitness(";
264  for (unsigned int i = 0; i < stack.size(); i++) {
265  if (i) {
266  ret += ", ";
267  }
268  ret += HexStr(stack[i]);
269  }
270  return ret + ")";
271 }
272 
274 {
276  while (it < end()) {
277  opcodetype opcode;
278  std::vector<unsigned char> item;
279  if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
280  return false;
281  }
282  }
283  return true;
284 }
285 
286 bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
287 {
288  opcodeRet = OP_INVALIDOPCODE;
289  if (pvchRet)
290  pvchRet->clear();
291  if (pc >= end)
292  return false;
293 
294  // Read instruction
295  if (end - pc < 1)
296  return false;
297  unsigned int opcode = *pc++;
298 
299  // Immediate operand
300  if (opcode <= OP_PUSHDATA4)
301  {
302  unsigned int nSize = 0;
303  if (opcode < OP_PUSHDATA1)
304  {
305  nSize = opcode;
306  }
307  else if (opcode == OP_PUSHDATA1)
308  {
309  if (end - pc < 1)
310  return false;
311  nSize = *pc++;
312  }
313  else if (opcode == OP_PUSHDATA2)
314  {
315  if (end - pc < 2)
316  return false;
317  nSize = ReadLE16(&pc[0]);
318  pc += 2;
319  }
320  else if (opcode == OP_PUSHDATA4)
321  {
322  if (end - pc < 4)
323  return false;
324  nSize = ReadLE32(&pc[0]);
325  pc += 4;
326  }
327  if (end - pc < 0 || (unsigned int)(end - pc) < nSize)
328  return false;
329  if (pvchRet)
330  pvchRet->assign(pc, pc + nSize);
331  pc += nSize;
332  }
333 
334  opcodeRet = static_cast<opcodetype>(opcode);
335  return true;
336 }
337 
338 bool IsOpSuccess(const opcodetype& opcode)
339 {
340  return opcode == 80 || opcode == 98 || (opcode >= 126 && opcode <= 129) ||
341  (opcode >= 131 && opcode <= 134) || (opcode >= 137 && opcode <= 138) ||
342  (opcode >= 141 && opcode <= 142) || (opcode >= 149 && opcode <= 153) ||
343  (opcode >= 187 && opcode <= 254);
344 }
345 
346 bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode) {
347  // Excludes OP_1NEGATE, OP_1-16 since they are by definition minimal
348  assert(0 <= opcode && opcode <= OP_PUSHDATA4);
349  if (data.size() == 0) {
350  // Should have used OP_0.
351  return opcode == OP_0;
352  } else if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) {
353  // Should have used OP_1 .. OP_16.
354  return false;
355  } else if (data.size() == 1 && data[0] == 0x81) {
356  // Should have used OP_1NEGATE.
357  return false;
358  } else if (data.size() <= 75) {
359  // Must have used a direct push (opcode indicating number of bytes pushed + those bytes).
360  return opcode == data.size();
361  } else if (data.size() <= 255) {
362  // Must have used OP_PUSHDATA.
363  return opcode == OP_PUSHDATA1;
364  } else if (data.size() <= 65535) {
365  // Must have used OP_PUSHDATA2.
366  return opcode == OP_PUSHDATA2;
367  }
368  return true;
369 }
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops.
Definition: script.cpp:156
Definition: script.h:155
Definition: script.h:143
int ret
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
Definition: script.h:505
bool CheckMinimalPush(const std::vector< unsigned char > &data, opcodetype opcode)
Definition: script.cpp:346
assert(!tx.IsCoinBase())
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
Definition: script.h:494
Definition: script.h:124
Definition: script.h:123
bool IsPayToScriptHash() const
Definition: script.cpp:204
bool GetScriptOp(CScriptBase::const_iterator &pc, CScriptBase::const_iterator end, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet)
Definition: script.cpp:286
Definition: script.h:159
bool IsPushOnly() const
Definition: script.cpp:256
std::vector< std::vector< unsigned char > > stack
Definition: script.h:568
Definition: script.h:142
bool HasValidOps() const
Check if the script contains valid OP_CODES.
Definition: script.cpp:273
Definition: script.h:160
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
Definition: script.cpp:223
Definition: script.h:133
Definition: script.h:74
CScriptID()
Definition: script.h:584
Definition: script.h:100
static const int MAX_PUBKEYS_PER_MULTISIG
Definition: script.h:32
Definition: script.h:101
Definition: script.h:86
Definition: script.h:81
Definition: script.h:162
Definition: script.h:91
Definition: script.h:102
iterator end()
Definition: prevector.h:301
Definition: script.h:92
Definition: script.h:176
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
opcodetype
Script opcodes.
Definition: script.h:71
Definition: script.h:128
Definition: script.h:163
Definition: script.h:177
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition: hash.h:93
Definition: script.h:88
Definition: script.h:94
Definition: script.h:90
std::string GetOpName(opcodetype opcode)
Definition: script.cpp:15
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:412
Definition: script.h:84
Definition: script.h:96
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:26
Definition: script.h:95
static uint32_t ReadLE32(const unsigned char *ptr)
Definition: common.h:24
Definition: script.h:89
Definition: script.h:161
iterator begin()
Definition: prevector.h:299
size_type size() const
Definition: prevector.h:291
Definition: script.h:83
Definition: script.h:97
static uint16_t ReadLE16(const unsigned char *ptr)
Definition: common.h:17
static const unsigned int MAX_OPCODE
Definition: script.h:214
std::string ToString() const
Definition: script.cpp:261
Definition: script.h:156
Definition: script.h:141
Definition: script.h:87
bool IsOpSuccess(const opcodetype &opcode)
Test for OP_SUCCESSx opcodes as defined by BIP342.
Definition: script.cpp:338
bool IsPayToWitnessScriptHash() const
Definition: script.cpp:213
Definition: script.h:85
Definition: script.h:93