Skip to content

Commit

Permalink
[token-client] use Signersinstead of Signer for confidential tran…
Browse files Browse the repository at this point in the history
…sfer client functions (solana-labs#7175)
  • Loading branch information
samkim-crypto committed Aug 21, 2024
1 parent 3ee1da9 commit 7440e4b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 69 deletions.
29 changes: 19 additions & 10 deletions token/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1604,27 +1604,32 @@ async fn command_transfer(
.unwrap();

// setup proofs
let create_range_proof_context_signer = &[&range_proof_context_state_account];
let create_equality_proof_context_signer = &[&equality_proof_context_state_account];
let create_ciphertext_validity_proof_context_signer =
&[&ciphertext_validity_proof_context_state_account];

let _ = try_join!(
token.confidential_transfer_create_context_state_account(
&range_proof_pubkey,
&context_state_authority_pubkey,
&range_proof_data,
true,
&range_proof_context_state_account,
create_range_proof_context_signer
),
token.confidential_transfer_create_context_state_account(
&equality_proof_pubkey,
&context_state_authority_pubkey,
&equality_proof_data,
false,
&equality_proof_context_state_account,
create_equality_proof_context_signer
),
token.confidential_transfer_create_context_state_account(
&ciphertext_validity_proof_pubkey,
&context_state_authority_pubkey,
&ciphertext_validity_proof_data,
false,
&ciphertext_validity_proof_context_state_account,
create_ciphertext_validity_proof_context_signer
)
)?;

Expand Down Expand Up @@ -1655,24 +1660,25 @@ async fn command_transfer(
.await?;

// close context state accounts
let close_context_state_signer = &[&context_state_authority];
let _ = try_join!(
token.confidential_transfer_close_context_state(
&equality_proof_pubkey,
&sender,
&context_state_authority_pubkey,
&context_state_authority,
close_context_state_signer
),
token.confidential_transfer_close_context_state(
&ciphertext_validity_proof_pubkey,
&sender,
&context_state_authority_pubkey,
&context_state_authority,
close_context_state_signer
),
token.confidential_transfer_close_context_state(
&range_proof_pubkey,
&sender,
&context_state_authority_pubkey,
&context_state_authority,
close_context_state_signer
),
)?;

Expand Down Expand Up @@ -3353,21 +3359,23 @@ async fn command_deposit_withdraw_confidential_tokens(

// set up context state accounts
let context_state_authority_pubkey = context_state_authority.pubkey();
let create_equality_proof_signer = &[&equality_proof_context_state_keypair];
let create_range_proof_signer = &[&range_proof_context_state_keypair];

let _ = try_join!(
token.confidential_transfer_create_context_state_account(
&equality_proof_context_state_pubkey,
&context_state_authority_pubkey,
&equality_proof_data,
false,
&equality_proof_context_state_keypair,
create_equality_proof_signer
),
token.confidential_transfer_create_context_state_account(
&range_proof_context_state_pubkey,
&context_state_authority_pubkey,
&range_proof_data,
true,
&range_proof_context_state_keypair,
create_range_proof_signer,
)
)?;

Expand All @@ -3392,18 +3400,19 @@ async fn command_deposit_withdraw_confidential_tokens(
.await?;

// close context state account
let close_context_state_signer = &[&context_state_authority];
let _ = try_join!(
token.confidential_transfer_close_context_state(
&equality_proof_context_state_pubkey,
&token_account_address,
&context_state_authority_pubkey,
&context_state_authority,
close_context_state_signer
),
token.confidential_transfer_close_context_state(
&range_proof_context_state_pubkey,
&token_account_address,
&context_state_authority_pubkey,
&context_state_authority,
close_context_state_signer
)
)?;

Expand Down
32 changes: 15 additions & 17 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2271,16 +2271,17 @@ where
/// Create a record account containing zero-knowledge proof needed for a
/// confidential transfer.
pub async fn confidential_transfer_create_record_account<
S: Signer,
S1: Signer,
S2: Signer,
ZK: Pod + ZkProofData<U>,
U: Pod,
>(
&self,
record_account: &Pubkey,
record_authority: &Pubkey,
proof_data: &ZK,
record_account_signer: &S,
record_authority_signer: &S,
record_account_signer: &S1,
record_authority_signer: &S2,
) -> TokenResult<Vec<T::Output>> {
let proof_data = bytes_of(proof_data);
let space = proof_data
Expand Down Expand Up @@ -2326,11 +2327,8 @@ where
};

let first_ixs = create_record_instructions(true, first_chunk, 0);
self.process_ixs(
&first_ixs,
&[record_account_signer, record_authority_signer],
)
.await?;
let first_ixs_signers: [&dyn Signer; 2] = [record_account_signer, record_authority_signer];
self.process_ixs(&first_ixs, &first_ixs_signers).await?;

let subsequent_chunk_size =
calculate_record_max_chunk_size(create_record_instructions, false);
Expand All @@ -2354,28 +2352,28 @@ where
}

/// Close a record account.
pub async fn confidential_transfer_close_record_account<S: Signer>(
pub async fn confidential_transfer_close_record_account<S: Signers>(
&self,
record_account: &Pubkey,
record_authority: &Pubkey,
receiver: &Pubkey,
record_authority_signer: &S,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
self.process_ixs(
&[spl_record::instruction::close_account(
record_account,
record_authority,
receiver,
)],
&[record_authority_signer],
signing_keypairs,
)
.await
}

/// Create a context state account containing zero-knowledge proof needed
/// for a confidential transfer instruction.
pub async fn confidential_transfer_create_context_state_account<
S: Signer,
S: Signers,
ZK: Pod + ZkProofData<U>,
U: Pod,
>(
Expand All @@ -2384,7 +2382,7 @@ where
context_state_authority: &Pubkey,
proof_data: &ZK,
split_account_creation_and_proof_verification: bool,
signer: &S,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let instruction_type = zk_proof_type_to_instruction(ZK::PROOF_TYPE)?;
let space = size_of::<ProofContextState<U>>();
Expand All @@ -2410,7 +2408,7 @@ where
space as u64,
&zk_elgamal_proof_program::id(),
)],
&[signer],
signing_keypairs,
)
.await?;

Expand Down Expand Up @@ -2443,14 +2441,14 @@ where
),
instruction_type.encode_verify_proof(Some(context_state_info), proof_data),
],
&[signer],
signing_keypairs,
)
.await
}
}

/// Close a ZK Token proof program context state
pub async fn confidential_transfer_close_context_state<S: Signer>(
pub async fn confidential_transfer_close_context_state<S: Signers>(
&self,
context_state_account: &Pubkey,
lamport_destination_account: &Pubkey,
Expand All @@ -2467,7 +2465,7 @@ where
context_state_info,
lamport_destination_account,
)],
&[signing_keypairs],
signing_keypairs,
)
.await
}
Expand Down
Loading

0 comments on commit 7440e4b

Please sign in to comment.