Sha256: 70634fa5a195058946b85b61694bdd1db82b1e5c8ec0f034dfb0b9b53d72a6c3

Contents?: true

Size: 1.48 KB

Versions: 18

Compression:

Stored size: 1.48 KB

Contents

require "etherlite/commands/utils/validate_address"

module Etherlite
  module Utils
    extend self

    def sha3(_data)
      Digest::SHA3.hexdigest(_data, 256)
    end

    def uint_to_hex(_value, bytes: 32)
      _value.to_s(16).rjust(bytes * 2, '0')
    end

    def int_to_hex(_value, bytes: 32)
      if _value < 0
        # 2's complement for negative values
        (_value & ((1 << bytes * 8) - 1)).to_s(16)
      else
        uint_to_hex(_value, bytes: bytes)
      end
    end

    def hex_to_uint(_hex_value)
      _hex_value.hex
    end

    def hex_to_int(_hex_value, bytes: 32)
      value = _hex_value.hex
      top_bit = (1 << (bytes * 8 - 1))
      value & top_bit > 0 ? (value - 2 * top_bit) : value
    end

    def valid_address?(_address)
      ValidateAddress.for(address: _address)
    end

    def normalize_address(_value)
      _value.gsub(/^0x/, '').downcase
    end

    def normalize_address_param(_value)
      if _value.respond_to? :normalized_address
        _value.normalized_address
      else
        _value = _value.to_s
        raise ArgumentError, 'invalid address' unless valid_address? _value
        normalize_address _value
      end
    end

    def encode_address_param(_value)
      '0x' + normalize_address_param(_value)
    end

    def encode_block_param(_value)
      return _value.to_s if ['pending', 'earliest', 'latest'].include?(_value.to_s)
      '0x' + _value.to_s(16)
    end

    def encode_quantity_param(_value)
      '0x' + _value.to_s(16)
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

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