Browse Source

#161 Ended the spike about chainspace in the boxes and tor - pushed the doc into the spikes folder in drive

Jordi Coscolla 1 year ago
parent
commit
b4f0919510

+ 4 - 3
apps/wallet-proxy/routes.py

@@ -2,7 +2,7 @@ import json
2 2
 import os
3 3
 from io import open
4 4
 
5
-from chainspaceapi import ChainspaceClient
5
+from src.chainspace_client import ChainspaceClient
6 6
 from chainspacecontract.examples import petition_encrypted as increment
7 7
 from flask import Flask
8 8
 from flask import jsonify
@@ -14,6 +14,7 @@ from src.petition import Petition
14 14
 
15 15
 app = Flask(__name__)
16 16
 INTERNAL_SERVER_ERROR = 500
17
+USE_TOR = False
17 18
 
18 19
 
19 20
 def config_for_env():
@@ -31,7 +32,7 @@ def createChainspaceClient():
31 32
     url = urlparse(env_data['csApiLink'])
32 33
     hostname = url.hostname
33 34
     port = url.port or 5000
34
-    return ChainspaceClient(host=hostname, port=port)
35
+    return ChainspaceClient(host=hostname, port=port, useTor=USE_TOR)
35 36
 
36 37
 
37 38
 def csApiLink():
@@ -39,7 +40,7 @@ def csApiLink():
39 40
 
40 41
 
41 42
 def petition():
42
-    chainspace_repository = ChainspaceRepository(createChainspaceClient(), csApiLink())
43
+    chainspace_repository = ChainspaceRepository(createChainspaceClient(), csApiLink(), useTor=USE_TOR)
43 44
     return Petition(chainspace_repository, increment)
44 45
 
45 46
 

+ 35 - 0
apps/wallet-proxy/src/chainspace_client.py

@@ -0,0 +1,35 @@
1
+import requests
2
+import json
3
+
4
+
5
+class ChainspaceClient(object):
6
+    def __init__(self, host='127.0.0.1', port=5000, useTor=False):
7
+        self.host = host
8
+        self.port = port
9
+        self.proxies = {
10
+            'http': 'socks5h://localhost:9050',
11
+            'https': 'socks5h://localhost:9050'
12
+        }
13
+        self.useTor = useTor
14
+
15
+    @property
16
+    def url(self):
17
+        return 'http://{}:{}'.format(self.host, self.port)
18
+
19
+    def process_transaction(self, transaction):
20
+        endpoint = self.url + '/api/1.0/transaction/process'
21
+        print "POST " + endpoint + " HTTP/1.1"
22
+        print "" + json.dumps(transaction)
23
+        if self.useTor:
24
+            r = requests.post(endpoint, json=transaction, proxies=self.proxies)
25
+        else:
26
+            r = requests.post(endpoint, json=transaction)
27
+
28
+        print "HTTP/1.1 " + str(r.status_code) + " " + r.reason
29
+        print r.json()
30
+        return r
31
+
32
+    def dump_transaction(self, transaction):
33
+        endpoint = self.url + '/api/1.0/transaction/dump'
34
+        r = requests.post(endpoint, json=transaction)
35
+        return r

+ 13 - 3
apps/wallet-proxy/src/chainspace_repository.py

@@ -5,9 +5,10 @@ from src.transaction_log import TransactionLog
5 5
 
6 6
 class ChainspaceRepository:
7 7
 
8
-    def __init__(self, client, cs_url):
8
+    def __init__(self, client, cs_url, useTor=False):
9 9
         self.client = client
10 10
         self.cs_url = cs_url
11
+        self.useTor = useTor
11 12
 
12 13
     def process_transaction(self, transaction):
13 14
         response = self.client.process_transaction(transaction)
@@ -17,13 +18,22 @@ class ChainspaceRepository:
17 18
         response = loads(response.content)
18 19
 
19 20
         if response['success'] != "True":
20
-            raise Exception('Failed to process transaction outcome %s' % r['outcome'])
21
+            raise Exception('Failed to process transaction outcome %s' % response['outcome'])
21 22
 
22 23
         return response
23 24
 
24 25
     def get_full_transaction_log(self):
26
+        proxies = {
27
+            'http': 'socks5h://localhost:9050',
28
+            'https': 'socks5h://localhost:9050'
29
+        }
25 30
         url = self.cs_url + "/transactions"
26
-        response = requests.get(url)
31
+
32
+        if self.useTor:
33
+            response = requests.get(url, proxies=proxies)
34
+        else:
35
+            response = requests.get(url)
36
+
27 37
         if response.status_code != 200:
28 38
             return {
29 39
                 'error': "failed to call cs api - " +