class Ipfs::Multihash

Constants

FUNCTIONS

Attributes

digest_length[R]
hash_func_type[R]

Public Class Methods

new(multihash) click to toggle source
# File lib/ruby-ipfs-http-client/multihash.rb, line 12
def initialize(multihash)
  @base58_encoded = multihash

  raise Error::InvalidMultihash,
        "The hash '#{@base58_encoded}' is invalid." unless @base58_encoded.is_a?(String)

  @bytes_encoded = to_bytes

  @function = find_hash_function(@bytes_encoded[0])

  raise Error::InvalidMultihash, "The hash func type could not be found" if @function.nil?

  @hash_func_type = @function[:name]
  @digest_length = @function[:digest_length]

  raise Error::InvalidMultihash,
        "The hash '#{@base58_encoded}' is invalid." unless correct_length?
end

Public Instance Methods

raw() click to toggle source
# File lib/ruby-ipfs-http-client/multihash.rb, line 37
def raw
  @base58_encoded
end
Also aliased as: to_s
to_bytes() click to toggle source
# File lib/ruby-ipfs-http-client/multihash.rb, line 31
def to_bytes
  [Base58.decode(@base58_encoded).to_s(16)]
    .pack('H*')
    .unpack('C*')
end
to_s()
Alias for: raw

Private Instance Methods

correct_length?() click to toggle source
# File lib/ruby-ipfs-http-client/multihash.rb, line 49
def correct_length?
  @digest_length == @bytes_encoded[2..-1].length
end
find_hash_function(func_type_code) click to toggle source
# File lib/ruby-ipfs-http-client/multihash.rb, line 45
def find_hash_function(func_type_code)
  FUNCTIONS.find { |function| function[:type_code] == func_type_code }
end