Class: Chainpoint::Hash

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

Constant Summary collapse

NUM_SERVERS =
3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, proof_handles: []) ⇒ Hash

Returns a new instance of Hash



15
16
17
18
19
20
21
# File 'lib/chainpoint/hash.rb', line 15

def initialize(hash, proof_handles: [])
  @hash = hash
  @proof_handles =
    proof_handles.map do |data|
      ProofHandle.new(data[:uri], data[:node_hash_id])
    end
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash



9
10
11
# File 'lib/chainpoint/hash.rb', line 9

def hash
  @hash
end

#proof_handlesObject (readonly)

Returns the value of attribute proof_handles



9
10
11
# File 'lib/chainpoint/hash.rb', line 9

def proof_handles
  @proof_handles
end

Class Method Details

.from_data(data) ⇒ Object



11
12
13
# File 'lib/chainpoint/hash.rb', line 11

def self.from_data(data)
  new(Digest::SHA256.hexdigest(data))
end

Instance Method Details

#proof(anchor_type = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/chainpoint/hash.rb', line 33

def proof(anchor_type = nil)
  return nil unless @proof_handles.any?

  proofs = @proof_handles.map(&:proof).compact

  anchor_type ? proofs.find { |p| p.anchors_complete.include?(anchor_type) } : proofs.first
end

#submitObject



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

def submit
  @proof_handles = Chainpoint.select_nodes(NUM_SERVERS).flat_map do |uri|
    post_hash(uri, hash)['hashes'].map do |hash|
      Chainpoint::ProofHandle.new(uri, hash['hash_id_node'])
    end
  end

  @proof_handles.map(&:to_h)
end

#to_hObject



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

def to_h
  {
    hash: @hash,
    proof_handles: @proof_handles.map(&:to_h)
  }
end