From c1514a49e907d89f87e7802bbf4d7c3ca4f3fb6b Mon Sep 17 00:00:00 2001 From: Mike MacCana Date: Wed, 6 Nov 2024 15:43:20 -0500 Subject: [PATCH] WIP work (my last day today) --- src/lib/keypair.ts | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/lib/keypair.ts b/src/lib/keypair.ts index 7c96820..51bc210 100644 --- a/src/lib/keypair.ts +++ b/src/lib/keypair.ts @@ -8,6 +8,38 @@ export const keypairToSecretKeyJSON = (keypair: Keypair): string => { return JSON.stringify(Array.from(keypair.secretKey)); }; +export const grindKeypairWithPrefix = async ( + prefix: string, +): Promise => { + // Check if the prefix contains characters outside the base58 alphabet + if ( + prefix.match( + /[^123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]/, + ) + ) { + throw new Error( + "Prefix contains invalid characters. Solana public keys may only include base58 characters, ie 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz", + ); + } + if (prefix.length > 3) { + console.warn("Prefix longer than 3 characters may take a long time."); + } + let keypair: Keypair; + + while (true) { + keypair = Keypair.generate(); + const publicKey = keypair.publicKey.toBase58(); + + if (publicKey.startsWith(prefix)) { + break; + } + } + + return keypair; +}; + +// const keypair = await grindKeypairWithPrefix(desiredPrefix); + export const getKeypairFromFile = async (filepath?: string) => { // Node-specific imports // @ts-expect-error TODO: fix the warning rather than disabling it when we have time @@ -105,4 +137,4 @@ export const addKeypairToEnvFile = async ( // Shout out to Dean from WBA for this technique export const makeKeypairs = (amount: number): Array => { return Array.from({ length: amount }, () => Keypair.generate()); -}; \ No newline at end of file +};