1

Is there a way to generate vanity keypairs using Typescript apart from using the generate method and matching the prefix/suffix of generated keypairs

const keypair = Keypair.generate();
while (true) {
    if (keypair.publicKey.toBase58().startsWith(prefix)) break;
}

Also, I'd love a solution that does not involve web workers.

1 Answer 1

0

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'));
1
  • Thank you for the answer but in my question I am seeking an alternative?
    – Jimii
    Commented Dec 13 at 16:31

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.