mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 22:24:23 +01:00
Refactors code to use appointment from common based on f4a1e34e2f4d75226c7e313b83ff5ae06c8c89ca
This commit is contained in:
@@ -21,6 +21,7 @@ from apps.cli import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from common.logger import Logger
|
from common.logger import Logger
|
||||||
|
from common.appointment import Appointment
|
||||||
from common.constants import LOCATOR_LEN_HEX
|
from common.constants import LOCATOR_LEN_HEX
|
||||||
from common.cryptographer import Cryptographer
|
from common.cryptographer import Cryptographer
|
||||||
from common.tools import check_sha256_hex_format
|
from common.tools import check_sha256_hex_format
|
||||||
@@ -128,13 +129,7 @@ def add_appointment(args):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
add_appointment_endpoint = "http://{}:{}".format(pisa_api_server, pisa_api_port)
|
add_appointment_endpoint = "http://{}:{}".format(pisa_api_server, pisa_api_port)
|
||||||
appointment = build_appointment(
|
appointment = Appointment.from_dict(appointment_data)
|
||||||
appointment_data.get("tx"),
|
|
||||||
appointment_data.get("tx_id"),
|
|
||||||
appointment_data.get("start_time"),
|
|
||||||
appointment_data.get("end_time"),
|
|
||||||
appointment_data.get("to_self_delay"),
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
sk_der = load_key_file_data(CLI_PRIVATE_KEY)
|
sk_der = load_key_file_data(CLI_PRIVATE_KEY)
|
||||||
@@ -152,7 +147,7 @@ def add_appointment(args):
|
|||||||
logger.error("I/O error", errno=e.errno, error=e.strerror)
|
logger.error("I/O error", errno=e.errno, error=e.strerror)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
signature = Cryptographer.sign(Cryptographer.signature_format(appointment), cli_sk)
|
signature = Cryptographer.sign(appointment.serialize(), cli_sk)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cli_pk_der = load_key_file_data(CLI_PUBLIC_KEY)
|
cli_pk_der = load_key_file_data(CLI_PUBLIC_KEY)
|
||||||
@@ -217,7 +212,7 @@ def add_appointment(args):
|
|||||||
logger.error("Failed to deserialize the public key. It might be in an unsupported format")
|
logger.error("Failed to deserialize the public key. It might be in an unsupported format")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
is_sig_valid = Cryptographer.verify(Cryptographer.signature_format(appointment), signature, pisa_pk)
|
is_sig_valid = Cryptographer.verify(appointment.serialize(), signature, pisa_pk)
|
||||||
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
logger.error("Pisa's public key file not found. Please check your settings")
|
logger.error("Pisa's public key file not found. Please check your settings")
|
||||||
@@ -278,24 +273,6 @@ def get_appointment(args):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def build_appointment(tx, tx_id, start_time, end_time, to_self_delay):
|
|
||||||
locator = compute_locator(tx_id)
|
|
||||||
|
|
||||||
# FIXME: The blob data should contain more things that just the transaction. Leaving like this for now.
|
|
||||||
blob = Blob(tx)
|
|
||||||
encrypted_blob = Cryptographer.encrypt(blob, tx_id)
|
|
||||||
|
|
||||||
appointment = {
|
|
||||||
"locator": locator,
|
|
||||||
"start_time": start_time,
|
|
||||||
"end_time": end_time,
|
|
||||||
"to_self_delay": to_self_delay,
|
|
||||||
"encrypted_blob": encrypted_blob,
|
|
||||||
}
|
|
||||||
|
|
||||||
return appointment
|
|
||||||
|
|
||||||
|
|
||||||
def show_usage():
|
def show_usage():
|
||||||
return (
|
return (
|
||||||
"USAGE: "
|
"USAGE: "
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from flask import Flask, request, abort, jsonify
|
|||||||
from pisa import HOST, PORT, logging
|
from pisa import HOST, PORT, logging
|
||||||
from common.logger import Logger
|
from common.logger import Logger
|
||||||
from pisa.inspector import Inspector
|
from pisa.inspector import Inspector
|
||||||
from pisa.appointment import Appointment
|
from common.appointment import Appointment
|
||||||
from pisa.block_processor import BlockProcessor
|
from pisa.block_processor import BlockProcessor
|
||||||
|
|
||||||
from common.constants import HTTP_OK, HTTP_BAD_REQUEST, HTTP_SERVICE_UNAVAILABLE, LOCATOR_LEN_HEX
|
from common.constants import HTTP_OK, HTTP_BAD_REQUEST, HTTP_SERVICE_UNAVAILABLE, LOCATOR_LEN_HEX
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from queue import Queue
|
from queue import Queue
|
||||||
|
|
||||||
from pisa.responder import TransactionTracker
|
from pisa.responder import TransactionTracker
|
||||||
from pisa.appointment import Appointment
|
from common.appointment import Appointment
|
||||||
|
|
||||||
|
|
||||||
class Builder:
|
class Builder:
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from common.cryptographer import Cryptographer
|
|||||||
from pisa import errors
|
from pisa import errors
|
||||||
import pisa.conf as conf
|
import pisa.conf as conf
|
||||||
from common.logger import Logger
|
from common.logger import Logger
|
||||||
from pisa.appointment import Appointment
|
from common.appointment import Appointment
|
||||||
from pisa.block_processor import BlockProcessor
|
from pisa.block_processor import BlockProcessor
|
||||||
|
|
||||||
logger = Logger("Inspector")
|
logger = Logger("Inspector")
|
||||||
@@ -309,12 +309,12 @@ class Inspector:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
# Verifies that the appointment signature is a valid signature with public key
|
# Verifies that the appointment signature is a valid signature with public key
|
||||||
def check_appointment_signature(appointment, signature, pk_der):
|
def check_appointment_signature(appointment_data, signature, pk_der):
|
||||||
"""
|
"""
|
||||||
Checks if the provided user signature is correct.
|
Checks if the provided user signature is correct.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
appointment (:obj:`dict`): the appointment that was signed by the user.
|
appointment_data (:obj:`dict`): the appointment that was signed by the user.
|
||||||
signature (:obj:`str`): the user's signature (hex encoded).
|
signature (:obj:`str`): the user's signature (hex encoded).
|
||||||
pk_der (:obj:`str`): the user's public key (hex encoded, DER format).
|
pk_der (:obj:`str`): the user's public key (hex encoded, DER format).
|
||||||
|
|
||||||
@@ -336,7 +336,7 @@ class Inspector:
|
|||||||
message = "empty signature received"
|
message = "empty signature received"
|
||||||
|
|
||||||
pk = Cryptographer.load_public_key_der(unhexlify(pk_der))
|
pk = Cryptographer.load_public_key_der(unhexlify(pk_der))
|
||||||
valid_sig = Cryptographer.verify(Cryptographer.signature_format(appointment), signature, pk)
|
valid_sig = Cryptographer.verify(Appointment.from_dict(appointment_data).serialize(), signature, pk)
|
||||||
|
|
||||||
if not valid_sig:
|
if not valid_sig:
|
||||||
rcode = errors.APPOINTMENT_INVALID_SIGNATURE
|
rcode = errors.APPOINTMENT_INVALID_SIGNATURE
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ class Watcher:
|
|||||||
|
|
||||||
logger.info("New appointment accepted", locator=appointment.locator)
|
logger.info("New appointment accepted", locator=appointment.locator)
|
||||||
|
|
||||||
signature = Cryptographer.sign(Cryptographer.signature_format(appointment.to_dict()), self.signing_key)
|
signature = Cryptographer.sign(appointment.serialize(), self.signing_key)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
appointment_added = False
|
appointment_added = False
|
||||||
|
|||||||
Reference in New Issue
Block a user