Skip to content

Commit

Permalink
feat: Light SDK without Anchor
Browse files Browse the repository at this point in the history
Provide a possibility to use the Rust Light SDK for programs which are
not using Anchor.

Related changes:

- Rename the former `sdk-test` program to `sdk-anchor-test`.
- Add the new `sdk-test` program, which uses only solana-program.
- Replace all re-imports from `anchor_lang` with direct imports from
  `solana_program`.
- Introduce `anchor` feature flag in `light_sdk`.
- Make the whole `light_sdk::verify` module independent from Anchor.
  • Loading branch information
vadorovsky committed Nov 22, 2024
1 parent bb82a7e commit c5c882b
Show file tree
Hide file tree
Showing 41 changed files with 1,439 additions and 728 deletions.
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ members = [
"test-programs/registry-test/",
"test-programs/system-cpi-test/",
"test-programs/system-test/",
"test-programs/sdk-test-program/programs/sdk-test/",
"test-programs/sdk-test/",
"test-programs/sdk-anchor-test/programs/sdk-anchor-test/",
"forester-utils",
"forester",
"photon-api",
Expand Down Expand Up @@ -79,6 +80,7 @@ thiserror = "1.0"
light-client = { path = "client", version = "0.9.1" }
light-concurrent-merkle-tree = { path = "merkle-tree/concurrent", version = "1.1.0" }
light-hasher = { path = "merkle-tree/hasher", version = "1.1.0" }
light-heap = { path = "heap", version = "1.1.0" }
light-indexed-merkle-tree = { path = "merkle-tree/indexed", version = "1.1.0" }
light-macros = { path = "macros/light", version = "1.1.0" }
light-merkle-tree-reference = { path = "merkle-tree/reference", version = "1.1.0" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ anchor-lang = { workspace=true}
borsh = { workspace = true }
light-hasher = { workspace = true, features = ["solana"] }
light-macros = { workspace = true }
light-sdk = { workspace = true }
light-sdk = { workspace = true, features = ["anchor"] }
light-sdk-macros = { workspace = true }
light-utils = { workspace = true }
light-verifier = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};
use light_hasher::bytes::AsByteVec;
use light_sdk::{
account::LightAccount, instruction_data::LightInstructionData, light_system_accounts,
verify::verify_light_accounts, LightDiscriminator, LightHasher, LightTraits,
account::LightAccount, instruction_data::LightInstructionData, verify::verify_light_accounts,
LightDiscriminator, LightHasher,
};

declare_id!("7yucc7fL3JGbyMwg4neUaenNSdySS39hbAk89Ao3t1Hz");
Expand All @@ -15,13 +15,13 @@ pub mod name_service {
use light_hasher::Discriminator;
use light_sdk::{
address::derive_address, error::LightSdkError,
program_merkle_context::unpack_address_merkle_context,
program_merkle_context::unpack_address_merkle_context, system_accounts::LightCpiAccounts,
};

use super::*;

pub fn create_record<'info>(
ctx: Context<'_, '_, '_, 'info, CreateRecord<'info>>,
pub fn create_record<'a, 'b, 'c, 'info>(
ctx: Context<'a, 'b, 'c, 'info, CreateRecord<'info>>,
inputs: Vec<u8>,
name: String,
rdata: RData,
Expand Down Expand Up @@ -55,7 +55,19 @@ pub mod name_service {
record.name = name;
record.rdata = rdata;

verify_light_accounts(&ctx, inputs.proof, &[record], None, false, None)?;
let light_cpi_accounts = LightCpiAccounts::new(
ctx.accounts.signer.as_ref(),
ctx.accounts.cpi_signer.as_ref(),
ctx.remaining_accounts,
);
verify_light_accounts(
&light_cpi_accounts,
inputs.proof,
&[record],
None,
false,
None,
)?;

Ok(())
}
Expand Down Expand Up @@ -84,7 +96,19 @@ pub mod name_service {

record.rdata = new_rdata;

verify_light_accounts(&ctx, inputs.proof, &[record], None, false, None)?;
let light_cpi_accounts = LightCpiAccounts::new(
ctx.accounts.signer.as_ref(),
ctx.accounts.cpi_signer.as_ref(),
ctx.remaining_accounts,
);
verify_light_accounts(
&light_cpi_accounts,
inputs.proof,
&[record],
None,
false,
None,
)?;

Ok(())
}
Expand All @@ -106,7 +130,19 @@ pub mod name_service {
return err!(CustomError::Unauthorized);
}

verify_light_accounts(&ctx, inputs.proof, &[record], None, false, None)?;
let light_cpi_accounts = LightCpiAccounts::new(
ctx.accounts.signer.as_ref(),
ctx.accounts.cpi_signer.as_ref(),
ctx.remaining_accounts,
);
verify_light_accounts(
&light_cpi_accounts,
inputs.proof,
&[record],
None,
false,
None,
)?;

Ok(())
}
Expand Down Expand Up @@ -154,41 +190,26 @@ pub enum CustomError {
Unauthorized,
}

#[light_system_accounts]
#[derive(Accounts, LightTraits)]
#[derive(Accounts)]
pub struct CreateRecord<'info> {
#[account(mut)]
#[fee_payer]
pub signer: Signer<'info>,
#[self_program]
pub self_program: Program<'info, crate::program::NameService>,
/// CHECK: Checked in light-system-program.
#[authority]
pub cpi_signer: AccountInfo<'info>,
}

#[light_system_accounts]
#[derive(Accounts, LightTraits)]
#[derive(Accounts)]
pub struct UpdateRecord<'info> {
#[account(mut)]
#[fee_payer]
pub signer: Signer<'info>,
#[self_program]
pub self_program: Program<'info, crate::program::NameService>,
/// CHECK: Checked in light-system-program.
#[authority]
pub cpi_signer: AccountInfo<'info>,
}

#[light_system_accounts]
#[derive(Accounts, LightTraits)]
#[derive(Accounts)]
pub struct DeleteRecord<'info> {
#[account(mut)]
#[fee_payer]
pub signer: Signer<'info>,
#[self_program]
pub self_program: Program<'info, crate::program::NameService>,
/// CHECK: Checked in light-system-program.
#[authority]
pub cpi_signer: AccountInfo<'info>,
}
Loading

0 comments on commit c5c882b

Please sign in to comment.