Repository navigation
Expand file tree
/
Copy pathMiniBank.py
More file actions
314 lines (274 loc) · 9.64 KB
/
Copy pathMiniBank.py
File metadata and controls
314 lines (274 loc) · 9.64 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python3
"""
Mini Banking System - CLI
"""
import sqlite3
import hashlib
import secrets
import getpass
import sys
from datetime import datetime
from typing import Optional
DB = "mini_bank.db"
# --------- Database helpers ----------
def get_conn():
return sqlite3.connect(DB)
def init_db():
with get_conn() as conn:
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
pin_hash TEXT NOT NULL,
salt TEXT NOT NULL,
balance REAL NOT NULL DEFAULT 0.0,
created_at TEXT NOT NULL
)""")
cur.execute("""
CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
type TEXT NOT NULL, -- deposit, withdraw, transfer_out, transfer_in
amount REAL NOT NULL,
timestamp TEXT NOT NULL,
details TEXT,
FOREIGN KEY (user_id) REFERENCES users(id)
)""")
conn.commit()
# --------- Security helpers ----------
def hash_pin(pin: str, salt: str) -> str:
# returns hex digest of sha256(salt + pin)
h = hashlib.sha256()
h.update((salt + pin).encode("utf-8"))
return h.hexdigest()
def create_salt() -> str:
return secrets.token_hex(16)
# --------- Core operations ----------
def create_account():
name = input("Full name: ").strip()
if not name:
print("Name cannot be empty.")
return
while True:
pin = getpass.getpass("Choose a 4-6 digit numeric PIN: ").strip()
if not (pin.isdigit() and 4 <= len(pin) <= 6):
print("PIN must be 4-6 digits. Try again.")
continue
pin_confirm = getpass.getpass("Confirm PIN: ").strip()
if pin != pin_confirm:
print("PINs do not match. Try again.")
continue
break
salt = create_salt()
pin_hash = hash_pin(pin, salt)
created_at = datetime.utcnow().isoformat()
with get_conn() as conn:
cur = conn.cursor()
cur.execute("INSERT INTO users (name, pin_hash, salt, balance, created_at) VALUES (?, ?, ?, ?, ?)",
(name, pin_hash, salt, 0.0, created_at))
conn.commit()
user_id = cur.lastrowid
print(f"Account created. Your account ID is: {user_id}")
print("Keep your account ID and PIN safe.")
def find_user_by_id(uid: int) -> Optional[dict]:
with get_conn() as conn:
cur = conn.cursor()
cur.execute("SELECT id, name, pin_hash, salt, balance, created_at FROM users WHERE id = ?", (uid,))
row = cur.fetchone()
if not row:
return None
return {
"id": row[0],
"name": row[1],
"pin_hash": row[2],
"salt": row[3],
"balance": float(row[4]),
"created_at": row[5],
}
def authenticate(uid: int) -> Optional[dict]:
user = find_user_by_id(uid)
if not user:
print("Account not found.")
return None
tries = 3
while tries > 0:
pin = getpass.getpass("Enter PIN: ").strip()
if hash_pin(pin, user["salt"]) == user["pin_hash"]:
print(f"Welcome, {user['name']}!")
return user
tries -= 1
print(f"Incorrect PIN. {tries} attempt(s) left.")
print("Authentication failed.")
return None
def update_balance(user_id: int, new_balance: float):
with get_conn() as conn:
cur = conn.cursor()
cur.execute("UPDATE users SET balance = ? WHERE id = ?", (new_balance, user_id))
conn.commit()
def record_transaction(user_id: int, ttype: str, amount: float, details: str = ""):
ts = datetime.utcnow().isoformat()
with get_conn() as conn:
cur = conn.cursor()
cur.execute("INSERT INTO transactions (user_id, type, amount, timestamp, details) VALUES (?, ?, ?, ?, ?)",
(user_id, ttype, amount, ts, details))
conn.commit()
def deposit(user: dict):
try:
amt = float(input("Enter amount to deposit: ").strip())
except ValueError:
print("Invalid amount.")
return
if amt <= 0:
print("Amount must be positive.")
return
new_bal = user["balance"] + amt
update_balance(user["id"], new_bal)
record_transaction(user["id"], "deposit", amt, "Deposit via CLI")
user["balance"] = new_bal
print(f"Deposited {amt:.2f}. New balance: {new_bal:.2f}")
def withdraw(user: dict):
try:
amt = float(input("Enter amount to withdraw: ").strip())
except ValueError:
print("Invalid amount.")
return
if amt <= 0:
print("Amount must be positive.")
return
if amt > user["balance"]:
print("Insufficient funds.")
return
new_bal = user["balance"] - amt
update_balance(user["id"], new_bal)
record_transaction(user["id"], "withdraw", amt, "Withdrawal via CLI")
user["balance"] = new_bal
print(f"Withdrew {amt:.2f}. New balance: {new_bal:.2f}")
def show_balance(user: dict):
# refresh from DB
u = find_user_by_id(user["id"])
if u:
user["balance"] = u["balance"]
print(f"Account ID: {user['id']} | Name: {user['name']} | Balance: {user['balance']:.2f}")
def transfer(user: dict):
try:
target_id = int(input("Recipient account ID: ").strip())
except ValueError:
print("Invalid account ID.")
return
if target_id == user["id"]:
print("Cannot transfer to the same account.")
return
target = find_user_by_id(target_id)
if not target:
print("Recipient account not found.")
return
try:
amt = float(input("Amount to transfer: ").strip())
except ValueError:
print("Invalid amount.")
return
if amt <= 0:
print("Amount must be positive.")
return
if amt > user["balance"]:
print("Insufficient funds.")
return
# perform transfer
new_bal_from = user["balance"] - amt
new_bal_to = target["balance"] + amt
with get_conn() as conn:
cur = conn.cursor()
cur.execute("UPDATE users SET balance = ? WHERE id = ?", (new_bal_from, user["id"]))
cur.execute("UPDATE users SET balance = ? WHERE id = ?", (new_bal_to, target_id))
# record both sides
ts = datetime.utcnow().isoformat()
cur.execute("INSERT INTO transactions (user_id, type, amount, timestamp, details) VALUES (?, ?, ?, ?, ?)",
(user["id"], "transfer_out", amt, ts, f"To account {target_id}"))
cur.execute("INSERT INTO transactions (user_id, type, amount, timestamp, details) VALUES (?, ?, ?, ?, ?)",
(target_id, "transfer_in", amt, ts, f"From account {user['id']}"))
conn.commit()
user["balance"] = new_bal_from
print(f"Transferred {amt:.2f} to account {target_id}. New balance: {user['balance']:.2f}")
def show_transactions(user: dict, limit: int = 20):
with get_conn() as conn:
cur = conn.cursor()
cur.execute("SELECT type, amount, timestamp, details FROM transactions WHERE user_id = ? ORDER BY id DESC LIMIT ?",
(user["id"], limit))
rows = cur.fetchall()
if not rows:
print("No transactions found.")
return
print(f"Last {len(rows)} transactions (most recent first):")
for ttype, amt, ts, details in rows:
print(f"{ts} | {ttype:12} | {amt:10.2f} | {details}")
def close_account(user: dict):
confirm = input("Are you sure you want to close your account? This is irreversible (yes/no): ").strip().lower()
if confirm != "yes":
print("Cancelled.")
return
with get_conn() as conn:
cur = conn.cursor()
cur.execute("DELETE FROM transactions WHERE user_id = ?", (user["id"],))
cur.execute("DELETE FROM users WHERE id = ?", (user["id"],))
conn.commit()
print("Account closed and all data removed. Goodbye.")
sys.exit(0)
# --------- CLI flow ----------
def user_menu(user: dict):
while True:
print("\n--- Account Menu ---")
print("1) Show balance")
print("2) Deposit")
print("3) Withdraw")
print("4) Transfer")
print("5) Transaction history")
print("6) Close account")
print("0) Logout")
choice = input("Select an option: ").strip()
if choice == "1":
show_balance(user)
elif choice == "2":
deposit(user)
elif choice == "3":
withdraw(user)
elif choice == "4":
transfer(user)
elif choice == "5":
show_transactions(user)
elif choice == "6":
close_account(user)
elif choice == "0":
print("Logging out...")
break
else:
print("Invalid option. Try again.")
def main_menu():
init_db()
print("=== Welcome to MiniBank CLI ===")
while True:
print("\nMain Menu:")
print("1) Create account")
print("2) Login")
print("0) Exit")
choice = input("Choice: ").strip()
if choice == "1":
create_account()
elif choice == "2":
try:
uid = int(input("Enter your account ID: ").strip())
except ValueError:
print("Invalid account ID.")
continue
user = authenticate(uid)
if user:
# refresh balance from DB
user = find_user_by_id(user["id"])
user_menu(user)
elif choice == "0":
print("Goodbye.")
sys.exit(0)
else:
print("Invalid choice.")
if __name__ == "__main__":
main_menu()