Crate light_sdk

Source
Expand description

§Core Functionality

  1. Instruction
    • AccountMeta - Compressed account metadata structs for instruction data.
    • PackedAccounts - Abstraction to prepare accounts offchain for instructions with compressed accounts.
  2. Program logic
    • LightAccount - Compressed account abstraction similar to anchor Account.
    • derive_address - Create a compressed account address.
    • LightHasher - DeriveMacro to derive a hashing scheme from a struct layout.
    • LightDiscriminator - DeriveMacro to derive a compressed account discriminator.
  3. Cpi
    • CpiAccounts - Prepare accounts to cpi the light system program.
    • CpiInputs - Prepare instruction data to cpi the light system program.
    • invoke_light_system_program - Invoke the light system program via cpi.

§Features

  1. anchor - Derives AnchorSerialize, AnchorDeserialize instead of BorshSerialize, BorshDeserialize.

  2. v2 - light protocol program v2 are currently in audit and only available on local host and with light-program-test. Deploy on devnet and mainnet only without v2 features enabled.

§Example Solana program code to create a compressed account

use anchor_lang::{prelude::*, Discriminator};
use light_sdk::{
    account::LightAccount,
    address::v1::derive_address,
    cpi::{CpiAccounts, CpiInputs},
    derive_light_cpi_signer,
    instruction::{account_meta::CompressedAccountMeta, PackedAddressTreeInfo},
    CpiSigner, LightDiscriminator, LightHasher, ValidityProof,
};

declare_id!("2tzfijPBGbrR5PboyFUFKzfEoLTwdDSHUjANCw929wyt");

pub const LIGHT_CPI_SIGNER: CpiSigner =
    derive_light_cpi_signer!("2tzfijPBGbrR5PboyFUFKzfEoLTwdDSHUjANCw929wyt");

#[program]
pub mod counter {

    use super::*;

    pub fn create_compressed_account<'info>(
        ctx: Context<'_, '_, '_, 'info, CreateCompressedAccount<'info>>,
        proof: ValidityProof,
        address_tree_info: PackedAddressTreeInfo,
        output_tree_index: u8,
    ) -> Result<()> {
        let light_cpi_accounts = CpiAccounts::new(
            ctx.accounts.fee_payer.as_ref(),
            ctx.remaining_accounts,
            crate::LIGHT_CPI_SIGNER,
        )
        .map_err(ProgramError::from)?;

        let (address, address_seed) = derive_address(
            &[b"counter", ctx.accounts.fee_payer.key().as_ref()],
            &address_tree_info.get_tree_pubkey(&light_cpi_accounts)?,
            &crate::ID,
        );
        let new_address_params = address_tree_info
            .into_new_address_params_packed(address_seed);

        let mut my_compressed_account = LightAccount::<'_, CounterAccount>::new_init(
            &crate::ID,
            Some(address),
            output_tree_index,
        );

        my_compressed_account.owner = ctx.accounts.fee_payer.key();

        let cpi_inputs = CpiInputs::new_with_address(
            proof,
            vec![my_compressed_account
                .to_account_info()
                .map_err(ProgramError::from)?],
            vec![new_address_params],
        );

        cpi_inputs
            .invoke_light_system_program(light_cpi_accounts)
            .map_err(ProgramError::from)?;

        Ok(())
    }
}

#[derive(Accounts)]
pub struct CreateCompressedAccount<'info> {
   #[account(mut)]
   pub fee_payer: Signer<'info>,
}

#[derive(Clone, Debug, Default, LightHasher, LightDiscriminator)]
pub struct CounterAccount {
   #[hash]
   pub owner: Pubkey,
   pub counter: u64
}

Re-exports§

pub use light_account_checks;
pub use light_hasher;

Modules§

account
Compressed account abstraction similar to anchor Account.
address
Functions to derive compressed account addresses.
constants
cpi
Utilities to invoke the light-system-program via cpi.
error
instruction
Utilities to build instructions for programs with compressed accounts.
legacy
Legacy types re-imported from programs which should be removed as soon as possible.
token
transfer
Transfer compressed sol between compressed accounts.
utils

Macros§

derive_light_cpi_signer
Derives a complete Light Protocol CPI configuration at compile time
find_cpi_signer_macro

Traits§

LightDiscriminator

Attribute Macros§

light_system_accounts
Adds required fields to your anchor instruction for applying a zk-compressed state transition.

Derive Macros§

LightDiscriminator
LightHasher
Makes the annotated struct hashable by implementing the following traits:
LightTraits
Implements traits on the given struct required for invoking The Light system program via CPI.