whallets_cli.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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, addresses, ntw_i = get_inputs()
  56. print(tabulate(tc._save_wallet_confirm(addresses, new_wallet)))
  57. confirm_entry(addresses, new_wallet)
  58. new_wallet_dictionary = refactor_wallet(addresses, new_wallet)
  59. save_wallet(ntw_i, new_wallet_dictionary)
  60. def confirm_entry(addresses, new_wallet):
  61. """Preview the new wallet and confirm saving it"""
  62. x = input(tc._confirm_entry()[0])
  63. if x == '1':
  64. # new_wallet_dict = refactor_wallet(addresses, **new_wallet)
  65. # save_wallet(**new_wallet_dict)
  66. print(tc._confirm_entry()[1])
  67. refactor_wallet(addresses, new_wallet)
  68. x = input(tc._confirm_entry()[2])
  69. if x == '1':
  70. add_wallet()
  71. def save_wallet(ntw_i, new_wallet_dictionary):
  72. """Saves the wallet into the database/dictionary and informs the user"""
  73. filename = 'wallets_dict.json'
  74. with open(filename) as f:
  75. all_wallets = json.load(f)
  76. dict = all_wallets[ntw_i]
  77. all_wallets[dict].update(new_wallet_dictionary)
  78. json.dump(f)
  79. def refactor_wallet(addresses, new_wallet):
  80. """Refactor the wallet items to the wallet_dict format"""
  81. username = new_wallet["username"]
  82. twtr = new_wallet["twitter address"]
  83. ens = new_wallet["ENS"]
  84. info = new_wallet["info/note"]
  85. new_wallet_dictionary = {username: {
  86. "twitter": twtr,
  87. "info": info,
  88. "ens":ens,
  89. "wallets": {}
  90. }
  91. }
  92. for idx, addr in enumerate(addresses):
  93. wallet = {f"wallet_{idx}":{
  94. "address":f"{addr}",
  95. "networks":[
  96. "erc"
  97. # need to add a code how to add networks
  98. ]
  99. }
  100. }
  101. new_wallet_dictionary[username]["wallets"].update(wallet)
  102. print(f"\n\n\n{new_wallet_dictionary}")
  103. return new_wallet_dictionary
  104. def remove_wallet():
  105. """Removes wallet from the wallet dictionary"""
  106. # This function needs to be developed
  107. def get_inputs():
  108. """Get infor to add a new wallet"""
  109. ntw_i = _check_network()
  110. network = tc._return_network(ntw_i)
  111. addresses = []
  112. new_wallet = {
  113. "username":" ",
  114. "twitter address":" ",
  115. "ENS":" ",
  116. "info/note":" ",
  117. "address": addresses
  118. }
  119. for item, value in new_wallet.items():
  120. x = input(tc._prompt_new_info(item))
  121. new_item = check_wallet_item(ntw_i,x)
  122. if item == 'address':
  123. addresses.append(new_item)
  124. y = input(tc._ask_more_wallets()[0])
  125. while y == '1':
  126. new_item = input(tc._ask_more_wallets()[1])
  127. addresses.append(new_item)
  128. y = input(tc._ask_more_wallets()[0])
  129. new_wallet[item] = new_item
  130. new_wallet["Network"] = network
  131. return new_wallet, addresses, ntw_i
  132. def _check_network():
  133. """Check if the existing network"""
  134. print(tabulate(tc._choose_network()))
  135. ntw = int(input(tc._menu_choice()))
  136. i = ntw - 1
  137. if i != 0:
  138. print(tc._missing_operation())
  139. _new_choice()
  140. else:
  141. return i
  142. def check_wallet_item(ntw_i,x):
  143. """
  144. Scans through wallets to check if a new wallet is not already in the
  145. database
  146. """
  147. dict = get_wallets()[ntw_i]
  148. for key, value in dict.items():
  149. username = key
  150. wallets = value['wallets']
  151. info = value['info']
  152. twitter = value['twitter']
  153. ens_saved = value['ens']
  154. if x == username:
  155. new_item = _correct_item(x,'username')
  156. elif x in wallets.values():
  157. new_item = _correct_item(x,'wallet address')
  158. elif x == twitter:
  159. new_item = _correct_item(x,'twitter address')
  160. elif x == ens_saved:
  161. new_item = _correct_item(x, 'ENS')
  162. else:
  163. new_item = x
  164. return new_item
  165. def _correct_item(x,z):
  166. """Allows user to rewrite an exisitng item in the wallet"""
  167. a = '2'
  168. while a == '2':
  169. a = (input(tc._display_wallet_check_result(z)[0]))
  170. if a == '1':
  171. new_item = x
  172. elif a == '2':
  173. new_item = input(tc._enter_new_info(z))
  174. if new_item == x:
  175. a = '2'
  176. else:
  177. new_item = x
  178. break
  179. return new_item
  180. def get_wallets():
  181. """ Gets the info from the dictionary """
  182. filename = 'wallets_dict.json'
  183. with open(filename) as f:
  184. all_wallets = json.load(f)
  185. evm_wallets = all_wallets['evm_wallets']
  186. spl_wallets = all_wallets['spl_wallets']
  187. return evm_wallets, spl_wallets,
  188. def display_wallets(chain):
  189. """Displays the wallets according the given chain"""
  190. # Chains available 'evm','spl'
  191. # evm shows all the forks
  192. i = _chain_index(chain)
  193. dict = get_wallets()[i]
  194. print(f"\n\n{chain.upper()} WALLETS:")
  195. for key, value in dict.items():
  196. user = key
  197. info = value['info']
  198. twitter = value['twitter']
  199. wallets = {}
  200. wlts = value['wallets']
  201. for x, y in wlts.items():
  202. wallets[x] = y
  203. print(f"\nUser: {user}")
  204. print(f"Info: {info}")
  205. print(f"Twitter: {twitter}")
  206. for wlt,inf in wallets.items():
  207. print(f"Wallets: {wlt}")
  208. print(f"Address: {inf['address']}")
  209. print(f"Active networks:")
  210. networks = inf['networks']
  211. networks_str = ', '.join(networks)
  212. print(networks_str)
  213. def _chain_index(chain):
  214. """Asigns an index based on given parameter of the chain"""
  215. if chain.lower() == 'evm':
  216. i = 0
  217. elif chain.lower() == 'spl':
  218. i = 1
  219. return i
  220. def display_all_wallets():
  221. display_wallets('evm')
  222. display_wallets('spl')
  223. # Run the program
  224. cli_main()