Skip to main content

vanity_core/chains/
mod.rs

1mod algorand;
2mod aptos;
3mod bitcoin_like;
4mod bitcoin_segwit;
5mod cardano;
6mod cosmos;
7pub mod create2;
8mod evm;
9mod filecoin;
10mod hedera;
11mod icp;
12mod kaspa;
13mod kusama;
14mod multiversx;
15mod near;
16mod polkadot;
17mod ripple;
18mod solana;
19mod stellar;
20mod sui;
21mod tezos;
22mod ton;
23mod tron;
24mod util;
25
26pub use algorand::AlgorandGrinder;
27pub use aptos::AptosGrinder;
28pub use bitcoin_like::BitcoinLikeGrinder;
29pub use bitcoin_segwit::BitcoinSegwitGrinder;
30pub use cardano::CardanoGrinder;
31pub use cosmos::CosmosGrinder;
32pub use create2::Create2Grinder;
33pub use evm::EvmGrinder;
34pub use filecoin::FilecoinGrinder;
35pub use hedera::HederaGrinder;
36pub use icp::IcpGrinder;
37pub use kaspa::KaspaGrinder;
38pub use kusama::KusamaGrinder;
39pub use multiversx::MultiversXGrinder;
40pub use near::NearGrinder;
41pub use polkadot::PolkadotGrinder;
42pub use ripple::RippleGrinder;
43pub use solana::SolanaGrinder;
44pub use stellar::StellarGrinder;
45pub use sui::SuiGrinder;
46pub use tezos::TezosGrinder;
47pub use ton::TonGrinder;
48pub use tron::TronGrinder;
49
50use crate::chain::{ChainGrinder, GrindAttempt, KeypairResult};
51use crate::pattern::Pattern;
52
53#[derive(Clone)]
54pub enum Chain {
55    Solana(SolanaGrinder),
56    Evm(EvmGrinder),
57    Bitcoin(BitcoinLikeGrinder),
58    BitcoinSegwit(BitcoinSegwitGrinder),
59    BitcoinTaproot(BitcoinSegwitGrinder),
60    Litecoin(BitcoinLikeGrinder),
61    Dogecoin(BitcoinLikeGrinder),
62    Dash(BitcoinLikeGrinder),
63    Tron(TronGrinder),
64    Cosmos(CosmosGrinder),
65    Osmosis(CosmosGrinder),
66    Sei(CosmosGrinder),
67    Injective(CosmosGrinder),
68    Celestia(CosmosGrinder),
69    Dydx(CosmosGrinder),
70    Ripple(RippleGrinder),
71    Stellar(StellarGrinder),
72    Aptos(AptosGrinder),
73    Sui(SuiGrinder),
74    Near(NearGrinder),
75    Algorand(AlgorandGrinder),
76    Tezos(TezosGrinder),
77    Icp(IcpGrinder),
78    Kaspa(KaspaGrinder),
79    Kusama(KusamaGrinder),
80    Ton(TonGrinder),
81    Filecoin(FilecoinGrinder),
82    Polkadot(PolkadotGrinder),
83    Cardano(CardanoGrinder),
84    Hedera(HederaGrinder),
85    MultiversX(MultiversXGrinder),
86    Create2(Create2Grinder),
87}
88
89/// Menu label for interactive chain picker (index 0-based).
90/// Ordered A–Z by display name for easier selection.
91pub const MENU_CHAINS: [(&str, &str); 31] = [
92    ("algo", "Algorand (base32)"),
93    ("aptos", "Aptos (0x hex)"),
94    ("btc", "Bitcoin (base58 · P2PKH)"),
95    ("btc-segwit", "Bitcoin SegWit (bc1q · P2WPKH)"),
96    ("btc-taproot", "Bitcoin Taproot (bc1p)"),
97    ("ada", "Cardano (enterprise addr1)"),
98    ("tia", "Celestia (bech32 · TIA)"),
99    ("cosmos", "Cosmos (bech32 · ATOM)"),
100    ("dash", "Dash (base58 · P2PKH)"),
101    ("doge", "Dogecoin (base58)"),
102    ("dydx", "dYdX (bech32)"),
103    ("evm", "EVM (0x hex · MetaMask / Base / Robinhood…)"),
104    ("fil", "Filecoin (f1 · secp256k1)"),
105    ("hedera", "Hedera (ed25519 pubkey hex)"),
106    ("inj", "Injective (bech32 · INJ)"),
107    ("icp", "Internet Computer (principal)"),
108    ("kaspa", "Kaspa (bech32)"),
109    ("ksm", "Kusama (SS58 · ed25519)"),
110    ("ltc", "Litecoin (base58 · P2PKH)"),
111    ("erd", "MultiversX (bech32 · erd1)"),
112    ("near", "NEAR (hex implicit account)"),
113    ("osmo", "Osmosis (bech32 · OSMO)"),
114    ("dot", "Polkadot (SS58 · ed25519)"),
115    ("xrp", "Ripple (base58 · r…)"),
116    ("sei", "Sei (bech32 · SEI)"),
117    ("sol", "Solana (base58 · Phantom, Solflare)"),
118    ("xlm", "Stellar (strkey · G…)"),
119    ("sui", "Sui (0x hex)"),
120    ("xtz", "Tezos (tz1 · ed25519)"),
121    ("ton", "TON (Wallet V4R2 · UQ…)"),
122    ("trx", "Tron (base58 · T…)"),
123];
124
125impl Chain {
126    pub fn from_menu_index(index: usize) -> Option<Self> {
127        match index {
128            0 => Some(Chain::Algorand(AlgorandGrinder)),
129            1 => Some(Chain::Aptos(AptosGrinder)),
130            2 => Some(Chain::Bitcoin(BitcoinLikeGrinder::bitcoin())),
131            3 => Some(Chain::BitcoinSegwit(BitcoinSegwitGrinder::segwit())),
132            4 => Some(Chain::BitcoinTaproot(BitcoinSegwitGrinder::taproot())),
133            5 => Some(Chain::Cardano(CardanoGrinder)),
134            6 => Some(Chain::Celestia(CosmosGrinder::celestia())),
135            7 => Some(Chain::Cosmos(CosmosGrinder::cosmos())),
136            8 => Some(Chain::Dash(BitcoinLikeGrinder::dash())),
137            9 => Some(Chain::Dogecoin(BitcoinLikeGrinder::dogecoin())),
138            10 => Some(Chain::Dydx(CosmosGrinder::dydx())),
139            11 => Some(Chain::Evm(EvmGrinder)),
140            12 => Some(Chain::Filecoin(FilecoinGrinder)),
141            13 => Some(Chain::Hedera(HederaGrinder)),
142            14 => Some(Chain::Injective(CosmosGrinder::injective())),
143            15 => Some(Chain::Icp(IcpGrinder)),
144            16 => Some(Chain::Kaspa(KaspaGrinder)),
145            17 => Some(Chain::Kusama(KusamaGrinder)),
146            18 => Some(Chain::Litecoin(BitcoinLikeGrinder::litecoin())),
147            19 => Some(Chain::MultiversX(MultiversXGrinder)),
148            20 => Some(Chain::Near(NearGrinder)),
149            21 => Some(Chain::Osmosis(CosmosGrinder::osmosis())),
150            22 => Some(Chain::Polkadot(PolkadotGrinder)),
151            23 => Some(Chain::Ripple(RippleGrinder)),
152            24 => Some(Chain::Sei(CosmosGrinder::sei())),
153            25 => Some(Chain::Solana(SolanaGrinder)),
154            26 => Some(Chain::Stellar(StellarGrinder)),
155            27 => Some(Chain::Sui(SuiGrinder)),
156            28 => Some(Chain::Tezos(TezosGrinder)),
157            29 => Some(Chain::Ton(TonGrinder)),
158            30 => Some(Chain::Tron(TronGrinder)),
159            _ => None,
160        }
161    }
162
163    pub fn from_id(id: &str) -> Result<Self, String> {
164        let id = id.to_ascii_lowercase();
165        match id.as_str() {
166            "sol" | "solana" => Ok(Chain::Solana(SolanaGrinder)),
167            // EVM + trending L2 / app-chain aliases (same address math)
168            "evm" | "eth" | "ethereum" | "base" | "arb" | "arbitrum" | "op" | "optimism"
169            | "polygon" | "matic" | "avax" | "avalanche" | "bnb" | "bsc" | "fantom" | "ftm"
170            | "scroll" | "linea" | "zksync" | "blast" | "mantle" | "mode" | "bera"
171            | "berachain" | "monad" | "abstract" | "unichain" | "robinhood" | "hood" | "rh" => {
172                Ok(Chain::Evm(EvmGrinder))
173            }
174            "btc" | "bitcoin" => Ok(Chain::Bitcoin(BitcoinLikeGrinder::bitcoin())),
175            "btc-segwit" | "segwit" | "bc1q" => {
176                Ok(Chain::BitcoinSegwit(BitcoinSegwitGrinder::segwit()))
177            }
178            "btc-taproot" | "taproot" | "bc1p" => {
179                Ok(Chain::BitcoinTaproot(BitcoinSegwitGrinder::taproot()))
180            }
181            "ltc" | "litecoin" => Ok(Chain::Litecoin(BitcoinLikeGrinder::litecoin())),
182            "doge" | "dogecoin" => Ok(Chain::Dogecoin(BitcoinLikeGrinder::dogecoin())),
183            "dash" => Ok(Chain::Dash(BitcoinLikeGrinder::dash())),
184            "trx" | "tron" => Ok(Chain::Tron(TronGrinder)),
185            "cosmos" | "atom" => Ok(Chain::Cosmos(CosmosGrinder::cosmos())),
186            "osmo" | "osmosis" => Ok(Chain::Osmosis(CosmosGrinder::osmosis())),
187            "sei" => Ok(Chain::Sei(CosmosGrinder::sei())),
188            "inj" | "injective" => Ok(Chain::Injective(CosmosGrinder::injective())),
189            "tia" | "celestia" => Ok(Chain::Celestia(CosmosGrinder::celestia())),
190            "dydx" => Ok(Chain::Dydx(CosmosGrinder::dydx())),
191            "xrp" | "ripple" => Ok(Chain::Ripple(RippleGrinder)),
192            "xlm" | "stellar" => Ok(Chain::Stellar(StellarGrinder)),
193            "aptos" | "apt" => Ok(Chain::Aptos(AptosGrinder)),
194            "sui" => Ok(Chain::Sui(SuiGrinder)),
195            "near" => Ok(Chain::Near(NearGrinder)),
196            "algo" | "algorand" => Ok(Chain::Algorand(AlgorandGrinder)),
197            "xtz" | "tezos" => Ok(Chain::Tezos(TezosGrinder)),
198            "icp" | "internet-computer" | "dfinity" => Ok(Chain::Icp(IcpGrinder)),
199            "kaspa" | "kas" => Ok(Chain::Kaspa(KaspaGrinder)),
200            "ksm" | "kusama" => Ok(Chain::Kusama(KusamaGrinder)),
201            "ton" => Ok(Chain::Ton(TonGrinder)),
202            "fil" | "filecoin" => Ok(Chain::Filecoin(FilecoinGrinder)),
203            "dot" | "polkadot" | "substrate" => Ok(Chain::Polkadot(PolkadotGrinder)),
204            "ada" | "cardano" => Ok(Chain::Cardano(CardanoGrinder)),
205            "hedera" | "hbar" => Ok(Chain::Hedera(HederaGrinder)),
206            "erd" | "mvx" | "elrond" | "multiversx" => Ok(Chain::MultiversX(MultiversXGrinder)),
207            _ => Err(format!(
208                "Unknown chain '{id}'. Supported: {}",
209                Self::supported_ids_display()
210            )),
211        }
212    }
213
214    /// Chain IDs in the same A–Z menu order.
215    pub fn all_ids() -> &'static [&'static str] {
216        &[
217            "algo",
218            "aptos",
219            "btc",
220            "btc-segwit",
221            "btc-taproot",
222            "ada",
223            "tia",
224            "cosmos",
225            "dash",
226            "doge",
227            "dydx",
228            "evm",
229            "fil",
230            "hedera",
231            "inj",
232            "icp",
233            "kaspa",
234            "ksm",
235            "ltc",
236            "erd",
237            "near",
238            "osmo",
239            "dot",
240            "xrp",
241            "sei",
242            "sol",
243            "xlm",
244            "sui",
245            "xtz",
246            "ton",
247            "trx",
248        ]
249    }
250
251    fn supported_ids_display() -> String {
252        Self::all_ids().join(", ")
253    }
254}
255
256macro_rules! dispatch {
257    ($self:expr, $method:ident ( $($arg:expr),* $(,)? )) => {
258        match $self {
259            Chain::Solana(g) => g.$method($($arg),*),
260            Chain::Evm(g) => g.$method($($arg),*),
261            Chain::Bitcoin(g) => g.$method($($arg),*),
262            Chain::BitcoinSegwit(g) => g.$method($($arg),*),
263            Chain::BitcoinTaproot(g) => g.$method($($arg),*),
264            Chain::Litecoin(g) => g.$method($($arg),*),
265            Chain::Dogecoin(g) => g.$method($($arg),*),
266            Chain::Dash(g) => g.$method($($arg),*),
267            Chain::Tron(g) => g.$method($($arg),*),
268            Chain::Cosmos(g) => g.$method($($arg),*),
269            Chain::Osmosis(g) => g.$method($($arg),*),
270            Chain::Sei(g) => g.$method($($arg),*),
271            Chain::Injective(g) => g.$method($($arg),*),
272            Chain::Celestia(g) => g.$method($($arg),*),
273            Chain::Dydx(g) => g.$method($($arg),*),
274            Chain::Ripple(g) => g.$method($($arg),*),
275            Chain::Stellar(g) => g.$method($($arg),*),
276            Chain::Aptos(g) => g.$method($($arg),*),
277            Chain::Sui(g) => g.$method($($arg),*),
278            Chain::Near(g) => g.$method($($arg),*),
279            Chain::Algorand(g) => g.$method($($arg),*),
280            Chain::Tezos(g) => g.$method($($arg),*),
281            Chain::Icp(g) => g.$method($($arg),*),
282            Chain::Kaspa(g) => g.$method($($arg),*),
283            Chain::Kusama(g) => g.$method($($arg),*),
284            Chain::Ton(g) => g.$method($($arg),*),
285            Chain::Filecoin(g) => g.$method($($arg),*),
286            Chain::Polkadot(g) => g.$method($($arg),*),
287            Chain::Cardano(g) => g.$method($($arg),*),
288            Chain::Hedera(g) => g.$method($($arg),*),
289            Chain::MultiversX(g) => g.$method($($arg),*),
290            Chain::Create2(g) => g.$method($($arg),*),
291        }
292    };
293}
294
295impl ChainGrinder for Chain {
296    fn id(&self) -> &'static str {
297        dispatch!(self, id())
298    }
299
300    fn display_name(&self) -> &'static str {
301        dispatch!(self, display_name())
302    }
303
304    fn grind_attempt(&self) -> (String, GrindAttempt) {
305        dispatch!(self, grind_attempt())
306    }
307
308    fn finalize(&self, attempt: GrindAttempt) -> KeypairResult {
309        dispatch!(self, finalize(attempt))
310    }
311
312    fn build_pattern(
313        &self,
314        prefix: Option<&str>,
315        suffix: Option<&str>,
316        contains: Option<&str>,
317        exact: bool,
318    ) -> Result<Pattern, String> {
319        dispatch!(self, build_pattern(prefix, suffix, contains, exact))
320    }
321
322    fn expected_attempts(&self, pattern: &Pattern) -> f64 {
323        dispatch!(self, expected_attempts(pattern))
324    }
325
326    fn matches(&self, address: &str, pattern: &Pattern) -> bool {
327        dispatch!(self, matches(address, pattern))
328    }
329
330    fn supports_exact_case(&self) -> bool {
331        dispatch!(self, supports_exact_case())
332    }
333
334    fn pattern_hint(&self) -> &'static str {
335        dispatch!(self, pattern_hint())
336    }
337}
338
339#[cfg(test)]
340mod tests {
341    use super::Chain;
342    use crate::chain::ChainGrinder;
343
344    #[test]
345    fn all_menu_chains_resolve_and_grind() {
346        assert_eq!(super::MENU_CHAINS.len(), 31);
347        assert_eq!(Chain::all_ids().len(), 31);
348        for (i, (id, _)) in super::MENU_CHAINS.iter().enumerate() {
349            let chain = Chain::from_menu_index(i).expect("menu index");
350            assert_eq!(chain.id(), *id);
351            let via_id = Chain::from_id(id).expect("from_id");
352            assert_eq!(via_id.id(), *id);
353            let (addr, attempt) = chain.grind_attempt();
354            assert!(!addr.is_empty(), "{id} empty address");
355            let finalized = chain.finalize(attempt);
356            assert_eq!(finalized.address, addr, "{id} finalize mismatch");
357            assert!(!finalized.exports.is_empty(), "{id} missing exports");
358        }
359    }
360
361    #[test]
362    fn from_menu_index_matches_menu_chains_order() {
363        for (i, (id, _)) in super::MENU_CHAINS.iter().enumerate() {
364            let chain = Chain::from_menu_index(i).expect("menu index");
365            assert_eq!(
366                chain.id(),
367                *id,
368                "from_menu_index({i}) misaligned with MENU_CHAINS"
369            );
370        }
371        assert!(Chain::from_menu_index(super::MENU_CHAINS.len()).is_none());
372    }
373
374    #[test]
375    fn menu_chains_sorted_a_to_z_by_label() {
376        let labels: Vec<&str> = super::MENU_CHAINS.iter().map(|(_, label)| *label).collect();
377        let mut sorted = labels.clone();
378        sorted.sort_by_key(|a| a.to_ascii_lowercase());
379        assert_eq!(labels, sorted, "MENU_CHAINS must stay A–Z by display name");
380        for (i, (id, _)) in super::MENU_CHAINS.iter().enumerate() {
381            assert_eq!(Chain::all_ids()[i], *id);
382        }
383    }
384
385    #[test]
386    fn evm_trending_aliases_resolve() {
387        for id in [
388            "robinhood",
389            "hood",
390            "base",
391            "arb",
392            "optimism",
393            "polygon",
394            "avax",
395            "bnb",
396            "monad",
397        ] {
398            let c = Chain::from_id(id).unwrap_or_else(|e| panic!("{id}: {e}"));
399            assert_eq!(c.id(), "evm");
400        }
401    }
402}