Class: Klay::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/klay/address.rb

Overview

The Address class to handle checksummed Klaytn addresses.

Defined Under Namespace

Classes: CheckSumError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ Address

Constructor of the Klay::Address class. Creates a new hex prefixed address.

Parameters:

  • address (String)

    hex string representing an ethereum address.



31
32
33
34
35
36
37
38
39
# File 'lib/klay/address.rb', line 31

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

Instance Attribute Details

#addressObject (readonly)

The prefixed and checksummed Klaytn address.



25
26
27
# File 'lib/klay/address.rb', line 25

def address
  @address
end

Instance Method Details

#checksummedString Also known as: to_s

Generate a checksummed address.

Returns:

  • (String)

    prefixed hexstring representing an checksummed address.

Raises:



57
58
59
60
61
62
63
64
65
# File 'lib/klay/address.rb', line 57

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

#valid?Boolean

Checks that the address is valid.

Returns:

  • (Boolean)

    true if valid address.



44
45
46
47
48
49
50
51
52
# File 'lib/klay/address.rb', line 44

def valid?
  if !matches_any_format?
    false
  elsif not_checksummed?
    true
  else
    checksum_matches?
  end
end