Example #1
0
def get_asset_files_list(asset, account):
    try:
        files_str = do_decrypt(
            asset.encrypted_files,
            account,
        )
        logger.debug(f'Got decrypted files str {files_str}')
        files_list = json.loads(files_str)
        if not isinstance(files_list, list):
            raise TypeError(f'Expected a files list, got {type(files_list)}.')

        return files_list
    except Exception as e:
        logger.error(
            f'Error decrypting asset files for asset {asset.did}: {str(e)}')
        raise
Example #2
0
def get_asset_files_list(asset, wallet):
    try:
        encrypted_files = asset.encrypted_files
        if encrypted_files.startswith("{"):
            encrypted_files = json.loads(encrypted_files)["encryptedDocument"]
        files_str = do_decrypt(encrypted_files, wallet)
        if not files_str:
            return None
        logger.debug(f"Got decrypted files str {files_str}")
        files_list = json.loads(files_str)
        if not isinstance(files_list, list):
            raise TypeError(f"Expected a files list, got {type(files_list)}.")

        return files_list
    except Exception as e:
        logger.error(f"Error decrypting asset files for asset {asset.did}: {str(e)}")
        raise
Example #3
0
def test_download_service(client):
    aqua = Aquarius('http://localhost:5000')
    for did in aqua.list_assets():
        aqua.retire_asset_ddo(did)

    init_endpoint = BaseURLs.ASSETS_URL + '/initialize'
    download_endpoint = BaseURLs.ASSETS_URL + '/download'

    pub_acc = get_publisher_account()
    cons_acc = get_consumer_account()

    request_ether('https://faucet.nile.dev-ocean.com', cons_acc)

    ddo = get_dataset_ddo_with_access_service(pub_acc)
    dt_address = ddo.as_dictionary()['dataTokenAddress']
    dt_token = DataTokenContract(dt_address)
    tx_id = dt_token.mint(cons_acc.address, 50, pub_acc)
    dt_token.get_tx_receipt(tx_id)
    time.sleep(2)

    def verify_supply(mint_amount=50):
        supply = dt_token.contract_concise.totalSupply()
        if supply <= 0:
            _tx_id = dt_token.mint(cons_acc.address, mint_amount, pub_acc)
            dt_token.get_tx_receipt(_tx_id)
            supply = dt_token.contract_concise.totalSupply()
        return supply

    while True:
        try:
            s = verify_supply()
            if s > 0:
                break
        except (ValueError, Exception):
            pass

    auth_token = generate_auth_token(cons_acc)
    index = 0

    sa = ServiceAgreement.from_ddo(ServiceTypes.ASSET_ACCESS, ddo)

    # Consume using decrypted url
    files_list = json.loads(
        do_decrypt(ddo.encrypted_files, pub_acc))

    # initialize an agreement
    payload = dict({
        'documentId': ddo.did,
        'serviceId': sa.index,
        'serviceType': sa.type,
        'tokenAddress': dt_address,
        'consumerAddress': cons_acc.address
    })

    payload['url'] = files_list[index]['url']
    request_url = init_endpoint + '?' + '&'.join([f'{k}={v}' for k, v in payload.items()])

    response = client.get(
        request_url
    )
    assert response.status == '200 OK'

    tx_params = response.json
    num_tokens = tx_params['numTokens']
    assert tx_params['from'] == cons_acc.address
    assert tx_params['to'] == pub_acc.address
    assert tx_params['dataTokenAddress'] == ddo.as_dictionary()['dataTokenAddress']

    # Transfer tokens to provider account
    tx_id = dt_token.transfer(tx_params['to'], num_tokens, cons_acc)
    dt_token.get_tx_receipt(tx_id)

    # Consume using url index and signature (let the provider do the decryption)
    payload.pop('url')
    payload['signature'] = auth_token
    payload['transferTxId'] = Web3.toHex(tx_id)
    payload['fileIndex'] = index
    request_url = download_endpoint + '?' + '&'.join([f'{k}={v}' for k, v in payload.items()])
    response = client.get(
        request_url
    )
    assert response.status == '200 OK'