Skip to content

Commit

Permalink
fixed up more SerderKERI verify tests
Browse files Browse the repository at this point in the history
clean up stale code in Prefixer
All verify functionality of prefixer is now in SerderKERI so Prefixer is legacy.
  • Loading branch information
SmithSamuelM committed Aug 7, 2024
1 parent 035d735 commit 3da4438
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 234 deletions.
108 changes: 6 additions & 102 deletions src/keri/core/coring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3265,126 +3265,30 @@ def _sha2_256(ser, raw):

class Prefixer(Matter):
"""
Prefixer is Matter subclass for autonomic identifier prefix using
derivation as determined by code from ked
Prefixer is Matter subclass for autonomic identifier AID prefix
Attributes:
Inherited Properties: (see Matter)
.pad is int number of pad chars given raw
.code is str derivation code to indicate cypher suite
.raw is bytes crypto material only without code
.index is int count of attached crypto material by context (receipts)
.qb64 is str in Base64 fully qualified with derivation code + crypto mat
.qb64b is bytes in Base64 fully qualified with derivation code + crypto mat
.qb2 is bytes in binary with derivation code + crypto material
.transferable is Boolean, True when transferable derivation code False otherwise
Properties:
Methods:
verify(): Verifies derivation of aid prefix from a ked
Hidden:
._pad is method to compute .pad property
._code is str value for .code property
._raw is bytes value for .raw property
._index is int value for .index property
._infil is method to compute fully qualified Base64 from .raw and .code
._exfil is method to extract .code and .raw from fully qualified Base64
"""
Dummy = "#" # dummy spaceholder char for pre. Must not be a valid Base64 char

def __init__(self, raw=None, code=None, ked=None, allows=None, **kwa):
"""
assign ._derive to derive aid prefix from ked
assign ._verify to verify derivation of aid prefix from ked
Default code is None to force EmptyMaterialError when only raw provided but
not code.
"""

def __init__(self, **kwa):
"""Checks for .code in PreDex so valid prefixive code
Inherited Parameters:
raw is bytes of unqualified crypto material usable for crypto operations
qb64b is bytes of fully qualified crypto material
qb64 is str or bytes of fully qualified crypto material
qb2 is bytes of fully qualified crypto material
code is str of derivation code
index is int of count of attached receipts for CryCntDex codes
Parameters:
allows (list): allowed codes for prefix. When None then all supported
codes are allowed. This enables a particular use case to restrict
the codes allowed to a subset of all supported.
See Matter
"""
super(Prefixer, self).__init__(raw=raw, code=code, **kwa)
super(Prefixer, self).__init__(**kwa)
if self.code not in PreDex:
raise InvalidCodeError(f"Invalid prefixer code = {self.code}.")

#if self.code in [MtrDex.Ed25519N, MtrDex.ECDSA_256r1N, MtrDex.ECDSA_256k1N]:
#self._verify = self._verify_non_transferable
#elif self.code in [MtrDex.Ed25519, MtrDex.ECDSA_256r1, MtrDex.ECDSA_256k1]:
#self._verify = self._verify_transferable


def _verify_non_transferable(self, ked, pre, prefixed=False):
"""
Returns True if verified False otherwise
Verify derivation of fully qualified Base64 pre from inception iked dict
Parameters:
ked is inception key event dict
pre is Base64 fully qualified prefix default to .qb64
"""
try:
keys = ked["k"]
if len(keys) != 1:
return False

if keys[0] != pre:
return False

if prefixed and ked["i"] != pre:
return False

if ked["n"]: # must be empty
return False

except Exception as ex:
return False

return True



def _verify_transferable(self, ked, pre, prefixed=False):
"""
Returns True if verified False otherwise
Verify derivation of fully qualified Base64 prefix from
inception key event dict (ked)
Parameters:
ked is inception key event dict
pre is Base64 fully qualified prefix default to .qb64
"""
try:
keys = ked["k"]
if len(keys) != 1:
return False

if keys[0] != pre:
return False

if prefixed and ked["i"] != pre:
return False

except Exception as ex:
return False

return True





# digest algorithm klas, digest size (not default), digest length
Expand Down
6 changes: 0 additions & 6 deletions src/keri/core/eventing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1794,10 +1794,6 @@ def incept(self, serder, estOnly=None):


self.prefixer = Prefixer(qb64=serder.pre)
#if not self.prefixer.verify(ked=ked, prefixed=True): # invalid prefix
#raise ValidationError("Invalid prefix = {} for inception evt = {}."
#"".format(self.prefixer.qb64, ked))
#serder._verify() redundant serder.__init__ defaults to verify
self.serder = serder # need whole serder for digest agility comparisons

ndigs = serder.ndigs # ked["n"]
Expand Down Expand Up @@ -1945,7 +1941,6 @@ def update(self, serder, sigers, wigers=None, delseqner=None, delsaider=None,

# nxt and signatures verify so update state
self.sner = sner # sequence number Number instance
#serder._verify() redundant serder.__init__ defaults to verify
self.serder = serder # need whole serder for digest agility compare
self.ilk = ilk
self.tholder = tholder
Expand Down Expand Up @@ -2006,7 +2001,6 @@ def update(self, serder, sigers, wigers=None, delseqner=None, delsaider=None,

# validates so update state
self.sner = sner # sequence number Number instance
#serder._verify() redundant serder.__init__ defaults to verify
self.serder = serder # need for digest agility includes .serder.diger
self.ilk = ilk
if fn is not None: # first is non-idempotent for fn check mode fn is None
Expand Down
33 changes: 31 additions & 2 deletions src/keri/core/serdering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,29 @@ class SerderKERI(Serder):
Proto = Protocols.keri # default protocol type


# can't do this override as is because would have to then redo the saidive field
# calculation. To do that would have to first extract the saidive calculations from
# both makify and verify into hidden method called ._saidify and then recall
# ._saidify in order to change defaults via an override

#def makify(self, **kwa):
#"""Makify given sad dict makes the versions
#Override for ilk and pre specific defaults
#"""
#super(SerderKERI, self).makify(**kwa) # all properties now setup

#if self.ilk in (Ilks.icp, Ilks.dip) and self.pre: # inceptive with pre
#try:
#code = Matter(qb64=self.pre).code
#except Exception as ex:
#raise ValidationError(f"Invalid identifier prefix = "
#f"{self.pre}.") from ex

#if code in PreNonDigDex:
#if not self.keys:
#self._sad['k'] = [self.pre] # default for non digestive prefix
#self._sad['kt'] = '1'


def _verify(self, **kwa):
"""Verifies said(s) in sad against raw
Expand Down Expand Up @@ -1610,8 +1633,14 @@ def _verify(self, **kwa):
if self.ilk in (Ilks.icp, Ilks.dip): # inceptive event
if code in PreNonDigDex:
if len(self.keys) != 1:
raise ValidationError(f"Invalid keys for non-digestive "
f"prefix {code=}.")
raise ValidationError(f"Invalid keys = {self.keys} "
"for non-digestive prefix "
f"{code=}.")

if self.tholder.sith != '1':
raise ValidationError(f"Invalid signing threshold ="
f" {self.tholder.sith} for "
f"non-digestive prefix {code=}.")

if self.pre != self.keys[0]:
raise ValidationError(f"Mismatch prefix = {self.pre} and"
Expand Down
1 change: 0 additions & 1 deletion src/keri/vdr/eventing.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,6 @@ def incept(self, serder):
raise ValidationError("Invalid toad = {} for baks = {} for evt = {}."
"".format(toad, baks, ked))
self.toad = toad
#serder._verify() redundant serder.__init__ defaults to verify
self.serder = serder

def config(self, serder, noBackers=None, estOnly=None):
Expand Down
Loading

0 comments on commit 3da4438

Please sign in to comment.