Skip to main content

Module account

Module account 

Source
Expand description

Compressed account abstraction similar to anchor Account.

§Light Account

LightAccount wraps the compressed account data so that it is easy to use similar to anchor Account. LightAccount sets the discriminator and creates the compressed account data hash.

Data structs used with LightAccount must implement the traits:

  • LightDiscriminator
  • BorshSerialize, BorshDeserialize
  • Debug, Default, Clone

§Account Data Hashing

Sha256 data hashing is recommended for most use cases. Account data is serialized into a vector with borsh and hashed with Sha256.

Poseidon data hashing is recommended for zk use cases. The data struct needs to implement the DataHasher trait. The LightHasher macro derives, the DataHasher trait. The hashing scheme resembles the struct layout. Alternatively, DataHasher can be implemented manually. Poseidon hashing is CU intensive and has limitations with regards to hash inputs see Poseidon module for details.

§Compressed account with LightDiscriminator

use light_sdk::LightDiscriminator;
use solana_pubkey::Pubkey;
use borsh::{BorshSerialize, BorshDeserialize};
#[derive(Clone, Debug, Default, LightDiscriminator, BorshSerialize, BorshDeserialize)]
pub struct CounterAccount {
    pub owner: Pubkey,
    pub counter: u64,
}

§Create compressed account

let mut my_compressed_account = LightAccount::<CounterAccount>::new_init(
    &program_id,
    Some(address),
    output_tree_index,
);
// Set data:
my_compressed_account.owner = owner;

§Update compressed account

let mut my_compressed_account = LightAccount::<CounterAccount>::new_mut(
    &program_id,
    &account_meta,
    compressed_account_data,
)?;
// Increment counter.
my_compressed_account.counter += 1;

§Close compressed account

let my_compressed_account = LightAccount::<CounterAccount>::new_close(
    &program_id,
    &account_meta,
    compressed_account_data,
)?;

Re-exports§

pub use sha::LightAccount;

Modules§

sha
SHA256 borsh flat hashed Light Account. This is the recommended account type for most use cases.

Traits§

Size