chainpoint 0.0.4 → 1.0.1

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.
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest'
4
+
5
+ module Chainpoint
6
+ class Hash
7
+ NUM_SERVERS = 3
8
+
9
+ attr_reader :hash, :proof_handles
10
+
11
+ def self.from_data(data)
12
+ new(Digest::SHA256.hexdigest(data))
13
+ end
14
+
15
+ def initialize(hash, proof_handles: [])
16
+ @hash = hash
17
+ @proof_handles =
18
+ proof_handles.map do |data|
19
+ ProofHandle.new(data[:uri], data[:node_hash_id])
20
+ end
21
+ end
22
+
23
+ def submit
24
+ @proof_handles = Chainpoint.select_nodes(NUM_SERVERS).flat_map do |uri|
25
+ post_hash(uri, hash)['hashes'].map do |hash|
26
+ Chainpoint::ProofHandle.new(uri, hash['hash_id_node'])
27
+ end
28
+ end
29
+
30
+ @proof_handles.map(&:to_h)
31
+ end
32
+
33
+ def proof(anchor_type = nil)
34
+ return nil unless @proof_handles.any?
35
+
36
+ proofs = @proof_handles.map(&:proof).compact
37
+
38
+ anchor_type ? proofs.find { |p| p.anchors_complete.include?(anchor_type) } : proofs.first
39
+ end
40
+
41
+ def to_h
42
+ {
43
+ hash: @hash,
44
+ proof_handles: @proof_handles.map(&:to_h)
45
+ }
46
+ end
47
+
48
+ private
49
+
50
+ def post_hash(uri, hash)
51
+ uri = URI(uri + '/hashes')
52
+ request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
53
+ request.body = { hashes: [hash] }.to_json
54
+
55
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
56
+ http.request(request)
57
+ end
58
+
59
+ JSON.parse(response.body)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'msgpack'
5
+ require 'zlib'
6
+
7
+ module Chainpoint
8
+ class Proof
9
+ attr_reader :proof, :hash_id_node, :anchors_complete
10
+
11
+ def initialize(proof, hash_id_node = nil, anchors_complete = [])
12
+ @proof = proof
13
+ @hash_id_node = hash_id_node
14
+ @anchors_complete = anchors_complete
15
+ end
16
+
17
+ def decode
18
+ MessagePack.unpack(
19
+ Zlib::Inflate.inflate(
20
+ Base64.decode64(@proof)
21
+ )
22
+ )
23
+ end
24
+
25
+ def verify
26
+ uri = URI(Chainpoint.select_nodes(1).first + '/verify')
27
+ request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
28
+ request.body = { proofs: [@proof] }.to_json
29
+
30
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
31
+ http.request(request)
32
+ end
33
+
34
+ JSON.parse(response.body).first
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Chainpoint
4
+ class ProofHandle
5
+ attr_reader :uri, :node_hash_id
6
+
7
+ def initialize(uri, node_hash_id)
8
+ @uri = uri
9
+ @node_hash_id = node_hash_id
10
+ end
11
+
12
+ def proof
13
+ data = request_proof
14
+ return unless data['proof']
15
+
16
+ Chainpoint::Proof.new(data['proof'], data['hash_id_node'], data['anchors_complete'])
17
+ end
18
+
19
+ def to_h
20
+ { uri: @uri, node_hash_id: @node_hash_id }
21
+ end
22
+
23
+ private
24
+
25
+ def request_proof
26
+ response = Net::HTTP.get_response(URI(@uri + '/proofs/' + @node_hash_id))
27
+ body = JSON.parse(response.body)
28
+ raise ArgumentError, body['message'] unless response.is_a? Net::HTTPSuccess
29
+
30
+ body.first
31
+ end
32
+ end
33
+ end
@@ -1,21 +1,21 @@
1
- =begin
2
- chainpoint_gem
3
- Copyright (C) 2019 Kenji Otsuka
1
+ # frozen_string_literal: true
4
2
 
5
- This program is free software: you can redistribute it and/or modify
6
- it under the terms of the GNU General Public License as published by
7
- the Free Software Foundation, either version 3 of the License, or
8
- (at your option) any later version.
3
+ # chainpoint_gem
4
+ # Copyright (C) 2019 Kenji Otsuka
5
+ #
6
+ # This program is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
9
18
 
10
- This program is distributed in the hope that it will be useful,
11
- but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- GNU General Public License for more details.
14
-
15
- You should have received a copy of the GNU General Public License
16
- along with this program. If not, see <http://www.gnu.org/licenses/>.
17
- =end
18
-
19
- class Chainpoint
20
- VERSION = "0.0.4"
19
+ module Chainpoint
20
+ VERSION = '1.0.1'
21
21
  end
data/lib/chainpoint.rb CHANGED
@@ -1,132 +1,37 @@
1
- =begin
2
- chainpoint_gem
3
- Copyright (C) 2019 Kenji Otsuka
4
-
5
- This program is free software: you can redistribute it and/or modify
6
- it under the terms of the GNU General Public License as published by
7
- the Free Software Foundation, either version 3 of the License, or
8
- (at your option) any later version.
9
-
10
- This program is distributed in the hope that it will be useful,
11
- but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- GNU General Public License for more details.
14
-
15
- You should have received a copy of the GNU General Public License
16
- along with this program. If not, see <http://www.gnu.org/licenses/>.
17
- =end
18
-
19
- require "chainpoint/version"
20
- require "net/https"
21
- require "json"
22
- require "digest"
23
-
24
- class Chainpoint
25
- def initialize(server_url = nil)
26
- if server_url.nil?
27
- @server_url = self.class.pickup_server
28
- return
29
- end
30
- @server_url = server_url
31
- end
32
-
33
- # Submit data into the chainpoint server.
34
- #
35
- # @param data [Object] data which you want to submit into the chainpoint server.
36
- def self.submit_data(data)
37
- self.new().submit_data(data)
38
- end
39
-
40
- # Submit data into the chainpoint server.
41
- #
42
- # @param data [Object] data which you want to submit into the chainpoint server.
43
- def submit_data(data)
44
- hash = Digest::SHA256.digest(data).unpack('H*')[0]
45
- return submit(hash)
46
- end
47
-
48
- # Submit hash string into the chainpoint server.
49
- #
50
- # @param data [String] hash String which you want to submit into the chainpoint server.
51
- def self.submit(hash)
52
- self.new().submit(hash)
53
- end
54
-
55
- # Submit a hash to a chainpoint server
56
- #
57
- # @param data [String] hash String which you want to submit into the chainpoint server.
58
- # @return [Array] An array of hashes containing the keys `hash`, `hash_id_node` and `uri`
59
- def submit(hash)
60
- uri = URI(@server_url + '/hashes')
61
- request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
62
- request.body = { hashes: [hash] }.to_json
63
- response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
64
- http.request(request)
65
- end
66
-
67
- hashes = JSON.parse(response.body)['hashes']
68
- hashes.map { |hash| hash.merge('uri' => @server_url) }
69
- end
70
-
71
- # Get proof data from the chainpoint server.
72
- #
73
- # @param hash_id_node [String] hash id node of the proof
74
- def self.get_proof(hash_id_node)
75
- self.new().get_proof(hash_id_node)
76
- end
77
-
78
- # Get proof data from the chainpoint server.
79
- #
80
- # @param hash_id_node [String] hash id node of the proof
81
- # @return [JSON]
82
- def get_proof(hash_id_node)
83
- uri = URI(@server_url + '/proofs/' + hash_id_node)
84
- r = Net::HTTP.get(uri)
85
- return JSON.parse(r)
1
+ # frozen_string_literal: true
2
+
3
+ # chainpoint_gem
4
+ # Copyright (C) 2019 Kenji Otsuka
5
+ #
6
+ # This program is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ require 'json'
20
+ require 'net/https'
21
+
22
+ require 'chainpoint/version'
23
+ require 'chainpoint/hash'
24
+ require 'chainpoint/proof'
25
+ require 'chainpoint/proof_handle'
26
+
27
+ module Chainpoint
28
+ NODE_LIST_ENDPOINTS = ['https://a.chainpoint.org/nodes/random',
29
+ 'https://b.chainpoint.org/nodes/random',
30
+ 'https://c.chainpoint.org/nodes/random'].freeze
31
+
32
+ def self.select_nodes(number)
33
+ node_list_uri = URI(NODE_LIST_ENDPOINTS.sample)
34
+ servers = JSON.parse(Net::HTTP.get(node_list_uri)).sample(number)
35
+ servers.map { |server| server['public_uri'] }
86
36
  end
87
-
88
- # Verify the proof data.
89
- #
90
- # @param proof [String] proof string
91
- def self.verify(proof)
92
- self.new().verify(proof)
93
- end
94
-
95
- # Verify the proof data.
96
- #
97
- # @param proof [String] proof string
98
- def verify(proof)
99
- uri = URI(@server_url + "/verify")
100
- req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
101
- req.body = {proofs: [proof]}.to_json
102
- res = Net::HTTP.start(
103
- uri.hostname, uri.port, use_ssl: uri.scheme == "https"
104
- ) do |http|
105
- http.request(req)
106
- end
107
- return JSON.parse(res.body)
108
- end
109
-
110
- private
111
- # Choose one server from the server list.
112
- #
113
- # @return [String] server URL.
114
- def self.pickup_server()
115
- uri = URI(pickup_node_list_server)
116
- r = Net::HTTP.get(uri)
117
- j = JSON.parse(r)
118
- return j[rand(j.length)]["public_uri"]
119
- end
120
-
121
- # Get one node list server URL randomely.
122
- #
123
- # @return [String] server URL.
124
- def self.pickup_node_list_server
125
- endpoint_array = [
126
- 'https://a.chainpoint.org/nodes/random',
127
- 'https://b.chainpoint.org/nodes/random',
128
- 'https://c.chainpoint.org/nodes/random'
129
- ]
130
- return endpoint_array[rand(endpoint_array.length)]
131
- end
132
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chainpoint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenji Ohtsuka
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: msgpack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -77,6 +91,7 @@ files:
77
91
  - ".idea/copyright/GPLv3_0.xml"
78
92
  - ".idea/copyright/profiles_settings.xml"
79
93
  - ".rspec"
94
+ - ".rubocop.yml"
80
95
  - ".travis.yml"
81
96
  - CONTRIBUTING.md
82
97
  - Gemfile
@@ -88,6 +103,9 @@ files:
88
103
  - bin/setup
89
104
  - chainpoint.gemspec
90
105
  - docs/Chainpoint.html
106
+ - docs/Chainpoint/Hash.html
107
+ - docs/Chainpoint/Proof.html
108
+ - docs/Chainpoint/ProofHandle.html
91
109
  - docs/_index.html
92
110
  - docs/class_list.html
93
111
  - docs/css/common.css
@@ -103,6 +121,9 @@ files:
103
121
  - docs/method_list.html
104
122
  - docs/top-level-namespace.html
105
123
  - lib/chainpoint.rb
124
+ - lib/chainpoint/hash.rb
125
+ - lib/chainpoint/proof.rb
126
+ - lib/chainpoint/proof_handle.rb
106
127
  - lib/chainpoint/version.rb
107
128
  homepage: https://github.com/KenjiOhtsuka/chainpoint_gem
108
129
  licenses: