From cf73707643899b44fb4aba6240ce929ae584e62b Mon Sep 17 00:00:00 2001
From: tecnovert <tecnovert@tecnovert.net>
Date: Sat, 27 Jul 2019 23:14:28 +0200
Subject: [PATCH] started rpc page.

---
 basicswap/__init__.py              |  2 +-
 basicswap/basicswap.py             | 16 +++++++++--
 basicswap/http_server.py           | 43 +++++++++++++++++++++++++++---
 basicswap/templates/index.html     |  5 ++--
 basicswap/templates/offer_new.html |  4 +--
 basicswap/templates/rpc.html       | 28 +++++++++++++++++++
 6 files changed, 88 insertions(+), 10 deletions(-)
 create mode 100644 basicswap/templates/rpc.html

diff --git a/basicswap/__init__.py b/basicswap/__init__.py
index 17b966c..73f5c3b 100644
--- a/basicswap/__init__.py
+++ b/basicswap/__init__.py
@@ -1,3 +1,3 @@
 name = "basicswap"
 
-__version__ = "0.0.1"
+__version__ = "0.0.2"
diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py
index 2433c86..14d7067 100644
--- a/basicswap/basicswap.py
+++ b/basicswap/basicswap.py
@@ -2319,11 +2319,23 @@ class BasicSwap():
 
     def calltx(self, cmd):
         bindir = self.coin_clients[Coins.PART]['bindir']
-        command_cli = os.path.join(bindir, cfg.PARTICL_TX)
+        command_tx = os.path.join(bindir, cfg.PARTICL_TX)
         chainname = '' if self.chain == 'mainnet' else (' -' + self.chain)
-        args = command_cli + chainname + ' ' + cmd
+        args = command_tx + chainname + ' ' + cmd
         p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
         out = p.communicate()
         if len(out[1]) > 0:
             raise ValueError('TX error ' + str(out[1]))
         return out[0].decode('utf-8').strip()
+
+    def callcoincli(self, coin_type, params, wallet=None):
+        bindir = self.coin_clients[coin_type]['bindir']
+        datadir = self.coin_clients[coin_type]['datadir']
+        command_cli = os.path.join(bindir, chainparams[coin_type]['name'] + '-cli')
+        chainname = '' if self.chain == 'mainnet' else (' -' + self.chain)
+        args = command_cli + chainname + ' ' + '-datadir=' + datadir + ' ' + params
+        p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
+        out = p.communicate()
+        if len(out[1]) > 0:
+            raise ValueError('CLI error ' + str(out[1]))
+        return out[0].decode('utf-8').strip()
diff --git a/basicswap/http_server.py b/basicswap/http_server.py
index 80352b3..16d3c9a 100644
--- a/basicswap/http_server.py
+++ b/basicswap/http_server.py
@@ -126,6 +126,44 @@ class HttpHandler(BaseHTTPRequestHandler):
     def js_index(self, url_split):
         return bytes(json.dumps(self.server.swap_client.getSummary()), 'UTF-8')
 
+    def page_rpc(self, url_split, post_string):
+        swap_client = self.server.swap_client
+
+        result = None
+        messages = []
+        if post_string != '':
+            form_data = urllib.parse.parse_qs(post_string)
+            form_id = form_data[b'formid'][0].decode('utf-8')
+            if self.server.last_form_id.get('rpc', None) == form_id:
+                messages.append('Prevented double submit for form {}.'.format(form_id))
+            else:
+                self.server.last_form_id['newoffer'] = form_id
+
+                try:
+                    coin_type = Coins(int(form_data[b'coin_type'][0]))
+                except Exception:
+                    raise ValueError('Unknown Coin Type')
+
+                cmd = form_data[b'cmd'][0].decode('utf-8')
+                try:
+                    result = swap_client.callcoincli(coin_type, cmd)
+                except Exception as ex:
+                    result = str(ex)
+
+        coins = []
+        for k, v in swap_client.coin_clients.items():
+            if v['connection_type'] == 'rpc':
+                coins.append((int(k), getCoinName(k)))
+
+        template = env.get_template('rpc.html')
+        return bytes(template.render(
+            title=self.server.title,
+            h2=self.server.title,
+            coins=coins,
+            result=result,
+            form_id=os.urandom(8).hex(),
+        ), 'UTF-8')
+
     def page_active(self, url_split, post_string):
         swap_client = self.server.swap_client
         active_swaps = swap_client.listSwapsInProgress()
@@ -141,9 +179,6 @@ class HttpHandler(BaseHTTPRequestHandler):
     def page_wallets(self, url_split, post_string):
         swap_client = self.server.swap_client
 
-        content = html_content_start(self.server.title, self.server.title) \
-            + '<h3>Wallets</h3>'
-
         messages = []
         if post_string != '':
             form_data = urllib.parse.parse_qs(post_string)
@@ -535,6 +570,8 @@ class HttpHandler(BaseHTTPRequestHandler):
                     return self.page_active(url_split, post_string)
                 if url_split[1] == 'wallets':
                     return self.page_wallets(url_split, post_string)
+                if url_split[1] == 'rpc':
+                    return self.page_rpc(url_split, post_string)
                 if url_split[1] == 'offer':
                     return self.page_offer(url_split, post_string)
                 if url_split[1] == 'offers':
diff --git a/basicswap/templates/index.html b/basicswap/templates/index.html
index 3f05048..6b0ee92 100644
--- a/basicswap/templates/index.html
+++ b/basicswap/templates/index.html
@@ -1,7 +1,5 @@
 {% include 'header.html' %}
 
-<p><a href="/wallets">View Wallets</a></p>
-
 <p>
 {% if refresh %}
 Page Refresh: {{ refresh }} seconds<br/>
@@ -9,6 +7,9 @@ Page Refresh: {{ refresh }} seconds<br/>
 Version: {{ version }}
 </p>
 <p>
+<a href="/wallets">View Wallets</a><br/>
+<a href="/rpc">RPC Console</a><br/>
+<br/>
 <a href="/active">Swaps in progress: {{ summary.num_swapping }}</a><br/>
 <a href="/offers">Network Offers: {{ summary.num_network_offers }}</a><br/>
 <a href="/sentoffers">Sent Offers: {{ summary.num_sent_offers }}</a><br/>
diff --git a/basicswap/templates/offer_new.html b/basicswap/templates/offer_new.html
index 3f95738..503ec43 100644
--- a/basicswap/templates/offer_new.html
+++ b/basicswap/templates/offer_new.html
@@ -13,7 +13,7 @@
 {% for c in coins %}
 <option value="{{ c[0] }}">{{ c[1] }}</option>
 {% endfor %}
-</select>'
+</select>
 </td><td>Amount From</td><td><input type="text" name="amt_from"></td></tr>
 
 <tr><td>Coin To</td><td>
@@ -21,7 +21,7 @@
 {% for c in coins %}
 <option value="{{ c[0] }}">{{ c[1] }}</option>
 {% endfor %}
-</select>'
+</select>
 </td><td>Amount To</td><td><input type="text" name="amt_to"></td></tr>
 
 <tr><td>Contract locked (hrs)</td><td><input type="number" name="lockhrs" min="2" max="96" value="48"></td><td colspan=2>Participate txn will be locked for half the time.</td></tr>
diff --git a/basicswap/templates/rpc.html b/basicswap/templates/rpc.html
new file mode 100644
index 0000000..5f3ad69
--- /dev/null
+++ b/basicswap/templates/rpc.html
@@ -0,0 +1,28 @@
+{% include 'header.html' %}
+
+<h3>New Offer</h3>
+{% for m in messages %}
+<p>{{ m }}</p>
+{% endfor %}
+
+<form method="post">
+<p>
+<select name="coin_type"><option value="-1">-- Select Coin --</option>
+{% for c in coins %}
+<option value="{{ c[0] }}">{{ c[1] }}</option>
+{% endfor %}
+</select><br/>
+<input type="text" name="cmd"><br/>
+<input type="submit" value="Submit">
+<input type="hidden" name="formid" value="{{ form_id }}">
+</p>
+</form>
+
+{% if result %}
+<textarea id="story" name="story" rows="40" cols="160">
+{{ result }}
+</textarea>
+{% endif %}
+
+<p><a href="/">home</a></p>
+</body></html>
\ No newline at end of file