Skip to main content

buildMerkleTree

Constructs a Merkle tree from a list of data items (e.g., all sensor readings within a day) and calculates the root hash (outRoot) of the tree.

The Merkle root is a single, unique cryptographic hash that represents the integrity of the entire data list. If even a single character in the data list changes, the Merkle root will be completely different. This is used for efficiently verifying large datasets.

Parameters

ParameterTypeDescription
dataListconst std::vector<String>&A list of data items (strings) to build the tree from.
outRootString&Output: The calculated Merkle root hash (hex format).

Return Value

  • bool: Returns true if the tree was built successfully, false otherwise.

Example Code (main.cpp)

#include <Arduino.h>
#include <vector>
#include "Infratic-lib.h"

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

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

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

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");

Serial.println("Building Merkle tree for data list:");
for (size_t i = 0; i < dataList.size(); ++i) {
Serial.println(" - " + dataList[i]);
}

String merkleRoot;
if (solana.buildMerkleTree(dataList, merkleRoot)) {
Serial.println("\n✅ Merkle tree built successfully!");
Serial.println(" -> Merkle Root: " + merkleRoot);
Serial.println(" -> This root hash represents all 4 data items.");
} else {
Serial.println("❌ Failed to build Merkle tree.");
}
}

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