Esempio n. 1
0
def agreements():
    publisher_acc = get_publisher_account()
    keeper = Keeper.get_instance()
    w3 = Web3Provider.get_web3()
    did_resolver = Mock()
    ddo = get_ddo_sample()
    service = ddo.get_service(ServiceTypes.ASSET_ACCESS)
    service.update_value(ServiceAgreementTemplate.TEMPLATE_ID_KEY,
                         w3.toChecksumAddress(publisher_acc.address))
    did_resolver.resolve = MagicMock(return_value=ddo)

    return Agreements(keeper, did_resolver, AssetConsumer, AssetExecutor,
                      ConfigProvider.get_config())
Esempio n. 2
0
def test_secret_store_encrypt_decrypt():
    metadata_json = get_assset_json_text(
        'https://raw.githubusercontent.com/nevermined-io/docs/master/docs/architecture/specs'
        '/examples/access/v0.1/ddo1.json')
    document_id = hashlib.sha256((metadata_json + secrets.token_hex(32)).encode()).hexdigest()
    print(document_id)
    config = ConfigProvider.get_config()
    ss_client = Mock
    ss_client.publish_document = MagicMock(return_value='!!document!!')
    ss_client.decrypt_document = MagicMock(return_value=metadata_json)
    SecretStore.set_client(ss_client)

    ss_args = (config.secret_store_url, config.parity_url, Account('0x0000', 'aaa'))
    result = SecretStore(*ss_args).encrypt_document(document_id, metadata_json)
    print(result)
    assert SecretStore(*ss_args).decrypt_document(document_id, result) == metadata_json
Esempio n. 3
0
 def _get_raw_message(self):
     return ConfigProvider.get_config(
     ).auth_token_message or self.DEFAULT_MESSAGE
Esempio n. 4
0
 def _get_expiration(self):
     return int(ConfigProvider.get_config().auth_token_expiration
                or self.DEFAULT_EXPIRATION_TIME)
Esempio n. 5
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")
Esempio n. 6
0
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

Esempio n. 7
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.')