Coverage Report

Created: 2025-02-21 14:36

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