Skip to content

Commit da39e89

Browse files
[program] Add cpi guard logic in confidential extensions (#819)
add cpi guard logic in confidential extensions
1 parent 19136a5 commit da39e89

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

program/src/extension/confidential_mint_burn/processor.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
#[cfg(feature = "zk-ops")]
2+
use {
3+
crate::{
4+
check_auditor_ciphertext,
5+
extension::confidential_mint_burn::verify_proof::{verify_burn_proof, verify_mint_proof},
6+
},
7+
spl_token_confidential_transfer_ciphertext_arithmetic as ciphertext_arithmetic,
8+
};
19
use {
2-
crate::processor::Processor,
10+
crate::{extension::cpi_guard::in_cpi, processor::Processor},
311
solana_account_info::{next_account_info, AccountInfo},
412
solana_msg::msg,
513
solana_program_error::{ProgramError, ProgramResult},
@@ -26,6 +34,7 @@ use {
2634
ConfidentialMintBurn,
2735
},
2836
confidential_transfer::{ConfidentialTransferAccount, ConfidentialTransferMint},
37+
cpi_guard::CpiGuard,
2938
pausable::PausableConfig,
3039
BaseStateWithExtensions, BaseStateWithExtensionsMut, PodStateWithExtensionsMut,
3140
},
@@ -34,14 +43,6 @@ use {
3443
},
3544
spl_token_confidential_transfer_proof_extraction::instruction::verify_and_extract_context,
3645
};
37-
#[cfg(feature = "zk-ops")]
38-
use {
39-
crate::{
40-
check_auditor_ciphertext,
41-
extension::confidential_mint_burn::verify_proof::{verify_burn_proof, verify_mint_proof},
42-
},
43-
spl_token_confidential_transfer_ciphertext_arithmetic as ciphertext_arithmetic,
44-
};
4546

4647
/// Processes an [`InitializeMint`] instruction.
4748
fn process_initialize_mint(accounts: &[AccountInfo], data: &InitializeMintData) -> ProgramResult {
@@ -337,6 +338,17 @@ fn process_confidential_burn(
337338
account_info_iter.as_slice(),
338339
)?;
339340

341+
if let Ok(cpi_guard) = token_account.get_extension::<CpiGuard>() {
342+
// Blocks all cases where the authority has signed if CPI Guard is
343+
// enabled, including:
344+
// * the account is delegated to the owner
345+
// * the account owner is the permanent delegate
346+
if *authority_info.key == token_account.base.owner && cpi_guard.lock_cpi.into() && in_cpi()
347+
{
348+
return Err(TokenError::CpiGuardBurnBlocked.into());
349+
}
350+
}
351+
340352
if token_account.base.is_frozen() {
341353
return Err(TokenError::AccountFrozen.into());
342354
}

program/src/extension/confidential_transfer/processor.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use {
1111
crate::{
1212
check_elgamal_registry_program_account,
1313
extension::{
14-
confidential_transfer::verify_proof::*,
14+
confidential_transfer::verify_proof::*, cpi_guard::in_cpi,
1515
memo_transfer::check_previous_sibling_instruction_is_memo,
1616
},
1717
processor::Processor,
@@ -44,6 +44,7 @@ use {
4444
ConfidentialTransferFeeAmount, ConfidentialTransferFeeConfig,
4545
EncryptedWithheldAmount,
4646
},
47+
cpi_guard::CpiGuard,
4748
memo_transfer::memo_required,
4849
pausable::PausableConfig,
4950
set_account_type,
@@ -872,6 +873,17 @@ fn process_source_for_transfer(
872873
signers,
873874
)?;
874875

876+
if let Ok(cpi_guard) = token_account.get_extension::<CpiGuard>() {
877+
// Blocks all cases where the authority has signed if CPI Guard is
878+
// enabled, including:
879+
// * the account is delegated to the owner
880+
// * the account owner is the permanent delegate
881+
if *authority_info.key == token_account.base.owner && cpi_guard.lock_cpi.into() && in_cpi()
882+
{
883+
return Err(TokenError::CpiGuardTransferBlocked.into());
884+
}
885+
}
886+
875887
if token_account.base.is_frozen() {
876888
return Err(TokenError::AccountFrozen.into());
877889
}
@@ -1008,6 +1020,17 @@ fn process_source_for_transfer_with_fee(
10081020
signers,
10091021
)?;
10101022

1023+
if let Ok(cpi_guard) = token_account.get_extension::<CpiGuard>() {
1024+
// Blocks all cases where the authority has signed if CPI Guard is
1025+
// enabled, including:
1026+
// * the account is delegated to the owner
1027+
// * the account owner is the permanent delegate
1028+
if *authority_info.key == token_account.base.owner && cpi_guard.lock_cpi.into() && in_cpi()
1029+
{
1030+
return Err(TokenError::CpiGuardTransferBlocked.into());
1031+
}
1032+
}
1033+
10111034
if token_account.base.is_frozen() {
10121035
return Err(TokenError::AccountFrozen.into());
10131036
}

0 commit comments

Comments
 (0)