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 <script/names.h>
9 #include <util/strencodings.h>
10 
11 #include <string>
12 
GetOpName(opcodetype opcode)13 std::string 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 "OP_NAME_NEW";
25     case OP_2                      : return "OP_NAME_FIRSTUPDATE";
26     case OP_3                      : return "OP_NAME_UPDATE";
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     // expansion
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     // Opcode added by BIP 342 (Tapscript)
145     case OP_CHECKSIGADD            : return "OP_CHECKSIGADD";
146 
147     case OP_INVALIDOPCODE          : return "OP_INVALIDOPCODE";
148 
149     default:
150         return "OP_UNKNOWN";
151     }
152 }
153 
GetSigOpCount(bool fAccurate) const154 unsigned int CScript::GetSigOpCount(bool fAccurate) const
155 {
156     unsigned int n = 0;
157     const_iterator pc = begin();
158     opcodetype lastOpcode = OP_INVALIDOPCODE;
159     while (pc < end())
160     {
161         opcodetype opcode;
162         if (!GetOp(pc, opcode))
163             break;
164         if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
165             n++;
166         else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
167         {
168             if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
169                 n += DecodeOP_N(lastOpcode);
170             else
171                 n += MAX_PUBKEYS_PER_MULTISIG;
172         }
173         lastOpcode = opcode;
174     }
175     return n;
176 }
177 
GetSigOpCount(const CScript & scriptSig) const178 unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
179 {
180     if (!IsPayToScriptHash(true))
181         return GetSigOpCount(true);
182 
183     // This is a pay-to-script-hash scriptPubKey;
184     // get the last item that the scriptSig
185     // pushes onto the stack:
186     const_iterator pc = scriptSig.begin();
187     std::vector<unsigned char> vData;
188     while (pc < scriptSig.end())
189     {
190         opcodetype opcode;
191         if (!scriptSig.GetOp(pc, opcode, vData))
192             return 0;
193         if (opcode > OP_16)
194             return 0;
195     }
196 
197     /// ... and return its opcount:
198     CScript subscript(vData.begin(), vData.end());
199     return subscript.GetSigOpCount(true);
200 }
201 
IsPayToScriptHash(bool allowNames) const202 bool CScript::IsPayToScriptHash(bool allowNames) const
203 {
204     // Extra-fast test for pay-to-script-hash CScripts:
205     if (!allowNames)
206         return (this->size() == 23 &&
207                 (*this)[0] == OP_HASH160 &&
208                 (*this)[1] == 0x14 &&
209                 (*this)[22] == OP_EQUAL);
210 
211     // Strip off a name prefix if present.
212     const CNameScript nameOp(*this);
213     return nameOp.getAddress().IsPayToScriptHash(false);
214 }
215 
IsPayToWitnessScriptHash(bool allowNames) const216 bool CScript::IsPayToWitnessScriptHash(bool allowNames) const
217 {
218     // Extra-fast test for pay-to-witness-script-hash CScripts:
219     if (!allowNames)
220         return (this->size() == 34 &&
221                 (*this)[0] == OP_0 &&
222                 (*this)[1] == 0x20);
223 
224     // Strip off a name prefix if present.
225     const CNameScript nameOp(*this);
226     return nameOp.getAddress().IsPayToWitnessScriptHash(false);
227 }
228 
229 // A witness program is any valid CScript that consists of a 1-byte push opcode
230 // followed by a data push between 2 and 40 bytes.
IsWitnessProgram(const bool allowNames,int & version,std::vector<unsigned char> & program) const231 bool CScript::IsWitnessProgram(const bool allowNames, int& version, std::vector<unsigned char>& program) const
232 {
233     // Strip off a name prefix if present.
234     if (allowNames)
235       {
236         const CNameScript nameOp(*this);
237         return nameOp.getAddress().IsWitnessProgram(false, version, program);
238       }
239 
240     // Handle the case without name prefix.
241     if (this->size() < 4 || this->size() > 42) {
242         return false;
243     }
244     if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
245         return false;
246     }
247     if ((size_t)((*this)[1] + 2) == this->size()) {
248         version = DecodeOP_N((opcodetype)(*this)[0]);
249         program = std::vector<unsigned char>(this->begin() + 2, this->end());
250         return true;
251     }
252     return false;
253 }
254 
IsPushOnly(const_iterator pc) const255 bool CScript::IsPushOnly(const_iterator pc) const
256 {
257     while (pc < end())
258     {
259         opcodetype opcode;
260         if (!GetOp(pc, opcode))
261             return false;
262         // Note that IsPushOnly() *does* consider OP_RESERVED to be a
263         // push-type opcode, however execution of OP_RESERVED fails, so
264         // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
265         // the P2SH special validation code being executed.
266         if (opcode > OP_16)
267             return false;
268     }
269     return true;
270 }
271 
IsPushOnly() const272 bool CScript::IsPushOnly() const
273 {
274     return this->IsPushOnly(begin());
275 }
276 
ToString() const277 std::string CScriptWitness::ToString() const
278 {
279     std::string ret = "CScriptWitness(";
280     for (unsigned int i = 0; i < stack.size(); i++) {
281         if (i) {
282             ret += ", ";
283         }
284         ret += HexStr(stack[i]);
285     }
286     return ret + ")";
287 }
288 
HasValidOps() const289 bool CScript::HasValidOps() const
290 {
291     CScript::const_iterator it = begin();
292     while (it < end()) {
293         opcodetype opcode;
294         std::vector<unsigned char> item;
295         if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
296             return false;
297         }
298     }
299     return true;
300 }
301 
GetScriptOp(CScriptBase::const_iterator & pc,CScriptBase::const_iterator end,opcodetype & opcodeRet,std::vector<unsigned char> * pvchRet)302 bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
303 {
304     opcodeRet = OP_INVALIDOPCODE;
305     if (pvchRet)
306         pvchRet->clear();
307     if (pc >= end)
308         return false;
309 
310     // Read instruction
311     if (end - pc < 1)
312         return false;
313     unsigned int opcode = *pc++;
314 
315     // Immediate operand
316     if (opcode <= OP_PUSHDATA4)
317     {
318         unsigned int nSize = 0;
319         if (opcode < OP_PUSHDATA1)
320         {
321             nSize = opcode;
322         }
323         else if (opcode == OP_PUSHDATA1)
324         {
325             if (end - pc < 1)
326                 return false;
327             nSize = *pc++;
328         }
329         else if (opcode == OP_PUSHDATA2)
330         {
331             if (end - pc < 2)
332                 return false;
333             nSize = ReadLE16(&pc[0]);
334             pc += 2;
335         }
336         else if (opcode == OP_PUSHDATA4)
337         {
338             if (end - pc < 4)
339                 return false;
340             nSize = ReadLE32(&pc[0]);
341             pc += 4;
342         }
343         if (end - pc < 0 || (unsigned int)(end - pc) < nSize)
344             return false;
345         if (pvchRet)
346             pvchRet->assign(pc, pc + nSize);
347         pc += nSize;
348     }
349 
350     opcodeRet = static_cast<opcodetype>(opcode);
351     return true;
352 }
353 
IsOpSuccess(const opcodetype & opcode)354 bool IsOpSuccess(const opcodetype& opcode)
355 {
356     return opcode == 80 || opcode == 98 || (opcode >= 126 && opcode <= 129) ||
357            (opcode >= 131 && opcode <= 134) || (opcode >= 137 && opcode <= 138) ||
358            (opcode >= 141 && opcode <= 142) || (opcode >= 149 && opcode <= 153) ||
359            (opcode >= 187 && opcode <= 254);
360 }
361