Exemple #1
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)
Exemple #2
0
 def _get_current_channel_state(self):
     stub = self.payment_channel_state_service_client
     current_block_number = self.web3.eth.getBlock("latest").number
     message = web3.Web3.soliditySha3(["string","address","uint256","uint256"], ["__get_channel_state",web3.Web3.toChecksumAddress(self.mpe_contract.contract.address),self.channel_id,current_block_number])
     signature = self.web3.eth.account.signHash(defunct_hash_message(message), self.account.signer_private_key).signature
     with add_to_path(str(RESOURCES_PATH.joinpath("proto"))):
         state_service_pb2 = importlib.import_module("state_service_pb2")
     request = state_service_pb2.ChannelStateRequest(channel_id=web3.Web3.toBytes(self.channel_id), signature=bytes(signature),current_block=current_block_number)
     response = stub.GetChannelState(request)
     return int.from_bytes(response.current_nonce, byteorder="big"), int.from_bytes(response.current_signed_amount, byteorder="big")
    def is_free_call_available(self, email, token_for_free_call, token_expiry_date_block, signature,
                               current_block_number, daemon_endpoint):

        try:

            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())
            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
Exemple #4
0
 def _get_current_channel_state(self):
     stub = self.payment_channel_state_service_client
     message = web3.Web3.soliditySha3(["uint256"], [self.channel_id])
     signature = self.web3.eth.account.signHash(
         defunct_hash_message(message),
         self.account.signer_private_key).signature
     with add_to_path(str(RESOURCES_PATH.joinpath("proto"))):
         state_service_pb2 = importlib.import_module("state_service_pb2")
     request = state_service_pb2.ChannelStateRequest(
         channel_id=web3.Web3.toBytes(self.channel_id),
         signature=bytes(signature))
     response = stub.GetChannelState(request)
     return int.from_bytes(response.current_nonce,
                           byteorder="big"), int.from_bytes(
                               response.current_signed_amount,
                               byteorder="big")