Thiết lập quyền hạn

Hiểu về quyền hạn Token

Quyền hạn của Mint Account

  • Mint Authority: Kiểm soát việc tạo ra token mới. Có thể mint token vào bất kỳ token account nào. Thường bị thu hồi sau khi lượng cung ban đầu được tạo ra để tạo token có lượng cung cố định.
  • Freeze Authority: Kiểm soát khả năng đóng băng và giải đóng token account. Có thể ngăn bất kỳ token account nào chuyển token. Thường bị thu hồi để đảm bảo với người dùng rằng token của họ không thể bị đóng băng.

Quyền hạn của Token Account

  • Account Owner: Có toàn quyền kiểm soát token account. Có thể chuyển token, đốt token, phê duyệt ủy quyền và đóng tài khoản khi số dư bằng không.
  • Close Authority: Có thể đóng token account khi số dư bằng không. Theo mặc định, đây là chủ sở hữu tài khoản nhưng có thể được ủy quyền cho một tài khoản khác.

Cách thiết lập quyền hạn

Lệnh SetAuthority thay đổi hoặc thu hồi quyền hạn trên mint và token account. Chỉ có quyền hạn hiện tại mới có thể chuyển quyền của họ sang một địa chỉ mới hoặc thu hồi vĩnh viễn bằng cách đặt quyền hạn thành null. Một khi bị thu hồi, quyền hạn không thể được khôi phục.

Token ProgramToken Extension Program có cách triển khai tương tự nhau để đạt được cùng một chức năng.

Typescript

import {
airdropFactory,
appendTransactionMessageInstructions,
createSolanaRpc,
createSolanaRpcSubscriptions,
createTransactionMessage,
generateKeyPairSigner,
getSignatureFromTransaction,
lamports,
pipe,
sendAndConfirmTransactionFactory,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
signTransactionMessageWithSigners
} from "@solana/kit";
import { getCreateAccountInstruction } from "@solana-program/system";
import {
getInitializeMintInstruction,
getMintSize,
TOKEN_PROGRAM_ADDRESS,
findAssociatedTokenPda,
getCreateAssociatedTokenInstructionAsync,
getSetAuthorityInstruction,
AuthorityType
} from "@solana-program/token";
// Create Connection, local validator in this example
const rpc = createSolanaRpc("http://localhost:8899");
const rpcSubscriptions = createSolanaRpcSubscriptions("ws://localhost:8900");
// Generate keypairs for fee payer and new authority
const feePayer = await generateKeyPairSigner();
const newAuthority = await generateKeyPairSigner();
// Fund fee payer
await airdropFactory({ rpc, rpcSubscriptions })({
recipientAddress: feePayer.address,
lamports: lamports(1_000_000_000n),
commitment: "confirmed"
});
// Generate keypair to use as address of mint
const mint = await generateKeyPairSigner();
// Get default mint account size (in bytes), no extensions enabled
const space = BigInt(getMintSize());
// Get minimum balance for rent exemption
const rent = await rpc.getMinimumBalanceForRentExemption(space).send();
// Get latest blockhash to include in transaction
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
// Instruction to create new account for mint (token program)
// Invokes the system program
const createAccountInstruction = getCreateAccountInstruction({
payer: feePayer,
newAccount: mint,
lamports: rent,
space,
programAddress: TOKEN_PROGRAM_ADDRESS
});
// Instruction to initialize mint account data
// Invokes the token program
const initializeMintInstruction = getInitializeMintInstruction({
mint: mint.address,
decimals: 9,
mintAuthority: feePayer.address,
freezeAuthority: feePayer.address
});
// Use findAssociatedTokenPda to derive the ATA address
const [associatedTokenAddress] = await findAssociatedTokenPda({
mint: mint.address,
owner: feePayer.address,
tokenProgram: TOKEN_PROGRAM_ADDRESS
});
// Create instruction to create the associated token account
const createAtaInstruction = await getCreateAssociatedTokenInstructionAsync({
payer: feePayer,
mint: mint.address,
owner: feePayer.address
});
const instructions = [
createAccountInstruction,
initializeMintInstruction,
createAtaInstruction
];
// Create transaction message
const transactionMessage = pipe(
createTransactionMessage({ version: 0 }),
(tx) => setTransactionMessageFeePayerSigner(feePayer, tx),
(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
(tx) => appendTransactionMessageInstructions(instructions, tx)
);
// Sign transaction message with all required signers
const signedTransaction =
await signTransactionMessageWithSigners(transactionMessage);
// Send and confirm transaction
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(
signedTransaction,
{ commitment: "confirmed" }
);
// Get transaction signature
const transactionSignature = getSignatureFromTransaction(signedTransaction);
console.log("\nMint Address:", mint.address.toString());
console.log("New Authority Address:", newAuthority.address.toString());
console.log(
"Associated Token Account Address:",
associatedTokenAddress.toString()
);
console.log("\nTransaction Signature:", transactionSignature);
// Get a fresh blockhash for the authority change transaction
const { value: authorityBlockhash } = await rpc.getLatestBlockhash().send();
// 1. Change Mint Authority (MintTokens)
const setMintAuthorityIx = getSetAuthorityInstruction({
owned: mint.address,
owner: feePayer,
authorityType: AuthorityType.MintTokens,
newAuthority: newAuthority.address
});
// 2. Change Freeze Authority (FreezeAccount)
const setFreezeAuthorityIx = getSetAuthorityInstruction({
owned: mint.address,
owner: feePayer,
authorityType: AuthorityType.FreezeAccount,
newAuthority: newAuthority.address
});
// Create transaction message for authority changes
const authorityTxMessage = pipe(
createTransactionMessage({ version: 0 }),
(tx) => setTransactionMessageFeePayerSigner(feePayer, tx),
(tx) => setTransactionMessageLifetimeUsingBlockhash(authorityBlockhash, tx),
(tx) =>
appendTransactionMessageInstructions(
[setMintAuthorityIx, setFreezeAuthorityIx],
tx
)
);
// Sign transaction message with all required signers
const signedAuthorityTx =
await signTransactionMessageWithSigners(authorityTxMessage);
// Send and confirm transaction
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(
signedAuthorityTx,
{ commitment: "confirmed" }
);
// Get transaction signature
const transactionSignature2 = getSignatureFromTransaction(signedAuthorityTx);
console.log("\nSuccessfully changed mint and freeze authorities");
console.log("Transaction Signature:", transactionSignature2);
// Get a fresh blockhash for the revoke transaction
const { value: revokeBlockhash } = await rpc.getLatestBlockhash().send();
// 3. Example of revoking authority (setting to null)
const revokeMintAuthorityIx = getSetAuthorityInstruction({
owned: mint.address,
owner: newAuthority,
authorityType: AuthorityType.MintTokens,
newAuthority: null
});
// Create transaction message for revoking
const revokeTxMessage = pipe(
createTransactionMessage({ version: 0 }),
(tx) => setTransactionMessageFeePayerSigner(feePayer, tx),
(tx) => setTransactionMessageLifetimeUsingBlockhash(revokeBlockhash, tx),
(tx) => appendTransactionMessageInstructions([revokeMintAuthorityIx], tx)
);
// Sign transaction message with all required signers (newAuthority needs to sign)
const signedRevokeTx = await signTransactionMessageWithSigners(revokeTxMessage);
// Send and confirm transaction
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(
signedRevokeTx,
{ commitment: "confirmed" }
);
// Get transaction signature
const transactionSignature3 = getSignatureFromTransaction(signedRevokeTx);
console.log("\nSuccessfully revoked mint authority");
console.log("Transaction Signature:", transactionSignature3);
Console
Click to execute the code.

Rust

Rust
use anyhow::Result;
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_commitment_config::CommitmentConfig;
use solana_sdk::{
program_pack::Pack,
signature::{Keypair, Signer},
transaction::Transaction,
};
use solana_system_interface::instruction::create_account;
use spl_token_interface::{
id as token_program_id,
instruction::{initialize_mint, set_authority, AuthorityType},
state::Mint,
};
#[tokio::main]
async fn main() -> Result<()> {
// Create connection to local validator
let client = RpcClient::new_with_commitment(
String::from("http://localhost:8899"),
CommitmentConfig::confirmed(),
);
let latest_blockhash = client.get_latest_blockhash().await?;
// Generate a new keypair for the fee payer
let fee_payer = Keypair::new();
// Generate a new keypair for the new authority
let new_authority = Keypair::new();
// Airdrop 1 SOL to fee payer
let airdrop_signature = client
.request_airdrop(&fee_payer.pubkey(), 1_000_000_000)
.await?;
client.confirm_transaction(&airdrop_signature).await?;
loop {
let confirmed = client.confirm_transaction(&airdrop_signature).await?;
if confirmed {
break;
}
}
// Generate keypair to use as address of mint
let mint = Keypair::new();
// Number of decimals for the mint
let decimals = 2;
// Get default mint account size (in bytes), no extensions enabled
let mint_space = Mint::LEN;
let mint_rent = client
.get_minimum_balance_for_rent_exemption(mint_space)
.await?;
// Instruction to create new account for mint (token program)
let create_account_instruction = create_account(
&fee_payer.pubkey(), // payer
&mint.pubkey(), // new account (mint)
mint_rent, // lamports
mint_space as u64, // space
&token_program_id(), // program id
);
// Instruction to initialize mint account data
let initialize_mint_instruction = initialize_mint(
&token_program_id(),
&mint.pubkey(), // mint
&fee_payer.pubkey(), // mint authority
Some(&fee_payer.pubkey()), // freeze authority
decimals, // decimals
)?;
// Create transaction and add instructions
let transaction = Transaction::new_signed_with_payer(
&[create_account_instruction, initialize_mint_instruction],
Some(&fee_payer.pubkey()),
&[&fee_payer, &mint],
latest_blockhash,
);
// Send and confirm transaction
client.send_and_confirm_transaction(&transaction).await?;
let mint_account = client.get_account(&mint.pubkey()).await?;
let mint_data = Mint::unpack(&mint_account.data)?;
println!("Mint Address: {}", mint.pubkey());
println!("{:#?}", mint_data);
// 1. Change Mint Authority (MintTokens)
let set_mint_authority_ix = set_authority(
&token_program_id(), // program id
&mint.pubkey(), // mint account
Some(&new_authority.pubkey()), // new authority
AuthorityType::MintTokens, // authority type
&fee_payer.pubkey(), // current authority
&[&fee_payer.pubkey()], // signers
)?;
// 2. Change Freeze Authority (FreezeAccount)
let set_freeze_authority_ix = set_authority(
&token_program_id(), // program id
&mint.pubkey(), // mint account
Some(&new_authority.pubkey()), // new authority
AuthorityType::FreezeAccount, // authority type
&fee_payer.pubkey(), // current authority
&[&fee_payer.pubkey()], // signers
)?;
// Create transaction for authority changes
let transaction = Transaction::new_signed_with_payer(
&[set_mint_authority_ix, set_freeze_authority_ix],
Some(&fee_payer.pubkey()),
&[&fee_payer],
latest_blockhash,
);
// Send and confirm transaction
client.send_and_confirm_transaction(&transaction).await?;
let mint_account = client.get_account(&mint.pubkey()).await?;
let mint_data = Mint::unpack(&mint_account.data)?;
println!("\nMint and Freeze Authorities Updated");
println!("{:#?}", mint_data);
// 3. Example of revoking authority (setting to null)
let revoke_mint_authority_ix = set_authority(
&token_program_id(), // program id
&mint.pubkey(), // mint account
None, // new authority (None to revoke)
AuthorityType::MintTokens, // authority type
&new_authority.pubkey(), // current authority
&[&new_authority.pubkey()], // signers
)?;
// Create transaction for revoking
let transaction = Transaction::new_signed_with_payer(
&[revoke_mint_authority_ix],
Some(&fee_payer.pubkey()),
&[&fee_payer, &new_authority], // new_authority needs to sign
latest_blockhash,
);
// Send and confirm transaction
client.send_and_confirm_transaction(&transaction).await?;
let mint_account = client.get_account(&mint.pubkey()).await?;
let mint_data = Mint::unpack(&mint_account.data)?;
println!("\nMint Authority Revoked");
println!("{:#?}", mint_data);
Ok(())
}
Console
Click to execute the code.

Python

Python
#!/usr/bin/env python3
import asyncio
from solana.rpc.async_api import AsyncClient
from solders.keypair import Keypair
from solders.pubkey import Pubkey
from solders.transaction import VersionedTransaction
from solders.message import MessageV0
from spl.token.instructions import set_authority, SetAuthorityParams
from spl.token.constants import TOKEN_PROGRAM_ID
from spl.token.instructions import AuthorityType
async def main():
rpc = AsyncClient("http://localhost:8899")
# Example keypairs and addresses
payer = Keypair()
current_authority = Keypair()
new_authority = Keypair()
mint_or_account = Pubkey.from_string("4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU")
async with rpc:
# Set new mint authority
set_mint_authority_instruction = set_authority(
SetAuthorityParams(
program_id=TOKEN_PROGRAM_ID,
account=mint_or_account,
authority=AuthorityType.MINT_TOKENS,
current_authority=current_authority.pubkey(),
new_authority=new_authority.pubkey()
)
)
# Get latest blockhash
recent_blockhash = await rpc.get_latest_blockhash()
# Create message
message = MessageV0.try_compile(
payer=payer.pubkey(),
instructions=[set_mint_authority_instruction],
address_lookup_table_accounts=[],
recent_blockhash=recent_blockhash.value.blockhash
)
# Create transaction
transaction = VersionedTransaction(message, [payer, current_authority])
print(f"Account/Mint: {mint_or_account}")
print(f"Current Authority: {current_authority.pubkey()}")
print(f"New Authority: {new_authority.pubkey()}")
print(f"Authority Type: MintTokens")
print(f"Payer: {payer.pubkey()}")
print(f"Set authority transaction created successfully")
async def set_freeze_authority_example():
"""Example of setting freeze authority"""
rpc = AsyncClient("http://localhost:8899")
payer = Keypair()
current_authority = Keypair()
new_authority = Keypair()
mint_address = Pubkey.from_string("4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU")
async with rpc:
# Set freeze authority
set_freeze_authority_instruction = set_authority(
SetAuthorityParams(
program_id=TOKEN_PROGRAM_ID,
account=mint_address,
authority=AuthorityType.FREEZE_ACCOUNT,
current_authority=current_authority.pubkey(),
new_authority=new_authority.pubkey()
)
)
recent_blockhash = await rpc.get_latest_blockhash()
message = MessageV0.try_compile(
payer=payer.pubkey(),
instructions=[set_freeze_authority_instruction],
address_lookup_table_accounts=[],
recent_blockhash=recent_blockhash.value.blockhash
)
transaction = VersionedTransaction(message, [payer, current_authority])
print(f"\nFreeze Authority Example:")
print(f"Mint: {mint_address}")
print(f"Current Freeze Authority: {current_authority.pubkey()}")
print(f"New Freeze Authority: {new_authority.pubkey()}")
print(f"Set freeze authority transaction created successfully")
if __name__ == "__main__":
asyncio.run(main())
asyncio.run(set_freeze_authority_example())
Console
Click to execute the code.

Is this page helpful?

Mục lục

Chỉnh sửa trang

Quản lý bởi

© 2025 Solana Foundation.
Đã đăng ký bản quyền.
Kết nối