Files
Shadowbroker/backend/services/mesh/mesh_merkle.py
T
anoracleofra-code 668ce16dc7 v0.9.6: InfoNet hashchain, Wormhole gate encryption, mesh reputation, 16 community contributors
Gate messages now propagate via the Infonet hashchain as encrypted blobs — every node syncs them
through normal chain sync while only Gate members with MLS keys can decrypt. Added mesh reputation
system, peer push workers, voluntary Wormhole opt-in for node participation, fork recovery,
killwormhole scripts, obfuscated terminology, and hardened the self-updater to protect encryption
keys and chain state during updates.

New features: Shodan search, train tracking, Sentinel Hub imagery, 8 new intelligence layers,
CCTV expansion to 11,000+ cameras across 6 countries, Mesh Terminal CLI, prediction markets,
desktop-shell scaffold, and comprehensive mesh test suite (215 frontend + backend tests passing).

Community contributors: @wa1id, @AlborzNazari, @adust09, @Xpirix, @imqdcr, @csysp, @suranyami,
@chr0n1x, @johan-martensson, @singularfailure, @smithbh, @OrfeoTerkuci, @deuza, @tm-const,
@Elhard1, @ttulttul
2026-03-26 05:58:04 -06:00

75 lines
2.0 KiB
Python

from __future__ import annotations
import hashlib
from typing import Any
def _hash_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def hash_leaf(value: str) -> str:
return _hash_bytes(value.encode("utf-8"))
def hash_pair(left: str, right: str) -> str:
return _hash_bytes(f"{left}{right}".encode("utf-8"))
def build_merkle_levels(leaves: list[str]) -> list[list[str]]:
if not leaves:
return []
level = [hash_leaf(leaf) for leaf in leaves]
levels = [level]
while len(level) > 1:
next_level: list[str] = []
for idx in range(0, len(level), 2):
left = level[idx]
right = level[idx + 1] if idx + 1 < len(level) else left
next_level.append(hash_pair(left, right))
level = next_level
levels.append(level)
return levels
def merkle_root(leaves: list[str]) -> str:
levels = build_merkle_levels(leaves)
if not levels:
return ""
return levels[-1][0]
def merkle_proof_from_levels(levels: list[list[str]], index: int) -> list[dict[str, Any]]:
if not levels:
return []
if index < 0 or index >= len(levels[0]):
return []
proof: list[dict[str, Any]] = []
idx = index
for level in levels[:-1]:
is_right = idx % 2 == 1
sibling_idx = idx - 1 if is_right else idx + 1
if sibling_idx >= len(level):
sibling_hash = level[idx]
else:
sibling_hash = level[sibling_idx]
proof.append({"hash": sibling_hash, "side": "left" if is_right else "right"})
idx //= 2
return proof
def verify_merkle_proof(
leaf_value: str, index: int, proof: list[dict[str, Any]], root: str
) -> bool:
current = hash_leaf(leaf_value)
idx = index
for step in proof:
sibling = str(step.get("hash", ""))
side = str(step.get("side", "right")).lower()
if side == "left":
current = hash_pair(sibling, current)
else:
current = hash_pair(current, sibling)
idx //= 2
return current == root