This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
/
scripteval.py
612 lines (484 loc) · 11.9 KB
/
scripteval.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
#
# scripteval.py
#
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#
from serialize import Hash, Hash160, ser_uint256, ser_uint160
from Crypto.Hash import SHA256, RIPEMD160
from script import *
from core import CTxOut, CTransaction
from key import CKey
from bignum import bn2vch, vch2bn
def SignatureHash(script, txTo, inIdx, hashtype):
if inIdx >= len(txTo.vin):
return (0L, "inIdx %d out of range (%d)" % (inIdx, len(txTo.vin)))
txtmp = CTransaction()
txtmp.copy(txTo)
for txin in txtmp.vin:
txin.scriptSig = ''
txtmp.vin[inIdx].scriptSig = script.vch
if (hashtype & 0x1f) == SIGHASH_NONE:
txtmp.vout = []
for i in xrange(len(txtmp.vin)):
if i != inIdx:
txtmp.vin[i].nSequence = 0
elif (hashtype & 0x1f) == SIGHASH_SINGLE:
outIdx = inIdx
if outIdx >= len(txtmp.vout):
return (0L, "outIdx %d out of range (%d)" % (outIdx, len(txtmp.vout)))
tmp = txtmp.vout[outIdx]
txtmp.vout = []
for i in xrange(outIdx):
txtmp.vout.append(CTxOut())
txtmp.vout.append(tmp)
for i in xrange(len(txtmp.vin)):
if i != inIdx:
txtmp.vin[i].nSequence = 0
if hashtype & SIGHASH_ANYONECANPAY:
tmp = txtmp.vin[inIdx]
txtmp.vin = []
txtmp.vin.append(tmp)
s = txtmp.serialize()
s += struct.pack("<I", hashtype)
hash = Hash(s)
return (hash,)
def CheckSig(sig, pubkey, script, txTo, inIdx, hashtype):
key = CKey()
key.set_pubkey(pubkey)
if len(sig) == 0:
return False
if hashtype == 0:
hashtype = ord(sig[-1])
elif hashtype != ord(sig[-1]):
return False
sig = sig[:-1]
tup = SignatureHash(script, txTo, inIdx, hashtype)
if tup[0] == 0L:
return False
return key.verify(ser_uint256(tup[0]), sig)
def CheckMultiSig(opcode, script, stack, txTo, inIdx, hashtype):
i = 1
if len(stack) < i:
return False
keys_count = CastToBigNum(stack[-i])
if keys_count < 0 or keys_count > 20:
return False
i += 1
ikey = i
i += keys_count
if len(stack) < i:
return False
sigs_count = CastToBigNum(stack[-i])
if sigs_count < 0 or sigs_count > keys_count:
return False
i += 1
isig = i
i += sigs_count
if len(stack) < i:
return False
for k in xrange(sigs_count):
sig = stack[-isig-k]
# FIXME: find-and-delete sig in script
success = True
while success and sigs_count > 0:
sig = stack[-isig]
pubkey = stack[-ikey]
if CheckSig(sig, pubkey, script, txTo, inIdx, hashtype):
isig += 1
sigs_count -= 1
ikey += 1
keys_count -= 1
if sigs_count > keys_count:
success = False
while i > 0:
stack.pop()
i -= 1
if success:
stack.append("\x01")
else:
stack.append("\x00")
if opcode == OP_CHECKMULTISIGVERIFY:
if success:
stack.pop()
else:
return False
return True
def dumpstack(msg, stack):
print "%s stacksz %d" % (msg, len(stack))
for i in xrange(len(stack)):
vch = stack[i]
print "#%d: %s" % (i, vch.encode('hex'))
ISA_UNOP = {
OP_1ADD: True,
OP_1SUB: True,
OP_2MUL: True,
OP_2DIV: True,
OP_NEGATE: True,
OP_ABS: True,
OP_NOT: True,
OP_0NOTEQUAL: True,
}
def UnaryOp(opcode, stack):
if len(stack) < 1:
return False
bn = CastToBigNum(stack.pop())
if opcode == OP_1ADD:
bn += 1
elif opcode == OP_1SUB:
bn -= 1
elif opcode == OP_2MUL:
bn <<= 1
elif opcode == OP_2DIV:
bn >>= 1
elif opcode == OP_NEGATE:
bn = -bn
elif opcode == OP_ABS:
if bn < 0:
bn = -bn
elif opcode == OP_NOT:
bn = long(bn == 0L)
elif opcode == OP_0NOTEQUAL:
bn = long(bn != 0L)
else:
return False
stack.append(bn2vch(bn))
return True
ISA_BINOP = {
OP_ADD: True,
OP_SUB: True,
OP_LSHIFT: True,
OP_RSHIFT: True,
OP_BOOLAND: True,
OP_BOOLOR: True,
OP_NUMEQUAL: True,
OP_NUMEQUALVERIFY: True,
OP_NUMNOTEQUAL: True,
OP_LESSTHAN: True,
OP_GREATERTHAN: True,
OP_LESSTHANOREQUAL: True,
OP_GREATERTHANOREQUAL: True,
OP_MIN: True,
OP_MAX: True,
}
def BinOp(opcode, stack):
if len(stack) < 2:
return False
bn2 = CastToBigNum(stack.pop())
bn1 = CastToBigNum(stack.pop())
if opcode == OP_ADD:
bn = bn1 + bn2
elif opcode == OP_SUB:
bn = bn1 - bn2
elif opcode == OP_LSHIFT:
if bn2 < 0 or bn2 > 2048:
return False
bn = bn1 << bn2
elif opcode == OP_RSHIFT:
if bn2 < 0 or bn2 > 2048:
return False
bn = bn1 >> bn2
elif opcode == OP_BOOLAND:
bn = long(bn1 != 0L and bn2 != 0L)
elif opcode == OP_BOOLOR:
bn = long(bn1 != 0L or bn2 != 0L)
elif opcode == OP_NUMEQUAL or opcode == OP_NUMEQUALVERIFY:
bn = long(bn1 == bn2)
elif opcode == OP_NUMNOTEQUAL:
bn = long(bn1 != bn2)
elif opcode == OP_LESSTHAN:
bn = long(bn1 < bn2)
elif opcode == OP_GREATERTHAN:
bn = long(bn1 > bn2)
elif opcode == OP_LESSTHANOREQUAL:
bn = long(bn1 <= bn2)
elif opcode == OP_GREATERTHANOREQUAL:
bn = long(bn1 >= bn2)
elif opcode == OP_MIN:
if bn1 < bn2:
bn = bn1
else:
bn = bn2
elif opcode == OP_MAX:
if bn1 > bn2:
bn = bn1
else:
bn = bn2
else:
return False # unknown binop opcode
stack.append(bn2vch(bn))
if opcode == OP_NUMEQUALVERIFY:
if CastToBool(stack[-1]):
stack.pop()
else:
return False
return True
def CheckExec(vfExec):
for b in vfExec:
if not b:
return False
return True
def EvalScript(stack, scriptIn, txTo, inIdx, hashtype):
altstack = []
vfExec = []
script = CScript(scriptIn)
while script.pc < script.pend:
if not script.getop():
return False
sop = script.sop
fExec = CheckExec(vfExec)
if fExec and sop.op <= OP_PUSHDATA4:
stack.append(sop.data)
continue
elif fExec and sop.op == OP_1NEGATE or ((sop.op >= OP_1) and (sop.op <= OP_16)):
v = sop.op - (OP_1 - 1)
stack.append(bn2vch(v))
elif fExec and sop.op in ISA_BINOP:
if not BinOp(sop.op, stack):
return False
elif fExec and sop.op in ISA_UNOP:
if not UnaryOp(sop.op, stack):
return False
elif fExec and sop.op == OP_2DROP:
if len(stack) < 2:
return False
stack.pop()
stack.pop()
elif fExec and sop.op == OP_2DUP:
if len(stack) < 2:
return False
v1 = stack[-2]
v2 = stack[-1]
stack.append(v1)
stack.append(v2)
elif fExec and sop.op == OP_2OVER:
if len(stack) < 4:
return False
v1 = stack[-4]
v2 = stack[-3]
stack.append(v1)
stack.append(v2)
elif fExec and sop.op == OP_2SWAP:
if len(stack) < 4:
return False
tmp = stack[-4]
stack[-4] = stack[-2]
stack[-2] = tmp
tmp = stack[-3]
stack[-3] = stack[-1]
stack[-1] = tmp
elif fExec and sop.op == OP_3DUP:
if len(stack) < 3:
return False
v1 = stack[-3]
v2 = stack[-2]
v3 = stack[-1]
stack.append(v1)
stack.append(v2)
stack.append(v3)
elif fExec and sop.op == OP_CHECKMULTISIG or sop.op == OP_CHECKMULTISIGVERIFY:
tmpScript = CScript(script.vch[script.pbegincodehash:script.pend])
ok = CheckMultiSig(sop.op, tmpScript, stack, txTo,
inIdx, hashtype)
if not ok:
return False
elif fExec and sop.op == OP_CHECKSIG or sop.op == OP_CHECKSIGVERIFY:
if len(stack) < 2:
return False
vchPubKey = stack.pop()
vchSig = stack.pop()
tmpScript = CScript(script.vch[script.pbegincodehash:script.pend])
# FIXME: find-and-delete vchSig
ok = CheckSig(vchSig, vchPubKey, tmpScript,
txTo, inIdx, hashtype)
if ok:
if sop.op != OP_CHECKSIGVERIFY:
stack.append("\x01")
else:
if sop.op == OP_CHECKSIGVERIFY:
return False
stack.append("\x00")
elif fExec and sop.op == OP_CODESEPARATOR:
script.pbegincodehash = script.pc
elif fExec and sop.op == OP_DEPTH:
bn = len(stack)
stack.append(bn2vch(bn))
elif fExec and sop.op == OP_DROP:
if len(stack) < 1:
return False
stack.pop()
elif fExec and sop.op == OP_DUP:
if len(stack) < 1:
return False
v = stack[-1]
stack.append(v)
elif sop.op == OP_ELSE:
if len(vfExec) == 0:
return false
vfExec[-1] = not vfExec[-1]
elif sop.op == OP_ENDIF:
if len(vfExec) == 0:
return false
vfExec.pop()
elif fExec and sop.op == OP_EQUAL or sop.op == OP_EQUALVERIFY:
if len(stack) < 2:
return False
v1 = stack.pop()
v2 = stack.pop()
is_equal = (v1 == v2)
if is_equal:
stack.append("\x01")
else:
stack.append("\x00")
if sop.op == OP_EQUALVERIFY:
if is_equal:
stack.pop()
else:
return False
elif fExec and sop.op == OP_FROMALTSTACK:
if len(altstack) < 1:
return False
v = altstack.pop()
stack.append(v)
elif fExec and sop.op == OP_HASH160:
if len(stack) < 1:
return False
stack.append(ser_uint160(Hash160(stack.pop())))
elif fExec and sop.op == OP_HASH256:
if len(stack) < 1:
return False
stack.append(ser_uint256(Hash(stack.pop())))
elif sop.op == OP_IF or sop.op == OP_NOTIF:
val = False
if fExec:
if len(stack) < 1:
return False
vch = stack.pop()
val = CastToBool(vch)
if sop.op == OP_NOTIF:
val = not val
vfExec.append(val)
elif fExec and sop.op == OP_IFDUP:
if len(stack) < 1:
return False
vch = stack[-1]
if CastToBool(vch):
stack.append(vch)
elif fExec and sop.op == OP_NIP:
if len(stack) < 2:
return False
del stack[-2]
elif fExec and sop.op == OP_NOP or (sop.op >= OP_NOP1 and sop.op <= OP_NOP10):
pass
elif fExec and sop.op == OP_OVER:
if len(stack) < 2:
return False
vch = stack[-2]
stack.append(vch)
elif fExec and sop.op == OP_PICK or sop.op == OP_ROLL:
if len(stack) < 2:
return False
n = CastToBigNum(stack.pop())
if n < 0 or n >= len(stack):
return False
vch = stack[-n-1]
if sop.op == OP_ROLL:
del stack[-n-1]
stack.append(vch)
elif fExec and sop.op == OP_RETURN:
return False
elif fExec and sop.op == OP_RIPEMD160:
if len(stack) < 1:
return False
stack.append(RIPEMD160.new(stack.pop()).digest())
elif fExec and sop.op == OP_ROT:
if len(stack) < 3:
return False
tmp = stack[-3]
stack[-3] = stack[-2]
stack[-2] = tmp
tmp = stack[-2]
stack[-2] = stack[-1]
stack[-1] = tmp
elif fExec and sop.op == OP_SIZE:
if len(stack) < 1:
return False
bn = len(stack[-1])
stack.append(bn2vch(bn))
elif fExec and sop.op == OP_SHA256:
if len(stack) < 1:
return False
stack.append(SHA256.new(stack.pop()).digest())
elif fExec and sop.op == OP_SWAP:
if len(stack) < 2:
return False
tmp = stack[-2]
stack[-2] = stack[-1]
stack[-1] = tmp
elif fExec and sop.op == OP_TOALTSTACK:
if len(stack) < 1:
return False
v = stack.pop()
altstack.append(v)
elif fExec and sop.op == OP_TUCK:
if len(stack) < 2:
return False
vch = stack[-1]
stack.insert(len(stack) - 2, vch)
elif fExec and sop.op == OP_VERIFY:
if len(stack) < 1:
return False
v = CastToBool(stack[-1])
if v:
stack.pop()
else:
return False
elif fExec and sop.op == OP_WITHIN:
if len(stack) < 3:
return False
bn3 = CastToBigNum(stack.pop())
bn2 = CastToBigNum(stack.pop())
bn1 = CastToBigNum(stack.pop())
v = (bn2 <= bn1) and (bn1 < bn3)
if v:
stack.append("\x01")
else:
stack.append("\x00")
elif fExec:
print "Unsupported opcode", OPCODE_NAMES[sop.op]
return False
return True
def CastToBigNum(s):
v = vch2bn(s)
return v
def CastToBool(s):
for i in xrange(len(s)):
sv = ord(s[i])
if sv != 0:
if (i == (len(s) - 1)) and (sv == 0x80):
return False
return True
return False
def VerifyScript(scriptSig, scriptPubKey, txTo, inIdx, hashtype):
stack = []
if not EvalScript(stack, scriptSig, txTo, inIdx, hashtype):
return False
if not EvalScript(stack, scriptPubKey, txTo, inIdx, hashtype):
return False
if len(stack) == 0:
return False
return CastToBool(stack[-1])
def VerifySignature(txFrom, txTo, inIdx, hashtype):
if inIdx >= len(txTo.vin):
return False
txin = txTo.vin[inIdx]
if txin.prevout.n >= len(txFrom.vout):
return False
txout = txFrom.vout[txin.prevout.n]
txFrom.calc_sha256()
if txin.prevout.hash != txFrom.sha256:
return False
if not VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, inIdx,
hashtype):
return False
return True