mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-18 06:34:19 +01:00
Separated signing logic of the Watcher in sign_appointment function; added unit test
This commit is contained in:
@@ -34,6 +34,10 @@ class Watcher:
|
||||
secret_key_pem = key_file.read().encode("utf-8")
|
||||
self.signing_key = load_pem_private_key(secret_key_pem, password=None, backend=default_backend())
|
||||
|
||||
def sign_appointment(self, appointment):
|
||||
data = appointment.to_json().encode("utf-8")
|
||||
return self.signing_key.sign(data, ec.ECDSA(hashes.SHA256()))
|
||||
|
||||
def add_appointment(self, appointment):
|
||||
# Rationale:
|
||||
# The Watcher will analyze every received block looking for appointment matches. If there is no work
|
||||
@@ -73,10 +77,7 @@ class Watcher:
|
||||
|
||||
logger.info("New appointment accepted.", locator=appointment.locator)
|
||||
|
||||
signature = self.signing_key.sign(
|
||||
appointment.to_json().encode("utf-8"),
|
||||
ec.ECDSA(hashes.SHA256())
|
||||
)
|
||||
signature = self.sign_appointment(appointment)
|
||||
else:
|
||||
appointment_added = False
|
||||
|
||||
|
||||
@@ -82,6 +82,15 @@ def create_appointments(n):
|
||||
return appointments, locator_uuid_map, dispute_txs
|
||||
|
||||
|
||||
def verify_signature(appointment, signature, pk):
|
||||
# verify the signature
|
||||
try:
|
||||
data = appointment.to_json().encode('utf-8')
|
||||
pk.verify(signature, data, ec.ECDSA(hashes.SHA256()))
|
||||
except InvalidSignature:
|
||||
assert False, "The appointment's signature is not correct"
|
||||
|
||||
|
||||
def test_init(watcher):
|
||||
assert type(watcher.appointments) is dict and len(watcher.appointments) == 0
|
||||
assert type(watcher.locator_uuid_map) is dict and len(watcher.locator_uuid_map) == 0
|
||||
@@ -92,6 +101,12 @@ def test_init(watcher):
|
||||
assert type(watcher.responder) is Responder
|
||||
|
||||
|
||||
def test_sign_appointment(watcher):
|
||||
appointment, _ = generate_dummy_appointment()
|
||||
signature = watcher.sign_appointment(appointment)
|
||||
verify_signature(appointment, signature, public_key)
|
||||
|
||||
|
||||
def test_add_appointment(run_bitcoind, watcher):
|
||||
# The watcher automatically fires do_watch and do_subscribe on adding an appointment if it is asleep (initial state)
|
||||
# Avoid this by setting the state to awake.
|
||||
@@ -104,12 +119,7 @@ def test_add_appointment(run_bitcoind, watcher):
|
||||
|
||||
assert added_appointment is True
|
||||
|
||||
# verify the signature
|
||||
try:
|
||||
data = appointment.to_json().encode('utf-8')
|
||||
public_key.verify(sig, data, ec.ECDSA(hashes.SHA256()))
|
||||
except InvalidSignature:
|
||||
assert False, "The appointment's signature is not correct"
|
||||
verify_signature(appointment, sig, public_key)
|
||||
|
||||
|
||||
def test_add_too_many_appointments(watcher):
|
||||
@@ -121,6 +131,7 @@ def test_add_too_many_appointments(watcher):
|
||||
added_appointment, sig = watcher.add_appointment(appointment)
|
||||
|
||||
assert added_appointment is True
|
||||
verify_signature(appointment, sig, public_key)
|
||||
|
||||
appointment, dispute_tx = generate_dummy_appointment()
|
||||
added_appointment, sig = watcher.add_appointment(appointment)
|
||||
|
||||
Reference in New Issue
Block a user