Hello. I have a question about spl-tokens’ token accounts
Is any token-accounts for any users are PDA(program derived accounts)?
If this is true, then they should not have secret key associated with its public key.
However, to create spl-token accounts, I can do following in my typescript code(not executable, just for explanation)
In below code, I did following
- First create a keypair by using Keypair() method
- add ‘createAccount’ transaction, with space 165, which is fixed length of space for token account, and assign it to TOKEN_PROGRAM_ID
- And assign it to a token-mint address by adding ‘initializeAccount’ transaction, which initialize the account so that I can use it as my token account.
Then this account is token account for some token and has it’s own secret key.
If token accounts are not PDA, where is PDA used for?
const owner = Keypair.fromSecretKey(~~~);
const newKeypair = new Keypair();
const transaction = new Transaction();
transaction.add(
SystemProgram.createAccount(
fromPubKey: owner.publicKey,
newAccountPubKey: newKeypair.publicKey,
lamports: ~~~,
space: 165,
programId: TOKEN_PROGRAM_ID
)
)
const keys = [
{ pubkey: newKeypair.publicKey, isSigner: false, isWritable: true },
{ pubkey: TARGET_TOKEN_MINT, isSigner: false, isWritable: false },
{ pubkey: owner.publicKey, isSigner: false, isWritable: false },
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
];
transaction.add(
TransactionInstructoin({
keys,
data: properly encoded data,
programId: TOKEN_PROGRAM_ID
})
)
connection.sendTransaction(transactoin, [owner, newKeypair])