vanity-core 0.8.1

Multi-chain vanity address grinding core library
Documentation
use super::util::Keypair;
use crate::chain::{ChainGrinder, GrindAttempt, KeyExport, KeypairResult};
use crate::pattern::Pattern;

use super::util::{
    build_hex_pattern, expected_from_pattern, grind_ed25519, hex_combinations, keypair_from_secret,
    matches_pattern, secret_from_attempt,
};

#[derive(Clone, Default)]
pub struct NearGrinder;

impl NearGrinder {
    fn derive_address(keypair: &Keypair) -> String {
        hex::encode(keypair.pubkey().to_bytes())
    }
}

impl ChainGrinder for NearGrinder {
    fn id(&self) -> &'static str {
        "near"
    }

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

    fn grind_attempt(&self) -> (String, GrindAttempt) {
        grind_ed25519(Self::derive_address)
    }

    fn finalize(&self, attempt: GrindAttempt) -> KeypairResult {
        let secret_bytes = secret_from_attempt(attempt);
        let keypair = keypair_from_secret(secret_bytes);
        let address = Self::derive_address(&keypair);

        KeypairResult {
            address,
            exports: vec![KeyExport {
                label: "Private Key (hex)".into(),
                value: hex::encode(secret_bytes),
                hint: Some("NEAR Wallet / implicit account".into()),
            }],
        }
    }

    fn build_pattern(
        &self,
        prefix: Option<&str>,
        suffix: Option<&str>,
        contains: Option<&str>,
        _exact: bool,
    ) -> Result<Pattern, String> {
        build_hex_pattern(prefix, suffix, contains, false, 64)
    }

    fn expected_attempts(&self, pattern: &Pattern) -> f64 {
        expected_from_pattern(pattern, hex_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 {
        "Hex (implicit NEAR account = ed25519 public key)."
    }
}