How to Validate a Public Key
In certain special cases (e.g. a Program Derived Address), public keys may not have a private key associated with them. You can check this by looking to see if the public key lies on the ed25519 curve. Only public keys that lie on the curve can be controlled by users with wallets.
import { isAddress } from "@solana/web3.js";
// Note that generateKeyPair() will always give a public key that is valid for users
// Valid public key
const key = "5oNDL3swdJJF1g9DzJiZ4ynHXgszjAEpUkxVYejchzrY";
// Lies on the ed25519 curve and is suitable for users
console.log("Valid Address: ", isAddress(key));
// // Valid public key
const offCurveAddress = "4BJXYkfvg37zEmBbsacZjeQDpTNx91KppxFJxRqrz48e";
// // Not on the ed25519 curve, therefore not suitable for users
console.log("Valid Off Curve Address: ", isAddress(offCurveAddress));
// // Not a valid public key
const errorPubkey = "testPubkey";
console.log("Invalid Address: ", isAddress(errorPubkey));