4

If I am given an arbitrary Solana public key without any transactions associated to it, is there a way I can determine if this is a normal account or an ATA?

My initial approach would be to see if it lies on the ed25519 curve, as ATAs are PDAs. However, this only tells me if I am dealing with a PDA or not. Is there a way to check specifically for ATAs?

2 Answers 2

5

A token account can also be a program-derived address of other programs than the associated token program. So being off the curve doesn't tell you enough.

For example, a vault program could store tokens in a PDA that isn't an ATA.

You cannot determine from the address alone if it is an ATA, because you cannot reverse the hash process.

However, if you fetch the token account and decode its field you can!

Once decoded you get access to the token account mint and owner, as a result, you can verify that this specific mint and the owner would result in the given token account address.

This is the snippet of the process

import { PublicKey, clusterApiUrl } from "@solana/web3.js"
import { getAssociatedTokenAddressSync, getAccount } from "@solana/spl-token"

const connection = new Connection(clusterApiUrl("mainnet-beta"));
const address = new PublicKey("<TheAddressHere>")

const tokenAccount = await getAccount(connection, address);

const expectedAta = getAssociatedTokenAddressSync(
  tokenAccount.mint,
  tokenAccount.owner
);

if (expectedAta.equals(address)) {
  console.log("It is an ATA");
} else {
  console.log("Not an ATA");
}
1

Not sure how to check if an address is specifically for an ATA, but you can use getAccount from @solana/spl-token to check if it is the address for a token account.

import { PublicKey } from "@solana/web3.js"
import { Account, getAccount } from "@solana/spl-token"

const addressToCheck = new PublicKey("...")

let tokenAccount: Account
try {
  tokenAccount = await getAccount(connection, addressToCheck)
} catch (e) {
  console.log("Token account does not exist")
}

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.