Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present 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 <crypto/common.h>
9 : : #include <crypto/hex_base.h>
10 : : #include <hash.h>
11 : : #include <uint256.h>
12 : : #include <util/hash_type.h>
13 : :
14 : : #include <string>
15 : :
16 : 144510 : CScriptID::CScriptID(const CScript& in) : BaseHash(Hash160(in)) {}
17 : :
18 : 6113273 : std::string GetOpName(opcodetype opcode)
19 : : {
20 [ + + + + : 6113273 : switch (opcode)
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+ ]
21 : : {
22 : : // push value
23 : 1229 : case OP_0 : return "0";
24 : 1 : case OP_PUSHDATA1 : return "OP_PUSHDATA1";
25 : 1 : case OP_PUSHDATA2 : return "OP_PUSHDATA2";
26 : 2 : case OP_PUSHDATA4 : return "OP_PUSHDATA4";
27 : 6686 : case OP_1NEGATE : return "-1";
28 : 68730 : case OP_RESERVED : return "OP_RESERVED";
29 : 140167 : case OP_1 : return "1";
30 : 116553 : case OP_2 : return "2";
31 : 43346 : case OP_3 : return "3";
32 : 14847 : case OP_4 : return "4";
33 : 14512 : case OP_5 : return "5";
34 : 6860 : case OP_6 : return "6";
35 : 13236 : case OP_7 : return "7";
36 : 93430 : case OP_8 : return "8";
37 : 14517 : case OP_9 : return "9";
38 : 7512 : case OP_10 : return "10";
39 : 45493 : case OP_11 : return "11";
40 : 10445 : case OP_12 : return "12";
41 : 33314 : case OP_13 : return "13";
42 : 10058 : case OP_14 : return "14";
43 : 17115 : case OP_15 : return "15";
44 : 9167 : case OP_16 : return "16";
45 : :
46 : : // control
47 : 619468 : case OP_NOP : return "OP_NOP";
48 : 9904 : case OP_VER : return "OP_VER";
49 : 18062 : case OP_IF : return "OP_IF";
50 : 13816 : case OP_NOTIF : return "OP_NOTIF";
51 : 13059 : case OP_VERIF : return "OP_VERIF";
52 : 21493 : case OP_VERNOTIF : return "OP_VERNOTIF";
53 : 92313 : case OP_ELSE : return "OP_ELSE";
54 : 20895 : case OP_ENDIF : return "OP_ENDIF";
55 : 42704 : case OP_VERIFY : return "OP_VERIFY";
56 : 75158 : case OP_RETURN : return "OP_RETURN";
57 : :
58 : : // stack ops
59 : 16245 : case OP_TOALTSTACK : return "OP_TOALTSTACK";
60 : 14030 : case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
61 : 10606 : case OP_2DROP : return "OP_2DROP";
62 : 7981 : case OP_2DUP : return "OP_2DUP";
63 : 34809 : case OP_3DUP : return "OP_3DUP";
64 : 12776 : case OP_2OVER : return "OP_2OVER";
65 : 9345 : case OP_2ROT : return "OP_2ROT";
66 : 19507 : case OP_2SWAP : return "OP_2SWAP";
67 : 19289 : case OP_IFDUP : return "OP_IFDUP";
68 : 15529 : case OP_DEPTH : return "OP_DEPTH";
69 : 11817 : case OP_DROP : return "OP_DROP";
70 : 30612 : case OP_DUP : return "OP_DUP";
71 : 90896 : case OP_NIP : return "OP_NIP";
72 : 8374 : case OP_OVER : return "OP_OVER";
73 : 11065 : case OP_PICK : return "OP_PICK";
74 : 14485 : case OP_ROLL : return "OP_ROLL";
75 : 32554 : case OP_ROT : return "OP_ROT";
76 : 181871 : case OP_SWAP : return "OP_SWAP";
77 : 11488 : case OP_TUCK : return "OP_TUCK";
78 : :
79 : : // splice ops
80 : 9485 : case OP_CAT : return "OP_CAT";
81 : 63306 : case OP_SUBSTR : return "OP_SUBSTR";
82 : 38117 : case OP_LEFT : return "OP_LEFT";
83 : 14235 : case OP_RIGHT : return "OP_RIGHT";
84 : 11346 : case OP_SIZE : return "OP_SIZE";
85 : :
86 : : // bit logic
87 : 20607 : case OP_INVERT : return "OP_INVERT";
88 : 24094 : case OP_AND : return "OP_AND";
89 : 10539 : case OP_OR : return "OP_OR";
90 : 9194 : case OP_XOR : return "OP_XOR";
91 : 134424 : case OP_EQUAL : return "OP_EQUAL";
92 : 29478 : case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
93 : 11640 : case OP_RESERVED1 : return "OP_RESERVED1";
94 : 10896 : case OP_RESERVED2 : return "OP_RESERVED2";
95 : :
96 : : // numeric
97 : 11599 : case OP_1ADD : return "OP_1ADD";
98 : 7265 : case OP_1SUB : return "OP_1SUB";
99 : 12754 : case OP_2MUL : return "OP_2MUL";
100 : 7427 : case OP_2DIV : return "OP_2DIV";
101 : 16378 : case OP_NEGATE : return "OP_NEGATE";
102 : 9701 : case OP_ABS : return "OP_ABS";
103 : 10645 : case OP_NOT : return "OP_NOT";
104 : 78361 : case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
105 : 10913 : case OP_ADD : return "OP_ADD";
106 : 8694 : case OP_SUB : return "OP_SUB";
107 : 12879 : case OP_MUL : return "OP_MUL";
108 : 49395 : case OP_DIV : return "OP_DIV";
109 : 12877 : case OP_MOD : return "OP_MOD";
110 : 35267 : case OP_LSHIFT : return "OP_LSHIFT";
111 : 27070 : case OP_RSHIFT : return "OP_RSHIFT";
112 : 35242 : case OP_BOOLAND : return "OP_BOOLAND";
113 : 11850 : case OP_BOOLOR : return "OP_BOOLOR";
114 : 30965 : case OP_NUMEQUAL : return "OP_NUMEQUAL";
115 : 9871 : case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
116 : 11972 : case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
117 : 14192 : case OP_LESSTHAN : return "OP_LESSTHAN";
118 : 9662 : case OP_GREATERTHAN : return "OP_GREATERTHAN";
119 : 216207 : case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
120 : 26493 : case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
121 : 15994 : case OP_MIN : return "OP_MIN";
122 : 45812 : case OP_MAX : return "OP_MAX";
123 : 19629 : case OP_WITHIN : return "OP_WITHIN";
124 : :
125 : : // crypto
126 : 10142 : case OP_RIPEMD160 : return "OP_RIPEMD160";
127 : 16045 : case OP_SHA1 : return "OP_SHA1";
128 : 10668 : case OP_SHA256 : return "OP_SHA256";
129 : 144223 : case OP_HASH160 : return "OP_HASH160";
130 : 10670 : case OP_HASH256 : return "OP_HASH256";
131 : 14934 : case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
132 : 94264 : case OP_CHECKSIG : return "OP_CHECKSIG";
133 : 18286 : case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
134 : 751931 : case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
135 : 53678 : case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
136 : :
137 : : // expansion
138 : 18586 : case OP_NOP1 : return "OP_NOP1";
139 : 117076 : case OP_CHECKLOCKTIMEVERIFY : return "OP_CHECKLOCKTIMEVERIFY";
140 : 14015 : case OP_CHECKSEQUENCEVERIFY : return "OP_CHECKSEQUENCEVERIFY";
141 : 12198 : case OP_NOP4 : return "OP_NOP4";
142 : 34727 : case OP_NOP5 : return "OP_NOP5";
143 : 9063 : case OP_NOP6 : return "OP_NOP6";
144 : 9035 : case OP_NOP7 : return "OP_NOP7";
145 : 16297 : case OP_NOP8 : return "OP_NOP8";
146 : 9237 : case OP_NOP9 : return "OP_NOP9";
147 : 15169 : case OP_NOP10 : return "OP_NOP10";
148 : :
149 : : // Opcode added by BIP 342 (Tapscript)
150 : 17491 : case OP_CHECKSIGADD : return "OP_CHECKSIGADD";
151 : :
152 : 73401 : case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
153 : :
154 : : } // no default case, so the compiler can warn about missing cases
155 : 1348290 : return "OP_UNKNOWN";
156 : : }
157 : :
158 : 15381798 : unsigned int CScript::GetSigOpCount(bool fAccurate) const
159 : : {
160 : 15381798 : unsigned int n = 0;
161 [ + + ]: 15381798 : const_iterator pc = begin();
162 : 15381798 : opcodetype lastOpcode = OP_INVALIDOPCODE;
163 [ + + + + ]: 208599362 : while (pc < end())
164 : : {
165 : 89338894 : opcodetype opcode;
166 [ + + ]: 89338894 : if (!GetOp(pc, opcode))
167 : : break;
168 [ + + ]: 88917883 : if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
169 : 531349 : n++;
170 [ + + ]: 88386534 : else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
171 : : {
172 [ + + + + ]: 857560 : if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
173 : 3761 : n += DecodeOP_N(lastOpcode);
174 : : else
175 : 853799 : n += MAX_PUBKEYS_PER_MULTISIG;
176 : : }
177 : : lastOpcode = opcode;
178 : : }
179 : 15381798 : return n;
180 : : }
181 : :
182 : 1485878 : unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
183 : : {
184 [ + + ]: 1485878 : if (!IsPayToScriptHash())
185 : 1483259 : return GetSigOpCount(true);
186 : :
187 : : // This is a pay-to-script-hash scriptPubKey;
188 : : // get the last item that the scriptSig
189 : : // pushes onto the stack:
190 [ + + ]: 2619 : const_iterator pc = scriptSig.begin();
191 : 2619 : std::vector<unsigned char> vData;
192 [ + + + + ]: 70688 : while (pc < scriptSig.end())
193 : : {
194 : 35045 : opcodetype opcode;
195 [ + - + + ]: 35045 : if (!scriptSig.GetOp(pc, opcode, vData))
196 : : return 0;
197 [ + + ]: 34827 : if (opcode > OP_16)
198 : : return 0;
199 : : }
200 : :
201 : : /// ... and return its opcount:
202 : 299 : CScript subscript(vData.begin(), vData.end());
203 [ + - ]: 299 : return subscript.GetSigOpCount(true);
204 : 2918 : }
205 : :
206 : 1455246 : bool CScript::IsPayToAnchor() const
207 : : {
208 [ + + ]: 1455246 : return (this->size() == 4 &&
209 [ + + + + ]: 108956 : (*this)[0] == OP_1 &&
210 [ + + + + ]: 9732 : (*this)[1] == 0x02 &&
211 [ + + + + : 1464972 : (*this)[2] == 0x4e &&
+ + ]
212 [ + + + + ]: 4460 : (*this)[3] == 0x73);
213 : : }
214 : :
215 : 13813 : bool CScript::IsPayToAnchor(int version, const std::vector<unsigned char>& program)
216 : : {
217 : 17928 : return version == 1 &&
218 [ + + ]: 4115 : program.size() == 2 &&
219 [ + + + + ]: 16764 : program[0] == 0x4e &&
220 [ + + ]: 1837 : program[1] == 0x73;
221 : : }
222 : :
223 : 17451479 : bool CScript::IsPayToScriptHash() const
224 : : {
225 : : // Extra-fast test for pay-to-script-hash CScripts:
226 [ + + ]: 17451479 : return (this->size() == 23 &&
227 [ + + + + ]: 1262332 : (*this)[0] == OP_HASH160 &&
228 [ + + + + : 18682557 : (*this)[1] == 0x14 &&
+ + ]
229 [ + + + + ]: 1223680 : (*this)[22] == OP_EQUAL);
230 : : }
231 : :
232 : 4446 : bool CScript::IsPayToWitnessScriptHash() const
233 : : {
234 : : // Extra-fast test for pay-to-witness-script-hash CScripts:
235 [ + + ]: 4446 : return (this->size() == 34 &&
236 [ + + + + : 4483 : (*this)[0] == OP_0 &&
+ + ]
237 [ + + + + ]: 14 : (*this)[1] == 0x20);
238 : : }
239 : :
240 : 29860 : bool CScript::IsPayToTaproot() const
241 : : {
242 [ + + ]: 29860 : return (this->size() == 34 &&
243 [ + + + - : 35826 : (*this)[0] == OP_1 &&
+ + ]
244 [ + - + + ]: 9738 : (*this)[1] == 0x20);
245 : : }
246 : :
247 : : // A witness program is any valid CScript that consists of a 1-byte push opcode
248 : : // followed by a data push between 2 and 40 bytes.
249 : 57370178 : bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
250 : : {
251 [ + + + + : 58224373 : if (this->size() < 4 || this->size() > 42) {
+ + + + ]
252 : : return false;
253 : : }
254 [ + + + + : 108603600 : if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
+ + + + ]
255 : : return false;
256 : : }
257 [ + + + + ]: 106737210 : if ((size_t)((*this)[1] + 2) == this->size()) {
258 : 53300159 : version = DecodeOP_N((opcodetype)(*this)[0]);
259 [ + + ]: 159900477 : program = std::vector<unsigned char>(this->begin() + 2, this->end());
260 : 53300159 : return true;
261 : : }
262 : : return false;
263 : : }
264 : :
265 : 4505617 : bool CScript::IsPushOnly(const_iterator pc) const
266 : : {
267 [ + + + + ]: 14754984 : while (pc < end())
268 : : {
269 : 2922471 : opcodetype opcode;
270 [ + + ]: 2922471 : if (!GetOp(pc, opcode))
271 : : return false;
272 : : // Note that IsPushOnly() *does* consider OP_RESERVED to be a
273 : : // push-type opcode, however execution of OP_RESERVED fails, so
274 : : // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
275 : : // the P2SH special validation code being executed.
276 [ + + ]: 2900325 : if (opcode > OP_16)
277 : : return false;
278 : : }
279 : : return true;
280 : : }
281 : :
282 : 4461768 : bool CScript::IsPushOnly() const
283 : : {
284 [ + + ]: 8923536 : return this->IsPushOnly(begin());
285 : : }
286 : :
287 : 1220891 : std::string CScriptWitness::ToString() const
288 : : {
289 : 1220891 : std::string ret = "CScriptWitness(";
290 [ - + + + ]: 7536945 : for (unsigned int i = 0; i < stack.size(); i++) {
291 [ + + ]: 6316054 : if (i) {
292 [ + - ]: 6289633 : ret += ", ";
293 : : }
294 [ - + + - ]: 12632108 : ret += HexStr(stack[i]);
295 : : }
296 [ + - ]: 1220891 : return ret + ")";
297 : 1220891 : }
298 : :
299 : 599031 : bool CScript::HasValidOps() const
300 : : {
301 [ + + ]: 599031 : CScript::const_iterator it = begin();
302 [ + + + + ]: 16121524 : while (it < end()) {
303 : 7465488 : opcodetype opcode;
304 : 7465488 : std::vector<unsigned char> item;
305 [ + - + + : 14927245 : if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
+ + + + ]
306 : 3757 : return false;
307 : : }
308 : 7465488 : }
309 : : return true;
310 : : }
311 : :
312 : 326919261 : bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
313 : : {
314 : 326919261 : opcodeRet = OP_INVALIDOPCODE;
315 [ + + ]: 326919261 : if (pvchRet)
316 [ + + ]: 59995391 : pvchRet->clear();
317 [ + + + + ]: 653838522 : if (pc >= end)
318 : : return false;
319 : :
320 : : // Read instruction
321 [ + - ]: 326526513 : if (end - pc < 1)
322 : : return false;
323 [ + + ]: 326526513 : unsigned int opcode = *pc++;
324 : :
325 : : // Immediate operand
326 [ + + ]: 326526513 : if (opcode <= OP_PUSHDATA4)
327 : : {
328 : 103193383 : unsigned int nSize = 0;
329 [ + + ]: 103193383 : if (opcode < OP_PUSHDATA1)
330 : : {
331 : : nSize = opcode;
332 : : }
333 [ + + ]: 29962635 : else if (opcode == OP_PUSHDATA1)
334 : : {
335 [ + + ]: 29588572 : if (end - pc < 1)
336 : : return false;
337 : 29581051 : nSize = *pc++;
338 : : }
339 [ + + ]: 374063 : else if (opcode == OP_PUSHDATA2)
340 : : {
341 [ + + ]: 211907 : if (end - pc < 2)
342 : : return false;
343 : 197851 : nSize = ReadLE16(&pc[0]);
344 : 197851 : pc += 2;
345 : : }
346 [ + - ]: 162156 : else if (opcode == OP_PUSHDATA4)
347 : : {
348 [ + + ]: 162156 : if (end - pc < 4)
349 : : return false;
350 : 153247 : nSize = ReadLE32(&pc[0]);
351 : 153247 : pc += 4;
352 : : }
353 [ + - + + ]: 103162897 : if (end - pc < 0 || (unsigned int)(end - pc) < nSize)
354 : 1231281 : return false;
355 [ + + ]: 101931616 : if (pvchRet)
356 : 25158545 : pvchRet->assign(pc, pc + nSize);
357 : 101931616 : pc += nSize;
358 : : }
359 : :
360 : 325264746 : opcodeRet = static_cast<opcodetype>(opcode);
361 : 325264746 : return true;
362 : : }
363 : :
364 : 698407 : bool IsOpSuccess(const opcodetype& opcode)
365 : : {
366 [ + + + + ]: 698392 : return opcode == 80 || opcode == 98 || (opcode >= 126 && opcode <= 129) ||
367 [ + + + + ]: 698384 : (opcode >= 131 && opcode <= 134) || (opcode >= 137 && opcode <= 138) ||
368 [ + + + + : 1396766 : (opcode >= 141 && opcode <= 142) || (opcode >= 149 && opcode <= 153) ||
+ + ]
369 [ + + ]: 698345 : (opcode >= 187 && opcode <= 254);
370 : : }
371 : :
372 : 9788751 : bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode) {
373 : : // Excludes OP_1NEGATE, OP_1-16 since they are by definition minimal
374 [ - + ]: 9788751 : assert(0 <= opcode && opcode <= OP_PUSHDATA4);
375 [ - + + + ]: 9788751 : if (data.size() == 0) {
376 : : // Should have used OP_0.
377 : 7927040 : return opcode == OP_0;
378 [ + + + + : 1861711 : } else if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) {
+ + ]
379 : : // Should have used OP_1 .. OP_16.
380 : : return false;
381 [ + + + + ]: 1855094 : } else if (data.size() == 1 && data[0] == 0x81) {
382 : : // Should have used OP_1NEGATE.
383 : : return false;
384 [ + + ]: 1853862 : } else if (data.size() <= 75) {
385 : : // Must have used a direct push (opcode indicating number of bytes pushed + those bytes).
386 : 1829520 : return opcode == data.size();
387 [ + + ]: 24342 : } else if (data.size() <= 255) {
388 : : // Must have used OP_PUSHDATA.
389 : 9994 : return opcode == OP_PUSHDATA1;
390 [ + + ]: 14348 : } else if (data.size() <= 65535) {
391 : : // Must have used OP_PUSHDATA2.
392 : 14323 : return opcode == OP_PUSHDATA2;
393 : : }
394 : : return true;
395 : : }
|