Init some fields, add ToString() for Hash160 and Hash256, start implementation of...
[NovacoinLibrary.git] / Novacoin / CScript.cs
1 \feffusing System;
2 using System.Text;
3
4 namespace Novacoin
5 {
6         /** Script opcodes */
7         enum opcodetype
8         {
9                 // push value
10                 OP_0 = 0x00,
11                 OP_FALSE = OP_0,
12                 OP_PUSHDATA1 = 0x4c,
13                 OP_PUSHDATA2 = 0x4d,
14                 OP_PUSHDATA4 = 0x4e,
15                 OP_1NEGATE = 0x4f,
16                 OP_RESERVED = 0x50,
17                 OP_1 = 0x51,
18                 OP_TRUE=OP_1,
19                 OP_2 = 0x52,
20                 OP_3 = 0x53,
21                 OP_4 = 0x54,
22                 OP_5 = 0x55,
23                 OP_6 = 0x56,
24                 OP_7 = 0x57,
25                 OP_8 = 0x58,
26                 OP_9 = 0x59,
27                 OP_10 = 0x5a,
28                 OP_11 = 0x5b,
29                 OP_12 = 0x5c,
30                 OP_13 = 0x5d,
31                 OP_14 = 0x5e,
32                 OP_15 = 0x5f,
33                 OP_16 = 0x60,
34
35                 // control
36                 OP_NOP = 0x61,
37                 OP_VER = 0x62,
38                 OP_IF = 0x63,
39                 OP_NOTIF = 0x64,
40                 OP_VERIF = 0x65,
41                 OP_VERNOTIF = 0x66,
42                 OP_ELSE = 0x67,
43                 OP_ENDIF = 0x68,
44                 OP_VERIFY = 0x69,
45                 OP_RETURN = 0x6a,
46
47                 // stack ops
48                 OP_TOALTSTACK = 0x6b,
49                 OP_FROMALTSTACK = 0x6c,
50                 OP_2DROP = 0x6d,
51                 OP_2DUP = 0x6e,
52                 OP_3DUP = 0x6f,
53                 OP_2OVER = 0x70,
54                 OP_2ROT = 0x71,
55                 OP_2SWAP = 0x72,
56                 OP_IFDUP = 0x73,
57                 OP_DEPTH = 0x74,
58                 OP_DROP = 0x75,
59                 OP_DUP = 0x76,
60                 OP_NIP = 0x77,
61                 OP_OVER = 0x78,
62                 OP_PICK = 0x79,
63                 OP_ROLL = 0x7a,
64                 OP_ROT = 0x7b,
65                 OP_SWAP = 0x7c,
66                 OP_TUCK = 0x7d,
67
68                 // splice ops
69                 OP_CAT = 0x7e,
70                 OP_SUBSTR = 0x7f,
71                 OP_LEFT = 0x80,
72                 OP_RIGHT = 0x81,
73                 OP_SIZE = 0x82,
74
75                 // bit logic
76                 OP_INVERT = 0x83,
77                 OP_AND = 0x84,
78                 OP_OR = 0x85,
79                 OP_XOR = 0x86,
80                 OP_EQUAL = 0x87,
81                 OP_EQUALVERIFY = 0x88,
82                 OP_RESERVED1 = 0x89,
83                 OP_RESERVED2 = 0x8a,
84
85                 // numeric
86                 OP_1ADD = 0x8b,
87                 OP_1SUB = 0x8c,
88                 OP_2MUL = 0x8d,
89                 OP_2DIV = 0x8e,
90                 OP_NEGATE = 0x8f,
91                 OP_ABS = 0x90,
92                 OP_NOT = 0x91,
93                 OP_0NOTEQUAL = 0x92,
94
95                 OP_ADD = 0x93,
96                 OP_SUB = 0x94,
97                 OP_MUL = 0x95,
98                 OP_DIV = 0x96,
99                 OP_MOD = 0x97,
100                 OP_LSHIFT = 0x98,
101                 OP_RSHIFT = 0x99,
102
103                 OP_BOOLAND = 0x9a,
104                 OP_BOOLOR = 0x9b,
105                 OP_NUMEQUAL = 0x9c,
106                 OP_NUMEQUALVERIFY = 0x9d,
107                 OP_NUMNOTEQUAL = 0x9e,
108                 OP_LESSTHAN = 0x9f,
109                 OP_GREATERTHAN = 0xa0,
110                 OP_LESSTHANOREQUAL = 0xa1,
111                 OP_GREATERTHANOREQUAL = 0xa2,
112                 OP_MIN = 0xa3,
113                 OP_MAX = 0xa4,
114
115                 OP_WITHIN = 0xa5,
116
117                 // crypto
118                 OP_RIPEMD160 = 0xa6,
119                 OP_SHA1 = 0xa7,
120                 OP_SHA256 = 0xa8,
121                 OP_HASH160 = 0xa9,
122                 OP_HASH256 = 0xaa,
123                 OP_CODESEPARATOR = 0xab,
124                 OP_CHECKSIG = 0xac,
125                 OP_CHECKSIGVERIFY = 0xad,
126                 OP_CHECKMULTISIG = 0xae,
127                 OP_CHECKMULTISIGVERIFY = 0xaf,
128
129                 // expansion
130                 OP_NOP1 = 0xb0,
131                 OP_NOP2 = 0xb1,
132                 OP_NOP3 = 0xb2,
133                 OP_NOP4 = 0xb3,
134                 OP_NOP5 = 0xb4,
135                 OP_NOP6 = 0xb5,
136                 OP_NOP7 = 0xb6,
137                 OP_NOP8 = 0xb7,
138                 OP_NOP9 = 0xb8,
139                 OP_NOP10 = 0xb9,
140
141
142                 // template matching params
143                 OP_SMALLDATA = 0xf9,
144                 OP_SMALLINTEGER = 0xfa,
145                 OP_PUBKEYS = 0xfb,
146                 OP_PUBKEYHASH = 0xfd,
147                 OP_PUBKEY = 0xfe,
148
149                 OP_INVALIDOPCODE = 0xff,
150         };
151
152         public class CScript
153         {
154                 private byte[] scriptCode = {};
155
156                 public CScript ()
157                 {
158                 }
159
160                 private string GetOpName(opcodetype opcode)
161                 {
162                         switch (opcode) {
163                         // push value
164                         case opcodetype.OP_0:
165                                 return "0";
166                         case opcodetype.OP_PUSHDATA1:
167                                 return "OP_PUSHDATA1";
168                         case opcodetype.OP_PUSHDATA2:
169                                 return "OP_PUSHDATA2";
170                         case opcodetype.OP_PUSHDATA4:
171                                 return "OP_PUSHDATA4";
172                         case opcodetype.OP_1NEGATE:
173                                 return "-1";
174                         case opcodetype.OP_RESERVED:
175                                 return "OP_RESERVED";
176                         case opcodetype.OP_1:
177                                 return "1";
178                         case opcodetype.OP_2:
179                                 return "2";
180                         case opcodetype.OP_3:
181                                 return "3";
182                         case opcodetype.OP_4:
183                                 return "4";
184                         case opcodetype.OP_5:
185                                 return "5";
186                         case opcodetype.OP_6:
187                                 return "6";
188                         case opcodetype.OP_7:
189                                 return "7";
190                         case opcodetype.OP_8:
191                                 return "8";
192                         case opcodetype.OP_9:
193                                 return "9";
194                         case opcodetype.OP_10:
195                                 return "10";
196                         case opcodetype.OP_11:
197                                 return "11";
198                         case opcodetype.OP_12:
199                                 return "12";
200                         case opcodetype.OP_13:
201                                 return "13";
202                         case opcodetype.OP_14:
203                                 return "14";
204                         case opcodetype.OP_15:
205                                 return "15";
206                         case opcodetype.OP_16:
207                                 return "16";
208
209                         // control
210                         case opcodetype.OP_NOP:
211                                 return "OP_NOP";
212                         case opcodetype.OP_VER:
213                                 return "OP_VER";
214                         case opcodetype.OP_IF:
215                                 return "OP_IF";
216                         case opcodetype.OP_NOTIF:
217                                 return "OP_NOTIF";
218                         case opcodetype.OP_VERIF:
219                                 return "OP_VERIF";
220                         case opcodetype.OP_VERNOTIF:
221                                 return "OP_VERNOTIF";
222                         case opcodetype.OP_ELSE:
223                                 return "OP_ELSE";
224                         case opcodetype.OP_ENDIF:
225                                 return "OP_ENDIF";
226                         case opcodetype.OP_VERIFY:
227                                 return "OP_VERIFY";
228                         case opcodetype.OP_RETURN:
229                                 return "OP_RETURN";
230
231                         // stack ops
232                         case opcodetype.OP_TOALTSTACK:
233                                 return "OP_TOALTSTACK";
234                         case opcodetype.OP_FROMALTSTACK:
235                                 return "OP_FROMALTSTACK";
236                         case opcodetype.OP_2DROP:
237                                 return "OP_2DROP";
238                         case opcodetype.OP_2DUP:
239                                 return "OP_2DUP";
240                         case opcodetype.OP_3DUP:
241                                 return "OP_3DUP";
242                         case opcodetype.OP_2OVER:
243                                 return "OP_2OVER";
244                         case opcodetype.OP_2ROT:
245                                 return "OP_2ROT";
246                         case opcodetype.OP_2SWAP:
247                                 return "OP_2SWAP";
248                         case opcodetype.OP_IFDUP:
249                                 return "OP_IFDUP";
250                         case opcodetype.OP_DEPTH:
251                                 return "OP_DEPTH";
252                         case opcodetype.OP_DROP:
253                                 return "OP_DROP";
254                         case opcodetype.OP_DUP:
255                                 return "OP_DUP";
256                         case opcodetype.OP_NIP:
257                                 return "OP_NIP";
258                         case opcodetype.OP_OVER:
259                                 return "OP_OVER";
260                         case opcodetype.OP_PICK:
261                                 return "OP_PICK";
262                         case opcodetype.OP_ROLL:
263                                 return "OP_ROLL";
264                         case opcodetype.OP_ROT:
265                                 return "OP_ROT";
266                         case opcodetype.OP_SWAP:
267                                 return "OP_SWAP";
268                         case opcodetype.OP_TUCK:
269                                 return "OP_TUCK";
270
271                         // splice ops
272                         case opcodetype.OP_CAT:
273                                 return "OP_CAT";
274                         case opcodetype.OP_SUBSTR:
275                                 return "OP_SUBSTR";
276                         case opcodetype.OP_LEFT:
277                                 return "OP_LEFT";
278                         case opcodetype.OP_RIGHT:
279                                 return "OP_RIGHT";
280                         case opcodetype.OP_SIZE:
281                                 return "OP_SIZE";
282
283                         // bit logic
284                         case opcodetype.OP_INVERT:
285                                 return "OP_INVERT";
286                         case opcodetype.OP_AND:
287                                 return "OP_AND";
288                         case opcodetype.OP_OR:
289                                 return "OP_OR";
290                         case opcodetype.OP_XOR:
291                                 return "OP_XOR";
292                         case opcodetype.OP_EQUAL:
293                                 return "OP_EQUAL";
294                         case opcodetype.OP_EQUALVERIFY:
295                                 return "OP_EQUALVERIFY";
296                         case opcodetype.OP_RESERVED1:
297                                 return "OP_RESERVED1";
298                         case opcodetype.OP_RESERVED2:
299                                 return "OP_RESERVED2";
300
301                         // numeric
302                         case opcodetype.OP_1ADD:
303                                 return "OP_1ADD";
304                         case opcodetype.OP_1SUB:
305                                 return "OP_1SUB";
306                         case opcodetype.OP_2MUL:
307                                 return "OP_2MUL";
308                         case opcodetype.OP_2DIV:
309                                 return "OP_2DIV";
310                         case opcodetype.OP_NEGATE:
311                                 return "OP_NEGATE";
312                         case opcodetype.OP_ABS:
313                                 return "OP_ABS";
314                         case opcodetype.OP_NOT:
315                                 return "OP_NOT";
316                         case opcodetype.OP_0NOTEQUAL:
317                                 return "OP_0NOTEQUAL";
318                         case opcodetype.OP_ADD:
319                                 return "OP_ADD";
320                         case opcodetype.OP_SUB:
321                                 return "OP_SUB";
322                         case opcodetype.OP_MUL:
323                                 return "OP_MUL";
324                         case opcodetype.OP_DIV:
325                                 return "OP_DIV";
326                         case opcodetype.OP_MOD:
327                                 return "OP_MOD";
328                         case opcodetype.OP_LSHIFT:
329                                 return "OP_LSHIFT";
330                         case opcodetype.OP_RSHIFT:
331                                 return "OP_RSHIFT";
332                         case opcodetype.OP_BOOLAND:
333                                 return "OP_BOOLAND";
334                         case opcodetype.OP_BOOLOR:
335                                 return "OP_BOOLOR";
336                         case opcodetype.OP_NUMEQUAL:
337                                 return "OP_NUMEQUAL";
338                         case opcodetype.OP_NUMEQUALVERIFY:
339                                 return "OP_NUMEQUALVERIFY";
340                         case opcodetype.OP_NUMNOTEQUAL:
341                                 return "OP_NUMNOTEQUAL";
342                         case opcodetype.OP_LESSTHAN:
343                                 return "OP_LESSTHAN";
344                         case opcodetype.OP_GREATERTHAN:
345                                 return "OP_GREATERTHAN";
346                         case opcodetype.OP_LESSTHANOREQUAL:
347                                 return "OP_LESSTHANOREQUAL";
348                         case opcodetype.OP_GREATERTHANOREQUAL:
349                                 return "OP_GREATERTHANOREQUAL";
350                         case opcodetype.OP_MIN:
351                                 return "OP_MIN";
352                         case opcodetype.OP_MAX:
353                                 return "OP_MAX";
354                         case opcodetype.OP_WITHIN:
355                                 return "OP_WITHIN";
356
357                         // crypto
358                         case opcodetype.OP_RIPEMD160:
359                                 return "OP_RIPEMD160";
360                         case opcodetype.OP_SHA1:
361                                 return "OP_SHA1";
362                         case opcodetype.OP_SHA256:
363                                 return "OP_SHA256";
364                         case opcodetype.OP_HASH160:
365                                 return "OP_HASH160";
366                         case opcodetype.OP_HASH256:
367                                 return "OP_HASH256";
368                         case opcodetype.OP_CODESEPARATOR:
369                                 return "OP_CODESEPARATOR";
370                         case opcodetype.OP_CHECKSIG:
371                                 return "OP_CHECKSIG";
372                         case opcodetype.OP_CHECKSIGVERIFY:
373                                 return "OP_CHECKSIGVERIFY";
374                         case opcodetype.OP_CHECKMULTISIG:
375                                 return "OP_CHECKMULTISIG";
376                         case opcodetype.OP_CHECKMULTISIGVERIFY:
377                                 return "OP_CHECKMULTISIGVERIFY";
378
379                         // expanson
380                         case opcodetype.OP_NOP1:
381                                 return "OP_NOP1";
382                         case opcodetype.OP_NOP2:
383                                 return "OP_NOP2";
384                         case opcodetype.OP_NOP3:
385                                 return "OP_NOP3";
386                         case opcodetype.OP_NOP4:
387                                 return "OP_NOP4";
388                         case opcodetype.OP_NOP5:
389                                 return "OP_NOP5";
390                         case opcodetype.OP_NOP6:
391                                 return "OP_NOP6";
392                         case opcodetype.OP_NOP7:
393                                 return "OP_NOP7";
394                         case opcodetype.OP_NOP8:
395                                 return "OP_NOP8";
396                         case opcodetype.OP_NOP9:
397                                 return "OP_NOP9";
398                         case opcodetype.OP_NOP10:
399                                 return "OP_NOP10";
400
401                         // template matching params
402                         case opcodetype.OP_PUBKEYHASH:
403                                 return "OP_PUBKEYHASH";
404                         case opcodetype.OP_PUBKEY:
405                                 return "OP_PUBKEY";
406                         case opcodetype.OP_SMALLDATA:
407                                 return "OP_SMALLDATA";
408
409                         case opcodetype.OP_INVALIDOPCODE:
410                                 return "OP_INVALIDOPCODE";
411                         default:
412                                 return "OP_UNKNOWN";
413                         }
414                 }
415
416                 public override string ToString()
417                 {
418                         // TODO: disassembly 
419
420                         StringBuilder sb = new StringBuilder(scriptCode.Length * 2);
421                         foreach (byte b in scriptCode)
422                         {
423                                 sb.AppendFormat ("{0:x2}", b);
424                         }
425                         return sb.ToString();
426                 }
427         }
428 }
429