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.
TODO write this section
To integrate with the Index program, add it to your dependencies (see CPI Examples for sample code snippets):
```yaml
[dependencies] index-program = { version = "0.5.0", features = ["cpi"] } ```
To download and play with the code, clone the repo:
```sh
git clone git@github.com:faktorfi/indexor.git cd indexor yarn anchor build anchor test ```
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.
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.
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, }, };
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
// 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,
)
} ```