class HexFormatter::Ipfs

Public Instance Methods

decode(data) click to toggle source
# File lib/hex_formatter.rb, line 19
def decode(data)
  payload = data.gsub("Qm", "")
  return "0x#{::Eth::Utils.bin_to_hex(::Base58.base58_to_binary(payload, :bitcoin))[2..-1]}"
end
encode(data) click to toggle source
# File lib/hex_formatter.rb, line 8
def encode(data)
  payload = data.gsub("0x", "").scan(/../).map(&:hex)
  raise ArgumentError if payload.length != 32
  if payload[0] > 60
    new_payload = "01" + data.gsub("0x", "")
  else
    new_payload = "02" + data.gsub("0x", "")
  end
  return "Qm#{::Base58.int_to_base58(new_payload.hex, :bitcoin)}"
end