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