-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransaction.py
56 lines (45 loc) · 2.01 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
from collections import OrderedDict
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA
import binascii
import time
# this class required to work on to make transactions more significant
class Transaction:
def __init__(self, amount=0, sender_public_key=None, recipient_public_key=None, timestamp=time.time()):
self.amount = amount
self.sender_public_key = sender_public_key
self.recipient_public_key = recipient_public_key
self.timestamp = timestamp
self.signautre = None
def get_transaction_bill(self):
return OrderedDict({'sender_public_key': self.sender_public_key,
'recipient_public_key': self.recipient_public_key,
'amount': self.amount,
'timestamp': self.timestamp
})
def sign_transaction(self, sender_private_key):
private_key = RSA.importKey(
binascii.unhexlify(sender_private_key))
signer = PKCS1_v1_5.new(private_key)
message = SHA.new(str(self.get_transaction_bill()).encode('utf8'))
self.signature = binascii.hexlify(signer.sign(message)).decode('ascii')
def verifyIt(self, blockchain):
if(self.sender_public_key != None
and self.signature != None
and self.amount > 0
and self.recipient_public_key != None):
response = blockchain.verify_transaction(
self.get_transaction_bill(), self.signature)
return response
else:
return False
@staticmethod
def printIt(transaction):
if isinstance(transaction, Transaction):
transaction = transaction.get_transaction_bill()
print('amount : ', transaction['amount'])
print('sender : ', transaction['sender_public_key'][-50:])
print('reciever : ', transaction['recipient_public_key'][-50:])
print('timestamp : ', transaction['timestamp'])
print()