Generating Vanity KeyPairs on Solana with TypeScript
As a developer working with Solana, you are probably familiar with the Keypair
class provided by the Solana SDK. One of the most common use cases for key pairs is generating unique keys, also known as “public keys”. In this article, we will explore how to generate key pairs using TypeScript beyond the default methods provided by the Keypair
class.
Why Vanity Keypairs?
Vanity key pairs are useful when you need a short, unique public key for testing or demo purposes. They are also required for certain Solana features, such as staking and account delegation.
Generating Vanilla KeyPairs with TypeScript
You can use the Keypair.generate()
method to create vanilla key pairs. However, this method does not provide an easy way to generate key pairs without manually specifying a prefix or suffix.
One approach is to use a custom generator function that takes a string argument and returns a new Keypair
instance with the generated vanity key pair. Here is an example implementation:
interface GenerateVanityKeypairOptions {
prefix?: string;
suffix?: string;
}
const generateVanityKeypair = (options: GenerateVanityKeypairOptions) => {
const [prefix, suffix] = options.prefix && options.suffix ? [options.prefix, options.suffix] : [''];
const keypair = Keypair.generate({ prefix, suffix });
return { key pair };
};
const generateVanityKeypair = (options: GenerateVanityKeypairOptions) => {
const keypair = generateVanityKeypair(options);
return a pair of keys;
};
// Usage example:
options const: GenerateVanityKeypairOptions = {
prefix: 'solana-keypair-',
suffix: '.json',
};
const vanillaKeypair = generateVanityKeypair(options);
console.log(vanillaKeypair.keypair.toString('base64')); // Output: your_vanity_keypair_key
In this example, the generateVanityKeypair
function takes an options object with a prefix
and/or suffix
property. If provided, these values are used to create a vanity key pair; otherwise the default prefixes/suffixes are used.
The function generateVanityKeypair
calls generateVanityKeypair
and returns the generated vanilla key pair.
Corresponding prefix/suffix
While this implementation provides a basic way to generate a key pair using TypeScript, it may not be suitable for all use cases. If you need to match specific prefixes or suffixes, you can create a custom method that takes an options object with a prefix
and/or suffix
property.
Here’s an updated example:
interface GenerateVanityKeypairOptions {
prefix?: string;
suffix?: string;
}
const generateVanityKeypair = (options: GenerateVanityKeypairOptions) => {
if (!('prefix' in options || 'suffix' in options)) {
throw new Error('A required option is missing');
}
const [prefix, suffix] = options.prefix && options.suffix ? [options.prefix, options.suffix] : [''];
const keypair = Keypair.generate({ prefix, suffix });
return a pair of keys;
};
const generateVanityKeypair = (options: GenerateVanityKeypairOptions) => {
if (!('prefix' in options || 'suffix' in options)) {
throw new Error('A required option is missing');
}
const keypair = generateVanityKeypair(options);
return a pair of keys;
};
In this updated example, the generateVanityKeypair
function checks the prefix
and suffix
properties. If any are missing, an error occurs.
Conclusion
Generating null key pairs using TypeScript in addition to the default methods provided by the Keypair
class can be accomplished using a custom generate function that takes a string argument. This implementation provides a basic way to generate vanilla key pairs with prefixes and suffixes, but you may need to create additional logic to match specific keys.