Class: Etherlite::Types::ByteString

Inherits:
Base
  • Object
show all
Defined in:
lib/etherlite/types/byte_string.rb

Direct Known Subclasses

String

Instance Method Summary collapse

Methods inherited from Base

#dynamic?, #fixed?, #size

Instance Method Details

#decode(_connection, _value) ⇒ Object



17
18
19
20
21
# File 'lib/etherlite/types/byte_string.rb', line 17

def decode(_connection, _value)
  size = Etherlite::Utils.hex_to_uint _value[0...64]

  [_value[64...64 + size * 2]].pack('H*')
end

#encode(_value) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
# File 'lib/etherlite/types/byte_string.rb', line 7

def encode(_value)
  raise ArgumentError, "invalid argument type for 'bytes'" unless _value.is_a? ::String

  bytes_as_hex = _value.unpack('H*').first
  bytes = bytes_as_hex.length / 2
  padded_size = (bytes.to_f / 32).ceil * 32

  Etherlite::Utils.uint_to_hex(bytes) + bytes_as_hex.ljust(padded_size * 2, '0')
end

#signatureObject



3
4
5
# File 'lib/etherlite/types/byte_string.rb', line 3

def signature
  "bytes"
end