To generate a vanity keypair in TypeScript, you can use a brute-force loop that repeatedly generates keypairs and checks if the public key matches the desired pattern.
import { Keypair } from '@solana/web3.js';
function generateVanityKeypair(prefix: string): Keypair {
let keypair: Keypair;
do {
keypair = Keypair.generate();
} while (!keypair.publicKey.toBase58().startsWith(prefix));
return keypair;
}
// Example usage
const prefix = 'abc'; // Desired prefix
const vanityKeypair = generateVanityKeypair(prefix);
console.log('Vanity Public Key:', vanityKeypair.publicKey.toBase58());
console.log('Private Key:', Buffer.from(vanityKeypair.secretKey).toString('hex'));