whallets_cli.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. # else:
  120. # print(tc._display_wallet_check_result(y)[1])
  121. # new_item = True
  122. #
  123. # wlt = [ntw_i, name, addr, twtr, ens, inf]
  124. #
  125. # if new_item != True:
  126. # wlt[x] = new_item
  127. #
  128. # return wlt
  129. def _correct_item(x,z):
  130. """Allows user to rewrite an exisitng item in the wallet"""
  131. a = (input(tc._display_wallet_check_result(z)[0]))
  132. if a == '1':
  133. new_item = x
  134. elif a == '2':
  135. new_item = input(tc._enter_new_info(z))
  136. return new_item
  137. def get_wallets():
  138. """ Gets the info from the dictionary """
  139. filename = 'wallets_dict.json'
  140. with open(filename) as f:
  141. all_wallets = json.load(f)
  142. evm_wallets = all_wallets['evm_wallets']
  143. spl_wallets = all_wallets['spl_wallets']
  144. return evm_wallets, spl_wallets,
  145. def display_wallets(chain):
  146. """Displays the wallets according the given chain"""
  147. # Chains available 'evm','spl'
  148. # evm shows all the forks
  149. i = _chain_index(chain)
  150. dict = get_wallets()[i]
  151. print(f"\n\n{chain.upper()} WALLETS:")
  152. for key, value in dict.items():
  153. user = key
  154. info = value['info']
  155. twitter = value['twitter']
  156. wallets = {}
  157. wlts = value['wallets']
  158. for x, y in wlts.items():
  159. wallets[x] = y
  160. print(f"\nUser: {user}")
  161. print(f"Info: {info}")
  162. print(f"Twitter: {twitter}")
  163. for wlt,inf in wallets.items():
  164. print(f"Wallets: {wlt}")
  165. print(f"Address: {inf['address']}")
  166. print(f"Active networks:")
  167. networks = inf['networks']
  168. networks_str = ', '.join(networks)
  169. print(networks_str)
  170. def _chain_index(chain):
  171. """Asigns an index based on given parameter of the chain"""
  172. if chain.lower() == 'evm':
  173. i = 0
  174. elif chain.lower() == 'spl':
  175. i = 1
  176. return i
  177. def display_all_wallets():
  178. display_wallets('evm')
  179. display_wallets('spl')
  180. # Run the program
  181. cli_main()