Improved paper message packing

This commit is contained in:
Mark Qvist 2022-11-22 19:53:25 +01:00
parent edbb887d81
commit abe2e78283
2 changed files with 20 additions and 19 deletions

View File

@ -1100,14 +1100,14 @@ class LXMRouter:
RNS.log("The contained exception was: "+str(e), RNS.LOG_DEBUG) RNS.log("The contained exception was: "+str(e), RNS.LOG_DEBUG)
return False return False
def ingest_lxm_url(self, url, signal_local_delivery=None, signal_duplicate=None): def ingest_lxm_uri(self, uri, signal_local_delivery=None, signal_duplicate=None):
try: try:
if not url.lower().startswith("lxm://"): if not uri.lower().startswith(LXMessage.URI_SCHEMA+"://"):
RNS.log("Cannot ingest LXM, invalid URL provided.", RNS.LOG_ERROR) RNS.log("Cannot ingest LXM, invalid URI provided.", RNS.LOG_ERROR)
return False return False
else: else:
lxmf_data = base64.urlsafe_b64decode(url.replace(LXMessage.URL_PROTO_SPECIFIER+"://", "").replace("/", "")+"==") lxmf_data = base64.urlsafe_b64decode(uri.replace(LXMessage.URI_SCHEMA+"://", "").replace("/", "")+"==")
transient_id = RNS.Identity.full_hash(lxmf_data) transient_id = RNS.Identity.full_hash(lxmf_data)
router_propagation_result = self.lxmf_propagation(lxmf_data, signal_local_delivery=signal_local_delivery, signal_duplicate=signal_duplicate) router_propagation_result = self.lxmf_propagation(lxmf_data, signal_local_delivery=signal_local_delivery, signal_duplicate=signal_duplicate)
@ -1115,11 +1115,11 @@ class LXMRouter:
RNS.log("LXM with transient ID "+RNS.prettyhexrep(transient_id)+" was ingested.", RNS.LOG_DEBUG) RNS.log("LXM with transient ID "+RNS.prettyhexrep(transient_id)+" was ingested.", RNS.LOG_DEBUG)
return router_propagation_result return router_propagation_result
else: else:
RNS.log("No valid LXM could be ingested from the provided URL", RNS.LOG_DEBUG) RNS.log("No valid LXM could be ingested from the provided URI", RNS.LOG_DEBUG)
return False return False
except Exception as e: except Exception as e:
RNS.log("Error while decoding URL-encoded LXMF message. The contained exception was: "+str(e), RNS.LOG_ERROR) RNS.log("Error while decoding URI-encoded LXMF message. The contained exception was: "+str(e), RNS.LOG_ERROR)
return False return False
def fail_message(self, lxmessage): def fail_message(self, lxmessage):

View File

@ -78,11 +78,11 @@ class LXMessage:
ENCRYPTION_DESCRIPTION_EC = "Curve25519" ENCRYPTION_DESCRIPTION_EC = "Curve25519"
ENCRYPTION_DESCRIPTION_UNENCRYPTED = "Unencrypted" ENCRYPTION_DESCRIPTION_UNENCRYPTED = "Unencrypted"
# Constants for QR/URL encoding LXMs # Constants for QR/URI encoding LXMs
URL_PROTO_SPECIFIER = "lxm" URI_SCHEMA = "lxm"
QR_ERROR_CORRECTION = "ERROR_CORRECT_L" QR_ERROR_CORRECTION = "ERROR_CORRECT_L"
QR_MAX_STORAGE = 2953 QR_MAX_STORAGE = 2953
PAPER_MDU = ((QR_MAX_STORAGE-(len(URL_PROTO_SPECIFIER)+len("://")))*6)//8 PAPER_MDU = ((QR_MAX_STORAGE-(len(URI_SCHEMA)+len("://")))*6)//8
def __str__(self): def __str__(self):
if self.hash != None: if self.hash != None:
@ -123,6 +123,9 @@ class LXMessage:
self.state = LXMessage.DRAFT self.state = LXMessage.DRAFT
self.method = LXMessage.UNKNOWN self.method = LXMessage.UNKNOWN
self.propagation_packed = None
self.paper_packed = None
self.incoming = False self.incoming = False
self.signature_validated = False self.signature_validated = False
self.unverified_reason = None self.unverified_reason = None
@ -483,7 +486,7 @@ class LXMessage:
RNS.log("Error while writing LXMF message to file \""+str(file_path)+"\". The contained exception was: "+str(e), RNS.LOG_ERROR) RNS.log("Error while writing LXMF message to file \""+str(file_path)+"\". The contained exception was: "+str(e), RNS.LOG_ERROR)
return None return None
def as_url(self, finalise=True): def as_uri(self, finalise=True):
self.determine_transport_encryption() self.determine_transport_encryption()
if not self.packed: if not self.packed:
self.pack() self.pack()
@ -493,18 +496,15 @@ class LXMessage:
encoded_bytes = base64.urlsafe_b64encode(self.paper_packed) encoded_bytes = base64.urlsafe_b64encode(self.paper_packed)
# Add protocol specifier and return # Add protocol specifier and return
lxm_url = LXMessage.URL_PROTO_SPECIFIER+"://"+encoded_bytes.decode("utf-8").replace("=","") lxm_uri = LXMessage.URI_SCHEMA+"://"+encoded_bytes.decode("utf-8").replace("=","")
# TODO: Remove
# RNS.log(str(len(lxm_url))+" byte LXM URL: "+str(lxm_url), RNS.LOG_EXTREME)
if finalise: if finalise:
self.__mark_paper_generated() self.__mark_paper_generated()
return lxm_url return lxm_uri
else: else:
raise TypeError("Attempt to represent LXM with non-paper delivery method as URL") raise TypeError("Attempt to represent LXM with non-paper delivery method as URI")
def as_qr(self): def as_qr(self):
self.determine_transport_encryption() self.determine_transport_encryption()
@ -519,7 +519,7 @@ class LXMessage:
qr = qrcode.make( qr = qrcode.make(
error_correction = qrcode.constants.__dict__[LXMessage.QR_ERROR_CORRECTION], error_correction = qrcode.constants.__dict__[LXMessage.QR_ERROR_CORRECTION],
border = 1, border = 1,
data = self.as_url(finalise=False), data = self.as_uri(finalise=False),
) )
self.__mark_paper_generated() self.__mark_paper_generated()
@ -535,7 +535,7 @@ class LXMessage:
raise TypeError("Attempt to represent LXM with non-paper delivery method as QR-code") raise TypeError("Attempt to represent LXM with non-paper delivery method as QR-code")
@staticmethod @staticmethod
def unpack_from_bytes(lxmf_bytes): def unpack_from_bytes(lxmf_bytes, original_method = None):
destination_hash = lxmf_bytes[:LXMessage.DESTINATION_LENGTH] destination_hash = lxmf_bytes[:LXMessage.DESTINATION_LENGTH]
source_hash = lxmf_bytes[LXMessage.DESTINATION_LENGTH:2*LXMessage.DESTINATION_LENGTH] source_hash = lxmf_bytes[LXMessage.DESTINATION_LENGTH:2*LXMessage.DESTINATION_LENGTH]
signature = lxmf_bytes[2*LXMessage.DESTINATION_LENGTH:2*LXMessage.DESTINATION_LENGTH+LXMessage.SIGNATURE_LENGTH] signature = lxmf_bytes[2*LXMessage.DESTINATION_LENGTH:2*LXMessage.DESTINATION_LENGTH+LXMessage.SIGNATURE_LENGTH]
@ -568,7 +568,8 @@ class LXMessage:
title = "", title = "",
fields = fields, fields = fields,
destination_hash = destination_hash, destination_hash = destination_hash,
source_hash = source_hash) source_hash = source_hash,
desired_method = original_method)
message.hash = message_hash message.hash = message_hash
message.signature = signature message.signature = signature