Skip to content

Commit

Permalink
[token-2022] Create proof-program feature (solana-labs#4040)
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto authored and HaoranYi committed Jul 19, 2023
1 parent e8e77b8 commit 81f2071
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 42 deletions.
1 change: 1 addition & 0 deletions token/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ thiserror = "1.0"
[features]
default = ["display"]
display = ["dep:solana-cli-output"]
proof-program = []
50 changes: 43 additions & 7 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use {
solana_program_test::tokio::time,
solana_sdk::{
account::Account as BaseAccount,
epoch_info::EpochInfo,
hash::Hash,
instruction::Instruction,
message::Message,
Expand All @@ -26,21 +25,25 @@ use {
},
instruction,
pod::EncryptionPubkey,
solana_zk_token_sdk::{
encryption::{auth_encryption::*, elgamal::*},
errors::ProofError,
instruction::transfer_with_fee::FeeParameters,
},
solana_zk_token_sdk::errors::ProofError,
state::{Account, AccountState, Mint, Multisig},
},
std::{
convert::TryInto,
fmt, io,
sync::{Arc, RwLock},
time::{Duration, Instant},
},
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 @@ -1516,6 +1519,7 @@ where
}

/// Configures confidential transfers for a token account
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_configure_token_account<S: Signer>(
&self,
token_account: &Pubkey,
Expand All @@ -1532,6 +1536,7 @@ where
.await
}

#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_configure_token_account_with_pending_counter<S: Signer>(
&self,
token_account: &Pubkey,
Expand All @@ -1554,6 +1559,7 @@ where
.await
}

#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_configure_token_account_with_pending_counter_and_keypair<
S: Signer,
>(
Expand Down Expand Up @@ -1585,6 +1591,7 @@ where
}

/// Approves a token account for confidential transfers
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_approve_account<S: Signer>(
&self,
token_account: &Pubkey,
Expand All @@ -1603,6 +1610,7 @@ where
}

/// Prepare a token account with the confidential transfer extension for closing
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_empty_account<S: Signer>(
&self,
token_account: &Pubkey,
Expand All @@ -1618,6 +1626,7 @@ where
.await
}

#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_empty_account_with_keypair<S: Signer>(
&self,
token_account: &Pubkey,
Expand Down Expand Up @@ -1649,6 +1658,7 @@ where

/// 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,
Expand All @@ -1666,6 +1676,7 @@ where

/// 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,
Expand All @@ -1688,6 +1699,7 @@ where

/// 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,
Expand All @@ -1702,6 +1714,7 @@ where

/// 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,
Expand All @@ -1728,6 +1741,7 @@ where
Ok(pending_balance)
}

#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_get_withheld_amount<S: Signer>(
&self,
withdraw_withheld_authority: &S,
Expand All @@ -1744,6 +1758,7 @@ where
.await
}

#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_get_withheld_amount_with_key(
&self,
withdraw_withheld_authority_elgamal_keypair: &ElGamalKeypair,
Expand All @@ -1770,6 +1785,7 @@ where
}

/// Fetch the ElGamal public key associated with a confidential token account
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_get_encryption_pubkey<S: Signer>(
&self,
token_account: &Pubkey,
Expand All @@ -1786,6 +1802,7 @@ where
}

/// 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_encryption_pubkey<S: Signer>(
&self,
) -> TokenResult<Option<ElGamalPubkey>> {
Expand All @@ -1806,6 +1823,7 @@ where

/// 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_encryption_pubkey<
S: Signer,
>(
Expand All @@ -1827,6 +1845,7 @@ where
}

/// Deposit SPL Tokens into the pending balance of a confidential token account
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_deposit<S: Signer>(
&self,
token_account: &Pubkey,
Expand Down Expand Up @@ -1856,6 +1875,7 @@ where
/// Withdraw SPL Tokens from the available balance of a confidential token account using the
/// uniquely derived decryption key from a signer
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_withdraw<S: Signer>(
&self,
token_account: &Pubkey,
Expand Down Expand Up @@ -1886,6 +1906,7 @@ where
/// 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: Signer>(
&self,
token_account: &Pubkey,
Expand Down Expand Up @@ -1930,6 +1951,7 @@ where

/// Transfer tokens confidentially using the uniquely derived decryption keys from a signer
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_transfer<S: Signer>(
&self,
source_token_account: &Pubkey,
Expand Down Expand Up @@ -1964,6 +1986,7 @@ where

/// Transfer tokens confidentially using custom decryption keys
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_transfer_with_key<S: Signer>(
&self,
source_token_account: &Pubkey,
Expand Down Expand Up @@ -2019,6 +2042,7 @@ where
/// Transfer tokens confidentially with fee using the uniquely derived decryption keys from a
/// signer
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_transfer_with_fee<S: Signer>(
&self,
source_token_account: &Pubkey,
Expand Down Expand Up @@ -2057,6 +2081,7 @@ where

/// Transfer tokens confidential with fee using custom decryption keys
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_transfer_with_fee_with_key<S: Signer>(
&self,
source_token_account: &Pubkey,
Expand Down Expand Up @@ -2124,6 +2149,7 @@ where

/// Applies the confidential transfer pending balance to the available balance using the
/// uniquely derived decryption key
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_apply_pending_balance<S: Signer>(
&self,
token_account: &Pubkey,
Expand All @@ -2148,6 +2174,7 @@ where

/// Applies the confidential transfer pending balance to the available balance using a custom
/// decryption key
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_apply_pending_balance_with_key<S: Signer>(
&self,
token_account: &Pubkey,
Expand Down Expand Up @@ -2176,6 +2203,7 @@ where
}

/// Enable confidential transfer `Deposit` and `Transfer` instructions for a token account
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_enable_confidential_credits<S: Signer>(
&self,
token_account: &Pubkey,
Expand All @@ -2196,6 +2224,7 @@ where
}

/// Disable confidential transfer `Deposit` and `Transfer` instructions for a token account
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_disable_confidential_credits<S: Signer>(
&self,
token_account: &Pubkey,
Expand All @@ -2216,6 +2245,7 @@ where
}

/// Enable a confidential extension token account to receive non-confidential payments
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_enable_non_confidential_credits<S: Signer>(
&self,
token_account: &Pubkey,
Expand All @@ -2236,6 +2266,7 @@ where
}

/// Disable non-confidential payments for a confidential extension token account
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_disable_non_confidential_credits<S: Signer>(
&self,
token_account: &Pubkey,
Expand All @@ -2256,6 +2287,7 @@ where
}

/// Withdraw withheld confidential tokens from mint using the uniquely derived decryption key
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_withdraw_withheld_tokens_from_mint<S: Signer>(
&self,
withdraw_withheld_authority: &S,
Expand All @@ -2281,6 +2313,7 @@ where
}

/// Withdraw withheld confidential tokens from mint using a custom decryption key
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_withdraw_withheld_tokens_from_mint_with_key<S: Signer>(
&self,
withdraw_withheld_authority: &S,
Expand Down Expand Up @@ -2314,6 +2347,7 @@ where

/// Withdraw withheld confidential tokens from accounts using the uniquely derived decryption
/// key
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_withdraw_withheld_tokens_from_accounts<S: Signer>(
&self,
withdraw_withheld_authority: &S,
Expand Down Expand Up @@ -2341,6 +2375,7 @@ where

/// Withdraw withheld confidential tokens from accounts using a custom decryption key
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_withdraw_withheld_tokens_from_accounts_with_key<
S: Signer,
>(
Expand Down Expand Up @@ -2377,6 +2412,7 @@ where
}

/// Harvest withheld confidential tokens to mint
#[cfg(feature = "proof-program")]
pub async fn confidential_transfer_harvest_withheld_tokens_to_mint(
&self,
sources: &[&Pubkey],
Expand Down
3 changes: 2 additions & 1 deletion token/program-2022-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ repository = "https://github.com/solana-labs/solana-program-library"
version = "0.0.1"

[features]
test-sbf = []
test-sbf = ["zk-ops"]
default = ["zk-ops"]
zk-ops = []
proof-program = []

[build-dependencies]
walkdir = "2"
Expand Down
2 changes: 1 addition & 1 deletion token/program-2022-test/tests/confidential_transfer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(feature = "test-sbf")]
#![cfg(all(feature = "test-sbf", feature = "proof-program"))]
#![cfg(twoxtx)]

mod program_test;
Expand Down
1 change: 1 addition & 0 deletions token/program-2022/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ serde-traits = ["serde", "serde_with"]
# Remove these features once the underlying syscalls are released on all networks
default = ["zk-ops"]
zk-ops = []
proof-program = []

[dependencies]
arrayref = "0.3.6"
Expand Down
Loading

0 comments on commit 81f2071

Please sign in to comment.