Sha256: fcced0607e15efb773b722b60ea58a200fa5b1571dea6549e481a31ae7f0a7d2

Contents?: true

Size: 657 Bytes

Versions: 24

Compression:

Stored size: 657 Bytes

Contents

module Etherlite::Utils
  class ValidateAddress < PowerTypes::Command.new(:address)
    MATCHER = /^0x[0-9a-fA-F]{40}$/

    def perform
      return false unless MATCHER === @address
      return false if /[A-F]/ === @address && !valid_checksum?
      true
    end

    private

    def valid_checksum?
      trimmed_address = @address.gsub(/^0x/, '')
      address_hash = Etherlite::Utils.sha3 trimmed_address.downcase

      trimmed_address.chars.each_with_index do |c, i|
        hash_byte = address_hash[i].to_i(16)
        return false if (hash_byte > 7 && c.upcase != c) || (hash_byte <= 7 && c.downcase != c)
      end

      true
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
etherlite-0.6.0 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.5.3 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.5.2 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.5.1 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.5.0 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.4.2 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.4.1 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.4.0 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.3.0 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.2.6 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.2.5 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.2.4 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.2.3 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.2.2 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.2.1 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.2.0 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.1.8 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.1.7 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.1.6 lib/etherlite/commands/utils/validate_address.rb
etherlite-0.1.5 lib/etherlite/commands/utils/validate_address.rb