Module: Ethlite::Helpers

Included in:
Utils
Defined in:
lib/ethlite/utils.rb

Instance Method Summary collapse

Instance Method Details

#decode_hex(hex) ⇒ Object Also known as: hex_to_bin

hex_to_bin

Raises:

  • (TypeError)


18
19
20
21
22
23
24
25
26
# File 'lib/ethlite/utils.rb', line 18

def decode_hex( hex )   ## hex_to_bin
  raise TypeError, "Value must be a string" unless hex.is_a?( String )
  raise TypeError, 'Non-hexadecimal char found' unless hex.empty? || hex =~ /\A(0x)?[0-9a-fA-F]{2,}\z/

  ## allow optional starting 0x - why? why not?
  hex = hex[2..-1]   if hex[0,2] == '0x'

  [hex].pack("H*")
end

#encode_hex(bin) ⇒ Object Also known as: bin_to_hex

bin_to_hex

Raises:

  • (TypeError)


11
12
13
14
15
# File 'lib/ethlite/utils.rb', line 11

def encode_hex( bin )   ## bin_to_hex
  raise TypeError, "Value must be a string" unless bin.is_a?( String )
  ## note: always return a hex string with default encoding e.g. utf-8 - why? why not?
  bin.unpack("H*").first.force_encoding( Encoding::UTF_8 )
end

#keccak256(bin) ⇒ Object



5
6
7
8
9
# File 'lib/ethlite/utils.rb', line 5

def keccak256( bin )
  # Digest::SHA3.new(256).digest(x)
  ## Digest::Keccak.digest(x, 256)
  Digest::KeccakLite.new(256).digest( bin )
end