whallets_cli.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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(ntw_i, addresses, new_wallet)
  58. def confirm_entry(ntw_i, addresses, new_wallet):
  59. """Preview the new wallet and confirm saving it"""
  60. x = input(tc._confirm_entry()[0])
  61. if x == '1':
  62. # new_wallet_dict = refactor_wallet(addresses, **new_wallet)
  63. # save_wallet(**new_wallet_dict)
  64. print(tc._confirm_entry()[1])
  65. new_wallet_dictionary = refactor_wallet(addresses, new_wallet)
  66. save_wallet(ntw_i, new_wallet_dictionary)
  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_0 = all_wallets['evm_wallets']
  77. dict_1 = all_wallets['spl_wallets']
  78. dicts = [dict_0,dict_1]
  79. dict = dicts[ntw_i]
  80. dict.update(new_wallet_dictionary)
  81. with open(filename, 'w') as f:
  82. json.dump(all_wallets,f, indent=4)
  83. def refactor_wallet(addresses, new_wallet):
  84. """Refactor the wallet items to the wallet_dict format"""
  85. username = new_wallet["username"]
  86. twtr = new_wallet["twitter address"]
  87. ens = new_wallet["ENS"]
  88. info = new_wallet["info/note"]
  89. new_wallet_dictionary = {username: {
  90. "twitter": twtr,
  91. "info": info,
  92. "ens":ens,
  93. "wallets": {}
  94. }
  95. }
  96. for idx, addr in enumerate(addresses):
  97. wallet = {f"wallet_{idx}":{
  98. "address":f"{addr}",
  99. "networks":[
  100. "erc"
  101. # need to add a code how to add networks
  102. ]
  103. }
  104. }
  105. new_wallet_dictionary[username]["wallets"].update(wallet)
  106. # print(f"\n\n\n{new_wallet_dictionary}")
  107. return new_wallet_dictionary
  108. def remove_wallet():
  109. """Removes wallet from the wallet dictionary"""
  110. # This function needs to be developed
  111. def get_inputs():
  112. """Get infor to add a new wallet"""
  113. ntw_i = _check_network()
  114. network = tc._return_network(ntw_i)
  115. addresses = []
  116. new_wallet = {
  117. "username":" ",
  118. "twitter address":" ",
  119. "ENS":" ",
  120. "info/note":" ",
  121. "address": addresses
  122. }
  123. for key, value in new_wallet.items():
  124. x = input(tc._prompt_new_info(key))
  125. new_item = check_wallet_item(ntw_i, x, key)
  126. if key == 'address':
  127. addresses.append(new_item)
  128. y = input(tc._ask_more_wallets()[0])
  129. while y == '1':
  130. new_item = input(tc._ask_more_wallets()[1])
  131. addresses.append(new_item)
  132. y = input(tc._ask_more_wallets()[0])
  133. new_wallet[key] = new_item
  134. new_wallet["Network"] = network
  135. return new_wallet, addresses, ntw_i
  136. def check_wallet_item(ntw_i,x,key,):
  137. """
  138. Scans through wallets to check if a new wallet is not already in the
  139. database
  140. """
  141. dict = get_wallets()[ntw_i]
  142. if x == "" or x == " ":
  143. new_item = x
  144. else:
  145. for a,b in dict.items():
  146. if x == a or x == b:
  147. # print("\n\na or b\n\n")
  148. new_item = _correct_item(x, key)
  149. for v in b.values():
  150. if x == v:
  151. # print("\n\nv\n\n")
  152. new_item = _correct_item(x, key)
  153. return new_item
  154. else:
  155. new_item = x
  156. return new_item
  157. # username = key
  158. # wallets = value['wallets']
  159. # info = value['info']
  160. # twitter = value['twitter']
  161. # ens_saved = value['ens']
  162. #
  163. # if x == username:
  164. # new_item = _correct_item(x,'username')
  165. # elif x in wallets.values():
  166. # new_item = _correct_item(x,'wallet address')
  167. # elif x == twitter:
  168. # new_item = _correct_item(x,'twitter address')
  169. # elif x == ens_saved:
  170. # new_item = _correct_item(x, 'ENS')
  171. # else:
  172. # new_item = x
  173. def _correct_item(x,key):
  174. """Allows user to rewrite an exisitng item in the wallet"""
  175. choice = input(tc._display_wallet_check_result(key)[0])
  176. if choice == '1':
  177. new_item = x
  178. elif choice == '2':
  179. new_item = input(tc._enter_new_info(key))
  180. return new_item
  181. def _check_network():
  182. """Check if the existing network"""
  183. print(tabulate(tc._choose_network()))
  184. ntw = int(input(tc._menu_choice()))
  185. i = ntw - 1
  186. if i != 0:
  187. print(tc._missing_operation())
  188. _new_choice()
  189. else:
  190. return i
  191. def get_wallets():
  192. """ Gets the info from the dictionary """
  193. filename = 'wallets_dict.json'
  194. with open(filename) as f:
  195. all_wallets = json.load(f)
  196. evm_wallets = all_wallets['evm_wallets']
  197. spl_wallets = all_wallets['spl_wallets']
  198. return evm_wallets, spl_wallets,
  199. def display_wallets(chain):
  200. """Displays the wallets according the given chain"""
  201. # Chains available 'evm','spl'
  202. # evm shows all the forks
  203. i = _chain_index(chain)
  204. dict = get_wallets()[i]
  205. print(f"\n\n{chain.upper()} WALLETS:")
  206. # for key, value in dict.items():
  207. # user = key
  208. # info = value['info']
  209. # twitter = value['twitter']
  210. # ens = value['ens']
  211. # wallets = {}
  212. # wlts = value['wallets']
  213. # for x, y in wlts.items():
  214. # wallets[x] = y
  215. #
  216. #
  217. # print(f"\nUser: {user}")
  218. # print(f"Info: {info}")
  219. # print(f"Twitter: {twitter}")
  220. # for wlt,inf in wallets.items():
  221. # print(f"Wallets: {wlt}")
  222. # print(f"Address: {inf['address']}")
  223. # print(f"Active networks:")
  224. # networks = inf['networks']
  225. # networks_str = ', '.join(networks)
  226. # print(networks_str)
  227. # table = []
  228. line_0 = [
  229. "INDEX", "USER", "TWITTER", "ENS", "INFO", "ADDRESSES", "NETWORKS"]
  230. line_ = ["===========", "===========", "===========", "===========",
  231. "===========", "===========", "==========="]
  232. table = [line_0, line_,]
  233. for key, value in dict.items():
  234. user = key
  235. info = value['info']
  236. twitter = value['twitter']
  237. ens = value['ens']
  238. wallets = {}
  239. wlts = value['wallets']
  240. for x, y in wlts.items():
  241. wallets[x] = y
  242. line = [user, ens, twitter, info]
  243. x = 1
  244. for wlt,inf in wallets.items():
  245. address = inf['address']
  246. networks = inf['networks']
  247. networks_str = ', '.join(networks)
  248. if x == 1:
  249. line.append(address)
  250. line.append(networks_str)
  251. x += 1
  252. else:
  253. line = [' ',' ',' ',' ',' ',address,networks_str]
  254. x += 1
  255. table.append(line)
  256. print(tabulate(table))
  257. def _chain_index(chain):
  258. """Asigns an index based on given parameter of the chain"""
  259. if chain.lower() == 'evm':
  260. i = 0
  261. elif chain.lower() == 'spl':
  262. i = 1
  263. return i
  264. def display_all_wallets():
  265. display_wallets('evm')
  266. display_wallets('spl')
  267. # Run the program
  268. if __name__ == '__main__':
  269. cli_main()