فهرست منبع

refactor dark_client and use async code

ghassmo 5 سال پیش
والد
کامیت
c8870bdc59
5فایلهای تغییر یافته به همراه211 افزوده شده و 202 حذف شده
  1. 5 0
      scripts/dark_client/README.md
  2. 107 0
      scripts/dark_client/drk.py
  3. 14 0
      scripts/dark_client/requirement.txt
  4. 85 0
      scripts/dark_client/util.py
  5. 0 202
      scripts/drk

+ 5 - 0
scripts/dark_client/README.md

@@ -0,0 +1,5 @@
+# Dark Client 
+
+	$ python3 -m venv env
+	$ source env/bin/activate
+	$ pip install -r requirements.txt

+ 107 - 0
scripts/dark_client/drk.py

@@ -0,0 +1,107 @@
+#!/usr/bin/env python
+
+from util import arg_parser
+
+import aiohttp
+import asyncio
+
+class DarkClient:
+    # TODO: generate random ID (4 byte unsigned int) (rand range 0 - max size
+    # uint32
+    def __init__(self, client_session):
+        self.url = "http://localhost:8000/"
+        self.client_session = client_session
+        self.payload = {
+                "method": [],
+                "params": [],
+                "jsonrpc": [],
+                "id": [],
+                }
+
+    #def ckeygen(self, payload):
+    #    payload['method'] = "cash_key_gen"
+    #    payload['jsonrpc'] = "2.0"
+    #    payload['id'] = "0"
+    #    ckeygen = self.__request(payload)
+    #    print(ckeygen)
+
+    #def cashkey(self, payload):
+    #    payload['method'] = "get_cash_key"
+    #    payload['jsonrpc'] = "2.0"
+    #    payload['id'] = "0"
+    #    cashk = self.__request(payload)
+    #    print(cashk)
+    #
+    #def test_path(self, payload):
+    #    payload['method'] = "test_path"
+    #    payload['jsonrpc'] = "2.0"
+    #    payload['id'] = "0"
+    #    test = self.__request(payload)
+    #    print(test)
+
+    async def key_gen(self, payload):
+        payload['method'] = "key_gen"
+        payload['jsonrpc'] = "2.0"
+        payload['id'] = "0"
+        key = await self.__request(payload)
+        print(key)
+
+    async def get_info(self, payload):
+        payload['method'] = "get_info"
+        payload['jsonrpc'] = "2.0"
+        payload['id'] = "0"
+        info = await self.__request(payload)
+        print(info)
+
+    async def stop(self, payload):
+        payload['method'] = "stop"
+        payload['jsonrpc'] = "2.0"
+        payload['id'] = "0"
+        stop = await self.__request(payload)
+        print(stop)
+
+    async def say_hello(self, payload):
+        payload['method'] = "say_hello"
+        payload['jsonrpc'] = "2.0"
+        payload['id'] = "0"
+        hello = await self.__request(payload)
+        print(hello)
+
+    async def create_wallet(self, payload):
+        payload['method'] = "create_wallet"
+        payload['jsonrpc'] = "2.0"
+        payload['id'] = "0"
+        wallet = await self.__request(payload)
+        print(wallet)
+
+    async def create_cashier_wallet(self, payload):
+        payload['method'] = "create_cashier_wallet"
+        payload['jsonrpc'] = "2.0"
+        payload['id'] = "0"
+        wallet = await self.__request(payload)
+        print(wallet)
+
+
+    async def __request(self, payload):
+        async with self.client_session.post(self.url, json=payload) as response:
+            resp = await response.text()
+            print(resp)
+
+
+
+
+async def main():
+    try:
+        async with aiohttp.ClientSession() as session:
+            client = DarkClient(session)
+            await arg_parser(client)
+    except aiohttp.ClientConnectorError as err:
+        print('CONNECTION ERROR:', str(err))
+    except Exception as err:
+        print("ERROR: ", str(err))
+
+if __name__ == "__main__":
+    loop = asyncio.get_event_loop()
+    loop.run_until_complete(main())
+
+

+ 14 - 0
scripts/dark_client/requirement.txt

@@ -0,0 +1,14 @@
+aiodns==3.0.0
+aiohttp==3.7.4.post0
+async-timeout==3.0.1
+attrs==21.2.0
+brotlipy==0.7.0
+cchardet==2.1.7
+cffi==1.14.5
+chardet==4.0.0
+idna==3.2
+multidict==5.1.0
+pycares==4.0.0
+pycparser==2.20
+typing-extensions==3.10.0.0
+yarl==1.6.3

+ 85 - 0
scripts/dark_client/util.py

@@ -0,0 +1,85 @@
+
+import argparse
+
+async def arg_parser(client):
+    parser = argparse.ArgumentParser(
+            prog='drk',
+            usage='%(prog)s [commands]',
+            description="""DarkFi wallet command-line tool"""
+            )
+
+    parser.add_argument('-c', '--cashier', action='store_true', help='Create a cashier wallet')
+    parser.add_argument('-w', '--wallet', action='store_true', help='Create a new wallet')
+    parser.add_argument('-k', '--key', action='store_true', help='Test key')
+    parser.add_argument('-i', '--info', action='store_true', help='Request info from daemon')
+    parser.add_argument('-hi', '--hello', action='store_true', help='Test hello')
+    parser.add_argument("-s", "--stop", action='store_true', help="Send a stop signal to the daemon")
+
+    try:
+        args = parser.parse_args()
+
+        if args.key:
+            print("Attemping to generate a create key pair...")
+            await client.key_gen(client.payload)
+
+        if args.wallet:
+            print("Attemping to generate a create wallet...")
+            await client.create_wallet(client.payload)
+
+        if args.info:
+            print("Info was entered")
+            await client.get_info(client.payload)
+            print("Requesting daemon info...")
+
+        if args.stop:
+            print("Stop was entered")
+            await client.stop(client.payload)
+            print("Sending a stop signal...")
+
+        if args.hello:
+            print("Hello was entered")
+            await client.say_hello(client.payload)
+
+        if args.cashier:
+            print("Cash was entered")
+            await client.create_cashier_wallet(client.payload)
+
+    except Exception:
+        raise
+
+    #subparser = parser.add_subparsers(help='All available commands', title="Commands", dest='cmd')
+    #subparser.metavar = 'subcommands';
+    #login = subparser.add_parser('login', help='wallet login')
+    ##test = subparser.add_parser('test', help='test wallet functions')
+    #new = subparser.add_parser('new', help='create something new')
+
+    #new.add_argument('-w', '--wallet', action='store_true', help='Create a new wallet')
+    #new.add_argument('-k', '--key', action='store_true', help='Create a new key')
+    #new.add_argument('-c', '--cashier', action='store_true', help='Create a cashier wallet')
+
+    #login.add_argument('-u', '--username', type=str, required=True)
+    #login.add_argument('-p', '--password', type=str, required=True)
+
+    ##test.add_argument('-k', '--key', dest='key', action='store_true', help='Test key')
+    ##test.add_argument('-p', '--path', dest='path', action='store_true', help='Test path')
+    ##test.add_argument('-pk', '--pkey', dest='pkey', action='store_true', help='Print test key')
+    ##test.add_argument('-ck', '--ckey', dest='ckey', action='store_true', help='Cashier test key')
+    ##test.add_argument('-w', '--wallet', dest='wallet', action='store_true', help='Create a new wallet')
+    ##test.add_argument('-c', '--cashier', dest='cashier',action='store_true', help='Create a cashier wallet')
+
+    #if args.path:
+    #    try:
+    #        print("Testing path...")
+    #        client.test_path(client.payload)
+    #    except Exception:
+    #        raise
+
+    #if args.pkey:
+    #    try:
+    #        print("Attempting to print cashier key...")
+    #        client.cashkey(client.payload)
+    #    except Exception:
+    #        raise
+
+
+

+ 0 - 202
scripts/drk

@@ -1,202 +0,0 @@
-#!/usr/bin/env python
-
-import argparse
-import requests
-import json
-
-def arg_parser(client):
-    parser = argparse.ArgumentParser(prog='drk',
-                                          usage='%(prog)s [commands]',
-                                          description="""DarkFi wallet
-                                          command-line tool""")
-    #subparser = parser.add_subparsers(help='All available commands', title="Commands", dest='cmd')
-    #subparser.metavar = 'subcommands';
-    #login = subparser.add_parser('login', help='wallet login')
-    ##test = subparser.add_parser('test', help='test wallet functions')
-    #new = subparser.add_parser('new', help='create something new')
-
-    #new.add_argument('-w', '--wallet', action='store_true', help='Create a new wallet')
-    #new.add_argument('-k', '--key', action='store_true', help='Create a new key')
-    #new.add_argument('-c', '--cashier', action='store_true', help='Create a cashier wallet')
-
-    #login.add_argument('-u', '--username', type=str, required=True)
-    #login.add_argument('-p', '--password', type=str, required=True)
-
-    ##test.add_argument('-k', '--key', dest='key', action='store_true', help='Test key')
-    ##test.add_argument('-p', '--path', dest='path', action='store_true', help='Test path')
-    ##test.add_argument('-pk', '--pkey', dest='pkey', action='store_true', help='Print test key')
-    ##test.add_argument('-ck', '--ckey', dest='ckey', action='store_true', help='Cashier test key')
-    ##test.add_argument('-w', '--wallet', dest='wallet', action='store_true', help='Create a new wallet')
-    ##test.add_argument('-c', '--cashier', dest='cashier',action='store_true', help='Create a cashier wallet')
-
-    parser.add_argument('-c', '--cashier', action='store_true', help='Create a cashier wallet')
-    parser.add_argument('-w', '--wallet', action='store_true', help='Create a new wallet')
-    parser.add_argument('-k', '--key', action='store_true', help='Test key')
-    parser.add_argument('-i', '--info', action='store_true', help='Request info from daemon')
-    parser.add_argument('-hi', '--hello', action='store_true', help='Test hello')
-    parser.add_argument("-s", "--stop", action='store_true', help="Send a stop signal to the daemon")
-    args = parser.parse_args()
-
-    #if args.path:
-    #    try:
-    #        print("Testing path...")
-    #        client.test_path(client.payload)
-    #    except Exception:
-    #        raise
-
-    #if args.pkey:
-    #    try:
-    #        print("Attempting to print cashier key...")
-    #        client.cashkey(client.payload)
-    #    except Exception:
-    #        raise
-
-    if args.key:
-        try:
-            print("Attemping to generate a create key pair...")
-            client.key_gen(client.payload)
-        except Exception:
-            raise
-
-    if args.wallet:
-        try:
-            print("Attemping to generate a create wallet...")
-            client.create_wallet(client.payload)
-        except Exception:
-            raise
-
-    if args.info:
-        try:
-            print("Info was entered")
-            client.get_info(client.payload)
-            print("Requesting daemon info...")
-        except Exception:
-            raise
-
-    if args.stop:
-        try:
-            print("Stop was entered")
-            client.stop(client.payload)
-            print("Sending a stop signal...")
-        except Exception:
-            raise
-
-    if args.hello:
-        try:
-            print("Hello was entered")
-            client.say_hello(client.payload)
-        except Exception:
-            raise
-
-    if args.cashier:
-        try:
-            print("Cash was entered")
-            client.create_cashier_wallet(client.payload)
-        except Exception:
-            raise
-
-# TODO: refactor into async
-class DarkClient:
-    # TODO: generate random ID (4 byte unsigned int) (rand range 0 - max size
-    # uint32
-    def __init__(self):
-        self.url = "http://localhost:8000/"
-        self.payload = {
-            "method": [],
-            "params": [],
-            "jsonrpc": [],
-            "id": [],
-        }
-
-    #def ckeygen(self, payload):
-    #    payload['method'] = "cash_key_gen"
-    #    payload['jsonrpc'] = "2.0"
-    #    payload['id'] = "0"
-    #    ckeygen = self.__request(payload)
-    #    print(ckeygen)
-
-    #def cashkey(self, payload):
-    #    payload['method'] = "get_cash_key"
-    #    payload['jsonrpc'] = "2.0"
-    #    payload['id'] = "0"
-    #    cashk = self.__request(payload)
-    #    print(cashk)
-    #    
-    #def test_path(self, payload):
-    #    payload['method'] = "test_path"
-    #    payload['jsonrpc'] = "2.0"
-    #    payload['id'] = "0"
-    #    test = self.__request(payload)
-    #    print(test)
-        
-    def key_gen(self, payload):
-        payload['method'] = "key_gen"
-        payload['jsonrpc'] = "2.0"
-        payload['id'] = "0"
-        key = self.__request(payload)
-        print(key)
-
-    def get_info(self, payload):
-        payload['method'] = "get_info"
-        payload['jsonrpc'] = "2.0"
-        payload['id'] = "0"
-        info = self.__request(payload)
-        print(info)
-
-    def stop(self, payload):
-        payload['method'] = "stop"
-        payload['jsonrpc'] = "2.0"
-        payload['id'] = "0"
-        stop = self.__request(payload)
-        print(stop)
-
-    def say_hello(self, payload):
-        payload['method'] = "say_hello"
-        payload['jsonrpc'] = "2.0"
-        payload['id'] = "0"
-        hello = self.__request(payload)
-        print(hello)
-    
-    def create_wallet(self, payload):
-        payload['method'] = "create_wallet"
-        payload['jsonrpc'] = "2.0"
-        payload['id'] = "0"
-        wallet = self.__request(payload)
-        print(wallet)
-
-    def create_cashier_wallet(self, payload):
-        payload['method'] = "create_cashier_wallet"
-        payload['jsonrpc'] = "2.0"
-        payload['id'] = "0"
-        wallet = self.__request(payload)
-        print(wallet)
-
-
-    def __request(self, payload):
-        response = requests.post(self.url, json=payload).json()
-        # print something better
-        # parse into data structure 
-        print(response)
-        assert response["jsonrpc"]
-
-    
-if __name__ == "__main__":
-    client = DarkClient()
-    arg_parser(client)
-
-    #rpc()
-    ## Example echo method
-    #payload = {
-    #    #"method:": args,
-    #    #"method": "stop",
-    #    "method": "get_info",
-    #    #"method": "say_hello",
-    #    #"params": [],
-    #    "jsonrpc": "2.0",
-    #    "id": 0,
-    #}
-    #response = requests.post(url, json=payload).json()
-
-    #print(response)
-    #assert response["result"] == "Hello World!"
-    #assert response["jsonrpc"]