إنشاء mint للرمز المميز

برنامج ZK ElGamal معطل مؤقتاً على الشبكة الرئيسية وشبكة المطورين حيث يخضع لتدقيق أمني. هذا يعني أن ملحق التحويلات السرية غير متاح حالياً. بينما المفاهيم لا تزال صالحة، فإن أمثلة الكود لن تعمل.

كيفية إنشاء mint مع ملحق التحويل السري

يتيح ملحق التحويل السري تحويلات الرموز المميزة الخاصة من خلال إضافة حالة إضافية إلى حساب mint. يشرح هذا القسم كيفية إنشاء mint للرمز المميز مع تمكين هذا الملحق.

يوضح الرسم التخطيطي التالي الخطوات المتضمنة في إنشاء mint مع ملحق التحويل السري:

Create Mint with Confidential Transfer Extension

حالة mint للتحويل السري

يضيف الملحق حالة ConfidentialTransferMint إلى حساب mint:

Confidential Mint State
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
pub struct ConfidentialTransferMint {
/// Authority to modify the `ConfidentialTransferMint` configuration and to
/// approve new accounts (if `auto_approve_new_accounts` is true)
///
/// The legacy Token Multisig account is not supported as the authority
pub authority: OptionalNonZeroPubkey,
/// Indicate if newly configured accounts must be approved by the
/// `authority` before they may be used by the user.
///
/// * If `true`, no approval is required and new accounts may be used
/// immediately
/// * If `false`, the authority must approve newly configured accounts (see
/// `ConfidentialTransferInstruction::ConfigureAccount`)
pub auto_approve_new_accounts: PodBool,
/// Authority to decode any transfer amount in a confidential transfer.
pub auditor_elgamal_pubkey: OptionalNonZeroElGamalPubkey,
}

تحتوي ConfidentialTransferMint على ثلاثة حقول تكوين:

  • authority: الحساب الذي لديه إذن لتغيير إعدادات التحويل السري لحساب mint والموافقة على حسابات سرية جديدة إذا كانت الموافقة التلقائية معطلة.

  • auto_approve_new_accounts: عند تعيينها على true، يمكن للمستخدمين إنشاء حسابات رموز مميزة مع تمكين التحويلات السرية افتراضياً. عند تعيينها على false، يجب على authority الموافقة على كل حساب رمز مميز جديد قبل أن يمكن استخدامه في التحويلات السرية.

  • auditor_elgamal_pubkey: مدقق اختياري يمكنه فك تشفير مبالغ التحويل في المعاملات السرية، مما يوفر آلية امتثال مع الحفاظ على الخصوصية من عامة الناس.

التعليمات المطلوبة

يتطلب إنشاء mint مع تمكين التحويل السري ثلاث تعليمات في معاملة واحدة:

  1. إنشاء حساب mint: استدعاء تعليمة CreateAccount من System Program لإنشاء حساب mint.

  2. تهيئة امتداد التحويل السري: استدعاء تعليمة ConfidentialTransferInstruction::InitializeMint من برنامج امتداد الرموز المميزة لتكوين حالة ConfidentialTransferMint لحساب mint.

  3. تهيئة حساب Mint: استدعاء تعليمة Instruction::InitializeMint من برنامج امتداد الرموز المميزة لتهيئة حالة mint القياسية.

بينما يمكنك كتابة هذه التعليمات يدويًا، توفر حزمة spl_token_client طريقة create_mint التي تبني وترسل معاملة تحتوي على جميع التعليمات الثلاث في استدعاء دالة واحدة، كما هو موضح في المثال أدناه.

مثال على الكود

يوضح الكود التالي كيفية إنشاء حساب mint مع امتداد التحويل السري.

لتشغيل المثال، ابدأ مدقق محلي مع برنامج امتداد الرموز المميزة المستنسخ من الشبكة الرئيسية باستخدام الأمر التالي. يجب أن يكون لديك Solana CLI مثبتًا لبدء المدقق المحلي.

Terminal
$
solana-test-validator --clone-upgradeable-program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb --url https://api.mainnet.solana.com -r

في وقت كتابة هذا المحتوى، لم يتم تفعيل التحويلات السرية على المدقق المحلي الافتراضي. يجب عليك استنساخ برنامج امتداد الرموز المميزة من الشبكة الرئيسية لتشغيل مثال الكود.

use anyhow::{Context, Result};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::{
commitment_config::CommitmentConfig,
signature::{Keypair, Signer},
};
use spl_token_client::{
client::{ProgramRpcClient, ProgramRpcClientSendTransaction},
spl_token_2022::id as token_2022_program_id,
token::{ExtensionInitializationParams, Token},
};
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<()> {
// Create connection to local test validator
let rpc_client = 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 keypair
let payer = Arc::new(load_keypair()?);
println!("Using payer: {}", payer.pubkey());
// Generate a new keypair to use as the address of the token mint
let mint = Keypair::new();
println!("Mint keypair generated: {}", mint.pubkey());
// Set up program client for Token client
let program_client =
ProgramRpcClient::new(Arc::new(rpc_client), ProgramRpcClientSendTransaction);
// Number of decimals for the mint
let decimals = 9;
// Create a token client for the Token-2022 program
// This provides high-level methods for token operations
let 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 mint
Some(decimals), // Number of decimal places
payer.clone(), // Fee payer for transactions (cloning Arc, not keypair)
);
// Create extension initialization parameters
// The ConfidentialTransferMint extension enables confidential (private) transfers of tokens
let extension_initialization_params =
vec![ExtensionInitializationParams::ConfidentialTransferMint {
authority: Some(payer.pubkey()), // Authority that can modify confidential transfer settings
auto_approve_new_accounts: true, // Automatically approve new confidential accounts
auditor_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 mint
let transaction_signature = token
.create_mint(
&payer.pubkey(), // Mint authority - can mint new tokens
Some(&payer.pubkey()), // Freeze authority - can freeze token accounts
extension_initialization_params, // Add the ConfidentialTransferMint extension
&[&mint], // Mint keypair needed as signer
)
.await?;
// Print results for user verification
println!("Mint Address: {}", mint.pubkey());
println!("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 tools
fn load_keypair() -> Result<Keypair> {
// Get the default keypair path
let 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 bytes
let 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 keypair
let keypair = Keypair::from_bytes(&keypair_bytes)?;
Ok(keypair)
}

Is this page helpful?

جدول المحتويات

تعديل الصفحة

تدار بواسطة

© 2026 مؤسسة سولانا.
جميع الحقوق محفوظة.
تواصل معنا