1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
8 #include "tinyformat.h"
9 #include "utilstrencodings.h"
11 const char* GetOpName(opcodetype opcode
)
16 case OP_0
: return "0";
17 case OP_PUSHDATA1
: return "OP_PUSHDATA1";
18 case OP_PUSHDATA2
: return "OP_PUSHDATA2";
19 case OP_PUSHDATA4
: return "OP_PUSHDATA4";
20 case OP_1NEGATE
: return "-1";
21 case OP_RESERVED
: return "OP_RESERVED";
22 case OP_1
: return "1";
23 case OP_2
: return "2";
24 case OP_3
: return "3";
25 case OP_4
: return "4";
26 case OP_5
: return "5";
27 case OP_6
: return "6";
28 case OP_7
: return "7";
29 case OP_8
: return "8";
30 case OP_9
: return "9";
31 case OP_10
: return "10";
32 case OP_11
: return "11";
33 case OP_12
: return "12";
34 case OP_13
: return "13";
35 case OP_14
: return "14";
36 case OP_15
: return "15";
37 case OP_16
: return "16";
40 case OP_NOP
: return "OP_NOP";
41 case OP_VER
: return "OP_VER";
42 case OP_IF
: return "OP_IF";
43 case OP_NOTIF
: return "OP_NOTIF";
44 case OP_VERIF
: return "OP_VERIF";
45 case OP_VERNOTIF
: return "OP_VERNOTIF";
46 case OP_ELSE
: return "OP_ELSE";
47 case OP_ENDIF
: return "OP_ENDIF";
48 case OP_VERIFY
: return "OP_VERIFY";
49 case OP_RETURN
: return "OP_RETURN";
52 case OP_TOALTSTACK
: return "OP_TOALTSTACK";
53 case OP_FROMALTSTACK
: return "OP_FROMALTSTACK";
54 case OP_2DROP
: return "OP_2DROP";
55 case OP_2DUP
: return "OP_2DUP";
56 case OP_3DUP
: return "OP_3DUP";
57 case OP_2OVER
: return "OP_2OVER";
58 case OP_2ROT
: return "OP_2ROT";
59 case OP_2SWAP
: return "OP_2SWAP";
60 case OP_IFDUP
: return "OP_IFDUP";
61 case OP_DEPTH
: return "OP_DEPTH";
62 case OP_DROP
: return "OP_DROP";
63 case OP_DUP
: return "OP_DUP";
64 case OP_NIP
: return "OP_NIP";
65 case OP_OVER
: return "OP_OVER";
66 case OP_PICK
: return "OP_PICK";
67 case OP_ROLL
: return "OP_ROLL";
68 case OP_ROT
: return "OP_ROT";
69 case OP_SWAP
: return "OP_SWAP";
70 case OP_TUCK
: return "OP_TUCK";
73 case OP_CAT
: return "OP_CAT";
74 case OP_SUBSTR
: return "OP_SUBSTR";
75 case OP_LEFT
: return "OP_LEFT";
76 case OP_RIGHT
: return "OP_RIGHT";
77 case OP_SIZE
: return "OP_SIZE";
80 case OP_INVERT
: return "OP_INVERT";
81 case OP_AND
: return "OP_AND";
82 case OP_OR
: return "OP_OR";
83 case OP_XOR
: return "OP_XOR";
84 case OP_EQUAL
: return "OP_EQUAL";
85 case OP_EQUALVERIFY
: return "OP_EQUALVERIFY";
86 case OP_RESERVED1
: return "OP_RESERVED1";
87 case OP_RESERVED2
: return "OP_RESERVED2";
90 case OP_1ADD
: return "OP_1ADD";
91 case OP_1SUB
: return "OP_1SUB";
92 case OP_2MUL
: return "OP_2MUL";
93 case OP_2DIV
: return "OP_2DIV";
94 case OP_NEGATE
: return "OP_NEGATE";
95 case OP_ABS
: return "OP_ABS";
96 case OP_NOT
: return "OP_NOT";
97 case OP_0NOTEQUAL
: return "OP_0NOTEQUAL";
98 case OP_ADD
: return "OP_ADD";
99 case OP_SUB
: return "OP_SUB";
100 case OP_MUL
: return "OP_MUL";
101 case OP_DIV
: return "OP_DIV";
102 case OP_MOD
: return "OP_MOD";
103 case OP_LSHIFT
: return "OP_LSHIFT";
104 case OP_RSHIFT
: return "OP_RSHIFT";
105 case OP_BOOLAND
: return "OP_BOOLAND";
106 case OP_BOOLOR
: return "OP_BOOLOR";
107 case OP_NUMEQUAL
: return "OP_NUMEQUAL";
108 case OP_NUMEQUALVERIFY
: return "OP_NUMEQUALVERIFY";
109 case OP_NUMNOTEQUAL
: return "OP_NUMNOTEQUAL";
110 case OP_LESSTHAN
: return "OP_LESSTHAN";
111 case OP_GREATERTHAN
: return "OP_GREATERTHAN";
112 case OP_LESSTHANOREQUAL
: return "OP_LESSTHANOREQUAL";
113 case OP_GREATERTHANOREQUAL
: return "OP_GREATERTHANOREQUAL";
114 case OP_MIN
: return "OP_MIN";
115 case OP_MAX
: return "OP_MAX";
116 case OP_WITHIN
: return "OP_WITHIN";
119 case OP_RIPEMD160
: return "OP_RIPEMD160";
120 case OP_SHA1
: return "OP_SHA1";
121 case OP_SHA256
: return "OP_SHA256";
122 case OP_HASH160
: return "OP_HASH160";
123 case OP_HASH256
: return "OP_HASH256";
124 case OP_CODESEPARATOR
: return "OP_CODESEPARATOR";
125 case OP_CHECKSIG
: return "OP_CHECKSIG";
126 case OP_CHECKSIGVERIFY
: return "OP_CHECKSIGVERIFY";
127 case OP_CHECKMULTISIG
: return "OP_CHECKMULTISIG";
128 case OP_CHECKMULTISIGVERIFY
: return "OP_CHECKMULTISIGVERIFY";
131 case OP_NOP1
: return "OP_NOP1";
132 case OP_CHECKLOCKTIMEVERIFY
: return "OP_CHECKLOCKTIMEVERIFY";
133 case OP_CHECKSEQUENCEVERIFY
: return "OP_CHECKSEQUENCEVERIFY";
134 case OP_NOP4
: return "OP_NOP4";
135 case OP_NOP5
: return "OP_NOP5";
136 case OP_NOP6
: return "OP_NOP6";
137 case OP_NOP7
: return "OP_NOP7";
138 case OP_NOP8
: return "OP_NOP8";
139 case OP_NOP9
: return "OP_NOP9";
140 case OP_NOP10
: return "OP_NOP10";
142 case OP_INVALIDOPCODE
: return "OP_INVALIDOPCODE";
145 // The template matching params OP_SMALLINTEGER/etc are defined in opcodetype enum
146 // as kind of implementation hack, they are *NOT* real opcodes. If found in real
147 // Script, just let the default: case deal with them.
154 unsigned int CScript::GetSigOpCount(bool fAccurate
) const
157 const_iterator pc
= begin();
158 opcodetype lastOpcode
= OP_INVALIDOPCODE
;
162 if (!GetOp(pc
, opcode
))
164 if (opcode
== OP_CHECKSIG
|| opcode
== OP_CHECKSIGVERIFY
)
166 else if (opcode
== OP_CHECKMULTISIG
|| opcode
== OP_CHECKMULTISIGVERIFY
)
168 if (fAccurate
&& lastOpcode
>= OP_1
&& lastOpcode
<= OP_16
)
169 n
+= DecodeOP_N(lastOpcode
);
171 n
+= MAX_PUBKEYS_PER_MULTISIG
;
178 unsigned int CScript::GetSigOpCount(const CScript
& scriptSig
) const
180 if (!IsPayToScriptHash())
181 return GetSigOpCount(true);
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())
191 if (!scriptSig
.GetOp(pc
, opcode
, vData
))
197 /// ... and return its opcount:
198 CScript
subscript(vData
.begin(), vData
.end());
199 return subscript
.GetSigOpCount(true);
202 bool CScript::IsPayToScriptHash() const
204 // Extra-fast test for pay-to-script-hash CScripts:
205 return (this->size() == 23 &&
206 (*this)[0] == OP_HASH160
&&
207 (*this)[1] == 0x14 &&
208 (*this)[22] == OP_EQUAL
);
211 bool CScript::IsPayToWitnessScriptHash() const
213 // Extra-fast test for pay-to-witness-script-hash CScripts:
214 return (this->size() == 34 &&
215 (*this)[0] == OP_0
&&
219 // A witness program is any valid CScript that consists of a 1-byte push opcode
220 // followed by a data push between 2 and 40 bytes.
221 bool CScript::IsWitnessProgram(int& version
, std::vector
<unsigned char>& program
) const
223 if (this->size() < 4 || this->size() > 42) {
226 if ((*this)[0] != OP_0
&& ((*this)[0] < OP_1
|| (*this)[0] > OP_16
)) {
229 if ((size_t)((*this)[1] + 2) == this->size()) {
230 version
= DecodeOP_N((opcodetype
)(*this)[0]);
231 program
= std::vector
<unsigned char>(this->begin() + 2, this->end());
237 bool CScript::IsPushOnly(const_iterator pc
) const
242 if (!GetOp(pc
, opcode
))
244 // Note that IsPushOnly() *does* consider OP_RESERVED to be a
245 // push-type opcode, however execution of OP_RESERVED fails, so
246 // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
247 // the P2SH special validation code being executed.
254 bool CScript::IsPushOnly() const
256 return this->IsPushOnly(begin());
259 std::string
CScriptWitness::ToString() const
261 std::string ret
= "CScriptWitness(";
262 for (unsigned int i
= 0; i
< stack
.size(); i
++) {
266 ret
+= HexStr(stack
[i
]);