def serialize(self, obj): if not self.is_valid_type(obj): raise SerializationError( 'Object is not a serializable ({})'.format(type(obj)), obj) if not self.is_valid_length(len(obj)): raise SerializationError('Object has invalid length', obj) return obj.encode(self.encoding)
def serialize(self, obj: Union[str, int]) -> bytes: ''' Serialize the object into a RLP encode-able "item". Parameters ---------- obj : Union[str, int] obj is either number in string or number int. ''' if not (_is_pure_str(obj) or _is_pure_int(obj)): raise SerializationError("The input is not str nor int.", obj) number = None if _is_pure_str(obj): if _is_hex_string(obj): number = int(obj, 16) if _is_decimal_string(obj): number = int(obj) if _is_pure_int(obj): number = obj # remove leading 0 from bytes sequence. result_bytes = super().serialize(number) byte_list = [x for x in result_bytes if x != 0] return bytes(byte_list)
def serialize(obj): try: cls = Transaction.exclude(['metadata']) if obj.metadata is None else Transaction field_value = [getattr(obj, field) for field, _ in cls.fields] ret = cls.get_sedes().serialize(field_value) return ret except Exception as e: raise SerializationError(e.format_exc, obj)
def serialize(self, obj: str) -> bytes: # 0x counts for 2 chars. 1 bytes = 2 hex char. allowed_hex_length = self.byte_length * 2 + 2 if len(obj) != allowed_hex_length: raise SerializationError( "Max allowed string length {}".format(allowed_hex_length), obj) return super().serialize(obj)
def serialize(self, obj): if not isinstance(obj, bool): raise SerializationError('Can only serialize bool', obj) if obj is False: return b'\x00' elif obj is True: return b'\x01' else: raise Exception("Invariant: no other options for boolean values")
def serialize(self, obj): if isinstance(obj, bool) or not isinstance(obj, int): raise SerializationError('Can only serialize integers', obj) if self.l is not None and obj >= 256**self.l: raise SerializationError( 'Integer too large (does not fit in {} ' 'bytes)'.format(self.l), obj) if obj < 0: raise SerializationError('Cannot serialize negative integers', obj) if obj == 0: s = b'' else: s = int_to_big_endian(obj) if self.l is not None: return b'\x00' * max(0, self.l - len(s)) + s else: return s
def serialize(self, obj: str) -> bytes: ''' Serialize a '0x...' string to bytes. Parameters ---------- obj : str '0x...' style string. Returns ------- bytes the "item" that can be rlp encodeded. ''' if not _is_hex_string(obj): raise SerializationError('expect 0x... style string', obj) if len(obj) % 2 != 0: raise SerializationError( 'expect 0x... style string of even length.', obj) obj2 = obj[2:] # remove '0x' return bytes.fromhex(obj2)
def serialize(self, obj: bytes) -> bytes: ''' Serialize the object into a RLP encode-able "item". Parameters ---------- obj : bytes The input. Returns ------- bytes The "item" in bytes. Raises ------ SerializationError raise if input is not bytes. ''' if not self.is_valid_type(obj): raise SerializationError( 'type of "obj" param is not right, bytes required.', obj) return obj
def serialize(obj): if not serializable(obj): raise SerializationError('Can only serialize nested lists of strings', obj) return obj