コード例 #1
0
def search_assets():
    ConfigProvider.set_config(ExampleConfig.get_config())
    nevermined = Nevermined()
    account = get_account(0)
    ddo = nevermined.assets.create(
        example_metadata.metadata,
        account,
    )

    sleep(ASYNC_DELAY)

    logging.info(f'Registered asset: did={ddo.did}, ddo={ddo.as_text()}')
    resolved_ddo = nevermined.assets.resolve(ddo.did)
    logging.info(
        f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}'
    )

    ddo_list = nevermined.assets.search('bonding curve')
    logging.info(
        f'found {len(ddo_list)} assets that contain `bonding curve` in their metadata.'
    )
    ddo_list = nevermined.assets.query(
        {"query": {
            "text": ['Nevermined protocol white paper']
        }})
    logging.info(
        f'found {len(ddo_list)} assets with name that contains `Nevermined protocol white paper`'
    )
コード例 #2
0
def resolve_asset():
    ConfigProvider.set_config(ExampleConfig.get_config())
    nevermined = Nevermined()
    account = get_account(0)
    ddo = nevermined.assets.create(
        example_metadata.metadata, account,
    )

    sleep(ASYNC_DELAY)

    logging.info(f'Registered asset: did={ddo.did}, ddo={ddo.as_text()}')
    resolved_ddo = nevermined.assets.resolve(ddo.did)
    logging.info(f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}')
コード例 #3
0
def sign_service_agreement():
    ConfigProvider.set_config(ExampleConfig.get_config())
    # make ocean instance and register an asset
    nevermined = Nevermined()
    acc = get_account(0)
    ddo = nevermined.assets.create(example_metadata.metadata, acc)

    consumer_account = get_account(1)
    agreement_id, signature = nevermined.agreements.prepare(
        ddo.did, consumer_account)

    sleep(ASYNC_DELAY)

    logging.info(f'service agreement signed: '
                 f'\nservice agreement id: {agreement_id}, '
                 f'\nsignature: {signature}')
コード例 #4
0
def test_buy_asset(publisher_instance_no_init, consumer_instance_no_init):
    config = ExampleConfig.get_config()
    ConfigProvider.set_config(config)
    keeper = Keeper.get_instance()
    # :TODO: enable the actual SecretStore
    # SecretStoreProvider.set_secret_store_class(SecretStore)
    w3 = Web3Provider.get_web3()
    pub_acc = get_publisher_account()

    # Register ddo
    ddo = get_registered_ddo(publisher_instance_no_init, pub_acc)
    assert isinstance(ddo, DDO)
    # nevermined here will be used only to publish the asset. Handling the asset by the publisher
    # will be performed by the Gateway server running locally

    # restore the http client because we want the actual Gateway server to do the work
    # not the GatewayMock.
    # Gateway.set_http_client(requests)
    consumer_account = get_consumer_account()

    downloads_path_elements = len(
        os.listdir(consumer_instance_no_init._config.downloads_path)) if os.path.exists(
        consumer_instance_no_init._config.downloads_path) else 0
    # sign agreement using the registered asset did above
    service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS)
    sa = ServiceAgreement.from_service_dict(service.as_dictionary())
    # This will send the access request to Gateway which in turn will execute the agreement on-chain
    consumer_instance_no_init.accounts.request_tokens(consumer_account, 100)

    agreement_id = consumer_instance_no_init.assets.order(
        ddo.did, sa.index, consumer_account, consumer_account)

    event_wait_time = 10
    event = keeper.lock_payment_condition.subscribe_condition_fulfilled(
        agreement_id,
        event_wait_time,
        log_event(keeper.lock_payment_condition.FULFILLED_EVENT),
        (),
        wait=True
    )
    assert event, 'no event for LockRewardCondition.Fulfilled'

    # give access
    publisher_instance_no_init.agreements.conditions.grant_access(
        agreement_id, ddo.asset_id, consumer_account.address, pub_acc)
    event = keeper.access_condition.subscribe_condition_fulfilled(
        agreement_id,
        event_wait_time,
        log_event(keeper.access_condition.FULFILLED_EVENT),
        (),
        wait=True
    )
    assert event, 'no event for AccessSecretStoreCondition.Fulfilled'
    assert consumer_instance_no_init.agreements.is_access_granted(agreement_id, ddo.did, consumer_account.address)

    amounts = list(map(int, service.get_param_value_by_name('_amounts')))
    receivers = service.get_param_value_by_name('_receivers')

    publisher_instance_no_init.agreements.conditions.release_reward(
        agreement_id, ddo.asset_id, amounts, receivers, pub_acc)

    assert consumer_instance_no_init.assets.access(
        agreement_id,
        ddo.did,
        sa.index,
        consumer_account,
        config.downloads_path)

    assert len(os.listdir(config.downloads_path)) == downloads_path_elements + 1

    # Check that we can access only an specific file in passing the index.
    assert consumer_instance_no_init.assets.access(
        agreement_id,
        ddo.did,
        sa.index,
        consumer_account,
        config.downloads_path,
        1
    )
    assert len(os.listdir(config.downloads_path)) == downloads_path_elements + 1

    with pytest.raises(AssertionError):
        consumer_instance_no_init.assets.access(
            agreement_id,
            ddo.did,
            sa.index,
            consumer_account,
            config.downloads_path,
            -2
        )

    with pytest.raises(AssertionError):
        consumer_instance_no_init.assets.access(
            agreement_id,
            ddo.did,
            sa.index,
            consumer_account,
            config.downloads_path,
            3
        )

    # decrypt the contentUrls using the publisher account instead of consumer account.
    # if the secret store is working and ACL check is enabled, this should fail
    # since SecretStore decrypt will fail the checkPermissions check and the gateway will return
    # an http error code back
    with pytest.raises(ValueError) as e:
        consumer_instance_no_init.assets.access(
            agreement_id,
            ddo.did,
            sa.index,
            pub_acc,
            config.downloads_path
        )
        print(e)
コード例 #5
0
def test_buy_asset_no_secret_store(publisher_instance_gateway, consumer_instance_gateway):
    config = ExampleConfig.get_config()
    ConfigProvider.set_config(config)
    keeper = Keeper.get_instance()

    w3 = Web3Provider.get_web3()
    pub_acc = get_publisher_account()

    for method in [ServiceAuthorizationTypes.SECRET_STORE, ServiceAuthorizationTypes.PSK_ECDSA, ServiceAuthorizationTypes.PSK_RSA]:
        # Register ddo
        ddo = get_registered_with_psk(publisher_instance_gateway, pub_acc, auth_method=method)
        assert isinstance(ddo, DDO)
        # nevermined here will be used only to publish the asset. Handling the asset by the publisher
        # will be performed by the Gateway server running locally

        # restore the http client because we want the actual Gateway server to do the work
        # not the GatewayMock.
        # Gateway.set_http_client(requests)
        consumer_account = get_consumer_account()

        downloads_path_elements = len(
            os.listdir(consumer_instance_gateway._config.downloads_path)) if os.path.exists(
            consumer_instance_gateway._config.downloads_path) else 0
        # sign agreement using the registered asset did above
        service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS)
        sa = ServiceAgreement.from_service_dict(service.as_dictionary())
        # This will send the access request to Gateway which in turn will execute the agreement on-chain
        consumer_instance_gateway.accounts.request_tokens(consumer_account, 100)
        agreement_id = consumer_instance_gateway.assets.order(
            ddo.did, sa.index, consumer_account, consumer_account)

        event_wait_time = 10
        event = keeper.access_template.subscribe_agreement_created(
            agreement_id,
            event_wait_time,
            log_event(keeper.access_template.AGREEMENT_CREATED_EVENT),
            (),
            wait=True
        )
        assert event, 'no event for EscrowAccessSecretStoreTemplate.AgreementCreated'

        event = keeper.lock_payment_condition.subscribe_condition_fulfilled(
            agreement_id,
            event_wait_time,
            log_event(keeper.lock_payment_condition.FULFILLED_EVENT),
            (),
            wait=True
        )
        assert event, 'no event for LockRewardCondition.Fulfilled'

        # give access
        publisher_instance_gateway.agreements.conditions.grant_access(
            agreement_id, ddo.asset_id, consumer_account.address, pub_acc)
        event = keeper.access_condition.subscribe_condition_fulfilled(
            agreement_id,
            event_wait_time,
            log_event(keeper.access_condition.FULFILLED_EVENT),
            (),
            wait=True
        )
        assert event, 'no event for AccessSecretStoreCondition.Fulfilled'
        assert consumer_instance_gateway.agreements.is_access_granted(agreement_id, ddo.did, consumer_account.address)

        publisher_instance_gateway.agreements.conditions.release_reward(
            agreement_id, ddo.asset_id, sa.get_amounts_int(), sa.get_receivers(), pub_acc)

        assert consumer_instance_gateway.assets.access(
            agreement_id,
            ddo.did,
            sa.index,
            consumer_account,
            config.downloads_path)

        assert len(os.listdir(config.downloads_path)) == downloads_path_elements + 1

        with pytest.raises(AssertionError):
            consumer_instance_gateway.assets.access(
                agreement_id,
                ddo.did,
                sa.index,
                consumer_account,
                config.downloads_path,
                -2
            )

        with pytest.raises(AssertionError):
            consumer_instance_gateway.assets.access(
                agreement_id,
                ddo.did,
                sa.index,
                consumer_account,
                config.downloads_path,
                3
            )
コード例 #6
0
def test_nfts_flow(publisher_instance_no_init, consumer_instance_no_init):
    config = ExampleConfig.get_config()
    ConfigProvider.set_config(config)
    keeper = Keeper.get_instance()

    pub_acc = get_publisher_account()

    # Register ddo
    ddo = get_registered_ddo_nft(publisher_instance_no_init, pub_acc)
    asset_id = ddo.asset_id
    nft_amounts = 1
    assert isinstance(ddo, DDO)

    consumer_account = get_consumer_account()

    consumer_instance_no_init.accounts.request_tokens(consumer_account, 100)

    service_sales = ddo.get_service(service_type=ServiceTypes.NFT_SALES)
    sa_sales = ServiceAgreement.from_service_dict(
        service_sales.as_dictionary())

    amounts = sa_sales.get_amounts_int()
    receivers = sa_sales.get_receivers()
    number_nfts = sa_sales.get_number_nfts()
    token_address = keeper.token.address

    sales_agreement_id = consumer_instance_no_init.assets.order(
        ddo.did, sa_sales.index, consumer_account, consumer_account)

    sales_agreement = keeper.agreement_manager.get_agreement(
        sales_agreement_id)
    assert sales_agreement.did == asset_id, ''

    lock_cond_id = sales_agreement.condition_ids[0]
    access_cond_id = sales_agreement.condition_ids[1]
    escrow_cond_id = sales_agreement.condition_ids[2]

    # transfer the nft
    keeper.transfer_nft_condition.fulfill(sales_agreement_id, asset_id,
                                          consumer_account.address,
                                          nft_amounts, lock_cond_id, pub_acc)

    # escrow payment
    keeper.escrow_payment_condition.fulfill(
        sales_agreement_id, asset_id, amounts, receivers,
        keeper.escrow_payment_condition.address, token_address, lock_cond_id,
        access_cond_id, pub_acc)

    assert keeper.condition_manager.get_condition_state(lock_cond_id) == 2, ''
    assert keeper.condition_manager.get_condition_state(
        access_cond_id) == 2, ''
    assert keeper.condition_manager.get_condition_state(
        escrow_cond_id) == 2, ''

    assert keeper.did_registry.balance(consumer_account.address,
                                       asset_id) >= number_nfts

    # CHECK ACCESS CREATING THE AGREEMENT
    nft_access_service_agreement = ServiceAgreement.from_ddo(
        ServiceTypes.NFT_ACCESS, ddo)
    sa_access = ddo.get_service(service_type=ServiceTypes.NFT_ACCESS)
    nft_access_agreement_id = consumer_instance_no_init.assets.order(
        ddo.did, sa_access.index, consumer_account, consumer_account)

    event = keeper.nft_access_template.subscribe_agreement_created(
        nft_access_agreement_id,
        10,
        log_event(keeper.nft_access_template.AGREEMENT_CREATED_EVENT), (),
        wait=True)
    assert event, 'no event for AgreementCreated '

    time.sleep(3)
    keeper.nft_holder_condition.fulfill(nft_access_agreement_id, asset_id,
                                        consumer_account.address, number_nfts,
                                        consumer_account)
    time.sleep(3)
    keeper.nft_access_condition.fulfill(nft_access_agreement_id, asset_id,
                                        consumer_account.address, pub_acc)
    time.sleep(3)

    access_agreement = keeper.agreement_manager.get_agreement(
        nft_access_agreement_id)
    assert access_agreement.did == asset_id, ''

    nft_holder_cond_id = access_agreement.condition_ids[0]
    nft_access_cond_id = access_agreement.condition_ids[1]

    assert keeper.condition_manager.get_condition_state(
        nft_access_cond_id) == 2, ''
    assert keeper.condition_manager.get_condition_state(
        nft_holder_cond_id) == 2, ''

    # AND HERE TESTING WITHOUT CREATING THE AGREEMENT
    service_access = ddo.get_service(service_type=ServiceTypes.NFT_ACCESS)

    no_agreement_id = '0x'

    assert consumer_instance_no_init.assets.access(
        no_agreement_id,
        ddo.did,
        service_access.index,
        consumer_account,
        config.downloads_path,
        service_type=ServiceTypes.NFT_ACCESS)

    # AND HERE CHECKING CREATING AN AGREEMENT FIRST
    nft_access_service_agreement = ServiceAgreement.from_ddo(
        ServiceTypes.NFT_ACCESS, ddo)
    nft_access_agreement_id = ServiceAgreement.create_new_agreement_id()
    (nft_access_cond_id, nft_holder_cond_id
     ) = nft_access_service_agreement.generate_agreement_condition_ids(
         nft_access_agreement_id, asset_id, consumer_account.address, keeper)

    keeper.nft_access_template.create_agreement(
        nft_access_agreement_id, asset_id,
        [nft_holder_cond_id, nft_access_cond_id],
        nft_access_service_agreement.conditions_timelocks,
        nft_access_service_agreement.conditions_timeouts,
        consumer_account.address, pub_acc)

    event = keeper.nft_access_template.subscribe_agreement_created(
        nft_access_agreement_id,
        10,
        log_event(keeper.nft_access_template.AGREEMENT_CREATED_EVENT), (),
        wait=True)
    assert event, 'no event for AgreementCreated '

    # This is because in this test the gateway is executed using the same account than the consumer
    keeper.did_registry.add_provider(asset_id, consumer_account.address,
                                     pub_acc)
    time.sleep(3)

    assert consumer_instance_no_init.assets.access(
        nft_access_agreement_id,
        ddo.did,
        service_access.index,
        consumer_account,
        config.downloads_path,
        index=0,
        service_type=ServiceTypes.NFT_ACCESS)
コード例 #7
0
def compute_example(verbose=False):
    print("Setting up...")

    if verbose:
        configure_logging()

    date_created = dates_generator()

    # Setup nevermined
    ConfigProvider.set_config(ExampleConfig.get_config())
    config = ConfigProvider.get_config()
    nevermined = Nevermined()
    keeper = Keeper.get_instance()
    provider = "0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0"

    asset_rewards = {
        "_amounts": ["10", "2"],
        "_receivers": ["0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e", "0x068ed00cf0441e4829d9784fcbe7b9e26d4bd8d0"]
    }

    # Setup accounts
    acc = Account(
        Web3.toChecksumAddress(PROVIDER_ADDRESS), PROVIDER_PASSWORD, PROVIDER_KEYFILE
    )
    nevermined.accounts.request_tokens(acc, 10)
    provider_acc = acc
    consumer_acc = acc

    # Publish compute
    example_metadata.compute_ddo["main"]["dateCreated"] = next(date_created)
    ddo_compute = nevermined.assets.create_compute(
        example_metadata.metadata, provider_acc, asset_rewards, providers=[provider]
    )
    assert ddo_compute is not None, "Creating compute asset on-chain failed."
    print(
        f"[PROVIDER --> NEVERMINED] Publishing compute to the data asset: {ddo_compute.did}"
    )

    # Publish algorithm
    example_metadata.algo_metadata["main"]["dateCreated"] = next(date_created)
    ddo_transformation = nevermined.assets.create(
        example_metadata.algo_metadata, consumer_acc, providers=[provider]
    )
    assert (
        ddo_transformation is not None
    ), "Creating asset transformation on-chain failed."
    print(
        f"[CONSUMER --> NEVERMINED] Publishing algorithm asset: {ddo_transformation.did}"
    )

    # Publish workflows
    workflow_metadata = example_metadata.workflow_ddo
    workflow_metadata["main"]["workflow"]["stages"][0]["input"][0]["id"] = ddo_compute.did
    workflow_metadata["main"]["workflow"]["stages"][0]["transformation"][
        "id"
    ] = ddo_transformation.did

    ddo_workflow = nevermined.assets.create(
        workflow_metadata, consumer_acc, providers=[provider]
    )
    assert ddo_workflow is not None, "Creating asset workflow on-chain failed."
    print(f"[CONSUMER --> NEVERMINED] Publishing compute workflow: {ddo_workflow.did}")

    # Order computation
    service = ddo_compute.get_service(service_type=ServiceTypes.CLOUD_COMPUTE)
    service_agreement = ServiceAgreement.from_service_dict(service.as_dictionary())
    agreement_id = nevermined.assets.order(
        ddo_compute.did, service_agreement.index, consumer_acc
    )
    print(
        f"[CONSUMER --> PROVIDER] Requesting an agreement for compute to the data: {agreement_id}"
    )

    event = keeper.lock_payment_condition.subscribe_condition_fulfilled(
        agreement_id, 60, None, (), wait=True
    )
    assert event is not None, "Reward condition is not found"

    event = keeper.compute_execution_condition.subscribe_condition_fulfilled(
        agreement_id, 60, None, (), wait=True
    )
    assert event is not None, "Execution condition not found"

    # Execute workflow
    nevermined.assets.execute(
        agreement_id,
        ddo_compute.did,
        service_agreement.index,
        consumer_acc,
        ddo_workflow.did,
    )
    print("[CONSUMER --> PROVIDER] Requesting execution of the compute workflow")

    event = keeper.escrow_payment_condition.subscribe_condition_fulfilled(
        agreement_id, 60, None, (), wait=True
    )
    assert event is not None, "Escrow Reward condition not found"
    print("Workflow successfully executed")
コード例 #8
0
from examples import ExampleConfig
from nevermined_sdk_py import ConfigProvider
from nevermined_sdk_py.nevermined.keeper import NeverminedKeeper as Keeper
from nevermined_sdk_py.assets.asset_executor import AssetExecutor
from nevermined_sdk_py.assets.asset_consumer import AssetConsumer
from nevermined_sdk_py.nevermined.agreements import Agreements
from tests.resources.helper_functions import (
    _get_asset, get_algorithm_ddo, get_consumer_account, get_consumer_instance,
    get_ddo_sample, get_metadata, get_publisher_account,
    get_publisher_instance, get_registered_ddo, get_workflow_ddo,
    setup_logging)
from tests.resources.mocks.secret_store_mock import SecretStoreMock

setup_logging()

ConfigProvider.set_config(ExampleConfig.get_config())
metadata_provider = Metadata(ConfigProvider.get_config().metadata_url)


@pytest.fixture(autouse=True)
def setup_all():
    config = ExampleConfig.get_config()
    Web3Provider.get_web3(config.keeper_url)
    ContractHandler.artifacts_path = config.keeper_path
    Keeper.get_instance(artifacts_path=config.keeper_path)


@pytest.fixture
def secret_store():
    return SecretStoreMock
コード例 #9
0
def buy_asset():
    """
    Requires all Nevermined services running.

    """
    ConfigProvider.set_config(ExampleConfig.get_config())
    config = ConfigProvider.get_config()
    providers = {
        'duero': '0xfEF2d5e1670342b9EF22eeeDcb287EC526B48095',
        'nile': '0x4aaab179035dc57b35e2ce066919048686f82972'
    }
    # make nevermined instance
    nevermined = Nevermined()
    Diagnostics.verify_contracts()
    acc = get_account(1)
    if not acc:
        acc = ([acc for acc in nevermined.accounts.list() if acc.password] or nevermined.accounts.list())[0]

    keeper = Keeper.get_instance()
    # Register ddo
    did = ''  # 'did:nv:7648596b60f74301ae1ef9baa5d637255d517ff362434754a3779e1de4c8219b'
    if did:
        ddo = nevermined.assets.resolve(did)
        logging.info(f'using ddo: {did}')
    else:
        ddo = nevermined.assets.create(example_metadata.metadata, acc, providers=[], authorization_type='SecretStore', use_secret_store=True)
        assert ddo is not None, f'Registering asset on-chain failed.'
        did = ddo.did
        logging.info(f'registered ddo: {did}')
        # nevermined here will be used only to publish the asset. Handling the asset by the publisher
        # will be performed by the Gateway server running locally
        test_net = os.environ.get('TEST_NET', '')
        if test_net.startswith('nile'):
            provider = keeper.did_registry.to_checksum_address(providers['nile'])
        elif test_net.startswith('duero'):
            provider = keeper.did_registry.to_checksum_address(providers['duero'])
        else:
            provider = '0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0'

        # Wait for did registry event
        event = keeper.did_registry.subscribe_to_event(
            keeper.did_registry.DID_REGISTRY_EVENT_NAME,
            30,
            event_filter={
                '_did': Web3Provider.get_web3().toBytes(hexstr=ddo.asset_id),
                '_owner': acc.address},
            wait=True
        )
        if not event:
            logging.warning(f'Failed to get the did registry event for asset with did {did}.')
        assert keeper.did_registry.get_block_number_updated(ddo.asset_id) > 0, \
            f'There is an issue in registering asset {did} on-chain.'

        keeper.did_registry.add_provider(ddo.asset_id, provider, acc)
        logging.info(f'is {provider} set as did provider: '
                     f'{keeper.did_registry.is_did_provider(ddo.asset_id, provider)}')

    nevermined_cons = Nevermined()
    consumer_account = get_account(0)

    # sign agreement using the registered asset did above
    service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS)
    # This will send the order request to Gateway which in turn will execute the agreement on-chain
    nevermined_cons.accounts.request_tokens(consumer_account, 10)
    sa = ServiceAgreement.from_service_dict(service.as_dictionary())
    agreement_id = ''
    if not agreement_id:
        # Use these 2 lines to request new agreement from Gateway
        # agreement_id, signature = nevermined_cons.agreements.prepare(did, sa.service_definition_id,
        # consumer_account)
        # nevermined_cons.agreements.send(did, agreement_id, sa.service_definition_id, signature,
        # consumer_account)

        # assets.order now creates agreement directly using consumer account.
        agreement_id = nevermined_cons.assets.order(
            did, sa.index, consumer_account)

    logging.info('placed order: %s, %s', did, agreement_id)
    event = keeper.access_template.subscribe_agreement_created(
        agreement_id, 60, None, (), wait=True
    )
    assert event, "Agreement event is not found, check the keeper node's logs"
    logging.info(f'Got agreement event, next: lock reward condition')

    event = keeper.lock_payment_condition.subscribe_condition_fulfilled(
        agreement_id, 60, None, (), wait=True
    )
    assert event, "Lock reward condition fulfilled event is not found, check the keeper node's logs"
    logging.info('Got lock reward event, next: wait for the access condition..')

    event = keeper.access_condition.subscribe_condition_fulfilled(
        agreement_id, 15, None, (), wait=True
    )
    logging.info(f'Got access event {event}')
    i = 0
    while nevermined.agreements.is_access_granted(
            agreement_id, did, consumer_account.address) is not True and i < 15:
        time.sleep(1)
        i += 1

    assert nevermined.agreements.is_access_granted(agreement_id, did, consumer_account.address)

    nevermined.assets.access(
        agreement_id,
        did,
        sa.index,
        consumer_account,
        config.downloads_path,
        index=0)
    logging.info('Success buying asset.')

    event = keeper.escrow_payment_condition.subscribe_condition_fulfilled(
        agreement_id,
        30,
        None,
        (),
        wait=True
    )
    assert event, 'no event for EscrowReward.Fulfilled'
    logging.info(f'got EscrowReward.FULFILLED event: {event}')
    logging.info('Done buy asset.')