Class: KairosMcp::KairosChain::MerkleTree
- Inherits:
-
Object
- Object
- KairosMcp::KairosChain::MerkleTree
- Defined in:
- lib/kairos_mcp/kairos_chain/merkle_tree.rb
Instance Attribute Summary collapse
-
#leaves ⇒ Object
readonly
Returns the value of attribute leaves.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Class Method Summary collapse
-
.verify(root, data, proof) ⇒ Object
Verify a proof.
Instance Method Summary collapse
- #calculate_root(hashes) ⇒ Object
-
#get_proof(data) ⇒ Object
Generate Merkle Proof for a specific data item.
-
#initialize(data) ⇒ MerkleTree
constructor
A new instance of MerkleTree.
Constructor Details
#initialize(data) ⇒ MerkleTree
Returns a new instance of MerkleTree.
8 9 10 11 |
# File 'lib/kairos_mcp/kairos_chain/merkle_tree.rb', line 8 def initialize(data) @leaves = data.map { |d| Digest::SHA256.hexdigest(d) } @root = calculate_root(@leaves) end |
Instance Attribute Details
#leaves ⇒ Object (readonly)
Returns the value of attribute leaves.
6 7 8 |
# File 'lib/kairos_mcp/kairos_chain/merkle_tree.rb', line 6 def leaves @leaves end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
6 7 8 |
# File 'lib/kairos_mcp/kairos_chain/merkle_tree.rb', line 6 def root @root end |
Class Method Details
.verify(root, data, proof) ⇒ Object
Verify a proof
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/kairos_mcp/kairos_chain/merkle_tree.rb', line 73 def self.verify(root, data, proof) current_hash = Digest::SHA256.hexdigest(data) proof.each do |node| if node[:position] == 'left' current_hash = Digest::SHA256.hexdigest(node[:data] + current_hash) else current_hash = Digest::SHA256.hexdigest(current_hash + node[:data]) end end current_hash == root end |
Instance Method Details
#calculate_root(hashes) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/kairos_mcp/kairos_chain/merkle_tree.rb', line 13 def calculate_root(hashes) return "" if hashes.empty? return hashes.first if hashes.length == 1 next_level = [] hashes.each_slice(2) do |left, right| if right combined = left + right next_level << Digest::SHA256.hexdigest(combined) else # If odd number of leaves, duplicate the last one combined = left + left next_level << Digest::SHA256.hexdigest(combined) end end calculate_root(next_level) end |
#get_proof(data) ⇒ Object
Generate Merkle Proof for a specific data item
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/kairos_mcp/kairos_chain/merkle_tree.rb', line 34 def get_proof(data) target_hash = Digest::SHA256.hexdigest(data) index = @leaves.index(target_hash) return nil unless index proof = [] current_hashes = @leaves while current_hashes.length > 1 level_proof = [] next_level = [] current_hashes.each_slice(2) do |left, right| if right # Pair found if left == target_hash proof << { position: 'right', data: right } target_hash = Digest::SHA256.hexdigest(left + right) elsif right == target_hash proof << { position: 'left', data: left } target_hash = Digest::SHA256.hexdigest(left + right) end next_level << Digest::SHA256.hexdigest(left + right) else # Odd number, duplicate last if left == target_hash proof << { position: 'right', data: left } target_hash = Digest::SHA256.hexdigest(left + left) end next_level << Digest::SHA256.hexdigest(left + left) end end current_hashes = next_level end proof end |