-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.py
68 lines (58 loc) · 2.25 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
from wallet import Wallet
class Transaction():
# sender and receiver's public keys in class format
def __init__(self, sender, input, receiver, output, miner = None, tx_fee = 0, sig = None, bonus = False):
self.input = (sender,input)
self.output = (receiver,output)
self.tx_fee = tx_fee
self.miner = miner
#calculates fee
tx_fee = input - output
if tx_fee > 0:
self.tx_fee = tx_fee
self.sender = sender
self.receiver = receiver
#default is None
self.sig = sig
self.bonus = bonus
def sign(self, wallet):
sig, is_signed = wallet.sign(self.__gather(), wallet.chain)
if not is_signed:
return False
self.sig = sig
return True
def __gather(self):
if self.tx_fee > 0:
return [self.input,self.output,(self.miner,self.tx_fee)]
return [self.input,self.output]
def to_dict(self):
if not self.bonus:
sender = Wallet.get_pu_ser(self.sender)[1]
sig = Wallet.convert_sig(self.sig)
miner = Wallet.get_pu_ser(self.miner)[1]
receiver = Wallet.get_pu_ser(self.receiver)[1]
dict = {'sender':self.sender if self.bonus else sender, # if bonus send self.sender = none
'input':self.input[1],
'receiver': receiver,
'output':self.output[1],
'miner':self.miner if self.bonus else miner, # if bonus send self.miner = none
'tx_fee':self.tx_fee,
'sig':self.sig if self.bonus else sig # if bonus send self.sig = none // bcs. no need !
}
return dict
@staticmethod
def from_dict(dict):
sender = Wallet.load_pu(Wallet.join_ser_text("public",dict['sender']))
receiver = Wallet.load_pu(Wallet.join_ser_text("public",dict['receiver']))
miner = Wallet.load_pu(Wallet.join_ser_text("public",dict['miner']))
sig = Wallet.convert_sig(dict['sig'])
tx = Transaction(
sender = sender,
input = dict['input'],
receiver = receiver,
output = dict['output'],
miner = miner,
tx_fee = dict['tx_fee'],
sig = sig
)
return tx