forked from revoxhere/duino-coin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLI_Wallet.py
1035 lines (923 loc) · 39.5 KB
/
CLI_Wallet.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
##########################################
# Duino-Coin CLI Wallet (v2.45)
# https://github.com/revoxhere/duino-coin
# Distributed under MIT license
# © Duino-Coin Community 2020
##########################################
import configparser
import datetime
import getpass
import json
import os
import platform
import socket
import sys
import time
from pathlib import Path
from signal import SIGINT, signal
from base64 import b64decode, b64encode
try:
from base64 import urlsafe_b64decode as b64d
from base64 import urlsafe_b64encode as b64e
except ModuleNotFoundError:
print("Base64 is not installed. "
+ "Please manually install \"base64\""
+ "\nExiting in 15s.")
sleep(15)
_exit(1)
try:
from cryptography.fernet import Fernet, InvalidToken
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
except:
now = datetime.datetime.now()
print(now.strftime("%H:%M:%S ")
+ "Cryptography is not installed. "
+ "Please install it using: python3 -m pip install cryptography."
+ "\nExiting in 15s.")
time.sleep(15)
os._exit(1)
try:
import secrets
except:
now = datetime.datetime.now()
print(now.strftime("%H:%M:%S ")
+ "Secrets is not installed. "
+ "Please install it using: python3 -m pip install secrets."
+ "\nExiting in 15s.")
time.sleep(15)
os._exit(1)
try:
import websocket
except:
now = datetime.datetime.now()
print(now.strftime("%H:%M:%S ")
+ "Websocket-client is not installed. "
+ "Please install it using: python3 -m pip install websocket-client."
+ "\nExiting in 15s.")
time.sleep(15)
os._exit(1)
try:
from base64 import urlsafe_b64decode as b64d
from base64 import urlsafe_b64encode as b64e
except:
now = datetime.datetime.now()
print(now.strftime("%H:%M:%S ")
+ "Base64 is not installed. "
+ "Please install it using: python3 -m pip install base64."
+ "\nExiting in 15s.")
time.sleep(15)
os._exit(1)
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."
+ "\nExiting in 15s.")
time.sleep(15)
os._exit(1)
try: # Check if colorama is installed
from colorama import Back, Fore, Style, init
except:
now = datetime.datetime.now()
print(now.strftime("%H:%M:%S ")
+ "Colorama is not installed. "
+ "Please install it using: python3 -m pip install colorama."
+ "\nExiting in 15s.")
time.sleep(15)
os._exit(1)
try:
import tronpy
from tronpy.keys import PrivateKey
tronpy_installed = True
except:
tronpy_installed = False
now = datetime.datetime.now()
print(now.strftime("%H:%M:%S ")
+ "Tronpy is not installed. "
+ "Please install it using: python3 -m pip install tronpy."
+ "\nWrapper is disabled because of tronpy is not installed.")
wrong_passphrase = False
backend = default_backend()
iterations = 100_000
timeout = 30 # Socket timeout
VER = 2.45
use_wrapper = False
WS_URI = "wss://server.duinocoin.com:15808"
# Serverip file
config = configparser.ConfigParser()
# Check if commands file exists
if not Path("cli_wallet_commands.json").is_file():
url = ("https://raw.githubusercontent.com/"
+ "revoxhere/"
+ "duino-coin/master/Resources/"
+ "cli_wallet_commands.json")
r = requests.get(url)
with open("cli_wallet_commands.json", "wb") as f:
f.write(r.content)
def title(title):
if os.name == 'nt':
os.system(
"title "
+ title)
else:
print(
'\33]0;'
+ title
+ '\a',
end='')
sys.stdout.flush()
def _derive_key(
password: bytes,
salt: bytes,
iterations: int = iterations) -> bytes:
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=iterations,
backend=backend)
return b64e(kdf.derive(password))
def print_command(name, desc):
print(" " + Style.RESET_ALL + Fore.WHITE +
Style.BRIGHT + name + Style.RESET_ALL + desc)
# Print the command names and description using a json file
def print_commands_norm():
with open('cli_wallet_commands.json') as f:
data = json.load(f)
for key, value in data.items():
if key == "wrapper_commands":
break
print_command(key, value)
def print_commands_wrapper():
with open('cli_wallet_commands.json') as f:
data = json.load(f)
for key in data["wrapper_commands"]:
print_command(key, data["wrapper_commands"][key])
def password_encrypt(
message: bytes,
password: str,
iterations: int = iterations) -> bytes:
salt = secrets.token_bytes(16)
key = _derive_key(
password.encode(),
salt,
iterations)
return b64e(
b'%b%b%b' % (
salt,
iterations.to_bytes(4, 'big'),
b64d(Fernet(key).encrypt(message))))
def password_decrypt(
token: bytes,
password: str) -> bytes:
decoded = b64d(token)
salt, iterations, token = decoded[:16], decoded[16:20], b64e(decoded[20:])
iterations = int.from_bytes(iterations, 'big')
key = _derive_key(
password.encode(),
salt,
iterations)
return Fernet(key).decrypt(token)
# If CTRL+C or SIGINT received, send CLOSE request to server in order to exit gracefully.
def handler(signal_received, frame):
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:
try: # Try to connect
s = websocket.create_connection(WS_URI)
s.settimeout(timeout)
SERVER_VER = s.recv().rstrip("\n")
# Use request to grab data from raw github file
jsonapi = requests.get(
"https://raw.githubusercontent.com/"
+ "revoxhere/"
+ "duco-statistics/master/api.json",
data=None)
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 Exception as e: # If it wasn't, display a message
print(e)
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)
except:
print(Style.RESET_ALL
+ Fore.RED +
" Cannot receive pool address and IP."
+ "\nExiting in 15 seconds.")
time.sleep(15)
os._exit(1)
def reconnect():
while True:
try: # Try to connect
s = websocket.create_connection(WS_URI)
s.settimeout(timeout)
SERVER_VER = s.recv().rstrip("\n")
# Use request to grab data from raw github file
jsonapi = requests.get(
"https://raw.githubusercontent.com/"
+ "revoxhere/"
+ "duco-statistics/master/api.json",
data=None)
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
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:
return s
while True:
title("Duino-Coin CLI Wallet")
if not Path("CLIWallet_config.cfg").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")
try:
int(choice)
except ValueError:
print("Error, value must be numeric")
if int(choice) <= 1:
username = input(
Style.RESET_ALL
+ Fore.YELLOW
+ "Enter your username: "
+ Style.BRIGHT)
password = getpass.getpass(prompt=Style.RESET_ALL
+ Fore.YELLOW
+ "Enter your password: "
+ Style.BRIGHT,
stream=None)
server_timeout = True
while server_timeout:
try:
s.send(bytes(
"LOGI,"
+ str(username)
+ ","
+ str(password)
+ str(",placeholder"),
encoding="utf8"))
loginFeedback = s.recv().rstrip("\n").split(",")
server_timeout = False
if loginFeedback[0] == "OK":
print(Style.RESET_ALL
+ Fore.YELLOW
+ "Successfull login")
config['wallet'] = {
"username": username,
"password": b64encode(bytes(password, encoding="utf8")).decode("utf-8")}
config['wrapper'] = {"use_wrapper": "false"}
with open("CLIWallet_config.cfg", "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)
except socket.timeout:
server_timeout = True
if int(choice) == 2:
print(Style.RESET_ALL
+ Fore.YELLOW
+ "By registering a new account you agree to the"
+ " terms of service and privacy policy available at "
+ Fore.WHITE
+ "https://github.com/revoxhere/duino-coin#terms-of-usage"
+ Fore.YELLOW)
username = input(
Style.RESET_ALL
+ Fore.YELLOW
+ "Enter your username: "
+ Style.BRIGHT)
password = getpass.getpass(prompt=Style.RESET_ALL
+ Fore.YELLOW
+ "Enter your password: "
+ Style.BRIGHT,
stream=None)
pconfirm = getpass.getpass(prompt=Style.RESET_ALL
+ Fore.YELLOW
+ "Confirm your password: "
+ Style.BRIGHT,
stream=None)
email = input(
Style.RESET_ALL
+ Fore.YELLOW
+ "Enter your e-mail address: "
+ Style.BRIGHT)
if password == pconfirm:
while True:
s.send(bytes(
"REGI,"
+ str(username)
+ ","
+ str(password)
+ ","
+ str(email),
encoding="utf8"))
regiFeedback = s.recv().rstrip("\n").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
+ "Couldn't register new user, reason: "
+ Style.BRIGHT
+ str(regiFeedback[1]))
time.sleep(15)
os._exit(1)
if int(choice) >= 3:
os._exit(0)
else:
config.read("CLIWallet_config.cfg")
if config["wrapper"]["use_wrapper"] == "true" and tronpy_installed:
use_wrapper = True
if config["wrapper"]["use_custom_passphrase"] == "true":
passphrase = getpass.getpass(prompt=Style.RESET_ALL
+ Fore.YELLOW
+ "Passphrase for decrypting private key: "
+ Style.BRIGHT,
stream=None)
try:
priv_key = str(password_decrypt(
config["wrapper"]["priv_key"],
passphrase))[2:66]
except InvalidToken:
print("Invalid passphrase, disabling wrapper for this session")
use_wrapper = False
wrong_passphrase = True
else:
priv_key = str(password_decrypt(
config["wrapper"]["priv_key"],
config["wallet"]["password"]))[2:66]
pub_key = config["wrapper"]["pub_key"]
tron = tronpy.Tron()
wduco = tron.get_contract(
"TWYaXdxA12JywrUdou3PFD1fvx2PWjqK9U") # wDUCO contract
wbalance = wduco.functions.balanceOf(config["wrapper"]["pub_key"])
try:
trx_balance = tron.get_account_balance(
config["wrapper"]["pub_key"])
except:
trx_balance = 0
while True:
config.read("CLIWallet_config.cfg")
username = config["wallet"]["username"]
password = b64decode(config["wallet"]["password"]).decode("utf8")
s.send(bytes(
"LOGI,"
+ str(username)
+ ","
+ str(password)
+ str(",placeholder"),
encoding="utf8"))
loginFeedback = s.recv().rstrip("\n").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:
try:
s.send(bytes(
"BALA",
encoding="utf8"))
except:
s = reconnect()
if use_wrapper:
wbalance = float(wduco.functions.balanceOf(pub_key))/10**6
try:
trx_balance = tron.get_account_balance(pub_key)
except:
trx_balance = 0
try:
balance = round(float(s.recv().rstrip("\n")), 8)
balanceusd = round(float(balance) * float(ducofiat), 6)
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
+ "DUCO price: "
+ Style.BRIGHT
+ str(ducofiat)
+ " USD")
if use_wrapper:
print(Style.RESET_ALL
+ Fore.YELLOW
+ "You have "
+ Style.BRIGHT
+ str(wbalance)
+ " wDUCO")
pendingbalance = float(
wduco.functions.pendingWithdrawals(
pub_key,
username))/(10**6)
if pendingbalance > 0:
print(Style.RESET_ALL
+ Fore.YELLOW
+ "Pending unwraps "
+ Style.BRIGHT
+ str(pendingbalance)
+ " wDUCO")
print(Style.RESET_ALL
+ Fore.YELLOW
+ "Tron address for receiving: "
+ Style.BRIGHT
+ str(pub_key))
print(Style.RESET_ALL
+ Fore.YELLOW
+ "TRX balance (useful for fees): "
+ Style.BRIGHT
+ str(trx_balance))
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)
try:
amount = float(input(
Style.RESET_ALL
+ Fore.WHITE
+ "Enter amount to transfer: "
+ Style.BRIGHT))
except ValueError:
print("Amount should be numeric... aborting")
continue
s.send(bytes(
"SEND,-,"
+ str(recipient)
+ ","
+ str(amount),
encoding="utf8"))
while True:
message = s.recv().rstrip("\n")
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().rstrip("\n")
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!"
+ Style.RESET_ALL)
try:
s.send(bytes("CLOSE", encoding="utf8"))
except:
pass
os._exit(0)
elif command == "wrapperconf": # wrapper config
config.read("CLIWallet_config.cfg")
if not config["wrapper"]["use_wrapper"] == "true" and tronpy_installed:
print(Style.RESET_ALL
+ Fore.WHITE
+ "Select an option")
try:
choice = int(
input("1 - Generate a new key\n2 - Import a key \n3 - Cancel\n"))
if choice <= 1:
priv_key = str(PrivateKey.random())
pub_key = PrivateKey(bytes.fromhex(
priv_key)).public_key.to_base58check_address()
print(
"How do you want to encrypt private key"
+ " within the config file?")
incorrect_value = True
while incorrect_value:
try:
encryption_choice = int(input(
"1 - encrypt it using DUCO password"
+ "\n2 - use your custom passphrase (more secure)"))
incorrect_value = False
except ValueError:
print("Error, value should be numeric")
incorrect_value = True
if encryption_choice <= 1:
config['wrapper'] = {
"use_wrapper": "true",
"priv_key": str(password_encrypt(
priv_key.encode(),
password).decode()),
"pub_key": pub_key,
"use_custom_passphrase": "false"}
# Write data to file
with open("CLIWallet_config.cfg", "w") as configfile:
config.write(configfile)
print("Success!")
elif encryption_choice >= 2:
passphrase = input(
"Input your passphrase "
+ "for encrypting private key: ")
config['wrapper'] = {
"use_wrapper": "true",
"priv_key": str(password_encrypt(
priv_key.encode(),
passphrase).decode()),
"pub_key": pub_key,
"use_custom_passphrase": "true"}
# Write data to file
with open("CLIWallet_config.cfg", "w") as configfile:
config.write(configfile)
print("Success !")
elif choice == 2:
priv_key = input("Input your own private key: ")
try:
pub_key = PrivateKey(bytes.fromhex(
priv_key)).public_key.to_base58check_address()
print(
"How do you want to encrypt private key "
+ "within config file?")
incorrect_value = True
while incorrect_value:
try:
encryption_choice = int(input(
"1 - encrypt it using DUCO password"
+ "\n2 - use your custom passphrase (more secure)\n"))
incorrect_value = False
except ValueError:
print("Error, should be numeric")
incorrect_value = True
if encryption_choice <= 1:
config['wrapper'] = {
"use_wrapper": "true",
"priv_key": str(password_encrypt(
priv_key.encode(),
password).decode()),
"pub_key": pub_key,
"use_custom_passphrase": "false"}
# Write data to file
with open("CLIWallet_config.cfg", "w") as configfile:
config.write(configfile)
print("Success!")
elif encryption_choice >= 2:
passphrase = input("Input your passphrase "
+ "for encrypting private key: ")
config['wrapper'] = {
"use_wrapper": "true",
"priv_key": str(password_encrypt(
priv_key.encode(),
passphrase).decode()),
"pub_key": pub_key,
"use_custom_passphrase": "true"}
# Write data to file
with open("CLIWallet_config.cfg", "w") as configfile:
config.write(configfile)
print("Success !")
except ValueError:
print("Incorrect key was provided")
else:
print("Cancelled...")
except ValueError:
print(Style.RESET_ALL
+ Fore.WHITE
+ "Incorrect value was provided"
+ Style.BRIGHT)
elif not tronpy_installed:
print(now.strftime(
"%H:%M:%S ")
+ "Tronpy is not installed. "
+ "Please install it using: python3 -m pip install tronpy.")
elif command == "wrap":
if use_wrapper:
try:
amount = float(input(
Style.RESET_ALL
+ Fore.WHITE
+ "Enter amount to wrap (minimum is 10): "
+ Style.BRIGHT))
except ValueError:
print("NO, Amount should be numeric... aborting")
continue
try:
s.send(bytes("BALA", encoding="utf8"))
balance = round(float(s.recv().rstrip("\n")), 8)
except:
s = reconnect()
if float(amount) >= 10 and float(amount) <= balance:
tron_address = input(
Style.RESET_ALL
+ Fore.WHITE
+ "Enter tron address or leave blank to choose local wallet: "
+ Style.BRIGHT)
if tron_address == "":
tron_address = pub_key
s.send(bytes(
"WRAP,"
+ str(amount)
+ ","
+ str(tron_address),
encoding='utf8'))
elif float(amount) < 10 and not float(amount) > balance:
print("Error, minimum amount is 10 DUCO")
else:
print("Error, unsufficient balance")
elif wrong_passphrase:
print("Wrapper disabled, you entered a wrong passphrase")
else:
print("Wrapper disabled, configure it using `wrapperconf`")
elif command == "unwrap":
if use_wrapper:
pendingvalues = wduco.functions.pendingWithdrawals(
pub_key, username)
txn_success = False # transaction wasn't initiated, but variable should be declared
try:
amount = float(
input(Style.RESET_ALL
+ Fore.WHITE
+ "Enter amount to unwrap: "
+ Style.BRIGHT))
except ValueError:
print("Value should be numeric... aborting")
continue
if int(float(amount)*10**6) >= pendingvalues:
toInit = int(float(amount)*10**6)-pendingvalues
else:
toInit = amount*10**6
if toInit > 0:
txn = wduco.functions.initiateWithdraw(username, toInit).with_owner(pub_key).fee_limit(5_000_000).build().sign(PrivateKey(bytes.fromhex(priv_key)))
print("Initiating unwrap...\nTXid :", txn.txid)
txn = txn.broadcast()
txnfeedback = txn.result()
if txnfeedback:
txn_success = True
else:
txn_success = False
if amount <= pendingvalues:
print("Amount is over pending values, "
+ "using pending values to save txn fees")
if txn_success or amount <= pendingvalues:
s.send(bytes(
"UNWRAP,"
+ str(amount)
+ ","
+ str(pub_key),
encoding='utf8'))
else:
print("There was an error while initiating unwrap, aborting")
if amount <= pendingvalues:
print(
"Amount is over pending values, using pending values in order to save txn fees")
elif wrong_passphrase:
print("Wrapper disabled, you entered a wrong passphrase")
else:
print("Wrapper disabled, configure it using `wrapperconf`")
elif command == "cancelunwraps":
if use_wrapper:
txn = wduco.functions.cancelWithdrawals(pub_key, username).with_owner(pub_key).fee_limit(5_000_000).build().sign(PrivateKey(bytes.fromhex(priv_key)))
print("Transaction sent, txid :", txn.txid)
txn = txn.broadcast()
if txn.result():
print("Success")
else:
print("Failed, you should have enough energy or trx")
elif wrong_passphrase:
print("Wrapper disabled, you entered a wrong passphrase")
else:
print("Wrapper disabled, configure it using `wrapperconf`")
elif command == "finishunwraps":
if use_wrapper:
pendingvalues = float(
wduco.functions.pendingWithdrawals(
pub_key,
username))/(10**6)
s.send(bytes(
"UNWRAP,"
+ str(pendingvalues)
+ ","
+ str(pub_key),
encoding='utf8'))
print("Finished unwrapping", str(pendingvalues), "DUCO")
elif wrong_passphrase:
print("Wrapper disabled, you entered a wrong passphrase")
else:
print("Wrapper disabled, configure it using `wrapperconf`")
elif command == "exportwrapkey":
if use_wrapper:
confirmation = input(
"Type YES for confirming export of private key : ")
if confirmation == "YES":
print("Private key:", priv_key)
else:
print("Cancelled, invalid confirmation")
elif wrong_passphrase:
print("Wrapper disabled, you entered a wrong passphrase")
else:
print("Wrapper disabled, configure it using `wrapperconf`")
elif command == "wsend":
if use_wrapper:
recipient = input(
Style.RESET_ALL
+ Fore.WHITE
+ "Enter recipients' TRON address: "
+ Style.BRIGHT)
try:
amount = float(input(
Style.RESET_ALL
+ Fore.WHITE
+ "Enter amount to transfer: "
+ Style.BRIGHT))
except ValueError:
print("Amount should be numeric... aborting")
continue
wbalance = float(wduco.functions.balanceOf(pub_key))/10**6
if float(amount) <= wbalance:
txn = wduco.functions.transfer(recipient, int(float(amount)*10**6)).with_owner(pub_key).fee_limit(5_000_000).build().sign(PrivateKey(bytes.fromhex(priv_key)))
txn = txn.broadcast()
print("Transaction submitted to TRON network"
+ "\nTXID:", txn.txid)
trontxresult = txn.wait()
if trontxresult:
print("Successful transaction")
else:
print("Error while confirming transaction")
elif wrong_passphrase:
print("Wrapper disabled, you entered a wrong passphrase")
else:
print("Wrapper disabled, configure it using `wrapperconf`")
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
+ "Server version :"
+ Style.BRIGHT
+ str(SERVER_VER)
+ Style.RESET_ALL)
if float(SERVER_VER) > VER:
print(Style.RESET_ALL
+ Fore.YELLOW
+ "Server is on version "
+ Fore.WHITE
+ Style.BRIGHT
+ SERVER_VER
+ Fore.YELLOW
+ Style.RESET_ALL
+ ", but client is on version "
+ Style.BRIGHT
+ Fore.WHITE
+ str(VER)
+ Style.RESET_ALL
+ Fore.YELLOW
+ ", you should consider downloading last release")
else:
print(Style.RESET_ALL
+ Fore.WHITE
+ "Client is up-to-date")
elif command == "logout":
os.remove("CLIWallet_config.cfg")
os.execl(sys.executable, sys.executable, *sys.argv)
elif command == "donate":
print(Style.RESET_ALL
+ Fore.BLUE
+ Style.BRIGHT
+ "Feel free of donating for helping to maintain DUCO and wDUCO "
+ "(wrapping/unwrapping involves fees for maintainers)")
print(Style.RESET_ALL
+ Fore.YELLOW