Создание аккаунта токена
Как создать аккаунт токена с расширением Confidential Transfer
Расширение Confidential Transfer позволяет осуществлять приватные переводы токенов, добавляя дополнительное состояние в аккаунт токена. В этом разделе объясняется, как создать аккаунт токена с включенным этим расширением.
На следующей схеме показаны шаги, необходимые для создания аккаунта токена с расширением Confidential Transfer:
Состояние аккаунта токена с Confidential Transfer
Расширение добавляет состояние ConfidentialTransferAccount в аккаунт токена:
#[repr(C)]#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]pub struct ConfidentialTransferAccount {/// `true` if this account has been approved for use. All confidential/// transfer operations for the account will fail until approval is/// granted.pub approved: PodBool,/// The public key associated with ElGamal encryptionpub elgamal_pubkey: PodElGamalPubkey,/// The low 16 bits of the pending balance (encrypted by `elgamal_pubkey`)pub pending_balance_lo: EncryptedBalance,/// The high 48 bits of the pending balance (encrypted by `elgamal_pubkey`)pub pending_balance_hi: EncryptedBalance,/// The available balance (encrypted by `encryption_pubkey`)pub available_balance: EncryptedBalance,/// The decryptable available balancepub decryptable_available_balance: DecryptableBalance,/// If `false`, the extended account rejects any incoming confidential/// transferspub allow_confidential_credits: PodBool,/// If `false`, the base account rejects any incoming transferspub allow_non_confidential_credits: PodBool,/// The total number of `Deposit` and `Transfer` instructions that have/// credited `pending_balance`pub pending_balance_credit_counter: PodU64,/// The maximum number of `Deposit` and `Transfer` instructions that can/// credit `pending_balance` before the `ApplyPendingBalance`/// instruction is executedpub maximum_pending_balance_credit_counter: PodU64,/// The `expected_pending_balance_credit_counter` value that was included in/// the last `ApplyPendingBalance` instructionpub expected_pending_balance_credit_counter: PodU64,/// The actual `pending_balance_credit_counter` when the last/// `ApplyPendingBalance` instruction was executedpub actual_pending_balance_credit_counter: PodU64,}
ConfidentialTransferAccount
содержит несколько полей для управления
конфиденциальными переводами:
-
approved: Статус одобрения аккаунта для конфиденциальных переводов. Если конфигурация аккаунта токена
auto_approve_new_accounts
установлена какtrue
, все аккаунты токенов автоматически одобряются для конфиденциальных переводов. -
elgamal_pubkey: Публичный ключ ElGamal, используемый для шифрования балансов и сумм переводов.
-
pending_balance_lo: Зашифрованные младшие 16 бит ожидаемого баланса. Баланс разделен на высокую и низкую части для эффективной расшифровки.
-
pending_balance_hi: Зашифрованные старшие 48 бит ожидаемого баланса. Баланс разделен на высокую и низкую части для эффективной расшифровки.
-
available_balance: Зашифрованный баланс, доступный для переводов.
-
decryptable_available_balance: Доступный баланс, зашифрованный с помощью ключа Advanced Encryption Standard (AES) для эффективной расшифровки владельцем аккаунта.
-
allow_confidential_credits: Если true, разрешает входящие конфиденциальные переводы.
-
allow_non_confidential_credits: Если true, разрешает входящие неконфиденциальные переводы.
-
pending_balance_credit_counter: Счетчик входящих ожидаемых кредитов баланса из инструкций депозита и перевода.
-
maximum_pending_balance_credit_counter: Лимит счетчика ожидаемых кредитов перед необходимостью выполнения инструкции
ApplyPendingBalance
для преобразования ожидаемого баланса в доступный баланс. -
expected_pending_balance_credit_counter: Значение
pending_balance_credit_counter
, предоставленное клиентом через instruction data в последний раз, когда инструкцияApplyPendingBalance
была обработана. -
actual_pending_balance_credit_counter: Значение
pending_balance_credit_counter
в token account на момент, когда инструкцияApplyPendingBalance
была обработана.
Ожидаемый баланс против доступного баланса
Конфиденциальные балансы разделены на ожидаемые и доступные, чтобы предотвратить DoS-атаки. Без этого разделения злоумышленник мог бы неоднократно отправлять токены на token account, блокируя возможность владельца token account переводить токены. Владелец token account не смог бы перевести токены, так как зашифрованный баланс изменился бы между моментом отправки транзакции и её обработкой, что привело бы к неудачной транзакции.
Все депозиты и суммы переводов изначально добавляются в ожидаемый баланс.
Владельцы token account должны использовать инструкцию
ApplyPendingBalance
, чтобы конвертировать ожидаемый баланс в доступный.
Входящие переводы или депозиты не влияют на доступный баланс token account.
Разделение ожидаемого баланса на высокую и низкую части
Конфиденциальный ожидаемый баланс разделён на pending_balance_lo
и
pending_balance_hi
, так как расшифровка ElGamal требует больше вычислений для
больших чисел. Реализацию арифметики шифротекста можно найти
здесь,
которая используется в инструкции ApplyPendingBalance
здесь.
Счётчики кредитов ожидаемого баланса
При вызове инструкции ApplyPendingBalance
для конвертации ожидаемого
баланса в доступный:
-
Клиент проверяет текущие ожидаемый и доступный балансы, шифрует сумму и предоставляет
decryptable_available_balance
, зашифрованное с использованием AES-ключа владельца token account. -
Ожидаемые и фактические счётчики кредитов отслеживают изменения значения счётчика между моментом создания и обработки инструкции
ApplyPendingBalance
:expected_pending_balance_credit_counter
: Значениеpending_balance_credit_counter
на момент создания клиентом инструкцииApplyPendingBalance
actual_pending_balance_credit_counter
: Значениеpending_balance_credit_counter
в token account на момент обработки инструкцииApplyPendingBalance
Совпадение ожидаемых/фактических счетчиков указывает, что
decryptable_available_balance
соответствует available_balance
.
При получении состояния token account для чтения
decryptable_available_balance
, различие значений ожидаемых/фактических
счетчиков требует от клиента поиска последних инструкций по депозиту/переводу,
соответствующих разнице счетчиков, чтобы рассчитать правильный баланс.
Процесс сверки баланса
Когда ожидаемые и фактические счетчики незавершенного баланса различаются,
выполните следующие шаги, чтобы сверить decryptable_available_balance
:
- Начните с
decryptable_available_balance
из token account - Получите последние транзакции, включая инструкции по депозиту и переводу, до
разницы счетчиков (фактический - ожидаемый):
- Добавьте публичные суммы из инструкций по депозиту
- Расшифруйте и добавьте зашифрованные суммы назначения из инструкций по переводу
Необходимые инструкции
Создание token account с расширением Confidential Transfer требует выполнения трех инструкций:
-
Создайте token account: Вызовите инструкцию
AssociatedTokenAccountInstruction:Create
из Associated Token Program для создания token account. -
Перераспределите пространство аккаунта: Вызовите инструкцию
TokenInstruction::Reallocate
из Token Extension Program, чтобы добавить пространство для состоянияConfidentialTransferAccount
. -
Настройте Confidential Transfers: Вызовите инструкцию ConfidentialTransferInstruction::ConfigureAccount из Token Extension Program для инициализации состояния
ConfidentialTransferAccount
.
Только владелец token account может настроить аккаунт для конфиденциальных переводов.
Инструкция ConfigureAccount
требует генерации на стороне клиента ключей
шифрования и данных доказательства, которые может сгенерировать только владелец
token account.
Инструкция PubkeyValidityProofData
создает доказательство, подтверждающее,
что ключ ElGamal является действительным. Для деталей реализации см.:
Пример кода
Следующий код демонстрирует, как создать Associated Token Account с расширением Confidential Transfer.
Чтобы запустить пример, запустите локальный валидатор с Token Extension Program, клонированной из основной сети, используя следующую команду. Для запуска локального валидатора необходимо установить Solana CLI.
$solana-test-validator --clone-upgradeable-program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb --url https://api.mainnet-beta.solana.com -r
На момент написания конфиденциальные переводы не включены в стандартном локальном валидаторе. Вам необходимо клонировать основную сеть 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 (cloning Arc, not keypair));// 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?;println!("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);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?