module Etherlite::Utils
Public Instance Methods
encode_address_param(_value)
click to toggle source
# File lib/etherlite/utils.rb, line 52 def encode_address_param(_value) '0x' + normalize_address_param(_value) end
encode_block_param(_value)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/etherlite/utils.rb, line 24 def hex_to_uint(_hex_value) _hex_value.hex end
int_to_hex(_value, bytes: 32)
click to toggle source
# 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)
click to toggle source
# File lib/etherlite/utils.rb, line 38 def normalize_address(_value) _value.gsub(/^0x/, '').downcase end
normalize_address_param(_value)
click to toggle source
# 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)
click to toggle source
# File lib/etherlite/utils.rb, line 7 def sha3(_data) Digest::SHA3.hexdigest(_data, 256) end
uint_to_hex(_value, bytes: 32)
click to toggle source
# 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)
click to toggle source
# File lib/etherlite/utils.rb, line 34 def valid_address?(_address) ValidateAddress.for(address: _address) end