whallets_cli.py 4.9 KB

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