Gets rid of blob classes. Close #66

This commit is contained in:
Sergi Delgado Segura
2020-04-09 17:54:05 +02:00
parent 80e72f089d
commit 71507d4c32
17 changed files with 42 additions and 109 deletions

View File

@@ -1,8 +1,6 @@
import struct
from binascii import unhexlify
from common.encrypted_blob import EncryptedBlob
class Appointment:
"""
@@ -15,9 +13,8 @@ class Appointment:
end_time (:obj:`int`): The block height where the tower will stop watching for breaches.
to_self_delay (:obj:`int`): The ``to_self_delay`` encoded in the ``csv`` of the ``to_remote`` output of the
commitment transaction that this appointment is covering.
encrypted_blob (:obj:`EncryptedBlob <common.encrypted_blob.EncryptedBlob>`): An ``EncryptedBlob`` object
containing an encrypted penalty transaction. The tower will decrypt it and broadcast the penalty transaction
upon seeing a breach on the blockchain.
encrypted_blob (:obj:`str`): An encrypted blob of data containing a penalty transaction. The tower will decrypt
it and broadcast the penalty transaction upon seeing a breach on the blockchain.
"""
def __init__(self, locator, start_time, end_time, to_self_delay, encrypted_blob):
@@ -25,7 +22,7 @@ class Appointment:
self.start_time = start_time # ToDo: #4-standardize-appointment-fields
self.end_time = end_time # ToDo: #4-standardize-appointment-fields
self.to_self_delay = to_self_delay
self.encrypted_blob = EncryptedBlob(encrypted_blob)
self.encrypted_blob = encrypted_blob
@classmethod
def from_dict(cls, appointment_data):
@@ -73,7 +70,7 @@ class Appointment:
"start_time": self.start_time,
"end_time": self.end_time,
"to_self_delay": self.to_self_delay,
"encrypted_blob": self.encrypted_blob.data,
"encrypted_blob": self.encrypted_blob,
}
return appointment
@@ -95,5 +92,5 @@ class Appointment:
+ struct.pack(">I", self.start_time)
+ struct.pack(">I", self.end_time)
+ struct.pack(">I", self.to_self_delay)
+ unhexlify(self.encrypted_blob.data)
+ unhexlify(self.encrypted_blob)
)

View File

@@ -1,9 +0,0 @@
import re
class Blob:
def __init__(self, data):
if type(data) is not str or re.search(r"^[0-9A-Fa-f]+$", data) is None:
raise ValueError("Non-Hex character found in transaction.")
self.data = data

View File

@@ -180,7 +180,7 @@ class Cryptographer:
:obj:`ValueError`: if either the ``secret`` or ``encrypted_blob`` is not properly formatted.
"""
Cryptographer.check_data_key_format(encrypted_blob.data, secret)
Cryptographer.check_data_key_format(encrypted_blob, secret)
# sk is the H(txid) (32-byte) and nonce is set to 0 (12-byte)
sk = sha256(unhexlify(secret)).digest()
@@ -195,7 +195,7 @@ class Cryptographer:
# Decrypt
cipher = ChaCha20Poly1305(sk)
data = unhexlify(encrypted_blob.data)
data = unhexlify(encrypted_blob)
try:
blob = cipher.decrypt(nonce=nonce, data=data, associated_data=None)

View File

@@ -1,6 +0,0 @@
class EncryptedBlob:
def __init__(self, data):
self.data = data
def __eq__(self, other):
return isinstance(other, EncryptedBlob) and self.data == other.data