Skip to main content

storeMerkleRootInAnchor

Stores a Merkle root hash (merkleRoot), calculated using buildMerkleTree, and the number of leaves (leafCount) in the tree into the ZK storage account (PDA) on the Anchor program.

This is an efficient way to commit the integrity of a batch of data (e.g., a day's worth of sensor readings) to the blockchain using a single hash. Later, individual data points from that day can be proven to belong to this root using verifyMerkleProof.

Parameters

ParameterTypeDescription
privateKeyBase58const String&The private key of the account's authority.
authorityPubkeyconst String&The public key of the authority.
programIdconst String&The ID of the Anchor program to interact with.
seedNameconst String&The seed used to find the target PDA storage account.
merkleRootconst String&The Merkle root hash to store.
leafCountuint32_tThe total number of leaves (data items) in the Merkle tree.
metadataconst String&Metadata string to store alongside the Merkle root.
outTxSignatureString&Output: If successful, the signature of the transaction.

Return Value

  • bool: Returns true if the data was stored successfully.

Example Code (main.cpp)

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

// --- WiFi Settings ---
const char* WIFI_SSID = "YOUR_WIFI_SSID";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";

// --- Solana Settings ---
const String SOLANA_RPC_URL = "[https://api.devnet.solana.com](https://api.devnet.solana.com)";
const String MY_PRIVATE_KEY_BASE58 = "YOUR_PRIVATE_KEY_BASE58";
const String MY_PUBLIC_KEY_BASE58 = "YOUR_PUBLIC_KEY_BASE58";
const String ZK_PROGRAM_ID = "YOUR_ANCHOR_PROGRAM_ID_HERE"; // Your Anchor program ID

Infratic solana(SOLANA_RPC_URL);

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

// 1. (Offline) First, build the Merkle tree
Serial.println("Building Merkle tree...");
std::vector<String> dataList;
dataList.push_back("DailyData_001");
dataList.push_back("DailyData_002");
dataList.push_back("DailyData_003");
dataList.push_back("DailyData_004");

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

// 2. (Online) Connect to WiFi and send on-chain
Serial.print("Connecting to WiFi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n✅ WiFi connected!");

Serial.println("\n=== storeMerkleRootInAnchor Example ===");
String pdaSeed = "my_merkle_storage";
uint32_t leafCount = dataList.size();
String metadata = "Daily sensor readings 10/10/2025";
String txSignature;

Serial.println("Storing Merkle root in the Anchor program...");

if (solana.storeMerkleRootInAnchor(MY_PRIVATE_KEY_BASE58, MY_PUBLIC_KEY_BASE58, ZK_PROGRAM_ID, pdaSeed, merkleRoot, leafCount, metadata, txSignature)) {
Serial.println("✅ Merkle root successfully stored in Anchor program!");
Serial.println(" -> Transaction Signature: " + txSignature);
} else {
Serial.println("❌ Failed to store Merkle root.");
}
}

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