Skip to content
Cannot retrieve contributors at this time
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 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): = name
self.protocol = protocol = 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"
return f"{self.protocol}://{}:{self.port}"
def post(self, segment: str, payload):
base = self.base_url()
return"{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}")'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(
if not v.valid():
raise Exception(f"Providing App {} is "
f"NOT valid according to MOTHER")
body = self.transform_request_id(user, request_hash, request_id)
payload = bundle(
self.keystore, requesting_app,, body, 'Success')
r ='data_request', payload)
d = r.json()
if r.status_code != 200:
raise Exception(d['message'])
bundle_d = unbundle(self.keystore,, d)
decrypted_body = bundle_d['data']
#'"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(
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']
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(f"Pay provider {}")
ret = und_reward.send_reward(, False)
log.debug(f"Pay Unification")
ret = und_reward.pay_unif()
return self.persist_data(, request_hash, d)
def read_data_from_store(
self, providing_app: Provider, request_hash):
temp_dir = Path(tempfile.gettempdir())
tp = temp_dir / Path(f"{}-{request_hash}")
encrypted_body = tp.read_text()
d = json.loads(encrypted_body)
bundle_d = unbundle(self.keystore,, d)
decrypted_body = bundle_d['data']'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'])