whallets_cli.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 remove_wallet():
  60. """Removes wallet from the wallet dictionary"""
  61. # This function needs to be developped
  62. def _get_inputs():
  63. """Get infor to add a new wallet"""
  64. ntw_i = _check_network()
  65. items_dict = {
  66. "username":" ",
  67. "twitter address":" ",
  68. "ENS":" ",
  69. "info/note":" ",
  70. "address": []
  71. }
  72. wallet = []
  73. adresses = []
  74. for item, value in items_dict:
  75. x = input(tc._prompt_info(item))
  76. new_item = check_wallet_item(ntw_i,x)
  77. if item == 'address':
  78. x = input(tc._ask_more_wallets())
  79. if x == '1':
  80. wallet.append(item)
  81. name = input(tc._prompt_name())
  82. addr = input(tc._prompt_address())
  83. inf = input(tc._prompt_info())
  84. twtr = input(tc._prompt_twitter())
  85. ens = input(tc._prompt_ens())
  86. return ntw_i, name, addr, inf, twtr, ens
  87. def _check_network():
  88. """Check if the existing network"""
  89. print(tabulate(tc._choose_network()))
  90. ntw = int(input(tc._menu_choice()))
  91. i = ntw - 1
  92. if i != 0:
  93. print(tc._missing_operation())
  94. _new_choice()
  95. else:
  96. return i
  97. def check_wallet_item(ntw_i,x):
  98. """
  99. Scans through wallets to check if a new wallet is not already in the
  100. database
  101. """
  102. dict = get_wallets()[ntw_i]
  103. for key, value in dict.items():
  104. username = key
  105. wallets = value['wallets']
  106. info = value['info']
  107. twitter = value['twitter']
  108. ens_saved = value['ens']
  109. if x == username:
  110. new_item = _correct_item(x,'username')
  111. elif x in wallets.values():
  112. new_item = _correct_item(x,'wallet address')
  113. elif x == twitter:
  114. new_item = _correct_item(x,'twitter address')
  115. elif x == ens_saved:
  116. new_item = _correct_item(x, 'ENS')
  117. else:
  118. new_item = x
  119. return new_item
  120. # else:
  121. # print(tc._display_wallet_check_result(y)[1])
  122. # new_item = True
  123. #
  124. # wlt = [ntw_i, name, addr, twtr, ens, inf]
  125. #
  126. # if new_item != True:
  127. # wlt[x] = new_item
  128. #
  129. # return wlt
  130. def _correct_item(x,z):
  131. """Allows user to rewrite an exisitng item in the wallet"""
  132. a = (input(tc._display_wallet_check_result(z)[0]))
  133. if a == '1':
  134. new_item = x
  135. elif a == '2':
  136. new_item = input(tc._enter_new_info(z))
  137. return new_item
  138. def get_wallets():
  139. """ Gets the info from the dictionary """
  140. filename = 'wallets_dict.json'
  141. with open(filename) as f:
  142. all_wallets = json.load(f)
  143. evm_wallets = all_wallets['evm_wallets']
  144. spl_wallets = all_wallets['spl_wallets']
  145. return evm_wallets, spl_wallets,
  146. def display_wallets(chain):
  147. """Displays the wallets according the given chain"""
  148. # Chains available 'evm','spl'
  149. # evm shows all the forks
  150. i = _chain_index(chain)
  151. dict = get_wallets()[i]
  152. print(f"\n\n{chain.upper()} WALLETS:")
  153. for key, value in dict.items():
  154. user = key
  155. info = value['info']
  156. twitter = value['twitter']
  157. wallets = {}
  158. wlts = value['wallets']
  159. for x, y in wlts.items():
  160. wallets[x] = y
  161. print(f"\nUser: {user}")
  162. print(f"Info: {info}")
  163. print(f"Twitter: {twitter}")
  164. for wlt,inf in wallets.items():
  165. print(f"Wallets: {wlt}")
  166. print(f"Address: {inf['address']}")
  167. print(f"Active networks:")
  168. networks = inf['networks']
  169. networks_str = ', '.join(networks)
  170. print(networks_str)
  171. def _chain_index(chain):
  172. """Asigns an index based on given parameter of the chain"""
  173. if chain.lower() == 'evm':
  174. i = 0
  175. elif chain.lower() == 'spl':
  176. i = 1
  177. return i
  178. def display_all_wallets():
  179. display_wallets('evm')
  180. display_wallets('spl')
  181. # Run the program
  182. cli_main()