module MoacEth::Utils
Public Instance Methods
base256_to_int(str)
click to toggle source
# File lib/moac_eth/utils.rb, line 26 def base256_to_int(str) RLP::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, '') end
bin_to_hex(string)
click to toggle source
# File lib/moac_eth/utils.rb, line 18 def bin_to_hex(string) RLP::Utils.encode_hex string end
bin_to_prefixed_hex(binary)
click to toggle source
# File lib/moac_eth/utils.rb, line 50 def bin_to_prefixed_hex(binary) prefix_hex bin_to_hex(binary) end
format_address(address)
click to toggle source
# File lib/moac_eth/utils.rb, line 104 def format_address(address) Address.new(address).checksummed end
hash160(x)
click to toggle source
# File lib/moac_eth/utils.rb, line 80 def hash160(x) ripemd160 sha256(x) end
hex_to_bin(string)
click to toggle source
# File lib/moac_eth/utils.rb, line 22 def hex_to_bin(string) RLP::Utils.decode_hex remove_hex_prefix(string) end
int_to_base256(int)
click to toggle source
# File lib/moac_eth/utils.rb, line 30 def int_to_base256(int) RLP::Sedes.big_endian_int.serialize int end
keccak256(x)
click to toggle source
# File lib/moac_eth/utils.rb, line 64 def keccak256(x) Digest::SHA3.new(256).digest(x) end
keccak256_rlp(x)
click to toggle source
# File lib/moac_eth/utils.rb, line 72 def keccak256_rlp(x) keccak256 RLP.encode(x) end
keccak512(x)
click to toggle source
# File lib/moac_eth/utils.rb, line 68 def keccak512(x) Digest::SHA3.new(512).digest(x) end
normalize_address(address)
click to toggle source
# File lib/moac_eth/utils.rb, line 6 def normalize_address(address) if address.nil? || address == '' '' elsif address.size == 40 hex_to_bin address elsif address.size == 42 && address[0..1] == '0x' hex_to_bin address[2..-1] else address end end
prefix_hex(hex)
click to toggle source
# File lib/moac_eth/utils.rb, line 42 def prefix_hex(hex) hex.match(/\A0x/) ? hex : "0x#{hex}" end
public_key_to_address(hex)
click to toggle source
# File lib/moac_eth/utils.rb, line 54 def public_key_to_address(hex) bytes = hex_to_bin(hex) address_bytes = Utils.keccak256(bytes[1..-1])[-20..-1] format_address bin_to_prefixed_hex(address_bytes) end
remove_hex_prefix(s)
click to toggle source
# File lib/moac_eth/utils.rb, line 46 def remove_hex_prefix(s) s[0,2] == '0x' ? s[2..-1] : s end
ripemd160(x)
click to toggle source
# File lib/moac_eth/utils.rb, line 76 def ripemd160(x) Digest::RMD160.digest x end
sha256(x)
click to toggle source
# File lib/moac_eth/utils.rb, line 60 def sha256(x) Digest::SHA256.digest x end
v_r_s_for(signature)
click to toggle source
# File lib/moac_eth/utils.rb, line 34 def v_r_s_for(signature) [ signature[0].bytes[0], Utils.base256_to_int(signature[1..32]), Utils.base256_to_int(signature[33..65]), ] end
valid_address?(address)
click to toggle source
# File lib/moac_eth/utils.rb, line 100 def valid_address?(address) Address.new(address).valid? end
zpad(x, l)
click to toggle source
# File lib/moac_eth/utils.rb, line 84 def zpad(x, l) lpad x, BYTE_ZERO, l end
zpad_hex(s, l=32)
click to toggle source
# File lib/moac_eth/utils.rb, line 96 def zpad_hex(s, l=32) zpad decode_hex(s), l end
zpad_int(n, l=32)
click to toggle source
# File lib/moac_eth/utils.rb, line 92 def zpad_int(n, l=32) zpad encode_int(n), l end
zunpad(x)
click to toggle source
# File lib/moac_eth/utils.rb, line 88 def zunpad(x) x.sub /\A\x00+/, '' end
Private Instance Methods
encode_int(n)
click to toggle source
# File lib/moac_eth/utils.rb, line 117 def encode_int(n) unless n.is_a?(Integer) && n >= 0 && n <= UINT_MAX raise ArgumentError, "Integer invalid or out of range: #{n}" end int_to_base256 n end
lpad(x, symbol, l)
click to toggle source
# File lib/moac_eth/utils.rb, line 112 def lpad(x, symbol, l) return x if x.size >= l symbol * (l - x.size) + x end