-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
144 lines (117 loc) · 4.8 KB
/
main.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
import hashlib
import time
from cryptography.fernet import Fernet
ACCOUNT_FILE = "account.txt"
BALANCE_FILE = "balance.txt"
KEY_FILE = "key.key"
DEFAULT_BALANCE = 0
def load_account():
try:
with open(ACCOUNT_FILE, "r") as f:
account_name = f.read().strip()
except FileNotFoundError:
print("Error: Account file not found.")
exit(1)
return account_name
def load_balance():
try:
with open(BALANCE_FILE, "rb") as f:
data = f.read()
fernet = Fernet(load_key())
balance = int(fernet.decrypt(data).decode())
except FileNotFoundError:
balance = DEFAULT_BALANCE
with open(BALANCE_FILE, "wb") as f:
fernet = Fernet(generate_key())
encrypted_data = fernet.encrypt(str(balance).encode())
f.write(encrypted_data)
return balance
def update_balance(balance):
with open(BALANCE_FILE, "wb") as f:
fernet = Fernet(load_key())
encrypted_data = fernet.encrypt(str(balance).encode())
f.write(encrypted_data)
def check_balance():
balance = load_balance()
print(f"Your current balance is: {balance}")
def create_hashcash():
amount = int(input("Enter the amount: "))
receiver = input("Enter the receiver address: ")
balance = load_balance()
if balance < amount:
print("Insufficient balance.")
return
timestamp = int(time.time() + 60) # expires in 1 minute
message = f"{amount}:{receiver}:{timestamp}"
nonce = 0
while True:
candidate = f"{message}:{nonce}".encode("utf-8")
hashcash = hashlib.sha1(candidate).hexdigest()
if hashcash.startswith("00000"):
print(f"Cash, this is what you send to the reciver: {candidate}")
print(f"Hash, Proof the transaction is valid: {hashcash}")
update_balance(balance - amount)
break
nonce += 1
def receive_hashcash():
account_name = load_account()
candidate = input("Enter the hashcash candidate: ")
try:
amount, receiver, timestamp, nonce = candidate.split(":")
amount = int(amount)
timestamp = int(timestamp)
nonce = int(nonce)
except ValueError:
print("Error: Invalid candidate format.")
return
balance = load_balance()
if balance < amount:
print("Error: Insufficient balance.")
return
current_time = int(time.time())
if timestamp <= current_time:
print("Error: Candidate has expired.")
return
message = f"{amount}:{receiver}:{timestamp}"
candidate_str = f"{message}:{nonce}"
hashcash = hashlib.sha1(candidate_str.encode()).hexdigest()
if hashcash[:5] != "00000":
print("Error: Invalid hashcash candidate.")
return
if receiver != account_name:
print("Error: Candidate is not for this account.")
return
print(f"Cash candidate verified: {candidate_str}")
update_balance(balance + amount)
print(f"Amount {amount} added to your account balance.")
def generate_key():
key = Fernet.generate_key()
with open(KEY_FILE, "wb") as f:
f.write(key)
return key
def load_key():
with open(KEY_FILE, "rb") as f:
key = f.read()
return key
print("""\033[91mi\033[0m\033[94mf\033[0m\033[92mx \033[0mHashPay v0.1
██████╗██████╗ ███████╗██████╗ ███████╗████████╗██╗██╗ ██╗███████╗
██╔════╝██╔══██╗██╔════╝██╔══██╗██╔════╝╚══██╔══╝██║██║ ██╔╝██╔════╝
██║ ██████╔╝█████╗ ██║ ██║███████╗ ██║ ██║█████╔╝ ███████╗
██║ ██╔══██╗██╔══╝ ██║ ██║╚════██║ ██║ ██║██╔═██╗ ╚════██║
╚██████╗██║ ██║███████╗██████╔╝███████║ ██║ ██║██║ ██╗███████║
╚═════╝╚═╝ ╚═╝╚══════╝╚═════╝ ╚══════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝
v0.1
""")
while True:
print("Choose an option:\n1. Check balance\n2. Make a transaction\n3. Receive Cash\n4. Exit")
choice = input("X-HashPay > ")
if choice == "1":
check_balance()
elif choice == "2":
create_hashcash()
elif choice == "3":
receive_hashcash()
elif choice == "4":
break
else:
print("Invalid choice.")