Esempio n. 1
0
    def is_free_call_available(self, service_client):
        try:
            org_id, service_id, group_id, daemon_endpoint = service_client.get_service_details(
            )
            email, token_for_free_call, token_expiry_date_block = service_client.get_free_call_config(
            )

            if not token_for_free_call:
                return False

            signature, current_block_number = self.generate_signature(
                service_client)
            with add_to_path(str(RESOURCES_PATH.joinpath("proto"))):
                state_service_pb2 = importlib.import_module(
                    "state_service_pb2")

            with add_to_path(str(RESOURCES_PATH.joinpath("proto"))):
                state_service_pb2_grpc = importlib.import_module(
                    "state_service_pb2_grpc")

            request = state_service_pb2.FreeCallStateRequest()
            request.user_id = email
            request.token_for_free_call = token_for_free_call
            request.token_expiry_date_block = token_expiry_date_block
            request.signature = signature
            request.current_block = current_block_number

            endpoint_object = urlparse(daemon_endpoint)
            if endpoint_object.port is not None:
                channel_endpoint = endpoint_object.hostname + ":" + str(
                    endpoint_object.port)
            else:
                channel_endpoint = endpoint_object.hostname

            if endpoint_object.scheme == "http":
                channel = grpc.insecure_channel(channel_endpoint)
            elif endpoint_object.scheme == "https":
                channel = grpc.secure_channel(
                    channel_endpoint,
                    grpc.ssl_channel_credentials(
                        root_certificates=root_certificate))
            else:
                raise ValueError(
                    'Unsupported scheme in service metadata ("{}")'.format(
                        endpoint_object.scheme))

            stub = state_service_pb2_grpc.FreeCallStateServiceStub(channel)
            response = stub.GetFreeCallsAvailable(request)
            if response.free_calls_available > 0:
                return True
            return False
        except Exception as e:
            return False
Esempio n. 2
0
 def _generate_payment_channel_state_service_client(self):
     grpc_channel = self.__base_grpc_channel
     with add_to_path(str(RESOURCES_PATH.joinpath("proto"))):
         state_service_pb2_grpc = importlib.import_module(
             "state_service_pb2_grpc")
     return state_service_pb2_grpc.PaymentChannelStateServiceStub(
         grpc_channel)
Esempio n. 3
0
    def __get_token_for_amount(self, service_client, channel, amount):
        nonce = channel.state["nonce"]
        stub = self.__get_stub_for_get_token(service_client)
        with add_to_path(str(RESOURCES_PATH.joinpath("proto"))):
            token_service_pb2 = importlib.import_module("token_service_pb2")
        current_block_number = service_client.sdk_web3.eth.getBlock("latest").number
        message = web3.Web3.soliditySha3(
            ["string", "address", "uint256", "uint256", "uint256"],
            ["__MPE_claim_message", service_client.mpe_address, channel.channel_id, nonce, amount]
        )
        mpe_signature = service_client.generate_signature(message)
        message = web3.Web3.soliditySha3(
            ["bytes", "uint256"],
            [mpe_signature, current_block_number]
        )
        sign_mpe_signature = service_client.generate_signature(message)

        request = token_service_pb2.TokenRequest(
            channel_id=channel.channel_id, current_nonce=nonce, signed_amount=amount,
            signature=bytes(sign_mpe_signature), claim_signature=bytes(mpe_signature),
            current_block=current_block_number)
        token_reply = stub.GetToken(request)
        return token_reply
Esempio n. 4
0
 def __get_stub_for_get_token(self, service_client):
     grpc_channel = service_client.get_grpc_base_channel()
     with add_to_path(str(RESOURCES_PATH.joinpath("proto"))):
         token_service_pb2_grpc = importlib.import_module("token_service_pb2_grpc")
     return token_service_pb2_grpc.TokenServiceStub(grpc_channel)