Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
64 changes: 64 additions & 0 deletions clients/rust-legacy/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4039,6 +4039,70 @@ where
));
self.process_ixs(&instructions, signing_keypairs).await
}

/// Get the pending balance for a confidential transfer account.
///
/// This decrypts and combines the low 16 bits and high 48 bits of the pending balance
/// into a single u64 value.
pub async fn confidential_transfer_get_pending_balance(
&self,
account: &Pubkey,
elgamal_secret_key: &ElGamalSecretKey,
) -> TokenResult<u64> {
let account_info = self.get_account_info(account).await?;
let confidential_transfer_account = account_info.get_extension::<ConfidentialTransferAccount>()?;
let account_info = ApplyPendingBalanceAccountInfo::new(confidential_transfer_account);

account_info.get_pending_balance(elgamal_secret_key).map_err(|_| TokenError::AccountDecryption)
}

/// Get the available balance for a confidential transfer account.
///
/// This decrypts the decryptable available balance using the provided AES key.
pub async fn confidential_transfer_get_available_balance(
&self,
account: &Pubkey,
aes_key: &AeKey,
) -> TokenResult<u64> {
let account_info = self.get_account_info(account).await?;
let confidential_transfer_account = account_info.get_extension::<ConfidentialTransferAccount>()?;
let account_info = ApplyPendingBalanceAccountInfo::new(confidential_transfer_account);

account_info.get_available_balance(aes_key).map_err(|_| TokenError::AccountDecryption)
}

/// Get the total balance (pending + available) for a confidential transfer account.
///
/// This combines both pending and available balances with overflow protection.
pub async fn confidential_transfer_get_total_balance(
&self,
account: &Pubkey,
elgamal_secret_key: &ElGamalSecretKey,
aes_key: &AeKey,
) -> TokenResult<u64> {
let account_info = self.get_account_info(account).await?;
let confidential_transfer_account = account_info.get_extension::<ConfidentialTransferAccount>()?;
let account_info = ApplyPendingBalanceAccountInfo::new(confidential_transfer_account);

account_info.get_total_balance(elgamal_secret_key, aes_key).map_err(|e| match e {
spl_token_2022::error::TokenError::Overflow => TokenError::AccountDecryption,
_ => TokenError::AccountDecryption,
})
}

/// Check if a confidential transfer account has any pending balance.
///
/// This checks if pending_balance_credit_counter > 0.
pub async fn confidential_transfer_has_pending_balance(
&self,
account: &Pubkey,
) -> TokenResult<bool> {
let account_info = self.get_account_info(account).await?;
let confidential_transfer_account = account_info.get_extension::<ConfidentialTransferAccount>()?;
let account_info = ApplyPendingBalanceAccountInfo::new(confidential_transfer_account);

Ok(account_info.has_pending_balance())
}
}

/// Calculates the maximum chunk size for a zero-knowledge proof record
Expand Down
Loading