Module: Ethereum::Utils

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

Instance Method Summary collapse

Instance Method Details

#base256_to_int(string) ⇒ Object



30
31
32
33
34
35
# File 'lib/ethereum/utils.rb', line 30

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

#bin_to_hex(string) ⇒ Object



22
23
24
# File 'lib/ethereum/utils.rb', line 22

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

#hex_to_bin(string) ⇒ Object



26
27
28
# File 'lib/ethereum/utils.rb', line 26

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

#int_to_base256(int) ⇒ Object



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

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

#keccak256(message) ⇒ Object



6
7
8
# File 'lib/ethereum/utils.rb', line 6

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

#normalize_address(address) ⇒ Object



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

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



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

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