-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_tomas.py
170 lines (127 loc) · 5.79 KB
/
main_tomas.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#today we'll be creating a blockchain in python
#these are going to be the following classes: block, wallet, transaction, blockVerify
#start with the transaction class
#in a transaction, there is the sender, the receiver, the amount, the date and the signature
from hashlib import sha256
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.asymmetric import rsa
class Signing:
def __init__(self):
self.public_key = ""
self.private_key = ""
self.signature = self.sign()
def sign(self, transaction_hash):
return self.private_key.sign(transaction_hash, padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH), hashes.SHA256())
class Transaction:
def __init__(self):
self.sender = "" #identified by the public key
self.receiver = "" #identified by the public key
self.amount = 0 #amount of money being sent after commission is taken by the miner
self.date = ""
self.hash = self.calculate_hash()
self.signature = self.sign()
def sign(self):
return Signing(self.sender, self.sender_pk).sign(self.hash)
def calculate_hash(self):
#we need to calculate the hash of the transaction
#the hash is going to be the hash of the sender, the receiver, the amount, the date and the signature
return sha256((str(self.sender) + str(self.receiver) + str(self.amount) + str(self.date)).encode()).hexdigest()
def getSender(self):
return self.sender
def setSender(self, sender):
self.sender = sender
class Wallet:
def __init__(self):
#generate a public key and a private key with cryptography library
self.private_key = rsa.generate_private_key(public_exponent=65537,key_size=2048,)
self.public_key = self.private_key.public_key()
self.balance = 0 #amount of money in the wallet
class Block:
def __init__(self, previous_hash, transactions, miner_address):
self.previous_hash = previous_hash
self.transactions = transactions
self.miner_address = miner_address
self.hash = self.calculate_hash()
def calculate_hash(self):
#we need to calculate the hash of the block
#the hash is going to be the hash of the previous hash, the transactions, the previous miner address and the previous miner token
#we need to convert the transactions to a string
transactions_string = ""
for transaction in self.transactions:
transactions_string += str(transaction.sender) + str(transaction.receiver) + str(transaction.amount) + str(transaction.date) + str(transaction.signature)
return sha256((str(self.previous_hash) + transactions_string + str(self.miner_address)).encode()).hexdigest()
class BlockVerify:
def __init__(self):
self.all_tokens = []
self.block_timer = 7
self.all_public_keys = []
#now we need to create the blockchain
#the blockchain is going to be a list of blocks
#the first block is going to be the genesis block
#the genesis block is going to be created by the creator of the blockchain
class Blockchain:
def __init__(self):
self.chain = [] #list of blocks
self.all_transactions = [] #segment the transactions for every week
self.all_tokens = []
self.all_public_keys = []
self.block_timer = 7 # a week
self.create_genesis_block()
def create_genesis_block(self, miner_address):
genesis_block = Block()
genesis_block.previous_hash = "0"
genesis_block.transactions = []
# genesis_block.public_key_miner = ??
genesis_block.miner_address = miner_address #to complete with the winner
genesis_block.hash = genesis_block.calculate_hash()
self.chain.append(genesis_block)
return genesis_block
def add_block(self, transactions, miner_address):
new_block = Block()
new_block.previous_hash = self.chain[-1].hash
new_block.transactions = transactions
new_block.miner_address = miner_address #to complete with the winner
new_block.hash = new_block.calculate_hash()
self.chain.append(new_block)
return new_block
def is_chain_valid(self):
for i in range(1, len(self.chain)):
current_block = self.chain[i]
previous_block = self.chain[i - 1]
if current_block.hash != current_block.calculate_hash():
return False
if current_block.previous_hash != previous_block.hash:
return False
return True
def add_transaction(self, sender, receiver, amount, date, signature):
new_transaction = Transaction()
new_transaction.sender = sender
new_transaction.receiver = receiver
new_transaction.amount = amount
new_transaction.date = date
new_transaction.signature = signature
self.all_transactions.append(new_transaction)
return new_transaction
def add_token(self, token):
self.all_tokens.append(token)
def add_public_key(self, public_key):
self.all_public_keys.append(public_key)
def get_blockchain(self):
return self.chain
def get_all_transactions(self):
return self.all_transactions
def get_all_tokens(self):
return self.all_tokens
def get_all_public_keys(self):
return self.all_public_keys
def get_block_timer(self):
return self.block_timer
def get_block(self, index):
return self.chain[index]
def get_last_block(self):
return self.chain[len(self.chain) - 1]
def get_blockchain_length(self):
return len(self.chain)
def get_all_transactions_length(self):
return len(self.all_transactions)