whallets_cli.py 5.4 KB

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