class AppChain::Utils

Constants

HEX_PREFIX

Public Class Methods

add_hex_prefix(hex) click to toggle source

add `0x` prefix to hex string

@param hex [String]

# File lib/appchain/utils.rb, line 11
def add_hex_prefix(hex)
  return if hex.nil?
  return hex if hex.start_with?(HEX_PREFIX)

  HEX_PREFIX + hex
end
add_prefix_for_not_blank(hex) click to toggle source

add `0x` prefix to not blank hex string

@param hex [String]

# File lib/appchain/utils.rb, line 21
def add_prefix_for_not_blank(hex)
  return add_hex_prefix(hex) unless hex.blank?

  hex
end
from_bytes(bytes_str) click to toggle source

byte code to string value, with `0x` prefix

@param bytes_str [String] byte code string @return [String] normal string

# File lib/appchain/utils.rb, line 66
def from_bytes(bytes_str)
  hex = bytes_str.unpack1("H*")
  return AppChain::Utils.add_hex_prefix(hex) unless hex.blank?

  hex
end
keccak256(*data) click to toggle source

keccak 256 hash

# File lib/appchain/utils.rb, line 75
def keccak256(*data)
  Ciri::Utils.keccak(*data)
end
nonce() click to toggle source

get nonce

@return [String]

# File lib/appchain/utils.rb, line 82
def nonce
  SecureRandom.hex
end
remove_hex_prefix(hex) click to toggle source

remove `0x` prefix from hex string

@param hex [String]

# File lib/appchain/utils.rb, line 30
def remove_hex_prefix(hex)
  return if hex.nil?
  return hex.gsub(HEX_PREFIX, "") if hex.start_with?(HEX_PREFIX)

  hex
end
to_bytes(str) click to toggle source

to byte code value remove `0x` prefix first

@param str [String] normal string @return [String] byte code string

# File lib/appchain/utils.rb, line 58
def to_bytes(str)
  [AppChain::Utils.remove_hex_prefix(str)].pack("H*")
end
to_decimal(hex) click to toggle source

convert hex string to decimal value

@param hex [String] @return [Integer]

# File lib/appchain/utils.rb, line 49
def to_decimal(hex)
  hex.hex
end
to_hex(decimal) click to toggle source

convert decimal value to hex string without `0x` prefix

@param decimal [Integer] @return [String]

# File lib/appchain/utils.rb, line 41
def to_hex(decimal)
  add_hex_prefix decimal.to_s(16)
end