From af2e280567550aa2f284e1f426172331cf70eaa1 Mon Sep 17 00:00:00 2001 From: Samuel M Smith Date: Sun, 28 Jul 2024 22:00:49 -0600 Subject: [PATCH] refactor streamlined xtra xs field support in Matter --- src/keri/core/coring.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/keri/core/coring.py b/src/keri/core/coring.py index 84bd0893..2c47104e 100644 --- a/src/keri/core/coring.py +++ b/src/keri/core/coring.py @@ -1424,21 +1424,18 @@ def _exfil(self, qb64b): # these are well formed. # when fs is None then ss > 0 otherwise fs > hs + ss when ss > 0 - xtra = qb64b[hs:hs+xs] # extract xtra prepad chars of soft, empty when xs==0 - if isinstance(xtra, memoryview): - xtra = bytes(xtra) - if hasattr(xtra, "decode"): - xtra = xtra.decode() # converts bytes/bytearray to str - if xtra != f"{self.Pad * xs}": - raise UnexpectedCodeError(f"Invalid prepad xtra ={xtra}.") - # extract soft chars excluding xtra, empty when ss==0 and xs == 0 + # extract soft chars including xtra, empty when ss==0 and xs == 0 # assumes that when ss == 0 then xs must be 0 - soft = qb64b[hs+xs:hs+ss] + soft = qb64b[hs:hs+ss] if isinstance(soft, memoryview): soft = bytes(soft) if hasattr(soft, "decode"): soft = soft.decode() # converts bytes/bytearray to str + xtra = soft[:xs] # extract xtra if any from front of soft + soft = soft[xs:] # strip xtra from soft + if xtra != f"{self.Pad * xs}": + raise UnexpectedCodeError(f"Invalid prepad xtra ={xtra}.") if not fs: # compute fs from soft from ss part which provides size B64 # compute variable size as int may have value 0 @@ -1516,13 +1513,14 @@ def _bexfil(self, qb2): raise ShortageError("Need {} more bytes.".format(bcs - len(qb2))) both = codeB2ToB64(qb2, cs) # extract and convert both hard and soft part of code - xtra = both[hs:hs+xs] # extract xtra prepad chars of soft, empty when xs==0 - if xtra != f"{self.Pad * xs}": - raise UnexpectedCodeError(f"Invalid prepad xtra ={xtra}.") - # extract soft chars excluding xtra, empty when ss==0 and xs == 0 + # extract soft chars including xtra, empty when ss==0 and xs == 0 # assumes that when ss == 0 then xs must be 0 - soft = both[hs+xs:hs+ss] # get soft may be empty + soft = both[hs:hs+ss] # get soft may be empty + xtra = soft[:xs] # extract xtra if any from front of soft + soft = soft[xs:] # strip xtra from soft + if xtra != f"{self.Pad * xs}": + raise UnexpectedCodeError(f"Invalid prepad xtra ={xtra}.") if not fs: # compute fs from size chars in ss part of code if len(qb2) < bcs: # need more bytes