Class: Hashtastic::MerkleTree
- Inherits:
-
Object
- Object
- Hashtastic::MerkleTree
- 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
-
#layers ⇒ Object
readonly
Returns the value of attribute layers.
-
#leafs ⇒ Object
readonly
Returns the value of attribute leafs.
Instance Method Summary collapse
-
#initialize(leafs, hashfunc = EthereumSHA3) ⇒ MerkleTree
constructor
A new instance of MerkleTree.
- #pair_element(index, layer) ⇒ Object
- #proof(leaf) ⇒ Object
- #root ⇒ Object
- #verify(proof:, root:, leaf:) ⇒ Object
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
#layers ⇒ Object (readonly)
Returns the value of attribute layers.
5 6 7 |
# File 'lib/hashtastic/merkle_tree/merkle_tree.rb', line 5 def layers @layers end |
#leafs ⇒ Object (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
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 |
#root ⇒ Object
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 |