대기 중인 잔액 적용하기
대기 중인 잔액을 사용 가능한 잔액으로 적용하는 방법
토큰을 기밀적으로 전송하기 전에 공개 토큰 잔액을 기밀 잔액으로 변환해야 합니다. 이 변환은 두 단계로 진행됩니다:
- 기밀 대기 잔액: 처음에는 토큰이 공개 잔액에서 "대기 중인" 기밀 잔액으로 "입금"됩니다.
- 기밀 사용 가능 잔액: 그런 다음 대기 잔액이 사용 가능한 잔액으로 "적용"되어 토큰을 기밀 전송에 사용할 수 있게 됩니다.
이 섹션에서는 두 번째 단계인 대기 잔액을 사용 가능한 잔액으로 적용하는 방법을 설명합니다.
토큰이 공개 잔액에서 "입금"되거나 토큰이 한 token account에서 다른 token account로 기밀적으로 전송될 때, 토큰은 처음에 기밀 대기 잔액에 추가됩니다. 토큰을 기밀 전송에 사용하기 전에 대기 잔액을 사용 가능한 잔액으로 "적용"해야 합니다.
다음 다이어그램은 대기 잔액을 사용 가능한 잔액으로 적용하는 단계를 보여줍니다:
필수 명령어
대기 잔액을 사용 가능한 잔액으로 변환하려면 ConfidentialTransferInstruction::ApplyPendingBalance 명령어를 호출하세요.
spl_token_client
크레이트는 아래 예제에서 보여주는 것처럼
ApplyPendingBalance
명령어가 포함된 트랜잭션을 구축하고 전송하는
confidential_transfer_apply_pending_balance
메서드를 제공합니다.
예제 코드
다음 예제는 기밀 대기 잔액을 기밀 사용 가능 잔액으로 적용하는 방법을 보여줍니다.
예제를 실행하려면 다음 명령어를 사용하여 메인넷에서 복제된 Token Extension Program으로 로컬 validator를 시작하세요. 로컬 validator를 시작하려면 Solana CLI가 설치되어 있어야 합니다.
$solana-test-validator --clone-upgradeable-program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb --url https://api.mainnet-beta.solana.com -r
작성 시점에서는 기본 로컬 validator에서 기밀 전송(Confidential Transfers)이 활성화되어 있지 않습니다. 예제 코드를 실행하려면 메인넷 Token Extensions Program을 복제해야 합니다.
use anyhow::{Context, Result};use solana_client::nonblocking::rpc_client::RpcClient;use solana_sdk::{commitment_config::CommitmentConfig,signature::{Keypair, Signer},transaction::Transaction,};use spl_associated_token_account::{get_associated_token_address_with_program_id, instruction::create_associated_token_account,};use spl_token_client::{client::{ProgramRpcClient, ProgramRpcClientSendTransaction},spl_token_2022::{extension::{confidential_transfer::instruction::{configure_account, PubkeyValidityProofData},ExtensionType,},id as token_2022_program_id,instruction::reallocate,solana_zk_sdk::encryption::{auth_encryption::*, elgamal::*},},token::{ExtensionInitializationParams, Token},};use spl_token_confidential_transfer_proof_extraction::instruction::{ProofData, ProofLocation};use std::sync::Arc;#[tokio::main]async fn main() -> Result<()> {// Create connection to local test validatorlet rpc_client = Arc::new(RpcClient::new_with_commitment(String::from("http://localhost:8899"),CommitmentConfig::confirmed(),));// Load the default Solana CLI keypair to use as the fee payer// This will be the wallet paying for the transaction fees// Use Arc to prevent multiple clones of the keypairlet payer = Arc::new(load_keypair()?);println!("Using payer: {}", payer.pubkey());// Generate a new keypair to use as the address of the token mintlet mint = Keypair::new();println!("Mint keypair generated: {}", mint.pubkey());// Set up program client for Token clientlet program_client = ProgramRpcClient::new(rpc_client.clone(), ProgramRpcClientSendTransaction);// Number of decimals for the mintlet decimals = 9;// Create a token client for the Token-2022 program// This provides high-level methods for token operationslet token = Token::new(Arc::new(program_client),&token_2022_program_id(), // Use the Token-2022 program (newer version with extensions)&mint.pubkey(), // Address of the new token mintSome(decimals), // Number of decimal placespayer.clone(), // Fee payer for transactions);// Create extension initialization parameters for the mint// The ConfidentialTransferMint extension enables confidential (private) transfers of tokenslet extension_initialization_params =vec![ExtensionInitializationParams::ConfidentialTransferMint {authority: Some(payer.pubkey()), // Authority that can modify confidential transfer settingsauto_approve_new_accounts: true, // Automatically approve new confidential accountsauditor_elgamal_pubkey: None, // Optional auditor ElGamal public key}];// Create and initialize the mint with the ConfidentialTransferMint extension// This sends a transaction to create the new token mintlet transaction_signature = token.create_mint(&payer.pubkey(), // Mint authority - can mint new tokensSome(&payer.pubkey()), // Freeze authority - can freeze token accountsextension_initialization_params, // Add the ConfidentialTransferMint extension&[&mint], // Mint keypair needed as signer).await?;// Print results for user verificationprintln!("Mint Address: {}", mint.pubkey());println!("Mint Creation Transaction Signature: {}",transaction_signature);// ===== Create and configure token account for confidential transfers =====println!("\nCreate and configure token account for confidential transfers");// Get the associated token account address for the ownerlet token_account_pubkey = get_associated_token_address_with_program_id(&payer.pubkey(), // Token account owner&mint.pubkey(), // Mint&token_2022_program_id(), // Token program ID);println!("Token Account Address: {}", token_account_pubkey);// Step 1: Create the associated token accountlet create_associated_token_account_instruction = create_associated_token_account(&payer.pubkey(), // Funding account&payer.pubkey(), // Token account owner&mint.pubkey(), // Mint&token_2022_program_id(), // Token program ID);// Step 2: Reallocate the token account to include space for the ConfidentialTransferAccount extensionlet reallocate_instruction = reallocate(&token_2022_program_id(), // Token program ID&token_account_pubkey, // Token account&payer.pubkey(), // Payer&payer.pubkey(), // Token account owner&[&payer.pubkey()], // Signers&[ExtensionType::ConfidentialTransferAccount], // Extension to reallocate space for)?;// Step 3: Generate the ElGamal keypair and AES key for token accountlet elgamal_keypair = ElGamalKeypair::new_from_signer(&payer, &token_account_pubkey.to_bytes()).expect("Failed to create ElGamal keypair");let aes_key = AeKey::new_from_signer(&payer, &token_account_pubkey.to_bytes()).expect("Failed to create AES key");// The maximum number of Deposit and Transfer instructions that can// credit pending_balance before the ApplyPendingBalance instruction is executedlet maximum_pending_balance_credit_counter = 65536;// Initial token balance is 0let decryptable_balance = aes_key.encrypt(0);// Generate the proof data client-sidelet proof_data = PubkeyValidityProofData::new(&elgamal_keypair).map_err(|_| anyhow::anyhow!("Failed to generate proof data"))?;// Indicate that proof is included in the same transactionlet proof_location =ProofLocation::InstructionOffset(1.try_into()?, ProofData::InstructionData(&proof_data));// Step 4: Create instructions to configure the account for confidential transferslet configure_account_instructions = configure_account(&token_2022_program_id(), // Program ID&token_account_pubkey, // Token account&mint.pubkey(), // Mint&decryptable_balance.into(), // Initial balancemaximum_pending_balance_credit_counter, // Maximum pending balance credit counter&payer.pubkey(), // Token Account Owner&[], // Additional signersproof_location, // Proof location)?;// Combine all instructionslet mut instructions = vec![create_associated_token_account_instruction,reallocate_instruction,];instructions.extend(configure_account_instructions);// Create and send the transactionlet recent_blockhash = rpc_client.get_latest_blockhash().await?;let transaction = Transaction::new_signed_with_payer(&instructions,Some(&payer.pubkey()),&[&payer],recent_blockhash,);let transaction_signature = rpc_client.send_and_confirm_transaction(&transaction).await?;println!("Create Token Account Transaction Signature: {}",transaction_signature);// Mint some tokens to the newly created token account// This gives the account some tokens to work withlet mint_signature = token.mint_to(&token_account_pubkey, // Destination account&payer.pubkey(), // Mint authority100 * 10u64.pow(decimals as u32), // Amount (100 tokens with decimal precision)&[&payer], // Signers).await?;println!("Mint Tokens Transaction Signature: {}", mint_signature);// Deposit the tokens to confidential state// This converts regular tokens to confidential tokensprintln!("Deposit tokens to confidential state pending balance");let deposit_signature = token.confidential_transfer_deposit(&token_account_pubkey, // The token account&payer.pubkey(), // Authority (owner) of the account100 * 10u64.pow(decimals as u32), // Amount to deposit (100 tokens)decimals, // Decimals of the token&[&payer], // Signers (owner must sign)).await?;println!("Confidential Transfer Deposit Signature: {}",deposit_signature);// Apply the pending balance to make funds availableprintln!("Apply pending balance to available balance");let apply_signature = token.confidential_transfer_apply_pending_balance(&token_account_pubkey, // The token account&payer.pubkey(), // Authority (owner) of the accountNone, // Optional new decryptable available balanceelgamal_keypair.secret(), // ElGamal keypair for public-key cryptography (decryption and ZK proofs)&aes_key, // AES key for encryption of balance and transfer amounts&[&payer], // Signers (owner must sign)).await?;println!("Apply Pending Balance Signature: {}", apply_signature);println!("Confidential transfer setup complete. Tokens are now in available balance.");println!("Associated Token Account with confidential transfers: {}",token_account_pubkey);Ok(())}// Load the keypair from the default Solana CLI keypair path (~/.config/solana/id.json)// This enables using the same wallet as the Solana CLI toolsfn load_keypair() -> Result<Keypair> {// Get the default keypair pathlet keypair_path = dirs::home_dir().context("Could not find home directory")?.join(".config/solana/id.json");// Read the keypair file directly into bytes using serde_json// The keypair file is a JSON array of byteslet file = std::fs::File::open(&keypair_path)?;let keypair_bytes: Vec<u8> = serde_json::from_reader(file)?;// Create keypair from the loaded bytes// This converts the byte array into a keypairlet keypair = Keypair::from_bytes(&keypair_bytes)?;Ok(keypair)}
Is this page helpful?