]>
Commit | Line | Data |
---|---|---|
cbd22a50 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2d79bba3 PK |
2 | // Copyright (c) 2009-2014 The Bitcoin developers |
3 | // Distributed under the MIT software license, see the accompanying | |
cbd22a50 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | ||
6 | #include "script.h" | |
7 | ||
85c579e3 CF |
8 | #include "tinyformat.h" |
9 | #include "utilstrencodings.h" | |
cbd22a50 | 10 | |
db8eb54b CF |
11 | namespace { |
12 | inline std::string ValueString(const std::vector<unsigned char>& vch) | |
13 | { | |
14 | if (vch.size() <= 4) | |
698c6abb | 15 | return strprintf("%d", CScriptNum(vch, false).getint()); |
db8eb54b CF |
16 | else |
17 | return HexStr(vch); | |
18 | } | |
19 | } // anon namespace | |
20 | ||
cbd22a50 | 21 | using namespace std; |
22 | ||
23 | const char* GetOpName(opcodetype opcode) | |
24 | { | |
25 | switch (opcode) | |
26 | { | |
27 | // push value | |
28 | case OP_0 : return "0"; | |
29 | case OP_PUSHDATA1 : return "OP_PUSHDATA1"; | |
30 | case OP_PUSHDATA2 : return "OP_PUSHDATA2"; | |
31 | case OP_PUSHDATA4 : return "OP_PUSHDATA4"; | |
32 | case OP_1NEGATE : return "-1"; | |
33 | case OP_RESERVED : return "OP_RESERVED"; | |
34 | case OP_1 : return "1"; | |
35 | case OP_2 : return "2"; | |
36 | case OP_3 : return "3"; | |
37 | case OP_4 : return "4"; | |
38 | case OP_5 : return "5"; | |
39 | case OP_6 : return "6"; | |
40 | case OP_7 : return "7"; | |
41 | case OP_8 : return "8"; | |
42 | case OP_9 : return "9"; | |
43 | case OP_10 : return "10"; | |
44 | case OP_11 : return "11"; | |
45 | case OP_12 : return "12"; | |
46 | case OP_13 : return "13"; | |
47 | case OP_14 : return "14"; | |
48 | case OP_15 : return "15"; | |
49 | case OP_16 : return "16"; | |
50 | ||
51 | // control | |
52 | case OP_NOP : return "OP_NOP"; | |
53 | case OP_VER : return "OP_VER"; | |
54 | case OP_IF : return "OP_IF"; | |
55 | case OP_NOTIF : return "OP_NOTIF"; | |
56 | case OP_VERIF : return "OP_VERIF"; | |
57 | case OP_VERNOTIF : return "OP_VERNOTIF"; | |
58 | case OP_ELSE : return "OP_ELSE"; | |
59 | case OP_ENDIF : return "OP_ENDIF"; | |
60 | case OP_VERIFY : return "OP_VERIFY"; | |
61 | case OP_RETURN : return "OP_RETURN"; | |
62 | ||
63 | // stack ops | |
64 | case OP_TOALTSTACK : return "OP_TOALTSTACK"; | |
65 | case OP_FROMALTSTACK : return "OP_FROMALTSTACK"; | |
66 | case OP_2DROP : return "OP_2DROP"; | |
67 | case OP_2DUP : return "OP_2DUP"; | |
68 | case OP_3DUP : return "OP_3DUP"; | |
69 | case OP_2OVER : return "OP_2OVER"; | |
70 | case OP_2ROT : return "OP_2ROT"; | |
71 | case OP_2SWAP : return "OP_2SWAP"; | |
72 | case OP_IFDUP : return "OP_IFDUP"; | |
73 | case OP_DEPTH : return "OP_DEPTH"; | |
74 | case OP_DROP : return "OP_DROP"; | |
75 | case OP_DUP : return "OP_DUP"; | |
76 | case OP_NIP : return "OP_NIP"; | |
77 | case OP_OVER : return "OP_OVER"; | |
78 | case OP_PICK : return "OP_PICK"; | |
79 | case OP_ROLL : return "OP_ROLL"; | |
80 | case OP_ROT : return "OP_ROT"; | |
81 | case OP_SWAP : return "OP_SWAP"; | |
82 | case OP_TUCK : return "OP_TUCK"; | |
83 | ||
84 | // splice ops | |
85 | case OP_CAT : return "OP_CAT"; | |
86 | case OP_SUBSTR : return "OP_SUBSTR"; | |
87 | case OP_LEFT : return "OP_LEFT"; | |
88 | case OP_RIGHT : return "OP_RIGHT"; | |
89 | case OP_SIZE : return "OP_SIZE"; | |
90 | ||
91 | // bit logic | |
92 | case OP_INVERT : return "OP_INVERT"; | |
93 | case OP_AND : return "OP_AND"; | |
94 | case OP_OR : return "OP_OR"; | |
95 | case OP_XOR : return "OP_XOR"; | |
96 | case OP_EQUAL : return "OP_EQUAL"; | |
97 | case OP_EQUALVERIFY : return "OP_EQUALVERIFY"; | |
98 | case OP_RESERVED1 : return "OP_RESERVED1"; | |
99 | case OP_RESERVED2 : return "OP_RESERVED2"; | |
100 | ||
101 | // numeric | |
102 | case OP_1ADD : return "OP_1ADD"; | |
103 | case OP_1SUB : return "OP_1SUB"; | |
104 | case OP_2MUL : return "OP_2MUL"; | |
105 | case OP_2DIV : return "OP_2DIV"; | |
106 | case OP_NEGATE : return "OP_NEGATE"; | |
107 | case OP_ABS : return "OP_ABS"; | |
108 | case OP_NOT : return "OP_NOT"; | |
109 | case OP_0NOTEQUAL : return "OP_0NOTEQUAL"; | |
110 | case OP_ADD : return "OP_ADD"; | |
111 | case OP_SUB : return "OP_SUB"; | |
112 | case OP_MUL : return "OP_MUL"; | |
113 | case OP_DIV : return "OP_DIV"; | |
114 | case OP_MOD : return "OP_MOD"; | |
115 | case OP_LSHIFT : return "OP_LSHIFT"; | |
116 | case OP_RSHIFT : return "OP_RSHIFT"; | |
117 | case OP_BOOLAND : return "OP_BOOLAND"; | |
118 | case OP_BOOLOR : return "OP_BOOLOR"; | |
119 | case OP_NUMEQUAL : return "OP_NUMEQUAL"; | |
120 | case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY"; | |
121 | case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL"; | |
122 | case OP_LESSTHAN : return "OP_LESSTHAN"; | |
123 | case OP_GREATERTHAN : return "OP_GREATERTHAN"; | |
124 | case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL"; | |
125 | case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL"; | |
126 | case OP_MIN : return "OP_MIN"; | |
127 | case OP_MAX : return "OP_MAX"; | |
128 | case OP_WITHIN : return "OP_WITHIN"; | |
129 | ||
130 | // crypto | |
131 | case OP_RIPEMD160 : return "OP_RIPEMD160"; | |
132 | case OP_SHA1 : return "OP_SHA1"; | |
133 | case OP_SHA256 : return "OP_SHA256"; | |
134 | case OP_HASH160 : return "OP_HASH160"; | |
135 | case OP_HASH256 : return "OP_HASH256"; | |
136 | case OP_CODESEPARATOR : return "OP_CODESEPARATOR"; | |
137 | case OP_CHECKSIG : return "OP_CHECKSIG"; | |
138 | case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY"; | |
139 | case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG"; | |
140 | case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY"; | |
141 | ||
142 | // expanson | |
143 | case OP_NOP1 : return "OP_NOP1"; | |
144 | case OP_NOP2 : return "OP_NOP2"; | |
145 | case OP_NOP3 : return "OP_NOP3"; | |
146 | case OP_NOP4 : return "OP_NOP4"; | |
147 | case OP_NOP5 : return "OP_NOP5"; | |
148 | case OP_NOP6 : return "OP_NOP6"; | |
149 | case OP_NOP7 : return "OP_NOP7"; | |
150 | case OP_NOP8 : return "OP_NOP8"; | |
151 | case OP_NOP9 : return "OP_NOP9"; | |
152 | case OP_NOP10 : return "OP_NOP10"; | |
153 | ||
154 | case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE"; | |
155 | ||
156 | // Note: | |
157 | // The template matching params OP_SMALLDATA/etc are defined in opcodetype enum | |
158 | // as kind of implementation hack, they are *NOT* real opcodes. If found in real | |
159 | // Script, just let the default: case deal with them. | |
160 | ||
161 | default: | |
162 | return "OP_UNKNOWN"; | |
163 | } | |
164 | } | |
165 | ||
166 | unsigned int CScript::GetSigOpCount(bool fAccurate) const | |
167 | { | |
168 | unsigned int n = 0; | |
169 | const_iterator pc = begin(); | |
170 | opcodetype lastOpcode = OP_INVALIDOPCODE; | |
171 | while (pc < end()) | |
172 | { | |
173 | opcodetype opcode; | |
174 | if (!GetOp(pc, opcode)) | |
175 | break; | |
176 | if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY) | |
177 | n++; | |
178 | else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY) | |
179 | { | |
180 | if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16) | |
181 | n += DecodeOP_N(lastOpcode); | |
182 | else | |
183 | n += 20; | |
184 | } | |
185 | lastOpcode = opcode; | |
186 | } | |
187 | return n; | |
188 | } | |
189 | ||
190 | unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const | |
191 | { | |
192 | if (!IsPayToScriptHash()) | |
193 | return GetSigOpCount(true); | |
194 | ||
195 | // This is a pay-to-script-hash scriptPubKey; | |
196 | // get the last item that the scriptSig | |
197 | // pushes onto the stack: | |
198 | const_iterator pc = scriptSig.begin(); | |
199 | vector<unsigned char> data; | |
200 | while (pc < scriptSig.end()) | |
201 | { | |
202 | opcodetype opcode; | |
203 | if (!scriptSig.GetOp(pc, opcode, data)) | |
204 | return 0; | |
205 | if (opcode > OP_16) | |
206 | return 0; | |
207 | } | |
208 | ||
209 | /// ... and return its opcount: | |
210 | CScript subscript(data.begin(), data.end()); | |
211 | return subscript.GetSigOpCount(true); | |
212 | } | |
213 | ||
214 | bool CScript::IsPayToScriptHash() const | |
215 | { | |
216 | // Extra-fast test for pay-to-script-hash CScripts: | |
217 | return (this->size() == 23 && | |
218 | this->at(0) == OP_HASH160 && | |
219 | this->at(1) == 0x14 && | |
220 | this->at(22) == OP_EQUAL); | |
221 | } | |
222 | ||
223 | bool CScript::IsPushOnly() const | |
224 | { | |
225 | const_iterator pc = begin(); | |
226 | while (pc < end()) | |
227 | { | |
228 | opcodetype opcode; | |
229 | if (!GetOp(pc, opcode)) | |
230 | return false; | |
231 | // Note that IsPushOnly() *does* consider OP_RESERVED to be a | |
232 | // push-type opcode, however execution of OP_RESERVED fails, so | |
d752ba86 | 233 | // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to |
cbd22a50 | 234 | // the P2SH special validation code being executed. |
235 | if (opcode > OP_16) | |
236 | return false; | |
237 | } | |
238 | return true; | |
239 | } | |
240 | ||
db8eb54b CF |
241 | std::string CScript::ToString() const |
242 | { | |
243 | std::string str; | |
244 | opcodetype opcode; | |
245 | std::vector<unsigned char> vch; | |
246 | const_iterator pc = begin(); | |
247 | while (pc < end()) | |
248 | { | |
249 | if (!str.empty()) | |
250 | str += " "; | |
251 | if (!GetOp(pc, opcode, vch)) | |
252 | { | |
253 | str += "[error]"; | |
254 | return str; | |
255 | } | |
256 | if (0 <= opcode && opcode <= OP_PUSHDATA4) | |
257 | str += ValueString(vch); | |
258 | else | |
259 | str += GetOpName(opcode); | |
260 | } | |
261 | return str; | |
262 | } |