Exemplo n.º 1
0
def test_store_token():
    ocn_auth = OceanAuth(Keeper.get_instance(), ':memory:')
    acc = get_publisher_account()
    token = ocn_auth.store(acc)
    assert ocn_auth.check(token) == acc.address, 'invalid token, check failed.'
    # verify it is saved
    assert ocn_auth.restore(acc) == token, 'Restoring token failed.'
Exemplo n.º 2
0
def test_check_token(web3_instance):
    ocn_auth = OceanAuth(Keeper.get_instance(), ':memory:')
    acc = get_publisher_account()

    token = ocn_auth.get(acc)
    address = ocn_auth.check(token)
    assert address != '0x0', 'Verifying token failed.'

    sig = token.split('-')[0]
    assert ocn_auth.check(sig) == '0x0'
Exemplo n.º 3
0
def test_get_token():
    ocn_auth = OceanAuth(Keeper.get_instance(), ':memory:')
    acc = get_publisher_account()
    token = ocn_auth.get(acc)
    assert isinstance(token, str), 'Invalid auth token type.'
    assert token.startswith('0x'), 'Invalid auth token.'
    parts = token.split('-')
    assert len(parts) == 2, 'Invalid token, timestamp separator is not found.'

    address = ocn_auth.check(token)
    assert address != '0x0', 'Verifying token failed.'
Exemplo n.º 4
0
def test_known_token():
    token = "0x1d2741dee30e64989ef0203957c01b14f250f5d2f6ccb0c" \
            "88c9518816e4fcec16f84e545094eb3f377b7e214ded22676" \
            "fbde8ca2e41b4eb1b3565047ecd9acf300-1568372035"
    pub_address = "0xe2DD09d719Da89e5a3D0F2549c7E24566e947260"

    ocn_auth = OceanAuth(Keeper.get_instance(), ':memory:')
    assert ocn_auth.is_token_valid(
        token), f'Invalid token!! has the token specs changed?'

    def _get_timestamp():
        return int('1568372035') + 10000

    ocn_auth._get_timestamp = _get_timestamp
    address = ocn_auth.check(token)
    assert address.lower() == pub_address.lower(), f'Recovered address {address} does not match ' \
                                                   f'known signer address {pub_address}, if the ' \
                                                   f'token generation method is changed please update ' \
                                                   f'the token in this test with the new format.'
Exemplo n.º 5
0
def test_restore_token(publisher_ocean_instance):
    ocn_auth = OceanAuth(Keeper.get_instance(), ':memory:')
    acc = get_publisher_account()
    assert ocn_auth.restore(
        acc) is None, 'Expecting None when restoring non-existing token.'

    token = ocn_auth.store(acc)
    assert ocn_auth.check(token) == acc.address, 'invalid token, check failed.'
    # verify it is saved
    assert ocn_auth.restore(acc) == token, 'Restoring token failed.'
Exemplo n.º 6
0
    def __init__(self, config=None):
        """
        Initialize Ocean class.
           >> # Make a new Ocean instance
           >> ocean = Ocean({...})

        This class provides the main top-level functions in ocean protocol:
         * Publish assets metadata and associated services
            * Each asset is assigned a unique DID and a DID Document (DDO)
            * The DDO contains the asset's services including the metadata
            * The DID is registered on-chain with a URL of the metadata store
              to retrieve the DDO from

            >> ddo = ocean.assets.create(metadata, publisher_account)

         * Discover/Search assets via the current configured metadata store (Aquarius)
            >> assets_list = ocean.assets.search('search text')

         * Purchase asset services by choosing a service agreement from the
           asset's DDO. Purchase goes through the service agreements interface
           and starts by signing a service agreement then sending the signature
           to the publisher's Brizo server via the `purchaseEndpoint` in the service
           definition:

           >> service_def_id = ddo.get_service(ServiceTypes.ASSET_ACCESS).service_definition_id
           >> service_agreement_id = ocean.assets.order(did, service_def_id, consumer_account)

        An instance of Ocean is parameterized by a `Config` instance.

        :param config: Config instance
        """
        # Configuration information for the market is stored in the Config class
        # config = Config(filename=config_file, options_dict=config_dict)
        if not config:
            config = ConfigProvider.get_config()

        self._config = config
        self._web3 = Web3Provider.get_web3(self._config.keeper_url)
        ContractHandler.set_artifacts_path(self._config.keeper_path)
        contracts = [
            'DIDRegistry', 'Dispenser', 'TemplateStoreManager', 'OceanToken',
            'ConditionStoreManager', 'EscrowAccessSecretStoreTemplate',
            'AgreementStoreManager', 'AgreementStoreManager',
            'AccessSecretStoreCondition', 'LockRewardCondition',
            'HashLockCondition', 'SignCondition', 'EscrowReward'
        ]
        self._keeper = Keeper.get_instance(contracts)
        self._did_resolver = DIDResolver(self._keeper.did_registry)

        # Initialize the public sub-modules
        self.tokens = OceanTokens(self._keeper)
        self.accounts = OceanAccounts(self._keeper, self._config, self.tokens)
        self.secret_store = OceanSecretStore(self._config)
        self.templates = OceanTemplates(self._keeper, config)
        self.agreements = self._make_ocean_agreements()
        self.assets = OceanAssets(self._keeper, self._did_resolver,
                                  self.agreements, AssetConsumer,
                                  AssetExecutor, self._config)
        self.services = OceanServices()
        self.ocean_providers = OceanProviders(self._keeper, self._did_resolver,
                                              self._config)
        self.auth = OceanAuth(self._keeper, self._config.storage_path)

        logger.debug('Squid Ocean instance initialized: ')
        logger.debug(
            f'\tOther accounts: {sorted([a.address for a in self.accounts.list()])}'
        )
        logger.debug(f'\tDIDRegistry @ {self._keeper.did_registry.address}')