ruby-ipfs-api 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/multihash.rb +27 -20
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 24d678460e6540e0cdb6ba0adb78059cf62cb57c
4
- data.tar.gz: 3654e0e6d008375a2a1c58ddd921dcb7992dbdeb
3
+ metadata.gz: 4e12832ab225871a45af126b0d9f44bfa8f62f45
4
+ data.tar.gz: 28ae276440460f49731372b1377a3cc1fcd0a401
5
5
  SHA512:
6
- metadata.gz: 68ebf7fc1d165de81ba34621dff8a403556fdb46e339dface000846c3d740c328d034ffdc2e6decdf6b98624a53a219e83d761d8729d6673a054a09295570627
7
- data.tar.gz: c9fa6d861c4f2bb36f5e1b9341c5cf28d9532b7253d5c271b5cd6a8b8ca2cbb4b88f64b73f257801a40783734ac98abe21b2add0850eb85bdd5c5d7654d8b0f3
6
+ metadata.gz: ed342f1e0f2a0c1e162d0096d2c9ca67632d9eb5ee339a53ea08eceb4931d2b871dc86d4f896771631f55d5b353c880e0dc35e6ad211054ec7ab93de736c53a8
7
+ data.tar.gz: ce0d390bf86ae14915e4cb22c5f01da2f9de0e4a0f2e65b2756833eca69a9b86e8b679a185149be55f030318cee0e9e4ffb2e92ca78ea129b1662f4ae98e4078
@@ -1,42 +1,49 @@
1
+ require_relative './base'
1
2
  require_relative './errors'
2
3
 
3
4
  module Ipfs
4
-
5
5
  class Multihash
6
- attr_reader :hash_func_type, :digest_length, :digest_value
6
+ attr_reader :hash_func_type, :digest_length
7
+
8
+ FUNCTIONS = [
9
+ { name: :sha256, type_code: 0x12, digest_length: 0x20 }
10
+ ]
11
+
12
+ def initialize(multihash)
13
+ @base58_encoded = multihash
14
+ @bytes_encoded = to_bytes
15
+
16
+ @function = find_hash_function(@bytes_encoded[0])
7
17
 
8
- DEFAULT_LENGTH = 46
9
- VALID_DIGEST_LENGTH = 'm'
10
- FUNCTION_TYPE_CODE = {
11
- sha256: 'Q'
12
- }
18
+ raise Error::InvalidMultihash, "The hash func type could not be found" if @function.nil?
13
19
 
14
- def initialize(hash)
15
- @hash_func_type = hash[0]
16
- @digest_length = hash[1]
17
- @digest_value = hash[2..-1]
20
+ @hash_func_type = @function[:name]
21
+ @digest_length = @function[:digest_length]
18
22
 
19
- raise Ipfs::Error::InvalidMultihash, "The hash '#{raw}' is invalid." unless valid?
23
+ raise Error::InvalidMultihash,
24
+ "The hash '#{@base58_encoded}' is invalid." unless correct_length?
25
+ end
26
+
27
+ def to_bytes
28
+ [Base58.decode(@base58_encoded).to_s(16)]
29
+ .pack('H*')
30
+ .unpack('C*')
20
31
  end
21
32
 
22
33
  def raw
23
- "#{@hash_func_type}#{@digest_length}#{@digest_value}"
34
+ @base58_encoded
24
35
  end
25
36
 
26
37
  alias to_s raw
27
38
 
28
39
  private
29
40
 
30
- def valid?
31
- correct_length? && encoded_digest?(:sha256)
41
+ def find_hash_function(func_type_code)
42
+ FUNCTIONS.find { |function| function[:type_code] == func_type_code }
32
43
  end
33
44
 
34
45
  def correct_length?
35
- raw.length == DEFAULT_LENGTH
36
- end
37
-
38
- def encoded_digest?(encoding)
39
- @hash_func_type == FUNCTION_TYPE_CODE[encoding]
46
+ @digest_length == @bytes_encoded[2..-1].length
40
47
  end
41
48
  end
42
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-ipfs-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Benett