-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransaction.py
41 lines (27 loc) · 1.3 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
from collections import OrderedDict
class Transaction:
def __init__(self, sender, recipient, amount, signature):
self.sender = sender
self.recipient = recipient
self.amount = amount
self.signature = signature
# def __str__(self):
# # return str(self.__dict__)
# # Custom stringification to get a consistant hash
# return f'{{"sender":"{self.sender}","recipient":"{self.recipient}","amount":"{self.amount}"}}'
def __repr__(self):
# Custom stringification to get a consistant hash
return f'{{"sender":"{self.sender}","recipient":"{self.recipient}","amount":"{self.amount}","signature":"{self.signature}"}}'
@classmethod
def from_dict(cls, transaction):
"""Factory function to return a Transaction object from a dictionary of transaction data
Arguments:
:transaction: A transaction dictionary to convert into object
"""
return cls(transaction.sender, transaction.recipient, transaction.amount, transaction.signature)
def to_dict(self):
"""Get a copy of the transaction as a dict"""
return self.__dict__.copy()
def to_ordered_dict(self):
"""Returns an OrderedDict representation of the current Transaction object (to get a consistant hash)"""
return OrderedDict([('sender', self.sender), ('receipient', self.recipient), ('amount', self.amount), ('signature',self.signature)])