Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit b9aba3f

Browse files
[confidential-extension] Use OptionalNonZeroPubkey and OptionalNonZeroEncryptionPubkey for confidential extension (#3943)
* use OptionalNonZeroPubkey for the confidential mint authority pubkey * add OptionalNonZeroEncryptionPubkey * update tests * Update token/program-2022/src/pod.rs Co-authored-by: Jon Cinque <[email protected]> * remove unnecessary COption convertion for OptionalNonZeroEncryptionPubkey Co-authored-by: Jon Cinque <[email protected]>
1 parent 19b8fae commit b9aba3f

File tree

6 files changed

+263
-184
lines changed

6 files changed

+263
-184
lines changed

token/client/src/token.rs

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ use {
2020
},
2121
spl_token_2022::{
2222
extension::{
23-
confidential_transfer, confidential_transfer::EncryptionPubkey, cpi_guard,
24-
default_account_state, interest_bearing_mint, memo_transfer, transfer_fee,
25-
BaseStateWithExtensions, ExtensionType, StateWithExtensionsOwned,
23+
confidential_transfer, cpi_guard, default_account_state, interest_bearing_mint,
24+
memo_transfer, transfer_fee, BaseStateWithExtensions, ExtensionType,
25+
StateWithExtensionsOwned,
2626
},
2727
instruction,
28+
pod::EncryptionPubkey,
2829
solana_zk_token_sdk::{
2930
encryption::{auth_encryption::*, elgamal::*},
3031
errors::ProofError,
@@ -106,8 +107,8 @@ pub enum ExtensionInitializationParams {
106107
ConfidentialTransferMint {
107108
authority: Option<Pubkey>,
108109
auto_approve_new_accounts: bool,
109-
auditor_encryption_pubkey: EncryptionPubkey,
110-
withdraw_withheld_authority_encryption_pubkey: EncryptionPubkey,
110+
auditor_encryption_pubkey: Option<EncryptionPubkey>,
111+
withdraw_withheld_authority_encryption_pubkey: Option<EncryptionPubkey>,
111112
},
112113
DefaultAccountState {
113114
state: AccountState,
@@ -158,10 +159,10 @@ impl ExtensionInitializationParams {
158159
} => confidential_transfer::instruction::initialize_mint(
159160
token_program_id,
160161
mint,
161-
authority.as_ref(),
162+
authority,
162163
auto_approve_new_accounts,
163-
&auditor_encryption_pubkey,
164-
&withdraw_withheld_authority_encryption_pubkey,
164+
auditor_encryption_pubkey,
165+
withdraw_withheld_authority_encryption_pubkey,
165166
),
166167
Self::DefaultAccountState { state } => {
167168
default_account_state::instruction::initialize_default_account_state(
@@ -1492,7 +1493,7 @@ where
14921493
authority: &S,
14931494
new_authority: Option<&S>,
14941495
auto_approve_new_account: bool,
1495-
auditor_encryption_pubkey: &EncryptionPubkey,
1496+
auditor_encryption_pubkey: Option<EncryptionPubkey>,
14961497
) -> TokenResult<T::Output> {
14971498
let mut signers = vec![authority];
14981499
let new_authority_pubkey = if let Some(new_authority) = new_authority {
@@ -1789,16 +1790,20 @@ where
17891790
/// Fetch the ElGamal pubkey key of the auditor associated with a confidential token mint
17901791
pub async fn confidential_transfer_get_auditor_encryption_pubkey<S: Signer>(
17911792
&self,
1792-
) -> TokenResult<ElGamalPubkey> {
1793+
) -> TokenResult<Option<ElGamalPubkey>> {
17931794
let mint_state = self.get_mint_info().await.unwrap();
17941795
let ct_mint =
17951796
mint_state.get_extension::<confidential_transfer::ConfidentialTransferMint>()?;
1796-
let auditor_pubkey = ct_mint
1797-
.auditor_encryption_pubkey
1798-
.try_into()
1799-
.map_err(TokenError::Proof)?;
1797+
let auditor_encryption_pubkey: Option<EncryptionPubkey> =
1798+
ct_mint.auditor_encryption_pubkey.into();
18001799

1801-
Ok(auditor_pubkey)
1800+
if let Some(encryption_pubkey) = auditor_encryption_pubkey {
1801+
let encryption_pubkey: ElGamalPubkey =
1802+
encryption_pubkey.try_into().map_err(TokenError::Proof)?;
1803+
Ok(Some(encryption_pubkey))
1804+
} else {
1805+
Ok(None)
1806+
}
18021807
}
18031808

18041809
/// Fetch the ElGamal pubkey key of the withdraw withheld authority associated with a
@@ -1807,16 +1812,20 @@ where
18071812
S: Signer,
18081813
>(
18091814
&self,
1810-
) -> TokenResult<ElGamalPubkey> {
1815+
) -> TokenResult<Option<ElGamalPubkey>> {
18111816
let mint_state = self.get_mint_info().await.unwrap();
18121817
let ct_mint =
18131818
mint_state.get_extension::<confidential_transfer::ConfidentialTransferMint>()?;
1814-
let auditor_pubkey = ct_mint
1815-
.withdraw_withheld_authority_encryption_pubkey
1816-
.try_into()
1817-
.map_err(TokenError::Proof)?;
1819+
let withdraw_withheld_authority_encryption_pubkey: Option<EncryptionPubkey> =
1820+
ct_mint.withdraw_withheld_authority_encryption_pubkey.into();
18181821

1819-
Ok(auditor_pubkey)
1822+
if let Some(encryption_pubkey) = withdraw_withheld_authority_encryption_pubkey {
1823+
let encryption_pubkey: ElGamalPubkey =
1824+
encryption_pubkey.try_into().map_err(TokenError::Proof)?;
1825+
Ok(Some(encryption_pubkey))
1826+
} else {
1827+
Ok(None)
1828+
}
18201829
}
18211830

18221831
/// Deposit SPL Tokens into the pending balance of a confidential token account
@@ -1932,7 +1941,7 @@ where
19321941
source_available_balance: u64,
19331942
source_available_balance_ciphertext: &ElGamalCiphertext,
19341943
destination_elgamal_pubkey: &ElGamalPubkey,
1935-
auditor_elgamal_pubkey: &ElGamalPubkey,
1944+
auditor_elgamal_pubkey: Option<ElGamalPubkey>,
19361945
) -> TokenResult<T::Output> {
19371946
let source_elgamal_keypair =
19381947
ElGamalKeypair::new(source_token_authority, source_token_account)
@@ -1966,22 +1975,24 @@ where
19661975
source_available_balance: u64,
19671976
source_available_balance_ciphertext: &ElGamalCiphertext,
19681977
destination_elgamal_pubkey: &ElGamalPubkey,
1969-
auditor_elgamal_pubkey: &ElGamalPubkey,
1978+
auditor_elgamal_pubkey: Option<ElGamalPubkey>,
19701979
source_elgamal_keypair: &ElGamalKeypair,
19711980
source_authenticated_encryption_key: &AeKey,
19721981
) -> TokenResult<T::Output> {
19731982
if amount >> confidential_transfer::MAXIMUM_DEPOSIT_TRANSFER_AMOUNT_BIT_LENGTH != 0 {
19741983
return Err(TokenError::MaximumDepositTransferAmountExceeded);
19751984
}
19761985

1986+
let auditor_elgamal_pubkey = auditor_elgamal_pubkey.unwrap_or_default();
1987+
19771988
let proof_data = confidential_transfer::instruction::TransferData::new(
19781989
amount,
19791990
(
19801991
source_available_balance,
19811992
source_available_balance_ciphertext,
19821993
),
19831994
source_elgamal_keypair,
1984-
(destination_elgamal_pubkey, auditor_elgamal_pubkey),
1995+
(destination_elgamal_pubkey, &auditor_elgamal_pubkey),
19851996
)
19861997
.map_err(TokenError::Proof)?;
19871998

@@ -2019,7 +2030,7 @@ where
20192030
source_available_balance: u64,
20202031
source_available_balance_ciphertext: &ElGamalCiphertext,
20212032
destination_elgamal_pubkey: &ElGamalPubkey,
2022-
auditor_elgamal_pubkey: &ElGamalPubkey,
2033+
auditor_elgamal_pubkey: Option<ElGamalPubkey>,
20232034
withdraw_withheld_authority_elgamal_pubkey: &ElGamalPubkey,
20242035
epoch_info: &EpochInfo,
20252036
) -> TokenResult<T::Output> {
@@ -2057,7 +2068,7 @@ where
20572068
source_available_balance: u64,
20582069
source_available_balance_ciphertext: &ElGamalCiphertext,
20592070
destination_elgamal_pubkey: &ElGamalPubkey,
2060-
auditor_elgamal_pubkey: &ElGamalPubkey,
2071+
auditor_elgamal_pubkey: Option<ElGamalPubkey>,
20612072
withdraw_withheld_authority_elgamal_pubkey: &ElGamalPubkey,
20622073
source_elgamal_keypair: &ElGamalKeypair,
20632074
source_authenticated_encryption_key: &AeKey,
@@ -2067,7 +2078,8 @@ where
20672078
return Err(TokenError::MaximumDepositTransferAmountExceeded);
20682079
}
20692080

2070-
// TODO: take transfer fee params as input
2081+
let auditor_elgamal_pubkey = auditor_elgamal_pubkey.unwrap_or_default();
2082+
20712083
let mint_state = self.get_mint_info().await.unwrap();
20722084
let transfer_fee_config = mint_state
20732085
.get_extension::<transfer_fee::TransferFeeConfig>()
@@ -2081,7 +2093,7 @@ where
20812093
source_available_balance_ciphertext,
20822094
),
20832095
source_elgamal_keypair,
2084-
(destination_elgamal_pubkey, auditor_elgamal_pubkey),
2096+
(destination_elgamal_pubkey, &auditor_elgamal_pubkey),
20852097
FeeParameters {
20862098
fee_rate_basis_points: u16::from(fee_parameters.transfer_fee_basis_points),
20872099
maximum_fee: u64::from(fee_parameters.maximum_fee),

0 commit comments

Comments
 (0)