Module: Etherlite::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/etherlite/utils.rb,
lib/etherlite/commands/utils/validate_address.rb

Defined Under Namespace

Classes: ValidateAddress

Instance Method Summary collapse

Instance Method Details

#encode_address_param(_value) ⇒ Object



50
51
52
# File 'lib/etherlite/utils.rb', line 50

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

#encode_block_param(_value) ⇒ Object



54
55
56
57
# File 'lib/etherlite/utils.rb', line 54

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

#encode_quantity_param(_value) ⇒ Object



59
60
61
# File 'lib/etherlite/utils.rb', line 59

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

#hex_to_int(_hex_value) ⇒ Object



28
29
30
# File 'lib/etherlite/utils.rb', line 28

def hex_to_int(_hex_value)
  # TODO.
end

#hex_to_uint(_hex_value) ⇒ Object



24
25
26
# File 'lib/etherlite/utils.rb', line 24

def hex_to_uint(_hex_value)
  _hex_value.hex
end

#int_to_hex(_value, bytes: 32) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/etherlite/utils.rb', line 15

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

#normalize_address(_value) ⇒ Object



36
37
38
# File 'lib/etherlite/utils.rb', line 36

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

#normalize_address_param(_value) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/etherlite/utils.rb', line 40

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

#sha3(_data) ⇒ Object



7
8
9
# File 'lib/etherlite/utils.rb', line 7

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

#uint_to_hex(_value, bytes: 32) ⇒ Object



11
12
13
# File 'lib/etherlite/utils.rb', line 11

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

#valid_address?(_address) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/etherlite/utils.rb', line 32

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