drk 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/env python
  2. import argparse
  3. import requests
  4. import json
  5. def arg_parser(client):
  6. #choices = ['wallet', 'cashierwallet', 'keypair', 'pub']
  7. parser = argparse.ArgumentParser(prog='drk',
  8. usage='%(prog)s [commands]',
  9. description="""DarkFi wallet
  10. command-line tool""")
  11. parser.add_argument("-k", "--key", action='store_true', help="Generate a new keypair")
  12. parser.add_argument("-i", "--info", action='store_true', help="Request info from daemon")
  13. parser.add_argument("-s", "--stop", action='store_true', help="Send a stop signal to the daemon")
  14. parser.add_argument("-n", "--new", action='store_true', help="Generate a new wallet")
  15. parser.add_argument("-o", "--hello", action='store_true', help="Say hello")
  16. parser.add_argument("-c", "--cash", action='store_true', help="Create a new cashier wallet")
  17. parser.add_argument("-q", "--ckeygen", action='store_true', help="Cash key gen")
  18. parser.add_argument("-x", "--cashkey", action='store_true', help="Print cashierkey")
  19. parser.add_argument("-t", "--test", action='store_true', help="Test path")
  20. args = parser.parse_args()
  21. if args.test:
  22. try:
  23. print("Testing path...")
  24. client.test_path(client.payload)
  25. except Exception:
  26. raise
  27. if args.ckeygen:
  28. try:
  29. print("Attempting to print cashier key...")
  30. client.ckeygen(client.payload)
  31. except Exception:
  32. raise
  33. if args.cashkey:
  34. try:
  35. print("Attempting to print cashier key...")
  36. client.cashkey(client.payload)
  37. except Exception:
  38. raise
  39. if args.key:
  40. try:
  41. print("Attemping to generate a new key pair...")
  42. client.key_gen(client.payload)
  43. except Exception:
  44. raise
  45. if args.new:
  46. try:
  47. print("Attemping to generate a new wallet...")
  48. client.new_wallet(client.payload)
  49. except Exception:
  50. raise
  51. if args.info:
  52. try:
  53. print("Info was entered")
  54. client.get_info(client.payload)
  55. print("Requesting daemon info...")
  56. except Exception:
  57. raise
  58. if args.stop:
  59. try:
  60. print("Stop was entered")
  61. client.stop(client.payload)
  62. print("Sending a stop signal...")
  63. except Exception:
  64. raise
  65. if args.hello:
  66. try:
  67. print("Hello was entered")
  68. client.say_hello(client.payload)
  69. except Exception:
  70. raise
  71. if args.cash:
  72. try:
  73. print("Cash was entered")
  74. client.new_cashier_wallet(client.payload)
  75. except Exception:
  76. raise
  77. #if args.wallet:
  78. # try:
  79. # print("This worked")
  80. # except Exception:
  81. # raise
  82. # TODO: refactor into async
  83. class DarkClient:
  84. # TODO: generate random ID (4 byte unsigned int) (rand range 0 - max size
  85. # uint32
  86. def __init__(self):
  87. self.url = "http://localhost:8000/"
  88. self.payload = {
  89. "method": [],
  90. "params": [],
  91. "jsonrpc": [],
  92. "id": [],
  93. }
  94. def ckeygen(self, payload):
  95. payload['method'] = "cash_key_gen"
  96. payload['jsonrpc'] = "2.0"
  97. payload['id'] = "0"
  98. ckeygen = self.__request(payload)
  99. print(ckeygen)
  100. def cashkey(self, payload):
  101. payload['method'] = "get_cash_key"
  102. payload['jsonrpc'] = "2.0"
  103. payload['id'] = "0"
  104. cashk = self.__request(payload)
  105. print(cashk)
  106. def test_path(self, payload):
  107. payload['method'] = "test_path"
  108. payload['jsonrpc'] = "2.0"
  109. payload['id'] = "0"
  110. test = self.__request(payload)
  111. print(test)
  112. def key_gen(self, payload):
  113. payload['method'] = "key_gen"
  114. payload['jsonrpc'] = "2.0"
  115. payload['id'] = "0"
  116. key = self.__request(payload)
  117. print(key)
  118. def get_info(self, payload):
  119. payload['method'] = "get_info"
  120. payload['jsonrpc'] = "2.0"
  121. payload['id'] = "0"
  122. info = self.__request(payload)
  123. print(info)
  124. def stop(self, payload):
  125. payload['method'] = "stop"
  126. payload['jsonrpc'] = "2.0"
  127. payload['id'] = "0"
  128. stop = self.__request(payload)
  129. print(stop)
  130. def say_hello(self, payload):
  131. payload['method'] = "say_hello"
  132. payload['jsonrpc'] = "2.0"
  133. payload['id'] = "0"
  134. hello = self.__request(payload)
  135. print(hello)
  136. def new_wallet(self, payload):
  137. payload['method'] = "new_wallet"
  138. payload['jsonrpc'] = "2.0"
  139. payload['id'] = "0"
  140. wallet = self.__request(payload)
  141. print(wallet)
  142. def new_cashier_wallet(self, payload):
  143. payload['method'] = "new_cashier_wallet"
  144. payload['jsonrpc'] = "2.0"
  145. payload['id'] = "0"
  146. wallet = self.__request(payload)
  147. print(wallet)
  148. def __request(self, payload):
  149. response = requests.post(self.url, json=payload).json()
  150. # print something better
  151. # parse into data structure
  152. print(response)
  153. assert response["jsonrpc"]
  154. if __name__ == "__main__":
  155. client = DarkClient()
  156. arg_parser(client)
  157. #rpc()
  158. ## Example echo method
  159. #payload = {
  160. # #"method:": args,
  161. # #"method": "stop",
  162. # "method": "get_info",
  163. # #"method": "say_hello",
  164. # #"params": [],
  165. # "jsonrpc": "2.0",
  166. # "id": 0,
  167. #}
  168. #response = requests.post(url, json=payload).json()
  169. #print(response)
  170. #assert response["result"] == "Hello World!"
  171. #assert response["jsonrpc"]