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

Commit 0580f5b

Browse files
committed
clean up context state constructor functions
1 parent 082e33b commit 0580f5b

File tree

2 files changed

+21
-317
lines changed

2 files changed

+21
-317
lines changed

token/client/src/token.rs

Lines changed: 16 additions & 316 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use {
4747
BaseStateWithExtensions, Extension, ExtensionType, StateWithExtensionsOwned,
4848
},
4949
instruction, offchain,
50-
proof::{ProofData, ProofLocation},
50+
proof::{zk_proof_type_to_instruction, ProofData, ProofLocation},
5151
solana_zk_sdk::{
5252
encryption::{
5353
auth_encryption::AeKey,
@@ -56,7 +56,7 @@ use {
5656
},
5757
zk_elgamal_proof_program::{
5858
self,
59-
instruction::{close_context_state, ContextStateInfo, ProofInstruction},
59+
instruction::{close_context_state, ContextStateInfo},
6060
proof_data::*,
6161
state::ProofContextState,
6262
},
@@ -2347,69 +2347,28 @@ where
23472347
.await
23482348
}
23492349

2350-
/// Create an equality proof context state account for a confidential
2351-
/// transfer.
2352-
#[allow(clippy::too_many_arguments)]
2353-
pub async fn create_equality_proof_context_state_for_transfer<S: Signer>(
2354-
&self,
2355-
context_state_account: &Pubkey,
2356-
context_state_authority: &Pubkey,
2357-
equality_proof_data: &CiphertextCommitmentEqualityProofData,
2358-
equality_proof_signer: &S,
2359-
) -> TokenResult<T::Output> {
2360-
// create equality proof context state
2361-
let instruction_type = ProofInstruction::VerifyCiphertextCommitmentEquality;
2362-
let space = size_of::<ProofContextState<CiphertextCommitmentEqualityProofContext>>();
2363-
let rent = self
2364-
.client
2365-
.get_minimum_balance_for_rent_exemption(space)
2366-
.await
2367-
.map_err(TokenError::Client)?;
2368-
2369-
let equality_proof_context_state_info = ContextStateInfo {
2370-
context_state_account,
2371-
context_state_authority,
2372-
};
2373-
2374-
self.process_ixs(
2375-
&[
2376-
system_instruction::create_account(
2377-
&self.payer.pubkey(),
2378-
context_state_account,
2379-
rent,
2380-
space as u64,
2381-
&zk_elgamal_proof_program::id(),
2382-
),
2383-
instruction_type.encode_verify_proof(
2384-
Some(equality_proof_context_state_info),
2385-
equality_proof_data,
2386-
),
2387-
],
2388-
&[equality_proof_signer],
2389-
)
2390-
.await
2391-
}
2392-
2393-
/// Create a ciphertext validity proof context state account for a
2394-
/// confidential transfer.
2395-
pub async fn create_ciphertext_validity_proof_context_state_for_transfer<S: Signer>(
2350+
/// Create a context state account containing zero-knowledge proof needed for a confidential
2351+
/// transfer instruction.
2352+
pub async fn confidential_transfer_create_context_state_account<
2353+
S: Signer,
2354+
ZK: Pod + ZkProofData<U>,
2355+
U: Pod,
2356+
>(
23962357
&self,
23972358
context_state_account: &Pubkey,
23982359
context_state_authority: &Pubkey,
2399-
ciphertext_validity_proof_data: &BatchedGroupedCiphertext3HandlesValidityProofData,
2400-
ciphertext_validity_proof_signer: &S,
2360+
proof_data: &ZK,
2361+
signer: &S,
24012362
) -> TokenResult<T::Output> {
2402-
// create ciphertext validity proof context state
2403-
let instruction_type = ProofInstruction::VerifyBatchedGroupedCiphertext3HandlesValidity;
2404-
let space =
2405-
size_of::<ProofContextState<BatchedGroupedCiphertext3HandlesValidityProofContext>>();
2363+
let instruction_type = zk_proof_type_to_instruction(ZK::PROOF_TYPE)?;
2364+
let space = size_of::<ProofContextState<U>>();
24062365
let rent = self
24072366
.client
24082367
.get_minimum_balance_for_rent_exemption(space)
24092368
.await
24102369
.map_err(TokenError::Client)?;
24112370

2412-
let ciphertext_validity_proof_context_state_info = ContextStateInfo {
2371+
let context_state_info = ContextStateInfo {
24132372
context_state_account,
24142373
context_state_authority,
24152374
};
@@ -2423,69 +2382,13 @@ where
24232382
space as u64,
24242383
&zk_elgamal_proof_program::id(),
24252384
),
2426-
instruction_type.encode_verify_proof(
2427-
Some(ciphertext_validity_proof_context_state_info),
2428-
ciphertext_validity_proof_data,
2429-
),
2385+
instruction_type.encode_verify_proof(Some(context_state_info), proof_data),
24302386
],
2431-
&[ciphertext_validity_proof_signer],
2387+
&[signer],
24322388
)
24332389
.await
24342390
}
24352391

2436-
/// Create a range proof context state account for a confidential transfer.
2437-
pub async fn create_range_proof_context_state_for_transfer<S: Signer>(
2438-
&self,
2439-
context_state_account: &Pubkey,
2440-
context_state_authority: &Pubkey,
2441-
range_proof_data: &BatchedRangeProofU128Data,
2442-
range_proof_keypair: &S,
2443-
) -> TokenResult<T::Output> {
2444-
let instruction_type = ProofInstruction::VerifyBatchedRangeProofU128;
2445-
let space = size_of::<ProofContextState<BatchedRangeProofContext>>();
2446-
let rent = self
2447-
.client
2448-
.get_minimum_balance_for_rent_exemption(space)
2449-
.await
2450-
.map_err(TokenError::Client)?;
2451-
let range_proof_context_state_info = ContextStateInfo {
2452-
context_state_account,
2453-
context_state_authority,
2454-
};
2455-
self.process_ixs(
2456-
&[system_instruction::create_account(
2457-
&self.payer.pubkey(),
2458-
context_state_account,
2459-
rent,
2460-
space as u64,
2461-
&zk_elgamal_proof_program::id(),
2462-
)],
2463-
&[range_proof_keypair],
2464-
)
2465-
.await?;
2466-
2467-
// This instruction is right at the transaction size limit, but in the
2468-
// future it might be able to support the transfer too
2469-
let blockhash = self
2470-
.client
2471-
.get_latest_blockhash()
2472-
.await
2473-
.map_err(TokenError::Client)?;
2474-
2475-
let transaction = Transaction::new_signed_with_payer(
2476-
&[instruction_type
2477-
.encode_verify_proof(Some(range_proof_context_state_info), range_proof_data)],
2478-
Some(&self.payer.pubkey()),
2479-
&[self.payer.as_ref()],
2480-
blockhash,
2481-
);
2482-
2483-
self.client
2484-
.send_transaction(&transaction)
2485-
.await
2486-
.map_err(TokenError::Client)
2487-
}
2488-
24892392
/// Close a ZK Token proof program context state
24902393
pub async fn confidential_transfer_close_context_state<S: Signer>(
24912394
&self,
@@ -2649,209 +2552,6 @@ where
26492552
self.process_ixs(&instructions, signing_keypairs).await
26502553
}
26512554

2652-
/// Create equality proof context state accounts for a confidential transfer
2653-
/// with fee.
2654-
#[allow(clippy::too_many_arguments)]
2655-
pub async fn create_equality_proof_context_state_for_transfer_with_fee<S: Signer>(
2656-
&self,
2657-
context_state_account: &Pubkey,
2658-
context_state_authority: &Pubkey,
2659-
equality_proof_data: &CiphertextCommitmentEqualityProofData,
2660-
signing_keypair: &S,
2661-
) -> TokenResult<T::Output> {
2662-
let instruction_type = ProofInstruction::VerifyCiphertextCommitmentEquality;
2663-
let space = size_of::<ProofContextState<CiphertextCommitmentEqualityProofContext>>();
2664-
let rent = self
2665-
.client
2666-
.get_minimum_balance_for_rent_exemption(space)
2667-
.await
2668-
.map_err(TokenError::Client)?;
2669-
let equality_proof_context_state_info = ContextStateInfo {
2670-
context_state_account,
2671-
context_state_authority,
2672-
};
2673-
self.process_ixs(
2674-
&[
2675-
system_instruction::create_account(
2676-
&self.payer.pubkey(),
2677-
context_state_account,
2678-
rent,
2679-
space as u64,
2680-
&zk_elgamal_proof_program::id(),
2681-
),
2682-
instruction_type.encode_verify_proof(
2683-
Some(equality_proof_context_state_info),
2684-
equality_proof_data,
2685-
),
2686-
],
2687-
&[signing_keypair],
2688-
)
2689-
.await
2690-
}
2691-
2692-
/// Create a transfer amount ciphertext validity proof context state account
2693-
/// for a confidential transfer with fee.
2694-
#[allow(clippy::too_many_arguments)]
2695-
pub async fn create_transfer_amount_ciphertext_validity_proof_context_state_for_transfer_with_fee<
2696-
S: Signer,
2697-
>(
2698-
&self,
2699-
context_state_account: &Pubkey,
2700-
context_state_authority: &Pubkey,
2701-
transfer_amount_ciphertext_validity_proof_data: &BatchedGroupedCiphertext3HandlesValidityProofData,
2702-
signing_keypair: &S,
2703-
) -> TokenResult<T::Output> {
2704-
let instruction_type = ProofInstruction::VerifyBatchedGroupedCiphertext3HandlesValidity;
2705-
let space =
2706-
size_of::<ProofContextState<BatchedGroupedCiphertext3HandlesValidityProofContext>>();
2707-
let rent = self
2708-
.client
2709-
.get_minimum_balance_for_rent_exemption(space)
2710-
.await
2711-
.map_err(TokenError::Client)?;
2712-
let transfer_amount_ciphertext_validity_proof_context_state_info = ContextStateInfo {
2713-
context_state_account,
2714-
context_state_authority,
2715-
};
2716-
self.process_ixs(
2717-
&[
2718-
system_instruction::create_account(
2719-
&self.payer.pubkey(),
2720-
context_state_account,
2721-
rent,
2722-
space as u64,
2723-
&zk_elgamal_proof_program::id(),
2724-
),
2725-
instruction_type.encode_verify_proof(
2726-
Some(transfer_amount_ciphertext_validity_proof_context_state_info),
2727-
transfer_amount_ciphertext_validity_proof_data,
2728-
),
2729-
],
2730-
&[signing_keypair],
2731-
)
2732-
.await
2733-
}
2734-
2735-
/// Create a fee sigma proof context state account for a confidential
2736-
/// transfer with fee.
2737-
#[allow(clippy::too_many_arguments)]
2738-
pub async fn create_fee_sigma_proof_context_state_for_transfer_with_fee<S: Signer>(
2739-
&self,
2740-
context_state_account: &Pubkey,
2741-
context_state_authority: &Pubkey,
2742-
fee_sigma_proof_data: &PercentageWithCapProofData,
2743-
signing_keypair: &S,
2744-
) -> TokenResult<T::Output> {
2745-
let instruction_type = ProofInstruction::VerifyPercentageWithCap;
2746-
let space = size_of::<ProofContextState<PercentageWithCapProofContext>>();
2747-
let rent = self
2748-
.client
2749-
.get_minimum_balance_for_rent_exemption(space)
2750-
.await
2751-
.map_err(TokenError::Client)?;
2752-
let fee_sigma_proof_context_state_info = ContextStateInfo {
2753-
context_state_account,
2754-
context_state_authority,
2755-
};
2756-
self.process_ixs(
2757-
&[
2758-
system_instruction::create_account(
2759-
&self.payer.pubkey(),
2760-
context_state_account,
2761-
rent,
2762-
space as u64,
2763-
&zk_elgamal_proof_program::id(),
2764-
),
2765-
instruction_type.encode_verify_proof(
2766-
Some(fee_sigma_proof_context_state_info),
2767-
fee_sigma_proof_data,
2768-
),
2769-
],
2770-
&[signing_keypair],
2771-
)
2772-
.await
2773-
}
2774-
2775-
/// Create a fee ciphertext validity proof context state account for a
2776-
/// confidential transfer with fee.
2777-
#[allow(clippy::too_many_arguments)]
2778-
pub async fn create_fee_ciphertext_validity_proof_context_state_for_transfer_with_fee<
2779-
S: Signer,
2780-
>(
2781-
&self,
2782-
context_state_account: &Pubkey,
2783-
context_state_authority: &Pubkey,
2784-
fee_ciphertext_validity_proof_data: &BatchedGroupedCiphertext2HandlesValidityProofData,
2785-
signing_keypair: &S,
2786-
) -> TokenResult<T::Output> {
2787-
let instruction_type = ProofInstruction::VerifyBatchedGroupedCiphertext2HandlesValidity;
2788-
let space =
2789-
size_of::<ProofContextState<BatchedGroupedCiphertext2HandlesValidityProofContext>>();
2790-
let rent = self
2791-
.client
2792-
.get_minimum_balance_for_rent_exemption(space)
2793-
.await
2794-
.map_err(TokenError::Client)?;
2795-
let fee_ciphertext_validity_proof_context_state_info = ContextStateInfo {
2796-
context_state_account,
2797-
context_state_authority,
2798-
};
2799-
self.process_ixs(
2800-
&[
2801-
system_instruction::create_account(
2802-
&self.payer.pubkey(),
2803-
context_state_account,
2804-
rent,
2805-
space as u64,
2806-
&zk_elgamal_proof_program::id(),
2807-
),
2808-
instruction_type.encode_verify_proof(
2809-
Some(fee_ciphertext_validity_proof_context_state_info),
2810-
fee_ciphertext_validity_proof_data,
2811-
),
2812-
],
2813-
&[signing_keypair],
2814-
)
2815-
.await
2816-
}
2817-
2818-
/// Create a range proof context state for a confidential transfer with fee.
2819-
#[allow(clippy::too_many_arguments)]
2820-
pub async fn create_range_proof_context_state_for_transfer_with_fee<S: Signer>(
2821-
&self,
2822-
context_state_account: &Pubkey,
2823-
context_state_authority: &Pubkey,
2824-
range_proof_data: &BatchedRangeProofU256Data,
2825-
signing_keypair: &S,
2826-
) -> TokenResult<T::Output> {
2827-
let instruction_type = ProofInstruction::VerifyBatchedRangeProofU256;
2828-
let space = size_of::<ProofContextState<BatchedRangeProofContext>>();
2829-
let rent = self
2830-
.client
2831-
.get_minimum_balance_for_rent_exemption(space)
2832-
.await
2833-
.map_err(TokenError::Client)?;
2834-
let range_proof_context_state_info = ContextStateInfo {
2835-
context_state_account,
2836-
context_state_authority,
2837-
};
2838-
self.process_ixs(
2839-
&[
2840-
system_instruction::create_account(
2841-
&self.payer.pubkey(),
2842-
context_state_account,
2843-
rent,
2844-
space as u64,
2845-
&zk_elgamal_proof_program::id(),
2846-
),
2847-
instruction_type
2848-
.encode_verify_proof(Some(range_proof_context_state_info), range_proof_data),
2849-
],
2850-
&[signing_keypair],
2851-
)
2852-
.await
2853-
}
2854-
28552555
/// Applies the confidential transfer pending balance to the available
28562556
/// balance
28572557
pub async fn confidential_transfer_apply_pending_balance<S: Signers>(

token/program-2022/src/proof.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ pub fn verify_and_extract_context<'a, T: Pod + ZkProofData<U>, U: Pod>(
134134
}
135135
}
136136

137-
fn zk_proof_type_to_instruction(proof_type: ProofType) -> Result<ProofInstruction, ProgramError> {
137+
/// Converts a zk proof type to a corresponding ZK ElGamal proof program instruction that verifies
138+
/// the proof.
139+
pub fn zk_proof_type_to_instruction(
140+
proof_type: ProofType,
141+
) -> Result<ProofInstruction, ProgramError> {
138142
match proof_type {
139143
ProofType::ZeroCiphertext => Ok(ProofInstruction::VerifyZeroCiphertext),
140144
ProofType::CiphertextCiphertextEquality => {

0 commit comments

Comments
 (0)