Skip to content

Commit

Permalink
Improved paper message packing
Browse files Browse the repository at this point in the history
  • Loading branch information
markqvist committed Nov 22, 2022
1 parent edbb887 commit abe2e78
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
12 changes: 6 additions & 6 deletions LXMF/LXMRouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1100,26 +1100,26 @@ def lxmf_propagation(self, lxmf_data, signal_local_delivery=None, signal_duplica
RNS.log("The contained exception was: "+str(e), RNS.LOG_DEBUG)
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:
if not url.lower().startswith("lxm://"):
RNS.log("Cannot ingest LXM, invalid URL provided.", RNS.LOG_ERROR)
if not uri.lower().startswith(LXMessage.URI_SCHEMA+"://"):
RNS.log("Cannot ingest LXM, invalid URI provided.", RNS.LOG_ERROR)
return False

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)

router_propagation_result = self.lxmf_propagation(lxmf_data, signal_local_delivery=signal_local_delivery, signal_duplicate=signal_duplicate)
if router_propagation_result != False:
RNS.log("LXM with transient ID "+RNS.prettyhexrep(transient_id)+" was ingested.", RNS.LOG_DEBUG)
return router_propagation_result
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

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

def fail_message(self, lxmessage):
Expand Down
27 changes: 14 additions & 13 deletions LXMF/LXMessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ class LXMessage:
ENCRYPTION_DESCRIPTION_EC = "Curve25519"
ENCRYPTION_DESCRIPTION_UNENCRYPTED = "Unencrypted"

# Constants for QR/URL encoding LXMs
URL_PROTO_SPECIFIER = "lxm"
# Constants for QR/URI encoding LXMs
URI_SCHEMA = "lxm"
QR_ERROR_CORRECTION = "ERROR_CORRECT_L"
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):
if self.hash != None:
Expand Down Expand Up @@ -123,6 +123,9 @@ def __init__(self, destination, source, content = "", title = "", fields = None,
self.state = LXMessage.DRAFT
self.method = LXMessage.UNKNOWN

self.propagation_packed = None
self.paper_packed = None

self.incoming = False
self.signature_validated = False
self.unverified_reason = None
Expand Down Expand Up @@ -483,7 +486,7 @@ def write_to_directory(self, directory_path):
RNS.log("Error while writing LXMF message to file \""+str(file_path)+"\". The contained exception was: "+str(e), RNS.LOG_ERROR)
return None

def as_url(self, finalise=True):
def as_uri(self, finalise=True):
self.determine_transport_encryption()
if not self.packed:
self.pack()
Expand All @@ -493,18 +496,15 @@ def as_url(self, finalise=True):
encoded_bytes = base64.urlsafe_b64encode(self.paper_packed)

# Add protocol specifier and return
lxm_url = LXMessage.URL_PROTO_SPECIFIER+"://"+encoded_bytes.decode("utf-8").replace("=","")

# TODO: Remove
# RNS.log(str(len(lxm_url))+" byte LXM URL: "+str(lxm_url), RNS.LOG_EXTREME)
lxm_uri = LXMessage.URI_SCHEMA+"://"+encoded_bytes.decode("utf-8").replace("=","")

if finalise:
self.__mark_paper_generated()

return lxm_url
return lxm_uri

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):
self.determine_transport_encryption()
Expand All @@ -519,7 +519,7 @@ def as_qr(self):
qr = qrcode.make(
error_correction = qrcode.constants.__dict__[LXMessage.QR_ERROR_CORRECTION],
border = 1,
data = self.as_url(finalise=False),
data = self.as_uri(finalise=False),
)

self.__mark_paper_generated()
Expand All @@ -535,7 +535,7 @@ def as_qr(self):
raise TypeError("Attempt to represent LXM with non-paper delivery method as QR-code")

@staticmethod
def unpack_from_bytes(lxmf_bytes):
def unpack_from_bytes(lxmf_bytes, original_method = None):
destination_hash = lxmf_bytes[: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]
Expand Down Expand Up @@ -568,7 +568,8 @@ def unpack_from_bytes(lxmf_bytes):
title = "",
fields = fields,
destination_hash = destination_hash,
source_hash = source_hash)
source_hash = source_hash,
desired_method = original_method)

message.hash = message_hash
message.signature = signature
Expand Down

0 comments on commit abe2e78

Please sign in to comment.