-
Notifications
You must be signed in to change notification settings - Fork 7
/
transaction.py
49 lines (42 loc) · 1.61 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
from hashutils import hash_sha256
from ecdsa import SigningKey
class Transaction():
""" Generic coin transaction """
pass
class Payment(Transaction):
""" Transfer coins between wallets """
def __init__(self, created_coins, consumed_coins, transaction_id=-1):
self.created_coins = created_coins
self.consumed_coins = consumed_coins
self.id = transaction_id
def verify_balance(self):
""" Verify that the total amount of created coins is
equal to the total amount of consumed coins
"""
total_created = 0
total_consumed = 0
for consumed_coin in self.consumed_coins:
total_consumed += consumed_coin.value
for created_coin in self.created_coins:
total_created += created_coin.value
return total_consumed == total_created
def __str__(self):
concat = 'TransID: ' + str(self.id) + '\t' + 'Type: Payment\n' + \
'\nConsumed coins: \n'
for coin in self.consumed_coins:
concat += str(coin) + '\n'
concat += '\nCreated coins: \n'
for coin in self.created_coins:
concat += str(coin) + '\n'
return concat
class CoinCreation(Transaction):
""" Creation of coins """
def __init__(self, created_coins, transaction_id=-1):
self.created_coins = created_coins
self.id = transaction_id
def __str__(self):
concat = 'TransID: ' + str(self.id) + '\t' + 'Type: Coin creation\n' + \
'\nCreated coins: \n'
for coin in self.created_coins:
concat += str(coin) + '\n'
return concat