Class: IpfsFormatter::Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/ipfs_formatter.rb

Class Method Summary collapse

Class Method Details

.decode(data) ⇒ Object



19
20
21
22
# File 'lib/ipfs_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) ⇒ Object

Raises:



8
9
10
11
12
13
14
15
16
17
# File 'lib/ipfs_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