🔢 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.

👉 Getting Started

Integrate and build

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

```yaml

Cargo.toml

[dependencies] index-program = { version = "0.1.0" } ```

Hack around

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

```sh

Terminal

git clone git@github.com:faktorfi/programs.git cd programs 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.

⚙️ How It Works

Frame 40158

🦀 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.

```rs // createmyindex.rs

use { crate::state::, anchor_lang::{prelude::, solanaprogram::systemprogram}, indexprogram::{ cpi::{accounts::CreateIndex, createindex}, 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, namespace: String, bump: u8) -> ProgramResult { // Get accounts. let authority = &ctx.accounts.authority; let index = &ctx.accounts.index; let indexprogram = &ctx.accounts.indexprogram; let systemprogram = &ctx.accounts.systemprogram;

// 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,
)

} ```