def search_assets(): ConfigProvider.set_config(ExampleConfig.get_config()) ocn = Ocean() account = get_account(0) ddo = ocn.assets.create( example_metadata.metadata, account, ) sleep(ASYNC_DELAY) logging.info(f'Registered asset: did={ddo.did}, ddo={ddo.as_text()}') resolved_ddo = ocn.assets.resolve(ddo.did) logging.info( f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}' ) ddo_list = ocn.assets.search('bonding curve') logging.info( f'found {len(ddo_list)} assets that contain `bonding curve` in their metadata.' ) ddo_list = ocn.assets.query( {"query": { "text": ['Ocean protocol white paper'] }}) logging.info( f'found {len(ddo_list)} assets with name that contains `Ocean protocol white paper`' )
def get_ocean(config_file): ConfigProvider.set_config(Config(filename=config_file)) ocn = Ocean() ocn.account = get_default_account(ConfigProvider.get_config()) ocn.balance = partial(ocn.accounts.balance, ocn.account) from ocean_cli.api.assets import ( authorize, consume, decrypt, list_assets, order, publish, search ) ocn.authorize = partial(authorize, ocean=ocn) ocn.consume = partial(consume, ocean=ocn) ocn.decrypt = partial(decrypt, ocean=ocn) ocn.order = partial(order, ocean=ocn) ocn.assets.list = partial(list_assets, ocean=ocn) ocn.publish = partial(publish, ocean=ocn) ocn.search = partial(search, ocean=ocn) from ocean_cli.api.conditions import ( check_permissions ) ocn.check_permissions = partial(check_permissions, ocean=ocn) return ocn
def create_ocean_request(self, query) -> None: ConfigProvider.set_config(Config('../../ocean/config.ini')) # Make a new instance of Ocean ocean = Ocean() # or Ocean(Config('config.ini')) config = ocean.config # make account instance, assuming the ethereum account and password are set # in the config file `config.ini` account = ocean.accounts.list()[0] filename = '../../ocean/bitcoin_2017.csv' did = query['did'] my_data = genfromtxt(filename, delimiter=',') #print('data at', filename, my_data) return my_data #did = 'did:op:71fae96b1d9a4651ba0d3946603fb4c11deb724685f64780985ce32ea2dfe517' service_agreement_id = ocean.assets.order(did, 0, account) # after a short wait (seconds to minutes) the asset data files should be available in the `downloads.path` defined in config # wait a bit to let things happen print(service_agreement_id) time.sleep(20) # Asset files are saved in a folder named after the asset id dataset_dir = os.path.join(ocean.config.downloads_path, f'datafile.{asset_ddo.asset_id}.0') if os.path.exists(dataset_dir): print('asset files downloaded: {}'.format(os.listdir(dataset_dir)))
def buy_asset(): """ Requires all ocean services running. """ ConfigProvider.set_config(ExampleConfig.get_config()) config = ConfigProvider.get_config() # make ocean instance ocn = Ocean() acc = get_publisher_account(config) if not acc: acc = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] # Register ddo ddo = ocn.assets.create(Metadata.get_example(), acc, providers=[acc.address]) logging.info(f'registered ddo: {ddo.did}') # ocn here will be used only to publish the asset. Handling the asset by the publisher # will be performed by the Brizo server running locally keeper = Keeper.get_instance() if 'TEST_LOCAL_NILE' in os.environ and os.environ['TEST_LOCAL_NILE'] == '1': provider = keeper.did_registry.to_checksum_address( '0x413c9ba0a05b8a600899b41b0c62dd661e689354' ) keeper.did_registry.add_provider(ddo.asset_id, provider, acc) logging.debug(f'is did provider: ' f'{keeper.did_registry.is_did_provider(ddo.asset_id, provider)}') cons_ocn = Ocean() consumer_account = get_account_from_config(config, 'parity.address1', 'parity.password1') # sign agreement using the registered asset did above service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) # This will send the purchase request to Brizo which in turn will execute the agreement on-chain cons_ocn.accounts.request_tokens(consumer_account, 100) sa = ServiceAgreement.from_service_dict(service.as_dictionary()) agreement_id = cons_ocn.assets.order( ddo.did, sa.service_definition_id, consumer_account) logging.info('placed order: %s, %s', ddo.did, agreement_id) i = 0 while ocn.agreements.is_access_granted( agreement_id, ddo.did, consumer_account.address) is not True and i < 30: time.sleep(1) i += 1 assert ocn.agreements.is_access_granted(agreement_id, ddo.did, consumer_account.address) ocn.assets.consume( agreement_id, ddo.did, sa.service_definition_id, consumer_account, config.downloads_path) logging.info('Success buying asset.')
def resolve_asset(): ConfigProvider.set_config(ExampleConfig.get_config()) ocn = Ocean() account = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] ddo = ocn.assets.create( Metadata.get_example(), account, ) sleep(ASYNC_DELAY) logging.info(f'Registered asset: did={ddo.did}, ddo={ddo.as_text()}') resolved_ddo = ocn.assets.resolve(ddo.did) logging.info(f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}')
def resolve_asset(): ConfigProvider.set_config(ExampleConfig.get_config()) ocn = Ocean() account = get_account(0) ddo = ocn.assets.create( example_metadata.metadata, account, ) sleep(ASYNC_DELAY) logging.info(f'Registered asset: did={ddo.did}, ddo={ddo.as_text()}') resolved_ddo = ocn.assets.resolve(ddo.did) logging.info(f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}')
def sign_service_agreement(): ConfigProvider.set_config(ExampleConfig.get_config()) # make ocean instance and register an asset ocn = Ocean() acc = get_account(0) ddo = ocn.assets.create(example_metadata.metadata, acc) consumer_account = get_account(1) agreement_id, signature = ocn.agreements.prepare(ddo.did, consumer_account) sleep(ASYNC_DELAY) logging.info(f'service agreement signed: ' f'\nservice agreement id: {agreement_id}, ' f'\nsignature: {signature}')
def sign_service_agreement(): ConfigProvider.set_config(ExampleConfig.get_config()) config = ConfigProvider.get_config() # make ocean instance and register an asset ocn = Ocean() acc = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] ddo = ocn.assets.create(Metadata.get_example(), acc) consumer_account = get_account_from_config(config, 'parity.address1', 'parity.password1') service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) agreement_id, signature = ocn.agreements.prepare( ddo.did, service.service_definition_id, consumer_account) sleep(ASYNC_DELAY) logging.info(f'service agreement signed: ' f'\nservice agreement id: {agreement_id}, ' f'\nsignature: {signature}')
from squid_py.did import id_to_did from squid_py.exceptions import OceanDIDNotFound from squid_py.http_requests.requests_session import get_requests_session from squid_py.keeper import Keeper from squid_py.ocean.ocean import Ocean from brizo.log import setup_logging from brizo.myapp import app from brizo.util import (check_required_attributes, get_provider_account) setup_logging() services = Blueprint('services', __name__) config_file = app.config['CONFIG_FILE'] config = Config(filename=config_file) ConfigProvider.set_config(config) # Prepare keeper contracts for on-chain access control # Prepare OceanDB ocn = Ocean() requests_session = get_requests_session() logger = logging.getLogger('brizo') # TODO run in cases of brizo crash or you restart # ocn.execute_pending_service_agreements() @services.route('/publish', methods=['POST']) def publish(): """Encrypt document using the SecretStore and keyed by the given documentId.
def buy_asset(): """ Requires all ocean services running. """ setup_logging(default_level=logging.INFO) ConfigProvider.set_config(ExampleConfig.get_config()) config = ConfigProvider.get_config() providers = { 'duero': '0xfEF2d5e1670342b9EF22eeeDcb287EC526B48095', 'nile': '0x4aaab179035dc57b35e2ce066919048686f82972' } # make ocean instance ocn = Ocean() acc = get_account(1) if not acc: acc = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] Diagnostics.verify_contracts() metadata = example_metadata.metadata.copy() metadata['main']['dateCreated'] = get_timestamp() keeper = Keeper.get_instance() # Register ddo did = '' if did: ddo = ocn.assets.resolve(did) logging.info(f'using ddo: {did}') else: ddo = ocn.assets.create(metadata, acc, providers=[], use_secret_store=True) assert ddo is not None, f'Registering asset on-chain failed.' did = ddo.did logging.info(f'registered ddo: {did}') # ocn here will be used only to publish the asset. Handling the asset by the publisher # will be performed by the Brizo 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)}') _providers = keeper.did_registry.get_did_providers(ddo.asset_id) access_template_name = keeper.template_manager.SERVICE_TO_TEMPLATE_NAME['access'] template_id = keeper.template_manager.create_template_id(access_template_name) approved = keeper.template_manager.is_template_approved(template_id) print(f'agreement template approved: {approved}') template_data = keeper.template_manager.get_template(template_id) print(f'access agreement template: {template_data}') cons_ocn = Ocean() 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 Brizo which in turn will execute the agreement on-chain cons_ocn.accounts.request_tokens(consumer_account, 10) sa = ServiceAgreement.from_json(service.as_dictionary()) agreement_id = '' if not agreement_id: agreement_id = cons_ocn.assets.order( did, sa.index, consumer_account) logging.info('placed order: %s, %s', did, agreement_id) event = keeper.agreement_manager.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_reward_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_secret_store_condition.subscribe_condition_fulfilled( agreement_id, 15, None, (), wait=True ) logging.info(f'Got access event {event}') i = 0 while ocn.agreements.is_access_granted( agreement_id, did, consumer_account.address) is not True and i < 30: time.sleep(1) i += 1 assert ocn.agreements.is_access_granted(agreement_id, did, consumer_account.address) ocn.assets.consume( agreement_id, did, sa.index, consumer_account, config.downloads_path) logging.info('Success buying asset.') event = keeper.escrow_reward_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.')
def test_buy_asset(consumer_ocean_instance, publisher_ocean_instance): 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_ocean_instance, pub_acc) assert isinstance(ddo, DDO) # ocn here will be used only to publish the asset. Handling the asset by the publisher # will be performed by the Brizo server running locally cons_ocn = consumer_ocean_instance # restore the http client because we want the actual Brizo server to do the work # not the BrizoMock. # Brizo.set_http_client(requests) consumer_account = get_consumer_account() downloads_path_elements = len( os.listdir( consumer_ocean_instance._config.downloads_path)) if os.path.exists( consumer_ocean_instance._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_json(service.as_dictionary()) # This will send the consume request to Brizo which in turn will execute the agreement on-chain cons_ocn.accounts.request_tokens(consumer_account, 100) agreement_id = cons_ocn.assets.order(ddo.did, sa.index, consumer_account, auto_consume=False) event_wait_time = 10 event = keeper.escrow_access_secretstore_template.subscribe_agreement_created( agreement_id, event_wait_time, log_event( keeper.escrow_access_secretstore_template.AGREEMENT_CREATED_EVENT), (), wait=True) assert event, 'no event for EscrowAccessSecretStoreTemplate.AgreementCreated' event = keeper.lock_reward_condition.subscribe_condition_fulfilled( agreement_id, event_wait_time, log_event(keeper.lock_reward_condition.FULFILLED_EVENT), (), wait=True) assert event, 'no event for LockRewardCondition.Fulfilled' # give access publisher_ocean_instance.agreements.conditions.grant_access( agreement_id, ddo.did, consumer_account.address, pub_acc) event = keeper.access_secret_store_condition.subscribe_condition_fulfilled( agreement_id, event_wait_time, log_event(keeper.access_secret_store_condition.FULFILLED_EVENT), (), wait=True) assert event, 'no event for AccessSecretStoreCondition.Fulfilled' assert cons_ocn.agreements.is_access_granted(agreement_id, ddo.did, consumer_account.address) assert cons_ocn.assets.consume(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 consume only an specific file in passing the index. assert cons_ocn.assets.consume(agreement_id, ddo.did, sa.index, consumer_account, config.downloads_path, 2) assert len(os.listdir( config.downloads_path)) == downloads_path_elements + 1 with pytest.raises(AssertionError): cons_ocn.assets.consume(agreement_id, ddo.did, sa.index, consumer_account, config.downloads_path, -2) with pytest.raises(AssertionError): cons_ocn.assets.consume(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 try: cons_ocn.assets.consume(agreement_id, ddo.did, sa.index, pub_acc, config.downloads_path) except RPCError: print('hooray, secret store is working as expected.') publisher_ocean_instance.agreements.conditions.release_reward( agreement_id, sa.get_price(), pub_acc) event = keeper.escrow_reward_condition.subscribe_condition_fulfilled( agreement_id, event_wait_time + 20, log_event(keeper.escrow_reward_condition.FULFILLED_EVENT), (), wait=True) assert event, 'no event for EscrowReward.Fulfilled' assert w3.toHex(event.args['_agreementId']) == agreement_id
def make_ocean_instance(): path_config = 'config_local.ini' os.environ['CONFIG_FILE'] = path_config ConfigProvider.set_config(Config(path_config)) ocn = Ocean() return ocn
from ocean_keeper.web3_provider import Web3Provider from ocean_utils.aquarius import AquariusProvider from examples import ExampleConfig from squid_py import ConfigProvider from squid_py.ocean.keeper import SquidKeeper as Keeper from tests.resources.helper_functions import ( get_consumer_ocean_instance, get_metadata, get_publisher_account, get_publisher_ocean_instance, get_registered_ddo, setup_logging) from tests.resources.mocks.secret_store_mock import SecretStoreMock from tests.resources.tiers import should_run_test setup_logging() if should_run_test('e2e'): ConfigProvider.set_config(ExampleConfig.get_config()) @pytest.fixture def setup_all(): config = ExampleConfig.get_config() Web3Provider.init_web3(config.keeper_url) ContractHandler.set_artifacts_path(config.keeper_path) Keeper.get_instance() @pytest.fixture def secret_store(): return SecretStoreMock
import os import time from squid_py import ( Ocean, ConfigProvider, Config ) ConfigProvider.set_config(Config('config.ini')) # Make a new instance of Ocean ocean = Ocean() # or Ocean(Config('config.ini')) config = ocean.config # make account instance, assuming the ethereum account and password are set # in the config file `config.ini` account = ocean.accounts.list()[0] print('account', account) did = 'did:op:71fae96b1d9a4651ba0d3946603fb4c11deb724685f64780985ce32ea2dfe517' service_agreement_id = ocean.assets.order(did, 0, account) # after a short wait (seconds to minutes) the asset data files should be available in the `downloads.path` defined in config # wait a bit to let things happen print(service_agreement_id) time.sleep(20) # Asset files are saved in a folder named after the asset id dataset_dir = os.path.join(ocean.config.downloads_path, f'datafile.{asset_ddo.asset_id}.0') if os.path.exists(dataset_dir):
def __init__(self, path_to_config): ConfigProvider.set_config(Config(path_to_config)) Ocean.__init__(self)
data = {} response = jsonify(data) response.status_code = status_code abort(response) # Asset files are saved in a folder named after the asset id def find_csv(asset_id): path = os.path.join(ocean.config.downloads_path, f'datafile.{asset_id}.0') csv_files = [f for f in os.listdir(path) if f.endswith('.csv')] return os.path.join(path, csv_files[0]) if csv_files else None app = Flask(__name__) CORS(app) ConfigProvider.set_config(demo_config) # ocean = Ocean() ocean = Ocean( Config('config_local.ini') ) # use local or regular config for production pacific network usage. config = ocean.config accounts = ocean.accounts.list() if accounts: account = accounts[0] # take first account as primary source. @app.route('/', methods=['GET']) def hello(): return "Welcome to Oceanapi"
import os import time import json from squid_py import ( Ocean, ConfigProvider, Config, ) from ocean_keeper.account import Account from ocean_utils.ddo.metadata import Metadata with open('config.ini', 'r') as fp: configs = json.load(fp) ConfigProvider.set_config(Config(options_dict=configs)) # Make a new instance of Ocean ocean = Ocean(Config(options_dict=configs)) config = ocean.config # make account instance, assuming the ethereum account and password are set # in the config file `config.ini` # account = ocean.accounts.list()[0] # or account = Account(config.parity_address, config.parity_password) # PUBLISHER # Let's start by registering an asset in the Ocean network metadata = Metadata.get_example() # consume and service endpoints require `brizo.url` is set in the config file