The PermanentDelegate
extension allows for a designated Permanent Delegate for
a Mint Account. This permanent delegate has unrestricted delegate privileges
over all Token Accounts for that mint, enabling them to burn or transfer tokens
without limitation.
In this guide, we'll walk through an example of creating a token with the Permanent Delegate using Solana Playground. Here is the final script that this guide will walkthrough.
Understanding the Implications
This is a very powerful feature, and its implications have to be clearly stated for both users and app developers.
The Permanent Delegate is effectively a global owner of all Token Accounts for the mint. Due to the unlimited powers of the Permanent Delegate, if the delegate's keys are compromised, an attacker will have complete control over all Token Accounts for that mint.
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
Lets 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
First, lets define the properties of the Mint Account we'll be creating in the following step.
Next, lets determine the size of the new Mint Account and calculate the minimum lamports needed for rent exemption.
With Token Extensions, the size of the Mint Account will vary based on the extensions enabled.
Build Instructions
We will need to build a set of instructions to:
- Create a new account
- Initialize the
PermanentDelegate
extension - Initialize the remaining Mint 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 PermanentDelegate
extension for
the Mint Account.
Lastly, build the instruction to initialize the rest of the Mint Account data. This is the same as with the original Token Program.
Send Transaction
Now add the instructions to a new transaction and send it to the network. This
will create a Mint Account with the PermanentDelegate
extension enabled.
Run the script by clicking the Run
button. You can then inspect the
transaction on the SolanaFM.
Create Token Accounts
Next, we will set up two Token Accounts to demonstrate the functionality of the Permanent Delegate.
First, generate a random keypair and use it as the owner of a
sourceTokenAccount
.
Next, create a destinationTokenAccount
owned by the Playground wallet.
Lastly, mint 2 tokens to the sourceTokenAccount
to fund it.
Transfer with Permanent Delegate
Next, lets send a transaction to transfer 1 token from the sourceTokenAccount
to the destinationTokenAccount
. Remember, the sourceTokenAccount
is owned by
a randomly generated keypair.
To transfer tokens using the Permanent Delegate, use the transferChecked
instruction and specify the Permanent Delegate as the owner of the
sourceTokenAccount
.
Burn with Permanent Delegate
Lets also send a transaction to burn 1 token from the sourceTokenAccount
.
To burn tokens using the Permanent Delegate
, use the burnChecked
instruction
and specify the Permanent Delegate as the owner of the sourceTokenAccount
.
Run the script by clicking the Run
button. You can then inspect the
transactions on the SolanaFM.
Note that both the transfer and burn transactions complete successfully, even though the transactions are not signed by the owner of the Token Account.
Conclusion
The PermanentDelegate
extension is a powerful extension that enables
developers to have much greater control over tokens they create, such as the
ability to retrieve tokens that have been mistakenly transferred. While this
extension offers greater flexibility, it's essential for users to be aware of
the implications of holding tokens with this extension enabled, particularly the
risks associated with compromised delegate keys.