Class: Rlp::Sedes::Binary

Inherits:
Object
  • Object
show all
Defined in:
lib/rlp-lite/sedes/binary.rb

Overview

A sedes type for binary values.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min_length: 0, max_length: INFINITY, allow_empty: false) ⇒ Binary

Create a serializable binary of variable size.

Parameters:

  • min_length (Integer) (defaults to: 0)

    the minimum size of the binary.

  • max_length (Integer) (defaults to: INFINITY)

    the maximum size of the binary.

  • allow_empty (Boolean) (defaults to: false)

    indicator wether empty binaries should be allowed.



32
33
34
35
36
# File 'lib/rlp-lite/sedes/binary.rb', line 32

def initialize( min_length: 0, max_length: INFINITY, allow_empty: false )
  @min_length = min_length
  @max_length = max_length
  @allow_empty = allow_empty
end

Class Method Details

.fixed_length(l, allow_empty: false) ⇒ Eth::Rlp::Sedes::Binary

Create a serializable binary of fixed size.

Parameters:

  • l (Integer)

    the fixed size of the binary.

  • allow_empty (Boolean) (defaults to: false)

    indicator wether empty binaries should be allowed.

Returns:

  • (Eth::Rlp::Sedes::Binary)

    a serializable binary of fixed size.



12
13
14
15
16
# File 'lib/rlp-lite/sedes/binary.rb', line 12

def self.fixed_length( l, allow_empty: false )
  new( min_length: l,
       max_length: l,
       allow_empty: allow_empty )
end

.valid_type?(obj) ⇒ Boolean

Checks wether the given object is of a valid binary type.

Parameters:

  • obj (Object)

    the supposed binary item to check.

Returns:

  • (Boolean)

    true if valid.



22
23
24
# File 'lib/rlp-lite/sedes/binary.rb', line 22

def self.valid_type?( obj )
  obj.instance_of?( String )
end

Instance Method Details

#deserialize(serial) ⇒ String

Deserializes a binary.

Parameters:

  • serial (Object)

    the serialized binary.

Returns:

  • (String)

    a deserialized binary.

Raises:



61
62
63
64
65
# File 'lib/rlp-lite/sedes/binary.rb', line 61

def deserialize(serial)
  raise DeserializationError, "Objects of type #{serial.class} cannot be deserialized" unless serial.instance_of?(String)
  raise DeserializationError, "#{serial.class} has invalid length" unless valid_length?( serial.size )
  serial
end

#serialize(obj) ⇒ Object

Serializes a binary.

Parameters:

  • obj (String)

    the binary to serialize.

Returns:

  • (Object)

    a serialized binary.

Raises:



44
45
46
47
48
49
50
51
52
# File 'lib/rlp-lite/sedes/binary.rb', line 44

def serialize( obj )
  raise SerializationError, "Object is not a serializable (#{obj.class})" unless self.class.valid_type?( obj )

  ##  make sure string is with binary encoding (ASCII-8BIT)
  ##    note: was Util.str_to_bytes( obj )
  serial =  obj.encoding.name == 'ASCII-8BIT' ? obj : obj.b
  raise SerializationError, "Object has invalid length" unless valid_length?( serial.size )
  serial
end

#valid_length?(length) ⇒ Boolean

Checks wether the given length fits the defined size boundaries of the binary type.

Parameters:

  • length (Integer)

    the supposed length of the binary item.

Returns:

  • (Boolean)

    true if valid.



72
73
74
75
# File 'lib/rlp-lite/sedes/binary.rb', line 72

def valid_length?( length )
  (@min_length <= length && length <= @max_length) ||
  (@allow_empty && length == 0)
end