With the Token Program, the SetAuthority
instruction can be used to change a
Token Account's owner to another account. The ImmutableOwner
extension ensures
that ownership of a Token Account cannot be reassigned.
In this guide, we WILL walk through an example of using Solana Playground. Here is the final script.
Understanding the Implications
So, why is this important? The addresses for Associated Token Accounts are derived based on the owner and the mint. This makes it easy to find the related Token Account for a specific owner.
If the owner of an existing Associated Token Account is changed, users may unintentionally transfer funds to an account under the assumption that it belongs to the original owner.
With Token Extensions, Associated Token Accounts have the ImmutableOwner
extension enabled by default, preventing the ownership from being changed.
The ImmutableOwner
extension can also be enabled for any new Token Account
created by the Token Extension program.
Getting Started
Start by opening this Solana Playground link with the following starter code.
If it is your first time using Solana Playground, you'll first need to create a Playground Wallet and fund the wallet with devnet SOL.
If you do not have a Playground wallet, you may see a type error within the
editor on all declarations of pg.wallet.publicKey
. This type error will clear
after you create a Playground wallet.
To get devnet SOL, run the solana airdrop
command in the Playground's
terminal, or visit this devnet faucet.
Once you've created and funded the Playground wallet, click the "Run" button to run the starter code.
Add Dependencies
Let's start by setting up our script. We'll be using the @solana/web3.js
and
@solana/spl-token
libraries.
Replace the starter code with the following:
Mint Setup
We'll first need to create a new Mint Account before we can create Token Accounts.
Associated Token Account
The ImmutableOwner
extension is enabled by default for Associated Token
Accounts created for Mint Accounts that are owned by the Token Extension
Program.
Let's demonstrate this concept by creating an Associated Token Account for the Playground wallet.
Attempting to change the owner of the Associated Token Account will result in an error.
Run the script by clicking the Run
button. You can then inspect the error in
the Playground terminal. You should see a message similar to the following:
Immutable Owner Token Account
Next, let's build a transaction to enable the ImmutableOwner
extension for a
new Token Account. Note that this can only be done for new Token Accounts.
First, let's generate a new keypair to use as the address of the Token Account.
Next, let's determine the size of the new Token Account and calculate the minimum lamports needed for rent exemption.
With Token Extensions, the size of the Token Account will vary based on the extensions enabled.
Build Instructions
Next, let's build the set of instructions to:
- Create a new account
- Initialize the
ImmutableOwner
extension - Initialize the remaining Token Account data
First, build the instruction to invoke the System Program to create an account and assign ownership to the Token Extensions Program.
Next, build the instruction to initialize the ImmutableOwner
extension for the
Token Account.
Lastly, build the instruction to initialize the rest of the Token Account data.
Send Transaction
Next, let's add the instructions to a new transaction and send it to the
network. This will create a Token Account with the ImmutableOwner
extension
enabled.
Run the script by clicking the Run
button. You can then inspect the
transaction details on SolanaFM.
If you attempt to change the owner of the Token Account, then you should see the same error as before.
Conclusion
The ImmutableOwner
extension prevents a vulnerability that was previously
possible by reassigning the owner of Associated Token Accounts. This security
feature can also be applied to any new Token Account, guarding against
unintended ownership changes.