Module: RLP::Utils

Instance Method Summary collapse

Instance Method Details

#big_endian_to_int(v) ⇒ Object



19
20
21
# File 'lib/rlp/utils.rb', line 19

def big_endian_to_int(v)
  v.unpack('H*').first.to_i(16)
end

#bytes_to_str(v) ⇒ Object



11
12
13
# File 'lib/rlp/utils.rb', line 11

def bytes_to_str(v)
  v.unpack('U*').pack('U*')
end

#encode_hex(b) ⇒ Object

Raises:

  • (TypeError)


29
30
31
32
33
# File 'lib/rlp/utils.rb', line 29

def encode_hex(b)
  raise TypeError, "Value must be an instance of String" unless b.instance_of?(String)
  b = str_to_bytes(b) unless b.encoding.name == 'ASCII-8BIT'
  b.unpack("H*").first
end

#int_to_big_endian(v) ⇒ Object



23
24
25
26
27
# File 'lib/rlp/utils.rb', line 23

def int_to_big_endian(v)
  hex = v.to_s(16)
  hex = "0#{hex}" if hex.size.odd?
  [hex].pack('H*')
end

#list?(item) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/rlp/utils.rb', line 7

def list?(item)
  item.respond_to?(:each)
end

#primitive?(item) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/rlp/utils.rb', line 3

def primitive?(item)
  item.instance_of?(String)
end

#str_to_bytes(v) ⇒ Object



15
16
17
# File 'lib/rlp/utils.rb', line 15

def str_to_bytes(v)
  v.dup.force_encoding('ascii-8bit')
end