How to Set Authority on Token Accounts or Mints

You can set/update authority. There are 4 types:

  1. MintTokens (mint account)
  2. FreezeAccount (mint account)
  3. AccountOwner (token account)
  4. CloseAccount (token account)
import { getCreateAccountInstruction } from "@solana-program/system";
import {
AuthorityType,
findAssociatedTokenPda,
getCreateAssociatedTokenInstructionAsync,
getInitializeMintInstruction,
getMintSize,
getMintToCheckedInstruction,
getSetAuthorityInstruction,
TOKEN_2022_PROGRAM_ADDRESS,
} from "@solana-program/token-2022";
import {
airdropFactory,
appendTransactionMessageInstructions,
createSolanaRpc,
createSolanaRpcSubscriptions,
createTransactionMessage,
generateKeyPairSigner,
getSignatureFromTransaction,
lamports,
pipe,
sendAndConfirmTransactionFactory,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
signTransactionMessageWithSigners,
} from "@solana/kit";
const rpc = createSolanaRpc("http://127.0.0.1:8899");
const rpcSubscriptions = createSolanaRpcSubscriptions("ws://localhost:8900");
/* constants */
const MINT_AUTHORITY = await generateKeyPairSigner();
const NEW_AUTHORITY = await generateKeyPairSigner();
const DECIMALS = 9;
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
let { mint, authorityATA } = await setup();
// 1. Change Mint Authority (MintTokens)
const setMintAuthorityIx = getSetAuthorityInstruction({
owned: mint,
owner: MINT_AUTHORITY,
authorityType: AuthorityType.MintTokens,
newAuthority: NEW_AUTHORITY.address,
});
// 2. Change Freeze Authority
const setFreezeAuthorityIx = getSetAuthorityInstruction({
owned: mint,
owner: MINT_AUTHORITY,
authorityType: AuthorityType.FreezeAccount,
newAuthority: NEW_AUTHORITY.address,
});
// Example of revoking authority (setting to null)
const revokeMintAuthorityIx = getSetAuthorityInstruction({
owned: mint,
owner: NEW_AUTHORITY,
authorityType: AuthorityType.MintTokens,
newAuthority: null,
});
const instruction = [
setMintAuthorityIx,
setFreezeAuthorityIx,
revokeMintAuthorityIx,
];
const transactionMessage = pipe(
createTransactionMessage({ version: 0 }),
(tx) => setTransactionMessageFeePayerSigner(MINT_AUTHORITY, tx),
(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
(tx) => appendTransactionMessageInstructions(instruction, tx)
);
const signedTransaction = await signTransactionMessageWithSigners(
transactionMessage
);
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(
signedTransaction,
{ commitment: "confirmed" }
);
const txSignature = getSignatureFromTransaction(signedTransaction);
console.log("Transaction Signature:", txSignature);
/*
* The setup function initializes the mint and associated token accounts,
*
*/
async function setup() {
await airdropFactory({ rpc, rpcSubscriptions })({
recipientAddress: MINT_AUTHORITY.address,
lamports: lamports(1_000_000_000n), // 1 SOL
commitment: "confirmed",
});
const mint = await generateKeyPairSigner();
const space = BigInt(getMintSize());
const rent = await rpc.getMinimumBalanceForRentExemption(space).send();
// create & initialize mint account
const createAccountInstruction = getCreateAccountInstruction({
payer: MINT_AUTHORITY,
newAccount: mint,
lamports: rent,
space,
programAddress: TOKEN_2022_PROGRAM_ADDRESS,
});
const initializeMintInstruction = getInitializeMintInstruction({
mint: mint.address,
decimals: DECIMALS,
mintAuthority: MINT_AUTHORITY.address,
freezeAuthority: MINT_AUTHORITY.address,
});
// create token account
const [authorityATA] = await findAssociatedTokenPda({
mint: mint.address,
owner: MINT_AUTHORITY.address,
tokenProgram: TOKEN_2022_PROGRAM_ADDRESS,
});
const crateAuthorityATAInstruction =
await getCreateAssociatedTokenInstructionAsync({
payer: MINT_AUTHORITY,
mint: mint.address,
owner: MINT_AUTHORITY.address,
});
// mintTo token account
const mintToInstruction = await getMintToCheckedInstruction({
mint: mint.address,
token: authorityATA,
mintAuthority: MINT_AUTHORITY,
amount: 1_000_000_000_000n, // 1000
decimals: DECIMALS,
});
const instructions = [
createAccountInstruction,
initializeMintInstruction,
crateAuthorityATAInstruction,
mintToInstruction,
];
const transactionMessage = pipe(
createTransactionMessage({ version: 0 }),
(tx) => setTransactionMessageFeePayerSigner(MINT_AUTHORITY, tx),
(tx) =>
setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
(tx) => appendTransactionMessageInstructions(instructions, tx)
);
const signedTransaction = await signTransactionMessageWithSigners(
transactionMessage
);
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(
signedTransaction,
{ commitment: "confirmed" }
);
return {
mint: mint.address,
authorityATA,
};
}

Is this page helpful?