vanity-core 0.8.1

Multi-chain vanity address grinding core library
Documentation
use crate::chain::{ChainGrinder, GrindAttempt, KeyExport, KeypairResult};
use crate::pattern::Pattern;
use bech32::{encode, Bech32, Hrp};
use secp256k1::SecretKey;

use super::util::{
    bech32_combinations, build_base58_pattern, expected_from_pattern, grind_secp256k1, hash160,
    matches_pattern, secret_from_attempt, BECH32_CHARSET,
};

#[derive(Clone)]
pub struct CosmosGrinder {
    pub id: &'static str,
    pub display_name: &'static str,
    pub hrp: &'static str,
}

impl CosmosGrinder {
    pub fn cosmos() -> Self {
        Self {
            id: "cosmos",
            display_name: "Cosmos (ATOM)",
            hrp: "cosmos",
        }
    }

    pub fn osmosis() -> Self {
        Self {
            id: "osmo",
            display_name: "Osmosis (OSMO)",
            hrp: "osmo",
        }
    }

    pub fn sei() -> Self {
        Self {
            id: "sei",
            display_name: "Sei",
            hrp: "sei",
        }
    }

    pub fn injective() -> Self {
        Self {
            id: "inj",
            display_name: "Injective (INJ)",
            hrp: "inj",
        }
    }

    pub fn celestia() -> Self {
        Self {
            id: "tia",
            display_name: "Celestia (TIA)",
            hrp: "celestia",
        }
    }

    pub fn dydx() -> Self {
        Self {
            id: "dydx",
            display_name: "dYdX",
            hrp: "dydx",
        }
    }

    fn derive(&self, secret: &SecretKey) -> String {
        let secp = secp256k1::Secp256k1::new();
        let pubkey = secret.public_key(&secp).serialize();
        let hash = hash160(&pubkey);
        let hrp = Hrp::parse(self.hrp).expect("valid hrp");
        encode::<Bech32>(hrp, &hash).expect("valid bech32 address")
    }
}

impl ChainGrinder for CosmosGrinder {
    fn id(&self) -> &'static str {
        self.id
    }

    fn display_name(&self) -> &'static str {
        self.display_name
    }

    fn grind_attempt(&self) -> (String, GrindAttempt) {
        grind_secp256k1(|secret| self.derive(secret))
    }

    fn finalize(&self, attempt: GrindAttempt) -> KeypairResult {
        let secret_bytes = secret_from_attempt(attempt);
        let secret_key = SecretKey::from_slice(&secret_bytes).expect("valid secp256k1 secret");
        let address = self.derive(&secret_key);

        KeypairResult {
            address,
            exports: vec![KeyExport {
                label: "Private Key (hex)".into(),
                value: hex::encode(secret_bytes),
                hint: Some("Keplr / Leap wallet import".into()),
            }],
        }
    }

    fn build_pattern(
        &self,
        prefix: Option<&str>,
        suffix: Option<&str>,
        contains: Option<&str>,
        exact: bool,
    ) -> Result<Pattern, String> {
        let mut pattern =
            build_base58_pattern(prefix, suffix, contains, exact, BECH32_CHARSET, 38)?;
        // Match on the data portion after hrp + "1"
        let hrp_prefix = format!("{}1", self.hrp);
        if pattern.has_prefix() && !pattern.prefix.starts_with(&hrp_prefix) {
            pattern.prefix = format!("{hrp_prefix}{}", pattern.prefix);
            pattern.prefix_match = if pattern.ignore_case {
                pattern.prefix.to_ascii_lowercase()
            } else {
                pattern.prefix.clone()
            };
        }
        pattern.set_fixed_prefix(&hrp_prefix);
        Ok(pattern)
    }

    fn expected_attempts(&self, pattern: &Pattern) -> f64 {
        expected_from_pattern(pattern, bech32_combinations)
    }

    fn matches(&self, address: &str, pattern: &Pattern) -> bool {
        matches_pattern(address, pattern, true)
    }

    fn supports_exact_case(&self) -> bool {
        false
    }

    fn pattern_hint(&self) -> &'static str {
        "Bech32 data chars, or full address including prefix (e.g. cosmos1…)."
    }
}

#[cfg(test)]
mod tests {
    use super::CosmosGrinder;
    use crate::chain::ChainGrinder;

    #[test]
    fn cosmos_address_has_prefix() {
        let g = CosmosGrinder::cosmos();
        let (addr, _) = g.grind_attempt();
        assert!(addr.starts_with("cosmos1"));
    }

    #[test]
    fn osmo_address_has_prefix() {
        let g = CosmosGrinder::osmosis();
        let (addr, _) = g.grind_attempt();
        assert!(addr.starts_with("osmo1"));
    }
}