Module: Eth::Util
Overview
Defines handy tools for the Eth gem for convenience.
Class Method Summary collapse
-
.big_endian_to_int(str) ⇒ Integer
Converts a big endian to an interger.
-
.bin_to_hex(bin) ⇒ String
Unpacks a binary string to a hexa-decimal string.
-
.bin_to_prefixed_hex(bin) ⇒ String
Unpacks a binary string to a prefixed hexa-decimal string.
-
.bytes?(str) ⇒ Boolean
Checks if a string is a byte-string.
-
.bytes_to_str(bin) ⇒ String
Converts bytes to a binary string.
-
.ceil32(num) ⇒ Integer
Ceil and integer to the next multiple of 32 bytes.
-
.deserialize_big_endian_to_int(str) ⇒ Integer
Deserializes big endian data string to integer.
-
.hex?(str) ⇒ String
Checks if a string is hex-adecimal.
-
.hex_to_bin(hex) ⇒ String
Packs a hexa-decimal string into a binary string.
-
.int_to_big_endian(num) ⇒ String
Converts an integer to big endian.
-
.keccak256(str) ⇒ String
Hashes a string with the Keccak-256 algorithm.
-
.list?(item) ⇒ Boolean
Checks if the given item is a list.
-
.lpad(str, sym, len) ⇒ String
Left-pad a number with a symbol.
-
.prefix_hex(hex) ⇒ String
Prefixes a hexa-decimal string with
0x
. -
.prefixed?(hex) ⇒ String
Checks if a string is prefixed with
0x
. -
.primitive?(item) ⇒ Boolean
Checks if the given item is a string primitive.
-
.public_key_to_address(str) ⇒ Eth::Address
Generates an Ethereum address from a given compressed or uncompressed binary or hexadecimal public key string.
-
.remove_hex_prefix(hex) ⇒ String
Removes the
0x
prefix of a hexa-decimal string. -
.serialize_int_to_big_endian(num) ⇒ String
Serializes an unsigned integer to big endian.
-
.str_to_bytes(str) ⇒ Object
Converts a binary string to bytes.
-
.zpad(str, len) ⇒ String
Left-pad a serialized string with zeros.
-
.zpad_hex(hex, len = 32) ⇒ String
Left-pad a hex number with zeros.
-
.zpad_int(num, len = 32) ⇒ String
Left-pad an unsigned integer with zeros.
Instance Method Summary collapse
-
#big_endian_to_int(str) ⇒ Integer
Converts a big endian to an interger.
-
#bin_to_hex(bin) ⇒ String
Unpacks a binary string to a hexa-decimal string.
-
#bin_to_prefixed_hex(bin) ⇒ String
Unpacks a binary string to a prefixed hexa-decimal string.
-
#bytes?(str) ⇒ Boolean
Checks if a string is a byte-string.
-
#bytes_to_str(bin) ⇒ String
Converts bytes to a binary string.
-
#ceil32(num) ⇒ Integer
Ceil and integer to the next multiple of 32 bytes.
-
#deserialize_big_endian_to_int(str) ⇒ Integer
Deserializes big endian data string to integer.
-
#hex?(str) ⇒ String
Checks if a string is hex-adecimal.
-
#hex_to_bin(hex) ⇒ String
Packs a hexa-decimal string into a binary string.
-
#int_to_big_endian(num) ⇒ String
Converts an integer to big endian.
-
#keccak256(str) ⇒ String
Hashes a string with the Keccak-256 algorithm.
-
#list?(item) ⇒ Boolean
Checks if the given item is a list.
-
#lpad(str, sym, len) ⇒ String
Left-pad a number with a symbol.
-
#prefix_hex(hex) ⇒ String
Prefixes a hexa-decimal string with
0x
. -
#prefixed?(hex) ⇒ String
Checks if a string is prefixed with
0x
. -
#primitive?(item) ⇒ Boolean
Checks if the given item is a string primitive.
-
#public_key_to_address(str) ⇒ Eth::Address
Generates an Ethereum address from a given compressed or uncompressed binary or hexadecimal public key string.
-
#remove_hex_prefix(hex) ⇒ String
Removes the
0x
prefix of a hexa-decimal string. -
#serialize_int_to_big_endian(num) ⇒ String
Serializes an unsigned integer to big endian.
-
#str_to_bytes(str) ⇒ Object
Converts a binary string to bytes.
-
#zpad(str, len) ⇒ String
Left-pad a serialized string with zeros.
-
#zpad_hex(hex, len = 32) ⇒ String
Left-pad a hex number with zeros.
-
#zpad_int(num, len = 32) ⇒ String
Left-pad an unsigned integer with zeros.
Class Method Details
.big_endian_to_int(str) ⇒ Integer
Converts a big endian to an interger.
149 150 151 |
# File 'lib/eth/util.rb', line 149 def big_endian_to_int(str) str.unpack("H*").first.to_i(16) end |
.bin_to_hex(bin) ⇒ String
Unpacks a binary string to a hexa-decimal string.
51 52 53 54 |
# File 'lib/eth/util.rb', line 51 def bin_to_hex(bin) raise TypeError, "Value must be an instance of String" unless bin.instance_of? String hex = bin.unpack("H*").first end |
.bin_to_prefixed_hex(bin) ⇒ String
Unpacks a binary string to a prefixed hexa-decimal string.
92 93 94 |
# File 'lib/eth/util.rb', line 92 def bin_to_prefixed_hex(bin) prefix_hex bin_to_hex bin end |
.bytes?(str) ⇒ Boolean
Checks if a string is a byte-string.
173 174 175 |
# File 'lib/eth/util.rb', line 173 def bytes?(str) str && str.instance_of?(String) && str.encoding.name == Constant::BINARY_ENCODING end |
.bytes_to_str(bin) ⇒ String
Converts bytes to a binary string.
165 166 167 |
# File 'lib/eth/util.rb', line 165 def bytes_to_str(bin) bin.unpack("U*").pack("U*") end |
.ceil32(num) ⇒ Integer
Ceil and integer to the next multiple of 32 bytes.
197 198 199 |
# File 'lib/eth/util.rb', line 197 def ceil32(num) num % 32 == 0 ? num : (num + 32 - num % 32) end |
.deserialize_big_endian_to_int(str) ⇒ Integer
Deserializes big endian data string to integer.
141 142 143 |
# File 'lib/eth/util.rb', line 141 def deserialize_big_endian_to_int(str) Rlp::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, "") end |
.hex?(str) ⇒ String
Checks if a string is hex-adecimal.
100 101 102 103 104 |
# File 'lib/eth/util.rb', line 100 def hex?(str) return false unless str.is_a? String str = remove_hex_prefix str str.match /\A[0-9a-fA-F]*\z/ end |
.hex_to_bin(hex) ⇒ String
Packs a hexa-decimal string into a binary string. Also works with 0x
-prefixed strings.
62 63 64 65 66 67 68 |
# File 'lib/eth/util.rb', line 62 def hex_to_bin(hex) raise TypeError, "Value must be an instance of String" unless hex.instance_of? String hex = remove_hex_prefix hex raise TypeError, "Non-hexadecimal digit found" unless hex? hex hex = "0#{hex}" if hex.size % 2 != 0 bin = [hex].pack("H*") end |
.int_to_big_endian(num) ⇒ String
Converts an integer to big endian.
131 132 133 134 135 |
# File 'lib/eth/util.rb', line 131 def int_to_big_endian(num) hex = num.to_s(16) unless hex? num hex = "0#{hex}" if hex.size.odd? hex_to_bin hex end |
.keccak256(str) ⇒ String
Hashes a string with the Keccak-256 algorithm.
42 43 44 |
# File 'lib/eth/util.rb', line 42 def keccak256(str) Digest::Keccak.new(256).digest str end |
.list?(item) ⇒ Boolean
Checks if the given item is a list.
189 190 191 |
# File 'lib/eth/util.rb', line 189 def list?(item) !primitive?(item) && item.respond_to?(:each) end |
.lpad(str, sym, len) ⇒ String
Left-pad a number with a symbol.
207 208 209 210 |
# File 'lib/eth/util.rb', line 207 def lpad(str, sym, len) return str if str.size >= len sym * (len - str.size) + str end |
.prefix_hex(hex) ⇒ String
Prefixes a hexa-decimal string with 0x
.
74 75 76 77 |
# File 'lib/eth/util.rb', line 74 def prefix_hex(hex) return hex if prefixed? hex return "0x#{hex}" end |
.prefixed?(hex) ⇒ String
Checks if a string is prefixed with 0x
.
110 111 112 |
# File 'lib/eth/util.rb', line 110 def prefixed?(hex) hex.match /\A0x/ end |
.primitive?(item) ⇒ Boolean
Checks if the given item is a string primitive.
181 182 183 |
# File 'lib/eth/util.rb', line 181 def primitive?(item) item.instance_of?(String) end |
.public_key_to_address(str) ⇒ Eth::Address
Generates an Ethereum address from a given compressed or uncompressed binary or hexadecimal public key string.
31 32 33 34 35 36 |
# File 'lib/eth/util.rb', line 31 def public_key_to_address(str) str = hex_to_bin str if hex? str str = Secp256k1::PublicKey.from_data(str).uncompressed bytes = keccak256(str[1..-1])[-20..-1] Address.new bin_to_prefixed_hex bytes end |
.remove_hex_prefix(hex) ⇒ String
Removes the 0x
prefix of a hexa-decimal string.
83 84 85 86 |
# File 'lib/eth/util.rb', line 83 def remove_hex_prefix(hex) return hex[2..-1] if prefixed? hex return hex end |
.serialize_int_to_big_endian(num) ⇒ String
Serializes an unsigned integer to big endian.
119 120 121 122 123 124 125 |
# File 'lib/eth/util.rb', line 119 def serialize_int_to_big_endian(num) num = num.to_i(16) if hex? num unless num.is_a? Integer and num >= 0 and num <= Constant::UINT_MAX raise ArgumentError, "Integer invalid or out of range: #{num}" end Rlp::Sedes.big_endian_int.serialize num end |
.str_to_bytes(str) ⇒ Object
Converts a binary string to bytes.
157 158 159 |
# File 'lib/eth/util.rb', line 157 def str_to_bytes(str) bytes?(str) ? str : str.b end |
.zpad(str, len) ⇒ String
Left-pad a serialized string with zeros.
217 218 219 |
# File 'lib/eth/util.rb', line 217 def zpad(str, len) lpad str, Constant::BYTE_ZERO, len end |
.zpad_hex(hex, len = 32) ⇒ String
Left-pad a hex number with zeros.
226 227 228 |
# File 'lib/eth/util.rb', line 226 def zpad_hex(hex, len = 32) zpad hex_to_bin(hex), len end |
.zpad_int(num, len = 32) ⇒ String
Left-pad an unsigned integer with zeros.
235 236 237 |
# File 'lib/eth/util.rb', line 235 def zpad_int(num, len = 32) zpad serialize_int_to_big_endian(num), len end |
Instance Method Details
#big_endian_to_int(str) ⇒ Integer
Converts a big endian to an interger.
149 150 151 |
# File 'lib/eth/util.rb', line 149 def big_endian_to_int(str) str.unpack("H*").first.to_i(16) end |
#bin_to_hex(bin) ⇒ String
Unpacks a binary string to a hexa-decimal string.
51 52 53 54 |
# File 'lib/eth/util.rb', line 51 def bin_to_hex(bin) raise TypeError, "Value must be an instance of String" unless bin.instance_of? String hex = bin.unpack("H*").first end |
#bin_to_prefixed_hex(bin) ⇒ String
Unpacks a binary string to a prefixed hexa-decimal string.
92 93 94 |
# File 'lib/eth/util.rb', line 92 def bin_to_prefixed_hex(bin) prefix_hex bin_to_hex bin end |
#bytes?(str) ⇒ Boolean
Checks if a string is a byte-string.
173 174 175 |
# File 'lib/eth/util.rb', line 173 def bytes?(str) str && str.instance_of?(String) && str.encoding.name == Constant::BINARY_ENCODING end |
#bytes_to_str(bin) ⇒ String
Converts bytes to a binary string.
165 166 167 |
# File 'lib/eth/util.rb', line 165 def bytes_to_str(bin) bin.unpack("U*").pack("U*") end |
#ceil32(num) ⇒ Integer
Ceil and integer to the next multiple of 32 bytes.
197 198 199 |
# File 'lib/eth/util.rb', line 197 def ceil32(num) num % 32 == 0 ? num : (num + 32 - num % 32) end |
#deserialize_big_endian_to_int(str) ⇒ Integer
Deserializes big endian data string to integer.
141 142 143 |
# File 'lib/eth/util.rb', line 141 def deserialize_big_endian_to_int(str) Rlp::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, "") end |
#hex?(str) ⇒ String
Checks if a string is hex-adecimal.
100 101 102 103 104 |
# File 'lib/eth/util.rb', line 100 def hex?(str) return false unless str.is_a? String str = remove_hex_prefix str str.match /\A[0-9a-fA-F]*\z/ end |
#hex_to_bin(hex) ⇒ String
Packs a hexa-decimal string into a binary string. Also works with 0x
-prefixed strings.
62 63 64 65 66 67 68 |
# File 'lib/eth/util.rb', line 62 def hex_to_bin(hex) raise TypeError, "Value must be an instance of String" unless hex.instance_of? String hex = remove_hex_prefix hex raise TypeError, "Non-hexadecimal digit found" unless hex? hex hex = "0#{hex}" if hex.size % 2 != 0 bin = [hex].pack("H*") end |
#int_to_big_endian(num) ⇒ String
Converts an integer to big endian.
131 132 133 134 135 |
# File 'lib/eth/util.rb', line 131 def int_to_big_endian(num) hex = num.to_s(16) unless hex? num hex = "0#{hex}" if hex.size.odd? hex_to_bin hex end |
#keccak256(str) ⇒ String
Hashes a string with the Keccak-256 algorithm.
42 43 44 |
# File 'lib/eth/util.rb', line 42 def keccak256(str) Digest::Keccak.new(256).digest str end |
#list?(item) ⇒ Boolean
Checks if the given item is a list.
189 190 191 |
# File 'lib/eth/util.rb', line 189 def list?(item) !primitive?(item) && item.respond_to?(:each) end |
#lpad(str, sym, len) ⇒ String
Left-pad a number with a symbol.
207 208 209 210 |
# File 'lib/eth/util.rb', line 207 def lpad(str, sym, len) return str if str.size >= len sym * (len - str.size) + str end |
#prefix_hex(hex) ⇒ String
Prefixes a hexa-decimal string with 0x
.
74 75 76 77 |
# File 'lib/eth/util.rb', line 74 def prefix_hex(hex) return hex if prefixed? hex return "0x#{hex}" end |
#prefixed?(hex) ⇒ String
Checks if a string is prefixed with 0x
.
110 111 112 |
# File 'lib/eth/util.rb', line 110 def prefixed?(hex) hex.match /\A0x/ end |
#primitive?(item) ⇒ Boolean
Checks if the given item is a string primitive.
181 182 183 |
# File 'lib/eth/util.rb', line 181 def primitive?(item) item.instance_of?(String) end |
#public_key_to_address(str) ⇒ Eth::Address
Generates an Ethereum address from a given compressed or uncompressed binary or hexadecimal public key string.
31 32 33 34 35 36 |
# File 'lib/eth/util.rb', line 31 def public_key_to_address(str) str = hex_to_bin str if hex? str str = Secp256k1::PublicKey.from_data(str).uncompressed bytes = keccak256(str[1..-1])[-20..-1] Address.new bin_to_prefixed_hex bytes end |
#remove_hex_prefix(hex) ⇒ String
Removes the 0x
prefix of a hexa-decimal string.
83 84 85 86 |
# File 'lib/eth/util.rb', line 83 def remove_hex_prefix(hex) return hex[2..-1] if prefixed? hex return hex end |
#serialize_int_to_big_endian(num) ⇒ String
Serializes an unsigned integer to big endian.
119 120 121 122 123 124 125 |
# File 'lib/eth/util.rb', line 119 def serialize_int_to_big_endian(num) num = num.to_i(16) if hex? num unless num.is_a? Integer and num >= 0 and num <= Constant::UINT_MAX raise ArgumentError, "Integer invalid or out of range: #{num}" end Rlp::Sedes.big_endian_int.serialize num end |
#str_to_bytes(str) ⇒ Object
Converts a binary string to bytes.
157 158 159 |
# File 'lib/eth/util.rb', line 157 def str_to_bytes(str) bytes?(str) ? str : str.b end |
#zpad(str, len) ⇒ String
Left-pad a serialized string with zeros.
217 218 219 |
# File 'lib/eth/util.rb', line 217 def zpad(str, len) lpad str, Constant::BYTE_ZERO, len end |
#zpad_hex(hex, len = 32) ⇒ String
Left-pad a hex number with zeros.
226 227 228 |
# File 'lib/eth/util.rb', line 226 def zpad_hex(hex, len = 32) zpad hex_to_bin(hex), len end |
#zpad_int(num, len = 32) ⇒ String
Left-pad an unsigned integer with zeros.
235 236 237 |
# File 'lib/eth/util.rb', line 235 def zpad_int(num, len = 32) zpad serialize_int_to_big_endian(num), len end |