class Eth::Rlp::Sedes::Binary

A sedes type for binary values.

def deserialize(serial)

Raises:
  • (DeserializationError) - if provided serial is of wrong length.
  • (DeserializationError) - if provided serial is of wrong type.

Returns:
  • (String) - a deserialized binary.

Parameters:
  • serial (Object) -- the serialized binary.
def deserialize(serial)
  raise DeserializationError, "Objects of type #{serial.class} cannot be deserialized" unless Util.primitive? serial
  raise DeserializationError, "#{serial.class} has invalid length" unless valid_length? serial.size
  serial
end

def fixed_length(l, allow_empty: false)

Returns:
  • (Eth::Rlp::Sedes::Binary) - a serializable binary of fixed size.

Parameters:
  • allow_empty (Boolean) -- indicator wether empty binaries should be allowed.
  • l (Integer) -- the fixed size of the binary.
def fixed_length(l, allow_empty: false)
  new(min_length: l, max_length: l, allow_empty: allow_empty)
end

def initialize(min_length: 0, max_length: Constant::INFINITY, allow_empty: false)

Parameters:
  • allow_empty (Boolean) -- indicator wether empty binaries should be allowed.
  • max_length (Integer) -- the maximum size of the binary.
  • min_length (Integer) -- the minimum size of the binary.
def initialize(min_length: 0, max_length: Constant::INFINITY, allow_empty: false)
  @min_length = min_length
  @max_length = max_length
  @allow_empty = allow_empty
end

def serialize(obj)

Raises:
  • (SerializationError) - if provided binary is of invalid length.
  • (SerializationError) - if provided object is of invalid type.

Returns:
  • (Object) - a serialized binary.

Parameters:
  • obj (String) -- the binary to serialize.
def serialize(obj)
  raise SerializationError, "Object is not a serializable (#{obj.class})" unless self.class.valid_type? obj
  serial = Util.str_to_bytes obj
  raise SerializationError, "Object has invalid length" unless valid_length? serial.size
  serial
end

def valid_length?(length)

Returns:
  • (Boolean) - true if valid.

Parameters:
  • length (Integer) -- the supposed length of the binary item.
def valid_length?(length)
  (@min_length <= length && length <= @max_length) || (@allow_empty && length == 0)
end

def valid_type?(obj)

Returns:
  • (Boolean) - true if valid.

Parameters:
  • obj (Object) -- the supposed binary item to check.
def valid_type?(obj)
  obj.instance_of? String
end