Add VerifiedKeyFrag.from_verified_bytes() for the purposes of Nucypher's datastore

pull/270/head
Bogdan Opanchuk 2021-06-01 23:55:48 -07:00
parent 2ad8223b68
commit f57c4ba5a4
3 changed files with 20 additions and 1 deletions

View File

@ -40,6 +40,7 @@ Intermediate objects
:show-inheritance:
.. autoclass:: VerifiedKeyFrag()
:members:
:special-members: __eq__, __hash__
.. autoclass:: CapsuleFrag()

View File

@ -1,7 +1,7 @@
import pytest
from umbral import KeyFrag, PublicKey, Signer, VerificationError
from umbral.key_frag import KeyFragID, KeyFragBase
from umbral.key_frag import KeyFragID, KeyFragBase, VerifiedKeyFrag
from umbral.curve_scalar import CurveScalar
@ -124,3 +124,9 @@ def test_kfrag_str(kfrags):
s = str(KeyFrag.from_bytes(bytes(kfrags[0])))
assert "VerifiedKeyFrag" not in s
assert "KeyFrag" in s
def test_from_verified_bytes(kfrags):
kfrag_bytes = bytes(kfrags[0])
verified_kfrag = VerifiedKeyFrag.from_verified_bytes(kfrag_bytes)
assert verified_kfrag == kfrags[0]

View File

@ -253,6 +253,18 @@ class VerifiedKeyFrag:
def __bytes__(self):
return bytes(self.kfrag)
@classmethod
def from_verified_bytes(cls, data) -> 'VerifiedKeyFrag':
"""
Restores a verified keyfrag directly from serialized bytes,
skipping :py:meth:`KeyFrag.verify` call.
Intended for internal storage;
make sure that the bytes come from a trusted source.
"""
kfrag = KeyFrag.from_bytes(data)
return cls(kfrag)
def __eq__(self, other):
return self.kfrag == other.kfrag