1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 using namespace std;
12
GetOpName(opcodetype opcode)13 const char* GetOpName(opcodetype opcode)
14 {
15 switch (opcode)
16 {
17 // push value
18 case OP_0 : return "0";
19 case OP_PUSHDATA1 : return "OP_PUSHDATA1";
20 case OP_PUSHDATA2 : return "OP_PUSHDATA2";
21 case OP_PUSHDATA4 : return "OP_PUSHDATA4";
22 case OP_1NEGATE : return "-1";
23 case OP_RESERVED : return "OP_RESERVED";
24 case OP_1 : return "1";
25 case OP_2 : return "2";
26 case OP_3 : return "3";
27 case OP_4 : return "4";
28 case OP_5 : return "5";
29 case OP_6 : return "6";
30 case OP_7 : return "7";
31 case OP_8 : return "8";
32 case OP_9 : return "9";
33 case OP_10 : return "10";
34 case OP_11 : return "11";
35 case OP_12 : return "12";
36 case OP_13 : return "13";
37 case OP_14 : return "14";
38 case OP_15 : return "15";
39 case OP_16 : return "16";
40
41 // control
42 case OP_NOP : return "OP_NOP";
43 case OP_VER : return "OP_VER";
44 case OP_IF : return "OP_IF";
45 case OP_NOTIF : return "OP_NOTIF";
46 case OP_VERIF : return "OP_VERIF";
47 case OP_VERNOTIF : return "OP_VERNOTIF";
48 case OP_ELSE : return "OP_ELSE";
49 case OP_ENDIF : return "OP_ENDIF";
50 case OP_VERIFY : return "OP_VERIFY";
51 case OP_RETURN : return "OP_RETURN";
52
53 // stack ops
54 case OP_TOALTSTACK : return "OP_TOALTSTACK";
55 case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
56 case OP_2DROP : return "OP_2DROP";
57 case OP_2DUP : return "OP_2DUP";
58 case OP_3DUP : return "OP_3DUP";
59 case OP_2OVER : return "OP_2OVER";
60 case OP_2ROT : return "OP_2ROT";
61 case OP_2SWAP : return "OP_2SWAP";
62 case OP_IFDUP : return "OP_IFDUP";
63 case OP_DEPTH : return "OP_DEPTH";
64 case OP_DROP : return "OP_DROP";
65 case OP_DUP : return "OP_DUP";
66 case OP_NIP : return "OP_NIP";
67 case OP_OVER : return "OP_OVER";
68 case OP_PICK : return "OP_PICK";
69 case OP_ROLL : return "OP_ROLL";
70 case OP_ROT : return "OP_ROT";
71 case OP_SWAP : return "OP_SWAP";
72 case OP_TUCK : return "OP_TUCK";
73
74 // splice ops
75 case OP_CAT : return "OP_CAT";
76 case OP_SUBSTR : return "OP_SUBSTR";
77 case OP_LEFT : return "OP_LEFT";
78 case OP_RIGHT : return "OP_RIGHT";
79 case OP_SIZE : return "OP_SIZE";
80
81 // bit logic
82 case OP_INVERT : return "OP_INVERT";
83 case OP_AND : return "OP_AND";
84 case OP_OR : return "OP_OR";
85 case OP_XOR : return "OP_XOR";
86 case OP_EQUAL : return "OP_EQUAL";
87 case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
88 case OP_RESERVED1 : return "OP_RESERVED1";
89 case OP_RESERVED2 : return "OP_RESERVED2";
90
91 // numeric
92 case OP_1ADD : return "OP_1ADD";
93 case OP_1SUB : return "OP_1SUB";
94 case OP_2MUL : return "OP_2MUL";
95 case OP_2DIV : return "OP_2DIV";
96 case OP_NEGATE : return "OP_NEGATE";
97 case OP_ABS : return "OP_ABS";
98 case OP_NOT : return "OP_NOT";
99 case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
100 case OP_ADD : return "OP_ADD";
101 case OP_SUB : return "OP_SUB";
102 case OP_MUL : return "OP_MUL";
103 case OP_DIV : return "OP_DIV";
104 case OP_MOD : return "OP_MOD";
105 case OP_LSHIFT : return "OP_LSHIFT";
106 case OP_RSHIFT : return "OP_RSHIFT";
107 case OP_BOOLAND : return "OP_BOOLAND";
108 case OP_BOOLOR : return "OP_BOOLOR";
109 case OP_NUMEQUAL : return "OP_NUMEQUAL";
110 case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
111 case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
112 case OP_LESSTHAN : return "OP_LESSTHAN";
113 case OP_GREATERTHAN : return "OP_GREATERTHAN";
114 case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
115 case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
116 case OP_MIN : return "OP_MIN";
117 case OP_MAX : return "OP_MAX";
118 case OP_WITHIN : return "OP_WITHIN";
119
120 // crypto
121 case OP_RIPEMD160 : return "OP_RIPEMD160";
122 case OP_SHA1 : return "OP_SHA1";
123 case OP_SHA256 : return "OP_SHA256";
124 case OP_HASH160 : return "OP_HASH160";
125 case OP_HASH256 : return "OP_HASH256";
126 case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
127 case OP_CHECKSIG : return "OP_CHECKSIG";
128 case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
129 case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
130 case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
131
132 // expanson
133 case OP_NOP1 : return "OP_NOP1";
134 case OP_CHECKLOCKTIMEVERIFY : return "OP_CHECKLOCKTIMEVERIFY";
135 case OP_CHECKSEQUENCEVERIFY : return "OP_CHECKSEQUENCEVERIFY";
136 case OP_NOP4 : return "OP_NOP4";
137 case OP_NOP5 : return "OP_NOP5";
138 case OP_NOP6 : return "OP_NOP6";
139 case OP_NOP7 : return "OP_NOP7";
140 case OP_NOP8 : return "OP_NOP8";
141 case OP_NOP9 : return "OP_NOP9";
142 case OP_NOP10 : return "OP_NOP10";
143
144 case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
145
146 // Note:
147 // The template matching params OP_SMALLINTEGER/etc are defined in opcodetype enum
148 // as kind of implementation hack, they are *NOT* real opcodes. If found in real
149 // Script, just let the default: case deal with them.
150
151 default:
152 return "OP_UNKNOWN";
153 }
154 }
155
GetSigOpCount(bool fAccurate) const156 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
173 n += MAX_PUBKEYS_PER_MULTISIG;
174 }
175 lastOpcode = opcode;
176 }
177 return n;
178 }
179
GetSigOpCount(const CScript & scriptSig) const180 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 vector<unsigned char> data;
190 while (pc < scriptSig.end())
191 {
192 opcodetype opcode;
193 if (!scriptSig.GetOp(pc, opcode, data))
194 return 0;
195 if (opcode > OP_16)
196 return 0;
197 }
198
199 /// ... and return its opcount:
200 CScript subscript(data.begin(), data.end());
201 return subscript.GetSigOpCount(true);
202 }
203
IsPayToScriptHash() const204 bool CScript::IsPayToScriptHash() const
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
IsPayToWitnessScriptHash() const213 bool CScript::IsPayToWitnessScriptHash() const
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.
IsWitnessProgram(int & version,std::vector<unsigned char> & program) const223 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
IsPushOnly(const_iterator pc) const239 bool CScript::IsPushOnly(const_iterator pc) const
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
IsPushOnly() const256 bool CScript::IsPushOnly() const
257 {
258 return this->IsPushOnly(begin());
259 }
260
ToString() const261 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