|
| 1 | +import { type Command, Option } from "commander"; |
| 2 | +import { Wallet } from "xrpl"; |
| 3 | +import { info, output } from "../../lib/output.js"; |
| 4 | +import type { GlobalOptions } from "../../lib/types.js"; |
| 5 | +import { formatWallet, parseAlgorithm } from "./shared.js"; |
| 6 | + |
| 7 | +function toRegex(pattern: string, template: string, label: string): RegExp { |
| 8 | + try { |
| 9 | + return new RegExp(template.replace("{}", pattern), "i"); |
| 10 | + } catch { |
| 11 | + throw new Error(`Invalid regex in ${label}: ${pattern}`); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +function buildMatcher( |
| 16 | + startsWith?: string, |
| 17 | + endsWith?: string, |
| 18 | +): (address: string) => boolean { |
| 19 | + const startsRe = startsWith |
| 20 | + ? toRegex(startsWith, "^r{}", "--starts-with") |
| 21 | + : null; |
| 22 | + const endsRe = endsWith ? toRegex(endsWith, "{}$", "--ends-with") : null; |
| 23 | + |
| 24 | + return (address: string) => { |
| 25 | + if (startsRe && !startsRe.test(address)) return false; |
| 26 | + if (endsRe && !endsRe.test(address)) return false; |
| 27 | + return true; |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +async function vanityWallet( |
| 32 | + opts: { algorithm: string; startsWith?: string; endsWith?: string }, |
| 33 | + globalOpts: GlobalOptions, |
| 34 | +) { |
| 35 | + if (!opts.startsWith && !opts.endsWith) { |
| 36 | + throw new Error("At least one of --starts-with or --ends-with is required"); |
| 37 | + } |
| 38 | + |
| 39 | + const matches = buildMatcher(opts.startsWith, opts.endsWith); |
| 40 | + const algorithm = parseAlgorithm(opts.algorithm); |
| 41 | + |
| 42 | + info( |
| 43 | + "WARNING: Vanity addresses should NOT be used for mainnet accounts holding real value.\n", |
| 44 | + globalOpts, |
| 45 | + ); |
| 46 | + info("Searching for matching address...", globalOpts); |
| 47 | + |
| 48 | + let attempts = 0; |
| 49 | + let wallet: Wallet; |
| 50 | + |
| 51 | + do { |
| 52 | + wallet = Wallet.generate(algorithm); |
| 53 | + attempts++; |
| 54 | + if (attempts % 10000 === 0) { |
| 55 | + info(` ${attempts.toLocaleString()} attempts...`, globalOpts); |
| 56 | + } |
| 57 | + } while (!matches(wallet.address)); |
| 58 | + |
| 59 | + info(`Found after ${attempts.toLocaleString()} attempts.\n`, globalOpts); |
| 60 | + output(formatWallet(wallet), globalOpts); |
| 61 | +} |
| 62 | + |
| 63 | +export function registerVanity(wallet: Command, program: Command) { |
| 64 | + wallet |
| 65 | + .command("vanity") |
| 66 | + .description( |
| 67 | + "Generate a vanity address matching a pattern (NOT safe for mainnet use)", |
| 68 | + ) |
| 69 | + .option( |
| 70 | + "--starts-with <pattern>", |
| 71 | + "Prefix pattern to match after the leading 'r' (case-insensitive regex)", |
| 72 | + ) |
| 73 | + .option( |
| 74 | + "--ends-with <pattern>", |
| 75 | + "Suffix pattern to match (case-insensitive regex)", |
| 76 | + ) |
| 77 | + .addOption( |
| 78 | + new Option("--algorithm <algorithm>", "Key algorithm") |
| 79 | + .choices(["ed25519", "secp256k1"]) |
| 80 | + .default("ed25519"), |
| 81 | + ) |
| 82 | + .action( |
| 83 | + (opts: { algorithm: string; startsWith?: string; endsWith?: string }) => |
| 84 | + vanityWallet(opts, program.opts() as GlobalOptions), |
| 85 | + ); |
| 86 | +} |
0 commit comments