Module: Klay::Util
Overview
Defines handy tools for the Klay 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_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_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.
-
.is_bytes?(str) ⇒ Boolean
Checks if a string is a byte-string.
-
.is_hex?(str) ⇒ String
Checks if a string is hex-adecimal.
-
.is_list?(item) ⇒ Boolean
Checks if the given item is a list.
-
.is_prefixed?(hex) ⇒ String
Checks if a string is prefixed with
0x
. -
.is_primitive?(item) ⇒ Boolean
Checks if the given item is a string primitive.
-
.keccak256(str) ⇒ String
Hashes a string with the Keccak-256 algorithm.
-
.lpad(str, sym, len) ⇒ String
Left-pad a number with a symbol.
-
.prefix_hex(hex) ⇒ String
Prefixes a hexa-decimal string with
0x
. -
.public_key_to_address(str) ⇒ Klay::Address
Generates an Klaytn 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_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_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.
-
#is_bytes?(str) ⇒ Boolean
Checks if a string is a byte-string.
-
#is_hex?(str) ⇒ String
Checks if a string is hex-adecimal.
-
#is_list?(item) ⇒ Boolean
Checks if the given item is a list.
-
#is_prefixed?(hex) ⇒ String
Checks if a string is prefixed with
0x
. -
#is_primitive?(item) ⇒ Boolean
Checks if the given item is a string primitive.
-
#keccak256(str) ⇒ String
Hashes a string with the Keccak-256 algorithm.
-
#lpad(str, sym, len) ⇒ String
Left-pad a number with a symbol.
-
#prefix_hex(hex) ⇒ String
Prefixes a hexa-decimal string with
0x
. -
#public_key_to_address(str) ⇒ Klay::Address
Generates an Klaytn 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.
145 146 147 |
# File 'lib/klay/util.rb', line 145 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.
48 49 50 51 |
# File 'lib/klay/util.rb', line 48 def bin_to_hex(bin) raise TypeError, "Value must be an instance of String" unless bin.instance_of? String bin.unpack("H*").first end |
.bin_to_prefixed_hex(bin) ⇒ String
Unpacks a binary string to a prefixed hexa-decimal string.
88 89 90 |
# File 'lib/klay/util.rb', line 88 def bin_to_prefixed_hex(bin) prefix_hex bin_to_hex bin end |
.bytes_to_str(bin) ⇒ String
Converts bytes to a binary string.
161 162 163 |
# File 'lib/klay/util.rb', line 161 def bytes_to_str(bin) bin.unpack("U*").pack("U*") end |
.ceil32(num) ⇒ Integer
Ceil and integer to the next multiple of 32 bytes.
193 194 195 |
# File 'lib/klay/util.rb', line 193 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.
137 138 139 |
# File 'lib/klay/util.rb', line 137 def deserialize_big_endian_to_int(str) Rlp::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, "") end |
.hex_to_bin(hex) ⇒ String
Packs a hexa-decimal string into a binary string. Also works with
0x
-prefixed strings.
59 60 61 62 63 64 |
# File 'lib/klay/util.rb', line 59 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 is_hex? hex [hex].pack("H*") end |
.int_to_big_endian(num) ⇒ String
Converts an integer to big endian.
127 128 129 130 131 |
# File 'lib/klay/util.rb', line 127 def int_to_big_endian(num) hex = num.to_s(16) unless is_hex? num hex = "0#{hex}" if hex.size.odd? hex_to_bin hex end |
.is_bytes?(str) ⇒ Boolean
Checks if a string is a byte-string.
169 170 171 |
# File 'lib/klay/util.rb', line 169 def is_bytes?(str) str && str.instance_of?(String) && str.encoding.name == Constant::BINARY_ENCODING end |
.is_hex?(str) ⇒ String
Checks if a string is hex-adecimal.
96 97 98 99 100 |
# File 'lib/klay/util.rb', line 96 def is_hex?(str) return false unless str.is_a? String str = remove_hex_prefix str str.match /\A[0-9a-fA-F]*\z/ end |
.is_list?(item) ⇒ Boolean
Checks if the given item is a list.
185 186 187 |
# File 'lib/klay/util.rb', line 185 def is_list?(item) !is_primitive?(item) && item.respond_to?(:each) end |
.is_prefixed?(hex) ⇒ String
Checks if a string is prefixed with 0x
.
106 107 108 |
# File 'lib/klay/util.rb', line 106 def is_prefixed?(hex) hex.match /\A0x/ end |
.is_primitive?(item) ⇒ Boolean
Checks if the given item is a string primitive.
177 178 179 |
# File 'lib/klay/util.rb', line 177 def is_primitive?(item) item.instance_of?(String) end |
.keccak256(str) ⇒ String
Hashes a string with the Keccak-256 algorithm.
39 40 41 |
# File 'lib/klay/util.rb', line 39 def keccak256(str) Digest::Keccak.new(256).digest str end |
.lpad(str, sym, len) ⇒ String
Left-pad a number with a symbol.
203 204 205 206 |
# File 'lib/klay/util.rb', line 203 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
.
70 71 72 73 |
# File 'lib/klay/util.rb', line 70 def prefix_hex(hex) return hex if is_prefixed? hex return "0x#{hex}" end |
.public_key_to_address(str) ⇒ Klay::Address
Generates an Klaytn address from a given compressed or uncompressed binary or hexadecimal public key string.
29 30 31 32 33 |
# File 'lib/klay/util.rb', line 29 def public_key_to_address(str) str = hex_to_bin str if is_hex? str 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.
79 80 81 82 |
# File 'lib/klay/util.rb', line 79 def remove_hex_prefix(hex) return hex[2..-1] if is_prefixed? hex return hex end |
.serialize_int_to_big_endian(num) ⇒ String
Serializes an unsigned integer to big endian.
115 116 117 118 119 120 121 |
# File 'lib/klay/util.rb', line 115 def serialize_int_to_big_endian(num) num = num.to_i(16) if is_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.
153 154 155 |
# File 'lib/klay/util.rb', line 153 def str_to_bytes(str) is_bytes?(str) ? str : str.b end |
.zpad(str, len) ⇒ String
Left-pad a serialized string with zeros.
213 214 215 |
# File 'lib/klay/util.rb', line 213 def zpad(str, len) lpad str, Constant::BYTE_ZERO, len end |
.zpad_hex(hex, len = 32) ⇒ String
Left-pad a hex number with zeros.
222 223 224 |
# File 'lib/klay/util.rb', line 222 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.
231 232 233 |
# File 'lib/klay/util.rb', line 231 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.
145 146 147 |
# File 'lib/klay/util.rb', line 145 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.
48 49 50 51 |
# File 'lib/klay/util.rb', line 48 def bin_to_hex(bin) raise TypeError, "Value must be an instance of String" unless bin.instance_of? String bin.unpack("H*").first end |
#bin_to_prefixed_hex(bin) ⇒ String
Unpacks a binary string to a prefixed hexa-decimal string.
88 89 90 |
# File 'lib/klay/util.rb', line 88 def bin_to_prefixed_hex(bin) prefix_hex bin_to_hex bin end |
#bytes_to_str(bin) ⇒ String
Converts bytes to a binary string.
161 162 163 |
# File 'lib/klay/util.rb', line 161 def bytes_to_str(bin) bin.unpack("U*").pack("U*") end |
#ceil32(num) ⇒ Integer
Ceil and integer to the next multiple of 32 bytes.
193 194 195 |
# File 'lib/klay/util.rb', line 193 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.
137 138 139 |
# File 'lib/klay/util.rb', line 137 def deserialize_big_endian_to_int(str) Rlp::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, "") end |
#hex_to_bin(hex) ⇒ String
Packs a hexa-decimal string into a binary string. Also works with
0x
-prefixed strings.
59 60 61 62 63 64 |
# File 'lib/klay/util.rb', line 59 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 is_hex? hex [hex].pack("H*") end |
#int_to_big_endian(num) ⇒ String
Converts an integer to big endian.
127 128 129 130 131 |
# File 'lib/klay/util.rb', line 127 def int_to_big_endian(num) hex = num.to_s(16) unless is_hex? num hex = "0#{hex}" if hex.size.odd? hex_to_bin hex end |
#is_bytes?(str) ⇒ Boolean
Checks if a string is a byte-string.
169 170 171 |
# File 'lib/klay/util.rb', line 169 def is_bytes?(str) str && str.instance_of?(String) && str.encoding.name == Constant::BINARY_ENCODING end |
#is_hex?(str) ⇒ String
Checks if a string is hex-adecimal.
96 97 98 99 100 |
# File 'lib/klay/util.rb', line 96 def is_hex?(str) return false unless str.is_a? String str = remove_hex_prefix str str.match /\A[0-9a-fA-F]*\z/ end |
#is_list?(item) ⇒ Boolean
Checks if the given item is a list.
185 186 187 |
# File 'lib/klay/util.rb', line 185 def is_list?(item) !is_primitive?(item) && item.respond_to?(:each) end |
#is_prefixed?(hex) ⇒ String
Checks if a string is prefixed with 0x
.
106 107 108 |
# File 'lib/klay/util.rb', line 106 def is_prefixed?(hex) hex.match /\A0x/ end |
#is_primitive?(item) ⇒ Boolean
Checks if the given item is a string primitive.
177 178 179 |
# File 'lib/klay/util.rb', line 177 def is_primitive?(item) item.instance_of?(String) end |
#keccak256(str) ⇒ String
Hashes a string with the Keccak-256 algorithm.
39 40 41 |
# File 'lib/klay/util.rb', line 39 def keccak256(str) Digest::Keccak.new(256).digest str end |
#lpad(str, sym, len) ⇒ String
Left-pad a number with a symbol.
203 204 205 206 |
# File 'lib/klay/util.rb', line 203 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
.
70 71 72 73 |
# File 'lib/klay/util.rb', line 70 def prefix_hex(hex) return hex if is_prefixed? hex return "0x#{hex}" end |
#public_key_to_address(str) ⇒ Klay::Address
Generates an Klaytn address from a given compressed or uncompressed binary or hexadecimal public key string.
29 30 31 32 33 |
# File 'lib/klay/util.rb', line 29 def public_key_to_address(str) str = hex_to_bin str if is_hex? str 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.
79 80 81 82 |
# File 'lib/klay/util.rb', line 79 def remove_hex_prefix(hex) return hex[2..-1] if is_prefixed? hex return hex end |
#serialize_int_to_big_endian(num) ⇒ String
Serializes an unsigned integer to big endian.
115 116 117 118 119 120 121 |
# File 'lib/klay/util.rb', line 115 def serialize_int_to_big_endian(num) num = num.to_i(16) if is_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.
153 154 155 |
# File 'lib/klay/util.rb', line 153 def str_to_bytes(str) is_bytes?(str) ? str : str.b end |
#zpad(str, len) ⇒ String
Left-pad a serialized string with zeros.
213 214 215 |
# File 'lib/klay/util.rb', line 213 def zpad(str, len) lpad str, Constant::BYTE_ZERO, len end |
#zpad_hex(hex, len = 32) ⇒ String
Left-pad a hex number with zeros.
222 223 224 |
# File 'lib/klay/util.rb', line 222 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.
231 232 233 |
# File 'lib/klay/util.rb', line 231 def zpad_int(num, len = 32) zpad serialize_int_to_big_endian(num), len end |