示例#1
0
def get_appointment(plugin, tower_id, locator):
    """
    Gets information about an appointment from the tower.

    Args:
        plugin (:obj:`Plugin`): this plugin.
        tower_id (:obj:`str`): the identifier of the tower to query.
        locator (:obj:`str`): the appointment locator.

    Returns:
        :obj:`dict`: a dictionary containing the appointment data.
    """

    # FIXME: All responses from the tower should be signed.
    try:
        tower_id, locator = arg_parser.parse_get_appointment_arguments(tower_id, locator)

        if tower_id not in plugin.wt_client.towers:
            raise InvalidParameter("tower_id is not within the registered towers", tower_id=tower_id)

        message = f"get appointment {locator}"
        signature = Cryptographer.sign(message.encode("utf-8"), plugin.wt_client.sk)
        data = {"locator": locator, "signature": signature}

        # Send request to the server.
        tower_netaddr = plugin.wt_client.towers[tower_id].netaddr
        get_appointment_endpoint = f"{tower_netaddr}/get_appointment"
        plugin.log(f"Requesting appointment from {tower_id}")

        response = process_post_response(post_request(data, get_appointment_endpoint, tower_id))
        return response

    except (InvalidParameter, TowerConnectionError, TowerResponseError) as e:
        plugin.log(str(e), level="warn")
        return e.to_json()
示例#2
0
def register(plugin, tower_id, host=None, port=None):
    """
    Registers the user to the tower.

    Args:
        plugin (:obj:`Plugin`): this plugin.
        tower_id (:obj:`str`): the identifier of the tower to connect to (a compressed public key).
        host (:obj:`str`): the ip or hostname to connect to, optional.
        port (:obj:`int`): the port to connect to, optional.

    Accepted tower_id formats:
        - tower_id@host:port
        - tower_id host port
        - tower_id@host (will default port to DEFAULT_PORT)
        - tower_id host (will default port to DEFAULT_PORT)

    Returns:
        :obj:`dict`: a dictionary containing the subscription data.
    """

    try:
        tower_id, tower_netaddr = arg_parser.parse_register_arguments(
            tower_id, host, port, plugin.wt_client.config)

        # Defaulting to http hosts for now
        if not tower_netaddr.startswith("http"):
            tower_netaddr = "http://" + tower_netaddr

        # Send request to the server.
        register_endpoint = f"{tower_netaddr}/register"
        data = {"public_key": plugin.wt_client.user_id}

        plugin.log(f"Registering in the Eye of Satoshi (tower_id={tower_id})")

        response = process_post_response(
            post_request(data, register_endpoint, tower_id))
        plugin.log(
            f"Registration succeeded. Available slots: {response.get('available_slots')}"
        )

        # Save data
        tower_info = TowerInfo(tower_netaddr, response.get("available_slots"))
        plugin.wt_client.lock.acquire()
        plugin.wt_client.towers[tower_id] = tower_info.get_summary()
        plugin.wt_client.db_manager.store_tower_record(tower_id, tower_info)
        plugin.wt_client.lock.release()

        return response

    except (InvalidParameter, TowerConnectionError, TowerResponseError) as e:
        plugin.log(str(e), level="warn")
        return e.to_json()
示例#3
0
def register(plugin, tower_id, host=None, port=None):
    """
    Registers the user to the tower.

    Args:
        plugin (:obj:`Plugin`): this plugin.
        tower_id (:obj:`str`): the identifier of the tower to connect to (a compressed public key).
        host (:obj:`str`): the ip or hostname to connect to, optional.
        port (:obj:`int`): the port to connect to, optional.

    Accepted tower_id formats:
        - tower_id@host:port
        - tower_id host port
        - tower_id@host (will default port to DEFAULT_PORT)
        - tower_id host (will default port to DEFAULT_PORT)

    Returns:
        :obj:`dict`: a dictionary containing the subscription data.
    """

    try:
        tower_id, tower_netaddr = arg_parser.parse_register_arguments(tower_id, host, port, plugin.wt_client.config)

        # Defaulting to http hosts for now
        if not tower_netaddr.startswith("http"):
            tower_netaddr = "http://" + tower_netaddr

        # Send request to the server.
        register_endpoint = f"{tower_netaddr}/register"
        data = {"public_key": plugin.wt_client.user_id}

        plugin.log(f"Registering in the Eye of Satoshi (tower_id={tower_id})")

        response = process_post_response(post_request(data, register_endpoint, tower_id))
        available_slots = response.get("available_slots")
        subscription_expiry = response.get("subscription_expiry")
        tower_signature = response.get("subscription_signature")

        if available_slots is None or not isinstance(available_slots, int):
            raise TowerResponseError(f"available_slots is missing or of wrong type ({available_slots})")
        if subscription_expiry is None or not isinstance(subscription_expiry, int):
            raise TowerResponseError(f"subscription_expiry is missing or of wrong type ({subscription_expiry})")
        if tower_signature is None or not isinstance(tower_signature, str):
            raise TowerResponseError(f"signature is missing or of wrong type ({tower_signature})")

        # Check tower signature
        registration_receipt = receipts.create_registration_receipt(
            plugin.wt_client.user_id, available_slots, subscription_expiry
        )
        Cryptographer.recover_pk(registration_receipt, tower_signature)

        plugin.log(f"Registration succeeded. Available slots: {available_slots}")

        # Save data
        tower_info = TowerInfo(tower_netaddr, available_slots)
        plugin.wt_client.lock.acquire()
        plugin.wt_client.towers[tower_id] = tower_info.get_summary()
        plugin.wt_client.db_manager.store_tower_record(tower_id, tower_info)
        plugin.wt_client.lock.release()

        return response

    except (InvalidParameter, TowerConnectionError, TowerResponseError, SignatureError) as e:
        plugin.log(str(e), level="warn")
        return e.to_json()