Skip to content

Commit

Permalink
fix lint errors. fix checks for soft as Base64 etc
Browse files Browse the repository at this point in the history
SmithSamuelM committed Mar 28, 2024
1 parent 7276774 commit 9575a7e
Showing 3 changed files with 50 additions and 20 deletions.
44 changes: 29 additions & 15 deletions src/keri/core/coring.py
Original file line number Diff line number Diff line change
@@ -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,28 +901,35 @@ 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:
raise InvalidSoftError(f"Non-empty {soft=} part when not"
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.")

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
9 changes: 5 additions & 4 deletions src/keri/core/serdering.py
Original file line number Diff line number Diff line change
@@ -1288,21 +1288,22 @@ 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
#frame.extend(e.encode("utf-8"))

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
17 changes: 16 additions & 1 deletion src/keri/kering.py
Original file line number Diff line number Diff line change
@@ -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,34 +535,39 @@ class InvalidVersionError(MaterialError):
raise InvalidVersionError("error message")
"""


class InvalidCodeError(MaterialError):
"""
Invalid, Unknown, or unrecognized code encountered during crypto material init
Usage:
raise InvalidCodeError("error message")
"""


class InvalidSoftError(MaterialError):
"""
Invalid, Unknown, or unrecognized soft part encountered during crypto material init
Usage:
raise InvalidSoftError("error message")
"""


class InvalidTypeError(MaterialError):
"""
Invalid material value type encountered during crypto material init
Usage:
raise InvalidTypeError("error message")
"""


class InvalidValueError(MaterialError):
"""
Invalid material value encountered during crypto material init
Usage:
raise InvalidValueError("error message")
"""


class InvalidSizeError(MaterialError):
"""
Invalid size encountered during crypto material init

0 comments on commit 9575a7e

Please sign in to comment.