Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generate keypair (WIP, my last day today) #64

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/lib/keypair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,38 @@ export const keypairToSecretKeyJSON = (keypair: Keypair): string => {
return JSON.stringify(Array.from(keypair.secretKey));
};

export const grindKeypairWithPrefix = async (
prefix: string,
): Promise<Keypair> => {
// 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
Expand Down Expand Up @@ -105,4 +137,4 @@ export const addKeypairToEnvFile = async (
// Shout out to Dean from WBA for this technique
export const makeKeypairs = (amount: number): Array<Keypair> => {
return Array.from({ length: amount }, () => Keypair.generate());
};
};
Loading