auto.py 781 B

12345678910111213141516171819202122232425262728
  1. from typing import (
  2. Union,
  3. )
  4. from eth_hash.abc import (
  5. BackendAPI,
  6. PreImageAPI,
  7. )
  8. from eth_hash.utils import (
  9. auto_choose_backend,
  10. )
  11. class AutoBackend(BackendAPI):
  12. def _initialize(self) -> None:
  13. backend = auto_choose_backend()
  14. # Use setattr to circumvent mypy's confusion, see:
  15. # https://github.com/python/mypy/issues/2427
  16. setattr(self, "keccak256", backend.keccak256) # noqa: B010
  17. setattr(self, "preimage", backend.preimage) # noqa: B010
  18. def keccak256(self, in_data: Union[bytearray, bytes]) -> bytes:
  19. self._initialize()
  20. return self.keccak256(in_data)
  21. def preimage(self, in_data: Union[bytearray, bytes]) -> PreImageAPI:
  22. self._initialize()
  23. return self.preimage(in_data)