whallets_cli.py 5.5 KB

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