mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 14:14:22 +01:00
Gets rid of cli_pk file
The public key can be generated from the private, and the latter has to be loaded anyway, so there's no point storing both
This commit is contained in:
@@ -10,7 +10,6 @@ DEFAULT_CONF = {
|
|||||||
"API_PORT": {"value": 9814, "type": int},
|
"API_PORT": {"value": 9814, "type": int},
|
||||||
"LOG_FILE": {"value": "teos_cli.log", "type": str, "path": True},
|
"LOG_FILE": {"value": "teos_cli.log", "type": str, "path": True},
|
||||||
"APPOINTMENTS_FOLDER_NAME": {"value": "appointment_receipts", "type": str, "path": True},
|
"APPOINTMENTS_FOLDER_NAME": {"value": "appointment_receipts", "type": str, "path": True},
|
||||||
"CLI_PUBLIC_KEY": {"value": "cli_pk.der", "type": str, "path": True},
|
|
||||||
"CLI_PRIVATE_KEY": {"value": "cli_sk.der", "type": str, "path": True},
|
"CLI_PRIVATE_KEY": {"value": "cli_sk.der", "type": str, "path": True},
|
||||||
"TEOS_PUBLIC_KEY": {"value": "teos_pk.der", "type": str, "path": True},
|
"TEOS_PUBLIC_KEY": {"value": "teos_pk.der", "type": str, "path": True},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,30 +26,30 @@ from common.tools import is_256b_hex_str, is_locator, compute_locator, is_compre
|
|||||||
logger = Logger(actor="Client", log_name_prefix=LOG_PREFIX)
|
logger = Logger(actor="Client", log_name_prefix=LOG_PREFIX)
|
||||||
|
|
||||||
|
|
||||||
def register(compressed_pk, teos_url):
|
def register(user_id, teos_url):
|
||||||
"""
|
"""
|
||||||
Registers the user to the tower.
|
Registers the user to the tower.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
compressed_pk (:obj:`str`): a 33-byte hex-encoded compressed public key representing the user.
|
user_id (:obj:`str`): a 33-byte hex-encoded compressed public key representing the user.
|
||||||
teos_url (:obj:`str`): the teos base url.
|
teos_url (:obj:`str`): the teos base url.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`dict`: a dictionary containing the tower response if the registration succeeded.
|
:obj:`dict`: a dictionary containing the tower response if the registration succeeded.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
:obj:`InvalidParameter <cli.exceptions.InvalidParameter>`: if `compressed_pk` is invalid.
|
:obj:`InvalidParameter <cli.exceptions.InvalidParameter>`: if `user_id` is invalid.
|
||||||
:obj:`ConnectionError`: if the client cannot connect to the tower.
|
:obj:`ConnectionError`: if the client cannot connect to the tower.
|
||||||
:obj:`TowerResponseError <cli.exceptions.TowerResponseError>`: if the tower responded with an error, or the
|
:obj:`TowerResponseError <cli.exceptions.TowerResponseError>`: if the tower responded with an error, or the
|
||||||
response was invalid.
|
response was invalid.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not is_compressed_pk(compressed_pk):
|
if not is_compressed_pk(user_id):
|
||||||
raise InvalidParameter("The cli public key is not valid")
|
raise InvalidParameter("The cli public key is not valid")
|
||||||
|
|
||||||
# Send request to the server.
|
# Send request to the server.
|
||||||
register_endpoint = "{}/register".format(teos_url)
|
register_endpoint = "{}/register".format(teos_url)
|
||||||
data = {"public_key": compressed_pk}
|
data = {"public_key": user_id}
|
||||||
|
|
||||||
logger.info("Registering in the Eye of Satoshi")
|
logger.info("Registering in the Eye of Satoshi")
|
||||||
response = process_post_response(post_request(data, register_endpoint))
|
response = process_post_response(post_request(data, register_endpoint))
|
||||||
@@ -57,7 +57,7 @@ def register(compressed_pk, teos_url):
|
|||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def add_appointment(appointment_data, cli_sk, teos_pk, teos_url):
|
def add_appointment(appointment_data, cli_sk, teos_id, teos_url):
|
||||||
"""
|
"""
|
||||||
Manages the add_appointment command.
|
Manages the add_appointment command.
|
||||||
|
|
||||||
@@ -73,7 +73,7 @@ def add_appointment(appointment_data, cli_sk, teos_pk, teos_url):
|
|||||||
Args:
|
Args:
|
||||||
appointment_data (:obj:`dict`): a dictionary containing the appointment data.
|
appointment_data (:obj:`dict`): a dictionary containing the appointment data.
|
||||||
cli_sk (:obj:`PrivateKey`): the client's private key.
|
cli_sk (:obj:`PrivateKey`): the client's private key.
|
||||||
teos_pk (:obj:`PublicKey`): the tower's public key.
|
teos_id (:obj:`str`): the tower's compressed public key.
|
||||||
teos_url (:obj:`str`): the teos base url.
|
teos_url (:obj:`str`): the teos base url.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@@ -119,7 +119,7 @@ def add_appointment(appointment_data, cli_sk, teos_pk, teos_url):
|
|||||||
raise TowerResponseError("The response does not contain the signature of the appointment")
|
raise TowerResponseError("The response does not contain the signature of the appointment")
|
||||||
|
|
||||||
rpk = Cryptographer.recover_pk(appointment.serialize(), signature)
|
rpk = Cryptographer.recover_pk(appointment.serialize(), signature)
|
||||||
if not teos_pk != Cryptographer.get_compressed_pk(rpk):
|
if teos_id != Cryptographer.get_compressed_pk(rpk):
|
||||||
raise TowerResponseError("The returned appointment's signature is invalid")
|
raise TowerResponseError("The returned appointment's signature is invalid")
|
||||||
|
|
||||||
logger.info("Appointment accepted and signed by the Eye of Satoshi")
|
logger.info("Appointment accepted and signed by the Eye of Satoshi")
|
||||||
@@ -128,14 +128,14 @@ def add_appointment(appointment_data, cli_sk, teos_pk, teos_url):
|
|||||||
return appointment, signature
|
return appointment, signature
|
||||||
|
|
||||||
|
|
||||||
def get_appointment(locator, cli_sk, teos_pk, teos_url):
|
def get_appointment(locator, cli_sk, teos_id, teos_url):
|
||||||
"""
|
"""
|
||||||
Gets information about an appointment from the tower.
|
Gets information about an appointment from the tower.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
locator (:obj:`str`): the appointment locator used to identify it.
|
locator (:obj:`str`): the appointment locator used to identify it.
|
||||||
cli_sk (:obj:`PrivateKey`): the client's private key.
|
cli_sk (:obj:`PrivateKey`): the client's private key.
|
||||||
teos_pk (:obj:`PublicKey`): the tower's public key.
|
teos_id (:obj:`PublicKey`): the tower's compressed public key.
|
||||||
teos_url (:obj:`str`): the teos base url.
|
teos_url (:obj:`str`): the teos base url.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@@ -149,7 +149,7 @@ def get_appointment(locator, cli_sk, teos_pk, teos_url):
|
|||||||
response was invalid.
|
response was invalid.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# FIXME: All responses from the tower should be signed. Not using teos_pk atm.
|
# FIXME: All responses from the tower should be signed. Not using teos_id atm.
|
||||||
|
|
||||||
if not is_locator(locator):
|
if not is_locator(locator):
|
||||||
raise InvalidParameter("The provided locator is not valid", locator=locator)
|
raise InvalidParameter("The provided locator is not valid", locator=locator)
|
||||||
@@ -199,18 +199,17 @@ def get_all_appointments(teos_url):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def load_keys(teos_pk_path, cli_sk_path, cli_pk_path):
|
def load_keys(teos_pk_path, cli_sk_path):
|
||||||
"""
|
"""
|
||||||
Loads all the keys required so sign, send, and verify the appointment.
|
Loads all the keys required so sign, send, and verify the appointment.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
teos_pk_path (:obj:`str`): path to the tower public key file.
|
teos_pk_path (:obj:`str`): path to the tower public key file.
|
||||||
cli_sk_path (:obj:`str`): path to the client private key file.
|
cli_sk_path (:obj:`str`): path to the client private key file.
|
||||||
cli_pk_path (:obj:`str`): path to the client public key file.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`tuple`: a three-item tuple containing a ``PrivateKey``, a ``PublicKey`` and a ``str``
|
:obj:`tuple`: a three-item tuple containing a ``str``, a ``PrivateKey`` and a ``str``
|
||||||
representing the tower pk, user sk and user compressed pk respectively.
|
representing the tower id (compressed pk), user sk and user id (compressed pk) respectively.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
:obj:`InvalidKey <cli.exceptions.InvalidKey>`: if any of the keys is invalid or cannot be loaded.
|
:obj:`InvalidKey <cli.exceptions.InvalidKey>`: if any of the keys is invalid or cannot be loaded.
|
||||||
@@ -222,30 +221,27 @@ def load_keys(teos_pk_path, cli_sk_path, cli_pk_path):
|
|||||||
if not cli_sk_path:
|
if not cli_sk_path:
|
||||||
raise InvalidKey("Client's private key file not found. Please check your settings")
|
raise InvalidKey("Client's private key file not found. Please check your settings")
|
||||||
|
|
||||||
if not cli_pk_path:
|
|
||||||
raise InvalidKey("Client's public key file not found. Please check your settings")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
teos_pk_der = Cryptographer.load_key_file(teos_pk_path)
|
teos_pk_der = Cryptographer.load_key_file(teos_pk_path)
|
||||||
teos_pk = PublicKey(teos_pk_der)
|
teos_id = Cryptographer.get_compressed_pk(PublicKey(teos_pk_der))
|
||||||
|
|
||||||
except ValueError:
|
except (InvalidParameter, InvalidKey, ValueError):
|
||||||
raise InvalidKey("TEOS public key is invalid or cannot be parsed")
|
raise InvalidKey("TEOS public key cannot be loaded")
|
||||||
|
|
||||||
|
try:
|
||||||
cli_sk_der = Cryptographer.load_key_file(cli_sk_path)
|
cli_sk_der = Cryptographer.load_key_file(cli_sk_path)
|
||||||
cli_sk = Cryptographer.load_private_key_der(cli_sk_der)
|
cli_sk = Cryptographer.load_private_key_der(cli_sk_der)
|
||||||
|
|
||||||
if cli_sk is None:
|
except (InvalidParameter, InvalidKey):
|
||||||
raise InvalidKey("Client private key is invalid or cannot be parsed")
|
raise InvalidKey("Client private key is invalid or cannot be parsed")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cli_pk_der = Cryptographer.load_key_file(cli_pk_path)
|
client_id = Cryptographer.get_compressed_pk(cli_sk.public_key)
|
||||||
compressed_cli_pk = Cryptographer.get_compressed_pk(PublicKey(cli_pk_der))
|
|
||||||
|
|
||||||
except ValueError:
|
except (InvalidParameter, InvalidKey):
|
||||||
raise InvalidKey("Client public key is invalid or cannot be parsed")
|
raise InvalidKey("Client public key cannot be loaded")
|
||||||
|
|
||||||
return teos_pk, cli_sk, compressed_cli_pk
|
return teos_id, cli_sk, client_id
|
||||||
|
|
||||||
|
|
||||||
def post_request(data, endpoint):
|
def post_request(data, endpoint):
|
||||||
@@ -406,17 +402,15 @@ def main(command, args, command_line_conf):
|
|||||||
teos_url = "http://" + teos_url
|
teos_url = "http://" + teos_url
|
||||||
|
|
||||||
try:
|
try:
|
||||||
teos_pk, cli_sk, compressed_cli_pk = load_keys(
|
teos_id, cli_sk, client_id = load_keys(config.get("TEOS_PUBLIC_KEY"), config.get("CLI_PRIVATE_KEY"))
|
||||||
config.get("TEOS_PUBLIC_KEY"), config.get("CLI_PRIVATE_KEY"), config.get("CLI_PUBLIC_KEY")
|
|
||||||
)
|
|
||||||
|
|
||||||
if command == "register":
|
if command == "register":
|
||||||
register_data = register(compressed_cli_pk, teos_url)
|
register_data = register(client_id, teos_url)
|
||||||
logger.info("Registration succeeded. Available slots: {}".format(register_data.get("available_slots")))
|
logger.info("Registration succeeded. Available slots: {}".format(register_data.get("available_slots")))
|
||||||
|
|
||||||
if command == "add_appointment":
|
if command == "add_appointment":
|
||||||
appointment_data = parse_add_appointment_args(args)
|
appointment_data = parse_add_appointment_args(args)
|
||||||
appointment, signature = add_appointment(appointment_data, cli_sk, teos_pk, teos_url)
|
appointment, signature = add_appointment(appointment_data, cli_sk, teos_id, teos_url)
|
||||||
save_appointment_receipt(appointment.to_dict(), signature, config.get("APPOINTMENTS_FOLDER_NAME"))
|
save_appointment_receipt(appointment.to_dict(), signature, config.get("APPOINTMENTS_FOLDER_NAME"))
|
||||||
|
|
||||||
elif command == "get_appointment":
|
elif command == "get_appointment":
|
||||||
@@ -429,7 +423,7 @@ def main(command, args, command_line_conf):
|
|||||||
if arg_opt in ["-h", "--help"]:
|
if arg_opt in ["-h", "--help"]:
|
||||||
sys.exit(help_get_appointment())
|
sys.exit(help_get_appointment())
|
||||||
|
|
||||||
appointment_data = get_appointment(arg_opt, cli_sk, teos_pk, teos_url)
|
appointment_data = get_appointment(arg_opt, cli_sk, teos_id, teos_url)
|
||||||
if appointment_data:
|
if appointment_data:
|
||||||
print(appointment_data)
|
print(appointment_data)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user