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.
-
.deserialize_rlp_int(str) ⇒ Integer
Deserializes an RLP integer, enforcing minimal encoding.
-
.hex?(str) ⇒ MatchData?
Checks if a string is hexadecimal.
-
.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.
-
#deserialize_rlp_int(str) ⇒ Integer
Deserializes an RLP integer, enforcing minimal encoding.
-
#hex?(str) ⇒ MatchData?
Checks if a string is hexadecimal.
-
#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.
161 162 163 |
# File 'lib/eth/util.rb', line 161 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.
91 92 93 |
# File 'lib/eth/util.rb', line 91 def bin_to_prefixed_hex(bin) prefix_hex bin_to_hex bin end |
.bytes?(str) ⇒ Boolean
Checks if a string is a byte-string.
185 186 187 |
# File 'lib/eth/util.rb', line 185 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.
177 178 179 |
# File 'lib/eth/util.rb', line 177 def bytes_to_str(bin) bin.unpack("U*").pack("U*") end |
.ceil32(num) ⇒ Integer
Ceil and integer to the next multiple of 32 bytes.
209 210 211 |
# File 'lib/eth/util.rb', line 209 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.
144 145 146 |
# File 'lib/eth/util.rb', line 144 def deserialize_big_endian_to_int(str) Rlp::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, "") end |
.deserialize_rlp_int(str) ⇒ Integer
Deserializes an RLP integer, enforcing minimal encoding.
153 154 155 |
# File 'lib/eth/util.rb', line 153 def deserialize_rlp_int(str) Rlp::Sedes.big_endian_int.deserialize str end |
.hex?(str) ⇒ MatchData?
Checks if a string is hexadecimal.
99 100 101 102 103 |
# File 'lib/eth/util.rb', line 99 def hex?(str) return 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.
130 131 132 133 134 135 136 137 138 |
# File 'lib/eth/util.rb', line 130 def int_to_big_endian(num) hex = if hex? num remove_hex_prefix num else num.to_s(16) end 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.
201 202 203 |
# File 'lib/eth/util.rb', line 201 def list?(item) !primitive?(item) && item.respond_to?(:each) end |
.lpad(str, sym, len) ⇒ String
Left-pad a number with a symbol.
219 220 221 222 |
# File 'lib/eth/util.rb', line 219 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 |
# File 'lib/eth/util.rb', line 74 def prefix_hex(hex) "0x#{remove_hex_prefix hex}" end |
.prefixed?(hex) ⇒ String
Checks if a string is prefixed with 0x
.
109 110 111 |
# File 'lib/eth/util.rb', line 109 def prefixed?(hex) hex.match /\A0x/i end |
.primitive?(item) ⇒ Boolean
Checks if the given item is a string primitive.
193 194 195 |
# File 'lib/eth/util.rb', line 193 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.
82 83 84 85 |
# File 'lib/eth/util.rb', line 82 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.
118 119 120 121 122 123 124 |
# File 'lib/eth/util.rb', line 118 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.
169 170 171 |
# File 'lib/eth/util.rb', line 169 def str_to_bytes(str) bytes?(str) ? str : str.b end |
.zpad(str, len) ⇒ String
Left-pad a serialized string with zeros.
229 230 231 |
# File 'lib/eth/util.rb', line 229 def zpad(str, len) lpad str, Constant::BYTE_ZERO, len end |
.zpad_hex(hex, len = 32) ⇒ String
Left-pad a hex number with zeros.
238 239 240 |
# File 'lib/eth/util.rb', line 238 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.
247 248 249 |
# File 'lib/eth/util.rb', line 247 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.
161 162 163 |
# File 'lib/eth/util.rb', line 161 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.
91 92 93 |
# File 'lib/eth/util.rb', line 91 def bin_to_prefixed_hex(bin) prefix_hex bin_to_hex bin end |
#bytes?(str) ⇒ Boolean
Checks if a string is a byte-string.
185 186 187 |
# File 'lib/eth/util.rb', line 185 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.
177 178 179 |
# File 'lib/eth/util.rb', line 177 def bytes_to_str(bin) bin.unpack("U*").pack("U*") end |
#ceil32(num) ⇒ Integer
Ceil and integer to the next multiple of 32 bytes.
209 210 211 |
# File 'lib/eth/util.rb', line 209 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.
144 145 146 |
# File 'lib/eth/util.rb', line 144 def deserialize_big_endian_to_int(str) Rlp::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, "") end |
#deserialize_rlp_int(str) ⇒ Integer
Deserializes an RLP integer, enforcing minimal encoding.
153 154 155 |
# File 'lib/eth/util.rb', line 153 def deserialize_rlp_int(str) Rlp::Sedes.big_endian_int.deserialize str end |
#hex?(str) ⇒ MatchData?
Checks if a string is hexadecimal.
99 100 101 102 103 |
# File 'lib/eth/util.rb', line 99 def hex?(str) return 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.
130 131 132 133 134 135 136 137 138 |
# File 'lib/eth/util.rb', line 130 def int_to_big_endian(num) hex = if hex? num remove_hex_prefix num else num.to_s(16) end 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.
201 202 203 |
# File 'lib/eth/util.rb', line 201 def list?(item) !primitive?(item) && item.respond_to?(:each) end |
#lpad(str, sym, len) ⇒ String
Left-pad a number with a symbol.
219 220 221 222 |
# File 'lib/eth/util.rb', line 219 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 |
# File 'lib/eth/util.rb', line 74 def prefix_hex(hex) "0x#{remove_hex_prefix hex}" end |
#prefixed?(hex) ⇒ String
Checks if a string is prefixed with 0x
.
109 110 111 |
# File 'lib/eth/util.rb', line 109 def prefixed?(hex) hex.match /\A0x/i end |
#primitive?(item) ⇒ Boolean
Checks if the given item is a string primitive.
193 194 195 |
# File 'lib/eth/util.rb', line 193 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.
82 83 84 85 |
# File 'lib/eth/util.rb', line 82 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.
118 119 120 121 122 123 124 |
# File 'lib/eth/util.rb', line 118 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.
169 170 171 |
# File 'lib/eth/util.rb', line 169 def str_to_bytes(str) bytes?(str) ? str : str.b end |
#zpad(str, len) ⇒ String
Left-pad a serialized string with zeros.
229 230 231 |
# File 'lib/eth/util.rb', line 229 def zpad(str, len) lpad str, Constant::BYTE_ZERO, len end |
#zpad_hex(hex, len = 32) ⇒ String
Left-pad a hex number with zeros.
238 239 240 |
# File 'lib/eth/util.rb', line 238 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.
247 248 249 |
# File 'lib/eth/util.rb', line 247 def zpad_int(num, len = 32) zpad serialize_int_to_big_endian(num), len end |