def get_registered_ddo(client, wallet, metadata, service_descriptor): aqua = Aquarius('http://localhost:5000') ddo_service_endpoint = aqua.get_service_endpoint() metadata_store_url = json.dumps({'t': 1, 'url': ddo_service_endpoint}) # Create new data token contract addresses = get_contracts_addresses(get_address_file()) dt_address = addresses.get(DTFactory.CONTRACT_NAME) if dt_address: factory_contract = DTFactory(dt_address) else: factory_contract = new_factory_contract() ddo_contract_address = addresses.get(MetadataContract.CONTRACT_NAME) metadata_contract = MetadataContract(ddo_contract_address) tx_id = factory_contract.createToken(metadata_store_url, 'DataToken1', 'DT1', to_base_18(1000000), wallet) dt_contract = DataToken(factory_contract.get_token_address(tx_id)) if not dt_contract: raise AssertionError('Creation of data token contract failed.') ddo = Asset() ddo.data_token_address = 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, wallet) did = ddo.assign_did(f'did:op:{remove_0x_prefix(ddo.data_token_address)}') 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, wallet.address) ddo.add_authentication(did, PUBLIC_KEY_TYPE_RSA) # 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)}' files_list_str = json.dumps(metadata['main']['files']) encrypted_files = encrypt_document(client, did, files_list_str, wallet) # encrypted_files = do_encrypt(files_list_str, provider_wallet) # 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 web3 = Web3Provider.get_web3() block = web3.eth.blockNumber try: data = lzma.compress(web3.toBytes(text=ddo.as_text())) tx_id = metadata_contract.create(ddo.asset_id, bytes([1]), data, wallet) if not metadata_contract.verify_tx(tx_id): raise AssertionError( f'create DDO on-chain failed, transaction status is 0. Transaction hash is {tx_id}' ) except Exception as e: print(f'error publishing ddo {ddo.did} in Aquarius: {e}') raise log = metadata_contract.get_event_log( metadata_contract.EVENT_METADATA_CREATED, block, ddo.asset_id, 30) assert log, f'no ddo created event.' ddo = wait_for_ddo(aqua, ddo.did) assert ddo, f'resolve did {ddo.did} failed.' 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 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_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