From 99db9a29ad4fd6b2a784b8c8613e8c25a2a2b4af Mon Sep 17 00:00:00 2001 From: Salvatore Ingala <6681844+bigspider@users.noreply.github.com> Date: Thu, 10 Oct 2019 16:38:34 +0700 Subject: [PATCH] Added signatures to appointment --- apps/generate_key.py | 6 ++++++ pisa/api.py | 4 ++-- pisa/appointment.py | 4 ++++ pisa/requirements.txt | 1 + pisa/watcher.py | 11 ++++++++--- 5 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 apps/generate_key.py diff --git a/apps/generate_key.py b/apps/generate_key.py new file mode 100644 index 0000000..806da97 --- /dev/null +++ b/apps/generate_key.py @@ -0,0 +1,6 @@ +import ecdsa + + +if __name__ == '__main__': + sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1) + print(sk.to_der()) diff --git a/pisa/api.py b/pisa/api.py index cd05e02..a218af2 100644 --- a/pisa/api.py +++ b/pisa/api.py @@ -30,12 +30,12 @@ def add_appointment(): appointment = inspector.inspect(request_data) if type(appointment) == Appointment: - appointment_added = watcher.add_appointment(appointment) + appointment_added, signature = watcher.add_appointment(appointment) # ToDo: #13-create-server-side-signature-receipt if appointment_added: rcode = HTTP_OK - response = "appointment accepted. locator: {}".format(appointment.locator) + response = "appointment accepted. locator: {}. signature: {}".format(appointment.locator, signature) else: rcode = HTTP_SERVICE_UNAVAILABLE response = "appointment rejected" diff --git a/pisa/appointment.py b/pisa/appointment.py index 816fde6..39569f9 100644 --- a/pisa/appointment.py +++ b/pisa/appointment.py @@ -1,3 +1,5 @@ +import json + from pisa.encrypted_blob import EncryptedBlob @@ -22,3 +24,5 @@ class Appointment: # ToDO: #3-improve-appointment-structure + def serialize(self): + return json.dumps(self.to_json()) diff --git a/pisa/requirements.txt b/pisa/requirements.txt index 8ec7402..10e961c 100644 --- a/pisa/requirements.txt +++ b/pisa/requirements.txt @@ -1,4 +1,5 @@ zmq flask cryptography +ecdsa requests \ No newline at end of file diff --git a/pisa/watcher.py b/pisa/watcher.py index 3deda68..e189c9c 100644 --- a/pisa/watcher.py +++ b/pisa/watcher.py @@ -1,12 +1,12 @@ from uuid import uuid4 from queue import Queue from threading import Thread +import ecdsa from pisa.logger import Logger from pisa.cleaner import Cleaner -from pisa.conf import EXPIRY_DELTA +from pisa.conf import EXPIRY_DELTA, MAX_APPOINTMENTS, SIGNING_KEY_DER from pisa.responder import Responder -from pisa.conf import MAX_APPOINTMENTS from pisa.block_processor import BlockProcessor from pisa.utils.zmq_subscriber import ZMQHandler @@ -22,6 +22,7 @@ class Watcher: self.max_appointments = max_appointments self.zmq_subscriber = None self.responder = Responder() + self.sk = ecdsa.SigningKey.from_der(SIGNING_KEY_DER) def add_appointment(self, appointment): # Rationale: @@ -31,6 +32,8 @@ class Watcher: # If the watcher is awake, every new appointment will just be added to the appointment list until # max_appointments is reached. + signature = None + if len(self.appointments) < self.max_appointments: # Appointments are identified by the locator: the sha256 of commitment txid (H(tx_id)). # Two different nodes may ask for appointments using the same commitment txid, what will result in a @@ -60,12 +63,14 @@ class Watcher: logger.info("New appointment accepted.", locator=appointment.locator) + signature = self.sk.sign(appointment.serialize().encode('utf8')) + else: appointment_added = False logger.info("Maximum appointments reached, appointment rejected.", locator=appointment.locator) - return appointment_added + return (appointment_added, signature) def do_subscribe(self): self.zmq_subscriber = ZMQHandler(parent="Watcher")