Class: Klay::Address
- Inherits:
-
Object
- Object
- Klay::Address
- Defined in:
- lib/klay/address.rb
Overview
The Address class to handle checksummed Klaytn addresses.
Defined Under Namespace
Classes: CheckSumError
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
The prefixed and checksummed Klaytn address.
Instance Method Summary collapse
-
#checksummed ⇒ String
(also: #to_s)
Generate a checksummed address.
-
#initialize(address) ⇒ Address
constructor
Constructor of the Address class.
-
#valid? ⇒ Boolean
Checks that the address is valid.
Constructor Details
#initialize(address) ⇒ Address
Constructor of the Klay::Address class. Creates a new hex prefixed 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
#address ⇒ Object (readonly)
The prefixed and checksummed Klaytn address.
25 26 27 |
# File 'lib/klay/address.rb', line 25 def address @address end |
Instance Method Details
#checksummed ⇒ String Also known as: to_s
Generate a checksummed address.
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.
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 |