Module: Klay::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/klay/util.rb

Overview

Defines handy tools for the Klay gem for convenience.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.big_endian_to_int(str) ⇒ Integer

Converts a big endian to an interger.

Parameters:

  • str (String)

    big endian to be converted.

Returns:

  • (Integer)

    an unpacked integer number.



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.

Parameters:

  • bin (String)

    a binary string to be unpacked.

Returns:

  • (String)

    a hexa-decimal string.

Raises:

  • (TypeError)

    if value is not a 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.

Parameters:

  • bin (String)

    a binary string to be unpacked.

Returns:

  • (String)

    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.

Parameters:

  • bin (Object)

    bytes to be converted.

Returns:

  • (String)

    a packed 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.

Parameters:

  • num (Integer)

    the number to ciel up.

Returns:

  • (Integer)

    the ceiled to 32 integer.



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.

Parameters:

  • str (String)

    serialized big endian integer string.

Returns:

  • (Integer)

    an deserialized unsigned 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.

Parameters:

  • hex (String)

    a hexa-decimal string to be packed.

Returns:

  • (String)

    a packed binary string.

Raises:

  • (TypeError)

    if value is not a string or string is not hex.



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.

Parameters:

  • num (Integer)

    integer to be converted.

Returns:

  • (String)

    packed, big-endian integer string.



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.

Parameters:

  • str (String)

    a string to check.

Returns:

  • (Boolean)

    true if it's an ASCII-8bit encoded 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.

Parameters:

  • str (String)

    a string to be checked.

Returns:

  • (String)

    a match if true; nil if not.



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.

Parameters:

  • item (Object)

    the item to check.

Returns:

  • (Boolean)

    true if it's 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.

Parameters:

  • hex (String)

    a string to be checked.

Returns:

  • (String)

    a match if true; nil if not.



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.

Parameters:

  • item (Object)

    the item to check.

Returns:

  • (Boolean)

    true if it's 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.

Parameters:

  • str (String)

    a string to be hashed.

Returns:

  • (String)

    a Keccak-256 hash of the given string.



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.

Parameters:

  • str (String)

    a serialized string to be padded.

  • sym (String)

    a symbol used for left-padding.

  • len (Integer)

    number of symbols for the final string.

Returns:

  • (String)

    a left-padded serialized string of wanted size.



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.

Parameters:

  • hex (String)

    a hex-string to be prefixed.

Returns:

  • (String)

    a prefixed hex-string.



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.

Parameters:

  • str (String)

    the public key to be converted.

Returns:



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.

Parameters:

  • hex (String)

    a prefixed hex-string.

Returns:

  • (String)

    an unprefixed hex-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.

Parameters:

  • num (Integer)

    unsigned integer to be serialized.

Returns:

  • (String)

    serialized big endian integer string.

Raises:

  • (ArgumentError)

    if unsigned integer is out of bounds.



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.

Parameters:

  • str (String)

    binary string to be converted.

Returns:

  • (Object)

    the string 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.

Parameters:

  • str (String)

    a serialized string to be padded.

  • len (Integer)

    number of symbols for the final string.

Returns:

  • (String)

    a zero-padded serialized string of wanted size.



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.

Parameters:

  • hex (String)

    a hex-string to be padded.

  • len (Integer) (defaults to: 32)

    number of symbols for the final string.

Returns:

  • (String)

    a zero-padded serialized string of wanted size.



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.

Parameters:

  • num (Integer)

    an unsigned integer to be padded.

  • len (Integer) (defaults to: 32)

    number of symbols for the final string.

Returns:

  • (String)

    a zero-padded serialized string of wanted size.



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.

Parameters:

  • str (String)

    big endian to be converted.

Returns:

  • (Integer)

    an unpacked integer number.



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.

Parameters:

  • bin (String)

    a binary string to be unpacked.

Returns:

  • (String)

    a hexa-decimal string.

Raises:

  • (TypeError)

    if value is not a 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.

Parameters:

  • bin (String)

    a binary string to be unpacked.

Returns:

  • (String)

    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.

Parameters:

  • bin (Object)

    bytes to be converted.

Returns:

  • (String)

    a packed 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.

Parameters:

  • num (Integer)

    the number to ciel up.

Returns:

  • (Integer)

    the ceiled to 32 integer.



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.

Parameters:

  • str (String)

    serialized big endian integer string.

Returns:

  • (Integer)

    an deserialized unsigned 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.

Parameters:

  • hex (String)

    a hexa-decimal string to be packed.

Returns:

  • (String)

    a packed binary string.

Raises:

  • (TypeError)

    if value is not a string or string is not hex.



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.

Parameters:

  • num (Integer)

    integer to be converted.

Returns:

  • (String)

    packed, big-endian integer string.



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.

Parameters:

  • str (String)

    a string to check.

Returns:

  • (Boolean)

    true if it's an ASCII-8bit encoded 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.

Parameters:

  • str (String)

    a string to be checked.

Returns:

  • (String)

    a match if true; nil if not.



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.

Parameters:

  • item (Object)

    the item to check.

Returns:

  • (Boolean)

    true if it's 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.

Parameters:

  • hex (String)

    a string to be checked.

Returns:

  • (String)

    a match if true; nil if not.



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.

Parameters:

  • item (Object)

    the item to check.

Returns:

  • (Boolean)

    true if it's 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.

Parameters:

  • str (String)

    a string to be hashed.

Returns:

  • (String)

    a Keccak-256 hash of the given string.



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.

Parameters:

  • str (String)

    a serialized string to be padded.

  • sym (String)

    a symbol used for left-padding.

  • len (Integer)

    number of symbols for the final string.

Returns:

  • (String)

    a left-padded serialized string of wanted size.



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.

Parameters:

  • hex (String)

    a hex-string to be prefixed.

Returns:

  • (String)

    a prefixed hex-string.



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.

Parameters:

  • str (String)

    the public key to be converted.

Returns:



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.

Parameters:

  • hex (String)

    a prefixed hex-string.

Returns:

  • (String)

    an unprefixed hex-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.

Parameters:

  • num (Integer)

    unsigned integer to be serialized.

Returns:

  • (String)

    serialized big endian integer string.

Raises:

  • (ArgumentError)

    if unsigned integer is out of bounds.



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.

Parameters:

  • str (String)

    binary string to be converted.

Returns:

  • (Object)

    the string 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.

Parameters:

  • str (String)

    a serialized string to be padded.

  • len (Integer)

    number of symbols for the final string.

Returns:

  • (String)

    a zero-padded serialized string of wanted size.



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.

Parameters:

  • hex (String)

    a hex-string to be padded.

  • len (Integer) (defaults to: 32)

    number of symbols for the final string.

Returns:

  • (String)

    a zero-padded serialized string of wanted size.



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.

Parameters:

  • num (Integer)

    an unsigned integer to be padded.

  • len (Integer) (defaults to: 32)

    number of symbols for the final string.

Returns:

  • (String)

    a zero-padded serialized string of wanted size.



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