File size: 3,794 Bytes
c42ffd1
 
 
 
f7564df
 
 
c42ffd1
 
 
 
 
03ef91d
c42ffd1
 
 
 
03ef91d
 
 
 
 
 
c42ffd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e52d3cb
c42ffd1
 
 
 
 
e52d3cb
c42ffd1
 
 
 
 
 
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
import streamlit as st
import hashlib
import time
import json
from datetime import datetime
import pytz


# Blockchain Classes
class Block:
    def __init__(self, index, timestamp, transactions, previous_hash):
        self.index = index
        self.timestamp = self.get_pakistan_time(timestamp)
        self.transactions = transactions
        self.previous_hash = previous_hash
        self.hash = self.calculate_hash()

    def get_pakistan_time(self, utc_time):
        """Convert UTC time to Pakistan Standard Time."""
        pst_timezone = pytz.timezone("Asia/Karachi")
        local_time = datetime.fromtimestamp(utc_time, pytz.utc).astimezone(pst_timezone)
        return local_time.strftime("%a %b %d %H:%M:%S %Y")  # Human-readable format

    def calculate_hash(self):
        block_content = json.dumps({
            "index": self.index,
            "timestamp": self.timestamp,
            "transactions": self.transactions,
            "previous_hash": self.previous_hash
        }, sort_keys=True).encode()
        return hashlib.sha256(block_content).hexdigest()

class Blockchain:
    def __init__(self):
        self.chain = [self.create_genesis_block()]
        self.pending_transactions = []

    def create_genesis_block(self):
        return Block(0, time.time(), [], "0")

    def get_latest_block(self):
        return self.chain[-1]

    def add_transaction(self, transaction):
        self.pending_transactions.append(transaction)

    def mine_block(self):
        if not self.pending_transactions:
            return None
        
        new_block = Block(
            index=len(self.chain),
            timestamp=time.time(),
            transactions=self.pending_transactions,
            previous_hash=self.get_latest_block().hash
        )
        self.chain.append(new_block)
        self.pending_transactions = []  # Clear pending transactions
        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

# Streamlit App
st.title("Simple Blockchain Demo")
st.sidebar.header("Add a Transaction")

# Initialize Blockchain
if "blockchain" not in st.session_state:
    st.session_state.blockchain = Blockchain()

blockchain = st.session_state.blockchain

# Add a Transaction
sender = st.sidebar.text_input("Sender")
recipient = st.sidebar.text_input("Recipient")
amount = st.sidebar.number_input("Amount", min_value=0.01, step=0.01)

if st.sidebar.button("Add Transaction"):
    if sender and recipient and amount:
        blockchain.add_transaction({"from": sender, "to": recipient, "amount": amount})
        st.sidebar.success("Transaction added to the pool!")
    else:
        st.sidebar.error("Please provide valid transaction details.")

# Mine a Block
if st.button("Mine a Block"):
    new_block = blockchain.mine_block()
    if new_block:
        st.success(f"Block {new_block.index} mined successfully!")
    else:
        st.error("No transactions to mine.")

# Display Blockchain
st.subheader("Blockchain Ledger")
for block in blockchain.chain:
    st.write({
        "Index": block.index,
        "Timestamp": block.timestamp,  # Already in PST format
        "Transactions": block.transactions,
        "Previous Hash": block.previous_hash,
        "Hash": block.hash,
    })


# Validate Blockchain
if st.button("Validate Blockchain"):
    if blockchain.is_chain_valid():
        st.success("Blockchain is valid!")
    else:
        st.error("Blockchain is invalid!")