Ορισμός εξουσιοδότησης

Τι Κάνει η Ορισμός Εξουσιοδότησης;

Ο ορισμός εξουσιοδότησης αλλάζει ή ανακαλεί συγκεκριμένους ρόλους εξουσιοδότησης σε έναν λογαριασμό mint ή token account.

Οι λογαριασμοί mint μπορούν να αποθηκεύουν μια εξουσιοδότηση mint και μια εξουσιοδότηση freeze. Οι λογαριασμοί token account αποθηκεύουν έναν owner και μπορούν προαιρετικά να αποθηκεύουν μια εξουσιοδότηση close. Κάθε ρόλος ελέγχει ένα συγκεκριμένο σύνολο δικαιωμάτων, και ο ορισμός μιας εξουσιοδότησης σε None αφαιρεί οριστικά τον επιλεγμένο ρόλο εξουσιοδότησης.

Το Token Extensions Program ορίζει επίσης πρόσθετους τύπους εξουσιοδότησης για συγκεκριμένες επεκτάσεις.

Πώς να Ορίσετε Εξουσιοδότηση

Η αλλαγή μιας εξουσιοδότησης χρησιμοποιεί την εντολή SetAuthority του Token Program.

Κάθε κλήση ενημερώνει έναν τύπο εξουσιοδότησης σε έναν λογαριασμό mint ή token account. Η τρέχουσα εξουσιοδότηση υπογράφει την αλλαγή, και η νέα εξουσιοδότηση μπορεί να είναι μια άλλη διεύθυνση ή None για να ανακληθεί οριστικά η επιλεγμένη εξουσιοδότηση.

Αναφορά Πηγής

ΣτοιχείοΠεριγραφήToken ProgramToken Extensions Program
MintΗ κατάσταση mint αποθηκεύει mint_authority και freeze_authority.ΠηγήΠηγή
AccountΗ κατάσταση token account αποθηκεύει owner και close_authority.ΠηγήΠηγή
AuthorityTypeΟι ρόλοι εξουσιοδότησης που μπορεί να ενημερώσει η SetAuthority.ΠηγήΠηγή
SetAuthorityΜια εντολή που αλλάζει ή ανακαλεί μία εξουσιοδότηση σε λογαριασμό mint ή token account.ΠηγήΠηγή
process_set_authorityΚοινή λογική επεξεργασίας για ενημερώσεις εξουσιοδότησης.ΠηγήΠηγή

Typescript

Τα παραδείγματα Kit παρακάτω δείχνουν την προτεινόμενη προσέγγιση χρησιμοποιώντας @solana/kit. Παλαιότερα παραδείγματα που χρησιμοποιούν @solana/web3.js περιλαμβάνονται για αναφορά.

Kit

import { generateKeyPairSigner } from "@solana/kit";
import { createLocalClient } from "@solana/kit-client-rpc";
import { AuthorityType, tokenProgram } from "@solana-program/token";
const client = await createLocalClient()
.use(tokenProgram());
const mint = await generateKeyPairSigner();
const newAuthority = await generateKeyPairSigner();
const result = await client.sendTransaction([
client.token.instructions.setAuthority({
owned: mint.address, // Mint whose authority changes.
owner: client.payer, // Current authority approving this change.
authorityType: AuthorityType.MintTokens, // Authority role to update on the mint.
newAuthority: newAuthority.address // New authority to assign to this role.
}),
client.token.instructions.setAuthority({
owned: mint.address, // Mint whose authority changes.
owner: client.payer, // Current authority approving this change.
authorityType: AuthorityType.FreezeAccount, // Authority role to update on the mint.
newAuthority: newAuthority.address // New authority to assign to this role.
})
]);
const mintAccount = await client.token.accounts.mint.fetch(mint.address);
console.log("Mint Address:", mint.address);
console.log("Mint Account:", mintAccount.data);
console.log("\nNew Authority Address:", newAuthority.address);
console.log("\nTransaction Signature:", result.context.signature);
Console
Click to execute the code.

Web3.js

import {
Connection,
Keypair,
sendAndConfirmTransaction,
Transaction,
LAMPORTS_PER_SOL
} from "@solana/web3.js";
import {
createMint,
AuthorityType,
createSetAuthorityInstruction,
getMint,
TOKEN_PROGRAM_ID
} from "@solana/spl-token";
const authorityBlockhash = await connection.getLatestBlockhash();
const result = await sendAndConfirmTransaction(
connection,
new Transaction({
feePayer: feePayer.publicKey,
blockhash: authorityBlockhash.blockhash,
lastValidBlockHeight: authorityBlockhash.lastValidBlockHeight
}).add(
createSetAuthorityInstruction(
mintPubkey, // Mint whose authority changes.
feePayer.publicKey, // Current authority approving this change.
AuthorityType.MintTokens, // Authority role to update on the mint.
newAuthority.publicKey, // New authority to assign to this role.
[], // Additional multisig signers.
TOKEN_PROGRAM_ID // Token program to invoke.
),
createSetAuthorityInstruction(
mintPubkey, // Mint whose authority changes.
feePayer.publicKey, // Current authority approving this change.
AuthorityType.FreezeAccount, // Authority role to update on the mint.
newAuthority.publicKey, // New authority to assign to this role.
[], // Additional multisig signers.
TOKEN_PROGRAM_ID // Token program to invoke.
)
),
[feePayer]
);
const mintAccount = await getMint(
connection,
mintPubkey,
"confirmed",
TOKEN_PROGRAM_ID
);
console.log("Mint Address:", mintPubkey.toBase58());
console.log("Mint Account:", mintAccount);
console.log("\nNew Authority Address:", newAuthority.publicKey.toBase58());
console.log("\nTransaction Signature:", result);
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<()> {
let client = RpcClient::new_with_commitment(
String::from("http://localhost:8899"),
CommitmentConfig::confirmed(),
);
let new_authority = Keypair::new();
let transaction = Transaction::new_signed_with_payer(
&[
set_authority(
&token_program_id(), // Token program to invoke.
&mint.pubkey(), // Mint whose authority changes.
Some(&new_authority.pubkey()), // New authority to assign to this role.
AuthorityType::MintTokens, // Authority role to update on the mint.
&fee_payer.pubkey(), // Current authority approving this change.
&[], // Additional multisig signers.
)?,
set_authority(
&token_program_id(), // Token program to invoke.
&mint.pubkey(), // Mint whose authority changes.
Some(&new_authority.pubkey()), // New authority to assign to this role.
AuthorityType::FreezeAccount, // Authority role to update on the mint.
&fee_payer.pubkey(), // Current authority approving this change.
&[], // Additional multisig signers.
)?,
],
Some(&fee_payer.pubkey()),
&[&fee_payer],
latest_blockhash,
);
let transaction_signature = 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 Account: {:#?}", mint_data);
println!("\nNew Authority Address: {}", new_authority.pubkey());
println!("\nTransaction Signature: {}", transaction_signature);
Ok(())
}
Console
Click to execute the code.

Python

Python
#!/usr/bin/env python3
import asyncio
import json
from solana.rpc.async_api import AsyncClient
from solders.keypair import Keypair
from solders.message import Message
from solders.pubkey import Pubkey
from solders.system_program import create_account, CreateAccountParams
from solders.transaction import Transaction
from spl.token.async_client import AsyncToken
from spl.token.instructions import (
AuthorityType,
initialize_mint,
InitializeMintParams,
set_authority,
SetAuthorityParams,
)
from spl.token.constants import MINT_LEN, TOKEN_PROGRAM_ID
async def main():
rpc = AsyncClient("http://localhost:8899")
new_authority = Keypair()
async with rpc:
set_authority_instruction = set_authority(
SetAuthorityParams(
program_id=TOKEN_PROGRAM_ID, # Token program to invoke.
account=mint.pubkey(), # Mint whose authority changes.
authority=AuthorityType.MINT_TOKENS, # Authority role to update.
current_authority=fee_payer.pubkey(), # Current authority approving the authority update.
new_authority=new_authority.pubkey(), # New authority for the selected role.
)
)
latest_blockhash = await rpc.get_latest_blockhash()
transaction = Transaction(
[fee_payer],
Message([set_authority_instruction], fee_payer.pubkey()),
latest_blockhash.value.blockhash,
)
result = await rpc.send_transaction(transaction)
mint_info = await token.get_mint_info()
mint_account = {
key: str(value) if isinstance(value, Pubkey) else value
for key, value in mint_info._asdict().items()
}
print("Mint Address:", mint.pubkey())
print("Mint Account:")
print(json.dumps(mint_account, indent=2))
print("\nNew Authority Address:", new_authority.pubkey())
print("\nTransaction Signature:", result.value)
if __name__ == "__main__":
asyncio.run(main())
Console
Click to execute the code.

Is this page helpful?

Πίνακας Περιεχομένων

Επεξεργασία Σελίδας

Διαχειρίζεται από

© 2026 Ίδρυμα Solana.
Με επιφύλαξη παντός δικαιώματος.
Συνδεθείτε