Просмотр исходного кода

generate json rpc api from darkfid's comments for mdbook

ghassmo 4 лет назад
Родитель
Сommit
2f53917417
6 измененных файлов с 200 добавлено и 16 удалено
  1. 1 0
      book/Makefile
  2. 70 0
      book/build_jsonrpc.py
  3. 2 0
      book/src/SUMMARY.md
  4. 1 0
      book/src/clients/clients.md
  5. 83 0
      book/src/clients/jsonrpc.md
  6. 43 16
      src/bin/darkfid.rs

+ 1 - 0
book/Makefile

@@ -1,6 +1,7 @@
 .POSIX:
 
 all:
+	python3 ./build_jsonrpc.py
 	mdbook build
 
 github: all

+ 70 - 0
book/build_jsonrpc.py

@@ -0,0 +1,70 @@
+
+class Method:
+    def __init__(self, name, params):
+        self.name = name
+        self.params = params.replace(',', ', ');
+        self.result = ""
+        self.note = ""
+
+
+    def set_result(self, result):
+        self.result = result.replace(':', ': ');
+        self.result = result.replace(',', ', ');
+
+    def __str__(self):
+        method_str = '### ' + self.name + ': \n' \
+                + '`params`: ' +  self.params + '\n' \
+                + '\n' \
+                + '`result`: ' +  self.result + '\n' \
+                + '\n' \
+
+        if not self.note == "":
+            method_str += '> `note`: ' +  self.note + '\n' 
+
+        return method_str
+
+
+def main():
+    methods =  []
+    with open('../src/bin/darkfid.rs') as f:
+        lines = f.readlines()
+        for i in range(0, len(lines)):
+            line = lines[i]
+
+            if line.__contains__("RPCAPI"):
+
+                line = lines[i + 1]
+                if line.__contains__(' --> '):
+                    line = line.strip()
+                    words = line.split(' ')
+                    method = words[3][1::][:-2:]
+                    params = words[5][:-1:]
+                    methods.append(Method(method, params))
+
+                line = lines[i + 2]
+                if line.__contains__(' <-- '):
+                    line = line.strip()
+                    words = line.split(' ')
+                    methods[-1].set_result(words[3][:-1:])
+
+                line = lines[i + 3]
+                if line.__contains__("APINOTE"):
+                    methods[-1].note = line.strip().replace('// APINOTE:','')
+                    count = i + 4
+                    line = lines[count]
+                    while line.strip().startswith('//'):
+                        count += 1
+                        methods[-1].note += line[6::]
+                        line = lines[count]
+
+
+
+    with open('src/clients/jsonrpc.md', 'w') as f:
+        f.write('# JSONRPC API \n')
+        f.write('## Methods \n')
+        for m in methods:
+            f.write(m.__str__())
+
+
+if __name__ == '__main__':
+    main()

+ 2 - 0
book/src/SUMMARY.md

@@ -3,6 +3,8 @@
 [DarkFi](README.md)
 - [Development](development.md)
 - [Tutorial](tutorial.md)
+- [Client](clients/clients.md)
+  - [JSONRPC API](clients/jsonrpc.md)
 - [zkas](zkas/zkas.md)
   - [Examples](zkas/examples.md)
     - [Sapling payment scheme](zkas/examples/sapling.md)

+ 1 - 0
book/src/clients/clients.md

@@ -0,0 +1 @@
+# Clients

+ 83 - 0
book/src/clients/jsonrpc.md

@@ -0,0 +1,83 @@
+# JSONRPC API 
+## Methods 
+### say_hello: 
+`params`: []
+
+`result`: "helloworld"
+
+### create_wallet: 
+`params`: []
+
+`result`: true
+
+### key_gen: 
+`params`: []
+
+`result`: true
+
+### get_key: 
+`params`: []
+
+`result`: "vdNS7oBj7KvsMWWmo9r96SV4SqATLrGsH2a3PGpCfJC"
+
+### get_keys: 
+`params`: []
+
+`result`: "[vdNS7oBj7KvsMWWmo9r96SV4SqATLrGsH2a3PGpCfJC, ...]"
+
+> `note`:  the first address in the returned vector is the default address
+
+### import_keypair: 
+`params`: [path]
+
+`result`: true
+
+### export_keypair: 
+`params`: [path]
+
+`result`: true
+
+### set_default_address: 
+`params`: [vdNS7oBj7KvsMWWmo9r96SV4SqATLrGsH2a3PGpCfJC]
+
+`result`: true
+
+### get_balances: 
+`params`: []
+
+`result`: "[{"btc":(value, network)}, ...]"
+
+### get_token_id: 
+`params`: [network, token]
+
+`result`: "Ht5G1RhkcKnpLVLMhqJc5aqZ4wYUEbxbtZwGCVbgU7DL"
+
+### features: 
+`params`: []
+
+`result`: {"network":["btc", "sol"]}
+
+### deposit: 
+`params`: [network, token, publickey]
+
+`result`: "Ht5G1RhkcKnpLVLMhqJc5aqZ4wYUEbxbtZwGCVbgU7DL"
+
+> `note`:  The publickey sent here is used so the cashier can know where to send
+ tokens once the deposit is received.
+
+### withdraw: 
+`params`: [network, token, publickey, amount]
+
+`result`: "txID"
+
+> `note`:  The publickey sent here is the address where the caller wants to receive
+ the tokens they plan to withdraw.
+ On request, send request to cashier to get deposit address, and then transfer
+ dark tokens to the cashier's wallet. Following that, the cashier should return
+ a transaction ID of them sending the funds that are requested for withdrawal.
+
+### transfer: 
+`params`: [network, dToken, address, amount]
+
+`result`: "txID"
+

+ 43 - 16
src/bin/darkfid.rs

@@ -139,14 +139,18 @@ impl Darkfid {
         Ok(())
     }
 
+    //// RPCAPI
     // --> {"method": "say_hello", "params": []}
-    // <-- {"result": "hello world"}
+    // <-- {"result": "helloworld"}
+    // APINOTE:
     async fn say_hello(&self, id: Value, _params: Value) -> JsonResult {
         JsonResult::Resp(jsonresp(json!("hello world"), id))
     }
 
+    //// RPCAPI
     // --> {"method": "create_wallet", "params": []}
     // <-- {"result": true}
+    // APINOTE:
     async fn create_wallet(&self, id: Value, _params: Value) -> JsonResult {
         match self.client.lock().await.init_db().await {
             Ok(()) => JsonResult::Resp(jsonresp(json!(true), id)),
@@ -154,8 +158,10 @@ impl Darkfid {
         }
     }
 
+    //// RPCAPI
     // --> {"method": "key_gen", "params": []}
     // <-- {"result": true}
+    // APINOTE:
     async fn key_gen(&self, id: Value, _params: Value) -> JsonResult {
         let client = self.client.lock().await;
         match client.key_gen().await {
@@ -164,17 +170,21 @@ impl Darkfid {
         }
     }
 
+    //// RPCAPI
     // --> {"method": "get_key", "params": []}
     // <-- {"result": "vdNS7oBj7KvsMWWmo9r96SV4SqATLrGsH2a3PGpCfJC"}
+    // APINOTE:
     async fn get_key(&self, id: Value, _params: Value) -> JsonResult {
         let pk = self.client.lock().await.main_keypair.public;
         let addr = Address::from(pk).to_string();
         JsonResult::Resp(jsonresp(json!(addr), id))
     }
 
+    //// RPCAPI
     // --> {"method": "get_keys", "params": []}
-    // <-- {"result": "[vdNS7oBj7KvsMWWmo9r96SV4SqATLrGsH2a3PGpCfJC, ... ]"}
-    // Note: the first address in the returned vector is the default address
+    // <-- {"result": "[vdNS7oBj7KvsMWWmo9r96SV4SqATLrGsH2a3PGpCfJC,...]"}
+    // APINOTE:
+    // the first address in the returned vector is the default address
     async fn get_keys(&self, id: Value, _params: Value) -> JsonResult {
         let result: Result<Vec<String>> = async {
             let keypairs = self.client.lock().await.get_keypairs().await?;
@@ -202,8 +212,10 @@ impl Darkfid {
         }
     }
 
-    // --> {"method": "import_keypair", "params": "[path/]"}
+    //// RPCAPI
+    // --> {"method": "import_keypair", "params": [path]}
     // <-- {"result": true}
+    // APINOTE:
     async fn import_keypair(&self, id: Value, params: Value) -> JsonResult {
         let args = params.as_array();
 
@@ -244,8 +256,10 @@ impl Darkfid {
         }
     }
 
-    // --> {"method": "export_keypair", "params": "[path/]"}
+    //// RPCAPI
+    // --> {"method": "export_keypair", "params": [path]}
     // <-- {"result": true}
+    // APINOTE:
     async fn export_keypair(&self, id: Value, params: Value) -> JsonResult {
         let args = params.as_array();
 
@@ -279,9 +293,10 @@ impl Darkfid {
         }
     }
 
-    // --> {"method": "set_default_address", "params":
-    // "[vdNS7oBj7KvsMWWmo9r96SV4SqATLrGsH2a3PGpCfJC]"}
+    //// RPCAPI
+    // --> {"method": "set_default_address", "params": [vdNS7oBj7KvsMWWmo9r96SV4SqATLrGsH2a3PGpCfJC]}
     // <-- {"result": true}
+    // APINOTE:
     async fn set_default_address(&self, id: Value, params: Value) -> JsonResult {
         let args = params.as_array();
 
@@ -304,8 +319,10 @@ impl Darkfid {
         }
     }
 
+    //// RPCAPI
     // --> {"method": "get_balances", "params": []}
-    // <-- {"result": "get_balances": "[ {"btc": (value, network)}, .. ]"}
+    // <-- {"result": "[{"btc":(value,network)},...]"}
+    // APINOTE:
     async fn get_balances(&self, id: Value, _params: Value) -> JsonResult {
         let result: Result<HashMap<String, (String, String)>> = async {
             let balances = self.client.lock().await.get_balances().await?;
@@ -337,8 +354,10 @@ impl Darkfid {
         }
     }
 
-    // --> {"method": "get_token_id", "params": [network, token]}
+    //// RPCAPI
+    // --> {"method": "get_token_id", "params": [network,token]}
     // <-- {"result": "Ht5G1RhkcKnpLVLMhqJc5aqZ4wYUEbxbtZwGCVbgU7DL"}
+    // APINOTE:
     async fn get_token_id(&self, id: Value, params: Value) -> JsonResult {
         let args = params.as_array();
 
@@ -405,8 +424,10 @@ impl Darkfid {
         }
     }
 
-    // --> {""method": "features", "params": []}
-    // <-- {"result": { "network": ["btc", "sol"] } }
+    //// RPCAPI
+    // --> {"method": "features", "params": []}
+    // <-- {"result": {"network":["btc","sol"]}}
+    // APINOTE:
     async fn features(&self, id: Value, _params: Value) -> JsonResult {
         let req = jsonreq(json!("features"), json!([]));
         let rep: JsonResult =
@@ -423,10 +444,12 @@ impl Darkfid {
         }
     }
 
-    // --> {"method": "deposit", "params": [network, token, publickey]}
+    //// RPCAPI
+    // --> {"method": "deposit", "params": [network,token,publickey]}
+    // <-- {"result": "Ht5G1RhkcKnpLVLMhqJc5aqZ4wYUEbxbtZwGCVbgU7DL"}
+    // APINOTE:
     // The publickey sent here is used so the cashier can know where to send
     // tokens once the deposit is received.
-    // <-- {"result": "Ht5G1RhkcKnpLVLMhqJc5aqZ4wYUEbxbtZwGCVbgU7DL"}
     async fn deposit(&self, id: Value, params: Value) -> JsonResult {
         let args = params.as_array();
 
@@ -487,13 +510,15 @@ impl Darkfid {
         }
     }
 
-    // --> {"method": "withdraw", "params": [network, token, publickey, amount]}
+    //// RPCAPI
+    // --> {"method": "withdraw", "params": [network,token,publickey,amount]}
+    // <-- {"result": "txID"}
+    // APINOTE:
     // The publickey sent here is the address where the caller wants to receive
     // the tokens they plan to withdraw.
     // On request, send request to cashier to get deposit address, and then transfer
     // dark tokens to the cashier's wallet. Following that, the cashier should return
     // a transaction ID of them sending the funds that are requested for withdrawal.
-    // <-- {"result": "txID"}
     async fn withdraw(&self, id: Value, params: Value) -> JsonResult {
         let args = params.as_array();
 
@@ -606,8 +631,10 @@ impl Darkfid {
         }
     }
 
-    // --> {"method": "transfer", [network, dToken, address, amount]}
+    //// RPCAPI
+    // --> {"method": "transfer", "params": [network,dToken,address,amount]}
     // <-- {"result": "txID"}
+    // APINOTE:
     async fn transfer(&self, id: Value, params: Value) -> JsonResult {
         let args = params.as_array();
         if args.is_none() {