Class: Ipfs::Multihash

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

Constant Summary collapse

FUNCTIONS =
[
  { name: :sha256, type_code: 0x12, digest_length: 0x20 }
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(multihash) ⇒ Multihash

Returns a new instance of Multihash.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/multihash.rb', line 12

def initialize(multihash)
  @base58_encoded = multihash
  @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

Instance Attribute Details

#digest_lengthObject (readonly)

Returns the value of attribute digest_length.



6
7
8
# File 'lib/multihash.rb', line 6

def digest_length
  @digest_length
end

#hash_func_typeObject (readonly)

Returns the value of attribute hash_func_type.



6
7
8
# File 'lib/multihash.rb', line 6

def hash_func_type
  @hash_func_type
end

Instance Method Details

#rawObject Also known as: to_s



33
34
35
# File 'lib/multihash.rb', line 33

def raw
  @base58_encoded
end

#to_bytesObject



27
28
29
30
31
# File 'lib/multihash.rb', line 27

def to_bytes
  [Base58.decode(@base58_encoded).to_s(16)]
    .pack('H*')
    .unpack('C*')
end