Skip to content

Commit

Permalink
account_info.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Swenschaeferjohann authored and Swenschaeferjohann committed Nov 22, 2024
1 parent 80fb05c commit 2304cb7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub mod name_service {
&crate::ID,
);

let mut record: LightAccount<'_, NameRecord> = LightAccount::from_meta_init(
let mut record: LightAccount<'_, NameRecord> = LightAccount::new(
&accounts[0],
NameRecord::discriminator(),
address,
Expand Down Expand Up @@ -75,7 +75,7 @@ pub mod name_service {

// Convert `LightAccountMeta` to `LightAccount`.
let mut record: LightAccount<'_, NameRecord> =
LightAccount::from_meta_mut(&accounts[0], NameRecord::discriminator(), &crate::ID)?;
LightAccount::mut(&accounts[0], NameRecord::discriminator(), &crate::ID)?;

// Check the ownership of the `record`.
if record.owner != ctx.accounts.signer.key() {
Expand All @@ -100,7 +100,7 @@ pub mod name_service {
.ok_or(LightSdkError::ExpectedAccounts)?;

let record: LightAccount<'_, NameRecord> =
LightAccount::from_meta_close(&accounts[0], NameRecord::discriminator(), &crate::ID)?;
LightAccount::close(&accounts[0], NameRecord::discriminator(), &crate::ID)?;

if record.owner != ctx.accounts.signer.key() {
return err!(CustomError::Unauthorized);
Expand Down
2 changes: 1 addition & 1 deletion sdk/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Rust SDK with helpers to interact with ZK Compression on Solana.

Documentation is available at https://zkcompression.com

Source code: https://github.com/Lightprotocol/light-protocol/tree/main/programs/system
Source code: https://github.com/Lightprotocol/light-protocol/tree/main/sdk/

## Audit

Expand Down
12 changes: 6 additions & 6 deletions sdk/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ impl<'info, T> LightAccount<'info, T>
where
T: AnchorDeserialize + AnchorSerialize + Clone + DataHasher + Default + Discriminator,
{
pub fn from_meta_init(
pub fn new(
meta: &'info LightAccountMeta,
discriminator: [u8; 8],
new_address: [u8; 32],
new_address_seed: [u8; 32],
owner: &'info Pubkey,
) -> Result<Self> {
let account_state = T::default();
let account_info = LightAccountInfo::from_meta_init_without_output_data(
let account_info = LightAccountInfo::new_without_output_data(
meta,
discriminator,
new_address,
Expand All @@ -59,13 +59,13 @@ where
})
}

pub fn from_meta_mut(
pub fn r#mut(
meta: &'info LightAccountMeta,
discriminator: [u8; 8],
owner: &'info Pubkey,
) -> Result<Self> {
let mut account_info =
LightAccountInfo::from_meta_without_output_data(meta, discriminator, owner)?;
LightAccountInfo::without_output_data(meta, discriminator, owner)?;
let account_state = T::try_from_slice(
meta.data
.as_ref()
Expand All @@ -87,13 +87,13 @@ where
})
}

pub fn from_meta_close(
pub fn close(
meta: &'info LightAccountMeta,
discriminator: [u8; 8],
owner: &'info Pubkey,
) -> Result<Self> {
let mut account_info =
LightAccountInfo::from_meta_without_output_data(meta, discriminator, owner)?;
LightAccountInfo::without_output_data(meta, discriminator, owner)?;
let account_state = T::try_from_slice(
meta.data
.as_ref()
Expand Down
68 changes: 39 additions & 29 deletions sdk/src/account_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,55 @@ use crate::{
merkle_context::PackedMerkleContext,
};

/// Information about compressed account which is being initialized.
/// Read-only information about existing compressed account
#[derive(Debug)]
pub struct LightInputAccountInfo<'a> {
/// Lamports.
/// Lamports assigned to the compressed account.
pub lamports: Option<u64>,
/// Address.
/// Address of the compressed account.
pub address: Option<[u8; 32]>,
/// Account data.
/// The data held in this compressed account.
pub data: Option<&'a [u8]>,
/// Data hash.
/// Hash of the data held in this compressed account.
pub data_hash: Option<[u8; 32]>,
/// Merkle tree context.
/// Context about the State tree that the compressed account is stored in.
pub merkle_context: PackedMerkleContext,
/// Root index.
/// Recent root index of the State tree that the compressed account is
/// stored in.
pub root_index: u16,
}

/// Information about compressed account which is being mutated.
#[derive(Debug)]
pub struct LightAccountInfo<'a> {
/// Input account.
/// Read-only information about existing compressed account state.
pub(crate) input: Option<LightInputAccountInfo<'a>>,
/// Owner of the account.
///
/// Defaults to the program ID.
/// Program or user that owns the account.
pub owner: &'a Pubkey,
/// Lamports.
/// Lamports assigned to the compressed account.
pub lamports: Option<u64>,
/// Discriminator.
/// Account discriminator.
pub discriminator: Option<[u8; 8]>,
/// Account data.
/// The data held in this compressed account.
pub data: Option<Rc<RefCell<Vec<u8>>>>,
/// Data hash.
/// Hash of the data held in this compressed account.
pub data_hash: Option<[u8; 32]>,
/// Address.
/// Address of the compressed account.
pub address: Option<[u8; 32]>,
/// New Merkle tree index. Set `None` for `close` account infos.
/// Index of Merkle tree account in account_infos passed to the program.
///
/// Index of the Merkle tree account in the program's account_infos array to
/// which the compressed account's data will be written.
///
/// Set `None` to indicate this is a close operation that will not write
/// output data.
pub output_merkle_tree_index: Option<u8>,
/// New address parameters.
/// Parameters required when creating a new compressed account address.
pub new_address_params: Option<PackedNewAddressParams>,
}

impl<'a> LightAccountInfo<'a> {
pub fn from_meta_init(
pub fn new(
meta: &'a LightAccountMeta,
discriminator: [u8; 8],
new_address: [u8; 32],
Expand Down Expand Up @@ -103,7 +108,7 @@ impl<'a> LightAccountInfo<'a> {
Ok(account_info)
}

pub fn from_meta_mut(
pub fn r#mut(
meta: &'a LightAccountMeta,
discriminator: [u8; 8],
owner: &'a Pubkey,
Expand Down Expand Up @@ -160,7 +165,7 @@ impl<'a> LightAccountInfo<'a> {
Ok(account_info)
}

pub fn from_meta_close(
pub fn close(
meta: &'a LightAccountMeta,
discriminator: [u8; 8],
owner: &'a Pubkey,
Expand Down Expand Up @@ -196,7 +201,7 @@ impl<'a> LightAccountInfo<'a> {
Ok(account_info)
}

pub(crate) fn from_meta_init_without_output_data(
pub(crate) fn new_without_output_data(
meta: &'a LightAccountMeta,
discriminator: [u8; 8],
new_address: [u8; 32],
Expand Down Expand Up @@ -239,7 +244,7 @@ impl<'a> LightAccountInfo<'a> {
///
/// Not intended for external use, intended for building upper abstraction
/// layers which handle data serialization on their own.
pub(crate) fn from_meta_without_output_data(
pub(crate) fn without_output_data(
meta: &'a LightAccountMeta,
discriminator: [u8; 8],
owner: &'a Pubkey,
Expand Down Expand Up @@ -274,19 +279,21 @@ impl<'a> LightAccountInfo<'a> {
Ok(account_info)
}

/// Assigns the provided lamports to the compressed account.
pub fn compress_and_add_sol(&mut self, lamports: u64) {
self.lamports = Some(lamports);
}

/// Returns the original data sent by the client, before any potential
/// modifications made by the program.
pub fn initial_data(&self) -> Option<&[u8]> {
/// Returns a reference to the original data sent by the client, before any
/// potential modifications made by the program.
pub fn input_data(&self) -> Option<&[u8]> {
self.input.as_ref().and_then(|input| input.data)
}

/// Converts the given [LightAccountInfo] into a
/// [PackedCompressedAccountWithMerkleContext] which can be sent to the
/// light-system program.
/// Returns the input compressed account data if it exists, otherwise
/// returns None.
///
/// To be passed along to the light-system program via `invoke_cpi`.
pub fn input_compressed_account(
&self,
) -> Result<Option<PackedCompressedAccountWithMerkleContext>> {
Expand Down Expand Up @@ -322,6 +329,9 @@ impl<'a> LightAccountInfo<'a> {
}
}

/// Returns the output compressed account data to be passed to the
/// light-system program via `invoke_cpi` if a merkle tree index was set,
/// otherwise returns None.
pub fn output_compressed_account(
&self,
) -> Result<Option<OutputCompressedAccountWithPackedContext>> {
Expand Down
6 changes: 3 additions & 3 deletions test-programs/sdk-test-program/programs/sdk-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub mod sdk_test {
);

let mut my_compressed_account: LightAccount<'_, MyCompressedAccount> =
LightAccount::from_meta_init(
LightAccount::new(
&accounts[0],
MyCompressedAccount::discriminator(),
address,
Expand Down Expand Up @@ -81,7 +81,7 @@ pub mod sdk_test {
);

let mut my_compressed_account: LightAccount<'_, MyCompressedAccount> =
LightAccount::from_meta_init(
LightAccount::new(
&accounts[0],
MyCompressedAccount::discriminator(),
address,
Expand Down Expand Up @@ -116,7 +116,7 @@ pub mod sdk_test {
.ok_or(LightSdkError::ExpectedAccounts)?;

let mut my_compressed_account: LightAccount<'_, MyCompressedAccount> =
LightAccount::from_meta_mut(
LightAccount::mut(
&accounts[0],
MyCompressedAccount::discriminator(),
&crate::ID,
Expand Down

0 comments on commit 2304cb7

Please sign in to comment.