]> Git Repo - VerusCoin.git/blame - src/script/script.cpp
Update contrib/debian/copyright for ax_boost_thread.m4
[VerusCoin.git] / src / script / script.cpp
CommitLineData
cbd22a50 1// Copyright (c) 2009-2010 Satoshi Nakamoto
f914f1a7 2// Copyright (c) 2009-2014 The Bitcoin Core developers
2d79bba3 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
11using namespace std;
12
13const 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
c938fb1f 132 // expansion
cbd22a50 133 case OP_NOP1 : return "OP_NOP1";
134 case OP_NOP2 : return "OP_NOP2";
135 case OP_NOP3 : return "OP_NOP3";
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_SMALLDATA/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
156unsigned 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 += 20;
174 }
175 lastOpcode = opcode;
176 }
177 return n;
178}
179
180unsigned 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
6cf0e50b
LR
204// insightexplorer
205// https://github.com/bitpay/bitcoin/commit/017f548ea6d89423ef568117447e61dd5707ec42#diff-f7ca24fb80ddba0f291cb66344ca6fcbR204
206bool CScript::IsPayToPublicKeyHash() const
207{
208 // Extra-fast test for pay-to-pubkey-hash CScripts:
209 return (this->size() == 25 &&
210 (*this)[0] == OP_DUP &&
211 (*this)[1] == OP_HASH160 &&
212 (*this)[2] == 0x14 &&
213 (*this)[23] == OP_EQUALVERIFY &&
214 (*this)[24] == OP_CHECKSIG);
215}
216
cbd22a50 217bool CScript::IsPayToScriptHash() const
218{
219 // Extra-fast test for pay-to-script-hash CScripts:
220 return (this->size() == 23 &&
29a8ade7
PW
221 (*this)[0] == OP_HASH160 &&
222 (*this)[1] == 0x14 &&
223 (*this)[22] == OP_EQUAL);
cbd22a50 224}
225
226bool CScript::IsPushOnly() const
227{
228 const_iterator pc = begin();
229 while (pc < end())
230 {
231 opcodetype opcode;
232 if (!GetOp(pc, opcode))
233 return false;
234 // Note that IsPushOnly() *does* consider OP_RESERVED to be a
235 // push-type opcode, however execution of OP_RESERVED fails, so
d752ba86 236 // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
cbd22a50 237 // the P2SH special validation code being executed.
238 if (opcode > OP_16)
239 return false;
240 }
241 return true;
242}
6cf0e50b
LR
243
244// insightexplorer
f381d4e0 245CScript::ScriptType CScript::GetType() const
6cf0e50b
LR
246{
247 if (this->IsPayToPublicKeyHash())
f381d4e0 248 return CScript::P2PKH;
6cf0e50b 249 if (this->IsPayToScriptHash())
f381d4e0 250 return CScript::P2SH;
6cf0e50b 251 // We don't know this script
f381d4e0 252 return CScript::UNKNOWN;
6cf0e50b
LR
253}
254
255// insightexplorer
256uint160 CScript::AddressHash() const
257{
258 // where the address bytes begin depends on the script type
259 int start;
260 if (this->IsPayToPublicKeyHash())
261 start = 3;
262 else if (this->IsPayToScriptHash())
263 start = 2;
264 else {
3d018ab5 265 // unknown script type; return zeros
6cf0e50b 266 vector<unsigned char> hashBytes;
3d018ab5 267 hashBytes.resize(20);
6cf0e50b
LR
268 return uint160(hashBytes);
269 }
270 vector<unsigned char> hashBytes(this->begin()+start, this->begin()+start+20);
271 return uint160(hashBytes);
272}
This page took 0.136854 seconds and 4 git commands to generate.