class Hashtastic::MerkleTree::LayersCreator

Public Class Methods

call(leafs, hashfunc = EthereumSHA3) click to toggle source
# File lib/hashtastic/merkle_tree/layers_creator.rb, line 7
def call(leafs, hashfunc = EthereumSHA3)
  @hashfunc = hashfunc
  @leafs = leafs

  return [['']] if @leafs.empty?

  layers = []
  layers.push(@leafs)

  while layers[layers.length - 1].length > 1
    layers.push(next_layer(layers[layers.length - 1]))
  end

  layers
end

Private Class Methods

combine_hash(first, second) click to toggle source
# File lib/hashtastic/merkle_tree/layers_creator.rb, line 25
def combine_hash(first, second)
  return first if second.nil?
  @hashfunc.call(sort_and_concat(first, second))
end
next_layer(leafs) click to toggle source
# File lib/hashtastic/merkle_tree/layers_creator.rb, line 30
def next_layer(leafs)
  leafs.each_with_object([]).with_index do |(leaf, arr), index|
    arr << combine_hash(leaf, leafs[index + 1]) if index.even?
  end
end
sort_and_concat(first, second) click to toggle source
# File lib/hashtastic/merkle_tree/layers_creator.rb, line 36
def sort_and_concat(first, second)
  [first, second].map { |leaf| Utils.hex_to_ascii(leaf) }.sort.join
end