whallets_cli.py 5.5 KB

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