light_sdk/
lib.rs

1//!
2//! # Core Functionality
3//! 1. Instruction
4//!     - `AccountMeta` - Compressed account metadata structs for instruction data.
5//!     - `PackedAccounts` - Abstraction to prepare accounts offchain for instructions with compressed accounts.
6//! 2. Program logic
7//!     - `LightAccount` - Compressed account abstraction similar to anchor Account.
8//!     - `derive_address` - Create a compressed account address.
9//!     - `LightHasher` - DeriveMacro to derive a hashing scheme from a struct layout.
10//!     - `LightDiscriminator` - DeriveMacro to derive a compressed account discriminator.
11//! 3. Cpi
12//!     - `CpiAccounts` - Prepare accounts to cpi the light system program.
13//!     - `CpiInputs` - Prepare instruction data to cpi the light system program.
14//!     - `invoke_light_system_program` - Invoke the light system program via cpi.
15//!
16//!
17//! # Features
18//! 1. `anchor` - Derives AnchorSerialize, AnchorDeserialize instead of BorshSerialize, BorshDeserialize.
19//!
20//! 2. `v2` - light protocol program v2 are currently in audit and only available on local host and with light-program-test.
21//!    Deploy on devnet and mainnet only without v2 features enabled.
22//!
23//! ### Example Solana program code to create a compressed account
24//! ```ignore
25//! use anchor_lang::{prelude::*, Discriminator};
26//! use light_sdk::{
27//!     account::LightAccount,
28//!     address::v1::derive_address,
29//!     cpi::{CpiAccounts, CpiInputs},
30//!     derive_light_cpi_signer,
31//!     instruction::{account_meta::CompressedAccountMeta, PackedAddressTreeInfo},
32//!     CpiSigner, LightDiscriminator, LightHasher, ValidityProof,
33//! };
34//!
35//! declare_id!("2tzfijPBGbrR5PboyFUFKzfEoLTwdDSHUjANCw929wyt");
36//!
37//! pub const LIGHT_CPI_SIGNER: CpiSigner =
38//!     derive_light_cpi_signer!("2tzfijPBGbrR5PboyFUFKzfEoLTwdDSHUjANCw929wyt");
39//!
40//! #[program]
41//! pub mod counter {
42//!
43//!     use super::*;
44//!
45//!     pub fn create_compressed_account<'info>(
46//!         ctx: Context<'_, '_, '_, 'info, CreateCompressedAccount<'info>>,
47//!         proof: ValidityProof,
48//!         address_tree_info: PackedAddressTreeInfo,
49//!         output_tree_index: u8,
50//!     ) -> Result<()> {
51//!         let light_cpi_accounts = CpiAccounts::new(
52//!             ctx.accounts.fee_payer.as_ref(),
53//!             ctx.remaining_accounts,
54//!             crate::LIGHT_CPI_SIGNER,
55//!         )
56//!         .map_err(ProgramError::from)?;
57//!
58//!         let (address, address_seed) = derive_address(
59//!             &[b"counter", ctx.accounts.fee_payer.key().as_ref()],
60//!             &address_tree_info.get_tree_pubkey(&light_cpi_accounts)?,
61//!             &crate::ID,
62//!         );
63//!         let new_address_params = address_tree_info
64//!             .into_new_address_params_packed(address_seed);
65//!
66//!         let mut my_compressed_account = LightAccount::<'_, CounterAccount>::new_init(
67//!             &crate::ID,
68//!             Some(address),
69//!             output_tree_index,
70//!         );
71//!
72//!         my_compressed_account.owner = ctx.accounts.fee_payer.key();
73//!
74//!         let cpi_inputs = CpiInputs::new_with_address(
75//!             proof,
76//!             vec![my_compressed_account
77//!                 .to_account_info()
78//!                 .map_err(ProgramError::from)?],
79//!             vec![new_address_params],
80//!         );
81//!
82//!         cpi_inputs
83//!             .invoke_light_system_program(light_cpi_accounts)
84//!             .map_err(ProgramError::from)?;
85//!
86//!         Ok(())
87//!     }
88//! }
89//!
90//! #[derive(Accounts)]
91//! pub struct CreateCompressedAccount<'info> {
92//!    #[account(mut)]
93//!    pub fee_payer: Signer<'info>,
94//! }
95//!
96//! #[derive(Clone, Debug, Default, LightHasher, LightDiscriminator)]
97//!pub struct CounterAccount {
98//!    #[hash]
99//!    pub owner: Pubkey,
100//!    pub counter: u64
101//!}
102//! ```
103
104/// Compressed account abstraction similar to anchor Account.
105pub mod account;
106/// Functions to derive compressed account addresses.
107pub mod address;
108/// Utilities to invoke the light-system-program via cpi.
109pub mod cpi;
110pub mod error;
111/// Utilities to build instructions for programs with compressed accounts.
112pub mod instruction;
113pub mod legacy;
114pub mod token;
115/// Transfer compressed sol between compressed accounts.
116pub mod transfer;
117pub mod utils;
118
119#[cfg(feature = "anchor")]
120use anchor_lang::{AnchorDeserialize, AnchorSerialize};
121#[cfg(not(feature = "anchor"))]
122use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize};
123pub use light_account_checks::{self, discriminator::Discriminator as LightDiscriminator};
124pub use light_hasher;
125pub use light_sdk_macros::{
126    derive_light_cpi_signer, light_system_accounts, LightDiscriminator, LightHasher, LightTraits,
127};
128pub use light_sdk_types::constants;
129use solana_account_info::AccountInfo;
130use solana_cpi::invoke_signed;
131use solana_instruction::{AccountMeta, Instruction};
132use solana_program_error::ProgramError;
133use solana_pubkey::Pubkey;