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



52
53
54
# File 'lib/etherlite/utils.rb', line 52

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

#encode_block_param(_value) ⇒ Object



56
57
58
59
# File 'lib/etherlite/utils.rb', line 56

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



61
62
63
# File 'lib/etherlite/utils.rb', line 61

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

#hex_to_int(_hex_value, bytes: 32) ⇒ Object



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

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

#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



38
39
40
# File 'lib/etherlite/utils.rb', line 38

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

#normalize_address_param(_value) ⇒ Object



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

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)


34
35
36
# File 'lib/etherlite/utils.rb', line 34

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