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

Commit 6875603

Browse files
committed
Initial ConfidentialTransferInstruction::EmptyAccount test
1 parent 29089e8 commit 6875603

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

token/client/src/token.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ use spl_token_2022::{
1919
StateWithExtensionsOwned,
2020
},
2121
instruction, native_mint,
22-
solana_zk_token_sdk::encryption::{auth_encryption::*, elgamal::*},
22+
solana_zk_token_sdk::{
23+
encryption::{auth_encryption::*, elgamal::*},
24+
errors::ProofError,
25+
},
2326
state::{Account, AccountState, Mint},
2427
};
2528
use std::{
29+
convert::TryInto,
2630
fmt, io,
2731
sync::{Arc, RwLock},
2832
time::{Duration, Instant},
@@ -41,6 +45,8 @@ pub enum TokenError {
4145
AccountInvalidOwner,
4246
#[error("invalid account mint")]
4347
AccountInvalidMint,
48+
#[error("proof error: {0}")]
49+
Proof(ProofError),
4450
}
4551
impl PartialEq for TokenError {
4652
fn eq(&self, other: &Self) -> bool {
@@ -968,7 +974,7 @@ where
968974
}
969975

970976
/// Approves a token account for confidential transfers
971-
pub async fn confidential_transfer_approve_token_account<S2: Signer>(
977+
pub async fn confidential_transfer_approve_account<S2: Signer>(
972978
&self,
973979
token_account: &Pubkey,
974980
authority: &S2,
@@ -985,6 +991,36 @@ where
985991
.await
986992
}
987993

994+
/// Prepare a token account for closing
995+
pub async fn confidential_transfer_empty_account<S2: Signer>(
996+
&self,
997+
token_account: &Pubkey,
998+
authority: &S2,
999+
elgamal_keypair: &ElGamalKeypair,
1000+
) -> TokenResult<T::Output> {
1001+
let state = self.get_account_info(&token_account).await.unwrap();
1002+
let extension =
1003+
state.get_extension::<confidential_transfer::ConfidentialTransferAccount>()?;
1004+
1005+
let proof_data = confidential_transfer::instruction::CloseAccountData::new(
1006+
&elgamal_keypair,
1007+
&extension.available_balance.try_into().unwrap(),
1008+
)
1009+
.map_err(TokenError::Proof)?;
1010+
1011+
self.process_ixs(
1012+
&confidential_transfer::instruction::empty_account(
1013+
&self.program_id,
1014+
token_account,
1015+
&authority.pubkey(),
1016+
&[],
1017+
&proof_data,
1018+
)?,
1019+
&[authority],
1020+
)
1021+
.await
1022+
}
1023+
9881024
/// Enable confidential transfer `Deposit` and `Transfer` instructions for a token account
9891025
pub async fn confidential_transfer_enable_balance_credits<S2: Signer>(
9901026
&self,

token/program-2022-test/tests/confidential_transfer.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ async fn ct_configure_token_account() {
176176
);
177177

178178
token
179-
.confidential_transfer_approve_token_account(&alice_token_account, &ct_mint_authority)
179+
.confidential_transfer_approve_account(&alice_token_account, &ct_mint_authority)
180180
.await
181181
.unwrap();
182182

@@ -235,3 +235,37 @@ async fn ct_enable_disable_balance_credits() {
235235
.unwrap();
236236
assert!(bool::from(&extension.allow_balance_credits));
237237
}
238+
239+
#[tokio::test]
240+
async fn ct_new_account_is_empty() {
241+
let ConfidentialTransferMintWithKeypairs { ct_mint, .. } =
242+
ConfidentialTransferMintWithKeypairs::new();
243+
let mut context = TestContext::new().await;
244+
context
245+
.init_token_with_mint(vec![
246+
ExtensionInitializationParams::ConfidentialTransferMint { ct_mint },
247+
])
248+
.await
249+
.unwrap();
250+
251+
let TokenContext { token, alice, .. } = context.token_context.unwrap();
252+
253+
let alice_token_account = token
254+
.create_auxiliary_token_account_with_extension_space(
255+
&alice,
256+
&alice.pubkey(),
257+
vec![ExtensionType::ConfidentialTransferAccount],
258+
)
259+
.await
260+
.unwrap();
261+
262+
let (alice_elgamal_keypair, _) = token
263+
.confidential_transfer_configure_token_account_and_keypairs(&alice_token_account, &alice)
264+
.await
265+
.unwrap();
266+
267+
token
268+
.confidential_transfer_empty_account(&alice_token_account, &alice, &alice_elgamal_keypair)
269+
.await
270+
.unwrap();
271+
}

0 commit comments

Comments
 (0)