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 get_asset_from_metadatastore(metadata_url, document_id): aqua = Aquarius(metadata_url) return aqua.get_asset_ddo(document_id)
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