Skip to content

Commit

Permalink
remove proof-program feature in token-client
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed Oct 27, 2023
1 parent 9eaf238 commit e299bb9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 248 deletions.
19 changes: 12 additions & 7 deletions token/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ solana-rpc-client-api = "1.17.2"
solana-sdk = "1.17.2"
# We never want the entrypoint for ATA, but we want the entrypoint for token when
# testing token
spl-associated-token-account = { version = "2.0", path = "../../associated-token-account/program", features = ["no-entrypoint"] }
spl-memo = { version = "4.0.0", path = "../../memo/program", features = ["no-entrypoint"] }
spl-token = { version = "4.0", path="../program", features = [ "no-entrypoint" ] }
spl-token-2022 = { version = "0.9", path="../program-2022" }
spl-token-metadata-interface = { version = "0.2", path="../../token-metadata/interface" }
spl-transfer-hook-interface = { version = "0.3", path="../transfer-hook/interface" }
spl-associated-token-account = { version = "2.0", path = "../../associated-token-account/program", features = [
"no-entrypoint",
] }
spl-memo = { version = "4.0.0", path = "../../memo/program", features = [
"no-entrypoint",
] }
spl-token = { version = "4.0", path = "../program", features = [
"no-entrypoint",
] }
spl-token-2022 = { version = "0.9", path = "../program-2022" }
spl-token-metadata-interface = { version = "0.2", path = "../../token-metadata/interface" }
spl-transfer-hook-interface = { version = "0.3", path = "../transfer-hook/interface" }
thiserror = "1.0"

[features]
default = ["display"]
display = ["dep:solana-cli-output"]
proof-program = ["spl-token-2022/proof-program"]
241 changes: 0 additions & 241 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,6 @@ use {
},
thiserror::Error,
};
#[cfg(feature = "proof-program")]
use {
solana_sdk::epoch_info::EpochInfo,
spl_token_2022::solana_zk_token_sdk::{
encryption::{auth_encryption::*, elgamal::*},
instruction::transfer_with_fee::FeeParameters,
},
std::convert::TryInto,
};

#[derive(Error, Debug)]
pub enum TokenError {
Expand Down Expand Up @@ -1859,191 +1850,6 @@ where
.await
}

/// Fetch and decrypt the available balance of a confidential token account using the uniquely
/// derived decryption key from a signer
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_get_available_balance<S: Signer>(
&self,
token_account: &Pubkey,
authority: &S,
) -> TokenResult<u64> {
let authenticated_encryption_key =
AeKey::new(authority, token_account).map_err(TokenError::Key)?;

self.confidential_transfer_get_available_balance_with_key(
token_account,
&authenticated_encryption_key,
)
.await
}

/// Fetch and decrypt the available balance of a confidential token account using a custom
/// decryption key
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_get_available_balance_with_key(
&self,
token_account: &Pubkey,
authenticated_encryption_key: &AeKey,
) -> TokenResult<u64> {
let state = self.get_account_info(token_account).await.unwrap();
let extension =
state.get_extension::<confidential_transfer::ConfidentialTransferAccount>()?;

let decryptable_balance_ciphertext: AeCiphertext = extension
.decryptable_available_balance
.try_into()
.map_err(TokenError::Proof)?;
let decryptable_balance = decryptable_balance_ciphertext
.decrypt(authenticated_encryption_key)
.ok_or(TokenError::AccountDecryption)?;

Ok(decryptable_balance)
}

/// Fetch and decrypt the pending balance of a confidential token account using the uniquely
/// derived decryption key from a signer
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_get_pending_balance<S: Signer>(
&self,
token_account: &Pubkey,
authority: &S,
) -> TokenResult<u64> {
let elgamal_keypair =
ElGamalKeypair::new(authority, token_account).map_err(TokenError::Key)?;

self.confidential_transfer_get_pending_balance_with_key(token_account, &elgamal_keypair)
.await
}

/// Fetch and decrypt the pending balance of a confidential token account using a custom
/// decryption key
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_get_pending_balance_with_key(
&self,
token_account: &Pubkey,
elgamal_keypair: &ElGamalKeypair,
) -> TokenResult<u64> {
let state = self.get_account_info(token_account).await.unwrap();
let extension =
state.get_extension::<confidential_transfer::ConfidentialTransferAccount>()?;

// decrypt pending balance
let pending_balance_lo = extension
.pending_balance_lo
.decrypt(&elgamal_keypair.secret)
.ok_or(TokenError::AccountDecryption)?;
let pending_balance_hi = extension
.pending_balance_hi
.decrypt(&elgamal_keypair.secret)
.ok_or(TokenError::AccountDecryption)?;

let pending_balance = pending_balance_lo
.checked_add(pending_balance_hi << confidential_transfer::PENDING_BALANCE_HI_BIT_LENGTH)
.ok_or(TokenError::AccountDecryption)?;

Ok(pending_balance)
}

#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_get_withheld_amount<S: Signer>(
&self,
withdraw_withheld_authority: &S,
sources: &[&Pubkey],
) -> TokenResult<u64> {
let withdraw_withheld_authority_elgamal_keypair =
ElGamalKeypair::new(withdraw_withheld_authority, &self.pubkey)
.map_err(TokenError::Key)?;

self.confidential_transfer_get_withheld_amount_with_key(
&withdraw_withheld_authority_elgamal_keypair,
sources,
)
.await
}

#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_get_withheld_amount_with_key(
&self,
withdraw_withheld_authority_elgamal_keypair: &ElGamalKeypair,
sources: &[&Pubkey],
) -> TokenResult<u64> {
let mut aggregate_withheld_amount_ciphertext = ElGamalCiphertext::default();
for &source in sources {
let state = self.get_account_info(source).await.unwrap();
let extension =
state.get_extension::<confidential_transfer::ConfidentialTransferAccount>()?;

let withheld_amount_ciphertext: ElGamalCiphertext =
extension.withheld_amount.try_into().unwrap();

aggregate_withheld_amount_ciphertext =
aggregate_withheld_amount_ciphertext + withheld_amount_ciphertext;
}

let aggregate_withheld_amount = aggregate_withheld_amount_ciphertext
.decrypt_u32(&withdraw_withheld_authority_elgamal_keypair.secret)
.ok_or(TokenError::AccountDecryption)?;

Ok(aggregate_withheld_amount)
}

/// Fetch the ElGamal public key associated with a confidential token account
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_get_elgamal_pubkey(
&self,
token_account: &Pubkey,
) -> TokenResult<ElGamalPubkey> {
let state = self.get_account_info(token_account).await.unwrap();
let extension =
state.get_extension::<confidential_transfer::ConfidentialTransferAccount>()?;
let elgamal_pubkey = extension
.elgamal_pubkey
.try_into()
.map_err(TokenError::Proof)?;

Ok(elgamal_pubkey)
}

/// Fetch the ElGamal pubkey key of the auditor associated with a confidential token mint
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_get_auditor_elgamal_pubkey(
&self,
) -> TokenResult<Option<ElGamalPubkey>> {
let mint_state = self.get_mint_info().await.unwrap();
let ct_mint =
mint_state.get_extension::<confidential_transfer::ConfidentialTransferMint>()?;
let auditor_elgamal_pubkey: Option<ElGamalPubkey> = ct_mint.auditor_elgamal_pubkey.into();

if let Some(elgamal_pubkey) = auditor_elgamal_pubkey {
let elgamal_pubkey: ElGamalPubkey =
elgamal_pubkey.try_into().map_err(TokenError::Proof)?;
Ok(Some(elgamal_pubkey))
} else {
Ok(None)
}
}

/// Fetch the ElGamal pubkey key of the withdraw withheld authority associated with a
/// confidential token mint
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_get_withdraw_withheld_authority_elgamal_pubkey(
&self,
) -> TokenResult<Option<ElGamalPubkey>> {
let mint_state = self.get_mint_info().await.unwrap();
let ct_mint =
mint_state.get_extension::<confidential_transfer::ConfidentialTransferMint>()?;
let withdraw_withheld_authority_elgamal_pubkey: Option<ElGamalPubkey> =
ct_mint.withdraw_withheld_authority_elgamal_pubkey.into();

if let Some(elgamal_pubkey) = withdraw_withheld_authority_elgamal_pubkey {
let elgamal_pubkey: ElGamalPubkey =
elgamal_pubkey.try_into().map_err(TokenError::Proof)?;
Ok(Some(elgamal_pubkey))
} else {
Ok(None)
}
}

/// Deposit SPL Tokens into the pending balance of a confidential token account
pub async fn confidential_transfer_deposit<S: Signers>(
&self,
Expand Down Expand Up @@ -2135,53 +1941,6 @@ where
.await
}

/// Withdraw SPL Tokens from the available balance of a confidential token account using custom
/// keys
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_withdraw_with_key<S: Signers>(
&self,
token_account: &Pubkey,
token_authority: &Pubkey,
amount: u64,
decimals: u8,
available_balance: u64,
available_balance_ciphertext: &ElGamalCiphertext,
elgamal_keypair: &ElGamalKeypair,
authenticated_encryption_key: &AeKey,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let proof_data = confidential_transfer::instruction::WithdrawData::new(
amount,
elgamal_keypair,
available_balance,
available_balance_ciphertext,
)
.map_err(TokenError::Proof)?;

let remaining_balance = available_balance
.checked_sub(amount)
.ok_or(TokenError::NotEnoughFunds)?;
let new_decryptable_available_balance =
authenticated_encryption_key.encrypt(remaining_balance);

self.process_ixs(
&confidential_transfer::instruction::withdraw(
&self.program_id,
token_account,
&self.pubkey,
amount,
decimals,
new_decryptable_available_balance,
token_authority,
&[],
&proof_data,
)?,
signing_keypairs,
)
.await
}

/// Transfer tokens confidentially
#[allow(clippy::too_many_arguments)]
pub async fn confidential_transfer_transfer<S: Signers>(
Expand Down

0 comments on commit e299bb9

Please sign in to comment.