vanity_core/chains/
bitcoin_like.rs1use crate::chain::{ChainGrinder, GrindAttempt, KeyExport, KeypairResult};
2use crate::pattern::Pattern;
3use secp256k1::SecretKey;
4
5use super::util::{
6 base58_combinations, build_base58_pattern, expected_from_pattern, grind_secp256k1,
7 matches_pattern, p2pkh_address, secret_from_attempt, BASE58_ALPHABET,
8};
9
10#[derive(Clone)]
11pub struct BitcoinLikeGrinder {
12 pub id: &'static str,
13 pub display_name: &'static str,
14 pub version_byte: u8,
15 pub wallet_hint: &'static str,
16}
17
18impl BitcoinLikeGrinder {
19 pub fn bitcoin() -> Self {
20 Self {
21 id: "btc",
22 display_name: "Bitcoin (P2PKH)",
23 version_byte: 0x00,
24 wallet_hint: "Electrum / Sparrow / hardware wallet WIF import",
25 }
26 }
27
28 pub fn litecoin() -> Self {
29 Self {
30 id: "ltc",
31 display_name: "Litecoin (P2PKH)",
32 version_byte: 0x30,
33 wallet_hint: "Litecoin Core / compatible wallets",
34 }
35 }
36
37 pub fn dogecoin() -> Self {
38 Self {
39 id: "doge",
40 display_name: "Dogecoin",
41 version_byte: 0x1e,
42 wallet_hint: "Dogecoin Core / compatible wallets",
43 }
44 }
45
46 pub fn dash() -> Self {
47 Self {
48 id: "dash",
49 display_name: "Dash (P2PKH)",
50 version_byte: 0x4c,
51 wallet_hint: "Dash Core / compatible wallets",
52 }
53 }
54
55 fn derive(&self, secret: &SecretKey) -> String {
56 p2pkh_address(secret, self.version_byte)
57 }
58}
59
60impl ChainGrinder for BitcoinLikeGrinder {
61 fn id(&self) -> &'static str {
62 self.id
63 }
64
65 fn display_name(&self) -> &'static str {
66 self.display_name
67 }
68
69 fn grind_attempt(&self) -> (String, GrindAttempt) {
70 grind_secp256k1(|secret| self.derive(secret))
71 }
72
73 fn finalize(&self, attempt: GrindAttempt) -> KeypairResult {
74 let secret_bytes = secret_from_attempt(attempt);
75 let secret_key = SecretKey::from_slice(&secret_bytes).expect("valid secp256k1 secret");
76 let address = self.derive(&secret_key);
77
78 KeypairResult {
79 address,
80 exports: vec![
81 KeyExport {
82 label: "Private Key (hex)".into(),
83 value: hex::encode(secret_bytes),
84 hint: Some(self.wallet_hint.into()),
85 },
86 KeyExport {
87 label: "Private Key (WIF)".into(),
88 value: wif_encode(secret_bytes, self.version_byte + 0x80),
89 hint: Some("Standard Bitcoin-family WIF format".into()),
90 },
91 ],
92 }
93 }
94
95 fn build_pattern(
96 &self,
97 prefix: Option<&str>,
98 suffix: Option<&str>,
99 contains: Option<&str>,
100 exact: bool,
101 ) -> Result<Pattern, String> {
102 build_base58_pattern(prefix, suffix, contains, exact, BASE58_ALPHABET, 34)
103 }
104
105 fn expected_attempts(&self, pattern: &Pattern) -> f64 {
106 expected_from_pattern(pattern, base58_combinations)
107 }
108
109 fn matches(&self, address: &str, pattern: &Pattern) -> bool {
110 matches_pattern(address, pattern, false)
111 }
112
113 fn supports_exact_case(&self) -> bool {
114 true
115 }
116
117 fn pattern_hint(&self) -> &'static str {
118 "Base58 characters (no 0, O, I, l)."
119 }
120}
121
122fn wif_encode(secret: [u8; 32], version: u8) -> String {
123 let mut payload = vec![version];
124 payload.extend_from_slice(&secret);
125 payload.push(0x01); super::util::base58_check_encode_raw(&payload)
127}
128
129#[cfg(test)]
130mod tests {
131 use super::BitcoinLikeGrinder;
132 use crate::chains::util::{p2pkh_address, random_secp256k1_secret};
133
134 #[test]
135 fn bitcoin_address_starts_with_1() {
136 let secret = random_secp256k1_secret();
137 let addr = p2pkh_address(&secret, BitcoinLikeGrinder::bitcoin().version_byte);
138 assert!(addr.starts_with('1'));
139 }
140
141 #[test]
142 fn litecoin_address_starts_with_l() {
143 let secret = random_secp256k1_secret();
144 let addr = p2pkh_address(&secret, BitcoinLikeGrinder::litecoin().version_byte);
145 assert!(addr.starts_with('L'));
146 }
147
148 #[test]
149 fn dogecoin_address_starts_with_d() {
150 let secret = random_secp256k1_secret();
151 let addr = p2pkh_address(&secret, BitcoinLikeGrinder::dogecoin().version_byte);
152 assert!(addr.starts_with('D'));
153 }
154
155 #[test]
156 fn dash_address_starts_with_x() {
157 let secret = random_secp256k1_secret();
158 let addr = p2pkh_address(&secret, BitcoinLikeGrinder::dash().version_byte);
159 assert!(addr.starts_with('X'));
160 }
161}