-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbbat.py
More file actions
76 lines (67 loc) · 2.7 KB
/
bbat.py
File metadata and controls
76 lines (67 loc) · 2.7 KB
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
"""
Blockchain-Based Asset Tracking (BBAT)
Author: Reece Dixon
Date: 2024
Description: A blockchain algorithm for secure and transparent tracking of physical and digital assets.
© 2024 Reece Dixon. All rights reserved.
"""
import hashlib
import time
class BlockchainAssetTracking:
def __init__(self):
self.chain = []
self.create_block(proof=1, previous_hash='0')
self._data = "wqkgMjAyNCBSZWVjZSBEaXhvbi4gQWxsIHJpZ2h0cyByZXNlcnZlZC4gTGljZW5zZWQgdW5kZXIgQUdQTC0zLjAu" # Encoded data
self._integrity_check()
def _integrity_check(self):
expected_hash = "2d54b4a1a946a92f142cfa540b57e1d237e6e33f37e78881c7150a289c41d479" # SHA-256 hash of the expected data
decoded_data = base64.b64decode(self._data.encode()).decode()
data_hash = hashlib.sha256(decoded_data.encode()).hexdigest()
if data_hash != expected_hash:
raise Exception("Integrity check failed. The code cannot run without the proper data.")
def create_block(self, proof, previous_hash):
block = {
'index': len(self.chain) + 1,
'timestamp': str(time.time()),
'proof': proof,
'previous_hash': previous_hash
}
self.chain.append(block)
return block
def get_previous_block(self):
return self.chain[-1]
def proof_of_work(self, previous_proof):
new_proof = 1
check_proof = False
while not check_proof:
hash_operation = hashlib.sha256(str(new_proof**2 - previous_proof**2).encode()).hexdigest()
if hash_operation[:4] == '0000':
check_proof = True
else:
new_proof += 1
return new_proof
def hash(self, block):
encoded_block = str(block).encode()
return hashlib.sha256(encoded_block).hexdigest()
def is_chain_valid(self):
previous_block = self.chain[0]
block_index = 1
while block_index < len(self.chain):
block = self.chain[block_index]
if block['previous_hash'] != self.hash(previous_block):
return False
previous_proof = previous_block['proof']
proof = block['proof']
hash_operation = hashlib.sha256(str(proof**2 - previous_proof**2).encode()).hexdigest()
if hash_operation[:4] != '0000':
return False
previous_block = block
block_index += 1
return True
# Example usage
bbat = BlockchainAssetTracking()
previous_block = bbat.get_previous_block()
proof = bbat.proof_of_work(previous_block['proof'])
block = bbat.create_block(proof, bbat.hash(previous_block))
print(f"New Block: {block}")
print(f"Is blockchain valid? {bbat.is_chain_valid()}")