vanity_core/chains/
multiversx.rs1use super::util::Keypair;
2use crate::chain::{ChainGrinder, GrindAttempt, KeyExport, KeypairResult};
3use crate::pattern::Pattern;
4use bech32::{encode, Bech32, Hrp};
5
6use super::util::{
7 bech32_combinations, build_base58_pattern, expected_from_pattern, grind_ed25519,
8 keypair_from_secret, matches_pattern, secret_from_attempt, BECH32_CHARSET,
9};
10
11#[derive(Clone, Default)]
12pub struct MultiversXGrinder;
13
14impl MultiversXGrinder {
15 fn encode_erd(pubkey: &[u8; 32]) -> String {
17 let hrp = Hrp::parse("erd").expect("valid hrp");
18 encode::<Bech32>(hrp, pubkey).expect("valid bech32 address")
19 }
20
21 fn derive_address(keypair: &Keypair) -> String {
22 Self::encode_erd(&keypair.pubkey().to_bytes())
23 }
24}
25
26impl ChainGrinder for MultiversXGrinder {
27 fn id(&self) -> &'static str {
28 "erd"
29 }
30
31 fn display_name(&self) -> &'static str {
32 "MultiversX (erd1)"
33 }
34
35 fn grind_attempt(&self) -> (String, GrindAttempt) {
36 grind_ed25519(Self::derive_address)
37 }
38
39 fn finalize(&self, attempt: GrindAttempt) -> KeypairResult {
40 let secret_bytes = secret_from_attempt(attempt);
41 let keypair = keypair_from_secret(secret_bytes);
42 let address = Self::derive_address(&keypair);
43
44 KeypairResult {
45 address,
46 exports: vec![KeyExport {
47 label: "Private Key (hex)".into(),
48 value: hex::encode(secret_bytes),
49 hint: Some("xPortal / MultiversX Wallet (ed25519 seed hex)".into()),
50 }],
51 }
52 }
53
54 fn build_pattern(
55 &self,
56 prefix: Option<&str>,
57 suffix: Option<&str>,
58 contains: Option<&str>,
59 exact: bool,
60 ) -> Result<Pattern, String> {
61 let mut pattern =
62 build_base58_pattern(prefix, suffix, contains, exact, BECH32_CHARSET, 62)?;
63 if pattern.has_prefix() && !pattern.prefix.starts_with("erd") {
64 pattern.prefix = format!("erd1{}", pattern.prefix);
65 pattern.prefix_match = if pattern.ignore_case {
66 pattern.prefix.to_ascii_lowercase()
67 } else {
68 pattern.prefix.clone()
69 };
70 }
71 pattern.set_fixed_prefix("erd1");
72 Ok(pattern)
73 }
74
75 fn expected_attempts(&self, pattern: &Pattern) -> f64 {
76 expected_from_pattern(pattern, |p| {
77 let data = p
78 .strip_prefix("erd1")
79 .or_else(|| p.strip_prefix("erd"))
80 .unwrap_or(p);
81 bech32_combinations(data)
82 })
83 }
84
85 fn matches(&self, address: &str, pattern: &Pattern) -> bool {
86 matches_pattern(address, pattern, true)
87 }
88
89 fn supports_exact_case(&self) -> bool {
90 false
91 }
92
93 fn pattern_hint(&self) -> &'static str {
94 "Bech32 (erd1… = ed25519 pubkey). Prefer --suffix; addresses always start with erd1."
95 }
96}
97
98#[cfg(test)]
99mod tests {
100 use super::MultiversXGrinder;
101 use crate::chain::ChainGrinder;
102
103 #[test]
104 fn multiversx_address_starts_with_erd1() {
105 let g = MultiversXGrinder;
106 let (addr, _) = g.grind_attempt();
107 assert!(addr.starts_with("erd1"));
108 assert_eq!(addr.len(), 62);
109 }
110
111 #[test]
113 fn erd_encoding_matches_alice_known_vector() {
114 let pubkey: [u8; 32] =
115 hex::decode("0139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1")
116 .unwrap()
117 .try_into()
118 .unwrap();
119 let addr = MultiversXGrinder::encode_erd(&pubkey);
120 assert_eq!(
121 addr,
122 "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"
123 );
124 }
125}