-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transactions.py
172 lines (145 loc) · 4.39 KB
/
Transactions.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
#Transaction.py
import Signatures
#Signatures.sign
#Signatures.verify
class Tx:
inputs = None
outputs =None
sigs = None
reqd = None
def __init__(self):
self.inputs = []
self.outputs = []
self.sigs = []
self.reqd = []
def add_input(self, from_addr, amount):
self.inputs.append((from_addr, amount))
def add_output(self, to_addr, amount):
self.outputs.append((to_addr, amount))
def add_reqd(self, addr):
self.reqd.append(addr)
def sign(self, private):
message = self.__gather()
newsig = Signatures.sign(message, private)
self.sigs.append(newsig)
def is_valid(self):
total_in = 0
total_out = 0
message = self.__gather()
for addr,amount in self.inputs:
found = False
for s in self.sigs:
if Signatures.verify(message, s, addr) :
found = True
if not found:
#print ("No good sig found for " + str(message))
return False
if amount < 0:
return False
total_in = total_in + amount
for addr in self.reqd:
found = False
for s in self.sigs:
if Signatures.verify(message, s, addr) :
found = True
if not found:
return False
for addr,amount in self.outputs:
if amount < 0:
return False
total_out = total_out + amount
#if total_out > total_in:
#print("Outputs exceed inputs")
# return False
return True
def __gather(self):
data=[]
data.append(self.inputs)
data.append(self.outputs)
data.append(self.reqd)
return data
def __repr__(self):
reprstr = "INPUTS:\n"
for addr, amt in self.inputs:
reprstr = reprstr + str(amt) + " from " + str(addr) + "\n"
reprstr = reprstr + "OUTPUTS:\n"
for addr, amt in self.outputs:
reprstr = reprstr + str(amt) + " to " + str(addr) + "\n"
reprstr = reprstr + "REQD:\n"
for r in self.reqd:
reprstr = reprstr + str(r) + "\n"
reprstr = reprstr + "SIGS:\n"
for s in self.sigs:
reprstr = reprstr + str(s) + "\n"
reprstr = reprstr + "END\n"
return reprstr
if __name__ == "__main__":
pr1, pu1 = Signatures.generate_keys()
pr2, pu2 = Signatures.generate_keys()
pr3, pu3 = Signatures.generate_keys()
pr4, pu4 = Signatures.generate_keys()
Tx1 = Tx()
Tx1.add_input(pu1, 1)
Tx1.add_output(pu2, 1)
Tx1.sign(pr1)
if Tx1.is_valid():
print("Success! Tx is valid")
else:
print("ERROR! Tx is invalid")
Tx2 = Tx()
Tx2.add_input(pu1, 2)
Tx2.add_output(pu2, 1)
Tx2.add_output(pu3, 1)
Tx2.sign(pr1)
Tx3 = Tx()
Tx3.add_input(pu3, 1.2)
Tx3.add_output(pu1, 1.1)
Tx3.add_reqd(pu4)
Tx3.sign(pr3)
Tx3.sign(pr4)
for t in [Tx1, Tx2, Tx3]:
if t.is_valid():
print("Success! Tx is valid")
else:
print("ERROR! Tx is invalid")
# Wrong signatures
Tx4 = Tx()
Tx4.add_input(pu1, 1)
Tx4.add_output(pu2, 1)
Tx4.sign(pr2)
# Escrow Tx not signed by the arbiter
Tx5 = Tx()
Tx5.add_input(pu3, 1.2)
Tx5.add_output(pu1, 1.1)
Tx5.add_reqd(pu4)
Tx5.sign(pr3)
# Two input addrs, signed by one
Tx6 = Tx()
Tx6.add_input(pu3, 1)
Tx6.add_input(pu4, 0.1)
Tx6.add_output(pu1, 1.1)
Tx6.sign(pr3)
# Outputs exceed inputs
Tx7 = Tx()
Tx7.add_input(pu4, 1.2)
Tx7.add_output(pu1, 1)
Tx7.add_output(pu2, 2)
Tx7.sign(pr4)
# Negative values
Tx8 = Tx()
Tx8.add_input(pu2, -1)
Tx8.add_output(pu1, -1)
Tx8.sign(pr2)
# Modified Tx
Tx9 = Tx()
Tx9.add_input(pu1, 1)
Tx9.add_output(pu2, 1)
Tx9.sign(pr1)
# outputs = [(pu2,1)]
# change to [(pu3,1)]
Tx9.outputs[0] = (pu3,1)
for t in [Tx4, Tx5, Tx6, Tx7, Tx8, Tx9]:
if t.is_valid():
print("ERROR! Bad Tx is valid")
else:
print("Success! Bad Tx is invalid")