Class: Hashtastic::MerkleTree

Inherits:
Object
  • Object
show all
Defined in:
lib/hashtastic/merkle_tree/merkle_tree.rb,
lib/hashtastic/merkle_tree/layers_creator.rb

Defined Under Namespace

Classes: LayersCreator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(leafs, hashfunc = EthereumSHA3) ⇒ MerkleTree

Returns a new instance of MerkleTree.



7
8
9
10
11
# File 'lib/hashtastic/merkle_tree/merkle_tree.rb', line 7

def initialize(leafs, hashfunc = EthereumSHA3)
  @hashfunc = hashfunc
  @leafs = leafs.sort.uniq
  @layers = LayersCreator.call(@leafs, hashfunc)
end

Instance Attribute Details

#layersObject (readonly)

Returns the value of attribute layers.



5
6
7
# File 'lib/hashtastic/merkle_tree/merkle_tree.rb', line 5

def layers
  @layers
end

#leafsObject (readonly)

Returns the value of attribute leafs.



5
6
7
# File 'lib/hashtastic/merkle_tree/merkle_tree.rb', line 5

def leafs
  @leafs
end

Instance Method Details

#pair_element(index, layer) ⇒ Object



30
31
32
33
34
# File 'lib/hashtastic/merkle_tree/merkle_tree.rb', line 30

def pair_element(index, layer)
  pair_index = index.even? ? index + 1 : index - 1

  return layer[pair_index] if pair_index < layer.length
end

#proof(leaf) ⇒ Object

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hashtastic/merkle_tree/merkle_tree.rb', line 17

def proof(leaf)
  index = leafs.index(leaf)
  raise LeafNotFoundError, leaf unless index

  layers.each_with_object([]) do |layer, accumulator|
    pair_element = pair_element(index, layer)

    accumulator.push(pair_element) if pair_element

    index = (index / 2).floor
  end
end

#rootObject



13
14
15
# File 'lib/hashtastic/merkle_tree/merkle_tree.rb', line 13

def root
  layers[layers.length - 1][0]
end

#verify(proof:, root:, leaf:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/hashtastic/merkle_tree/merkle_tree.rb', line 36

def verify(proof:, root:, leaf:)
  computed_hash =
    proof.reduce(leaf) do |acc, element|
      combined = [acc, element].map { |h| Utils.hex_to_ascii(h) }.sort.join

      @hashfunc.call(combined)
    end

  computed_hash == root
end