Skip to content

Commit

Permalink
added new Structor class Sealer class some refactoring
Browse files Browse the repository at this point in the history
Merge branch 'development'
  • Loading branch information
SmithSamuelM committed Apr 8, 2024
2 parents 4abd0db + 505af32 commit 0bcba8f
Show file tree
Hide file tree
Showing 14 changed files with 1,430 additions and 265 deletions.
14 changes: 8 additions & 6 deletions src/keri/app/habbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ def openHby(*, name="test", base="", temp=True, salt=None, **kwa):
all secrets are re-encrypted using new aeid. In this case the
provided prikey must not be empty. A change in aeid should require
a second authentication mechanism besides the prikey.
bran (str): Base64 22 char string that is used as base material for
seed. bran allows alphanumeric passcodes generated by key managers
like 1password to be key store for seed.
bran (str): Base64 char string of which first 21 chars are used as
base material for seed. bran allows alphanumeric passcodes
generated by key managers like 1password to be key store for
bran as salt base material for seed.
pidx (int): Initial prefix index for vacuous ks
algo (str): algorithm (randy or salty) for creating key pairs
default is root algo which defaults to salty
Expand Down Expand Up @@ -184,9 +185,10 @@ def __init__(self, *, name='test', base="", temp=False,
all secrets Haberyare re-encrypted using new aeid. In this case the
provided prikey must not be empty. A change in aeid should require
a second authentication mechanism besides the prikey.
bran (str): Base64 22 char string that is used as base material for
seed. bran allows alphanumeric passcodes generated by key managers
like 1password to be key store for seed.
bran (str): Base64 char string of which first 21 chars are used as
base material for seed. bran allows alphanumeric passcodes
generated by key managers like 1password to be key store for
bran as salt base material for seed.
pidx (int): Initial prefix index for vacuous ks
algo (str): algorithm (randy or salty) for creating key pairs
default is root algo which defaults to salty
Expand Down
2 changes: 1 addition & 1 deletion src/keri/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#__all__ = ["coring", "eventing", "parsing", "scheming"]

# Matter class and its subclasses
from .coring import (Matter, MtrDex, Number, NumDex, Dater,Texter,
from .coring import (Matter, MtrDex, Number, NumDex, Dater, Texter,
Bexter, Pather, Verfer, Cigar, Signer, Salter,
Cipher, Encrypter, Decrypter, Diger, DigDex,
Prefixer, PreDex, )
Expand Down
106 changes: 90 additions & 16 deletions src/keri/core/coring.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,70 @@ def randomNonce():
Tiers = Tierage(low='low', med='med', high='high')



@dataclass
class MapDom:
"""Base class for dataclasses that support map syntax
Adds support for dunder methods for map syntax dc[name].
Converts exceptions from attribute syntax to raise map syntax when using
map syntax.
Enables dataclass instances to use Mapping item syntax
"""

def __getitem__(self, name):
try:
return getattr(self, name)
except AttributeError as ex:
raise IndexError(ex.args) from ex


def __setitem__(self, name, value):
try:
return setattr(self, name, value)
except AttributeError as ex:
raise IndexError(ex.args) from ex


def __delitem__(self, name):
try:
return delattr(self, name)
except AttributeError as ex:
raise IndexError(ex.args) from ex


@dataclass(frozen=True)
class MapCodex:
"""Base class for frozen dataclasses (codexes) that support map syntax
Adds support for dunder methods for map syntax dc[name].
Converts exceptions from attribute syntax to raise map syntax when using
map syntax.
Enables dataclass instances to use Mapping item syntax
"""

def __getitem__(self, name):
try:
return getattr(self, name)
except AttributeError as ex:
raise IndexError(ex.args) from ex


def __setitem__(self, name, value):
try:
return setattr(self, name, value)
except AttributeError as ex:
raise IndexError(ex.args) from ex


def __delitem__(self, name):
try:
return delattr(self, name)
except AttributeError as ex:
raise IndexError(ex.args) from ex



@dataclass(frozen=True)
class MatterCodex:
"""
Expand Down Expand Up @@ -335,12 +399,12 @@ class MatterCodex:
X25519_Cipher_QB64_Big_L0: str = '7AAD' # X25519 sealed box cipher bytes of QB64 plaintext big lead size 0
X25519_Cipher_QB64_Big_L1: str = '8AAD' # X25519 sealed box cipher bytes of QB64 plaintext big lead size 1
X25519_Cipher_QB64_Big_L2: str = '9AAD' # X25519 sealed box cipher bytes of QB64 plaintext big lead size 2
X25519_Cipher_QB2_L0: str = '4D' # X25519 sealed box cipher bytes of QB2 plaintext lead size 0
X25519_Cipher_QB2_L1: str = '5D' # X25519 sealed box cipher bytes of QB2 plaintext lead size 1
X25519_Cipher_QB2_L2: str = '6D' # X25519 sealed box cipher bytes of QB2 plaintext lead size 2
X25519_Cipher_QB2_Big_L0: str = '7AAD' # X25519 sealed box cipher bytes of QB2 plaintext big lead size 0
X25519_Cipher_QB2_Big_L1: str = '8AAD' # X25519 sealed box cipher bytes of QB2 plaintext big lead size 1
X25519_Cipher_QB2_Big_L2: str = '9AAD' # X25519 sealed box cipher bytes of QB2 plaintext big lead size 2
X25519_Cipher_QB2_L0: str = '4E' # X25519 sealed box cipher bytes of QB2 plaintext lead size 0
X25519_Cipher_QB2_L1: str = '5E' # X25519 sealed box cipher bytes of QB2 plaintext lead size 1
X25519_Cipher_QB2_L2: str = '6E' # X25519 sealed box cipher bytes of QB2 plaintext lead size 2
X25519_Cipher_QB2_Big_L0: str = '7AAE' # X25519 sealed box cipher bytes of QB2 plaintext big lead size 0
X25519_Cipher_QB2_Big_L1: str = '8AAE' # X25519 sealed box cipher bytes of QB2 plaintext big lead size 1
X25519_Cipher_QB2_Big_L2: str = '9AAE' # X25519 sealed box cipher bytes of QB2 plaintext big lead size 2


def __iter__(self):
Expand Down Expand Up @@ -1336,19 +1400,23 @@ def _binfil(self):

def _exfil(self, qb64b):
"""
Extracts self.code and self.raw from qualified base64 str or bytes qb64b
Detects is str and converts to bytes
Extracts self.code and self.raw from qualified base64 qb64b of type
str or bytes or bytearray or memoryview
Detects if str and converts to bytes
Parameters:
qb64b (str | bytes | bytearray): fully qualified base64 from stream
qb64b (str | bytes | bytearray | memoryview): fully qualified base64 from stream
"""
if not qb64b: # empty need more bytes
raise ShortageError("Empty material.")

first = qb64b[:1] # extract first char code selector
if isinstance(first, memoryview):
first = bytes(first)
if hasattr(first, "decode"):
first = first.decode("utf-8")
first = first.decode() # converts bytes/bytearray to str
if first not in self.Hards:
if first[0] == '-':
raise UnexpectedCountCodeError("Unexpected count code start"
Expand All @@ -1364,8 +1432,10 @@ def _exfil(self, qb64b):
raise ShortageError(f"Need {hs - len(qb64b)} more characters.")

hard = qb64b[:hs] # extract hard code
if isinstance(hard, memoryview):
hard = bytes(hard)
if hasattr(hard, "decode"):
hard = hard.decode("utf-8") # converts bytes/bytearray to str
hard = hard.decode() # converts bytes/bytearray to str
if hard not in self.Sizes:
raise UnexpectedCodeError(f"Unsupported code ={hard}.")

Expand All @@ -1376,8 +1446,10 @@ def _exfil(self, qb64b):
# when fs is None then ss > 0 otherwise fs > hs + ss when ss > 0

soft = qb64b[hs:hs + ss] # extract soft chars, empty when ss==0
if isinstance(soft, memoryview):
soft = bytes(soft)
if hasattr(soft, "decode"):
soft = soft.decode("utf-8")
soft = soft.decode() # converts bytes/bytearray to str

if not fs: # compute fs from soft from ss part which provides size B64
# compute variable size as int may have value 0
Expand All @@ -1387,8 +1459,10 @@ def _exfil(self, qb64b):
raise ShortageError(f"Need {fs - len(qb64b)} more chars.")

qb64b = qb64b[:fs] # fully qualified primitive code plus material
if isinstance(qb64b, memoryview):
qb64b = bytes(qb64b)
if hasattr(qb64b, "encode"): # only convert extracted chars from stream
qb64b = qb64b.encode("utf-8")
qb64b = qb64b.encode() # converts str to bytes

# check for non-zeroed pad bits and/or lead bytes
# net prepad ps == cs % 4 (remainer). Assumes ps != 3 i.e ps in (0,1,2)
Expand All @@ -1407,8 +1481,8 @@ def _exfil(self, qb64b):
if len(raw) != ((len(qb64b) - cs) * 3 // 4) - ls: # exact lengths
raise ConversionError(f"Improperly qualified material = {qb64b}")

self._code = hard # hard only
self._soft = soft # soft only
self._code = hard # hard only str
self._soft = soft # soft only str
self._raw = raw # ensure bytes for crypto ops, may be empty


Expand All @@ -1417,7 +1491,7 @@ def _bexfil(self, qb2):
Extracts self.code and self.raw from qualified base2 qb2
Parameters:
qb2 (bytes | bytearray): fully qualified base2 from stream
qb2 (bytes | bytearray | memoryview): fully qualified base2 from stream
"""
if not qb2: # empty need more bytes
raise ShortageError("Empty material, Need more bytes.")
Expand Down
79 changes: 13 additions & 66 deletions src/keri/core/counting.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .. import kering
from ..kering import (Versionage, Vrsn_1_0, Vrsn_2_0)

from ..core.coring import Sizage
from ..core.coring import Sizage, MapCodex



Expand All @@ -42,63 +42,6 @@ def __iter__(self):



@dataclass
class MapDom:
"""Base class for dataclasses that support map syntax
Adds support for dunder methods for map syntax dc[name].
Converts exceptions from attribute syntax to raise map syntax when using
map syntax.
"""

def __getitem__(self, name):
try:
return getattr(self, name)
except AttributeError as ex:
raise IndexError(ex.args) from ex


def __setitem__(self, name, value):
try:
return setattr(self, name, value)
except AttributeError as ex:
raise IndexError(ex.args) from ex


def __delitem__(self, name):
try:
return delattr(self, name)
except AttributeError as ex:
raise IndexError(ex.args) from ex


@dataclass(frozen=True)
class MapCodex:
"""Base class for frozen dataclasses (codexes) that support map syntax
Adds support for dunder methods for map syntax dc[name].
Converts exceptions from attribute syntax to raise map syntax when using
map syntax.
"""

def __getitem__(self, name):
try:
return getattr(self, name)
except AttributeError as ex:
raise IndexError(ex.args) from ex


def __setitem__(self, name, value):
try:
return setattr(self, name, value)
except AttributeError as ex:
raise IndexError(ex.args) from ex


def __delitem__(self, name):
try:
return delattr(self, name)
except AttributeError as ex:
raise IndexError(ex.args) from ex



@dataclass(frozen=True)
Expand All @@ -107,6 +50,9 @@ class CounterCodex_1_0(MapCodex):
CounterCodex is codex hard (stable) part of all counter derivation codes.
Only provide defined codes.
Undefined are left out so that inclusion(exclusion) via 'in' operator works.
As subclass of MapCodex can get codes with item syntax using tag variables.
Example: codex[tag]
"""
ControllerIdxSigs: str = '-A' # Qualified Base64 Indexed Signature.
WitnessIdxSigs: str = '-B' # Qualified Base64 Indexed Signature.
Expand Down Expand Up @@ -136,6 +82,9 @@ class CounterCodex_2_0(MapCodex):
CounterCodex is codex hard (stable) part of all counter derivation codes.
Only provide defined codes.
Undefined are left out so that inclusion(exclusion) via 'in' operator works.
As subclass of MapCodex can get codes with item syntax using tag variables.
Example: codex[tag]
"""
GenericGroup: str = '-A' # Generic Group (Universal with Override).
BigGenericGroup: str = '-0A' # Big Generic Group (Universal with Override).
Expand Down Expand Up @@ -165,18 +114,18 @@ class CounterCodex_2_0(MapCodex):
BigTransReceiptQuadruples: str = '-0M' # Big Trans Receipt Quadruple(s), pre+snu+dig+sig.
FirstSeenReplayCouples: str = '-N' # First Seen Replay Couple(s), fnu+dts.
BigFirstSeenReplayCouples: str = '-0N' # First Seen Replay Couple(s), fnu+dts.
TransIdxSigGroups: str = '-O' # Trans Indexed Signature Group(s), pre+snu+dig+ControllerIdxSigs of qb64.
TransIdxSigGroups: str = '-0O' # Big Trans Indexed Signature Group(s), pre+snu+dig+ControllerIdxSigs of qb64.
TransLastIdxSigGroups: str = '-P' # Trans Last Est Evt Indexed Signature Group(s), pre+ControllerIdxSigs of qb64.
BigTransLastIdxSigGroups: str = '-0P' # Big Trans Last Est Evt Indexed Signature Group(s), pre+ControllerIdxSigs of qb64.
TransIdxSigGroups: str = '-O' # Trans Indexed Signature Group(s), pre+snu+dig+CtrControllerIdxSigs of qb64.
TransIdxSigGroups: str = '-0O' # Big Trans Indexed Signature Group(s), pre+snu+dig+CtrControllerIdxSigs of qb64.
TransLastIdxSigGroups: str = '-P' # Trans Last Est Evt Indexed Signature Group(s), pre+CtrControllerIdxSigs of qb64.
BigTransLastIdxSigGroups: str = '-0P' # Big Trans Last Est Evt Indexed Signature Group(s), pre+CtrControllerIdxSigs of qb64.
SealSourceCouples: str = '-Q' # Seal Source Couple(s), snu+dig of source sealing or sealed event.
BigSealSourceCouples: str = '-0Q' # Seal Source Couple(s), snu+dig of source sealing or sealed event.
SealSourceTriples: str = '-R' # Seal Source Triple(s), pre+snu+dig of source sealing or sealed event.
BigSealSourceTriples: str = '-0R' # Seal Source Triple(s), pre+snu+dig of source sealing or sealed event.
PathedMaterialGroup: str = '-S' # Pathed Material Group.
BigPathedMaterialGroup: str = '-0S' # Big Pathed Material Group.
SadPathSigGroups: str = '-T' # SAD Path Group(s) sadpath+TransIdxSigGroup(s) of SAID qb64 of content.
BigSadPathSigGroups: str = '-0T' # Big SAD Path Group(s) sadpath+TransIdxSigGroup(s) of SAID qb64 of content.
SadPathSigGroups: str = '-T' # SAD Path Group(s) sadpath+CtrTransIdxSigGroup(s) of SAID qb64 of content.
BigSadPathSigGroups: str = '-0T' # Big SAD Path Group(s) sadpath+CtrTransIdxSigGroup(s) of SAID qb64 of content.
RootSadPathSigGroups: str = '-U' # Root Path SAD Path Group(s), rootpath+SadPathGroup(s).
BigRootSadPathSigGroups: str = '-0U' # Big Root Path SAD Path Group(s), rootpath+SadPathGroup(s).
DigestSealSingles: str = '-V' # Digest Seal Single(s), dig of sealed data.
Expand All @@ -189,8 +138,6 @@ class CounterCodex_2_0(MapCodex):
BigESSRPayloadGroup: str = '-0Z' # Big ESSR Payload Group.
KERIACDCGenusVersion: str = '--AAA' # KERI ACDC Stack CESR Protocol Genus Version (Universal)



def __iter__(self):
return iter(astuple(self)) # enables value not key inclusion test with "in"

Expand Down
18 changes: 10 additions & 8 deletions src/keri/core/eventing.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@
# used to indicate to get the latest keys available from KEL for 'i'
SealLast = namedtuple("SealLast", 'i')

# State Establishment Event (latest current) : quadruple (s, d, br, ba)
# s = sn of latest est event as lowercase hex string no leading zeros,
# d = SAID digest qb64 of latest establishment event
# br = backer (witness) remove list (cuts) from latest est event
# ba = backer (witness) add list (adds) from latest est event
StateEstEvent = namedtuple("StateEstEvent", 's d br ba')

# Transaction Event Seal for Transaction Event: duple (s, d)
# s = sn of transaction event as lowercase hex string no leading zeros,
# d = SAID digest qb64 of transaction event
Expand All @@ -102,7 +95,16 @@
# use SealSourceCouples count code for attachment
SealTrans = namedtuple("SealTrans", 's d')

# not used should this be depricated
# Following are not seals only used in database

# State Establishment Event (latest current) : quadruple (s, d, br, ba)
# s = sn of latest est event as lowercase hex string no leading zeros,
# d = SAID digest qb64 of latest establishment event
# br = backer (witness) remove list (cuts) from latest est event
# ba = backer (witness) add list (adds) from latest est event
StateEstEvent = namedtuple("StateEstEvent", 's d br ba')

# not used should this be depricated?
# State Event (latest current) : triple (s, t, d)
# s = sn of latest event as lowercase hex string no leading zeros,
# t = message type of latest event (ilk)
Expand Down
7 changes: 4 additions & 3 deletions src/keri/core/serdering.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from .. import kering
from ..kering import (ValidationError, MissingFieldError, ExtraFieldError,
AlternateFieldError,
AlternateFieldError, InvalidValueError,
ShortageError, VersionError, ProtocolError, KindError,
DeserializeError, FieldError, SerializeError)
from ..kering import (Versionage, Version, Vrsn_1_0, Vrsn_2_0,
Expand Down Expand Up @@ -642,7 +642,8 @@ def __init__(self, *, raw=b'', sad=None, strip=False, smellage=None,
f"{self._sad}.") from ex

else:
raise ValueError("Improper initialization need raw or sad or makify.")
raise InvalidValueError("Improper initialization need raw or sad "
f"or makify.")



Expand Down Expand Up @@ -840,7 +841,7 @@ def makify(self, sad, *, proto=None, vrsn=None, kind=None,
if sad and 'v' in sad: # attempt to get from vs in sad
try: # extract version string elements as defaults if provided
sproto, svrsn, skind, _, _ = deversify(sad["v"])
except ValueError as ex:
except VersionError as ex:
pass
else:
silk = sad.get('t') # if 't' not in sad .get returns None which may be valid
Expand Down
Loading

0 comments on commit 0bcba8f

Please sign in to comment.