rainbowadn/rainbowadn/hashing/hashresolver.py
2022-05-08 21:56:17 +03:00

30 lines
1.1 KiB
Python

from typing import TypeVar
from rainbowadn.hashing.hashmentionable import HashMentionable
from rainbowadn.hashing.hashpoint import HashPoint
from rainbowadn.hashing.nullability.notnull import NotNull
from rainbowadn.hashing.nullability.null import Null
__all__ = ('HashResolver',)
RHashMentioned = TypeVar('RHashMentioned')
class HashResolver:
def _resolve(self, point: bytes) -> bytes:
raise NotImplementedError
def resolve(self, hashpoint: HashPoint[RHashMentioned]) -> RHashMentioned:
assert isinstance(hashpoint, HashPoint)
if isinstance(hashpoint.value, NotNull):
return hashpoint.value.value
elif isinstance(hashpoint.value, Null):
resolved: bytes = self._resolve(bytes(hashpoint))
assert isinstance(resolved, bytes)
mentioned: RHashMentioned = hashpoint.factory.from_bytes(resolved[HashPoint.HASH_LENGTH:])
assert isinstance(mentioned, HashMentionable)
assert mentioned.__topology_hash__() == resolved[:HashPoint.HASH_LENGTH]
return mentioned
else:
raise TypeError