Skip to main content

verifyMerkleProof

Verifies if a specific data item (data) is valid according to a provided Merkle proof (proof) and its associated Merkle root (often contained within the proof structure).

This function cryptographically answers the question: "Was this data item genuinely part of the dataset represented by this Merkle root?"

Parameters

ParameterTypeDescription
dataconst String&The data item whose validity is being checked.
proofconst MerkleProof&The proof structure generated by createMerkleProof from the original list.

Return Value

  • bool: Returns true if the proof is valid, false if it's invalid (meaning the data was tampered with or the proof is incorrect).

Example Code (main.cpp)

#include <Arduino.h>
#include <vector>
#include "Infratic-lib.h" // Assume MerkleProof struct is defined here

// This function does not require network connectivity.
Infratic solana(""); // RPC URL not needed

void setup() {
Serial.begin(115200);
delay(1000);

Serial.println("\n=== verifyMerkleProof Example ===");

// 1. First, generate the root and the proof
std::vector<String> dataList;
dataList.push_back("SensorData:10.5");
dataList.push_back("SensorData:11.2");
dataList.push_back("SensorData:10.9");
dataList.push_back("SensorData:12.1");

String merkleRoot;
if (!solana.buildMerkleTree(dataList, merkleRoot)) {
Serial.println("❌ Test Failed: Could not build Merkle tree first.");
return;
}

size_t dataIndexToProve = 2; // "SensorData:10.9"
String dataToVerify = dataList[dataIndexToProve];
MerkleProof proof;

if (!solana.createMerkleProof(dataList, dataIndexToProve, proof)) {
Serial.println("❌ Test Failed: Could not create Merkle proof.");
return;
}

// (Assumption: If proof struct doesn't contain root, we need to add it manually)
// proof.root = merkleRoot; // This depends on your library's design.

// 2. Successful Verification
Serial.println("Test 1: Verifying with original data...");
if (solana.verifyMerkleProof(dataToVerify, proof)) {
Serial.println("✅ SUCCESS: Data verified with Merkle proof!");
} else {
Serial.println("❌ ERROR: Verification failed.");
}

// 3. Failed Verification (Tampered Data)
Serial.println("\nTest 2: Verifying with tampered data...");
String tamperedData = "SensorData:99.9"; // Data was changed
if (solana.verifyMerkleProof(tamperedData, proof)) {
Serial.println("❌ ERROR: Tampered data should not have verified!");
} else {
Serial.println("✅ SUCCESS: Tampered data was correctly rejected.");
}
}

void loop() {
// Can be left empty
}