|
| 1 | +use borsh::{BorshDeserialize, BorshSerialize}; |
| 2 | +use light_sdk::{ |
| 3 | + account::LightAccount, |
| 4 | + cpi::{CpiAccounts, CpiAccountsConfig, CpiInputs}, |
| 5 | + error::LightSdkError, |
| 6 | + instruction::{ |
| 7 | + account_meta::{CompressedAccountMeta, CompressedAccountMetaTrait}, |
| 8 | + ValidityProof, |
| 9 | + }, |
| 10 | + LightDiscriminator, LightHasher, |
| 11 | +}; |
| 12 | +use solana_program::{ |
| 13 | + account_info::AccountInfo, clock::Clock, msg, program::invoke_signed, pubkey::Pubkey, |
| 14 | + rent::Rent, system_instruction, sysvar::Sysvar, |
| 15 | +}; |
| 16 | + |
| 17 | +pub const SLOTS_UNTIL_COMPRESSION: u64 = 100; |
| 18 | + |
| 19 | +/// Account structure for the decompressed PDA |
| 20 | +#[derive(Clone, Debug, BorshDeserialize, BorshSerialize)] |
| 21 | +pub struct DecompressedPdaAccount { |
| 22 | + /// The compressed account address this PDA was derived from |
| 23 | + pub compressed_address: [u8; 32], |
| 24 | + /// Slot when this account was last written |
| 25 | + pub last_written_slot: u64, |
| 26 | + /// Number of slots until this account can be compressed again |
| 27 | + pub slots_until_compression: u64, |
| 28 | + /// The actual account data |
| 29 | + pub data: [u8; 31], |
| 30 | + /// Flag to indicate if this is a decompressed account |
| 31 | + pub is_decompressed: bool, |
| 32 | +} |
| 33 | + |
| 34 | +/// Compressed account structure with decompression flag |
| 35 | +#[derive( |
| 36 | + Clone, Debug, Default, LightHasher, LightDiscriminator, BorshDeserialize, BorshSerialize, |
| 37 | +)] |
| 38 | +pub struct DecompressedMarkerAccount { |
| 39 | + /// Flag to indicate this account has been decompressed |
| 40 | + pub is_decompressed: bool, |
| 41 | +} |
| 42 | + |
| 43 | +/// Decompresses a compressed account into a PDA |
| 44 | +/// The PDA is derived from the compressed account's address and other seeds |
| 45 | +pub fn decompress_to_pda( |
| 46 | + accounts: &[AccountInfo], |
| 47 | + instruction_data: &[u8], |
| 48 | +) -> Result<(), LightSdkError> { |
| 49 | + msg!("Decompressing compressed account to PDA"); |
| 50 | + |
| 51 | + let mut instruction_data = instruction_data; |
| 52 | + let instruction_data = DecompressToPdaInstructionData::deserialize(&mut instruction_data) |
| 53 | + .map_err(|_| LightSdkError::Borsh)?; |
| 54 | + |
| 55 | + // Get accounts |
| 56 | + let fee_payer = &accounts[0]; |
| 57 | + let pda_account = &accounts[1]; |
| 58 | + let rent_payer = &accounts[2]; // Account that pays for PDA rent |
| 59 | + let system_program = &accounts[3]; |
| 60 | + |
| 61 | + // Derive PDA from compressed address |
| 62 | + let compressed_address = instruction_data.compressed_account.meta.address; |
| 63 | + let (pda_pubkey, pda_bump) = Pubkey::find_program_address( |
| 64 | + &[ |
| 65 | + b"decompressed_pda", |
| 66 | + &compressed_address, |
| 67 | + &instruction_data.additional_seed, |
| 68 | + ], |
| 69 | + &crate::ID, |
| 70 | + ); |
| 71 | + |
| 72 | + // Verify PDA matches |
| 73 | + if pda_pubkey != *pda_account.key { |
| 74 | + msg!("Invalid PDA pubkey"); |
| 75 | + return Err(LightSdkError::ConstraintViolation); |
| 76 | + } |
| 77 | + |
| 78 | + // Get current slot |
| 79 | + let clock = Clock::get().map_err(|_| LightSdkError::Borsh)?; |
| 80 | + let current_slot = clock.slot; |
| 81 | + |
| 82 | + // Calculate space needed for PDA |
| 83 | + let space = std::mem::size_of::<DecompressedPdaAccount>() + 8; // +8 for discriminator |
| 84 | + |
| 85 | + // Get minimum rent |
| 86 | + let rent = Rent::get().map_err(|_| LightSdkError::Borsh)?; |
| 87 | + let minimum_balance = rent.minimum_balance(space); |
| 88 | + |
| 89 | + // Create PDA account (rent payer pays for the PDA creation) |
| 90 | + let create_account_ix = system_instruction::create_account( |
| 91 | + rent_payer.key, |
| 92 | + pda_account.key, |
| 93 | + minimum_balance, |
| 94 | + space as u64, |
| 95 | + &crate::ID, |
| 96 | + ); |
| 97 | + |
| 98 | + let signer_seeds = &[ |
| 99 | + b"decompressed_pda".as_ref(), |
| 100 | + compressed_address.as_ref(), |
| 101 | + instruction_data.additional_seed.as_ref(), |
| 102 | + &[pda_bump], |
| 103 | + ]; |
| 104 | + |
| 105 | + invoke_signed( |
| 106 | + &create_account_ix, |
| 107 | + &[ |
| 108 | + rent_payer.clone(), |
| 109 | + pda_account.clone(), |
| 110 | + system_program.clone(), |
| 111 | + ], |
| 112 | + &[signer_seeds], |
| 113 | + )?; |
| 114 | + |
| 115 | + // Initialize PDA with decompressed data |
| 116 | + let decompressed_pda = DecompressedPdaAccount { |
| 117 | + compressed_address, |
| 118 | + last_written_slot: current_slot, |
| 119 | + slots_until_compression: SLOTS_UNTIL_COMPRESSION, |
| 120 | + data: instruction_data.compressed_account.data, |
| 121 | + is_decompressed: true, |
| 122 | + }; |
| 123 | + |
| 124 | + // Write data to PDA |
| 125 | + decompressed_pda |
| 126 | + .serialize(&mut &mut pda_account.try_borrow_mut_data()?[8..]) |
| 127 | + .map_err(|_| LightSdkError::Borsh)?; |
| 128 | + |
| 129 | + // Write discriminator |
| 130 | + pda_account.try_borrow_mut_data()?[..8].copy_from_slice(b"decomppd"); |
| 131 | + |
| 132 | + // Now handle the compressed account side |
| 133 | + // Create a marker account that indicates this compressed account has been decompressed |
| 134 | + let marker_account = LightAccount::<'_, DecompressedMarkerAccount>::new_mut( |
| 135 | + &crate::ID, |
| 136 | + &instruction_data.compressed_account.meta, |
| 137 | + DecompressedMarkerAccount { |
| 138 | + is_decompressed: true, |
| 139 | + }, |
| 140 | + )?; |
| 141 | + |
| 142 | + // Set up CPI accounts for light system program |
| 143 | + let mut config = CpiAccountsConfig::new(crate::LIGHT_CPI_SIGNER); |
| 144 | + config.sol_pool_pda = false; |
| 145 | + config.sol_compression_recipient = true; // We need to decompress SOL to the PDA |
| 146 | + |
| 147 | + let cpi_accounts = CpiAccounts::new_with_config( |
| 148 | + fee_payer, |
| 149 | + &accounts[instruction_data.system_accounts_offset as usize..], |
| 150 | + config, |
| 151 | + ); |
| 152 | + |
| 153 | + // Create CPI inputs with decompression |
| 154 | + let mut cpi_inputs = CpiInputs::new( |
| 155 | + instruction_data.proof, |
| 156 | + vec![marker_account.to_account_info()?], |
| 157 | + ); |
| 158 | + |
| 159 | + // Set decompression parameters |
| 160 | + // Transfer all lamports from compressed account to the PDA |
| 161 | + let lamports_to_decompress = instruction_data |
| 162 | + .compressed_account |
| 163 | + .meta |
| 164 | + .get_lamports() |
| 165 | + .unwrap_or(0); |
| 166 | + |
| 167 | + cpi_inputs.compress_or_decompress_lamports = Some(lamports_to_decompress); |
| 168 | + cpi_inputs.is_compress = false; // This is decompression |
| 169 | + |
| 170 | + // Invoke light system program |
| 171 | + cpi_inputs.invoke_light_system_program(cpi_accounts)?; |
| 172 | + |
| 173 | + msg!("Successfully decompressed account to PDA"); |
| 174 | + Ok(()) |
| 175 | +} |
| 176 | + |
| 177 | +#[derive(Clone, Debug, Default, BorshDeserialize, BorshSerialize)] |
| 178 | +pub struct DecompressToPdaInstructionData { |
| 179 | + pub proof: ValidityProof, |
| 180 | + pub compressed_account: DecompressMyCompressedAccount, |
| 181 | + pub additional_seed: [u8; 32], // Additional seed for PDA derivation |
| 182 | + pub system_accounts_offset: u8, |
| 183 | +} |
| 184 | + |
| 185 | +#[derive(Clone, Debug, Default, BorshDeserialize, BorshSerialize)] |
| 186 | +pub struct DecompressMyCompressedAccount { |
| 187 | + pub meta: CompressedAccountMeta, |
| 188 | + pub data: [u8; 31], |
| 189 | +} |
0 commit comments