Class: Tierion::HashApi::Receipt

Inherits:
Hash
  • Object
show all
Includes:
Hashie::Extensions::IndifferentAccess, Hashie::Extensions::MergeInitializer, Hashie::Extensions::MethodAccess
Defined in:
lib/tierion/hash_api/receipt.rb

Instance Method Summary collapse

Instance Method Details

#confirmationsObject



12
13
14
15
# File 'lib/tierion/hash_api/receipt.rb', line 12

def confirmations
  get_confirmations
  @confs
end

#to_pretty_jsonObject



8
9
10
# File 'lib/tierion/hash_api/receipt.rb', line 8

def to_pretty_json
  puts JSON.pretty_generate(self)
end

#valid?Boolean

Checks the validity of the Merkle tree proof and return true or false

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tierion/hash_api/receipt.rb', line 19

def valid?
  return false if targetHash.blank? || merkleRoot.blank?

  # No siblings, single item tree, so the hash
  # should also be the root
  return targetHash == merkleRoot if proof.empty?

  # The target hash (the hash the user submitted)
  # is always hashed in the first cycle through the
  # proofs. After that, the proof_hash value will
  # contain intermediate hashes.
  proof_hash = targetHash

  proof.each do |p|
    h = Digest::SHA256.new
    if p.key?('left')
      h.update hex2bin(p['left'])
      h.update hex2bin(proof_hash)
    elsif p.key?('right')
      h.update hex2bin(proof_hash)
      h.update hex2bin(p['right'])
    else
      return false
    end
    proof_hash = h.hexdigest
  end

  proof_hash == merkleRoot
end