Class: Chainpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/chainpoint/version.rb,
lib/chainpoint.rb

Overview

chainpoint_gem

Copyright (C) 2019 Kenji Otsuka

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_url = nil) ⇒ Chainpoint

Returns a new instance of Chainpoint.



25
26
27
28
29
30
31
# File 'lib/chainpoint.rb', line 25

def initialize(server_url = nil)
  if server_url.nil?
    @server_url = self.class.pickup_server
    return
  end
  @server_url = server_url
end

Class Method Details

.get_proof(hash_id_node) ⇒ Object

Get proof data from the chainpoint server.

Parameters:

  • hash_id_node (String)

    hash id node of the proof



74
75
76
# File 'lib/chainpoint.rb', line 74

def self.get_proof(hash_id_node)
  self.new().get_proof(hash_id_node)
end

.pickup_node_list_serverString

Get one node list server URL randomely.

Returns:

  • (String)

    server URL.



124
125
126
127
128
129
130
131
# File 'lib/chainpoint.rb', line 124

def self.pickup_node_list_server
  endpoint_array = [
    'https://a.chainpoint.org/nodes/random',
    'https://b.chainpoint.org/nodes/random',
    'https://c.chainpoint.org/nodes/random'
  ]
  return endpoint_array[rand(endpoint_array.length)]
end

.pickup_serverString

Choose one server from the server list.

Returns:

  • (String)

    server URL.



114
115
116
117
118
119
# File 'lib/chainpoint.rb', line 114

def self.pickup_server()
  uri = URI(pickup_node_list_server)
  r = Net::HTTP.get(uri)
  j = JSON.parse(r)
  return j[rand(j.length)]["public_uri"]
end

.submit(hash) ⇒ Object

Submit hash string into the chainpoint server.

Parameters:

  • data (String)

    hash String which you want to submit into the chainpoint server.



51
52
53
# File 'lib/chainpoint.rb', line 51

def self.submit(hash)
  self.new().submit(hash)
end

.submit_data(data) ⇒ Object

Submit data into the chainpoint server.

Parameters:

  • data (Object)

    data which you want to submit into the chainpoint server.



36
37
38
# File 'lib/chainpoint.rb', line 36

def self.submit_data(data)
  self.new().submit_data(data)
end

.verify(proof) ⇒ Object

Verify the proof data.

Parameters:

  • proof (String)

    proof string



91
92
93
# File 'lib/chainpoint.rb', line 91

def self.verify(proof)
  self.new().verify(proof)
end

Instance Method Details

#get_proof(hash_id_node) ⇒ JSON

Get proof data from the chainpoint server.

Parameters:

  • hash_id_node (String)

    hash id node of the proof

Returns:

  • (JSON)


82
83
84
85
86
# File 'lib/chainpoint.rb', line 82

def get_proof(hash_id_node)
  uri = URI(@server_url + '/proofs/' + hash_id_node)
  r = Net::HTTP.get(uri)
  return JSON.parse(r)
end

#submit(hash) ⇒ Array

Submit a hash to a chainpoint server

Parameters:

  • data (String)

    hash String which you want to submit into the chainpoint server.

Returns:

  • (Array)

    An array of hashes containing the keys hash, hash_id_node and uri



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chainpoint.rb', line 59

def submit(hash)
  uri = URI(@server_url + '/hashes')
  request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
  request.body = { hashes: [hash] }.to_json
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(request)
  end

  hashes = JSON.parse(response.body)['hashes']
  hashes.map { |hash| hash.merge('uri' => @server_url) }
end

#submit_data(data) ⇒ Object

Submit data into the chainpoint server.

Parameters:

  • data (Object)

    data which you want to submit into the chainpoint server.



43
44
45
46
# File 'lib/chainpoint.rb', line 43

def submit_data(data)
  hash = Digest::SHA256.digest(data).unpack('H*')[0]
  return submit(hash)
end

#verify(proof) ⇒ Object

Verify the proof data.

Parameters:

  • proof (String)

    proof string



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chainpoint.rb', line 98

def verify(proof)
  uri = URI(@server_url + "/verify")
  req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
  req.body = {proofs: [proof]}.to_json
  res = Net::HTTP.start(
    uri.hostname, uri.port, use_ssl: uri.scheme == "https"
  ) do |http|
    http.request(req)
  end
  return JSON.parse(res.body)
end