Module: Eth::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/eth/utils.rb

Instance Method Summary collapse

Instance Method Details

#base256_to_int(string) ⇒ Object



31
32
33
34
35
36
# File 'lib/eth/utils.rb', line 31

def base256_to_int(string)
  string.bytes.inject do |result, byte|
    result *= 256
    result + byte
  end
end

#bin_to_hex(string) ⇒ Object



23
24
25
# File 'lib/eth/utils.rb', line 23

def bin_to_hex(string)
  string.unpack("H*")[0]
end

#hex_to_bin(string) ⇒ Object



27
28
29
# File 'lib/eth/utils.rb', line 27

def hex_to_bin(string)
  [string].pack("H*")
end

#int_to_base256(int) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/eth/utils.rb', line 38

def int_to_base256(int)
  bytes = []
  while int > 0 do
    bytes.unshift(int % 256)
    int /= 256
  end
  bytes.pack('C*')
end

#keccak256(message) ⇒ Object



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

def keccak256(message)
  Digest::SHA3.new(256).digest(message)
end

#normalize_address(address) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/eth/utils.rb', line 11

def normalize_address(address)
  if address.nil? || address == ''
    ''
  elsif address.size == 40
    hex_to_bin address
  elsif address.size == 42 && address[0..1] == '0x'
    hex_to_bin address[2..-1]
  else
    address
  end
end

#v_r_s_for(signature) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/eth/utils.rb', line 47

def v_r_s_for(signature)
  [
    signature[0].bytes[0],
    Utils.base256_to_int(signature[1..32]),
    Utils.base256_to_int(signature[33..65]),
  ]
end