Skip to content

Commit

Permalink
Merge pull request #482 from SmithSamuelM/dev
Browse files Browse the repository at this point in the history
finished refactor of ident to proto to make clear its the protocol ty…
  • Loading branch information
SmithSamuelM committed Apr 25, 2023
2 parents e134785 + baea566 commit 9840659
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 66 deletions.
77 changes: 42 additions & 35 deletions src/keri/core/coring.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,75 +87,81 @@
VERFULLSIZE = 17 # number of characters in full versions string


def versify(ident=Protos.keri, version=None, kind=Serials.json, size=0):
def versify(proto=Protos.keri, version=None, kind=Serials.json, size=0):
"""
Return version string
Returns version string
"""
if ident not in Protos:
raise ValueError("Invalid message identifier = {}".format(ident))
if proto not in Protos:
raise ValueError("Invalid message identifier = {}".format(proto))
if kind not in Serials:
raise ValueError("Invalid serialization kind = {}".format(kind))
version = version if version else Version
return VERFMT.format(ident, version[0], version[1], kind, size, VERRAWSIZE)
return VERFMT.format(proto, version[0], version[1], kind, size, VERRAWSIZE)


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

VEREX = b'(?P<ident>[A-Z]{4})(?P<major>[0-9a-f])(?P<minor>[0-9a-f])(?P<kind>[A-Z]{4})(?P<size>[0-9a-f]{6})_'
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
MINSNIFFSIZE = 12 + VERFULLSIZE # min bytes in buffer to sniff else need more


def deversify(vs):
"""
Returns tuple(ident, kind, version, size)
Where:
proto is protocol type identifier one of Protos (Protocolage)
Returns: tuple(proto, kind, version, size) Where:
proto (str): value is protocol type identifier one of Protos (Protocolage)
acdc='ACDC', keri='KERI'
kind is serialization kind, one of Serials
kind (str): value is serialization kind, one of Serials
json='JSON', mgpk='MGPK', cbor='CBOR'
version is version tuple of type Version
size is int of raw size
version (tuple): is version tuple of type Version
size (int): raw size in bytes
Parameters:
vs is version string str
vs (str): version string
Uses regex match to extract:
event type identifier
protocol type
serialization kind
keri version
serialization size
"""
match = Rever.match(vs.encode("utf-8")) # match takes bytes
if match:
ident, major, minor, kind, size = match.group("ident", "major", "minor", "kind", "size")
proto, major, minor, kind, size = match.group("proto", "major", "minor", "kind", "size")
version = Versionage(major=int(major, 16), minor=int(minor, 16))
ident = ident.decode("utf-8")
proto = proto.decode("utf-8")
kind = kind.decode("utf-8")

if ident not in Protos:
raise ValueError("Invalid message identifier = {}".format(ident))
if proto not in Protos:
raise ValueError("Invalid message identifier = {}".format(proto))
if kind not in Serials:
raise ValueError("Invalid serialization kind = {}".format(kind))
size = int(size, 16)
return ident, kind, version, size
return proto, kind, version, size

raise ValueError("Invalid version string = {}".format(vs))


def sizeify(ked, kind=None):
"""
ked is key event dict
kind is serialization if given else use one given in ked
Compute serialized size of ked and update version field
Returns tuple of associated values extracted and or changed by sizeify
Returns tuple of (raw, proto, kind, ked, version) where:
raw (str): serialized event as bytes of kind
proto (str): protocol type as value of Protocolage
kind (str): serialzation kind as value of Serialage
ked (dict): key event dict
version (Versionage): instance
Parameters:
ked (dict): key event dict
kind (str): value of Serials is serialization type
if not provided use that given in ked["v"]
Assumes only supports Version
"""
if "v" not in ked:
Expand All @@ -182,7 +188,7 @@ def sizeify(ked, kind=None):

fore, back = match.span() # full version string
# update vs with latest kind version size
vs = versify(ident=proto, version=version, kind=kind, size=size)
vs = versify(proto=proto, version=version, kind=kind, size=size)
# replace old version string in raw with new one
raw = b'%b%b%b' % (raw[:fore], vs.encode("utf-8"), raw[back:])
if size != len(raw): # substitution messed up
Expand Down Expand Up @@ -324,15 +330,15 @@ def sniff(raw):
if not match or match.start() > 12:
raise VersionError("Invalid version string in raw = {}".format(raw))

ident, major, minor, kind, size = match.group("ident", "major", "minor", "kind", "size")
proto, major, minor, kind, size = match.group("proto", "major", "minor", "kind", "size")
version = Versionage(major=int(major, 16), minor=int(minor, 16))
kind = kind.decode("utf-8")
ident = ident.decode("utf-8")
proto = proto.decode("utf-8")
if kind not in Serials:
raise DeserializationError("Invalid serialization kind = {}".format(kind))
size = int(size, 16)

return ident, kind, version, size
return proto, kind, version, size


def dumps(ked, kind=Serials.json):
Expand Down Expand Up @@ -3151,7 +3157,8 @@ def _derive_blake3_256(self, ked):
# put in dummy pre to get size correct
ked["i"] = self.Dummy * Matter.Sizes[MtrDex.Blake3_256].fs
ked["d"] = ked["i"] # must be same dummy
raw, ident, kind, ked, version = sizeify(ked=ked)
#raw, proto, kind, ked, version = sizeify(ked=ked)
raw, _, _, _, _ = sizeify(ked=ked)
dig = blake3.blake3(raw).digest() # digest with dummy 'i' and 'd'
return (dig, MtrDex.Blake3_256) # dig is derived correct new 'i' and 'd'

Expand Down Expand Up @@ -3372,7 +3379,7 @@ def _derive(clas, sad: dict, *,
# fill id field denoted by label with dummy chars to get size correct
sad[label] = clas.Dummy * Matter.Sizes[code].fs
if 'v' in sad: # if versioned then need to set size in version string
raw, ident, kind, sad, version = sizeify(ked=sad, kind=kind)
raw, proto, kind, sad, version = sizeify(ked=sad, kind=kind)

ser = dict(sad)
if ignore:
Expand Down Expand Up @@ -4732,7 +4739,7 @@ def _inhale(self, raw):
loads and jumps of json use str whereas cbor and msgpack use bytes
"""
ident, kind, version, size = sniff(raw)
proto, kind, version, size = sniff(raw)
if version != Version:
raise VersionError("Unsupported version = {}.{}, expected {}."
"".format(version.major, version.minor, Version))
Expand All @@ -4741,7 +4748,7 @@ def _inhale(self, raw):

ked = loads(raw=raw, size=size, kind=kind)

return ked, ident, kind, version, size
return ked, proto, kind, version, size


def _exhale(self, ked, kind=None):
Expand Down Expand Up @@ -4790,10 +4797,10 @@ def raw(self):
@raw.setter
def raw(self, raw):
""" raw property setter """
ked, ident, kind, version, size = self._inhale(raw=raw)
ked, proto, kind, version, size = self._inhale(raw=raw)
self._raw = bytes(raw[:size]) # crypto ops require bytes not bytearray
self._ked = ked
self._proto = ident
self._proto = proto
self._kind = kind
self._version = version
self._size = size
Expand All @@ -4807,11 +4814,11 @@ def ked(self):
@ked.setter
def ked(self, ked):
""" ked property setter assumes ._kind """
raw, ident, kind, ked, version = self._exhale(ked=ked, kind=self._kind)
raw, proto, kind, ked, version = self._exhale(ked=ked, kind=self._kind)
size = len(raw)
self._raw = raw[:size]
self._ked = ked
self._proto = ident
self._proto = proto
self._kind = kind
self._size = size
self._version = version
Expand All @@ -4825,10 +4832,10 @@ def kind(self):
@kind.setter
def kind(self, kind):
""" kind property setter Assumes ._ked. Serialization kind. """
raw, ident, kind, ked, version = self._exhale(ked=self._ked, kind=kind)
raw, proto, kind, ked, version = self._exhale(ked=self._ked, kind=kind)
size = len(raw)
self._raw = raw[:size]
self._proto = ident
self._proto = proto
self._ked = ked
self._kind = kind
self._size = size
Expand Down
2 changes: 1 addition & 1 deletion src/keri/core/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ def msgParsator(self, ims=None, framed=True, pipeline=False,
"= {}.".format(creder.pretty()))

else:
raise kering.ValidationError("Unexpected message ident = {} for evt ="
raise kering.ValidationError("Unexpected protocol type = {} for event message ="
" {}.".format(sadder.proto, sadder.pretty()))

return True # done state
2 changes: 1 addition & 1 deletion src/keri/vc/proving.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def credential(schema,
Creder: credential instance
"""
vs = versify(ident=coring.Protos.acdc, version=version, kind=kind, size=0)
vs = versify(proto=coring.Protos.acdc, version=version, kind=kind, size=0)

vc = dict(
v=vs,
Expand Down
52 changes: 26 additions & 26 deletions tests/core/test_coring.py
Original file line number Diff line number Diff line change
Expand Up @@ -5113,62 +5113,62 @@ def test_versify():
vs = versify(kind=Serials.json, size=0)
assert vs == "KERI10JSON000000_"
assert len(vs) == VERFULLSIZE
ident, kind, version, size = deversify(vs)
assert ident == Protos.keri
proto, kind, version, size = deversify(vs)
assert proto == Protos.keri
assert kind == Serials.json
assert version == Version
assert size == 0

vs = versify(kind=Serials.json, size=65)
assert vs == "KERI10JSON000041_"
assert len(vs) == VERFULLSIZE
ident, kind, version, size = deversify(vs)
assert ident == Protos.keri
proto, kind, version, size = deversify(vs)
assert proto == Protos.keri
assert kind == Serials.json
assert version == Version
assert size == 65

vs = versify(ident=Protos.acdc, kind=Serials.json, size=86)
vs = versify(proto=Protos.acdc, kind=Serials.json, size=86)
assert vs == "ACDC10JSON000056_"
assert len(vs) == VERFULLSIZE
ident, kind, version, size = deversify(vs)
assert ident == Protos.acdc
proto, kind, version, size = deversify(vs)
assert proto == Protos.acdc
assert kind == Serials.json
assert version == Version
assert size == 86

vs = versify(kind=Serials.mgpk, size=0)
assert vs == "KERI10MGPK000000_"
assert len(vs) == VERFULLSIZE
ident, kind, version, size = deversify(vs)
assert ident == Protos.keri
proto, kind, version, size = deversify(vs)
assert proto == Protos.keri
assert kind == Serials.mgpk
assert version == Version
assert size == 0

vs = versify(kind=Serials.mgpk, size=65)
assert vs == "KERI10MGPK000041_"
assert len(vs) == VERFULLSIZE
ident, kind, version, size = deversify(vs)
assert ident == Protos.keri
proto, kind, version, size = deversify(vs)
assert proto == Protos.keri
assert kind == Serials.mgpk
assert version == Version
assert size == 65

vs = versify(kind=Serials.cbor, size=0)
assert vs == "KERI10CBOR000000_"
assert len(vs) == VERFULLSIZE
ident, kind, version, size = deversify(vs)
assert ident == Protos.keri
proto, kind, version, size = deversify(vs)
assert proto == Protos.keri
assert kind == Serials.cbor
assert version == Version
assert size == 0

vs = versify(kind=Serials.cbor, size=65)
assert vs == "KERI10CBOR000041_"
assert len(vs) == VERFULLSIZE
ident, kind, version, size = deversify(vs)
assert ident == Protos.keri
proto, kind, version, size = deversify(vs)
assert proto == Protos.keri
assert kind == Serials.cbor
assert version == Version
assert size == 65
Expand Down Expand Up @@ -5221,15 +5221,15 @@ def test_serder():

e1s = json.dumps(e1, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
with pytest.raises(ShortageError): # test too short
ident1, kind1, vers1, size1 = sniff(e1s[:VERFULLSIZE])
protos1, kind1, vers1, size1 = sniff(e1s[:VERFULLSIZE])

ident1, kind1, vers1, size1 = sniff(e1s[:MINSNIFFSIZE])
assert ident1 == Protos.keri
protos1, kind1, vers1, size1 = sniff(e1s[:MINSNIFFSIZE])
assert protos1 == Protos.keri
assert kind1 == Serials.json
assert size1 == 111

ident1, kind1, vers1, size1 = sniff(e1s)
assert ident1 == Protos.keri
protos1, kind1, vers1, size1 = sniff(e1s)
assert protos1 == Protos.keri
assert kind1 == Serials.json
assert size1 == 111
e1ss = e1s + b'extra attached at the end.'
Expand Down Expand Up @@ -5270,7 +5270,7 @@ def test_serder():
assert size2 == 92

ident2, kind2, vers2, size2 = sniff(e2s)
assert ident1 == Protos.keri
assert ident2 == Protos.keri
assert kind2 == Serials.mgpk
assert size2 == 92
e2ss = e2s + b'extra attached at the end.'
Expand Down Expand Up @@ -5306,7 +5306,7 @@ def test_serder():
ident3, kind3, vers3, size3 = sniff(e3s[:VERFULLSIZE])

ident3, kind3, vers3, size3 = sniff(e3s[:MINSNIFFSIZE])
assert ident1 == Protos.keri
assert ident3 == Protos.keri
assert kind3 == Serials.cbor
assert size3 == 92

Expand All @@ -5332,7 +5332,7 @@ def test_serder():
assert ked3 == e3
assert vrs3 == vers3

e4 = dict(v=versify(ident=Protos.acdc, kind=Serials.json, size=0),
e4 = dict(v=versify(proto=Protos.acdc, kind=Serials.json, size=0),
d="",
i="ABCDEFG",
s="0001",
Expand All @@ -5343,7 +5343,7 @@ def test_serder():
e4s = json.dumps(e4, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
assert e4s == (b'{"v":"ACDC10JSON00006f_","d":"EMFw6MEBmwWU28-7wK4SJ2kasSzVgLKkAM7iwoqJJ07Z",'
b'"i":"ABCDEFG","s":"0001","t":"rot"}')
vs = versify(ident=Protos.acdc, kind=Serials.json, size=len(e4s)) # use real length
vs = versify(proto=Protos.acdc, kind=Serials.json, size=len(e4s)) # use real length
assert vs == 'ACDC10JSON00006f_'
e4["v"] = vs # has real length
serder = Sadder(ked=e4)
Expand Down Expand Up @@ -5476,8 +5476,8 @@ def test_serder():
assert evt2.kind == Serials.cbor
evt2.kind = Serials.json
assert evt2.kind == Serials.json
ident, knd, version, size = deversify(evt2.ked["v"])
assert ident == Protos.keri
proto, knd, version, size = deversify(evt2.ked["v"])
assert proto == Protos.keri
assert knd == Serials.json

# Test diger code
Expand Down
6 changes: 3 additions & 3 deletions tests/vc/test_proving.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_credentialer():
Creder()
sub = dict(a=123, b="abc", issuanceDate="2021-06-27T21:26:21.233257+00:00")
d = dict(
v=coring.versify(ident=coring.Protos.acdc, kind=Serials.json, size=0),
v=coring.versify(proto=coring.Protos.acdc, kind=Serials.json, size=0),
d="",
s="abc",
i="i",
Expand Down Expand Up @@ -142,7 +142,7 @@ def test_credentialer():
assert creder.size == 168

d2 = dict(d)
d2["v"] = coring.versify(ident=coring.Protos.acdc, kind=Serials.cbor, size=0)
d2["v"] = coring.versify(proto=coring.Protos.acdc, kind=Serials.cbor, size=0)
creder = Creder(ked=d2)
assert creder.said == said # shouldnt this be different here?
assert creder.issuer == "i"
Expand All @@ -165,7 +165,7 @@ def test_credentialer():
assert creder.crd == d2

d3 = dict(d)
d3["v"] = coring.versify(ident=coring.Protos.acdc, kind=Serials.mgpk, size=0)
d3["v"] = coring.versify(proto=coring.Protos.acdc, kind=Serials.mgpk, size=0)
creder = Creder(ked=d3)

assert creder.said == said # shouldn't this be different here
Expand Down

0 comments on commit 9840659

Please sign in to comment.