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