whallets_cli.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. """CLI for users to manage the wallet database and run research functions"""
  2. #TODO:
  3. # 1) Learn operating Blockscan APIs
  4. # 2) Program functions pulling APIs data based on users interaction
  5. # 3) CLI function to easy add new wallets and other data to the wallets_dict.json
  6. # - loop through each info separately
  7. # - ask to re-add info if matches
  8. # 4) Prints options for the user (-h)
  9. import json
  10. from tabulate import tabulate
  11. import texts_cli as tc
  12. print(tc._welcome_message())
  13. def cli_main():
  14. """Main program for the cli"""
  15. print(tabulate(tc._main_menu()))
  16. main_menu_choice()
  17. def main_menu_choice():
  18. """main cli operations choice"""
  19. choice = input(tc._menu_choice())
  20. if choice == '1':
  21. add_wallet()
  22. elif choice == '2':
  23. print(tc._missing_operation())
  24. _new_choice()
  25. elif choice == '3':
  26. display_all_wallets()
  27. elif choice == '4':
  28. print(tc._missing_operation())
  29. _new_choice()
  30. elif choice == '5':
  31. print(tc._missing_operation())
  32. _new_choice()
  33. elif choice.lower() == "q":
  34. quit()
  35. else:
  36. _new_choice()
  37. _new_choice()
  38. def _new_choice():
  39. """Offer user a new choice"""
  40. x = 1
  41. while x < 3:
  42. chc = input(tc._ask_new_choice())
  43. if chc == '1':
  44. cli_main()
  45. elif chc == '2':
  46. quit()
  47. else:
  48. x += 1
  49. else:
  50. quit()
  51. def add_wallet():
  52. """A function to add wallet to the wallets_dict.json"""
  53. chain, name, addr, inf, twtr, ens = _get_inputs()
  54. msg = tc._check_wallet_result(chain, name, addr, twtr, ens)
  55. print(msg)
  56. def remove_wallet():
  57. """Removes wallet from the wallet dictionary"""
  58. # This function needs to be developped
  59. def _get_inputs():
  60. """Get infor to add a new wallet"""
  61. ntw = int(input(tabulate(tc._choose_network())))
  62. name = input(tc._prompt_name())
  63. addr = input(tc._prompt_address())
  64. inf = input(tc._prompt_info())
  65. twtr = input(tc._prompt_twitter())
  66. ens = input(tc._prompt_ens())
  67. return ntw, name, addr, inf, twtr, ens
  68. def check_wallet(ntw, name, addr, twtr, ens):
  69. """
  70. Scans through wallets to check if a new wallet is not already in the
  71. database
  72. """
  73. i = ntw - 1
  74. if i != 0:
  75. print(tc._missing_operation())
  76. _new_choice()
  77. else:
  78. dict = get_wallets()[i]
  79. for key, value in dict.items():
  80. user = key
  81. wallets = value['wallets']
  82. info = value['info']
  83. twitter = value['twitter']
  84. ens_saved = value['ens']
  85. if user == name:
  86. x = 0
  87. elif addr in wallets.values():
  88. x = 1
  89. elif twtr == twitter:
  90. x = 2
  91. elif ens == ens_saved:
  92. x = 3
  93. else:
  94. x = 4
  95. return x
  96. def get_wallets():
  97. """ Gets the info from the dictionary """
  98. filename = 'wallets_dict.json'
  99. with open(filename) as f:
  100. all_wallets = json.load(f)
  101. evm_wallets = all_wallets['evm_wallets']
  102. spl_wallets = all_wallets['spl_wallets']
  103. return evm_wallets, spl_wallets,
  104. def display_wallets(chain):
  105. """Displays the wallets according the given chain"""
  106. # Chains available 'evm','spl'
  107. # evm shows all the forks
  108. i = _chain_index(chain)
  109. dict = get_wallets()[i]
  110. print(f"\n\n{chain.upper()} WALLETS:")
  111. for key, value in dict.items():
  112. user = key
  113. info = value['info']
  114. twitter = value['twitter']
  115. wallets = {}
  116. wlts = value['wallets']
  117. for x, y in wlts.items():
  118. wallets[x] = y
  119. print(f"\nUser: {user}")
  120. print(f"Info: {info}")
  121. print(f"Twitter: {twitter}")
  122. for wlt,inf in wallets.items():
  123. print(f"Wallets: {wlt}")
  124. print(f"Address: {inf['address']}")
  125. print(f"Active networks:")
  126. networks = inf['networks']
  127. networks_str = ', '.join(networks)
  128. print(networks_str)
  129. def _chain_index(chain):
  130. """Asigns an index based on given parameter of the chain"""
  131. if chain.lower() == 'evm':
  132. i = 0
  133. elif chain.lower() == 'spl':
  134. i = 1
  135. return i
  136. def display_all_wallets():
  137. display_wallets('evm')
  138. display_wallets('spl')
  139. # Run the program
  140. cli_main()