verifyProof method

bool verifyProof(
  1. T data,
  2. MerkleProof<T> proof
)

Verifies a Merkle proof

Implementation

bool verifyProof(T data, MerkleProof<T> proof) {
  if (proof.leafIndex < 0 || proof.leafIndex >= proof.totalLeaves) {
    return false;
  }

  final leafHash = _computeHash(data);
  String currentHash = leafHash;

  for (int i = 0; i < proof.hashes.length; i++) {
    final siblingHash = proof.hashes[i];
    final isRight = proof.directions[i];

    if (isRight) {
      currentHash = _combineHashes(currentHash, siblingHash);
    } else {
      currentHash = _combineHashes(siblingHash, currentHash);
    }
  }

  return currentHash == root?.hash;
}