-
Notifications
You must be signed in to change notification settings - Fork 2
/
transaction.py
58 lines (49 loc) · 2.02 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
from collections import OrderedDict
import binascii
import time
import Crypto
import Crypto.Random
from Crypto.Hash import SHA
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
import random
import requests
from flask import Flask, jsonify, request, render_template
def makeRSAjsonSendable(rsa):
return rsa.exportKey("PEM").decode('ascii')
def makejsonSendableRSA(jsonSendable):
return RSA.importKey(jsonSendable.encode('ascii'))
class Transaction:
def __init__(self, sender_address, sender_private_key, recipient_address, value, reals=None, realr=None):
self.sender_address = sender_address
self.receiver_address = recipient_address
self.amount = value
self.reals = reals
self.realr = realr
self.rand = Crypto.Random.get_random_bytes(10)
self.transaction_id = SHA.new((str(sender_address)+str(recipient_address)+str(value) + str(self.rand)).encode())
self.transaction_myid = str(sender_address)+str(recipient_address)+str(value) + str(self.rand)
self.transaction_inputs = []
self.transaction_outputs = []
self.transaction_id_hex=self.transaction_id.hexdigest()
self.timeCreated = time.time()
self.timeAdded = None
self.signature = None
if(not type(self.sender_address) == type(0)):
self.signature = self.sign_transaction(sender_private_key)
def sign_transaction(self, private_key):
"""
Sign transaction with private key
"""
signature = PKCS1_v1_5.new(private_key).sign(self.transaction_id)
return signature
def verify_transaction(self):
return True
def printMe(self):
sender = self.sender_address
receiver = self.receiver_address
if(not self.reals == None):
sender = self.reals
if(not self.realr == None):
receiver = self.realr
print("\t \t I am transaction ({}) giving {} $ from node {} to node {}".format(self.transaction_id_hex, self.amount, sender, receiver))