From 9575a7e249289c3f89172012429938091a7003a8 Mon Sep 17 00:00:00 2001 From: Samuel M Smith Date: Thu, 28 Mar 2024 17:00:50 -0600 Subject: [PATCH] fix lint errors. fix checks for soft as Base64 etc --- src/keri/core/coring.py | 44 +++++++++++++++++++++++++------------- src/keri/core/serdering.py | 9 ++++---- src/keri/kering.py | 17 ++++++++++++++- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/keri/core/coring.py b/src/keri/core/coring.py index 820058c92..44f962c1d 100644 --- a/src/keri/core/coring.py +++ b/src/keri/core/coring.py @@ -27,7 +27,7 @@ from ..kering import MaxON -from ..kering import (EmptyMaterialError, RawMaterialError, +from ..kering import (EmptyMaterialError, RawMaterialError, SoftMaterialError, InvalidCodeError, InvalidSoftError, InvalidSizeError, InvalidCodeSizeError, InvalidVarIndexError, @@ -901,8 +901,10 @@ def __init__(self, raw=None, code=MtrDex.Ed25519N, rize=None, else: hs, ss, fs, ls = self.Sizes[code] # get sizes assumes ls consistent - if not fs: # invalid - raise InvalidVarSizeError(f"Unsupported variable size {code=}.") + if not fs: # invalid must not be variable size + raise InvalidVarSizeError(f"Unsupported {code=} for " + f"variable size {fs=}.") + rize = Matter._rawSize(code) if ss == 0 and soft: @@ -910,10 +912,6 @@ def __init__(self, raw=None, code=MtrDex.Ed25519N, rize=None, f" special.") if fs == hs + ss and ss > 0: # special soft size - if not soft or len(soft) < ss: - raise ShortageError(f"Not enough chars in {code=} " - f"{soft=} with {ss=}.") - if ls != 0: # lead must be zero raise InvalidSoftError(f"Nonzero lead(ls)) for {code=}" f" {soft=} when special.") @@ -921,8 +919,17 @@ def __init__(self, raw=None, code=MtrDex.Ed25519N, rize=None, if rize: # raw must be empty raise RawMaterialError(f"Nonzero raw size {rize=} when " f" special {code=} {soft=}.") + + if not soft or len(soft) < ss: + raise SoftMaterialError(f"Not enough chars in {code=} " + f"{soft=} with {ss=}.") + soft = soft[:ss] + if not Reb64.match(soft): + raise InvalidSoftError(f"Non Base64 chars in {soft=}.") + + raw = raw[:rize] # copy only exact size from raw stream if len(raw) != rize: # forbids shorter raise RawMaterialError(f"Not enougth raw bytes for code={code}" @@ -935,21 +942,28 @@ def __init__(self, raw=None, code=MtrDex.Ed25519N, rize=None, elif soft and code: # special when raw None hs, ss, fs, ls = self.Sizes[code] # get sizes assumes ls consistent - if not fs: # invalid can be variable size - raise InvalidVarSizeError(f"Missing raw for variable size {code=}.") - - if ss == 0: - raise InvalidCodeError("Nonempty {soft=} part for zero soft " - f"size {ss=} for {code=}.") + if not fs: # + raise InvalidSoftError(f"Unsupported {code=} {fs=} for special" + f" soft.") - if fs != hs + ss or len(soft) != ss or ls != 0: # not special soft code - raise InvalidSoftError("Invalid {soft=} or {code=} when special.") + if fs != hs + ss or ss == 0 or ls != 0: # not special soft code + raise InvalidSoftError("Invalid {code=} {fs=} or lead={ls} " + f" when special soft.") rize = Matter._rawSize(code) if rize: raise InvalidSizeError(f"Nonzero raw size {rize=} when special" f" {code=}.") + if not soft or len(soft) < ss: + raise SoftMaterialError(f"Not enough chars in {code=} " + f"{soft=} with {ss=}.") + + soft = soft[:ss] + + if not Reb64.match(soft): + raise InvalidSoftError(f"Non Base64 chars in {soft=}.") + self._code = code # str hard part of code self._soft = soft # str soft part of code, empty when ss=0 self._size = size # int of soft part value, None when fs != None diff --git a/src/keri/core/serdering.py b/src/keri/core/serdering.py index 0a83a36cf..50a7bfc2f 100644 --- a/src/keri/core/serdering.py +++ b/src/keri/core/serdering.py @@ -1288,7 +1288,7 @@ def _dumps(self, sad): pass val = bytearray(Counter(tag=AllTags.GenericMapGroup, count=len(frame) % 4, - version=cversion).qb64b) + version=self.gvrsn).qb64b) else: for e in v: # list pass @@ -1296,13 +1296,14 @@ def _dumps(self, sad): val = bytearray(Counter(tag=AllTags.GenericListGroup, count=len(frame) % 4, - version=cversion).qb64b) + version=self.gvrsn).qb64b) val.extend(frame) case _: # if extra fields this is where logic would be raise SerializeError(f"Unsupported protocol field label" - f"='{l}' for {protocol=} {version=}.") + f"='{l}' for protocol={self.proto}" + f" version={self.vrsn}.") raw.extend(val) @@ -1316,7 +1317,7 @@ def _dumps(self, sad): else: - raise SerializeError(f"Unsupported protocol={self.protocol}.") + raise SerializeError(f"Unsupported protocol={self.proto}.") # prepend count code for message diff --git a/src/keri/kering.py b/src/keri/kering.py index 556b1cd4e..0ab43c76a 100644 --- a/src/keri/kering.py +++ b/src/keri/kering.py @@ -222,6 +222,7 @@ def snatch(match, size=0): regular expressions work with memoryview objects not just bytes or bytearrays """ + full = match.group() # full matched version string if len(full) == VFFULLSPAN: proto, major, minor, gmajor, gminor = match.group("proto0", "major0", @@ -506,7 +507,15 @@ class RawMaterialError(MaterialError): """ Not Enough bytes in buffer bytearray for raw material Usage: - raise ShortageError("error message") + raise RawMaterialError("error message") + """ + + +class SoftMaterialError(MaterialError): + """ + Not Enough chars in soft for soft material + Usage: + raise SoftMaterialError("error message") """ @@ -517,6 +526,7 @@ class EmptyMaterialError(MaterialError): raise EmptyMaterialError("error message") """ + class InvalidVersionError(MaterialError): """ Invalid, Unknown, or unrecognized CESR code table version encountered during @@ -525,6 +535,7 @@ class InvalidVersionError(MaterialError): raise InvalidVersionError("error message") """ + class InvalidCodeError(MaterialError): """ Invalid, Unknown, or unrecognized code encountered during crypto material init @@ -532,6 +543,7 @@ class InvalidCodeError(MaterialError): raise InvalidCodeError("error message") """ + class InvalidSoftError(MaterialError): """ Invalid, Unknown, or unrecognized soft part encountered during crypto material init @@ -539,6 +551,7 @@ class InvalidSoftError(MaterialError): raise InvalidSoftError("error message") """ + class InvalidTypeError(MaterialError): """ Invalid material value type encountered during crypto material init @@ -546,6 +559,7 @@ class InvalidTypeError(MaterialError): raise InvalidTypeError("error message") """ + class InvalidValueError(MaterialError): """ Invalid material value encountered during crypto material init @@ -553,6 +567,7 @@ class InvalidValueError(MaterialError): raise InvalidValueError("error message") """ + class InvalidSizeError(MaterialError): """ Invalid size encountered during crypto material init