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



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

def encode_address_param(_value)
  "0x#{normalize_address_param(_value)}"
end

#encode_block_param(_value) ⇒ Object



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

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



63
64
65
# File 'lib/etherlite/utils.rb', line 63

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).positive? ? (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.negative?
    # 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
51
# 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::Keccak.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