index-program 0.1.0

Virtual namespaces for indexing Solana accounts on-chain
Documentation

🔢 Index Program

The Index Program can create virtual namespaces for indexing Solana accounts on-chain. It provides key-value stores (KVS) for mapping account addresses to more predictable names. It supports creating indicies with either serial namespaces (0, 1, 2, 3 ... like an array) or freeform namespaces ("foo", "bar", "baz" ... like a hashmap). It additionally supports constant-time "reverse-lookup" searches from an address to its name in an index.

⚙️ How It Works

TODO write this section

👉 Getting Started

Integrate and build

To integrate with the Index program, add it to your dependencies (see CPI Examples for sample code snippets):

# Cargo.toml

[dependencies]
index-program = { version = "0.5.0", features = ["cpi"] }

Hack around

To download and play with the code, clone the repo:

# Terminal

git clone git@github.com:faktorfi/indexor.git
cd indexor
yarn
anchor build
anchor test

Submit an improvement

If you spot a bug or want to submit a improvement, join the Faktor Discord and come say hello! We're a group of people building public key infrastructure (PKI) and payments systems on Solana.

🦀 CPI Examples

The code snippets in this section are for Solana programs that need to create and manage their own on-chain indices. These examples assume the program has a singleton "program authority account" for signing instructions on behalf of the program.

Creating an index

This example instruction create_my_index displays a program creating a freeform index in a custom namespace. Since the program signs the create_index instruction with its authority account (a PDA), the Index Program marks the authority as the index's owner – guaranteeing only the program authority may write to the index.

// create_my_index.rs

use {
    crate::state::*,
    anchor_lang::{prelude::*, solana_program::system_program},
    index_program::{
        cpi::{accounts::CreateIndex, create_index},
        program::index_program,
        state::Index,
    },
};

#[derive(Accounts)]
#[instruction(namespace: String, bump: u8)]
pub struct CreateMyIndex<'info> {
    #[account(mut, seeds = [SEED_AUTHORITY], bump = authority.bump)]
    pub authority: Account<'info, Authority>,

    #[account(mut)]
    pub index: Account<'info, Index>,

    #[account(address = index_program::ID)]
    pub index_program: Program<'info, Index>,

    #[account(mut)]
    pub signer: Signer<'info>,

    #[account(address = system_program::ID)]
    pub system_program: Program<'info, System>,
}

pub fn handler(ctx: Context<CreateMyIndex>, namespace: String, bump: u8) -> ProgramResult {
    // Get accounts.
    let authority = &ctx.accounts.authority;
    let index = &ctx.accounts.index;
    let index_program = &ctx.accounts.index_program;
    let system_program = &ctx.accounts.system_program;

    // Initialize index account.
    create_index(
        CpiContext::new_with_signer(
            index_program.to_account_info(),
            CreateIndex {
                index: index.to_account_info(),
                signer: authority.to_account_info(),
                system_program: system_program.to_account_info(),
            },
            &[&[SEED_AUTHORITY, &[authority.bump]]],
        ),
        namespace,
        false,
        bump,
    )
}