1

I want to use getOrCreateAssociatedTokenAccount function in @solana/spl-token library. I want to change second field for findATA function to wallet without secretKey.

const adminWallet = Keypair.fromSecretKey(secretKey);

const adminTokenAccount = await findATA(
    connection,
    adminWallet,
    new anchor.web3.PublicKey(mintAccount.toString()),
    adminWallet.publicKey, // adminPubKey => wallet.publicKey
  );

const findATA = async (connection, payer, tokenMintAddress, walletAddress) => {
  return await getOrCreateAssociatedTokenAccount(
    connection,
    payer,
    tokenMintAddress,
    walletAddress,
  );
};

1 Answer 1

2

If the associated token account does not already exist, the getOrCreateAssociatedTokenAccount API may create it. As such, it may need to pay rent and the transaction fee to do so. Any such transaction needs to be signed by the secret key of the account designated to pay that fee.

If you would like to designate a payer whose secret key you do not have, consider building the transaction using the lower level createAssociatedTokenAccountInstruction method.

const transaction = new Transaction()
  .add(createAssociatedTokenAccountInstruction(
    payer,
    associatedToken,
    owner,
    mint,
  ));
const serializedTransaction = transaction.serialize({
  requireAllSignatures: false,
});

You can then send that serialized transaction to the payer, who can deserialize it and sign it.

const deserializedTransaction = Transaction.from(
  Buffer.from(serializedTransaction, "base64"),
);
deserializedTransaction.sign(payer);

At that point, the payer can submit the transaction to the network, or they can send it back to you to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.