forked from kokjo/pycoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
73 lines (65 loc) · 1.72 KB
/
script.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
class ScriptError(Exception):
def __init__(self, err):
self.err = err
def __repr__(self):
return "ScriptError(%s)" % self.err
class Stack:
def __init__(self, initstack=[], maxlen = 10):
self._stack = initstack
self._maxlength = 10
def push(self):
opcodes = {}
def _add_opcode(opcode):
opcodes[opcode.num] = opcode
class Opcode:
type = "Unknown"
num = None
def eval(self, stack):
pass
def __repr__(self):
return type
@staticmethod
def decode(data):
return Opcode(),data
def encode(self):
return chr(num)
class OP_0(Opcode):
type = "OP_0"
num = 0
def eval(self, stack):
stack.push(0)
@staticmethod
def decode(data):
return OP_0(), data[1:]
_add_opcode(OP_0)
class OP_PUSH(Opcode):
type = "OP_PUSH"
def __init__(self, value):
self.type = value.encode("hex")
self.value = value
def eval(self, stack):
stack.push(self.value)
def encode(self):
if not self.value:
return ""
if len(self.value) < 76:
return chr(len(self.value))+self.value
elif len(self.value) < 255:
return chr(76)+chr(len(self.value))+self.value
for i in range(1,76):
class _OP_PUSH(Opcode):
type = "OP_PUSH"
num = i
@staticmethod
def decode(data):
l = ord(data[0])
return OP_PUSH(data[1:l+1]),data[l+1:]
_add_opcode(_OP_PUSH)
class Script:
def __init__(self, data=None):
self.opcodes = []
self.decode(data)
def eval(self, stack):
for op in self.opcodes:
op.eval(stack)
def decode(self, data):