Skip to main content

initializeZKStorage

Initializes a dedicated ZK storage account on a specified Anchor program, designed for permanently storing ZK-related data like commitments and Merkle roots.

This function derives a unique Program Derived Address (PDA) using a seedName (e.g., "device_1_data") and the authorityPubkey, then sends a transaction to create and initialize this account on the target program. This operation is typically performed only once per storage account.

Parameters

ParameterTypeDescription
privateKeyBase58const String&The private key of the authority who will own and pay for the account creation.
authorityPubkeyconst String&The public key of the authority, also used for PDA derivation.
programIdconst String&The ID of the Anchor program to interact with.
seedNameconst String&A unique seed string used for deriving the PDA (e.g., "device_data_pda").
outPDAString&Output: If successful, the Base58 address of the created storage account PDA.
outTxSignatureString&Output: If successful, the signature of the initialization transaction.

Return Value

  • bool: Returns true if the account was initialized successfully.

Example Code (main.cpp)

#include <Arduino.h>
#include <WiFi.h>
#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);

// Connect to WiFi
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=== initializeZKStorage Example ===");

String pdaSeed = "my_zk_storage_account"; // A unique name for this PDA
String pdaAddress;
String txSignature;

Serial.println("Initializing ZK storage account on the Anchor program...");

if (solana.initializeZKStorage(MY_PRIVATE_KEY_BASE58, MY_PUBLIC_KEY_BASE58, ZK_PROGRAM_ID, pdaSeed, pdaAddress, txSignature)) {
Serial.println("✅ Storage account initialized successfully!");
Serial.println(" -> PDA Address: " + pdaAddress);
Serial.println(" -> Transaction Signature: " + txSignature);
} else {
Serial.println("❌ Failed to initialize account (maybe it already exists?).");
}
}

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