Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Cargo.lock

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

12 changes: 10 additions & 2 deletions sdk-libs/account-pinocchio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,16 @@ pub use light_account_checks::{
account_info::pinocchio::OwnedAccountMeta, discriminator::Discriminator as LightDiscriminator,
packed_accounts, AccountInfoTrait, AccountMetaTrait,
};
pub use light_compressed_account::instruction_data::compressed_proof::ValidityProof;
pub use light_macros::{derive_light_cpi_signer, derive_light_cpi_signer_pda};
pub use light_compressed_account::instruction_data::{
compressed_proof::ValidityProof, cpi_context::CompressedCpiContext,
with_account_info::InstructionDataInvokeCpiWithAccountInfo,
};
pub use light_macros::{derive_light_cpi_signer, derive_light_cpi_signer_pda, pubkey_array};
// Re-export for macro-generated client code (off-chain only)
#[cfg(feature = "std")]
pub extern crate solana_instruction;
#[cfg(feature = "std")]
pub extern crate solana_pubkey;
pub use light_sdk_macros::{
AnchorDiscriminator as Discriminator, CompressAs, HasCompressionInfo, LightAccount,
LightDiscriminator, LightHasher, LightHasherSha, LightPinocchioAccount, LightProgramPinocchio,
Expand Down
4 changes: 2 additions & 2 deletions sdk-libs/macros/src/light_pdas/accounts/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ impl VariantBuilder {

quote! {
#[cfg(not(target_os = "solana"))]
impl light_account_pinocchio::Pack<solana_instruction::AccountMeta> for #variant_name {
impl light_account_pinocchio::Pack<light_account_pinocchio::solana_instruction::AccountMeta> for #variant_name {
type Packed = #packed_variant_name;

fn pack(
Expand Down Expand Up @@ -482,7 +482,7 @@ impl VariantBuilder {
let field = &sf.field_name;
if sf.is_account_seed {
let idx_field = format_ident!("{}_idx", field);
quote! { #idx_field: accounts.insert_or_get(solana_pubkey::Pubkey::from(self.seeds.#field)) }
quote! { #idx_field: accounts.insert_or_get(light_account_pinocchio::solana_pubkey::Pubkey::from(self.seeds.#field)) }
} else if sf.has_le_bytes {
quote! { #field: self.seeds.#field.to_le_bytes() }
} else {
Expand Down
28 changes: 24 additions & 4 deletions sdk-libs/macros/src/light_pdas/program/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,11 @@ impl DecompressBuilder {
let ctx_fields_decl: Vec<_> = ctx_fields
.iter()
.map(|field| {
quote! { pub #field: solana_pubkey::Pubkey }
if is_pinocchio {
quote! { pub #field: [u8; 32] }
} else {
quote! { pub #field: solana_pubkey::Pubkey }
}
})
.collect();

Expand Down Expand Up @@ -613,11 +617,27 @@ fn generate_pda_seed_derivation_for_trait_with_ctx_seeds(

let indices: Vec<usize> = (0..seed_refs.len()).collect();

let pda_derivation = if is_pinocchio {
quote! {
let (pda, bump) = pinocchio::pubkey::find_program_address(seeds, program_id);
}
} else {
quote! {
let program_id_pubkey = solana_pubkey::Pubkey::from(*program_id);
let (pda, bump) = solana_pubkey::Pubkey::find_program_address(seeds, &program_id_pubkey);
}
};

let pda_to_bytes = if is_pinocchio {
quote! { pda }
} else {
quote! { pda.to_bytes() }
};

Ok(quote! {
#(#bindings)*
let seeds: &[&[u8]] = &[#(#seed_refs,)*];
let program_id_pubkey = solana_pubkey::Pubkey::from(*program_id);
let (pda, bump) = solana_pubkey::Pubkey::find_program_address(seeds, &program_id_pubkey);
#pda_derivation
let mut seeds_vec = Vec::with_capacity(seeds.len() + 1);
#(
seeds_vec.push(seeds[#indices].to_vec());
Expand All @@ -628,7 +648,7 @@ fn generate_pda_seed_derivation_for_trait_with_ctx_seeds(
bump_vec.push(bump);
seeds_vec.push(bump_vec);
}
Ok((seeds_vec, pda.to_bytes()))
Ok((seeds_vec, #pda_to_bytes))
})
}

Expand Down
6 changes: 4 additions & 2 deletions sdk-libs/macros/src/light_pdas/program/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ pub(crate) fn generate_light_program_items(
&pda_seeds,
&token_seeds,
&instruction_data,
false, // Anchor (not pinocchio)
)?;

// Collect all generated items into a Vec<TokenStream>
Expand Down Expand Up @@ -1286,16 +1287,17 @@ pub(crate) fn generate_light_program_pinocchio_items(
pub write_top_up: u32,
pub rent_sponsor: [u8; 32],
pub compression_authority: [u8; 32],
pub rent_config: light_compressible::rent::RentConfig,
pub rent_config: light_account_pinocchio::rent::RentConfig,
pub address_space: Vec<[u8; 32]>,
}
};

// Client functions (module + pub use - framework-agnostic)
// Client functions (module + pub use - pinocchio uses light_account_pinocchio re-exports)
let client_functions = super::seed_codegen::generate_client_seed_functions(
&pda_seeds,
&token_seeds,
&instruction_data,
true, // Pinocchio
)?;

// Collect all generated items
Expand Down
41 changes: 30 additions & 11 deletions sdk-libs/macros/src/light_pdas/program/seed_codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,33 @@ pub fn generate_client_seed_functions(
pda_seeds: &Option<Vec<TokenSeedSpec>>,
token_seeds: &Option<Vec<TokenSeedSpec>>,
instruction_data: &[InstructionDataSpec],
is_pinocchio: bool,
) -> Result<TokenStream> {
let mut functions = Vec::new();

// Choose the correct pubkey path based on framework
let pubkey_path = if is_pinocchio {
quote! { light_account_pinocchio::solana_pubkey::Pubkey }
} else {
quote! { solana_pubkey::Pubkey }
};

if let Some(pda_seed_specs) = pda_seeds {
for spec in pda_seed_specs {
let variant_name = &spec.variant;
let snake_case = camel_to_snake_case(&variant_name.to_string());
let function_name = format_ident!("get_{}_seeds", snake_case);

let (parameters, seed_expressions) =
analyze_seed_spec_for_client(spec, instruction_data)?;
analyze_seed_spec_for_client(spec, instruction_data, is_pinocchio)?;

let fn_body = generate_seed_derivation_body(
&seed_expressions,
quote! { &solana_pubkey::Pubkey::from(crate::LIGHT_CPI_SIGNER.program_id) },
quote! { &#pubkey_path::from(crate::LIGHT_CPI_SIGNER.program_id) },
is_pinocchio,
);
let function = quote! {
pub fn #function_name(#(#parameters),*) -> (Vec<Vec<u8>>, solana_pubkey::Pubkey) {
pub fn #function_name(#(#parameters),*) -> (Vec<Vec<u8>>, #pubkey_path) {
#fn_body
}
};
Expand All @@ -63,14 +72,15 @@ pub fn generate_client_seed_functions(
format_ident!("get_{}_seeds", variant_name.to_string().to_lowercase());

let (parameters, seed_expressions) =
analyze_seed_spec_for_client(spec, instruction_data)?;
analyze_seed_spec_for_client(spec, instruction_data, is_pinocchio)?;

let fn_body = generate_seed_derivation_body(
&seed_expressions,
quote! { &solana_pubkey::Pubkey::from(crate::LIGHT_CPI_SIGNER.program_id) },
quote! { &#pubkey_path::from(crate::LIGHT_CPI_SIGNER.program_id) },
is_pinocchio,
);
let function = quote! {
pub fn #function_name(#(#parameters),*) -> (Vec<Vec<u8>>, solana_pubkey::Pubkey) {
pub fn #function_name(#(#parameters),*) -> (Vec<Vec<u8>>, #pubkey_path) {
#fn_body
}
};
Expand All @@ -96,28 +106,33 @@ pub fn generate_client_seed_functions(
owner_seeds_spec.seeds.push(owner_seed.clone());
}

let (owner_parameters, owner_seed_expressions) =
analyze_seed_spec_for_client(&owner_seeds_spec, instruction_data)?;
let (owner_parameters, owner_seed_expressions) = analyze_seed_spec_for_client(
&owner_seeds_spec,
instruction_data,
is_pinocchio,
)?;

let (fn_params, fn_body) = if owner_parameters.is_empty() {
(
quote! { _program_id: &solana_pubkey::Pubkey },
quote! { _program_id: &#pubkey_path },
generate_seed_derivation_body(
&owner_seed_expressions,
quote! { _program_id },
is_pinocchio,
),
)
} else {
(
quote! { #(#owner_parameters),* },
generate_seed_derivation_body(
&owner_seed_expressions,
quote! { &solana_pubkey::Pubkey::from(crate::LIGHT_CPI_SIGNER.program_id) },
quote! { &#pubkey_path::from(crate::LIGHT_CPI_SIGNER.program_id) },
is_pinocchio,
),
)
};
let owner_seeds_function = quote! {
pub fn #owner_seeds_function_name(#fn_params) -> (Vec<Vec<u8>>, solana_pubkey::Pubkey) {
pub fn #owner_seeds_function_name(#fn_params) -> (Vec<Vec<u8>>, #pubkey_path) {
#fn_body
}
};
Expand All @@ -127,11 +142,13 @@ pub fn generate_client_seed_functions(
}

Ok(quote! {
#[cfg(not(target_os = "solana"))]
mod __client_seed_functions {
use super::*;
#(#functions)*
}

#[cfg(not(target_os = "solana"))]
pub use __client_seed_functions::*;
})
}
Expand All @@ -144,6 +161,7 @@ pub fn generate_client_seed_functions(
fn analyze_seed_spec_for_client(
spec: &TokenSeedSpec,
instruction_data: &[InstructionDataSpec],
is_pinocchio: bool,
) -> Result<(Vec<TokenStream>, Vec<TokenStream>)> {
let mut parameters = Vec::new();
let mut expressions = Vec::new();
Expand All @@ -160,6 +178,7 @@ fn analyze_seed_spec_for_client(
&mut seen_params,
&mut parameters,
&mut expressions,
is_pinocchio,
)?;
}

Expand Down
11 changes: 10 additions & 1 deletion sdk-libs/macros/src/light_pdas/program/seed_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,24 @@ use syn::Ident;
pub fn generate_seed_derivation_body(
seed_expressions: &[TokenStream],
program_id_expr: TokenStream,
is_pinocchio: bool,
) -> TokenStream {
let seed_count = seed_expressions.len();

// Choose the correct pubkey path based on framework
let pubkey_path = if is_pinocchio {
quote! { light_account_pinocchio::solana_pubkey::Pubkey }
} else {
quote! { solana_pubkey::Pubkey }
};

quote! {
let mut seed_values = Vec::with_capacity(#seed_count + 1);
#(
seed_values.push((#seed_expressions).to_vec());
)*
let seed_slices: Vec<&[u8]> = seed_values.iter().map(|v| v.as_slice()).collect();
let (pda, bump) = solana_pubkey::Pubkey::find_program_address(&seed_slices, #program_id_expr);
let (pda, bump) = #pubkey_path::find_program_address(&seed_slices, #program_id_expr);
seed_values.push(vec![bump]);
(seed_values, pda)
}
Expand Down
14 changes: 7 additions & 7 deletions sdk-libs/macros/src/light_pdas/program/variant_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ impl<'a> LightVariantBuilder<'a> {
.iter()
.map(|f| {
let idx = format_ident!("{}_idx", f);
quote! { #idx: remaining_accounts.insert_or_get(solana_pubkey::Pubkey::from(self.#f)) }
quote! { #idx: remaining_accounts.insert_or_get(light_account_pinocchio::solana_pubkey::Pubkey::from(self.#f)) }
})
.collect();

Expand Down Expand Up @@ -710,17 +710,17 @@ impl<'a> LightVariantBuilder<'a> {
}

#[cfg(not(target_os = "solana"))]
impl light_account_pinocchio::Pack<solana_instruction::AccountMeta> for #seeds_name {
impl light_account_pinocchio::Pack<light_account_pinocchio::solana_instruction::AccountMeta> for #seeds_name {
type Packed = #packed_seeds_name;

fn pack(
&self,
remaining_accounts: &mut light_account_pinocchio::PackedAccounts,
) -> std::result::Result<Self::Packed, light_account_pinocchio::LightSdkTypesError> {
let __seeds: &[&[u8]] = &[#(#bump_seed_refs),*];
let (_, __bump) = solana_pubkey::Pubkey::find_program_address(
let (_, __bump) = light_account_pinocchio::solana_pubkey::Pubkey::find_program_address(
__seeds,
&solana_pubkey::Pubkey::from(crate::LIGHT_CPI_SIGNER.program_id),
&light_account_pinocchio::solana_pubkey::Pubkey::from(crate::LIGHT_CPI_SIGNER.program_id),
);
Ok(#packed_seeds_name {
#(#pack_stmts,)*
Expand Down Expand Up @@ -824,9 +824,9 @@ impl<'a> LightVariantBuilder<'a> {
})
.collect();
quote! {
let (__owner, _) = solana_pubkey::Pubkey::find_program_address(
let (__owner, _) = light_account_pinocchio::solana_pubkey::Pubkey::find_program_address(
&[#(#owner_seed_refs),*],
&solana_pubkey::Pubkey::from(crate::LIGHT_CPI_SIGNER.program_id),
&light_account_pinocchio::solana_pubkey::Pubkey::from(crate::LIGHT_CPI_SIGNER.program_id),
);
__owner.to_bytes()
}
Expand Down Expand Up @@ -1076,7 +1076,7 @@ impl<'a> LightVariantBuilder<'a> {

quote! {
#[cfg(not(target_os = "solana"))]
impl light_account_pinocchio::Pack<solana_instruction::AccountMeta> for LightAccountVariant {
impl light_account_pinocchio::Pack<light_account_pinocchio::solana_instruction::AccountMeta> for LightAccountVariant {
type Packed = PackedLightAccountVariant;

fn pack(
Expand Down
Loading
Loading