Beispiel #1
0
def mock_get_public_key(app_name):
    encoded_password = str.encode(password_for_app(app_name))
    current_path = Path(os.path.dirname(os.path.abspath(__file__)))
    ks_path = current_path / Path('data/keys')
    ks = UnificationKeystore(encoded_password,
                             app_name=app_name,
                             keystore_path=ks_path)
    return ks.get_rpc_auth_public_key()
def generate_keystore(app_name):
    password = Fernet.generate_key()
    click.echo(password)

    current_path = Path(os.path.dirname(os.path.abspath(__file__)))
    ks_path = current_path / Path(f'data/keys')
    ks = UnificationKeystore(password,
                             app_name=app_name,
                             keystore_path=ks_path)
    pub = keystore[app_name]['public_key']
    priv = keystore[app_name]['private_key']

    ks.set_rpc_auth_keys(pub, priv)
Beispiel #3
0
def spawn_haiku_node(pw, config):
    encoded_password = str.encode(pw)
    ks = UnificationKeystore(encoded_password)

    setattr(app, 'keystore', ks)
    setattr(app, 'unification_config', config)
    app.run(debug=False, host="0.0.0.0", port=PORT, ssl_context='adhoc')
Beispiel #4
0
def view(provider, request_hash):
    """
    Read data stored locally from an Data Provider for a particular user.

    \b
    :param provider: The app name of the data provider.
    :param request_hash: The particular piece of data in concern.
    :return:
    """
    requesting_app = os.environ['app_name']
    password = os.environ['keystore']

    eos_client = get_eos_rpc_client()
    mother = UnificationMother(eos_client, provider, get_cleos(),
                               get_ipfs_client())

    provider_obj = Provider(provider, 'https', mother)
    req_hash = f'request-{request_hash}'

    click.echo(f'App {requesting_app} is reading ingested data from '
               f'{provider_obj.name}')

    encoded_password = str.encode(password)
    keystore = UnificationKeystore(encoded_password)

    client = HaikuDataClient(keystore)
    data = client.read_data_from_store(provider_obj, req_hash)

    click.echo(json.loads(data))
def systest_auth(requesting_app, providing_app, user):
    """
    Ensuring that an incorrectly signed request is rejected.

    """
    def broken(d, field):
        d[field] = 'unlucky' + d[field][7:]
        return d

    log.info(f'{requesting_app} is requesting data from {providing_app}')

    body = {'users': [user], 'data_id': 'request_hash'}

    app_config = demo_config['demo_apps'][providing_app]
    port = app_config['rpc_server_port']

    eos_client = get_eos_rpc_client()
    mother = UnificationMother(eos_client, providing_app, get_cleos(),
                               get_ipfs_client())
    provider_obj = Provider(providing_app, 'https', mother)

    encoded_password = demo_config['system'][requesting_app]['password']

    ks = UnificationKeystore(encoded_password,
                             app_name=requesting_app,
                             keystore_path=Path('data/keys'))
    payload = bundle(ks, requesting_app, provider_obj.name, body, 'Success')
    payload = broken(payload, 'signature')

    r = provider_obj.post('data_request', payload)
    assert r.status_code == 401
Beispiel #6
0
def unbundle(ks: UnificationKeystore, sender: str, reload: dict) -> dict:
    """
    Un-bundle a JSON payload.

    """
    recipient_priv_key = ks.get_rpc_auth_private_key()
    public_key = get_public_key(sender)

    return __unbundle(recipient_priv_key, public_key, reload)
Beispiel #7
0
def bundle(ks: UnificationKeystore, uapp_contract: str, recipient: str,
           message_d: dict, message: str) -> dict:
    """
    Bundle an encrypted payload.

    """
    sender_priv_key = ks.get_rpc_auth_private_key()
    recipient_pub_key = get_public_key(recipient)

    return __bundle(
        sender_priv_key, recipient_pub_key, uapp_contract, message_d, message)
Beispiel #8
0
def fetch(provider, request_hash, user):
    """
    Fetch data from an App to this App in an Enterprise/DSP/B2B environment,
    where data request is agreed external to the UApp Store.

    \b
    :param provider: The app name of the data provider.
    :param request_hash: The particular piece of data in concern.
    :param user: Obtain data for a specific user EOS user account.
    :return:
    """
    requesting_app = os.environ['app_name']
    password = os.environ['keystore']

    # Write the data request to the Consumer's UApp smart contract
    eos_client = get_eos_rpc_client()
    mother = UnificationMother(eos_client, provider, get_cleos(),
                               get_ipfs_client())

    provider = Provider(provider, 'https', mother)
    req_hash = f'request-{request_hash}'

    suffix = 'for all users' if user is None else f'for {user}'
    click.echo(f'App {requesting_app} is requesting data from {provider}'
               f' {suffix}')

    encoded_password = str.encode(password)
    keystore = UnificationKeystore(encoded_password)

    # tmp - get the price for the transfer from Schema[0] in provider's UApp SC
    # This will possibly be determined externally as part of the B2B agreement
    provider_uapp_sc = UnificationUapp(eos_client, provider.name)
    db_schema = provider_uapp_sc.get_db_schema_by_pkey(
        0)  # tmp - only 1 schema
    sched_price = db_schema['price_sched']

    # initiate request in Consumer's UApp SC
    consumer_uapp_sc = UnificationUapp(eos_client, requesting_app)
    latest_req_id = consumer_uapp_sc.init_data_request(provider.name, "0", "0",
                                                       sched_price)

    client = HaikuDataClient(keystore)
    data_path = client.make_data_request(requesting_app, provider, user,
                                         req_hash, latest_req_id)

    click.echo(f'Data written to: {data_path}')
    click.echo(f'View using: haiku view {provider.name} {request_hash}')
Beispiel #9
0
def __request_from_uapp_store(data_request):
    """
    Receives a data request from the UApp Store, and
    processes the request

    \b
    :param data_request: Dict containing request parameters
    """
    requesting_app = os.environ['app_name']
    password = os.environ['keystore']

    click.echo("Processing request from UApp Store:")
    click.echo(data_request)

    eos_client = get_eos_rpc_client()

    # Write the data request to the Consumer's smart contract
    uapp_sc = UnificationUapp(eos_client, requesting_app)
    latest_req_id = uapp_sc.init_data_request(data_request['provider'],
                                              data_request['schema_pkey'], "0",
                                              data_request['price'])

    request_hash = f"{data_request['provider']}-{data_request['schema_pkey']}" \
                   f"-{latest_req_id}.dat"

    provider_name = data_request['provider']
    mother = UnificationMother(eos_client, provider_name, get_cleos(),
                               get_ipfs_client())
    provider_obj = Provider(provider_name, 'https', mother)
    req_hash = f'request-{request_hash}'

    click.echo(f'App {requesting_app} is requesting data from '
               f'{provider_obj.name}')

    encoded_password = str.encode(password)
    keystore = UnificationKeystore(encoded_password)

    client = HaikuDataClient(keystore)
    data_path = client.make_data_request(requesting_app, provider_obj, None,
                                         req_hash, latest_req_id)

    click.echo(f'Data written to: {data_path}')
    click.echo(f'View using: haiku view {provider_obj.name} {request_hash}')
def systest_ingest(requesting_app, providing_app, user, balances):
    log.info(f'Testing Fetch ingestion: {requesting_app} '
             f'is requesting data from {providing_app}')
    request_hash = f'data-request-{providing_app}-{requesting_app}'

    app_config = demo_config['demo_apps'][providing_app]
    port = app_config['rpc_server_port']

    eos_client = get_eos_rpc_client()
    mother = UnificationMother(eos_client, providing_app, get_cleos(),
                               get_ipfs_client())
    provider_obj = Provider(providing_app, 'https', mother)

    password = demo_config['system'][requesting_app]['password']
    encoded_password = str.encode(password)
    keystore = UnificationKeystore(encoded_password,
                                   app_name=requesting_app,
                                   keystore_path=Path('data/keys'))

    conf = UnificationConfig()
    eos_client = Client(
        nodes=[f"http://{conf['eos_rpc_ip']}:{conf['eos_rpc_port']}"])
    consumer_uapp_sc = UnificationUapp(eos_client, requesting_app)

    price_sched = demo_config['demo_apps'][providing_app]['db_schemas'][0][
        'price_sched']

    latest_req_id = consumer_uapp_sc.init_data_request(provider_obj.name, "0",
                                                       "0", price_sched)

    client = HaikuDataClient(keystore)
    client.make_data_request(requesting_app, provider_obj, user, request_hash,
                             latest_req_id)
    client.read_data_from_store(provider_obj, request_hash)

    # Update the system test record of the balances
    balances[requesting_app] = balances[requesting_app] - price_sched
    und_rewards = UndRewards(providing_app, price_sched)
    balances[providing_app] = (balances[providing_app] +
                               und_rewards.calculate_reward(is_user=False))

    return balances
def systest_process_permission_batches():
    appnames = ['app1', 'app2', 'app3']

    for app_name in appnames:
        log.debug(f'run systest_process_permission_batches for {app_name}')
        mother = UnificationMother(get_eos_rpc_client(), app_name, get_cleos(),
                                   get_ipfs_client())
        provider_obj = Provider(app_name, 'https', mother)

        password = demo_config['system'][app_name]['password']
        encoded_password = str.encode(password)
        keystore = UnificationKeystore(encoded_password,
                                       app_name=app_name,
                                       keystore_path=Path('data/keys'))

        client = HaikuDataClient(keystore)
        try:
            client.process_permissions_batch(provider_obj)
        except Exception as e:
            log.error(f'systest_process_permission_batches failed: {e}')