getValidityProof

Retrieve a single ZK Proof used by the compression program to verify that the given accounts are valid and the new addresses can be created. RPC method guide with use cases, tips and examples.

ThegetValidityProof RPC method generates zero-knowledge proofs to verify that the given accounts are valid or the new addresses can be created. This proof is required for any operation on compressed accounts (transfer, approve, decompress, etc.) for on-chain verification of compressed state.

You can test this method via the OpenAPI example or custom examples below.

  • Proof limits per request are:

    • hashes: 1, 2, 3, 4, or 8

    • newAddressesWithTrees : 1, 2

  • The newAddresses param field is supported but deprecated. Please use newAddressesWithTreesinstead.

post
Body
idstring · enumRequired

An ID to identify the request.

Possible values:
jsonrpcstring · enumRequired

The version of the JSON-RPC protocol.

Possible values:
methodstring · enumRequired

The name of the method to invoke.

Possible values:
Responses
200Success
application/json
post
POST / HTTP/1.1
Host: mainnet.helius-rpc.com
Content-Type: application/json
Accept: */*
Content-Length: 264

{
  "id": "test-account",
  "jsonrpc": "2.0",
  "method": "getValidityProof",
  "params": {
    "hashes": [
      "11111112cMQwSC9qirWGjZM6gLGwW69X22mqwLLGP"
    ],
    "newAddressesWithTrees": [
      {
        "address": "11111118eRTi4fUVRoeYEeeTyL4DPAwxatvWT5q1Z",
        "tree": "11111118eRTi4fUVRoeYEeeTyL4DPAwxatvWT5q1Z"
      }
    ]
  }
}
{
  "context": {
    "slot": 100
  },
  "value": {
    "compressedProof": {
      "a": "binary",
      "b": "binary",
      "c": "binary"
    },
    "leafIndices": [
      1
    ],
    "leaves": [
      "text"
    ],
    "merkleTrees": [
      "text"
    ],
    "rootIndices": [
      1
    ],
    "roots": [
      "text"
    ]
  }
}

Common Use Cases

  • Token Transfers: Generate proofs required for transferring compressed tokens.

  • Account Operations: Create proofs needed for any compressed account modification.

  • Batch Processing: Generate proofs for multiple accounts in a single call.

  • State Tree Verification: Prove account inclusion in the current state tree.

  • Transaction Building: Obtain proof data needed for compressed transaction instructions.

  • Program Integration: Get validity proofs for custom program operations on compressed accounts.

Parameters

  1. hashes (BN254[], required): Array of BN254 objects representing compressed account hashes to generate proofs for.

  2. newAddresses (BN254[], optional): Array of BN254 objects representing new addresses to include in the proof for address tree verification.

Response

The response contains ValidityProofWithContext data:

  • compressedProof (ValidityProof | null): The compressed validity proof object for zero-knowledge verification

  • roots (BN[]): Array of merkle tree roots used in proof generation

  • rootIndices (number[]): Array of indices of the roots in the state tree

  • leafIndices (number[]): Array of indices of the leaves being proven

  • leaves (BN[]): Array of leaf values in the merkle tree

  • treeInfos (TreeInfo[]): Array of information about the state trees used

  • proveByIndices (boolean[]): Array indicating whether to prove by indices for each element

Developer Tips

  • Fetch Accounts First: Always get compressed accounts before generating proofs - you need existing account hashes

  • Batch Processing: You can generate proofs for multiple accounts in a single call. Allowed counts: 1, 2, 3, 4, or 8 accounts

  • Proof Usage: The proof is required for any operation that modifies compressed accounts (transfers, burns, etc.)

  • Caching: Validity proofs are only valid for the current state - don't cache them for long periods

  • Error Handling: If accounts don't exist or have been modified, proof generation will fail

  • Integration: Use proof.compressedProof and proof.rootIndices when building transactions

  • State Changes: After any transaction, previously generated proofs become invalid for those accounts

Examples

The below examples work - just make sure you installed the dependencies.

Dependencies & Setup
npm install @lightprotocol/stateless.js @solana/web3.js

Example: Generate Validity Proof

Generate a validity proof for compressed accounts.

import { Rpc, createRpc, bn } from '@lightprotocol/stateless.js';
import { PublicKey } from '@solana/web3.js';

async function generateValidityProof(): Promise<void> {
  const connection: Rpc = createRpc(
    'https://devnet.helius-rpc.com/?api-key=YOUR_API_KEY',
    'https://devnet.helius-rpc.com/?api-key=YOUR_API_KEY'
  );

  try {
    // Get compressed accounts
    const owner = new PublicKey('OWNER_PUBLIC_KEY_HERE');
    const accounts = await connection.getCompressedAccountsByOwner(owner);
    
    if (accounts.items.length === 0) {
      console.log('No compressed accounts found');
      return;
    }

    // Generate validity proof
    const hashes = accounts.items.slice(0, 2).map(acc => bn(acc.hash));
    const validityProof = await connection.getValidityProof(hashes);

    console.log('Validity Proof Generated:');
    console.log(`  Accounts: ${hashes.length}`);
    console.log(`  Roots: ${validityProof.roots.length}`);
    console.log(`  Root Indices: ${validityProof.rootIndices.length} elements`);
    console.log(`  Leaf Indices: ${validityProof.leafIndices.length} elements`);
    
    // Ready for use in transactions
    console.log('\nProof ready for compressed transactions');
    
  } catch (error: unknown) {
    console.error('Error generating validity proof:', error);
  }
}

generateValidityProof();

Example: Proof Generation for Token Transfer

Generate a validity proof for a compressed token transfer operation:

import { Rpc, createRpc, bn } from '@lightprotocol/stateless.js';
import { PublicKey } from '@solana/web3.js';

async function generateProofForTransfer() {
  const connection = createRpc(
    'https://devnet.helius-rpc.com/?api-key=YOUR_API_KEY',
    'https://devnet.helius-rpc.com/?api-key=YOUR_API_KEY'
  );

  try {
    // Get compressed token accounts
    const owner = new PublicKey('OWNER_PUBLIC_KEY_HERE');
    const mint = new PublicKey('TOKEN_MINT_HERE');
    
    const tokenAccounts = await connection.getCompressedTokenAccountsByOwner(
      owner,
      { mint }
    );
    
    if (tokenAccounts.items.length === 0) {
      console.log('No compressed token accounts found');
      return;
    }

    // Generate proof for the token accounts (limit to allowed numbers: 1, 2, 3, 4, or 8)
    const limitedAccounts = tokenAccounts.items.slice(0, 4);
    const hashes = limitedAccounts.map(acc => bn(acc.compressedAccount.hash));
    const validityProof = await connection.getValidityProof(hashes);

    console.log('Validity Proof for Transfer:');
    console.log(`  Token accounts: ${hashes.length}`);
    console.log(`  Roots: ${validityProof.roots.length}`);
    
    // This proof is now ready to use with CompressedTokenProgram.transfer()
    console.log('\nProof ready for token transfer instruction');
    console.log('Use this with:');
    console.log('  recentValidityProof: validityProof.compressedProof');
    console.log('  recentInputStateRootIndices: validityProof.rootIndices');
    
  } catch (error) {
    console.error('Error generating proof:', error);
  }
}

generateProofForTransfer();

Last updated