Module: Trx::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/trx/utils.rb

Instance Method Summary collapse

Instance Method Details

#base58_decode_hex(hex) ⇒ Object



31
32
33
# File 'lib/trx/utils.rb', line 31

def base58_decode_hex(hex)
  Base58.decode_hex(remove_hex_prefix(hex))
end

#base58check(value) ⇒ Object



27
28
29
# File 'lib/trx/utils.rb', line 27

def base58check(value)
  sha256(sha256(value).digest)
end

#bin_to_hex(bytes) ⇒ Object



11
12
13
# File 'lib/trx/utils.rb', line 11

def bin_to_hex(bytes)
  RLP::Utils.encode_hex bytes
end

#hex_to_bin(string) ⇒ Object



7
8
9
# File 'lib/trx/utils.rb', line 7

def hex_to_bin(string)
  RLP::Utils.decode_hex remove_hex_prefix(string)
end

#is_hex?(str) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/trx/utils.rb', line 42

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

#keccak256(value) ⇒ Object



23
24
25
# File 'lib/trx/utils.rb', line 23

def keccak256(value)
  Digest::Keccak.new(256).digest(value)
end

#prefix_hex(hex) ⇒ Object



19
20
21
# File 'lib/trx/utils.rb', line 19

def prefix_hex(hex)
  hex.match(/\A0x/) ? hex : "0x#{hex}"
end

#remove_hex_prefix(string) ⇒ Object



15
16
17
# File 'lib/trx/utils.rb', line 15

def remove_hex_prefix(string)
  string[0, 2] == "0x" ? string[2..-1] : string
end

#sha256(value) ⇒ Object



35
36
37
38
39
40
# File 'lib/trx/utils.rb', line 35

def sha256(value)
  hash = OpenSSL::Digest.new("SHA256")
  hash.update(value)

  hash
end