boolean.py 781 B

1234567891011121314151617181920212223242526272829303132
  1. from rlp.exceptions import (
  2. DeserializationError,
  3. SerializationError,
  4. )
  5. class Boolean:
  6. """A sedes for booleans"""
  7. def serialize(self, obj):
  8. if not isinstance(obj, bool):
  9. raise SerializationError("Can only serialize integers", obj)
  10. if obj is False:
  11. return b""
  12. elif obj is True:
  13. return b"\x01"
  14. else:
  15. raise Exception("Invariant: no other options for boolean values")
  16. def deserialize(self, serial):
  17. if serial == b"":
  18. return False
  19. elif serial == b"\x01":
  20. return True
  21. else:
  22. raise DeserializationError(
  23. "Invalid serialized boolean. Must be either 0x01 or 0x00", serial
  24. )
  25. boolean = Boolean()