-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathop.py
250 lines (216 loc) · 5.04 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
"""
Operation Codes (Opcodes)
"""
from typing import List
from helper import hash256, hash160, encode_num, decode_num
from ecc import S256Point, Signature
def op_dup(stack) -> bool:
"""
Duplicates an element on the stack
"""
if len(stack) < 1:
return False
stack.append(stack[-1])
return True
def op_hash256(stack) -> bool:
"""
Performs two rounds of sha256
"""
if len(stack) < 1:
return False
element = stack.pop()
stack.append(hash256(element))
return True
def op_hash160(stack) -> bool:
"""
Performs SHA256 followed by RIPEMD160
"""
if len(stack) < 1:
return False
element = stack.pop()
stack.append(hash160(element))
return True
def op_0(stack):
"""
Append 0, i.e. b'', to the stack
"""
stack.append(encode_num(0))
return True
def op_checksig(stack, z) -> bool:
"""
Operation Check Signature
"""
if len(stack) < 2:
return False
sec_pubkey = stack.pop()
der_signature = stack.pop()[:-1]
try:
point = S256Point.parse(sec_pubkey)
sig = Signature.parse(der_signature)
except (ValueError, SyntaxError) as e:
return False
if point.verify(z, sig):
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
def op_checkmultisig(stack, z) -> bool:
"""
Operation check multisig: consumes m + n + 3 elements
Args:
stack:
z (): signature hash/ digital fingerprint of a transaction input
"""
if len(stack) < 1:
return False
n = decode_num(stack.pop())
if len(stack) < n + 1:
return False
sec_pubkeys = []
for _ in range(n):
sec_pubkeys.append(stack.pop())
m = decode_num(stack.pop())
if len(stack) < m + 1:
return False
der_signatures = []
for _ in range(m):
der_signatures.append(stack.pop()[:-1])
stack.pop()
try:
points = [S256Point.parse(sec) for sec in sec_pubkeys]
sigs = [Signature.parse(der) for der in der_signatures]
for sig in sigs:
if len(points) == 0:
return False
while points:
point = points.pop(0)
if point.verify(z, sig):
break
stack.append(encode_num(1))
except (ValueError, SyntaxError):
return False
return True
# TODO: implement op_equal
def op_equal(stack: List):
"""
Checks if two stack elements are equal
"""
if len(stack) < 2:
return False
element1 = stack.pop()
element2 = stack.pop()
if element1 == element2:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
# TODO: implement op_verify
def op_verify(stack):
"""
Verifies that the stack is neither empty nor is the value of
element left on the stack equal to 0
"""
if len(stack) < 1:
return False
element = stack.pop()
if decode_num(element) == 0:
return False
return True
OP_CODE_FUNCTIONS = {
118: op_dup, # 0x76
170: op_hash256,
169: op_hash160, #0xa9
}
OP_CODE_NAMES = {
0: 'OP_0',
76: 'OP_PUSHDATA1',
77: 'OP_PUSHDATA2',
78: 'OP_PUSHDATA4',
79: 'OP_1NEGATE',
81: 'OP_1',
82: 'OP_2',
83: 'OP_3',
84: 'OP_4',
85: 'OP_5',
86: 'OP_6',
87: 'OP_7',
88: 'OP_8',
89: 'OP_9',
90: 'OP_10',
91: 'OP_11',
92: 'OP_12',
93: 'OP_13',
94: 'OP_14',
95: 'OP_15',
96: 'OP_16',
97: 'OP_NOP',
99: 'OP_IF',
100: 'OP_NOTIF',
103: 'OP_ELSE',
104: 'OP_ENDIF',
105: 'OP_VERIFY',
106: 'OP_RETURN',
107: 'OP_TOALTSTACK',
108: 'OP_FROMALTSTACK',
109: 'OP_2DROP',
110: 'OP_2DUP',
111: 'OP_3DUP',
112: 'OP_2OVER',
113: 'OP_2ROT',
114: 'OP_2SWAP',
115: 'OP_IFDUP',
116: 'OP_DEPTH',
117: 'OP_DROP',
118: 'OP_DUP',
119: 'OP_NIP',
120: 'OP_OVER',
121: 'OP_PICK',
122: 'OP_ROLL',
123: 'OP_ROT',
124: 'OP_SWAP',
125: 'OP_TUCK',
130: 'OP_SIZE',
135: 'OP_EQUAL',
136: 'OP_EQUALVERIFY',
139: 'OP_1ADD',
140: 'OP_1SUB',
143: 'OP_NEGATE',
144: 'OP_ABS',
145: 'OP_NOT',
146: 'OP_0NOTEQUAL',
147: 'OP_ADD',
148: 'OP_SUB',
149: 'OP_MUL',
154: 'OP_BOOLAND',
155: 'OP_BOOLOR',
156: 'OP_NUMEQUAL',
157: 'OP_NUMEQUALVERIFY',
158: 'OP_NUMNOTEQUAL',
159: 'OP_LESSTHAN',
160: 'OP_GREATERTHAN',
161: 'OP_LESSTHANOREQUAL',
162: 'OP_GREATERTHANOREQUAL',
163: 'OP_MIN',
164: 'OP_MAX',
165: 'OP_WITHIN',
166: 'OP_RIPEMD160',
167: 'OP_SHA1',
168: 'OP_SHA256',
169: 'OP_HASH160',
170: 'OP_HASH256',
171: 'OP_CODESEPARATOR',
172: 'OP_CHECKSIG',
173: 'OP_CHECKSIGVERIFY',
174: 'OP_CHECKMULTISIG',
175: 'OP_CHECKMULTISIGVERIFY',
176: 'OP_NOP1',
177: 'OP_CHECKLOCKTIMEVERIFY',
178: 'OP_CHECKSEQUENCEVERIFY',
179: 'OP_NOP4',
180: 'OP_NOP5',
181: 'OP_NOP6',
182: 'OP_NOP7',
183: 'OP_NOP8',
184: 'OP_NOP9',
185: 'OP_NOP10',
}