def simple_register(name, price, description, author, azure_url): pub_ocn = Ocean('config_local.ini') cons_ocn = Ocean('config_local.ini') logging.debug("".format()) # Format metadata as required by Ocean metadata = { "base": { "name": name, "type": "dataset", "description": description, "size": "0.1MB", # Approx "author": author, "license": "CC-BY", "contentType": "text/json", "contentUrls": [azure_url] # contentUrls is an array } } aquarius_acct = pub_ocn.main_account consumer_acct = cons_ocn.main_account # Ensure there are tokens for the contract if aquarius_acct.ocean_balance == 0: rcpt = aquarius_acct.request_tokens(200) pub_ocn._web3.eth.waitForTransactionReceipt(rcpt) if consumer_acct.ocean_balance == 0: rcpt = consumer_acct.request_tokens(200) cons_ocn._web3.eth.waitForTransactionReceipt(rcpt) assert aquarius_acct.ocean_balance > 0 assert consumer_acct.ocean_balance > 0 # If you are hosting Aquarius somewhere else, use the second parameter here asset = Asset.create_from_metadata( metadata, "http://localhost:5000/api/v1/provider/assets/metadata/") # Register asset pub_ocn.keeper.market.register_asset(asset, price, aquarius_acct.address) # These checks work for local deployment # Check asset exists #chain_asset_exists = pub_ocn.keeper.market.check_asset(asset.asset_id) #logging.debug("check_asset = {}".format(chain_asset_exists)) #assert chain_asset_exists # Check price is as specified #chain_price = pub_ocn.keeper.market.get_asset_price(asset.asset_id) #assert price == chain_price #logging.debug("chain_price = {}".format(chain_price)) print('\033[0;32mAsset published:') print(asset.metadata['base']['name'] + '\033[0m')
def get_consumer_ocean_instance(init_tokens=True, use_ss_mock=True, use_brizo_mock=False): ocn = Ocean() account = get_consumer_account() ocn.main_account = account if init_tokens: init_ocn_tokens(ocn, ocn.main_account) if use_ss_mock: SecretStoreProvider.set_secret_store_class(SecretStoreMock) if use_brizo_mock: BrizoProvider.set_brizo_class(BrizoMock) return ocn
def init_ocean(web3, account=None): """ Create an ocean instance with all the contracts deployed. :param web3: Web3 instance :param account: Account address, str :return: Ocean instance """ ocn = Ocean() token_contract = deploy_contract(web3, account, 'OceanToken') ContractHandler.set('OceanToken', token_contract) ContractHandler.set('DIDRegistry', deploy_contract(web3, account, 'DIDRegistry')) ContractHandler.set( 'OceanMarket', deploy_contract(web3, account, 'OceanMarket', token_contract.address)) service_agreement_contract = deploy_contract(web3, account, 'ServiceExecutionAgreement') ContractHandler.set('ServiceExecutionAgreement', service_agreement_contract) ContractHandler.set( 'PaymentConditions', deploy_contract(web3, account, 'PaymentConditions', service_agreement_contract.address, token_contract.address)) ContractHandler.set( 'AccessConditions', deploy_contract(web3, account, 'AccessConditions', service_agreement_contract.address)) return ocn
def make_ocean_instance(account_index): ocn = Ocean(ExampleConfig.get_config()) account = ocn.accounts.list()[account_index] if account_index == 0: account.password = ExampleConfig.get_config().get( 'keeper-contracts', 'parity.password') else: account.password = ExampleConfig.get_config().get( 'keeper-contracts', 'parity.password1') return ocn
def ocean(ctx, config_file, as_json, verbose): """ Simple CLI for registering and consuming assets in Ocean Protocol \b O , .</ , ____ _______ ____ ,aT&/t ,</ / __ \_______ ___ ____ / ___/ / / _/ o o:\:::::95/b/ / /_/ / __/ -_) _ `/ _ \ / /__/ /___/ / ' >::7:::::U2/)( \____/\__/\__/\_,_/_//_/ \___/____/___/ '*qf/P '</ '<) ' """ if not verbose: logging.getLogger().setLevel(logging.ERROR) ocean = Ocean(Config(filename=config_file)) account = get_default_account(ConfigProvider.get_config()) ctx.obj = { 'account': account, 'ocean': ocean, 'json': as_json }
import json from squid_py.ocean.ocean import Ocean from squid_py.service_agreement.service_factory import ServiceDescriptor from ..config import CONFIG_FILE from ..input import INPUT import logging logging.getLogger().setLevel(logging.WARNING) ocean = Ocean(CONFIG_FILE) cons_address = ocean.config.get('keeper-contracts', 'parity.address1') cons_password = ocean.config.get('keeper-contracts', 'parity.password1') ocean.set_main_account(cons_address, cons_password) _url = ocean.config.get('resources', 'brizo.url') if ocean.config.has_option( 'resources', 'brizo.url') else 'http://localhost:8030' brizo_url = '{}/api/v1/brizo'.format(_url) purchase_endpoint = '{}/services/access/initialize'.format(brizo_url) service_endpoint = '{}/services/consume'.format(brizo_url) # Call the Register function service_descriptors = [ ServiceDescriptor.access_service_descriptor( 10, purchase_endpoint, service_endpoint, 600, '0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d') ] ddo = ocean.register_asset(INPUT, ocean.main_account.address, service_descriptors)
import json from squid_py.ocean.ocean import Ocean from ..config import CONFIG_FILE from ..input import INPUT import logging logging.getLogger().setLevel(logging.WARNING) ocean = Ocean(CONFIG_FILE) did = INPUT service_id = '1' sa_id, sa, _, _ = ocean._get_service_agreement_to_sign(did, service_id) print("__result__{}".format( json.dumps( { 'did': did, 'serviceDefinitionId': service_id, 'serviceAgreementId': sa_id }, indent=2, sort_keys=True)))
import json from squid_py.service_agreement.service_agreement import ServiceAgreement from squid_py.service_agreement.service_types import ServiceTypes from squid_py.ocean.ocean import Ocean from ..config import CONFIG_FILE from ..input import INPUT import logging logging.getLogger().setLevel(logging.WARNING) ocean = Ocean(CONFIG_FILE) consumer_address = ocean.config.get('keeper-contracts', 'consumer.address') consumer_password = ocean.config.get('keeper-contracts', 'consumer.password') ocean.set_main_account(consumer_address, consumer_password) did = INPUT['did'] sa_id = INPUT['serviceAgreementId'] service_id = INPUT['serviceDefinitionId'] ddo = ocean.resolve_did(did) service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) sa = ServiceAgreement.from_service_dict(service.as_dictionary()) sa_hash = sa.get_service_agreement_hash(ocean._web3, ocean.keeper.contract_path, sa_id).hex() print("__result__{}".format( json.dumps(
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 flask import Flask, request, jsonify from ocean_cli.api.assets import decrypt from squid_py.config import Config from squid_py.did import did_to_id_bytes from squid_py.ocean.ocean import Ocean from ocean_cli.ocean import get_default_account config = Config(filename='./config.ini') ocean = Ocean(config) account = get_default_account(config) app = Flask(__name__) def verify(did, address, token): grant_secret_store = False if did and address: grant_secret_store = ocean.keeper.\ access_secret_store_condition.get_instance(). \ check_permissions(did_to_id_bytes(did), address) grant_token = False if token: # TODO check did.provider == me grant_token = decrypt(ocean, account, did)[0]['token'] == token return grant_secret_store and grant_token @app.route('/', defaults={'path': ''})
import json from squid_py.ocean.ocean import Ocean from ..config import CONFIG_FILE from ..input import INPUT import logging logging.getLogger().setLevel(logging.WARNING) ocean = Ocean(CONFIG_FILE) assets = ocean.search_assets_by_text(INPUT) print("__result__{}".format(json.dumps([a.id for a in assets], indent=2, sort_keys=True)))
from squid_py.ocean.account import Account from squid_py.ocean.ocean import Ocean from werkzeug.contrib.cache import SimpleCache from brizo.constants import ConfigSections from brizo.log import setup_logging from brizo.myapp import app setup_logging() services = Blueprint('services', __name__) config_file = app.config['CONFIG_FILE'] config = Config(filename=config_file) # Prepare keeper contracts for on-chain access control # Prepare OceanDB ocn = Ocean(config=config) cache = SimpleCache() logger = logging.getLogger('brizo') # TODO run in cases of brizo crash or you restart # ocn.execute_pending_service_agreements() @services.route('/access/initialize', methods=['POST']) def initialize(): """Initialize the SLA between the publisher and the consumer. ---
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. This can be used by the publisher of an asset to encrypt the urls of the asset data files before publishing the asset ddo. The publisher to use this service is one that is using a front-end with a wallet app such as MetaMask.
def make_ocean_instance(account_index): path_config = 'config_local.ini' os.environ['CONFIG_FILE'] = path_config ocn = Ocean(Config(path_config)) account = list(ocn.accounts)[account_index] return ocn
# %% [markdown] # #%% # Standard imports import logging import os # Import mantaray and the Ocean API (squid) from squid_py.ocean.ocean import Ocean import mantaray_utilities.config as manta_config import mantaray_utilities.logging as manta_logging import mantaray_utilities.asset_pretty_print as manta_print # %% For testing, set the desired environment os.environ['USE_K8S_CLUSTER'] = 'true' #%% # Get the configuration file path for this environment CONFIG_INI_PATH = manta_config.get_config_file_path() logging.info("Configuration file selected: {}".format(CONFIG_INI_PATH)) #%% # Instantiate Ocean ocn = Ocean(CONFIG_INI_PATH) #%%
# Get the configuration file path for this environment # os.environ['USE_K8S_CLUSTER'] = 'true' CONFIG_INI_PATH = manta_utils.config.get_config_file_path() logging.critical("Deployment type: {}".format( manta_utils.config.get_deployment_type())) logging.critical("Configuration file selected: {}".format(CONFIG_INI_PATH)) logging.critical("Squid API version: {}".format(squid_py.__version__)) # %% [markdown] # ### Section 1: Instantiate a simulated User # A 'User' in an abstract class representing a user of Ocean Protocol. # # Instantiate Ocean with the default configuration file. configuration = Config(CONFIG_INI_PATH) ocn = Ocean(configuration) # Get the publisher account publisher_address = configuration['keeper-contracts']['parity.address'] publisher_pass = configuration['keeper-contracts']['parity.password'] publisher_acct = [ ocn.accounts[addr] for addr in ocn.accounts if addr.lower() == publisher_address.lower() ][0] publisher_acct.password = publisher_pass # %% [markdown] # Your account will need some Ocean Token to make real transactions # %% # ensure Ocean token balance if publisher_acct.ocean_balance == 0: tx_hash = publisher_acct.request_tokens(1)
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
import json from squid_py.utils.utilities import get_metadata_url, prepare_purchase_payload from squid_py.service_agreement.service_agreement import ServiceAgreement from squid_py.service_agreement.service_types import ServiceTypes from squid_py.ocean.ocean import Ocean from ..config import CONFIG_FILE from ..input import INPUT import logging logging.getLogger().setLevel(logging.WARNING) ocean = Ocean(CONFIG_FILE) consumer_address = ocean.config.get('keeper-contracts', 'consumer.address') consumer_password = ocean.config.get('keeper-contracts', 'consumer.password') ocean.set_main_account(consumer_address, consumer_password) did = INPUT['did'] sa_id = INPUT['serviceAgreementId'] sa_hash = INPUT['serviceAgreementHash'] sa_signature = INPUT['serviceAgreementSignature'] service_id = INPUT['serviceDefinitionId'] ddo = ocean.resolve_did(did) service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) sa = ServiceAgreement.from_service_dict(service.as_dictionary()) # Must approve token transfer for this purchase ocean._approve_token_transfer(sa.get_price())
import json from squid_py.ocean.ocean import Ocean from ..config import CONFIG_FILE from ..input import INPUT import logging logging.getLogger().setLevel(logging.WARNING) ocean = Ocean(CONFIG_FILE) provider_address = ocean.config.get('keeper-contracts', 'provider.address') provider_password = ocean.config.get('keeper-contracts', 'provider.password') ocean.set_main_account(provider_address, provider_password) did = INPUT['did'] sa_id = INPUT['serviceAgreementId'] sa_hash = INPUT['serviceAgreementHash'] sa_signature = INPUT['serviceAgreementSignature'] service_id = INPUT['serviceDefinitionId'] consumer_address = INPUT['consumerAddress'] receipt = ocean.execute_service_agreement( service_agreement_id=sa_id, service_index=service_id, service_agreement_signature=sa_signature, did=did, consumer_address=consumer_address, publisher_address=ocean.main_account.address)
#%% # get_registered_ddo -> register_service_agreement_template -> get_conditions_data_from_keeper_contracts # The data: # contract_addresses # fingerprints # fulfillment_indices # conditions_keys # %% [markdown] # ### Section 2: Instantiate Ocean() #%% # The contract addresses are loaded from file PATH_CONFIG = pathlib.Path.cwd() / 'config_local.ini' assert PATH_CONFIG.exists(), "{} does not exist".format(PATH_CONFIG) ocn = Ocean(config_file=PATH_CONFIG) ocn._http_client ocn._secret_store_client # %% [markdown] # ### Section 3.1: Get a Service Execution Agreement for *access* #%% # Get the asset type SEA_type = squid_py.service_agreement.service_types.ServiceTypes.ASSET_ACCESS SEA_type_name = 'access_sla_template.json' # There are 2 other types currently available: 'compute_sla_template.json', and 'fitchain_sla_template.json' #TODO: The templates are copied here in mantaray - will need to use the templates direct from pip-installed squid! # Get the path of the .json template SEA_template_json_path = pathlib.Path( '.') / 'assets' / 'SEA_templates' / SEA_type_name
import json from squid_py.ocean.ocean import Ocean from ..config import CONFIG_FILE ocean = Ocean(CONFIG_FILE) balance = ocean.main_account.get_balance() response = { 'eth': balance.eth, 'ocn': balance.ocn, } print("__result__{}".format(json.dumps(response, indent=2, sort_keys=True)))
import json from squid_py.service_agreement.service_agreement import ServiceAgreement from squid_py.service_agreement.service_types import ServiceTypes from squid_py.ocean.ocean import Ocean from ..config import CONFIG_FILE from ..input import INPUT import logging logging.getLogger().setLevel(logging.WARNING) ocean = Ocean(CONFIG_FILE) consumer_address = ocean.config.get('keeper-contracts', 'consumer.address') consumer_password = ocean.config.get('keeper-contracts', 'consumer.password') ocean.set_main_account(consumer_address, consumer_password) did = INPUT['did'] sa_id = INPUT['serviceAgreementId'] sa_hash = INPUT['serviceAgreementHash'] sa_signature = INPUT['serviceAgreementSignature'] sa_consumer = INPUT['consumerAddress'] service_id = INPUT['serviceDefinitionId'] ddo = ocean.resolve_did(did) service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) sa = ServiceAgreement.from_service_dict(service.as_dictionary()) print("__result__{}".format( json.dumps( {
# ## Connect to Ocean Protocal with the configuration file #%% # Load the configuration configuration = Config(CONFIG_INI_PATH) print("Configuration loaded. Will connect to a node at: ", configuration.keeper_url) # %% [markdown] # Feel free to inspect the `configuration` object. # %% [markdown] # From the configuration, instantiate the Ocean object, the primary interface to Ocean Protocol. # %% # Instantiate Ocean ocn = Ocean(configuration) # %% [markdown] # The following cell will print some summary information of the Ocean connection. #TODO: add pretty printing of the connection #%% print("***OCEAN***") print("{} accounts".format(len(ocn.accounts._accounts))) for i, account in enumerate(ocn.accounts._accounts): print(i, account.address) #%% [markdown] # ## Alternatively, connect to Ocean with a configuration as dictionary # The configuration of the client (mantaray) can be inspected in the below code cells. The following configuration # is set for local testing.
import json from squid_py.ocean.ocean import Ocean from ..config import CONFIG_FILE from ..input import INPUT import logging logging.getLogger().setLevel(logging.WARNING) ocean = Ocean(CONFIG_FILE) ddo = ocean.resolve_did(INPUT) print("__result__{}".format( json.dumps(ddo.as_dictionary(), indent=2, sort_keys=True)))