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.
- checksums.yaml +4 -4
- data/lib/multihash.rb +27 -20
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e12832ab225871a45af126b0d9f44bfa8f62f45
|
4
|
+
data.tar.gz: 28ae276440460f49731372b1377a3cc1fcd0a401
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed342f1e0f2a0c1e162d0096d2c9ca67632d9eb5ee339a53ea08eceb4931d2b871dc86d4f896771631f55d5b353c880e0dc35e6ad211054ec7ab93de736c53a8
|
7
|
+
data.tar.gz: ce0d390bf86ae14915e4cb22c5f01da2f9de0e4a0f2e65b2756833eca69a9b86e8b679a185149be55f030318cee0e9e4ffb2e92ca78ea129b1662f4ae98e4078
|
data/lib/multihash.rb
CHANGED
@@ -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
|
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
|
-
|
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
|
-
|
15
|
-
@
|
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
|
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
|
-
|
34
|
+
@base58_encoded
|
24
35
|
end
|
25
36
|
|
26
37
|
alias to_s raw
|
27
38
|
|
28
39
|
private
|
29
40
|
|
30
|
-
def
|
31
|
-
|
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
|
-
|
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
|