def test_service_factory(): ddo = DDO(dictionary=_get_sample_ddo('ddo_sample1.json')) type_to_service = {s.type: s for s in ddo.services} metadata = ddo.metadata md_descriptor = ServiceDescriptor.metadata_service_descriptor(metadata, type_to_service[ServiceTypes.METADATA].service_endpoint) access_service = type_to_service[ServiceTypes.ASSET_ACCESS] access_descriptor = ServiceDescriptor.access_service_descriptor(access_service.attributes, access_service.service_endpoint, access_service.template_id) compute_descriptor = ServiceDescriptor.compute_service_descriptor(access_service.attributes, access_service.service_endpoint, access_service.template_id) services = ServiceFactory.build_services([md_descriptor, access_descriptor, compute_descriptor]) assert len(services) == 3 assert services[0].type == ServiceTypes.METADATA assert services[1].type == ServiceTypes.ASSET_ACCESS assert services[2].type == ServiceTypes.CLOUD_COMPUTE keeper = Keeper.get_instance() name_to_address = {name: i.address for name, i in keeper.contract_name_to_instance.items()} s = services[1] s.init_conditions_values(ddo.did, name_to_address) s = services[2] s.init_conditions_values(ddo.did, name_to_address) assert isinstance(services[1], ServiceAgreement) assert isinstance(services[2], ServiceAgreement)
def test_ddo_dict(): sample_ddo_path = get_resource_path('ddo', 'ddo_sample1.json') assert sample_ddo_path.exists(), f'{sample_ddo_path} does not exist!' ddo1 = DDO(json_filename=sample_ddo_path) assert len(ddo1.public_keys) == 3 assert ddo1.did == 'did:op:0c184915b07b44c888d468be85a9b28253e80070e5294b1aaed81c2f0264e429'
def test_create_data_asset(publisher_ocean_instance, consumer_ocean_instance): """ Setup accounts and asset, register this asset on Aquarius (MetaData store) """ pub_ocn = publisher_ocean_instance cons_ocn = consumer_ocean_instance logging.debug("".format()) sample_ddo_path = get_resource_path('ddo', 'ddo_sa_sample.json') assert sample_ddo_path.exists(), "{} does not exist!".format( sample_ddo_path) ########################################################## # Setup 2 accounts ########################################################## aquarius_acct = pub_ocn.main_account consumer_acct = cons_ocn.main_account # ensure Ocean token balance if pub_ocn.accounts.balance(aquarius_acct).ocn == 0: rcpt = pub_ocn.accounts.request_tokens(aquarius_acct, 200) Web3Provider.get_web3().eth.waitForTransactionReceipt(rcpt) if cons_ocn.accounts.balance(consumer_acct).ocn == 0: rcpt = cons_ocn.accounts.request_tokens(consumer_acct, 200) Web3Provider.get_web3().eth.waitForTransactionReceipt(rcpt) # You will need some token to make this transfer! assert pub_ocn.accounts.balance(aquarius_acct).ocn > 0 assert cons_ocn.accounts.balance(consumer_acct).ocn > 0 ########################################################## # Create an Asset with valid metadata ########################################################## asset = DDO(json_filename=sample_ddo_path) ########################################################## # List currently published assets ########################################################## meta_data_assets = pub_ocn.assets.search('') if meta_data_assets: print("Currently registered assets:") print(meta_data_assets) if asset.did in meta_data_assets: pub_ocn.assets.resolve(asset.did) pub_ocn.assets.retire(asset.did) # Publish the metadata new_asset = pub_ocn.assets.create(asset.metadata, aquarius_acct) # get_asset_metadata only returns 'main' key, is this correct? published_metadata = cons_ocn.assets.resolve(new_asset.did) assert published_metadata # only compare top level keys assert sorted(list( asset.metadata['main'].keys())).remove('files') == sorted( list(published_metadata.metadata.keys())).remove('encryptedFiles') publisher_ocean_instance.assets.retire(new_asset.did)
def test_create_asset_with_address(publisher_ocean_instance): """Tests that an asset can be created with specific DT address.""" ocn = publisher_ocean_instance alice = get_publisher_wallet() sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json") asset = DDO(json_filename=sample_ddo_path) asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4()) my_secret_store = "http://myownsecretstore.com" auth_service = ServiceDescriptor.authorization_service_descriptor( my_secret_store) token = ocn.create_data_token("DataToken1", "DT1", from_wallet=alice, blob="foo_blob") assert ocn.assets.create(asset.metadata, alice, [auth_service], data_token_address=token.address)
def create_asset(publisher_ocean_instance): ocn = publisher_ocean_instance sample_ddo_path = get_resource_path('ddo', 'ddo_sa_sample.json') assert sample_ddo_path.exists(), "{} does not exist!".format( sample_ddo_path) acct = ocn.main_account asset = DDO(json_filename=sample_ddo_path) my_secret_store = 'http://myownsecretstore.com' auth_service = ServiceDescriptor.authorization_service_descriptor( my_secret_store) return ocn.assets.create(asset.metadata, acct, [auth_service])
def test_create_asset_with_different_secret_store(publisher_ocean_instance): ocn = publisher_ocean_instance sample_ddo_path = get_resource_path('ddo', 'ddo_sa_sample.json') assert sample_ddo_path.exists(), "{} does not exist!".format( sample_ddo_path) acct = ocn.main_account aqua = AquariusProvider.get_aquarius(ocn.config.aquarius_url) aqua.retire_all_assets() asset = DDO(json_filename=sample_ddo_path) my_secret_store = 'http://myownsecretstore.com' auth_service = ServiceDescriptor.authorization_service_descriptor( my_secret_store) asset.metadata['main']['name'] += str(datetime.now().timestamp()) new_asset = ocn.assets.create(asset.metadata, acct, [auth_service]) assert new_asset.get_service( ServiceTypes.AUTHORIZATION).service_endpoint == my_secret_store assert new_asset.get_service(ServiceTypes.ASSET_ACCESS) assert new_asset.get_service(ServiceTypes.METADATA) publisher_ocean_instance.assets.retire(new_asset.did) access_service = ServiceDescriptor.access_service_descriptor( { "main": { "name": "dataAssetAccessServiceAgreement", "creator": '0x1234', "price": '1', "timeout": 3600, "datePublished": '2019-08-30T12:19:54Z' } }, 'service/endpoint', '0x0011001100110011') asset.metadata['main']['name'] += str(datetime.now().timestamp()) new_asset = ocn.assets.create(asset.metadata, acct, [access_service]) assert new_asset.get_service(ServiceTypes.AUTHORIZATION) assert new_asset.get_service(ServiceTypes.ASSET_ACCESS) assert new_asset.get_service(ServiceTypes.METADATA) publisher_ocean_instance.assets.retire(new_asset.did) asset.metadata['main']['name'] += str(datetime.now().timestamp()) new_asset = ocn.assets.create(asset.metadata, acct) assert new_asset.get_service(ServiceTypes.AUTHORIZATION) assert new_asset.get_service(ServiceTypes.ASSET_ACCESS) assert new_asset.get_service(ServiceTypes.METADATA) publisher_ocean_instance.assets.retire(new_asset.did)
def test_verify_order_tx(alice_address, bob_address, alice_ocean, alice_wallet): """Tests verify_order_tx function.""" alice_w3 = alice_ocean.web3.eth.blockNumber token = alice_ocean.create_data_token( "DataToken1", "DT1", from_wallet=alice_wallet, blob="foo_blob" ) token.mint(alice_address, to_base_18(100.0), from_wallet=alice_wallet) token.approve(bob_address, to_base_18(1.0), from_wallet=alice_wallet) transfer_tx_id = token.transfer( bob_address, to_base_18(5.0), from_wallet=alice_wallet ) with pytest.raises(AssertionError): # dummy tx id token.verify_order_tx( alice_w3, "0x0", "some_did", "some_index", "some_amount", alice_address ) transfer_tx_id = token.transfer( bob_address, to_base_18(5.0), from_wallet=alice_wallet ) with pytest.raises(AssertionError): # tx id is from transfer, not order token.verify_order_tx( alice_w3, transfer_tx_id, "some_did", "some_index", "some_amount", alice_address, ) sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json") asset = DDO(json_filename=sample_ddo_path) order_tx_id = token.startOrder( alice_address, to_base_18(1.0), 1, ZERO_ADDRESS, alice_wallet ) with pytest.raises(AssertionError): # the wrong asset did, this is a sample token.verify_order_tx( alice_w3, order_tx_id, asset.did, "some_index", "some_amount", alice_address )
def get_asset_ddo(self, did): """ Retrieve asset ddo for a given did. :param did: Asset DID string :return: DDO instance """ response = self.requests_session.get(f'{self.url}/{did}').content if not response: return {} try: parsed_response = json.loads(response) except TypeError: parsed_response = None except ValueError: raise ValueError(response.decode('UTF-8')) if parsed_response is None: return {} return DDO(dictionary=parsed_response)
def search(self, text, sort=None, offset=100, page=1, aquarius_url=None): """ Search an asset in oceanDB using aquarius. :param text: String with the value that you are searching :param sort: Dictionary to choose order main in some value :param offset: Number of elements shows by page :param page: Page number :param aquarius_url: Url of the aquarius where you want to search. If there is not provided take the default :return: List of assets that match with the query """ assert page >= 1, f'Invalid page value {page}. Required page >= 1.' logger.info(f'Searching asset containing: {text}') return [ DDO(dictionary=ddo_dict) for ddo_dict in self._get_aquarius(aquarius_url).text_search( text, sort, offset, page)['results'] ]
def query(self, query, sort=None, offset=100, page=1, aquarius_url=None): """ Search an asset in oceanDB using search query. :param query: dict with query parameters (e.g.) https://github.com/oceanprotocol/aquarius/blob/develop/docs/for_api_users/API.md :param sort: Dictionary to choose order main in some value :param offset: Number of elements shows by page :param page: Page number :param aquarius_url: Url of the aquarius where you want to search. If there is not provided take the default :return: List of assets that match with the query. """ logger.info(f'Searching asset query: {query}') aquarius = self._get_aquarius(aquarius_url) return [ DDO(dictionary=ddo_dict) for ddo_dict in aquarius.query_search( query, sort, offset, page)['results'] ]
def get_ddo_sample(datatoken_address): did = f'did:op:{remove_0x_prefix(datatoken_address)}' sample_ddo_path = get_resource_path('ddo', 'ddo_sa_sample.json') assert sample_ddo_path.exists(), "{} does not exist!".format(sample_ddo_path) asset = DDO(json_filename=sample_ddo_path) asset.metadata['main']['files'][0]['checksum'] = str(uuid.uuid4()) checksum_dict = dict() for service in asset.services: checksum_dict[str(service.index)] = checksum(service.main) asset.add_proof(checksum_dict, get_publisher_wallet()) asset._did = did return asset
def get_ddo_sample(datatoken_address): """Helper function to get a sample ddo for testing.""" did = f"did:op:{remove_0x_prefix(datatoken_address)}" sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json") assert sample_ddo_path.exists(), "{} does not exist!".format(sample_ddo_path) asset = DDO(json_filename=sample_ddo_path) asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4()) checksum_dict = dict() for service in asset.services: checksum_dict[str(service.index)] = checksum(service.main) asset.add_proof(checksum_dict, get_publisher_wallet()) asset._did = did return asset
def test_create_ddo(metadata): pub_acc = get_publisher_account() ddo = DDO() ddo.add_service(ServiceTypes.METADATA, 'http://myaquarius.com', values=metadata, index='0') checksums = dict() for service in ddo.services: checksums[str(service.index)] = checksum(service.main) ddo.add_proof(checksums, pub_acc) did = ddo.assign_did(DID.did(ddo.proof['checksum'])) ddo.proof['signatureValue'] = Keeper.sign_hash(did_to_id_bytes(did), pub_acc) ddo.add_public_key(did, pub_acc.address) ddo.add_authentication(did, PUBLIC_KEY_TYPE_RSA)
def create(self, metadata, publisher_account, service_descriptors=None, providers=None, use_secret_store=True): """ Register an asset in both the keeper's DIDRegistry (on-chain) and in the Metadata store ( Aquarius). :param metadata: dict conforming to the Metadata accepted by Ocean Protocol. :param publisher_account: Account of the publisher registering this asset :param service_descriptors: list of ServiceDescriptor tuples of length 2. The first item must be one of ServiceTypes and the second item is a dict of parameters and values required by the service :param providers: list of addresses of providers of this asset (a provider is an ethereum account that is authorized to provide asset services) :param use_secret_store: bool indicate whether to use the secret store directly for encrypting urls (Uses Brizo provider service if set to False) :return: DDO instance """ assert isinstance( metadata, dict), f'Expected metadata of type dict, got {type(metadata)}' assert service_descriptors is None or isinstance(service_descriptors, list), \ f'bad type of `service_descriptors` {type(service_descriptors)}' # if not metadata or not Metadata.validate(metadata): # raise OceanInvalidMetadata('Metadata seems invalid. Please make sure' # ' the required metadata values are filled in.') # copy metadata so we don't change the original metadata_copy = copy.deepcopy(metadata) asset_type = metadata_copy['main']['type'] assert asset_type in ( 'dataset', 'algorithm'), f'Invalid/unsupported asset type {asset_type}' service_descriptors = service_descriptors or [] brizo = BrizoProvider.get_brizo() services = self._process_service_descriptors(service_descriptors, metadata_copy, publisher_account) stype_to_service = {s.type: s for s in services} checksum_dict = dict() for service in services: checksum_dict[str(service.index)] = checksum(service.main) # Create a DDO object ddo = DDO() # Adding proof to the ddo. ddo.add_proof(checksum_dict, publisher_account) # Generating the did and adding to the ddo. did = ddo.assign_did(DID.did(ddo.proof['checksum'])) logger.debug(f'Generating new did: {did}') # Check if it's already registered first! if did in self._get_aquarius().list_assets(): raise OceanDIDAlreadyExist( f'Asset id {did} is already registered to another asset.') md_service = stype_to_service[ServiceTypes.METADATA] ddo_service_endpoint = md_service.service_endpoint if '{did}' in ddo_service_endpoint: ddo_service_endpoint = ddo_service_endpoint.replace('{did}', did) md_service.set_service_endpoint(ddo_service_endpoint) # Populate the ddo services ddo.add_service(md_service) ddo.add_service(stype_to_service[ServiceTypes.AUTHORIZATION]) access_service = stype_to_service.get(ServiceTypes.ASSET_ACCESS, None) compute_service = stype_to_service.get(ServiceTypes.CLOUD_COMPUTE, None) if access_service: access_service.init_conditions_values( did, { cname: c.address for cname, c in self._keeper.contract_name_to_instance.items() }) ddo.add_service(access_service) if compute_service: compute_service.init_conditions_values( did, { cname: c.address for cname, c in self._keeper.contract_name_to_instance.items() }) ddo.add_service(compute_service) ddo.proof['signatureValue'] = self._keeper.sign_hash( add_ethereum_prefix_and_hash_msg(did_to_id_bytes(did)), publisher_account) # Add public key and authentication ddo.add_public_key(did, publisher_account.address) ddo.add_authentication(did, PUBLIC_KEY_TYPE_RSA) # Setup metadata service # First compute files_encrypted if metadata_copy['main']['type'] == 'dataset': assert metadata_copy['main']['files'], \ 'files is required in the metadata main attributes.' logger.debug('Encrypting content urls in the metadata.') if not use_secret_store: encrypt_endpoint = brizo.get_encrypt_endpoint(self._config) files_encrypted = brizo.encrypt_files_dict( metadata_copy['main']['files'], encrypt_endpoint, ddo.asset_id, publisher_account.address, self._keeper.sign_hash( add_ethereum_prefix_and_hash_msg(ddo.asset_id), publisher_account)) else: files_encrypted = self._get_secret_store(publisher_account) \ .encrypt_document( did_to_id(did), json.dumps(metadata_copy['main']['files']), ) # only assign if the encryption worked if files_encrypted: logger.debug( f'Content urls encrypted successfully {files_encrypted}') index = 0 for file in metadata_copy['main']['files']: file['index'] = index index = index + 1 del file['url'] metadata_copy['encryptedFiles'] = files_encrypted else: raise AssertionError('Encrypting the files failed.') # DDO url and `Metadata` service logger.debug(f'Generated ddo and services, DID is {ddo.did},' f' metadata service @{ddo_service_endpoint}.') response = None # register on-chain registered_on_chain = self._keeper.did_registry.register( ddo.asset_id, checksum=Web3Provider.get_web3().toBytes(hexstr=ddo.asset_id), url=ddo_service_endpoint, account=publisher_account, providers=providers) if registered_on_chain is False: logger.warning(f'Registering {did} on-chain failed.') return None logger.info(f'Successfully registered DDO (DID={did}) on chain.') try: # publish the new ddo in ocean-db/Aquarius response = self._get_aquarius().publish_asset_ddo(ddo) logger.info('Asset/ddo published successfully in aquarius.') except ValueError as ve: raise ValueError( f'Invalid value to publish in the metadata: {str(ve)}') except Exception as e: logger.error(f'Publish asset in aquarius failed: {str(e)}') if not response: return None return ddo
def get_registered_ddo(account, metadata, service_descriptor, providers=None): keeper = keeper_instance() aqua = Aquarius('http://localhost:5000') ddo = DDO() ddo_service_endpoint = aqua.get_service_endpoint() metadata_service_desc = ServiceDescriptor.metadata_service_descriptor( metadata, ddo_service_endpoint) service_descriptors = list([ ServiceDescriptor.authorization_service_descriptor( 'http://localhost:12001') ]) service_descriptors.append(service_descriptor) service_type = service_descriptor[0] service_descriptors = [metadata_service_desc] + service_descriptors services = ServiceFactory.build_services(service_descriptors) checksums = dict() for service in services: checksums[str(service.index)] = checksum(service.main) # Adding proof to the ddo. ddo.add_proof(checksums, account) did = ddo.assign_did(DID.did(ddo.proof['checksum'])) ddo_service_endpoint.replace('{did}', did) services[0].set_service_endpoint(ddo_service_endpoint) stype_to_service = {s.type: s for s in services} _service = stype_to_service[service_type] name_to_address = { cname: cinst.address for cname, cinst in keeper.contract_name_to_instance.items() } _service.init_conditions_values(did, contract_name_to_address=name_to_address) for service in services: ddo.add_service(service) ddo.proof['signatureValue'] = keeper.sign_hash(did_to_id_bytes(did), account) ddo.add_public_key(did, account.address) ddo.add_authentication(did, PUBLIC_KEY_TYPE_RSA) try: _oldddo = aqua.get_asset_ddo(ddo.did) if _oldddo: aqua.retire_asset_ddo(ddo.did) except ValueError: pass if not plecos.is_valid_dict_local(ddo.metadata): print(f'invalid metadata: {plecos.validate_dict_local(ddo.metadata)}') assert False, f'invalid metadata: {plecos.validate_dict_local(ddo.metadata)}' encrypted_files = do_secret_store_encrypt( remove_0x_prefix(ddo.asset_id), json.dumps(metadata['main']['files']), account, get_config()) # only assign if the encryption worked if encrypted_files: index = 0 for file in metadata['main']['files']: file['index'] = index index = index + 1 del file['url'] metadata['encryptedFiles'] = encrypted_files keeper_instance().did_registry.register( ddo.asset_id, checksum=web3().toBytes(hexstr=ddo.asset_id), url=ddo_service_endpoint, account=account, providers=providers) try: aqua.publish_asset_ddo(ddo) except Exception as e: print(f'error publishing ddo {ddo.did} in Aquarius: {e}') raise return ddo
def test_creating_ddo_from_scratch(): # create an empty ddo ddo = DDO() assert ddo.did is None assert ddo.asset_id is None assert ddo.created is not None did = DID.did({"0": "0x99999999999999999"}) ddo.assign_did(did) assert ddo.did == did pub_acc = get_publisher_account() ddo.add_service(TEST_SERVICE_TYPE, TEST_SERVICE_URL) # add a proof to the first public_key/authentication ddo.add_proof('checksum', pub_acc) ddo_text_proof = ddo.as_text() assert ddo_text_proof pub_acc = get_publisher_account() assert not ddo.public_keys ddo.add_public_key(did, pub_acc.address) assert len(ddo.public_keys) == 1 assert ddo.get_public_key(0) == ddo.public_keys[0] with pytest.raises(IndexError): ddo.get_public_key(1) assert ddo.get_public_key(did) == ddo.public_keys[0] assert ddo.get_public_key('0x32233') is None assert not ddo.authentications ddo.add_authentication(did, '') assert len(ddo.authentications) == 1
def registered_ddo(): return DDO()
def test_order(publisher_ocean_instance, consumer_ocean_instance): consumer_account = consumer_ocean_instance.main_account publisher_account = publisher_ocean_instance.main_account keeper = publisher_ocean_instance.keeper ocean = consumer_ocean_instance BrizoProvider.set_brizo_class(Brizo) SecretStoreProvider.set_secret_store_class(SecretStore) sample_ddo_path = get_resource_path('ddo', 'ddo_with_compute_service.json') old_ddo = DDO(json_filename=sample_ddo_path) metadata = old_ddo.metadata metadata['main']['files'][0]['checksum'] = str(uuid.uuid4()) template_name = keeper.template_manager.SERVICE_TO_TEMPLATE_NAME[ServiceTypes.CLOUD_COMPUTE] service = old_ddo.get_service(ServiceTypes.CLOUD_COMPUTE) brizo = BrizoProvider.get_brizo() compute_service = ServiceDescriptor.compute_service_descriptor( service.attributes, brizo.get_compute_endpoint(ocean.config), keeper.template_manager.create_template_id(template_name) ) compute_ddo = publisher_ocean_instance.assets.create( metadata, publisher_account, providers=[], service_descriptors=[compute_service], ) did = compute_ddo.did _compute_ddo = publisher_ocean_instance.assets.resolve(compute_ddo.did) algorithm_ddo_path = get_resource_path('ddo', 'ddo_sample_algorithm.json') algo_main = DDO(json_filename=algorithm_ddo_path).metadata['main'] algo_meta_dict = algo_main['algorithm'].copy() algo_meta_dict['url'] = algo_main['files'][0]['url'] algorithm_meta = AlgorithmMetadata(algo_meta_dict) try: ocean.accounts.request_tokens(publisher_account, 50*keeper.dispenser.get_scale()) except Exception as err: print(f'Requesting tokens failed: {err}') agreement_id = ocean.compute.order( compute_ddo.did, consumer_account, None, None ) assert agreement_id, f'got agreementId {agreement_id}' def fulfill_compute_execution(_, ocn_instance, agr_id, _did, cons_address, account): ocn_instance.agreements.conditions.grant_compute( agr_id, _did, cons_address, account) event = keeper.lock_reward_condition.subscribe_condition_fulfilled( agreement_id, 15, fulfill_compute_execution, (publisher_ocean_instance, agreement_id, compute_ddo.did, consumer_account.address, publisher_account), from_block=0, wait=True ) assert event, f'lock condition event is not found.' print(f'processed agreement and got lockReward fulfilled: ' f'{agreement_id}, {compute_ddo.did}, ') event = keeper.compute_execution_condition.subscribe_condition_fulfilled( agreement_id, 20, None, (agreement_id, compute_ddo.did, consumer_account, None, algorithm_meta), from_block=0, wait=True ) assert event, f'compute execution condition event is not found.' job_id = consumer_ocean_instance.compute.start(agreement_id, consumer_account, algorithm_meta=algorithm_meta) assert job_id, f'expected a job id, got {job_id}' status = consumer_ocean_instance.compute.status(agreement_id, job_id, consumer_account) print(f'got job status: {status}') assert status and status['ok'], f'something not right about the compute job, got status: {status}' status = consumer_ocean_instance.compute.stop(agreement_id, job_id, consumer_account) print(f'got job status after requesting stop: {status}') assert status, f'something not right about the compute job, got status: {status}'
def _get_asset(file_name): sample_ddo_path = get_resource_path('ddo', file_name) assert sample_ddo_path.exists(), "{} does not exist!".format( sample_ddo_path) return DDO(json_filename=sample_ddo_path)
def get_registered_ddo(account, providers=None): keeper = keeper_instance() aqua = Aquarius('http://localhost:5000') for did in aqua.list_assets(): aqua.retire_asset_ddo(did) metadata = get_sample_ddo()['service'][0]['attributes'] metadata['main']['files'][0]['checksum'] = str(uuid.uuid4()) ddo = DDO() ddo_service_endpoint = aqua.get_service_endpoint() metadata_service_desc = ServiceDescriptor.metadata_service_descriptor( metadata, ddo_service_endpoint) access_service_attributes = { "main": { "name": "dataAssetAccessServiceAgreement", "creator": account.address, "price": metadata[MetadataMain.KEY]['price'], "timeout": 3600, "datePublished": metadata[MetadataMain.KEY]['dateCreated'] } } service_descriptors = [ ServiceDescriptor.authorization_service_descriptor( 'http://localhost:12001') ] service_descriptors += [ ServiceDescriptor.access_service_descriptor( access_service_attributes, 'http://localhost:8030', keeper.escrow_access_secretstore_template.address) ] service_descriptors = [metadata_service_desc] + service_descriptors services = ServiceFactory.build_services(service_descriptors) checksums = dict() for service in services: checksums[str(service.index)] = checksum(service.main) # Adding proof to the ddo. ddo.add_proof(checksums, account) did = ddo.assign_did(DID.did(ddo.proof['checksum'])) stype_to_service = {s.type: s for s in services} access_service = stype_to_service[ServiceTypes.ASSET_ACCESS] name_to_address = { cname: cinst.address for cname, cinst in keeper.contract_name_to_instance.items() } access_service.init_conditions_values( did, contract_name_to_address=name_to_address) ddo.add_service(access_service) for service in services: ddo.add_service(service) ddo.proof['signatureValue'] = keeper.sign_hash(did_to_id_bytes(did), account) ddo.add_public_key(did, account.address) ddo.add_authentication(did, PUBLIC_KEY_TYPE_RSA) encrypted_files = do_secret_store_encrypt( remove_0x_prefix(ddo.asset_id), json.dumps(metadata['main']['files']), account, get_config()) _files = metadata['main']['files'] # only assign if the encryption worked if encrypted_files: index = 0 for file in metadata['main']['files']: file['index'] = index index = index + 1 del file['url'] metadata['encryptedFiles'] = encrypted_files keeper_instance().did_registry.register( ddo.asset_id, checksum=web3().toBytes(hexstr=ddo.asset_id), url=ddo_service_endpoint, account=account, providers=providers) aqua.publish_asset_ddo(ddo) return ddo
def get_computing_ddo(): return DDO(json_filename=get_resource_path('ddo', 'ddo_computing.json'))
def get_ddo_sample(): return DDO(json_filename=get_resource_path('ddo', 'ddo_sa_sample.json'))
def get_registered_ddo(account, metadata, service_descriptor): aqua = Aquarius('http://localhost:5000') ddo = DDO() ddo_service_endpoint = aqua.get_service_endpoint() # Create new data token contract dt_contract = FactoryContract(get_config().factory_address)\ .create_data_token(account, metadata_url=ddo_service_endpoint) if not dt_contract: raise AssertionError('Creation of data token contract failed.') ddo._other_values = {'dataTokenAddress': dt_contract.address} metadata_service_desc = ServiceDescriptor.metadata_service_descriptor( metadata, ddo_service_endpoint ) service_descriptors = list( [ServiceDescriptor.authorization_service_descriptor('http://localhost:12001')]) service_descriptors.append(service_descriptor) service_type = service_descriptor[0] service_descriptors = [metadata_service_desc] + service_descriptors services = ServiceFactory.build_services(service_descriptors) checksums = dict() for service in services: checksums[str(service.index)] = checksum(service.main) # Adding proof to the ddo. ddo.add_proof(checksums, account) did = ddo.assign_did(DID.did(ddo.proof['checksum'])) ddo_service_endpoint.replace('{did}', did) services[0].set_service_endpoint(ddo_service_endpoint) stype_to_service = {s.type: s for s in services} _service = stype_to_service[service_type] for service in services: ddo.add_service(service) # ddo.proof['signatureValue'] = ocean_lib.sign_hash( # did_to_id_bytes(did), account) ddo.add_public_key(did, account.address) ddo.add_authentication(did, PUBLIC_KEY_TYPE_RSA) try: _oldddo = aqua.get_asset_ddo(ddo.did) if _oldddo: aqua.retire_asset_ddo(ddo.did) except ValueError: pass if not plecos.is_valid_dict_local(ddo.metadata): print(f'invalid metadata: {plecos.validate_dict_local(ddo.metadata)}') assert False, f'invalid metadata: {plecos.validate_dict_local(ddo.metadata)}' encrypted_files = do_encrypt( json.dumps(metadata['main']['files']), account, ) # only assign if the encryption worked if encrypted_files: index = 0 for file in metadata['main']['files']: file['index'] = index index = index + 1 del file['url'] metadata['encryptedFiles'] = encrypted_files # ddo._other_values try: aqua.publish_asset_ddo(ddo) except Exception as e: print(f'error publishing ddo {ddo.did} in Aquarius: {e}') raise return ddo