-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.py
162 lines (125 loc) · 5.78 KB
/
transaction.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
from time import time
from flask import Flask, jsonify, request
import requests
import json
import threading
from Crypto.Hash import SHA384
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
import base64
import copy
import data
import utilities
class transaction:
def __init__(self,sender, recipient, amount,timestamp, inputs,outputs, id=None, signature=None):
self.sender=sender
self.recipient=recipient
self.amount=amount
self.timestamp=timestamp
self.inputs=inputs
self.outputs=outputs
self.id=id
self.signature=signature
def calculateId(self):
tempDict={
"sender":self.sender,
"recipient":self.recipient,
"timestamp":self.timestamp
}
asString=json.dumps(tempDict,sort_keys=True)
hash = utilities.hashStringToString(asString)
return hash
def asDictionary(self):
tempDict={
"sender":self.sender,
"recipient":self.recipient,
"amount":self.amount,
"timestamp":self.timestamp,
"inputs":self.inputs,
"outputs":self.outputs,
"id":self.id,
"signature":self.signature
}
return tempDict
def sign(self):
rsa_key = RSA.importKey(data.privateKey) # δημιουργία αντικειμένου τύπου κλειδιού
signer = PKCS1_v1_5.new(rsa_key) # δημιουργία του υπογραφέα
signedId=SHA384.new(self.id.encode()) # αντικείμενο πρός υπογραφή
self.signature = base64.b64encode(signer.sign(signedId)).decode() # υπογραφή
def verify_signature(self):
try:
if self.id != self.calculateId():
return False
rsa_key1 = RSA.importKey(self.sender)
signedId=SHA384.new(self.id.encode())
verifier = PKCS1_v1_5.new(rsa_key1)
ver= verifier.verify(signedId, base64.b64decode(self.signature))
return ver
except Exception as e:
print(f'verify_signature: {e.__class__.__name__}: {e}')
return False
def validate_transaction(self):#validates a trans and changes our current state
try: #true trans is valid , flase trans not valid
with data.lock:
if not data.hasReceivedGenesisBlock:
data.utxos[0][self.id]=self.amount
data.hasReceivedGenesisBlock=True
return True
else:
if self.sender==self.recipient:
print("Sender is Recipient")
return False
if self.amount<0:
print("Negate amount")
return False
indexOfSender=data.allPublicKeys.index(self.sender)
indexOfRecipient=data.allPublicKeys.index(self.recipient)
tempCopyOfUtxosSender=copy.deepcopy(data.utxos[indexOfSender])#θα δουλευουμε σε αντιγραφα ωστε αν κατι παει λαθος αν μην χαλασει τα δεδομενα μας
tempCopyOfUtxosRecipient=copy.deepcopy(data.utxos[indexOfRecipient])
allMoney=0
for transId in self.inputs:
if transId in tempCopyOfUtxosSender:
allMoney=allMoney+ tempCopyOfUtxosSender[transId]#παιρνουμε τα λεφτα που πηρε απο εκεινο το trans
tempCopyOfUtxosSender.pop(transId)#το αφαιρουμε για να μην τα ξαναμετρησουμε
else:
print("A past transaction was not found")
return False
if(allMoney<self.amount):
print("Not enough money")
return False
#δεν πηγε κατι λαθος, παιρνει ο καθενας τα λεφτα του και ολοι ειμαστε μια χαρα
if allMoney-self.amount>0:
tempCopyOfUtxosSender[self.id]=allMoney-self.amount#ο sender παιρνει τα ρεστα του
tempCopyOfUtxosRecipient[self.id]=self.amount
data.utxos[indexOfSender]=tempCopyOfUtxosSender
data.utxos[indexOfRecipient]=tempCopyOfUtxosRecipient
return True
except Exception as e:
print(f'validate_transaction: {e.__class__.__name__}: {e}')
return False
'''
def new_transaction(self, sender, recipient, amount,id):
self.current_transactions.append({
'sender': sender,
'recipient': recipient,
'amount': amount,
'id':id
})
data.nextIndex=id+1
return self.last_block['index'] + 1
'''
def createTranasactionFromDictionary(dictionary):
b=dictionary
return transaction(b['sender'], b['recipient'], b['amount'],b['timestamp'], b['inputs'],b['outputs'], b['id'], b['signature'])
def create_transaction(rec_address,amount):
with data.lock:
inputs=utilities.getListOfKeys( data.utxos[data.id] )# για input βαζουμε ο,τι συναλλαγη εχω στο ονομα μου
new_trans=transaction(data.publicKey,data.allPublicKeys[rec_address],amount,time(),inputs,[])
new_trans.id=new_trans.calculateId()
new_trans.sign()
return new_trans
def createGenesisTransaction():#mono o admin to ektelei
timestamp=time()
genTransaction=transaction(0,data.publicKey,100*data.numOfParticipants,timestamp,[],[])
genTransaction.id= genTransaction.calculateId()
return genTransaction