class Eth::Address

The {Eth::Address} class to handle checksummed Ethereum addresses.

def all_lowercase?

Checks whether the address is all lower-case.
def all_lowercase?
  address.match /(?:0[xX])[a-f0-9]{40}/
end

def all_uppercase?

Checks whether the address is all upper-case.
def all_uppercase?
  address.match /(?:0[xX])[A-F0-9]{40}/
end

def checksum

Computes the checksum of the address.
def checksum
  Util.bin_to_hex Util.keccak256 unprefixed.downcase
end

def checksum_matches?

Checks whether the address checksum matches.
def checksum_matches?
  address == checksummed
end

def checksummed

Returns:
  • (String) - prefixed hexstring representing an checksummed address.
def checksummed
  raise CheckSumError, "Invalid address: #{address}" unless matches_any_format?
  cased = unprefixed.chars.zip(checksum.chars).map do |char, check|
    check.match(/[0-7]/) ? char.downcase : char.upcase
  end
  Util.prefix_hex cased.join
end

def initialize(address)

Parameters:
  • address (String) -- hex string representing an ethereum address.
def initialize(address)
  unless Util.is_hex? address
    raise CheckSumError, "Unknown address type #{address}!"
  end
  @address = Util.prefix_hex address
  unless self.valid?
    raise CheckSumError, "Invalid address provided #{address}"
  end
end

def matches_any_format?

Checks whether the address matches any known format.
def matches_any_format?
  address.match /\A(?:0[xX])[a-fA-F0-9]{40}\z/
end

def not_checksummed?

Checks whether the address is not checksummed.
def not_checksummed?
  all_uppercase? || all_lowercase?
end

def unprefixed

Removes the hex prefix.
def unprefixed
  Util.remove_hex_prefix address
end

def valid?

Returns:
  • (Boolean) - true if valid address.
def valid?
  if !matches_any_format?
    false
  elsif not_checksummed?
    true
  else
    checksum_matches?
  end
end