module Eth::Utils

Public Instance Methods

base256_to_int(str) click to toggle source
# File lib/eth/utils.rb, line 32
def base256_to_int(str)
  RLP::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, '')
end
bin_to_hex(string) click to toggle source
# File lib/eth/utils.rb, line 24
def bin_to_hex(string)
  RLP::Utils.encode_hex string
end
bin_to_prefixed_hex(binary) click to toggle source
# File lib/eth/utils.rb, line 56
def bin_to_prefixed_hex(binary)
  prefix_hex bin_to_hex(binary)
end
format_address(address) click to toggle source
# File lib/eth/utils.rb, line 110
def format_address(address)
  Address.new(address).checksummed
end
hash160(x) click to toggle source
# File lib/eth/utils.rb, line 86
def hash160(x)
  ripemd160 sha256(x)
end
hex_to_bin(string) click to toggle source
# File lib/eth/utils.rb, line 28
def hex_to_bin(string)
  RLP::Utils.decode_hex remove_hex_prefix(string)
end
int_to_base256(int) click to toggle source
# File lib/eth/utils.rb, line 36
def int_to_base256(int)
  RLP::Sedes.big_endian_int.serialize int
end
keccak256(x) click to toggle source
# File lib/eth/utils.rb, line 70
def keccak256(x)
  Digest::SHA3.new(256).digest(x)
end
keccak256_rlp(x) click to toggle source
# File lib/eth/utils.rb, line 78
def keccak256_rlp(x)
  keccak256 RLP.encode(x)
end
keccak512(x) click to toggle source
# File lib/eth/utils.rb, line 74
def keccak512(x)
  Digest::SHA3.new(512).digest(x)
end
normalize_address(address) click to toggle source
# File lib/eth/utils.rb, line 12
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
prefix_hex(hex) click to toggle source
# File lib/eth/utils.rb, line 48
def prefix_hex(hex)
  hex.match(/\A0x/) ? hex : "0x#{hex}"
end
prefix_message(message) click to toggle source
# File lib/eth/utils.rb, line 6
def prefix_message(message)
  prefix = "\u0019Ethereum Signed Message:\n#{message.length}"
  return "#{prefix}#{message}"
end
public_key_to_address(hex) click to toggle source
# File lib/eth/utils.rb, line 60
def public_key_to_address(hex)
  bytes = hex_to_bin(hex)
  address_bytes = Utils.keccak256(bytes[1..-1])[-20..-1]
  format_address bin_to_prefixed_hex(address_bytes)
end
remove_hex_prefix(s) click to toggle source
# File lib/eth/utils.rb, line 52
def remove_hex_prefix(s)
  s[0,2] == '0x' ? s[2..-1] : s
end
ripemd160(x) click to toggle source
# File lib/eth/utils.rb, line 82
def ripemd160(x)
  Digest::RMD160.digest x
end
sha256(x) click to toggle source
# File lib/eth/utils.rb, line 66
def sha256(x)
  Digest::SHA256.digest x
end
v_r_s_for(signature) click to toggle source
# File lib/eth/utils.rb, line 40
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
valid_address?(address) click to toggle source
# File lib/eth/utils.rb, line 106
def valid_address?(address)
  Address.new(address).valid?
end
zpad(x, l) click to toggle source
# File lib/eth/utils.rb, line 90
def zpad(x, l)
  lpad x, BYTE_ZERO, l
end
zpad_hex(s, l=32) click to toggle source
# File lib/eth/utils.rb, line 102
def zpad_hex(s, l=32)
  zpad decode_hex(s), l
end
zpad_int(n, l=32) click to toggle source
# File lib/eth/utils.rb, line 98
def zpad_int(n, l=32)
  zpad encode_int(n), l
end
zunpad(x) click to toggle source
# File lib/eth/utils.rb, line 94
def zunpad(x)
  x.sub(/\A\x00+/, '')
end

Private Instance Methods

encode_int(n) click to toggle source
# File lib/eth/utils.rb, line 123
def encode_int(n)
  unless n.is_a?(Integer) && n >= 0 && n <= UINT_MAX
    raise ArgumentError, "Integer invalid or out of range: #{n}"
  end

  int_to_base256 n
end
lpad(x, symbol, l) click to toggle source
# File lib/eth/utils.rb, line 118
def lpad(x, symbol, l)
  return x if x.size >= l
  symbol * (l - x.size) + x
end