Skip to content

Commit

Permalink
Merge pull request #689 from SmithSamuelM/development
Browse files Browse the repository at this point in the history
Refactor remove hard coded field label tests now superseded by Serder.verify which does versioned field label tests
  • Loading branch information
SmithSamuelM authored Feb 24, 2024
2 parents 3e838f8 + 522821b commit 4e33296
Show file tree
Hide file tree
Showing 9 changed files with 554 additions and 450 deletions.
153 changes: 84 additions & 69 deletions src/keri/core/coring.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,19 @@
from ..kering import (Versionage, Version, VERRAWSIZE, VERFMT, VERFULLSIZE,
versify, deversify, Rever, smell)
from ..kering import Serials, Serialage, Protos, Protocolage, Ilkage, Ilks
from ..kering import (ICP_LABELS, DIP_LABELS, ROT_LABELS, DRT_LABELS, IXN_LABELS,
RPY_LABELS)
from ..kering import (VCP_LABELS, VRT_LABELS, ISS_LABELS, BIS_LABELS, REV_LABELS,
BRV_LABELS, TSN_LABELS, CRED_TSN_LABELS)


from ..help import helping
from ..help.helping import sceil, nonStringIterable, nonStringSequence


Labels = Ilkage(icp=ICP_LABELS, rot=ROT_LABELS, ixn=IXN_LABELS, dip=DIP_LABELS,
drt=DRT_LABELS, rct=[], qry=[], rpy=RPY_LABELS,
exn=[], pro=[], bar=[],
vcp=VCP_LABELS, vrt=VRT_LABELS, iss=ISS_LABELS, rev=REV_LABELS,
bis=BIS_LABELS, brv=BRV_LABELS)


DSS_SIG_MODE = "fips-186-3"
ECDSA_256r1_SEEDBYTES = 32
ECDSA_256k1_SEEDBYTES = 32


Vstrings = Serialage(json=versify(kind=Serials.json, size=0),
mgpk=versify(kind=Serials.mgpk, size=0),
cbor=versify(kind=Serials.cbor, size=0))

# SAID field labels
Saidage = namedtuple("Saidage", "dollar at id_ i d")

Expand Down Expand Up @@ -304,7 +292,8 @@ def loads(raw, size=None, kind=Serials.json):

return ked


# deprecated don't use anymore need to fix demo tests that use
# use with context instead
def generateSigners(salt=None, count=8, transferable=True):
"""
Returns list of Signers for Ed25519
Expand Down Expand Up @@ -1861,6 +1850,75 @@ def datetime(self):
return helping.fromIso8601(self.dts)


class Streamer:
"""
Streamer is CESR sniffable stream class
Has the following public properties:
Properties:
Methods:
Hidden:
"""

def __init__(self, stream):
"""Initialize instance
Parameters:
stream (bytes | bytearray): sniffable CESR stream
"""
self._stream = bytes(stream)


@property
def stream(self):
"""stream property getter
"""
return self._stream

@property
def text(self):
"""expanded stream as qb64 text
Returns:
stream (bytes): expanded text qb64 version of stream
"""
return self._stream

@property
def binary(self):
"""compacted stream as qb2 binary
Returns:
stream (bytes): compacted binary qb2 version of stream
"""
return self._stream

@property
def texter(self):
"""expanded stream as Texter instance
Returns:
texter (Texter): Texter primitive of stream suitable wrapping
"""
return self._stream






class Texter(Matter):
"""
Texter is subclass of Matter, cryptographic material, for variable length
Expand Down Expand Up @@ -2764,51 +2822,6 @@ def _secp256k1(ser, seed, verfer, index, only=False, ondex=None, **kwa):
ondex=ondex,
verfer=verfer,)

# def derive_index_code(code, index, only=False, ondex=None, **kwa):
# # should add Indexer class method to get ms main index size for given code
# if only: # only main index ondex not used
# ondex = None
# if index <= 63: # (64 ** ms - 1) where ms is main index size, use small current only
# if code == MtrDex.Ed25519_Seed:
# indxSigCode = IdrDex.Ed25519_Crt_Sig
# elif code == MtrDex.ECDSA_256r1_Seed:
# indxSigCode = IdrDex.ECDSA_256r1_Crt_Sig
# elif code == MtrDex.ECDSA_256k1_Seed:
# indxSigCode = IdrDex.ECDSA_256k1_Crt_Sig
# else:
# raise ValueError("Unsupported signer code = {}.".format(code))
# else: # use big current only
# if code == MtrDex.Ed25519_Seed:
# indxSigCode = IdrDex.Ed25519_Big_Crt_Sig
# elif code == MtrDex.ECDSA_256r1_Seed:
# indxSigCode = IdrDex.ECDSA_256r1_Big_Crt_Sig
# elif code == MtrDex.ECDSA_256k1_Seed:
# indxSigCode = IdrDex.ECDSA_256k1_Big_Crt_Sig
# else:
# raise ValueError("Unsupported signer code = {}.".format(code))
# else: # both
# if ondex == None:
# ondex = index # enable default to be same
# if ondex == index and index <= 63: # both same and small so use small both same
# if code == MtrDex.Ed25519_Seed:
# indxSigCode = IdrDex.Ed25519_Sig
# elif code == MtrDex.ECDSA_256r1_Seed:
# indxSigCode = IdrDex.ECDSA_256r1_Sig
# elif code == MtrDex.ECDSA_256k1_Seed:
# indxSigCode = IdrDex.ECDSA_256k1_Sig
# else:
# raise ValueError("Unsupported signer code = {}.".format(code))
# else: # otherwise big or both not same so use big both
# if code == MtrDex.Ed25519_Seed:
# indxSigCode = IdrDex.Ed25519_Big_Sig
# elif code == MtrDex.ECDSA_256r1_Seed:
# indxSigCode = IdrDex.ECDSA_256r1_Big_Sig
# elif code == MtrDex.ECDSA_256k1_Seed:
# indxSigCode = IdrDex.ECDSA_256k1_Big_Sig
# else:
# raise ValueError("Unsupported signer code = {}.".format(code))

# return (indxSigCode, ondex)

class Salter(Matter):
"""
Expand Down Expand Up @@ -3542,11 +3555,12 @@ def derive(self, ked):
if ilk not in (Ilks.icp, Ilks.dip, Ilks.vcp, Ilks.iss):
raise ValueError("Nonincepting ilk={} for prefix derivation.".format(ilk))

labels = getattr(Labels, ilk)
for k in labels:
if k not in ked:
raise ValidationError("Missing element = {} from {} event for "
"evt = {}.".format(k, ilk, ked))
# Serder now does this check
#labels = getattr(Labels, ilk)
#for k in labels:
#if k not in ked:
#raise ValidationError("Missing element = {} from {} event for "
#"evt = {}.".format(k, ilk, ked))

return (self._derive(ked=ked))

Expand All @@ -3563,11 +3577,12 @@ def verify(self, ked, prefixed=False):
if ilk not in (Ilks.icp, Ilks.dip, Ilks.vcp, Ilks.iss):
raise ValueError("Nonincepting ilk={} for prefix derivation.".format(ilk))

labels = getattr(Labels, ilk)
for k in labels:
if k not in ked:
raise ValidationError("Missing element = {} from {} event for "
"evt = {}.".format(k, ilk, ked))
# Serder now does this check
#labels = getattr(Labels, ilk)
#for k in labels:
#if k not in ked:
#raise ValidationError("Missing element = {} from {} event for "
#"evt = {}.".format(k, ilk, ked))

return (self._verify(ked=ked, pre=self.qb64, prefixed=prefixed))

Expand Down
10 changes: 4 additions & 6 deletions src/keri/core/eventing.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
QueryNotFoundError, MisfitEventSourceError,
MissingDelegableApprovalError)
from ..kering import Version, Versionage
from ..kering import (ICP_LABELS, DIP_LABELS, ROT_LABELS, DRT_LABELS, IXN_LABELS,
RPY_LABELS)

from ..help import helping

Expand Down Expand Up @@ -2071,10 +2069,10 @@ def update(self, serder, sigers, wigers=None, delseqner=None, delsaider=None,
raise ValidationError("Unexpected non-establishment event = {}."
"".format(serder.ked))

for k in IXN_LABELS:
if k not in ked:
raise ValidationError("Missing element = {} from {} event."
" evt = {}.".format(k, Ilks.ixn, ked))
#for k in IXN_LABELS:
#if k not in ked:
#raise ValidationError("Missing element = {} from {} event."
#" evt = {}.".format(k, Ilks.ixn, ked))

if not sner.num == (self.sner.num + 1): # sn not in order
raise ValidationError("Invalid sn = {} expecting = {} for evt "
Expand Down
5 changes: 4 additions & 1 deletion src/keri/core/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def __init__(self, ims=None, framed=True, pipeline=False, kvy=None,
self.local = True if local else False



@staticmethod
def extract(ims, klas, cold=Colds.txt):
"""
Expand All @@ -95,6 +94,7 @@ def extract(ims, klas, cold=Colds.txt):
else:
raise kering.ColdStartError("Invalid stream state cold={}.".format(cold))


@staticmethod
def _extractor(ims, klas, cold=Colds.txt, abort=False):
"""
Expand Down Expand Up @@ -122,6 +122,7 @@ def _extractor(ims, klas, cold=Colds.txt, abort=False):
raise # bad pipelined frame so abort by raising error
yield


def _sadPathSigGroup(self, ctr, ims, root=None, cold=Colds.txt, pipelined=False):
"""
Expand Down Expand Up @@ -174,6 +175,7 @@ def _sadPathSigGroup(self, ctr, ims, root=None, cold=Colds.txt, pipelined=False)
"count code={}.Expected code={}."
"".format(ctr.code, CtrDex.ControllerIdxSigs))


def _transIdxSigGroups(self, ctr, ims, cold=Colds.txt, pipelined=False):
"""
Extract attaced trans indexed sig groups each made of
Expand Down Expand Up @@ -228,6 +230,7 @@ def _transIdxSigGroups(self, ctr, ims, cold=Colds.txt, pipelined=False):

yield prefixer, seqner, saider, isigers


def _nonTransReceiptCouples(self, ctr, ims, cold=Colds.txt, pipelined=False):
"""
Extract attached rct couplets into list of sigvers
Expand Down
8 changes: 4 additions & 4 deletions src/keri/core/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ def processReply(self, serder, cigars=None, tsgs=None):
Escrow process logic is route dependent and is dispatched by route,
i.e. route is address of buffer with route specific handler of escrow.
"""
for k in eventing.RPY_LABELS:
if k not in serder.ked:
raise kering.ValidationError(f"Missing element={k} from {coring.Ilks.rpy}"
f" msg={serder.ked}.")
#for k in eventing.RPY_LABELS:
#if k not in serder.ked:
#raise kering.ValidationError(f"Missing element={k} from {coring.Ilks.rpy}"
#f" msg={serder.ked}.")
# fetch from serder to process
ked = serder.ked

Expand Down
18 changes: 0 additions & 18 deletions src/keri/core/serdering.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,24 +761,6 @@ def _inhale(clas, raw, version=Version, smellage=None):
else: # not passed in so smell raw
proto, vrsn, kind, size = smell(raw, version=version)


#proto = proto.decode("utf-8")
#if proto not in Protos:
#raise ProtocolError(f"Invalid protocol type = {proto}.")

#vrsn = Versionage(major=int(major, 16), minor=int(minor, 16))
#if version is not None and vrsn != version:
#raise VersionError(f"Expected version = {version}, got "
#f"{vrsn.major}.{vrsn.minor}.")

#kind = kind.decode("utf-8")
#if kind not in Serials:
#raise KindError(f"Invalid serialization kind = {kind}.")

#size = int(size, 16)
#if len(raw) < size:
#raise ShortageError(f"Need more bytes.")

sad = clas.loads(raw=raw, size=size, kind=kind)

if "v" not in sad: # Regex does not check for version string label itself
Expand Down
50 changes: 20 additions & 30 deletions src/keri/kering.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,22 @@
VERFMT = "{}{:x}{:x}{}{:0{}x}_" # version format string
VERFULLSIZE = 17 # number of characters in full version string

VEREX = b'(?P<proto>[A-Z]{4})(?P<major>[0-9a-f])(?P<minor>[0-9a-f])(?P<kind>[A-Z]{4})(?P<size>[0-9a-f]{6})_'
Rever = re.compile(VEREX) # compile is faster
VEREX0 = b'(?P<proto>[A-Z]{4})(?P<major>[0-9a-f])(?P<minor>[0-9a-f])(?P<kind>[A-Z]{4})(?P<size>[0-9a-f]{6})_'

Rever = re.compile(VEREX0) # compile is faster


VER1FULLSPAN = 17 # number of characters in full version string
VER1TERM = b'_'
VEREX1 = b'(?P<proto1>[A-Z]{4})(?P<major1>[0-9a-f])(?P<minor1>[0-9a-f])(?P<kind1>[A-Z]{4})(?P<size1>[0-9a-f]{6})_'

VER2FULLSPAN = 16 # number of characters in full version string
VER2TERM = b'.'
VEREX2 = b'(?P<proto2>[A-Z]{4})(?P<major2>[0-9A-Za-z_-])(?P<minor2>[0-9A-Za-z_-]{2})(?P<kind2>[A-Z]{4})(?P<size2>[0-9A-Za-z_-]{4}).'

VEREX = VEREX2 + b'|' + VEREX1

pattern = re.compile(VEREX) # compile is faster


def versify(proto=Protos.keri, version=Version, kind=Serials.json, size=0):
Expand Down Expand Up @@ -69,13 +83,17 @@ def deversify(vs, version=None):
serialization kind
serialization size
"""
# length of matched string is sum of lengths of returned group elements
# span

match = Rever.match(vs.encode("utf-8")) # match takes bytes
if match:
proto, major, minor, kind, size = match.group("proto",
"major",
"minor",
"kind",
"size")

proto = proto.decode("utf-8")
vrsn = Versionage(major=int(major, 16), minor=int(minor, 16))
kind = kind.decode("utf-8")
Expand Down Expand Up @@ -292,34 +310,6 @@ def sniff(ims):
watcher='watcher', judge='judge', juror='juror', peer='peer', mailbox="mailbox", agent="agent")


ICP_LABELS = ["v", "t", "d", "i", "s", "kt", "k", "nt", "n",
"bt", "b", "c", "a"]
DIP_LABELS = ["v", "d", "i", "s", "t", "kt", "k", "nt", "n",
"bt", "b", "c", "a", "di"]
ROT_LABELS = ["v", "d", "i", "s", "t", "p", "kt", "k", "nt", "n",
"bt", "br", "ba", "a"]
DRT_LABELS = ["v", "d", "i", "s", "t", "p", "kt", "k", "nt", "n",
"bt", "br", "ba", "a"]
IXN_LABELS = ["v", "d", "i", "s", "t", "p", "a"]

#KSN_LABELS = ["v", "d", "i", "s", "p", "d", "f", "dt", "et", "kt", "k", "nt", "n",
#"bt", "b", "c", "ee", "di"]

RPY_LABELS = ["v", "d", "t", "d", "dt", "r", "a"]

VCP_LABELS = ["v", "d", "i", "s", "t", "bt", "b", "c"]
VRT_LABELS = ["v", "d", "i", "s", "t", "p", "bt", "b", "ba", "br"]

ISS_LABELS = ["v", "i", "s", "t", "ri", "dt"]
BIS_LABELS = ["v", "i", "s", "t", "ra", "dt"]

REV_LABELS = ["v", "i", "s", "t", "p", "dt"]
BRV_LABELS = ["v", "i", "s", "t", "ra", "p", "dt"]

TSN_LABELS = ["v", "i", "s", "d", "ii", "a", "et", "bt", "b", "c", "br", "ba"]
CRED_TSN_LABELS = ["v", "i", "s", "d", "ri", "a", "ra"]



# Exception Subclasses
class KeriError(Exception):
Expand Down
Loading

0 comments on commit 4e33296

Please sign in to comment.