/
client.py
158 lines (119 loc) · 5.23 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import hashlib
import json
import logging
import tempfile
from pathlib import Path
import requests
from haiku_node.blockchain.eos.mother import UnificationMother
from haiku_node.blockchain.eos.uapp import UnificationUapp
from haiku_node.blockchain.eos.und_rewards import UndRewards
from haiku_node.config.config import UnificationConfig
from haiku_node.encryption.payload import bundle, unbundle
from haiku_node.network.eos import get_eos_rpc_client, get_enum, Environment
from haiku_node.validation.validation import UnificationAppScValidation
log = logging.getLogger(__name__)
class Provider:
def __init__(self, name: str, protocol: str, mother: UnificationMother):
self.name = name
self.protocol = protocol
self.host = mother.get_haiku_rpc_ip()
self.port = mother.get_haiku_rpc_port()
def base_url(self):
haiku_env = get_enum()
if haiku_env == Environment.HOST:
return f"https://localhost:8852"
else:
return f"{self.protocol}://{self.host}:{self.port}"
def post(self, segment: str, payload):
base = self.base_url()
return requests.post(f"{base}/{segment}", json=payload, verify=False)
def get(self, segment: str):
base = self.base_url()
return requests.get(f"{base}/{segment}", verify=False)
def __repr__(self):
return self.base_url()
class HaikuDataClient:
def __init__(self, keystore, protocol='https', local=False):
self.keystore = keystore
self.local = local
self.protocol = protocol
def transform_request_id(self, user, request_hash, request_id=None):
"""
# TODO: Convert to a particular request body
"""
return {
'users': [] if user is None else [user],
'data_id': request_hash,
'request_id': request_id
}
def persist_data(self, providing_app_name: str, request_hash, data: dict):
temp_dir = Path(tempfile.gettempdir())
tp = temp_dir / Path(f"{providing_app_name}-{request_hash}")
log.info(f'Writing to {tp}')
tp.write_text(json.dumps(data), encoding='utf-8')
return tp
def make_data_request(self, requesting_app, providing_app: Provider, user,
request_hash, request_id):
# Check if the providing app is valid according to MOTHER
eos_client = get_eos_rpc_client()
v = UnificationAppScValidation(
eos_client, providing_app.name)
if not v.valid():
raise Exception(f"Providing App {providing_app.name} is "
f"NOT valid according to MOTHER")
body = self.transform_request_id(user, request_hash, request_id)
payload = bundle(
self.keystore, requesting_app, providing_app.name, body, 'Success')
r = providing_app.post('data_request', payload)
d = r.json()
if r.status_code != 200:
raise Exception(d['message'])
bundle_d = unbundle(self.keystore, providing_app.name, d)
decrypted_body = bundle_d['data']
# log.info(f'"In the air" decrypted content is: {decrypted_body}')
checksum_ok = False
# Computationally validate the received data the checksum of the payload
data_hash = hashlib.sha224(
str(d['payload']).encode('utf-8')).hexdigest()
uapp_sc = UnificationUapp(eos_client, requesting_app)
data_request = uapp_sc.get_data_request_by_pkey(request_id)
und_reward = UndRewards(requesting_app, data_request['price'])
if data_request['hash'] == data_hash:
print("Data computationally valid")
checksum_ok = True
json_obj = json.loads(decrypted_body)
if 'no-data' not in json_obj and checksum_ok:
users_to_pay = json_obj['unification_users']
print("users_to_pay")
print(users_to_pay)
num_users = len(users_to_pay)
print(f"Pay {num_users} users")
for username in users_to_pay:
print(f'pay {username}')
ret = und_reward.send_reward(
username, is_user=True, num_users=num_users)
log.debug(ret)
log.debug(f"Pay provider {providing_app.name}")
ret = und_reward.send_reward(providing_app.name, False)
log.debug(ret)
log.debug(f"Pay Unification")
ret = und_reward.pay_unif()
log.debug(ret)
return self.persist_data(
providing_app.name, request_hash, d)
def read_data_from_store(
self, providing_app: Provider, request_hash):
temp_dir = Path(tempfile.gettempdir())
tp = temp_dir / Path(f"{providing_app.name}-{request_hash}")
encrypted_body = tp.read_text()
d = json.loads(encrypted_body)
bundle_d = unbundle(self.keystore, providing_app.name, d)
decrypted_body = bundle_d['data']
log.info(f'Decrypted content from persistence store: {decrypted_body}')
return decrypted_body
def process_permissions_batch(self, providing_app: Provider):
r = providing_app.get('process_permission_batch')
d = r.json()
if r.status_code != 200:
raise Exception(d['message'])
log.debug(d)