whallets_cli.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. # - ask for additional wallets for the same user
  9. # 4) Prints options for the user (-h)
  10. import json
  11. from tabulate import tabulate
  12. import texts_cli as tc
  13. print(tc._welcome_message())
  14. def cli_main():
  15. """Main program for the cli"""
  16. print(tabulate(tc._main_menu()))
  17. main_menu_choice()
  18. def main_menu_choice():
  19. """main cli operations choice"""
  20. choice = input(tc._menu_choice())
  21. if choice == '1':
  22. add_wallet()
  23. elif choice == '2':
  24. print(tc._missing_operation())
  25. _new_choice()
  26. elif choice == '3':
  27. display_all_wallets()
  28. elif choice == '4':
  29. print(tc._missing_operation())
  30. _new_choice()
  31. elif choice == '5':
  32. print(tc._missing_operation())
  33. _new_choice()
  34. elif choice.lower() == "q":
  35. quit()
  36. else:
  37. _new_choice()
  38. _new_choice()
  39. def _new_choice():
  40. """Offer user a new choice"""
  41. x = 1
  42. while x < 3:
  43. chc = input(tc._ask_new_choice())
  44. if chc == '1':
  45. cli_main()
  46. elif chc == '2':
  47. quit()
  48. else:
  49. x += 1
  50. else:
  51. quit()
  52. def add_wallet():
  53. """A function to add wallet to the wallets_dict.json"""
  54. ntw_i, name, addr, inf, twtr, ens = _get_inputs()
  55. wlt = check_wallet(ntw_i, name, addr, twtr, ens, inf)
  56. ntw_i, name, addr, twtr, ens, inf = \
  57. wlt[0], wlt[1], wlt[2], wlt[3], wlt[4], wlt[5]
  58. print(tabulate(tc._save_wallet_confirm(ntw_i, name, addr, twtr, ens, inf)))
  59. def _correct_item(y):
  60. """Allows user to rewrite an exisitng item in the wallet"""
  61. z = (input(tc._display_wallet_check_result(y)[0]))
  62. if z =='1':
  63. print(tc._display_wallet_check_result(y)[1])
  64. return True
  65. elif z == '2':
  66. new_item = input(tc._enter_new_info(y))
  67. return new_item
  68. def remove_wallet():
  69. """Removes wallet from the wallet dictionary"""
  70. # This function needs to be developped
  71. def _get_inputs():
  72. """Get infor to add a new wallet"""
  73. ntw_i = _check_network()
  74. name = input(tc._prompt_name())
  75. addr = input(tc._prompt_address())
  76. inf = input(tc._prompt_info())
  77. twtr = input(tc._prompt_twitter())
  78. ens = input(tc._prompt_ens())
  79. return ntw_i, name, addr, inf, twtr, ens
  80. def _check_network():
  81. """Check if the existing network"""
  82. print(tabulate(tc._choose_network()))
  83. ntw = int(input(tc._menu_choice()))
  84. i = ntw - 1
  85. if i != 0:
  86. print(tc._missing_operation())
  87. _new_choice()
  88. else:
  89. return i
  90. def check_wallet(ntw_i,name, addr, twtr, ens, inf):
  91. """
  92. Scans through wallets to check if a new wallet is not already in the
  93. database
  94. """
  95. i = ntw_i
  96. dict = get_wallets()[i]
  97. for key, value in dict.items():
  98. user = key
  99. wallets = value['wallets']
  100. info = value['info']
  101. twitter = value['twitter']
  102. ens_saved = value['ens']
  103. if user == name:
  104. x = [1,'name']
  105. elif addr in wallets.values():
  106. x = [2, 'address']
  107. elif twtr == twitter:
  108. x = [3, 'twitter']
  109. elif ens == ens_saved:
  110. x = [4, 'ENS']
  111. else:
  112. x = [5, "none"]
  113. y = x[1]
  114. x = x[0]
  115. if x != 5:
  116. new_item = _correct_item(y)
  117. else:
  118. print(tc._display_wallet_check_result(y)[1])
  119. new_item = True
  120. wlt = [ntw_i, name, addr, twtr, ens, inf]
  121. if new_item != True:
  122. wlt[x] = new_item
  123. return wlt
  124. def get_wallets():
  125. """ Gets the info from the dictionary """
  126. filename = 'wallets_dict.json'
  127. with open(filename) as f:
  128. all_wallets = json.load(f)
  129. evm_wallets = all_wallets['evm_wallets']
  130. spl_wallets = all_wallets['spl_wallets']
  131. return evm_wallets, spl_wallets,
  132. def display_wallets(chain):
  133. """Displays the wallets according the given chain"""
  134. # Chains available 'evm','spl'
  135. # evm shows all the forks
  136. i = _chain_index(chain)
  137. dict = get_wallets()[i]
  138. print(f"\n\n{chain.upper()} WALLETS:")
  139. for key, value in dict.items():
  140. user = key
  141. info = value['info']
  142. twitter = value['twitter']
  143. wallets = {}
  144. wlts = value['wallets']
  145. for x, y in wlts.items():
  146. wallets[x] = y
  147. print(f"\nUser: {user}")
  148. print(f"Info: {info}")
  149. print(f"Twitter: {twitter}")
  150. for wlt,inf in wallets.items():
  151. print(f"Wallets: {wlt}")
  152. print(f"Address: {inf['address']}")
  153. print(f"Active networks:")
  154. networks = inf['networks']
  155. networks_str = ', '.join(networks)
  156. print(networks_str)
  157. def _chain_index(chain):
  158. """Asigns an index based on given parameter of the chain"""
  159. if chain.lower() == 'evm':
  160. i = 0
  161. elif chain.lower() == 'spl':
  162. i = 1
  163. return i
  164. def display_all_wallets():
  165. display_wallets('evm')
  166. display_wallets('spl')
  167. # Run the program
  168. cli_main()