forked from duino-coin/duino-coin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLI_Wallet.py
More file actions
220 lines (192 loc) · 9.2 KB
/
Copy pathCLI_Wallet.py
File metadata and controls
220 lines (192 loc) · 9.2 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
#!/usr/bin/env python3
##########################################
# Duino-Coin Console Wallet (v1.5)
# https://github.com/revoxhere/duino-coin
# Distributed under MIT license
# © revox 2020
##########################################
import socket, configparser, getpass, os, platform, sys, time, json
from signal import signal, SIGINT
from pathlib import Path
try: # Check if requests is installed
import requests
except:
now = datetime.datetime.now()
print(now.strftime("%H:%M:%S ") + "Requests is not installed. Please install it using: python3 -m pip install requests.\nIf you can't install it, use Minimal-PC_Miner.\nExiting in 15s.")
time.sleep(15)
os._exit(1)
try: # Check if colorama is installed
from colorama import init, Fore, Back, Style
except:
now = datetime.datetime.now()
print(now.strftime("%H:%M:%S ") + "Colorama is not installed. Please install it using: python3 -m pip install colorama.\nIf you can't install it, use Minimal-PC_Miner.\nExiting in 15s.")
time.sleep(15)
os._exit(1)
timeout = 5 # Socket timeout
VER = 1.5
res = "https://raw.githubusercontent.com/revoxhere/duino-coin/gh-pages/serverip.txt" # Serverip file
config = configparser.ConfigParser()
pcusername = getpass.getuser() # Username
platform = str(platform.system()) + " " + str(platform.release()) # Platform information
publicip = requests.get("https://api.ipify.org").text # Public IP
s = socket.socket()
def title(title):
if os.name == 'nt':
os.system("title "+title)
else:
print('\33]0;'+title+'\a', end='')
sys.stdout.flush()
def handler(signal_received, frame): # If CTRL+C or SIGINT received, send CLOSE request to server in order to exit gracefully.
print(Style.RESET_ALL + Style.BRIGHT + Fore.YELLOW + "See you soon!")
try:
s.send(bytes("CLOSE", encoding="utf8"))
except:
pass
os._exit(0)
signal(SIGINT, handler) # Enable signal handler
while True: # Grab data grom GitHub section
try:
res = requests.get(res, data = None) #Use request to grab data from raw github file
if res.status_code == 200: #Check for response
content = res.content.decode().splitlines() #Read content and split into lines
pool_address = content[0] #Line 1 = pool address
pool_port = content[1] #Line 2 = pool port
try: # Try to connect
s.connect((str(pool_address), int(pool_port)))
s.settimeout(timeout)
SERVER_VER = s.recv(3).decode()
jsonapi = requests.get("https://raw.githubusercontent.com/revoxhere/duco-statistics/master/api.json", data = None) # Use request to grab data from raw github file
if jsonapi.status_code == 200: # Check for reponse
content = jsonapi.content.decode() # Read content and split into lines
contentjson = json.loads(content)
ducofiat = float(contentjson["Duco price"])
else:
ducofiat = 0.0025 # If json api request fails, wallet will use this value
break # If connection was established, continue
except: # If it wasn't, display a message
print(Style.RESET_ALL + Fore.RED + "Cannot connect to the server. It is probably under maintenance or temporarily down.\nRetrying in 15 seconds.")
time.sleep(15)
os.execl(sys.executable, sys.executable, *sys.argv)
else:
print("Retrying connection...")
time.sleep(0.025) # Restart if wrong status code
except:
print(Style.RESET_ALL + Fore.RED + " Cannot receive pool address and IP.\nExiting in 15 seconds.")
time.sleep(15)
os._exit(1)
while True:
title("Duino-Coin CLI Wallet")
if not Path("WalletCLI_config.ini").is_file(): # Initial configuration section
print(Style.RESET_ALL + Style.BRIGHT + Fore.YELLOW + "Duino-Coin CLI Wallet first-run\n")
print(Style.RESET_ALL + "Select an option")
choice = input(" 1 - Login\n 2 - Register\n 3 - Exit\n")
if int(choice) <= 1:
username = input(Style.RESET_ALL + Fore.YELLOW + "Enter your username: " + Style.BRIGHT)
password = input(Style.RESET_ALL + Fore.YELLOW + "Enter your password: " + Style.BRIGHT)
s.send(bytes("LOGI," + str(username) + "," + str(password), encoding="utf8"))
loginFeedback = s.recv(64).decode().split(",")
if loginFeedback[0] == "OK":
print(Style.RESET_ALL + Fore.YELLOW + "Successfull login")
config['wallet'] = {"username": username, "password": password}
with open("WalletCLI_config.ini", "w") as configfile: # Write data to file
config.write(configfile)
else:
print(Style.RESET_ALL + Fore.RED + "Couldn't login, reason: " + Style.BRIGHT + str(loginFeedback[1]))
time.sleep(15)
os._exit(1)
if int(choice) == 2:
username = input(Style.RESET_ALL + Fore.YELLOW + "Enter your username: " + Style.BRIGHT)
password = input(Style.RESET_ALL + Fore.YELLOW + "Enter your password: " + Style.BRIGHT)
pconfirm = input(Style.RESET_ALL + Fore.YELLOW + "Confirm your password: " + Style.BRIGHT)
email = input(Style.RESET_ALL + Fore.YELLOW + "Enter your e-mail address (used for exchange and support): " + Style.BRIGHT)
if password == pconfirm:
while True:
s.send(bytes("REGI," + str(username) + "," + str(password) + "," + str(email), encoding="utf8"))
regiFeedback = s.recv(256).decode().split(",")
if regiFeedback[0] == "OK":
print(Style.RESET_ALL + Fore.YELLOW + Style.BRIGHT + "Successfully registered new account")
break
elif regiFeedback[0] == "NO":
print(Style.RESET_ALL + Fore.RED +
"\nCouldn't register new user, reason: " + Style.BRIGHT + str(regiFeedback[1]))
time.sleep(15)
os._exit(1)
if int(choice) >= 3:
os._exit(0)
else: # If config already exists, load from it
while True:
config.read("WalletCLI_config.ini")
username = config["wallet"]["username"]
password = config["wallet"]["password"]
s.send(bytes("LOGI," + str(username) + "," + str(password), encoding="utf8"))
loginFeedback = s.recv(128).decode().split(",")
if loginFeedback[0] == "OK":
break
else:
print(Style.RESET_ALL + Fore.RED + "Couldn't login, reason: " + Style.BRIGHT + str(loginFeedback[1]))
time.sleep(15)
os._exit(1)
while True:
while True:
s.send(bytes("BALA", encoding="utf8"))
try:
balance = round(float(s.recv(256).decode()), 8)
balanceusd = round(float(balance) * float(ducofiat), 4)
break
except:
pass
print(Style.RESET_ALL + Style.BRIGHT + Fore.YELLOW + "\nDuino-Coin CLI Wallet")
print(Style.RESET_ALL + Fore.YELLOW + "You have " + Style.BRIGHT + str(balance) + " DUCO")
print(Style.RESET_ALL + Fore.YELLOW + "Which is about " + Style.BRIGHT + str(balanceusd) + " USD")
print(Style.RESET_ALL + Fore.YELLOW + "Type `help` to list available commands")
command = input(Style.RESET_ALL + Fore.WHITE + "DUCO Console $ " + Style.BRIGHT)
if command == "refresh":
continue
elif command == "send":
recipient = input(Style.RESET_ALL + Fore.WHITE + "Enter recipients' username: " + Style.BRIGHT)
amount = input(Style.RESET_ALL + Fore.WHITE + "Enter amount to transfer: " + Style.BRIGHT)
s.send(bytes("SEND,deprecated,"+str(recipient)+","+str(amount), encoding="utf8"))
while True:
message = s.recv(1024).decode()
print(Style.RESET_ALL + Fore.BLUE + "Server message: " + Style.BRIGHT + str(message))
break
elif command == "changepass":
oldpassword = input(Style.RESET_ALL + Fore.WHITE + "Enter your current password: " + Style.BRIGHT)
newpassword = input(Style.RESET_ALL + Fore.WHITE + "Enter new password: " + Style.BRIGHT)
s.send(bytes("CHGP,"+ str(oldpassword) + "," + str(newpassword), encoding="utf8"))
while True:
message = s.recv(1024).decode()
print(Style.RESET_ALL + Fore.BLUE + "Server message: " + Style.BRIGHT + str(message))
break
elif command == "exit":
print(Style.RESET_ALL + Style.BRIGHT + Fore.YELLOW + "\nSIGINT detected - Exiting gracefully." + Style.NORMAL + " See you soon!")
try:
s.send(bytes("CLOSE", encoding="utf8"))
except:
pass
os._exit(0)
elif command == "userinfo":
s.send(bytes("STAT", encoding="utf8"))
while True:
message = s.recv(1024).decode()
break
print(Style.RESET_ALL + Fore.BLUE + "Server message: " + Style.BRIGHT + str(message))
elif command == "about":
print(Style.RESET_ALL + Fore.WHITE + "Duino-Coin CLI Wallet is made with <3 by Duino-Coin community")
print(Style.RESET_ALL + Fore.WHITE + "This is version "+str(VER))
print(Style.RESET_ALL + Fore.WHITE + "And is distributed under MIT license")
print(Style.RESET_ALL + Fore.WHITE + Style.BRIGHT + "https://duinocoin.com")
print(Style.RESET_ALL + Fore.WHITE + "Stay safe everyone!")
elif command == "logout":
os.remove("WalletCLI_config.ini")
os.execl(sys.executable, sys.executable, *sys.argv)
else:
print(Style.RESET_ALL + Fore.WHITE + Style.BRIGHT + "Available commands:")
print(Style.RESET_ALL + Fore.WHITE + " help - shows this help message")
print(Style.RESET_ALL + Fore.WHITE + " refresh - refreshes balance")
print(Style.RESET_ALL + Fore.WHITE + " send - sends funds")
print(Style.RESET_ALL + Fore.WHITE + " userinfo - displays account informations")
print(Style.RESET_ALL + Fore.WHITE + " changepass - changes account password")
print(Style.RESET_ALL + Fore.WHITE + " exit - exits Duino-Coin wallet")
print(Style.RESET_ALL + Fore.WHITE + " about - displays about message")
print(Style.RESET_ALL + Fore.WHITE + " logout - logs off the user")