-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathop.py
1269 lines (1110 loc) · 36.8 KB
/
op.py
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import hashlib
import math
from ecc import S256Point, Signature
from logging import getLogger
from unittest import TestCase
from helper import (
hash160,
hash256,
)
LOGGER = getLogger(__name__)
# encodes num = converts num to byte format, LE.
def encode_num(num):
if num == 0:
return b''
# absolute value of num.
abs_num = abs(num)
# negative boolean.
negative = num < 0
# The bytearray() method returns a bytearray object which is a mutable (can be modified)
# sequence of integers in the range 0 <= x < 256.
result = bytearray()
# loops abs_num byte by byte and appends each byte to the result.
while abs_num:
# appends abs_num in byte format to result.
result.append(abs_num & 0xff)
# shifts to the next byte.
abs_num >>= 8
# if the top bit is set,
# for negative numbers we ensure that the top bit is set.
# for positive numbers we ensure that the top bit is not set.
# this is because when the top bit is zero, the number is positive.
# when it's 1, the number is negative.
if result[-1] & 0x80: # True only if top bit is a 1
# if top bit is already a 1, I need to add a byte to indicate it's negative or positive.
if negative:
result.append(0x80)
else:
result.append(0)
# if top bit isn't a 1, but number is negative, then I change top bit for a 1.
elif negative:
# ensures that top bit is 1
result[-1] |= 0x80
return bytes(result)
# decodes num from byte format to int.
def decode_num(element):
if element == b'':
return 0
# reverse for big endian
big_endian = element[::-1]
# top bit being 1 means it's negative
if big_endian[0] & 0x80:
negative = True
result = big_endian[0] & 0x7f
else:
negative = False
result = big_endian[0]
for c in big_endian[1:]:
result <<= 8
result += c
if negative:
return -result
else:
return result
"""
The following methods (until op_16) just add encoded numbers to the stack.
"""
def op_0(stack):
stack.append(encode_num(0))
return True
def op_1negate(stack):
stack.append(encode_num(-1))
return True
def op_1(stack):
stack.append(encode_num(1))
return True
def op_2(stack):
stack.append(encode_num(2))
return True
def op_3(stack):
stack.append(encode_num(3))
return True
def op_4(stack):
stack.append(encode_num(4))
return True
def op_5(stack):
stack.append(encode_num(5))
return True
def op_6(stack):
stack.append(encode_num(6))
return True
def op_7(stack):
stack.append(encode_num(7))
return True
def op_8(stack):
stack.append(encode_num(8))
return True
def op_9(stack):
stack.append(encode_num(9))
return True
def op_10(stack):
stack.append(encode_num(10))
return True
def op_11(stack):
stack.append(encode_num(11))
return True
def op_12(stack):
stack.append(encode_num(12))
return True
def op_13(stack):
stack.append(encode_num(13))
return True
def op_14(stack):
stack.append(encode_num(14))
return True
def op_15(stack):
stack.append(encode_num(15))
return True
def op_16(stack):
stack.append(encode_num(16))
return True
# does nothing
def op_nop(stack):
return True
# if the top stack value is not 0, the statements are executed. The top stack value is removed.
# items is a list (array) of commands that come from the script being evaluated (see evaluate method in Script class).
def op_if(stack, cmds):
# if there's nothing in the stack, return False.
if len(stack) < 1:
return False
# go through and re-make the items array based on the top stack element.
if_cmds = [] # will hold the commands for the IF statement.
else_cmds = [] # will hold the commands for the ELSE statement.
current_array = if_cmds
# boolean that indicates the if statement exited correctly.
found = False
# number of endifs needed to exit the if (we don't know where the if ends a priori).
num_endifs_needed = 1
while len(cmds) > 0:
cmd = cmds.pop(0)
# 99 and 100 are OP_IF and OP_NOTIF, so it would mean we have a nested loop.
if cmd in (99, 100):
# we increase the # of endifs needed to exit the if.
num_endifs_needed += 1
# add the OP_IF or OP_NOTIF to the if statement array.
current_array.append(cmd)
# 103 is OP_ELSE and if we only needed 1 endif, then we exit the if and get into the else statement.
elif num_endifs_needed == 1 and cmd == 103:
# else statement items.
current_array = else_cmds
# 104 is and OP_ENDIF
elif cmd == 104:
# if we only needed 1 endif, exit the if statement.
if num_endifs_needed == 1:
found = True
break
else:
# else, subtract 1 to the endifs needed to exit the if statement.
num_endifs_needed -= 1
# add the OP_ENDIF to the if statement array.
current_array.append(cmd)
# for any other operation or element, add it to the if statement array.
else:
current_array.append(cmd)
# if items array is empty and we didn't exit the if statement, fail.
if not found:
return False
# get the top element from the stack
element = stack.pop()
# if top stack element is 0, if there's an OP_ELSE, OP_ELSE statements are executed, else,
# nothing from the if statements gets appended back to items array to be executed.
if decode_num(element) == 0:
cmds[:0] = else_cmds
# else, they are executed. All statements between the IF and the END_IF are executed,
# excluding statements from the OP_ELSE if there's an OP_ELSE.
else:
cmds[:0] = if_cmds
return True
# duplicates the top element of the stack and pushes it to the stack.
def op_dup(stack):
# if stack is empty, return False
if len(stack) < 1:
return False
top_element = stack[-1]
stack.append(top_element)
return True
# consumes top element of the stack, performs a hash256 operation on it and pushes the hashed element
# into the stack.
def op_hash256(stack):
# if stack is empty, return False
if len(stack) < 1:
return False
element = stack.pop()
stack.append(hash256(element))
return True
# consumes top element of the stack, performs a hash160 operation on it and pushes the hashed element
# into the stack.
def op_hash160(stack):
# if stack is empty, return False
if len(stack) < 1:
return False
element = stack.pop()
stack.append(hash160(element))
return True
# consumes top 2 elements, adds them and pushes the result into the stack.
def op_add(stack):
# if stack has less than 2 elements, return False
if len(stack) < 2:
return False
element_1 = decode_num(stack.pop())
element_2 = decode_num(stack.pop())
elem_sum = element_1 + element_2
stack.append(encode_num(elem_sum))
return True
# consumes top 2 elements. pushes a 1 into the stack if the topmost 2 elements are equal. Else pushes a 0.
def op_equal(stack):
# if stack has less than 2 elements, return False
if len(stack) < 2:
return False
element_1 = stack.pop()
element_2 = stack.pop()
if element_1 == element_2:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
# op_endif logic is incorporated in the op_if function.
def op_endif(stack):
return True
# same as op_if, but statements are executed it top value is 0.
def op_notif(stack, cmds):
# if there's nothing in the stack, return False.
if len(stack) < 1:
return False
# go through and re-make the items array based on the top stack element.
if_cmds = [] # will hold the commands for the IF statement.
else_cmds = [] # will hold the commands for the ELSE statement.
current_array = if_cmds
# boolean that indicates the if statement exited correctly.
found = False
# number of endifs needed to exit the if (we don't know where the if ends a priori).
num_endifs_needed = 1
while len(cmds) > 0:
cmd = cmds.pop(0)
# 99 and 100 are OP_IF and OP_NOTIF, so it would mean we have a nested loop.
if cmd in (99, 100):
# we increase the # of endifs needed to exit the if.
num_endifs_needed += 1
# add the OP_IF or OP_NOTIF to the if statement array.
current_array.append(cmd)
# 103 is OP_ELSE and if we only needed 1 endif, then we exit the if and get into the else statement.
elif num_endifs_needed == 1 and cmd == 103:
# else statement items.
current_array = else_cmds
# 104 is and OP_ENDIF
elif cmd == 104:
# if we only needed 1 endif, exit the if statement.
if num_endifs_needed == 1:
found = True
break
else:
# else, subtract 1 to the endifs needed to exit the if statement.
num_endifs_needed -= 1
# add the OP_ENDIF to the if statement array.
current_array.append(cmd)
# for any other operation or element, add it to the if statement array.
else:
current_array.append(cmd)
# if items array is empty and we didn't exit the if statement, fail.
if not found:
return False
# get the top element from the stack
element = stack.pop()
# if top stack element is 0, if there's an OP_ELSE, OP_ELSE statements are executed, else,
# nothing from the if statements gets appended back to items array to be executed.
if decode_num(element) == 0:
cmds[:0] = if_cmds
# else, they are executed. All statements between the IF and the END_IF are executed,
# excluding statements from the OP_ELSE if there's an OP_ELSE.
else:
cmds[:0] = else_cmds
return True
# Marks transaction as invalid if top stack value is not true. The top stack value is removed.
def op_verify(stack):
if len(stack) < 1:
return False
element = stack.pop()
if decode_num(element) == 0:
return False
return True
# Marks transaction as invalid.
def op_return(stack):
return False
# Removes top element from stack and pushes it into the altstack.
def op_toaltstack(stack, altstack):
if len(stack) < 1:
return False
altstack.append(stack.pop())
return True
# Puts the input onto the top of the main stack. Removes it from the alt stack.
def op_fromaltstack(stack, altstack):
if len(stack) < 1:
return False
stack.append(altstack.pop())
return True
# Removes the top two stack items.
def op_2drop(stack):
if len(stack) < 2:
return False
stack.pop()
stack.pop()
return True
# Duplicates the top two stack items.
def op_2dup(stack):
if len(stack) < 2:
return False
elem1 = stack[-1]
elem2 = stack[-2]
stack.append(elem2)
stack.append(elem1)
return True
# Duplicates the top three stack items.
def op_3dup(stack):
if len(stack) < 3:
return False
stack.extend(stack[-3:])
return True
# Copies the pair of items two spaces back in the stack to the front.
def op_2over(stack):
if len(stack) < 4:
return False
stack.extend(stack[-4:-2])
return True
# The fifth and sixth items back are moved to the top of the stack.
def op_2rot(stack):
if len(stack) < 6:
return False
stack[-6:] = stack[-4:] + stack[-6:-4]
return True
# Swaps the top two pairs of items.
def op_2swap(stack):
if len(stack) < 4:
return False
stack[-4:] = stack[-2:] + stack[-4:-2]
return True
# If the top stack value is not 0, duplicate it.
def op_ifdup(stack):
if len(stack) < 1:
return False
item = stack[-1]
if decode_num(item) != 0:
stack.append(item)
return True
# Puts the number of stack items onto the stack.
def op_depth(stack):
encoded_length = encode_num(len(stack))
stack.append(encoded_length)
return True
# Removes the top stack item.
def op_drop(stack):
if len(stack) < 1:
return False
stack.pop()
return True
# Removes the second-to-top stack item.
def op_nip(stack):
if len(stack) < 2:
return False
stack.pop(-2)
return True
# Copies the second-to-top stack item to the top.
def op_over(stack):
if len(stack) < 2:
return False
stack.append(stack[-2])
return True
# The item n (starting from 0) back in the stack is copied to the top.
def op_pick(stack):
if len(stack) < 1:
return False
n = decode_num(stack.pop())
if len(stack) < n + 1:
return False
stack.append(stack[-n-1])
return True
# The item n back in the stack is moved to the top.
def op_roll(stack):
if len(stack) < 1:
return False
n = decode_num(stack.pop())
if len(stack) < n + 1:
return False
stack.append(stack.pop(-n-1))
# The top three items on the stack are rotated to the left.
def op_rot(stack):
if len(stack) < 3:
return False
stack.append(stack.pop(-3))
return True
# The top two items on the stack are swapped.
def op_swap(stack):
if len(stack) < 2:
return False
stack.append(stack.pop(-2))
return True
# The item at the top of the stack is copied and inserted before the second-to-top item.
def op_tuck(stack):
if len(stack) < 2:
return False
stack.insert(-2, stack[-1])
return True
# Pushes the string length of the top element of the stack (without popping it).
def op_size(stack):
if len(stack) < 1:
return False
stack.append(encode_num(len(stack[-1])))
return True
# Same as OP_EQUAL, but runs OP_VERIFY afterward.
def op_equalverify(stack):
return op_equal(stack) and op_verify(stack)
# 1 is added to the top element of the stack.
def op_1add(stack):
if len(stack) < 1:
return False
item = decode_num(stack.pop())
stack.append(encode_num(item + 1))
return True
# 1 is subtracted to the top element of the stack.
def op_1sub(stack):
if len(stack) < 1:
return False
item = decode_num(stack.pop())
stack.append(encode_num(item - 1))
return True
# The sign of the top element of the stack is flipped.
def op_negate(stack):
if len(stack) < 1:
return False
item = decode_num(stack.pop())
stack.append(encode_num(-item))
return True
# The top element of the stack is made positive.
def op_abs(stack):
if len(stack) < 1:
return False
item = decode_num(stack.pop())
stack.append(encode_num(abs(item)))
return True
# Top element is removed. If top element is 0, a 1 is pushed onto the stack,
# otherwise a 0 is pushed onto the stack.
def op_not(stack):
if len(stack) < 1:
return False
item = decode_num(stack.pop())
if item == 0:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
# Removes top element. If it is 0, a 0 is added onto the stack, otherwise a 1 is pushed onto the stack.
def op_0notequal(stack):
if len(stack) < 1:
return False
item = decode_num(stack.pop())
if item == 0:
stack.append(encode_num(0))
else:
stack.append(encode_num(1))
# top element is subtracted from second-to-top stack element. Both elements are consumed.
# result is pushed onto the stack.
def op_sub(stack):
if len(stack) < 2:
return False
elem1 = decode_num(stack.pop())
elem2 = decode_num(stack.pop())
result = elem2 - elem1
stack.append(encode_num(result))
return True
# consumes top 2 elements, if both are not 0, push 1 onto the stack. Otherwise push 0.
def op_booland(stack):
if len(stack) < 2:
return False
elem1 = decode_num(stack.pop())
elem2 = decode_num(stack.pop())
if elem1 != 0 and elem2 != 0:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
# consumes top 2 elements, if one of them is not 0, push 1 onto the stack. Otherwise push 0.
def op_boolor(stack):
if len(stack) < 2:
return False
elem1 = decode_num(stack.pop())
elem2 = decode_num(stack.pop())
if elem1 != 0 or elem2 != 0:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
# consumes top 2 elements, if they are equal, pushes a 1 onto the stack. Otherwise pushes a 0.
def op_numequal(stack):
if len(stack) < 2:
return False
elem1 = decode_num(stack.pop())
elem2 = decode_num(stack.pop())
if elem1 == elem2:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
def op_numequalverify(stack):
return op_numequal(stack) and op_verify(stack)
# consumes top 2 elements, if they are not equal, pushes a 1 onto the stack. Otherwise pushes a 0.
def op_numnotequal(stack):
if len(stack) < 2:
return False
elem1 = decode_num(stack.pop())
elem2 = decode_num(stack.pop())
if elem1 != elem2:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
# Consumes top 2 elements. Pushes a 1 onto the stack if second-to-top element
# is less than top element. 0 otherwise.
def op_lessthan(stack):
if len(stack) < 2:
return False
elem1 = decode_num(stack.pop())
elem2 = decode_num(stack.pop())
if elem2 < elem1:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
# Consumes top 2 elements. Pushes a 1 onto the stack if second-to-top element
# is greater than top element. 0 otherwise.
def op_greaterthan(stack):
if len(stack) < 2:
return False
elem1 = decode_num(stack.pop())
elem2 = decode_num(stack.pop())
if elem2 > elem1:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
# Consumes top 2 elements. Pushes a 1 onto the stack if second-to-top element
# is less than or equal to top element. 0 otherwise.
def op_lessthanorequal(stack):
if len(stack) < 2:
return False
elem1 = decode_num(stack.pop())
elem2 = decode_num(stack.pop())
if elem2 <= elem1:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
# Consumes top 2 elements. Pushes a 1 onto the stack if second-to-top element
# is greater than or equal to the top element. 0 otherwise.
def op_greaterthanorequal(stack):
if len(stack) < 2:
return False
elem1 = decode_num(stack.pop())
elem2 = decode_num(stack.pop())
if elem2 >= elem1:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
# Consumes top 2 elements. Pushes the smaller element between the two onto the stack.
def op_min(stack):
if len(stack) < 2:
return False
elem1 = decode_num(stack.pop())
elem2 = decode_num(stack.pop())
if elem2 <= elem1:
stack.append(encode_num(elem2))
else:
stack.append(encode_num(elem1))
return True
# Consumes top 2 elements. Pushes the larger element between the two onto the stack.
def op_max(stack):
if len(stack) < 2:
return False
elem1 = decode_num(stack.pop())
elem2 = decode_num(stack.pop())
if elem2 >= elem1:
stack.append(encode_num(elem2))
else:
stack.append(encode_num(elem1))
return True
# Consumes top 3 elements. If third to top element is between second to top (min, inclusive)
# and top elements (max, not inclusive) pushes 1 onto the stack. 0 otherwise.
def op_within(stack):
if len(stack) < 3:
return False
max_elem = decode_num(stack.pop())
min_elem = decode_num(stack.pop())
elem = decode_num(stack.pop())
if elem >= min_elem and elem < max_elem:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
# Consumes top element of the stack, performs a ripemd160 operation on it and pushes the hashed element
# into the stack.
def op_ripemd160(stack):
if len(stack) < 1:
return False
elem = stack.pop()
stack.append(hashlib.new('ripemd160', elem).digest())
return True
# Consumes top element of the stack, performs a sha1 operation on it and pushes the hashed element
# into the stack.
def op_sha1(stack):
if len(stack) < 1:
return False
elem = stack.pop()
stack.append(hashlib.sha1(elem).digest())
return True
# Consumes top element of the stack, performs a sha256 operation on it and pushes the hashed element
# into the stack.
def op_sha256(stack):
if len(stack) < 1:
return False
elem = stack.pop()
# hashlib.new method receives as its parameter a bytes-like object and returns a SHA256 Hash Object.
# digest() converts the SHA256 object into bytes format.
stack.append(hashlib.sha256(elem).digest())
return True
# Same as OP_CHECKSIG, but OP_VERIFY is executed afterward.
def op_checksigverify(stack, z):
return op_checksig(stack, z) and op_verify(stack)
# consumes 2 stack elements (pubkey and signature) and determines if they are valid for this transaction.
# OP_CHECKSIG will push a 1 to the stack if they are valid. 0 otherwise - page 112
def op_checksig(stack, z):
# if stack is has less than 2 elements, fail.
if len(stack) < 2:
return False
# sec_pubkey is the top element of stack.
sec_pubkey = stack.pop()
# take off the last byte of the signature as that's the hash_type.
# Signature format is [<DER signature> <1 byte hash-type>]. Hashtype value is last byte of the sig.
der_signature = stack.pop()[:-1]
try:
point = S256Point.parse(sec_pubkey)
sig = Signature.parse(der_signature)
except (ValueError, SyntaxError) as e:
LOGGER.info(e)
return False
valid = point.verify(z, sig)
# push a 1 if it's valid, 0 otherwise.
if valid:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
# If all signatures are valid, 1 is returned, 0 otherwise.
# Due to a bug, one extra unused value is removed from the stack - page 148.
# REWRITE: one sig can only sign one pubkey. Then that pubkey should be removed from consideration.
def op_checkmultisig(stack, z):
if len(stack) < 1:
return False
# n is the number of public keys
n = decode_num(stack.pop())
if len(stack) < n + 1:
return False
# get all the public keys into a list.
pubkeys = []
for _ in range(n):
pubkeys.append(stack.pop())
if len(stack) < 1:
return False
# m is the number of signatures
m = decode_num(stack.pop())
# m + 2 because of the additional element at the bottom of the stack that is added.
if len(stack) < m + 1:
return False
# get all the signatures.
signatures = []
for _ in range(m):
# take off last byte, which is the hashtype
signatures.append(stack.pop()[:-1])
if len(stack) < 1:
return False
# we remove the last element from the stack (the one included because the off by one error)
stack.pop()
# verify all the signatures against all pubkeys. If a signature isn't valid for any pubkeys, fail.
try:
sigs = [Signature.parse(signature) for signature in signatures]
points = [S256Point.parse(pubkey) for pubkey in pubkeys]
except (ValueError, SyntaxError) as e:
LOGGER.info(e)
return False
# variable to count the number of valid signatures.
count = 0
# in the next loop, we check that each signature is valid for a pubkey.
while len(points) > 0:
# point is popped so each signature can only be valid for 1 point.
point = points.pop()
for sig in sigs:
# if the signature is valid for this pubkey, increase the count and break from the while
# to check next signature.
if point.verify(z, sig):
count += 1
# if the number of valid signatures is m = each signature is valid for some pubkey, then script is valid.
if count == m:
stack.append(encode_num(1))
# else, script should fail.
else:
stack.append(encode_num(0))
return True
# Same as OP_CHECKMULTISIG, but OP_VERIFY is executed afterward.
def op_checkmultisigverify(stack, z):
return op_checkmultisig(stack, z) and op_verify(stack)
class TestOp(TestCase):
def test_op_2over(self):
stack = [1, 2, 3, 4]
op_2over(stack)
self.assertEqual(stack, [1, 2, 3, 4, 1, 2])
def test_op_2rot(self):
stack = [1, 2, 3, 4, 5, 6]
op_2rot(stack)
self.assertEqual(stack, [3, 4, 5, 6, 1, 2])
def test_op_2swap(self):
stack = [1, 2, 3, 4]
op_2swap(stack)
self.assertEqual(stack, [3, 4, 1, 2])
def test_op_depth(self):
stack = [1, 2, 3, 4]
op_depth(stack)
self.assertEqual(stack, [1, 2, 3, 4, encode_num(4)])
def test_op_drop(self):
stack = [1, 2]
op_drop(stack)
self.assertEqual(stack, [1])
def test_op_nip(self):
stack = [1, 2, 3]
op_nip(stack)
self.assertEqual(stack, [1, 3])
def test_op_over(self):
stack = [1, 2, 3]
op_over(stack)
self.assertEqual(stack, [1, 2, 3, 2])
def test_op_pick(self):
stack = [1, 2, 3, 4, encode_num(3)]
op_pick(stack)
self.assertEqual(stack, [1, 2, 3, 4, 1])
def test_op_roll(self):
stack = [1, 2, 3, 4, encode_num(3)]
op_roll(stack)
self.assertEqual(stack, [2, 3, 4, 1])
def test_op_rot(self):
stack = [1, 2, 3]
op_rot(stack)
self.assertEqual(stack, [2, 3, 1])
def test_op_swap(self):
stack = [1, 2, 3]
op_swap(stack)
self.assertEqual(stack, [1, 3, 2])
def test_op_tuck(self):
stack = [1, 2]
op_tuck(stack)
self.assertEqual(stack, [2, 1, 2])
# def test_op_size(self):
# stack = [encode_num(16)]
# op_size(stack)
# self.assertEqual(stack, [encode_num(16), encode_num(2)])
def test_op_equalverify(self):
stack = [1, 1]
self.assertEqual(op_equalverify(stack), True)
def test_op_1add(self):
stack = [encode_num(10)]
op_1add(stack)
self.assertEqual(stack, [encode_num(11)])
def test_op_1sub(self):
stack = [encode_num(10)]
op_1sub(stack)
self.assertEqual(stack, [encode_num(9)])
def test_op_negate(self):
stack = [encode_num(-10)]
op_negate(stack)
self.assertEqual(stack, [encode_num(10)])
stack = [encode_num(10)]
op_negate(stack)
self.assertEqual(stack, [encode_num(-10)])
def test_op_abs(self):
stack = [encode_num(-10)]
op_abs(stack)
self.assertEqual(stack, [encode_num(10)])
stack = [encode_num(10)]
op_abs(stack)
self.assertEqual(stack, [encode_num(10)])
def test_op_not(self):
stack = [encode_num(0)]
op_not(stack)
self.assertEqual(stack, [encode_num(1)])
stack = [encode_num(2)]
op_not(stack)
self.assertEqual(stack, [encode_num(0)])
def test_op_0notequal(self):
stack = [encode_num(0)]
op_0notequal(stack)
self.assertEqual(stack, [encode_num(0)])
stack = [encode_num(10)]
op_0notequal(stack)
self.assertEqual(stack, [encode_num(1)])
def test_op_sub(self):
stack = [encode_num(10), encode_num(4)]
op_sub(stack)
self.assertEqual(stack, [encode_num(6)])
def test_op_booland(self):
stack = [encode_num(1), encode_num(25)]
op_booland(stack)
self.assertEqual(stack, [encode_num(1)])
stack = [encode_num(0), encode_num(25)]
op_booland(stack)
self.assertEqual(stack, [encode_num(0)])
def test_op_boolor(self):
stack = [encode_num(1), encode_num(25)]
op_boolor(stack)
self.assertEqual(stack, [encode_num(1)])
stack = [encode_num(0), encode_num(25)]
op_boolor(stack)
self.assertEqual(stack, [encode_num(1)])
stack = [encode_num(0), encode_num(0)]
op_boolor(stack)
self.assertEqual(stack, [encode_num(0)])
def test_op_numequal(self):
stack = [encode_num(10), encode_num(10)]
op_numequal(stack)
self.assertEqual(stack, [encode_num(1)])
stack = [encode_num(9), encode_num(10)]
op_numequal(stack)
self.assertEqual(stack, [encode_num(0)])
def test_op_numnotequal(self):
stack = [encode_num(10), encode_num(10)]
op_numnotequal(stack)
self.assertEqual(stack, [encode_num(0)])
stack = [encode_num(9), encode_num(10)]
op_numnotequal(stack)
self.assertEqual(stack, [encode_num(1)])
def test_op_lessthan(self):
stack = [encode_num(1), encode_num(2)]
op_lessthan(stack)
self.assertEqual(stack, [encode_num(1)])
stack = [encode_num(2), encode_num(2)]
op_lessthan(stack)
self.assertEqual(stack, [encode_num(0)])
stack = [encode_num(10), encode_num(2)]
op_lessthan(stack)
self.assertEqual(stack, [encode_num(0)])
def test_op_greaterthan(self):
stack = [encode_num(1), encode_num(2)]
op_greaterthan(stack)
self.assertEqual(stack, [encode_num(0)])
stack = [encode_num(2), encode_num(2)]
op_greaterthan(stack)
self.assertEqual(stack, [encode_num(0)])