-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBitcoinScript.cpp
746 lines (727 loc) · 14.6 KB
/
BitcoinScript.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#include "BitcoinScript.h"
#include <assert.h>
#include <string.h>
enum ScriptOpcodes
{
OP_0 = 0x00,
OP_PUSHDATA1 = 0x4c,
OP_PUSHDATA2 = 0x4d,
OP_PUSHDATA4 = 0x4e,
OP_1NEGATE = 0x4f,
OP_RESERVED = 0x50,
OP_1 = 0x51,
OP_2 = 0x52,
OP_3 = 0x53,
OP_4 = 0x54,
OP_5 = 0x55,
OP_6 = 0x56,
OP_7 = 0x57,
OP_8 = 0x58,
OP_9 = 0x59,
OP_10 = 0x5a,
OP_11 = 0x5b,
OP_12 = 0x5c,
OP_13 = 0x5d,
OP_14 = 0x5e,
OP_15 = 0x5f,
OP_16 = 0x60,
OP_NOP = 0x61,
OP_VER = 0x62,
OP_IF = 0x63,
OP_NOTIF = 0x64,
OP_VERIF = 0x65,
OP_VERNOTIF = 0x66,
OP_ELSE = 0x67,
OP_ENDIF = 0x68,
OP_VERIFY = 0x69,
OP_RETURN = 0x6a,
OP_TOALTSTACK = 0x6b,
OP_FROMALTSTACK = 0x6c,
OP_2DROP = 0x6d,
OP_2DUP = 0x6e,
OP_3DUP = 0x6f,
OP_2OVER = 0x70,
OP_2ROT = 0x71,
OP_2SWAP = 0x72,
OP_IFDUP = 0x73,
OP_DEPTH = 0x74,
OP_DROP = 0x75,
OP_DUP = 0x76,
OP_NIP = 0x77,
OP_OVER = 0x78,
OP_PICK = 0x79,
OP_ROLL = 0x7a,
OP_ROT = 0x7b,
OP_SWAP = 0x7c,
OP_TUCK = 0x7d,
OP_CAT = 0x7e, // Currently disabled
OP_SUBSTR = 0x7f, // Currently disabled
OP_LEFT = 0x80, // Currently disabled
OP_RIGHT = 0x81, // Currently disabled
OP_SIZE = 0x82, // Currently disabled
OP_INVERT = 0x83, // Currently disabled
OP_AND = 0x84, // Currently disabled
OP_OR = 0x85, // Currently disabled
OP_XOR = 0x86, // Currently disabled
OP_EQUAL = 0x87,
OP_EQUALVERIFY = 0x88,
OP_RESERVED1 = 0x89,
OP_RESERVED2 = 0x8a,
OP_1ADD = 0x8b,
OP_1SUB = 0x8c,
OP_2MUL = 0x8d, // Currently disabled
OP_2DIV = 0x8e, // Currently disabled
OP_NEGATE = 0x8f,
OP_ABS = 0x90,
OP_NOT = 0x91,
OP_0NOTEQUAL = 0x92,
OP_ADD = 0x93,
OP_SUB = 0x94,
OP_MUL = 0x95, // Currently disabled
OP_DIV = 0x96, // Currently disabled
OP_MOD = 0x97, // Currently disabled
OP_LSHIFT = 0x98, // Currently disabled
OP_RSHIFT = 0x99, // Currently disabled
OP_BOOLAND = 0x9a,
OP_BOOLOR = 0x9b,
OP_NUMEQUAL = 0x9c,
OP_NUMEQUALVERIFY = 0x9d,
OP_NUMNOTEQUAL = 0x9e,
OP_LESSTHAN = 0x9f,
OP_GREATERTHAN = 0xa0,
OP_LESSTHANOREQUAL = 0xa1,
OP_GREATERTHANOREQUAL = 0xa2,
OP_MIN = 0xa3,
OP_MAX = 0xa4,
OP_WITHIN = 0xa5,
OP_RIPEMD160 = 0xa6,
OP_SHA1 = 0xa7,
OP_SHA256 = 0xa8,
OP_HASH160 = 0xa9,
OP_HASH256 = 0xaa,
OP_CODESEPARATOR = 0xab,
OP_CHECKSIG = 0xac,
OP_CHECKSIGVERIFY = 0xad,
OP_CHECKMULTISIG = 0xae,
OP_CHECKMULTISIGVERIFY = 0xaf,
OP_NOP1 = 0xb0,
OP_NOP2 = 0xb1,
OP_NOP3 = 0xb2,
OP_NOP4 = 0xb3,
OP_NOP5 = 0xb4,
OP_NOP6 = 0xb5,
OP_NOP7 = 0xb6,
OP_NOP8 = 0xb7,
OP_NOP9 = 0xb8,
OP_NOP10 = 0xb9,
OP_SMALLINTEGER = 0xfa,
OP_PUBKEYS = 0xfb,
OP_PUBKEYHASH = 0xfd,
OP_PUBKEY = 0xfe,
OP_INVALIDOPCODE = 0xff
};
#define MAX_STACK_SIZE 2048 // maximum size of the stack
#define MAX_STACK_ELEMENTS 64 // never expect more than 64 elements to be pushed onto the stack
class BitcoinScriptImpl : public BitcoinScript
{
public:
// A simple stack class for processing bitcoin scripts
class Stack
{
public:
Stack(void)
{
resetStack();
}
void resetStack(void)
{
mStackPtr8 = mStack;
mStackIndex = 0;
}
void push(uint8_t size,const uint8_t *&script)
{
mStackElements[mStackIndex] = mStackPtr8;
mStackSize[mStackIndex] = size;
mStackIndex++;
assert( mStackIndex < MAX_STACK_ELEMENTS );
switch ( size )
{
case 0:
// it is considered valid to push zero bytes onto the stack; essentially just increments the stack pointer
break;
case 1:
*mStackPtr8++ = *script; // copy one byte
break;
case 2:
*mStackPtr16++ = *script; // copy two bytes
break;
case 4:
*mStackPtr32++ = *script; // copy 4 bytes
break;
case 8:
*mStackPtr64++ = *script; // copy 8 bytes
break;
default:
memcpy(mStackPtr8,script,size);
mStackPtr8+=size;
break;
}
script+=size; // advance the script intput instruction pointer.
assert(mStackPtr8 < &mStack[MAX_STACK_SIZE]); // assert that we have not had a stack overflow
}
inline uint32_t getStackSize(void) const
{
return mStackIndex;
}
inline const uint8_t *getStackEntry(uint32_t offset,uint8_t &size) const
{
assert( offset <= mStackIndex );
offset = mStackIndex-offset;
size = mStackSize[offset];
return mStackElements[offset];
}
union
{
uint8_t *mStackPtr8; // current stack pointer as U8
uint16_t *mStackPtr16; // current stack pointer as U16
uint32_t *mStackPtr32; // current stack pointer.as U32
uint64_t *mStackPtr64; // current stack pointer.as U64
};
uint8_t mStack[MAX_STACK_SIZE];
uint32_t mStackIndex;
uint8_t *mStackElements[MAX_STACK_ELEMENTS];
uint8_t mStackSize[MAX_STACK_ELEMENTS];
};
BitcoinScriptImpl(void)
{
mLastError = NULL;
}
~BitcoinScriptImpl(void)
{
}
virtual bool executeScript(const uint8_t *script,uint32_t scriptLength)
{
bool ret = true;
mLastError = NULL; // clear the last error state
const uint8_t *endOfScript = script+scriptLength;
while ( script < endOfScript && ret )
{
uint8_t opcode = *script++;
if ( opcode < OP_PUSHDATA1 ) // Any opcode less than OP_PUSHDATA1 is simply an instruction to push that many bytes onto the stack
{
mStack.push(opcode,script);
}
else
{
switch ( opcode )
{
case OP_0:
{
assert(0); // Not yet implemented
}
break;
case OP_PUSHDATA1:
{
assert(0); // Not yet implemented
}
break;
case OP_PUSHDATA2:
{
assert(0); // Not yet implemented
}
break;
case OP_PUSHDATA4:
{
assert(0); // Not yet implemented
}
break;
case OP_RESERVED:
{
assert(0); // Not yet implemented
}
break;
case OP_1NEGATE:
case OP_1:
case OP_2:
case OP_3:
case OP_4:
case OP_5:
case OP_6:
case OP_7:
case OP_8:
case OP_9:
case OP_10:
case OP_11:
case OP_12:
case OP_13:
case OP_14:
case OP_15:
case OP_16:
{
//int value = (int)opcode - (int)(OP_1 - 1);
assert(0); // Not yet implemented
}
break;
case OP_NOP:
{
assert(0); // Not yet implemented
}
break;
case OP_VER:
{
assert(0); // Not yet implemented
}
break;
case OP_IF:
{
assert(0); // Not yet implemented
}
break;
case OP_NOTIF:
{
assert(0); // Not yet implemented
}
break;
case OP_VERIF:
{
assert(0); // Not yet implemented
}
break;
case OP_VERNOTIF:
{
assert(0); // Not yet implemented
}
break;
case OP_ELSE:
{
assert(0); // Not yet implemented
}
break;
case OP_ENDIF:
{
assert(0); // Not yet implemented
}
break;
case OP_VERIFY:
{
assert(0); // Not yet implemented
}
break;
case OP_RETURN:
{
assert(0); // Not yet implemented
}
break;
case OP_TOALTSTACK:
{
assert(0); // Not yet implemented
}
break;
case OP_FROMALTSTACK:
{
assert(0); // Not yet implemented
}
break;
case OP_2DROP:
{
assert(0); // Not yet implemented
}
break;
case OP_2DUP:
{
assert(0); // Not yet implemented
}
break;
case OP_3DUP:
{
assert(0); // Not yet implemented
}
break;
case OP_2OVER:
{
assert(0); // Not yet implemented
}
break;
case OP_2ROT:
{
assert(0); // Not yet implemented
}
break;
case OP_2SWAP:
{
assert(0); // Not yet implemented
}
break;
case OP_IFDUP:
{
assert(0); // Not yet implemented
}
break;
case OP_DEPTH:
{
assert(0); // Not yet implemented
}
break;
case OP_DROP:
{
assert(0); // Not yet implemented
}
break;
case OP_DUP:
{
if ( mStack.getStackSize() < 1 )
{
ret = false;
mLastError = "OP_DUP : Nothing on the stack to duplicate";
}
else
{
uint8_t len;
const uint8_t *val = mStack.getStackEntry(1,len);
mStack.push(len,val);
}
}
break;
case OP_NIP:
{
assert(0); // Not yet implemented
}
break;
case OP_OVER:
{
assert(0); // Not yet implemented
}
break;
case OP_PICK:
{
assert(0); // Not yet implemented
}
break;
case OP_ROLL:
{
assert(0); // Not yet implemented
}
break;
case OP_ROT:
{
assert(0); // Not yet implemented
}
break;
case OP_SWAP:
{
assert(0); // Not yet implemented
}
break;
case OP_TUCK:
{
assert(0); // Not yet implemented
}
break;
case OP_SIZE:
{
assert(0); // Not yet implemented
}
break;
case OP_CAT:
case OP_SUBSTR:
case OP_LEFT:
case OP_RIGHT:
case OP_INVERT:
case OP_AND:
case OP_OR:
case OP_XOR:
case OP_2MUL:
case OP_2DIV:
case OP_MUL:
case OP_DIV:
case OP_MOD:
case OP_LSHIFT:
case OP_RSHIFT:
{
mLastError = "Encountered a disabled opcode";
ret = true;
}
break;
case OP_EQUAL:
{
assert(0); // Not yet implemented
}
break;
case OP_EQUALVERIFY:
{
assert(0); // Not yet implemented
}
break;
case OP_RESERVED1:
{
assert(0); // Not yet implemented
}
break;
case OP_RESERVED2:
{
assert(0); // Not yet implemented
}
break;
case OP_1ADD:
{
assert(0); // Not yet implemented
}
break;
case OP_1SUB:
{
assert(0); // Not yet implemented
}
break;
case OP_NEGATE:
{
assert(0); // Not yet implemented
}
break;
case OP_ABS:
{
assert(0); // Not yet implemented
}
break;
case OP_NOT:
{
assert(0); // Not yet implemented
}
break;
case OP_0NOTEQUAL:
{
assert(0); // Not yet implemented
}
break;
case OP_ADD:
{
assert(0); // Not yet implemented
}
break;
case OP_SUB:
{
assert(0); // Not yet implemented
}
break;
case OP_BOOLAND:
{
assert(0); // Not yet implemented
}
break;
case OP_BOOLOR:
{
assert(0); // Not yet implemented
}
break;
case OP_NUMEQUAL:
{
assert(0); // Not yet implemented
}
break;
case OP_NUMEQUALVERIFY:
{
assert(0); // Not yet implemented
}
break;
case OP_NUMNOTEQUAL:
{
assert(0); // Not yet implemented
}
break;
case OP_LESSTHAN:
{
assert(0); // Not yet implemented
}
break;
case OP_GREATERTHAN:
{
assert(0); // Not yet implemented
}
break;
case OP_LESSTHANOREQUAL:
{
assert(0); // Not yet implemented
}
break;
case OP_GREATERTHANOREQUAL:
{
assert(0); // Not yet implemented
}
break;
case OP_MIN:
{
assert(0); // Not yet implemented
}
break;
case OP_MAX:
{
assert(0); // Not yet implemented
}
break;
case OP_WITHIN:
{
assert(0); // Not yet implemented
}
break;
case OP_RIPEMD160:
{
assert(0); // Not yet implemented
}
break;
case OP_SHA1:
{
assert(0); // Not yet implemented
}
break;
case OP_SHA256:
{
assert(0); // Not yet implemented
}
break;
case OP_HASH160:
{
assert(0); // Not yet implemented
}
break;
case OP_HASH256:
{
assert(0); // Not yet implemented
}
break;
case OP_CODESEPARATOR:
{
assert(0); // Not yet implemented
}
break;
case OP_CHECKSIG:
{
if ( mStack.getStackSize() < 2 ) // must be two items on the stack!
{
ret = false;
mLastError = "OP_CHECKSIG : Not two data items on the stack.";
}
else
{
assert(0); // Not yet implemented
}
}
break;
case OP_CHECKSIGVERIFY:
{
assert(0); // Not yet implemented
}
break;
case OP_CHECKMULTISIG:
{
assert(0); // Not yet implemented
}
break;
case OP_CHECKMULTISIGVERIFY:
{
assert(0); // Not yet implemented
}
break;
case OP_NOP1:
{
assert(0); // Not yet implemented
}
break;
case OP_NOP2:
{
assert(0); // Not yet implemented
}
break;
case OP_NOP3:
{
assert(0); // Not yet implemented
}
break;
case OP_NOP4:
{
assert(0); // Not yet implemented
}
break;
case OP_NOP5:
{
assert(0); // Not yet implemented
}
break;
case OP_NOP6:
{
assert(0); // Not yet implemented
}
break;
case OP_NOP7:
{
assert(0); // Not yet implemented
}
break;
case OP_NOP8:
{
assert(0); // Not yet implemented
}
break;
case OP_NOP9:
{
assert(0); // Not yet implemented
}
break;
case OP_NOP10:
{
assert(0); // Not yet implemented
}
break;
case OP_SMALLINTEGER:
{
assert(0); // Not yet implemented
}
break;
case OP_PUBKEYS:
{
assert(0); // Not yet implemented
}
break;
case OP_PUBKEYHASH:
{
assert(0); // Not yet implemented
}
break;
case OP_PUBKEY:
{
assert(0); // Not yet implemented
}
break;
case OP_INVALIDOPCODE:
{
assert(0); // Not yet implemented
}
break;
default:
{
assert(0); // invalid opcode!
}
break;
}
}
}
return ret;
}
virtual void resetStack(void)
{
mStack.resetStack();
}
virtual void release(void)
{
delete this;
}
virtual const char * getLastError(void) const
{
return mLastError;
}
const char *mLastError;
Stack mStack; // the script virtual machine stack
};
BitcoinScript *createBitcoinScript(void)
{
BitcoinScriptImpl *b = new BitcoinScriptImpl;
return static_cast< BitcoinScript *>(b);
}