def test_did(): """Tests various DID functions.""" assert DID.did({"0": "0x123"}).startswith(OCEAN_PREFIX) assert len(DID.did({"0": "0x123"})) - len(OCEAN_PREFIX) == 64 _id = did_to_id(DID.did({"0": "0x123"})) assert not _id.startswith( "0x"), "id portion of did should not have a 0x prefix."
def pay_for_service( amount: float, token_address: str, did: str, service_id: int, fee_receiver: str, from_wallet: Wallet, consumer: str = None, ) -> str: """ Submits the payment for chosen service in DataTokens. :param amount: :param token_address: :param did: :param service_id: :param fee_receiver: :param from_wallet: Wallet instance :param consumer: str the address of consumer of the service, defaults to the payer (the `from_wallet` address) :return: hex str id of transfer transaction """ amount_base = to_base_18(amount) dt = DataToken(token_address) balance = dt.balanceOf(from_wallet.address) if balance < amount_base: raise InsufficientBalance( f"Your token balance {balance} is not sufficient " f"to execute the requested service. This service " f"requires {amount_base} number of tokens.") if did.startswith("did:"): did = add_0x_prefix(did_to_id(did)) if fee_receiver is None: fee_receiver = ZERO_ADDRESS if consumer is None: consumer = from_wallet.address tx_hash = dt.startOrder(consumer, amount_base, service_id, fee_receiver, from_wallet) try: dt.verify_order_tx( Web3Provider.get_web3(), tx_hash, did, service_id, amount_base, from_wallet.address, ) return tx_hash except (AssertionError, Exception) as e: msg = ( f"Downloading asset files failed. The problem is related to " f"the transfer of the data tokens required for the download " f"service: {e}") logger.error(msg) raise AssertionError(msg)
def _get_num_assets(_minter): dids = [ add_0x_prefix(did_to_id(a)) for a in ocn.assets.owner_assets(_minter) ] dids = [a for a in dids if len(a) == 42] return len([ a for a in dids if DataToken(a).contract_concise.isMinter(_minter) ])
def test_did_to_id(): """Tests did to id conversion.""" did = DID.did({"0": "0x123"}) _id = did_to_id(did) assert _id is not None and len(_id) == 64, "" test_id = "%s" % secrets.token_hex(32) assert did_to_id(f"{OCEAN_PREFIX}{test_id}") == test_id assert did_to_id("did:op1:011") == "011" assert did_to_id("did:op:0") == "0" with pytest.raises(ValueError): did_to_id(OCEAN_PREFIX) assert did_to_id(f"{OCEAN_PREFIX}AB*&$#") == "AB", ""
def asset_id(self): """The asset id part of the DID""" if not self._did: return None return add_0x_prefix(did_to_id(self._did))